public void Overlapping_AreNot_NonOverlapping()
        {
            var d1 = DoubleConverter.FromFloatingPointBinaryString("101");
            var d2 = DoubleConverter.FromFloatingPointBinaryString("10");

            Assert.IsFalse(ExpansionExtensions.AreNonOverlapping(d1, d2));
        }
        public void NonOverlapping_Are_NonOverlapping()
        {
            var d1 = DoubleConverter.FromFloatingPointBinaryString("1100");
            var d2 = DoubleConverter.FromFloatingPointBinaryString("-10.1");

            Assert.IsTrue(ExpansionExtensions.AreNonOverlapping(d1, d2));
        }
        public void StronglyNonOverlapping()
        {
            // Two examples that are strongly nonoverlapping (S.p12)
            var sn1 = new[] { DoubleConverter.FromFloatingPointBinaryString("11000"),
                              DoubleConverter.FromFloatingPointBinaryString("11") };

            var sn2 = new[] { DoubleConverter.FromFloatingPointBinaryString("10000"),
                              DoubleConverter.FromFloatingPointBinaryString("1000"),
                              DoubleConverter.FromFloatingPointBinaryString("10"),
                              DoubleConverter.FromFloatingPointBinaryString("1") };

            Assert.IsTrue(sn1.IsStronglyNonOverlapping());
            Assert.IsTrue(sn2.IsStronglyNonOverlapping());

            // Two examples that are _not_ strongly nonoverlapping (S.p12)
            var nsn1 = new[] { DoubleConverter.FromFloatingPointBinaryString("11100"),
                               DoubleConverter.FromFloatingPointBinaryString("11") };

            var nsn2 = new[] { DoubleConverter.FromFloatingPointBinaryString("100"),
                               DoubleConverter.FromFloatingPointBinaryString("10"),
                               DoubleConverter.FromFloatingPointBinaryString("1") };

            Assert.IsFalse(nsn1.IsStronglyNonOverlapping());
            Assert.IsFalse(nsn2.IsStronglyNonOverlapping());
        }
        public void BitWidth_Large()
        {
            double d     = DoubleConverter.FromFloatingPointBinaryString("1010101" + '0'.Repeat(200));
            int    width = d.BitWidth();

            Assert.AreEqual(7, width);
        }
Ejemplo n.º 5
0
        public void ShowDoubleRoundingPriestExplicit()
        {
            double a          = DoubleConverter.FromFloatingPointBinaryString("1" + '0'.Repeat(51) + "1"); // 100....00001 (53-bits wide)
            double b          = DoubleConverter.FromFloatingPointBinaryString("0.0" + '1'.Repeat(53));     //            0.0111...111 (53 1's)
            double expected53 = a;
            double expected64 = a + 1.0;                                                                   // The point is that this is different to expceted53.

            Debug.Print(DoubleConverter.ToFloatingPointBinaryString(a));
            Debug.Print(DoubleConverter.ToFloatingPointBinaryString(b));
            Debug.Print(DoubleConverter.ToFloatingPointBinaryString(expected53));
            Debug.Print(DoubleConverter.ToFloatingPointBinaryString(expected64));

            // Set Fpu to 53-bit precision (the default)
            FpuControl.SetState((uint)FpuControl.PrecisionControl.Double53Bits, FpuControl.Mask.PrecisionControl);

            double result53 = a + b;

            Debug.Print(DoubleConverter.ToFloatingPointBinaryString(result53));
            Assert.AreEqual(expected53, result53);

            // Explicit rounding makes no difference here (since we're in Double53bits precision FPU mode)
            result53 = (double)(a + b);
            Debug.Print(DoubleConverter.ToFloatingPointBinaryString(result53));
            Assert.AreEqual(expected53, result53);

            // Set Fpu to 64-bit precision (extended precision)
            FpuControl.SetState((uint)FpuControl.PrecisionControl.Extended64Bits, FpuControl.Mask.PrecisionControl);
            double result64 = (double)(a + b);

            Debug.Print(DoubleConverter.ToFloatingPointBinaryString(result64));
            Assert.AreEqual(expected64, result64);
        }
        public void OptimTest()
        {
            // 53 ones
            string a_s = "11111111111111111111111111111111111111111111111111110.0";
            double a   = DoubleConverter.FromFloatingPointBinaryString("11111111111111111111111111111111111111111111111111110.0");
            double b   = DoubleConverter.FromFloatingPointBinaryString("-11111111111111111111111111111111111111111111111111110.0");
            double c   = DoubleConverter.FromFloatingPointBinaryString("00000000000000000000000000000000000000000000000000000.01");

            string a_s2 = DoubleConverter.ToFloatingPointBinaryString(9.0071992547409900e+0015);

            Assert.AreEqual(a_s, a_s2);

            bool ok = Test(a, b, c);

            Assert.IsTrue(ok);

            double acc  = TestAcc(a, b, c);
            double acc2 = TestAcc(a, b, c);

            if (acc != acc2)
            {
                Assert.Fail();
            }
            Assert.AreEqual(0.0, acc);
            Assert.AreEqual(0.0, acc2);

            double tst2 = Test2(a, b, c);

            if (tst2 != 0.0)
            {
                Assert.Fail();
            }
        }
Ejemplo n.º 7
0
        public void ShowDoubleRounding()
        {
            double a          = DoubleConverter.FromFloatingPointBinaryString('1'.Repeat(52) + "0"); // 111....11110
            double b          = DoubleConverter.FromFloatingPointBinaryString("0.100000000001");     // 000....00000.100000000001
            double expected53 = DoubleConverter.FromFloatingPointBinaryString('1'.Repeat(52) + "1"); // 111....11111
            double expected64 = a;

            // Set Fpu to 53-bit precision (the default)
            FpuControl.SetState((uint)FpuControl.PrecisionControl.Double53Bits, FpuControl.Mask.PrecisionControl);

            double result53 = a + b;

            Assert.AreEqual(expected53, result53);

            // Set Fpu to 64-bit precision (extended precision)
            FpuControl.SetState((uint)FpuControl.PrecisionControl.Extended64Bits, FpuControl.Mask.PrecisionControl);
            double result64 = (double)(a + b);

            Assert.AreEqual(expected64, result64);

            double result64_0 = (a + b) - a;

            Assert.AreNotEqual(0.0, result64_0);
            Assert.AreNotEqual(b, result64_0);
            Assert.AreEqual(0.5, result64_0);       // 000....00000.1
        }
        public void BitWidth_Simple()
        {
            double d     = DoubleConverter.FromFloatingPointBinaryString("-0.001010101000");
            int    width = d.BitWidth();

            Assert.AreEqual(7, width);
        }
Ejemplo n.º 9
0
        public void Split_Simple()
        {
            double d = DoubleConverter.FromFloatingPointBinaryString("-0.001010101000");
            double ahi;
            double alo;

            Split_Checked(d, out ahi, out alo);
        }
        // Here we add an explicit cast, which ensures that (a + b) is evaluated to 64-bits before continuing
        public double TestCalcSafe()
        {
            double a = DoubleConverter.FromFloatingPointBinaryString("11111111111111111111111111111111111111111111111111110.0");
            double b = DoubleConverter.FromFloatingPointBinaryString("00000000000000000000000000000000000000000000000000000.1");

            double result = (double)(a + b) - a;

            return(result);
        }
Ejemplo n.º 11
0
        public void Split_FullPrecision()
        {
            var    dstr = "1." + '1'.Repeat(52);
            double d    = DoubleConverter.FromFloatingPointBinaryString(dstr);

            var dstr2 = d.ToFloatingPointBinaryString();

            NUnit.Framework.Assert.AreEqual(dstr, dstr2);

            double ahi;
            double alo;

            Split_Checked(d, out ahi, out alo);
        }
Ejemplo n.º 12
0
        public void FastTwoSum_OppositeSign()
        {
            // Adapted example from Shewchuk p. 7 - Figure 4 - expanded to double
            // We expect a+b=x+y
            double a         = DoubleConverter.FromFloatingPointBinaryString("1" + '0'.Repeat(51) + "10");      //  100...0010
            double b         = DoubleConverter.FromFloatingPointBinaryString("-" + '1'.Repeat(49) + "1011");    //  -11...1011
            double x_correct = DoubleConverter.FromFloatingPointBinaryString("111");                            //         111
            double y_correct = 0.0;

            double x; double y;

            EA.FastTwoSum(a, b, out x, out y);

            Assert.AreEqual(x_correct, x);
            Assert.AreEqual(y_correct, y);
        }
Ejemplo n.º 13
0
        public void Split_FullPrecisionLarge()
        {
            // -1111...(53)...11111000000....000000000
            var    dstr = "-1" + '1'.Repeat(52) + '0'.Repeat(200);
            double d    = DoubleConverter.FromFloatingPointBinaryString(dstr);

            var dstr2 = d.ToFloatingPointBinaryString();

            NUnit.Framework.Assert.AreEqual(DoubleConverter.FromFloatingPointBinaryString(dstr),
                                            DoubleConverter.FromFloatingPointBinaryString(dstr2));

            double ahi;
            double alo;

            Split_Checked(d, out ahi, out alo);
        }
Ejemplo n.º 14
0
        public void TwoSum_Small_a()
        {
            // This is an example that FastTwoSum gets wrong (|a|<|b|), but TwoSum does right
            // Shewchuck p. 8, Figure 5
            double a         = DoubleConverter.FromFloatingPointBinaryString('1'.Repeat(51) + ".11");       //    111...111.11
            double b         = DoubleConverter.FromFloatingPointBinaryString("110" + '0'.Repeat(49) + "1"); //  11000...001
            double x_correct = DoubleConverter.FromFloatingPointBinaryString("1" + '0'.Repeat(53));         // 100000...000
            double y_correct = DoubleConverter.FromFloatingPointBinaryString("0.11");                       //            0.11

            double x; double y;

            EA.TwoSum(a, b, out x, out y);

            Assert.AreEqual(x_correct, x);
            Assert.AreEqual(y_correct, y);
        }
Ejemplo n.º 15
0
        public void FastTwoSum_SameSign()
        {
            // Example ismilar to Shewchuk p. 7 - Figure 3 (expanded for double)
            // We expect a+b=x+y

            double a         = DoubleConverter.FromFloatingPointBinaryString('1'.Repeat(53) + "00");           //  1111...1100
            double b         = DoubleConverter.FromFloatingPointBinaryString("1001");                          //         1001
            double x_correct = DoubleConverter.FromFloatingPointBinaryString("1" + '0'.Repeat(51) + "1000");   // 1000...00100
            double y_correct = DoubleConverter.FromFloatingPointBinaryString("-11");                           //         - 11

            double x; double y;

            EA.FastTwoSum(a, b, out x, out y);

            Assert.AreEqual(x_correct, x);
            Assert.AreEqual(y_correct, y);
        }
Ejemplo n.º 16
0
        public void RoundToEvenTiebreaking()
        {
            var d1 = DoubleConverter.FromFloatingPointBinaryString("100000000000000000000000000000000000000000000000000010");
            var d2 = DoubleConverter.FromFloatingPointBinaryString("1");
            // Closest even to ~~~011
            var d3    = DoubleConverter.FromFloatingPointBinaryString("100000000000000000000000000000000000000000000000000100");
            var dtest = (double)d1 + d2;
            var stest = dtest.ToFloatingPointBinaryString();

            Assert.AreEqual(d3, dtest);

            // Closest even to ~~~001
            var d4     = DoubleConverter.FromFloatingPointBinaryString("100000000000000000000000000000000000000000000000000000");
            var dtest2 = (double)d1 - d2;
            var stest2 = dtest2.ToFloatingPointBinaryString();

            Assert.AreEqual(d4, dtest2);
        }
        public void TestPrecision53()
        {
            double a          = DoubleConverter.FromFloatingPointBinaryString('1'.Repeat(52) + "0"); // 111....11110
            double b          = DoubleConverter.FromFloatingPointBinaryString("0.1");                // 000....00000.1
            double expected53 = a;

            FpuControl.SetState((uint)FpuControl.PrecisionControl.Double53Bits, FpuControl.Mask.PrecisionControl);
            double result53 = (double)(a + b);

            Assert.AreEqual(expected53, result53);

            double result53_0 = (a + b) - a;

            Assert.AreEqual(0.0, result53_0);

            double result53_X = ((double)(a + b)) - a;

            Assert.AreEqual(0.0, result53_X);
        }
        public void TestPrecision64()
        {
            double a          = DoubleConverter.FromFloatingPointBinaryString('1'.Repeat(52) + "0"); // 111....11110
            double b          = DoubleConverter.FromFloatingPointBinaryString("0.1");                // 000....00000.1
            double expected64 = a;

            FpuControl.SetState((uint)FpuControl.PrecisionControl.Extended64Bits, FpuControl.Mask.PrecisionControl);
            double result64 = (double)(a + b);

            Assert.AreEqual(expected64, result64);

            double result64_b = (a + b) - a;

            Assert.AreEqual(b, result64_b);

            double result64_0 = ((double)(a + b)) - a;

            Assert.AreNotEqual(b, result64_0);
            Assert.AreEqual(0.0, result64_0);
        }
Ejemplo n.º 19
0
        public void FloatingPointBinaryString_MatchesValue()
        {
            var tests = new[]
            {
                Tuple.Create(4.0, "100.0"),
                Tuple.Create(0.25, "0.01"),
                Tuple.Create(0.0, "0.0"),
                Tuple.Create(-2.5, "-10.1"),
                Tuple.Create(double.MaxValue, "1111111111111111111111111111111111111111111111111111100000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000.0"),
                Tuple.Create(double.MinValue, "-1111111111111111111111111111111111111111111111111111100000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000.0"),
                Tuple.Create(double.Epsilon, "0.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001"),
                Tuple.Create(DoubleEpsilonSplitter().Item1, "0.00000000000000000000000000000000000000000000000000001"), // biggest exponent of 2 s.t. 1.0 + eps = 1.0
            };

            foreach (var t in tests)
            {
                string r = DoubleConverter.ToFloatingPointBinaryString(t.Item1);
                Assert.AreEqual(t.Item2, r);
                double d = DoubleConverter.FromFloatingPointBinaryString(r);
                Assert.AreEqual(t.Item1, d);
            }
        }