public void MirroredLongReinterpretation()
 {
     Assert.AreEqual(
         12345.67890,
         FloatingPointNumerics.ReinterpretAsDouble(
             FloatingPointNumerics.ReinterpretAsLong(12345.67890)
             )
         );
 }
 public void MirroredFloatReinterpretation()
 {
     Assert.AreEqual(
         12345,
         FloatingPointNumerics.ReinterpretAsInt(
             FloatingPointNumerics.ReinterpretAsFloat(12345)
             )
         );
 }
Exemple #3
0
        private static bool AreEqual(double expected, double actual, ref Tolerance tolerance)
        {
            if (double.IsNaN(expected) && double.IsNaN(actual))
            {
                return(true);
            }

            // Handle infinity specially since subtracting two infinite _values gives
            // NaN and the following test fails. mono also needs NaN to be handled
            // specially although ms.net could use either method. Also, handle
            // situation where no tolerance is used.
            if (double.IsInfinity(expected) || double.IsNaN(expected) || double.IsNaN(actual))
            {
                return(expected.Equals(actual));
            }

            if (tolerance.IsUnsetOrDefault)
            {
                var temp = TestExecutionContext.CurrentContext?.DefaultFloatingPointTolerance;
                if (temp != null && !temp.IsUnsetOrDefault)
                {
                    tolerance = temp;
                }
            }

            switch (tolerance.Mode)
            {
            case ToleranceMode.Unset:
                return(expected.Equals(actual));

            case ToleranceMode.Linear:
                return(Math.Abs(expected - actual) <= Convert.ToDouble(tolerance.Amount));

            case ToleranceMode.Percent:
                if (expected == 0.0)
                {
                    return(expected.Equals(actual));
                }

                double relativeError = Math.Abs((expected - actual) / expected);
                return(relativeError <= Convert.ToDouble(tolerance.Amount) / 100.0);

            case ToleranceMode.Ulps:
                return(FloatingPointNumerics.AreAlmostEqualUlps(
                           expected, actual, Convert.ToInt64(tolerance.Amount)));

            default:
                throw new ArgumentException("Unknown tolerance mode specified", "mode");
            }
        }
        public void DoubleEqualityWithUlps()
        {
            Assert.IsTrue(
                FloatingPointNumerics.AreAlmostEqualUlps(0.00000001, 0.000000010000000000000002, 1)
                );
            Assert.IsFalse(
                FloatingPointNumerics.AreAlmostEqualUlps(0.00000001, 0.000000010000000000000004, 1)
                );

            Assert.IsTrue(
                FloatingPointNumerics.AreAlmostEqualUlps(1000000.00, 1000000.0000000001, 1)
                );
            Assert.IsFalse(
                FloatingPointNumerics.AreAlmostEqualUlps(1000000.00, 1000000.0000000002, 1)
                );
        }
        public void FloatEqualityWithUlps()
        {
            Assert.IsTrue(
                FloatingPointNumerics.AreAlmostEqualUlps(0.00000001f, 0.0000000100000008f, 1)
                );
            Assert.IsFalse(
                FloatingPointNumerics.AreAlmostEqualUlps(0.00000001f, 0.0000000100000017f, 1)
                );

            Assert.IsTrue(
                FloatingPointNumerics.AreAlmostEqualUlps(1000000.00f, 1000000.06f, 1)
                );
            Assert.IsFalse(
                FloatingPointNumerics.AreAlmostEqualUlps(1000000.00f, 1000000.13f, 1)
                );
        }
Exemple #6
0
        private static bool AreEqual(double expected, double actual, ref Tolerance tolerance)
        {
            if (double.IsNaN(expected) && double.IsNaN(actual))
            {
                return(true);
            }

            // Handle infinity specially since subtracting two infinite values gives
            // NaN and the following test fails. mono also needs NaN to be handled
            // specially although ms.net could use either method. Also, handle
            // situation where no tolerance is used.
            if (double.IsInfinity(expected) || double.IsNaN(expected) || double.IsNaN(actual))
            {
                return(expected.Equals(actual));
            }

            if (tolerance.IsEmpty && GlobalSettings.DefaultFloatingPointTolerance > 0.0d)
            {
                tolerance = new Tolerance(GlobalSettings.DefaultFloatingPointTolerance);
            }

            switch (tolerance.Mode)
            {
            case ToleranceMode.None:
                return(expected.Equals(actual));

            case ToleranceMode.Linear:
                return(Math.Abs(expected - actual) <= Convert.ToDouble(tolerance.Value));

            case ToleranceMode.Percent:
                if (expected == 0.0)
                {
                    return(expected.Equals(actual));
                }

                double relativeError = Math.Abs((expected - actual) / expected);
                return(relativeError <= Convert.ToDouble(tolerance.Value) / 100.0);

#if !NETCF_1_0
            case ToleranceMode.Ulps:
                return(FloatingPointNumerics.AreAlmostEqualUlps(
                           expected, actual, Convert.ToInt64(tolerance.Value)));
#endif
            default:
                throw new ArgumentException("Unknown tolerance mode specified", "mode");
            }
        }
Exemple #7
0
        private static bool AreEqual(float expected, float actual, ref Tolerance tolerance)
        {
            if (float.IsNaN(expected) && float.IsNaN(actual))
            {
                return(true);
            }

            // handle infinity specially since subtracting two infinite _values gives
            // NaN and the following test fails. mono also needs NaN to be handled
            // specially although ms.net could use either method.
            if (float.IsInfinity(expected) || float.IsNaN(expected) || float.IsNaN(actual))
            {
                return(expected.Equals(actual));
            }

            if (tolerance.IsUnsetOrDefault && GlobalSettings.DefaultFloatingPointTolerance > 0.0d)
            {
                tolerance = new Tolerance(GlobalSettings.DefaultFloatingPointTolerance);
            }

            switch (tolerance.Mode)
            {
            case ToleranceMode.Unset:
                return(expected.Equals(actual));

            case ToleranceMode.Linear:
                return(Math.Abs(expected - actual) <= Convert.ToDouble(tolerance.Amount));

            case ToleranceMode.Percent:
                if (expected == 0.0f)
                {
                    return(expected.Equals(actual));
                }
                float relativeError = Math.Abs((expected - actual) / expected);
                return(relativeError <= Convert.ToSingle(tolerance.Amount) / 100.0f);

            case ToleranceMode.Ulps:
                return(FloatingPointNumerics.AreAlmostEqualUlps(
                           expected, actual, Convert.ToInt32(tolerance.Amount)));

            default:
                throw new ArgumentException("Unknown tolerance mode specified", "mode");
            }
        }
        public void DoubleEqualityWithUlps()
        {
            Assert.IsTrue(
                FloatingPointNumerics.AreAlmostEqualUlps(0.00000001, 0.000000010000000000000002, 1)
                );
            Assert.IsFalse(
                FloatingPointNumerics.AreAlmostEqualUlps(0.00000001, 0.000000010000000000000004, 1)
                );

            Assert.IsTrue(
                FloatingPointNumerics.AreAlmostEqualUlps(1000000.00, 1000000.0000000001, 1)
                );
            Assert.IsFalse(
                FloatingPointNumerics.AreAlmostEqualUlps(1000000.00, 1000000.0000000002, 1)
                );
            Assert.IsFalse( // Ensure we don't overflow on twos complement values
                FloatingPointNumerics.AreAlmostEqualUlps(2.0, -2.0, 1)
                );
        }
        public void FloatEqualityWithUlps()
        {
            Assert.IsTrue(
                FloatingPointNumerics.AreAlmostEqualUlps(0.00000001f, 0.0000000100000008f, 1)
                );
            Assert.IsFalse(
                FloatingPointNumerics.AreAlmostEqualUlps(0.00000001f, 0.0000000100000017f, 1)
                );

            Assert.IsTrue(
                FloatingPointNumerics.AreAlmostEqualUlps(1000000.00f, 1000000.06f, 1)
                );
            Assert.IsFalse(
                FloatingPointNumerics.AreAlmostEqualUlps(1000000.00f, 1000000.13f, 1)
                );
            Assert.IsFalse( // Ensure we don't overflow on twos complement values
                FloatingPointNumerics.AreAlmostEqualUlps(2.0f, -2.0f, 1)
                );
        }
Exemple #10
0
        private static bool AreEqual(float expected, float actual, ref Tolerance tolerance)
        {
            if (float.IsNaN(expected) && float.IsNaN(actual))
            {
                return(true);
            }
            if (float.IsInfinity(expected) || float.IsNaN(expected) || float.IsNaN(actual))
            {
                return(expected.Equals(actual));
            }
            if (tolerance.IsEmpty && GlobalSettings.DefaultFloatingPointTolerance > 0.0)
            {
                tolerance = new Tolerance(GlobalSettings.DefaultFloatingPointTolerance);
            }
            switch (tolerance.Mode)
            {
            case ToleranceMode.None:
                return(expected.Equals(actual));

            case ToleranceMode.Linear:
                return((double)Math.Abs(expected - actual) <= Convert.ToDouble(tolerance.Value));

            case ToleranceMode.Percent:
            {
                if (expected == 0f)
                {
                    return(expected.Equals(actual));
                }
                float num = Math.Abs((expected - actual) / expected);
                return(num <= Convert.ToSingle(tolerance.Value) / 100f);
            }

            case ToleranceMode.Ulps:
                return(FloatingPointNumerics.AreAlmostEqualUlps(expected, actual, Convert.ToInt32(tolerance.Value)));

            default:
                throw new ArgumentException("Unknown tolerance mode specified", "mode");
            }
        }