Ejemplo n.º 1
0
        private static int Math_Frexp(ILuaState lua)
        {
            double d = lua.L_CheckNumber(1);

            // Translate the double into sign, exponent and mantissa.
            long bits = BitConverter.DoubleToInt64Bits(d);
            // Note that the shift is sign-extended, hence the test against -1 not 1
            bool negative = (bits < 0);
            int  exponent = (int)((bits >> 52) & 0x7ffL);
            long mantissa = bits & 0xfffffffffffffL;

            // Subnormal numbers; exponent is effectively one higher,
            // but there's no extra normalisation bit in the mantissa
            if (exponent == 0)
            {
                exponent++;
            }
            // Normal numbers; leave exponent as it is but add extra
            // bit to the front of the mantissa
            else
            {
                mantissa = mantissa | (1L << 52);
            }

            // Bias the exponent. It's actually biased by 1023, but we're
            // treating the mantissa as m.0 rather than 0.m, so we need
            // to subtract another 52 from it.
            exponent -= 1075;

            if (mantissa == 0)
            {
                lua.PushNumber(0.0);
                lua.PushNumber(0.0);
                return(2);
            }

            /* Normalize */
            while ((mantissa & 1) == 0)
            {                /*  i.e., Mantissa is even */
                mantissa >>= 1;
                exponent++;
            }

            double m = (double)mantissa;
            double e = (double)exponent;

            while (m >= 1)
            {
                m /= 2.0;
                e += 1.0;
            }

            if (negative)
            {
                m = -m;
            }
            lua.PushNumber(m);
            lua.PushNumber(e);
            return(2);
        }
Ejemplo n.º 2
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="num"></param>
        public void Add(double num)
        {
            long numBits = BitConverter.DoubleToInt64Bits(num);

            if (isFirst)
            {
                commonBits    = numBits;
                commonSignExp = SignExpBits(commonBits);
                isFirst       = false;
                return;
            }

            long numSignExp = SignExpBits(numBits);

            if (numSignExp != commonSignExp)
            {
                commonBits = 0;
                return;
            }
            commonMantissaBitsCount = NumCommonMostSigMantissaBits(commonBits, numBits);
            commonBits = ZeroLowerBits(commonBits, 64 - (12 + commonMantissaBitsCount));
        }