コード例 #1
0
ファイル: PlatformConfig.cs プロジェクト: Hengle/SharpLua-1
        // Converts a IEEE754 single number to a 4-byte little-endian string
        static string toSingle(double x)
        {
            int sign = 0;

            if (x < 0)
            {
                sign = 1;
                x    = -x;
            }
            Bit.FRexpResult fr = Bit.frexp(x);
            if (x == 0) // zero
            {
                fr.mantissa = 0;
                fr.exponent = 0;
            }
            else
            {
                fr.mantissa = (fr.mantissa * 2 - 1) * Bit.ldexp(0.5, 24);
                fr.exponent = fr.exponent + 126;
            }
            string v = "";
            char   b = '\0';

            grab_byte(fr.mantissa, out x, out b);
            v = v + b; // 7:0
            grab_byte(x, out x, out b);
            v = v + b; // 15:8
            grab_byte(fr.exponent * 128 + x, out x, out b);
            v = v + b; // 23:16
            grab_byte(sign * 128 + x, out x, out b);
            v = v + b; // 31:24
            return(v);
        }
コード例 #2
0
ファイル: PlatformConfig.cs プロジェクト: iamjuarez/SharpLua
// WARNING this will fail for large long longs (64-bit numbers)
// because long longs exceeds the precision of doubles.
// ConvertFrom["long long"] = ConvertFrom["int"]

// Converts a IEEE754 double number to an 8-byte little-endian string
        static string toDouble(double x)
        {
            int sign = 0;

            if (x < 0)
            {
                sign = 1;
                x    = -x;
            }
            Bit.FRexpResult fr = Bit.frexp(x);
            if (x == 0)// zero
            {
                fr.mantissa = 0;
                fr.exponent = 0;
            }
            else
            {
                fr.mantissa = (fr.mantissa * 2 - 1) * Bit.ldexp(0.5, 53);
                fr.exponent = fr.exponent + 1022;
            }
            string v = "";
            char   b = '\0';// convert to bytes

            x = fr.mantissa;
            for (int i = 0; i < 6; i++)
            {
                x = Math.Floor(x / 256);
                b = (char)(Math.Floor(x) % 256);
                v = v + b;
                // 47:0
            }
            x  = Math.Floor((fr.exponent * 16 + x) / 256);
            b  = (char)(Math.Floor((fr.exponent * 16 + x)) % 256);
            v += b;
            // 55:48
            x  = Math.Floor((sign * 128 + x) / 256);
            b  = (char)(Math.Floor((sign * 128 + x) % 256));
            v += b;
            // 63:56
            return(v);
        }
コード例 #3
0
ファイル: PlatformConfig.cs プロジェクト: Hengle/SharpLua-1
        // WARNING this will fail for large long longs (64-bit numbers)
        // because long longs exceeds the precision of doubles.
        // ConvertFrom["long long"] = ConvertFrom["int"]

        // Converts a IEEE754 double number to an 8-byte little-endian string
        static string toDouble(double x)
        {
            int sign = 0;

            if (x < 0)
            {
                sign = 1;
                x    = -x;
            }
            Bit.FRexpResult fr = Bit.frexp(x);
            if (x == 0)
            {
                fr.mantissa = 0;
                fr.exponent = 0;
            }
            else
            {
                fr.mantissa = (fr.mantissa * 2 - 1) * Bit.ldexp(0.5, 53);
                fr.exponent = fr.exponent + 1022;
            }
            string v = "";
            char   b = '\0';

            x = fr.mantissa;
            for (int i = 0; i < 6; i++)
            {
                grab_byte(x, out x, out b);
                v += b;
            }
            grab_byte(fr.exponent * 16 + x, out x, out b);
            v += b;
            grab_byte(sign * 128 + x, out x, out b);
            v += b;
            return(v);


            /*
             * int sign = 0;
             * if (x < 0)
             * {
             *  sign = 1;
             *  x = -x;
             * }
             * Bit.FRexpResult fr = Bit.frexp(x);
             * if (x == 0)// zero
             * {
             *  fr.mantissa = 0;
             *  fr.exponent = 0;
             * }
             * else
             * {
             *  fr.mantissa = (fr.mantissa * 2 - 1) * Bit.ldexp(0.5, 53);
             *  fr.exponent = fr.exponent + 1022;
             * }
             * string v = "";
             * char b = '\0';// convert to bytes
             * x = fr.mantissa;
             * for (int i = 0; i < 6; i++)
             * {
             *  x = Math.Floor(x / 256);
             *  b = (char)(Math.Floor(x) % 256);
             *  v = v + b;
             *  // 47:0
             * }
             * x = Math.Floor((fr.exponent * 16 + x) / 256);
             * b = (char)(Math.Floor((fr.exponent * 16 + x)) % 256);
             * v += b;
             * // 55:48
             * x = Math.Floor((sign * 128 + x) / 256);
             * b = (char)(Math.Floor((sign * 128 + x) % 256));
             * v += b;
             * // 63:56
             * return v;
             */
        }