Exemplo n.º 1
0
        public void Construction_WhereNegativeFloatingPointNumber_ShouldBeNagative_Test()
        {
            double d      = -100.005;
            var    result = new ExpandedDouble(d);

            result.IsNegative.Should().Be(true, "-100.005 is a negative number");
        }
Exemplo n.º 2
0
        public void Construction_WherePositiveInfinity_ShouldBePositive_Test()
        {
            double d      = double.PositiveInfinity;
            var    result = new ExpandedDouble(d);

            result.IsNegative.Should().Be(false, "+inf is a positive number");
        }
Exemplo n.º 3
0
        public void Construction_WhereNegativeWholeNumber_ShouldBeNagative_Test()
        {
            double d      = -100;
            var    result = new ExpandedDouble(d);

            result.IsNegative.Should().Be(true, "-100 is a negative number");
        }
Exemplo n.º 4
0
        public void IsQuietNaN_WithSignallingNaN_ShouldBeTrue_Test()
        {
            double d      = 0d / 0d;
            var    result = new ExpandedDouble(d);

            result.IsQuietNaN.Should().Be(true);
        }
Exemplo n.º 5
0
        public void MantissaAsStored_WhereExponentIsNotZero_ShouldBeCorrect_Test()
        {
            double d      = 75.5;
            var    result = new ExpandedDouble(d);

            result.MantissaAsStored.Should().Be(809240558043136, "The mantissa of 75.5 should be 809240558043136 as it is stored in binary");
        }
Exemplo n.º 6
0
        public void Mantissa_WhereExponentIsNotZero_ShouldBeCorrectWithImpliedLeadingOne_Test()
        {
            double d      = 75.5;
            var    result = new ExpandedDouble(d);

            result.Mantissa.Should().Be(809240558043137, "The mantissa of 75.5 should be 809240558043137 with the leading one added");
        }
Exemplo n.º 7
0
        public void Exponent_WhereExponentIsNotZero_ShouldBeCorrect_Test()
        {
            double d      = 75.5;
            var    result = new ExpandedDouble(d);

            result.Exponent.Should().Be(3, "The exponent of 75.5 should be 3 after removing the bias");
        }
Exemplo n.º 8
0
        public void ExponentBitsAsStored_WhereExponentIsNotZero_ShouldBeCorrect_Test()
        {
            double d      = 75.5;
            var    result = new ExpandedDouble(d);

            result.ExponentBitsAsStored.Should().BeEquivalentTo(1, 0, 0, 0, 0, 0, 0, 0, 1, 0, 1);
        }
Exemplo n.º 9
0
        public void IsNegativeInfinity_WithOne_ShouldBeFalse_Test()
        {
            double d      = 1d;
            var    result = new ExpandedDouble(d);

            result.IsNegativeInfinity.Should().Be(false);
        }
Exemplo n.º 10
0
        public void ExponentAsStored_WhereExponentIsNotZero_ShouldBeCorrect_Test()
        {
            double d      = 75.5;
            var    result = new ExpandedDouble(d);

            result.ExponentAsStored.Should().Be(1029, "The exponent of 75.5 should be 1029 before removing the bias");
        }
Exemplo n.º 11
0
        public void IsNegativeInfinity_WithNegativeInfinity_ShouldBeTrue_Test()
        {
            double d      = double.NegativeInfinity;
            var    result = new ExpandedDouble(d);

            result.IsNegativeInfinity.Should().Be(true);
        }
Exemplo n.º 12
0
        public void IsPositiveInfinity_WithNegativeInfinity_ShouldBeFalse_Test()
        {
            double d      = double.NegativeInfinity;
            var    result = new ExpandedDouble(d);

            result.IsPositiveInfinity.Should().Be(false);
        }
Exemplo n.º 13
0
        public void IsInfinite_WithPositiveInfinity_ShouldBeTrue_Test()
        {
            double d      = double.PositiveInfinity;
            var    result = new ExpandedDouble(d);

            result.IsInifinite.Should().Be(true);
        }
Exemplo n.º 14
0
        public void Construction_WhereNegativeInfinity_ShouldBeNagative_Test()
        {
            double d      = double.NegativeInfinity;
            var    result = new ExpandedDouble(d);

            result.IsNegative.Should().Be(true, "-inf is a negative number");
        }
Exemplo n.º 15
0
        public void Construction_WherePositiveFloatingPointNumber_ShouldBePositive_Test()
        {
            double d      = 100.005;
            var    result = new ExpandedDouble(d);

            result.IsNegative.Should().Be(false, "100.005 is a positive number");
        }
Exemplo n.º 16
0
        public void IsSignnallingNaN_WithSignallingNaN_ShouldBeTrue_Test()
        {
            double d      = 0d / 0d; // Produces quite NaN
            var    result = new ExpandedDouble(d);

            result.IsSignallingNaN.Should().Be(true);
        }
Exemplo n.º 17
0
        public void IsNaN_WithOne_ShouldBeFalse_Test()
        {
            double d      = 1d;
            var    result = new ExpandedDouble(d);

            result.IsNaN.Should().Be(false);
        }
Exemplo n.º 18
0
        private static void CheckNormaliseBaseTenResult(ExpandedDouble orig, NormalisedDecimal result)
        {
            String     sigDigs = result.GetSignificantDecimalDigits();
            BigInteger frac    = orig.GetSignificand();

            while (frac.BitLength() + orig.GetBinaryExponent() < 200)
            {
                frac = frac * (BIG_POW_10);
            }
            int binaryExp = orig.GetBinaryExponent() - orig.GetSignificand().BitLength();

            String origDigs = (frac << (binaryExp + 1)).ToString(10);

            if (!origDigs.StartsWith(sigDigs))
            {
                throw new AssertionException("Expected '" + origDigs + "' but got '" + sigDigs + "'.");
            }

            double     dO       = Double.Parse("0." + origDigs.Substring(sigDigs.Length));
            double     d1       = Double.Parse(result.GetFractionalPart().ToString());
            BigInteger subDigsO = new BigInteger((int)(dO * 32768 + 0.5));
            BigInteger subDigsB = new BigInteger((int)(d1 * 32768 + 0.5));

            if (subDigsO.Equals(subDigsB))
            {
                return;
            }
            BigInteger diff = (subDigsB - subDigsO).Abs();

            if (diff.IntValue() > 100)
            {
                // 100/32768 ~= 0.003
                throw new AssertionException("minor mistake");
            }
        }
Exemplo n.º 19
0
        public void IsNaN_WithNaN_ShouldBeTrue_Test()
        {
            double d      = double.NaN;
            var    result = new ExpandedDouble(d);

            result.IsNaN.Should().Be(true);
        }
Exemplo n.º 20
0
        public void Construction_WhereNaN_ShouldBePositive_Test()
        {
            // Todo: We should maybe throw an exception when testing the sign of NaN instead
            double d      = double.NaN;
            var    result = new ExpandedDouble(d);

            result.IsNegative.Should().Be(true, "-NaN is a negative number");
        }
Exemplo n.º 21
0
 public static String GetBaseDecimal(ExpandedDouble hd)
 {
     /*int gg = 64 - hd.GetBinaryExponent() - 1;
      * BigDecimal bd = new BigDecimal(hd.GetSignificand()).divide(new BigDecimal(BigInteger.ONE<<gg));
      * int excessPrecision = bd.precision() - 23;
      * if (excessPrecision > 0)
      * {
      *  bd = bd.SetScale(bd.scale() - excessPrecision, BigDecimal.ROUND_HALF_UP);
      * }
      * return bd.unscaledValue().ToString();*/
     throw new NotImplementedException("This Method need BigDecimal class");
 }
Exemplo n.º 22
0
        public void TestSubnormal()
        {
            ExpandedDouble hd = new ExpandedDouble(0x0000000000000001L);

            if (hd.GetBinaryExponent() == -1023)
            {
                throw new AssertionException("identified bug - subnormal numbers not decoded properly");
            }
            Assert.AreEqual(-1086, hd.GetBinaryExponent());
            BigInteger frac = hd.GetSignificand();

            Assert.AreEqual(64, frac.BitLength());
            Assert.AreEqual(1, frac.BitCount());
        }
Exemplo n.º 23
0
        public void TestNegative()
        {
            ExpandedDouble hd = new ExpandedDouble(unchecked ((long)0xC010000000000000L));

            if (hd.GetBinaryExponent() == -2046)
            {
                throw new AssertionException("identified bug - sign bit not masked out of exponent");
            }
            Assert.AreEqual(2, hd.GetBinaryExponent());
            BigInteger frac = hd.GetSignificand();

            Assert.AreEqual(64, frac.BitLength());
            Assert.AreEqual(1, frac.BitCount());
        }
Exemplo n.º 24
0
 public static BigInteger GetNearby(ExpandedDouble hd, int offset)
 {
     return(GetNearby(hd.GetSignificand(), hd.GetBinaryExponent(), offset));
 }
Exemplo n.º 25
0
        public static bool ConfirmRoundTrip(int i, long rawBitsA)
        {
            double a = BitConverter.Int64BitsToDouble(rawBitsA);

            if (a == 0.0)
            {
                // Can't represent 0.0 or -0.0 with NormalisedDecimal
                return(true);
            }
            ExpandedDouble    ed1;
            NormalisedDecimal nd2;
            ExpandedDouble    ed3;

            try
            {
                ed1 = new ExpandedDouble(rawBitsA);
                nd2 = ed1.NormaliseBaseTen();
                CheckNormaliseBaseTenResult(ed1, nd2);

                ed3 = nd2.NormaliseBaseTwo();
            }
            catch (Exception e)
            {
                Console.WriteLine("example[" + i + "] ("
                                  + FormatDoubleAsHex(a) + ") exception: " + e.Message);
                return(false);
            }
            if (ed3.GetBinaryExponent() != ed1.GetBinaryExponent())
            {
                Console.WriteLine("example[" + i + "] ("
                                  + FormatDoubleAsHex(a) + ") bin exp mismatch");
                return(false);
            }
            BigInteger diff = ed3.GetSignificand() - (ed1.GetSignificand()).Abs();

            if (diff.Signum() == 0)
            {
                return(true);
            }
            // original quantity only has 53 bits of precision
            // these quantities may have errors in the 64th bit, which hopefully don't make any difference

            if (diff.BitCount() < 2)
            {
                // errors in the 64th bit happen from time to time
                // this is well below the 53 bits of precision required
                return(true);
            }

            // but bigger errors are a concern
            Console.WriteLine("example[" + i + "] ("
                              + FormatDoubleAsHex(a) + ") frac mismatch: " + diff.ToString());

            for (int j = -2; j < 3; j++)
            {
                Console.WriteLine((j < 0 ? "" : "+") + j + ": " + GetNearby(ed1, j));
            }
            for (int j = -2; j < 3; j++)
            {
                Console.WriteLine((j < 0 ? "" : "+") + j + ": " + GetNearby(nd2, j));
            }


            return(false);
        }