コード例 #1
0
        private static void TestNumber(double value)
        {
            var    expected = GetBytes(value);
            double result   = IEEEFloat.ToDouble(expected, 0);
            var    actual   = GetBytes(result);

            AreEqual(expected, actual);
        }
コード例 #2
0
        private static void TestNumber(float value)
        {
            var   expected = GetBytes(value);
            float result   = IEEEFloat.ToFloat(expected, 0);
            var   actual   = GetBytes(result);

            AreEqual(expected, actual);
        }
コード例 #3
0
        public void TestDoublePrecision()
        {
            TestNumber(0.0);
            //TestNumber(-0.0);
            TestNumber(4.3);
            TestNumber(Math.PI);
            TestNumber(double.NegativeInfinity);
            TestNumber(double.PositiveInfinity);
            TestNumber(double.NaN);

            int start = 120; // pick val < 127
            int end   = 135; // pick val > 128

            var buf = new byte[8];

            for (int b0 = start; b0 < end; ++b0)
            {
                for (int b1 = start; b1 < end; ++b1)
                {
                    for (int b2 = start; b2 < end; ++b2)
                    {
                        for (int b3 = start; b3 < end; ++b3)
                        {
                            buf[0] = (byte)b0;
                            buf[1] = (byte)b1;
                            buf[2] = (byte)b2;
                            buf[3] = (byte)b3;
                            buf[4] = (byte)b0;
                            buf[5] = (byte)b1;
                            buf[6] = (byte)b2;
                            buf[7] = (byte)b3;
                            double val = IEEEFloat.ToDouble(buf, 0);
                            if (!double.IsNaN(val))
                            {
                                var buf2 = GetBytes(val);
                                AreEqual(buf, buf2);
                            }
                        }
                    }
                }
            }
        }
コード例 #4
0
        public void TestSinglePrecision()
        {
            TestNumber(0f);
            //TestNumber(-0f);
            TestNumber(4.3f);
            TestNumber(float.NegativeInfinity);
            TestNumber(float.PositiveInfinity);
            TestNumber(float.NaN);

            int start = 120; // pick val < 127
            int end   = 135; // pick val > 128

            var buf = new byte[4];

            for (int b0 = start; b0 < end; ++b0)
            {
                for (int b1 = start; b1 < end; ++b1)
                {
                    for (int b2 = start; b2 < end; ++b2)
                    {
                        for (int b3 = start; b3 < end; ++b3)
                        {
                            buf[0] = (byte)b0;
                            buf[1] = (byte)b1;
                            buf[2] = (byte)b2;
                            buf[3] = (byte)b3;
                            float val = IEEEFloat.ToFloat(buf, 0);
                            if (!float.IsNaN(val))
                            {
                                var buf2 = GetBytes(val);
                                AreEqual(buf, buf2);
                            }
                        }
                    }
                }
            }
        }