From 8bc882bf88608fdbbafeaecd3931c9dcac26df6d Mon Sep 17 00:00:00 2001 From: Christoph Ruegg Date: Mon, 14 Oct 2013 14:14:09 +0200 Subject: [PATCH] Precision: cleanup --- .../Complex/Factorization/DenseEvd.cs | 4 +- .../Complex/Factorization/UserEvd.cs | 4 +- .../Complex32/Factorization/DenseEvd.cs | 4 +- .../Complex32/Factorization/UserEvd.cs | 4 +- .../Double/Factorization/DenseEvd.cs | 2 +- .../Double/Factorization/UserEvd.cs | 4 +- .../Single/Factorization/DenseEvd.cs | 4 +- .../Single/Factorization/UserEvd.cs | 4 +- src/Numerics/Precision.cs | 152 +++++------------- src/Numerics/RootFinding/Brent.cs | 2 +- src/Numerics/SpecialFunctions/Beta.cs | 2 +- src/UnitTests/PrecisionTest.cs | 52 +++--- 12 files changed, 84 insertions(+), 154 deletions(-) diff --git a/src/Numerics/LinearAlgebra/Complex/Factorization/DenseEvd.cs b/src/Numerics/LinearAlgebra/Complex/Factorization/DenseEvd.cs index 362d50ea..1f393f50 100644 --- a/src/Numerics/LinearAlgebra/Complex/Factorization/DenseEvd.cs +++ b/src/Numerics/LinearAlgebra/Complex/Factorization/DenseEvd.cs @@ -247,7 +247,7 @@ namespace MathNet.Numerics.LinearAlgebra.Complex.Factorization var f = 0.0; var tst1 = 0.0; - var eps = Precision.DoubleMachinePrecision; + var eps = Precision.DoublePrecision; for (var l = 0; l < order; l++) { // Find small subdiagonal element @@ -584,7 +584,7 @@ namespace MathNet.Numerics.LinearAlgebra.Complex.Factorization { // Initialize var n = order - 1; - var eps = Precision.DoubleMachinePrecision; + var eps = Precision.DoublePrecision; double norm; Complex x, y, z, exshift = Complex.Zero; diff --git a/src/Numerics/LinearAlgebra/Complex/Factorization/UserEvd.cs b/src/Numerics/LinearAlgebra/Complex/Factorization/UserEvd.cs index d33ef636..5a700087 100644 --- a/src/Numerics/LinearAlgebra/Complex/Factorization/UserEvd.cs +++ b/src/Numerics/LinearAlgebra/Complex/Factorization/UserEvd.cs @@ -269,7 +269,7 @@ namespace MathNet.Numerics.LinearAlgebra.Complex.Factorization var f = 0.0; var tst1 = 0.0; - var eps = Precision.DoubleMachinePrecision; + var eps = Precision.DoublePrecision; for (var l = 0; l < order; l++) { // Find small subdiagonal element @@ -598,7 +598,7 @@ namespace MathNet.Numerics.LinearAlgebra.Complex.Factorization { // Initialize var n = order - 1; - var eps = Precision.DoubleMachinePrecision; + var eps = Precision.DoublePrecision; double norm; Complex x, y, z, exshift = Complex.Zero; diff --git a/src/Numerics/LinearAlgebra/Complex32/Factorization/DenseEvd.cs b/src/Numerics/LinearAlgebra/Complex32/Factorization/DenseEvd.cs index a36d05d4..1f3fd3a3 100644 --- a/src/Numerics/LinearAlgebra/Complex32/Factorization/DenseEvd.cs +++ b/src/Numerics/LinearAlgebra/Complex32/Factorization/DenseEvd.cs @@ -248,7 +248,7 @@ namespace MathNet.Numerics.LinearAlgebra.Complex32.Factorization var f = 0.0f; var tst1 = 0.0f; - var eps = Precision.DoubleMachinePrecision; + var eps = Precision.DoublePrecision; for (var l = 0; l < order; l++) { // Find small subdiagonal element @@ -585,7 +585,7 @@ namespace MathNet.Numerics.LinearAlgebra.Complex32.Factorization { // Initialize var n = order - 1; - var eps = (float) Precision.SingleMachinePrecision; + var eps = (float) Precision.SinglePrecision; float norm; Complex32 x, y, z, exshift = Complex32.Zero; diff --git a/src/Numerics/LinearAlgebra/Complex32/Factorization/UserEvd.cs b/src/Numerics/LinearAlgebra/Complex32/Factorization/UserEvd.cs index 7e72fd1a..087bc5ca 100644 --- a/src/Numerics/LinearAlgebra/Complex32/Factorization/UserEvd.cs +++ b/src/Numerics/LinearAlgebra/Complex32/Factorization/UserEvd.cs @@ -271,7 +271,7 @@ namespace MathNet.Numerics.LinearAlgebra.Complex32.Factorization var f = 0.0f; var tst1 = 0.0f; - var eps = Precision.DoubleMachinePrecision; + var eps = Precision.DoublePrecision; for (var l = 0; l < order; l++) { // Find small subdiagonal element @@ -600,7 +600,7 @@ namespace MathNet.Numerics.LinearAlgebra.Complex32.Factorization { // Initialize var n = order - 1; - var eps = (float) Precision.SingleMachinePrecision; + var eps = (float) Precision.SinglePrecision; float norm; Complex32 x, y, z, exshift = Complex32.Zero; diff --git a/src/Numerics/LinearAlgebra/Double/Factorization/DenseEvd.cs b/src/Numerics/LinearAlgebra/Double/Factorization/DenseEvd.cs index c81c09e2..c81a4ab9 100644 --- a/src/Numerics/LinearAlgebra/Double/Factorization/DenseEvd.cs +++ b/src/Numerics/LinearAlgebra/Double/Factorization/DenseEvd.cs @@ -276,7 +276,7 @@ namespace MathNet.Numerics.LinearAlgebra.Double.Factorization var f = 0.0; var tst1 = 0.0; - var eps = Precision.DoubleMachinePrecision; + var eps = Precision.DoublePrecision; for (var l = 0; l < order; l++) { // Find small subdiagonal element diff --git a/src/Numerics/LinearAlgebra/Double/Factorization/UserEvd.cs b/src/Numerics/LinearAlgebra/Double/Factorization/UserEvd.cs index 336ec750..c0a4c97d 100644 --- a/src/Numerics/LinearAlgebra/Double/Factorization/UserEvd.cs +++ b/src/Numerics/LinearAlgebra/Double/Factorization/UserEvd.cs @@ -312,7 +312,7 @@ namespace MathNet.Numerics.LinearAlgebra.Double.Factorization var f = 0.0; var tst1 = 0.0; - var eps = Precision.DoubleMachinePrecision; + var eps = Precision.DoublePrecision; for (var l = 0; l < order; l++) { // Find small subdiagonal element @@ -566,7 +566,7 @@ namespace MathNet.Numerics.LinearAlgebra.Double.Factorization { // Initialize var n = order - 1; - var eps = Precision.DoubleMachinePrecision; + var eps = Precision.DoublePrecision; var exshift = 0.0; double p = 0, q = 0, r = 0, s = 0, z = 0, w, x, y; diff --git a/src/Numerics/LinearAlgebra/Single/Factorization/DenseEvd.cs b/src/Numerics/LinearAlgebra/Single/Factorization/DenseEvd.cs index 3f77117e..40b43784 100644 --- a/src/Numerics/LinearAlgebra/Single/Factorization/DenseEvd.cs +++ b/src/Numerics/LinearAlgebra/Single/Factorization/DenseEvd.cs @@ -276,7 +276,7 @@ namespace MathNet.Numerics.LinearAlgebra.Single.Factorization var f = 0.0f; var tst1 = 0.0f; - var eps = Precision.SingleMachinePrecision; + var eps = Precision.SinglePrecision; for (var l = 0; l < order; l++) { // Find small subdiagonal element @@ -540,7 +540,7 @@ namespace MathNet.Numerics.LinearAlgebra.Single.Factorization { // Initialize var n = order - 1; - var eps = (float) Precision.SingleMachinePrecision; + var eps = (float) Precision.SinglePrecision; var exshift = 0.0f; float p = 0, q = 0, r = 0, s = 0, z = 0; float w, x, y; diff --git a/src/Numerics/LinearAlgebra/Single/Factorization/UserEvd.cs b/src/Numerics/LinearAlgebra/Single/Factorization/UserEvd.cs index 67b0d1f1..4c8640cd 100644 --- a/src/Numerics/LinearAlgebra/Single/Factorization/UserEvd.cs +++ b/src/Numerics/LinearAlgebra/Single/Factorization/UserEvd.cs @@ -311,7 +311,7 @@ namespace MathNet.Numerics.LinearAlgebra.Single.Factorization var f = 0.0f; var tst1 = 0.0f; - var eps = Precision.DoubleMachinePrecision; + var eps = Precision.DoublePrecision; for (var l = 0; l < order; l++) { // Find small subdiagonal element @@ -565,7 +565,7 @@ namespace MathNet.Numerics.LinearAlgebra.Single.Factorization { // Initialize var n = order - 1; - var eps = (float) Precision.SingleMachinePrecision; + var eps = (float) Precision.SinglePrecision; var exshift = 0.0f; float p = 0, q = 0, r = 0, s = 0, z = 0, w, x, y; diff --git a/src/Numerics/Precision.cs b/src/Numerics/Precision.cs index 1c1d890c..3beafb3a 100644 --- a/src/Numerics/Precision.cs +++ b/src/Numerics/Precision.cs @@ -81,117 +81,49 @@ namespace MathNet.Numerics /// public static class Precision { - #region Constants - - /// - /// The base number for binary values - /// - private const int BinaryBaseNumber = 2; - /// /// The number of binary digits used to represent the binary number for a double precision floating /// point value. i.e. there are this many digits used to represent the /// actual number, where in a number as: 0.134556 * 10^5 the digits are 0.134556 and the exponent is 5. /// - private const int DoublePrecision = 53; + private const int DoubleWidth = 53; /// /// The number of binary digits used to represent the binary number for a single precision floating /// point value. i.e. there are this many digits used to represent the /// actual number, where in a number as: 0.134556 * 10^5 the digits are 0.134556 and the exponent is 5. /// - private const int SinglePrecision = 24; - - #endregion - - #region Fields + private const int SingleWidth = 24; /// - /// The maximum relative precision of a double + /// The maximum relative precision of of double-precision floating numbers (64 bit) /// - private static readonly double _doubleMachinePrecision = Math.Pow(BinaryBaseNumber, -DoublePrecision); + public static readonly double DoublePrecision = Math.Pow(2, -DoubleWidth); /// - /// The maximum relative precision of a single + /// The maximum relative precision of of single-precision floating numbers (32 bit) /// - private static readonly double _singleMachinePrecision = Math.Pow(BinaryBaseNumber, -SinglePrecision); + public static readonly double SinglePrecision = Math.Pow(2, -SingleWidth); /// - /// The number of significant figures that a double-precision floating point has. + /// The number of significant decimal places of double-precision floating numbers (64 bit). /// - private static readonly int _numberOfDecimalPlacesForDoubles; + public static readonly int DoubleDecimalPlaces = (int) Math.Floor(Math.Abs(Math.Log10(DoublePrecision))); /// - /// The number of significant figures that a single-precision floating point has. + /// The number of significant decimal places of single-precision floating numbers (32 bit). /// - private static readonly int _numberOfDecimalPlacesForFloats; + public static readonly int SingleDecimalPlaces = (int) Math.Floor(Math.Abs(Math.Log10(SinglePrecision))); - /// Value representing 10 * 2^(-52) - private static readonly double _defaultDoubleRelativeAccuracy = _doubleMachinePrecision * 10; - - /// Value representing 10 * 2^(-52) - private static readonly float _defaultSingleRelativeAccuracy = (float)(_singleMachinePrecision * 10); - - #endregion - - #region Properties /// - /// Gets the maximum relative precision of a double. + /// Value representing 10 * 2^(-52) /// - /// The maximum relative precision of a double. - public static double DoubleMachinePrecision - { - get - { - return _doubleMachinePrecision; - } - } + private static readonly double DefaultDoubleRelativeAccuracy = DoublePrecision * 10; /// - /// Gets the maximum relative precision of a single. + /// Value representing 10 * 2^(-24) /// - /// The maximum relative precision of a single. - public static double SingleMachinePrecision - { - get - { - return _singleMachinePrecision; - } - } - #endregion - - /// - /// Initializes static members of the Precision class. - /// - static Precision() - { - _numberOfDecimalPlacesForFloats = (int)Math.Floor(Math.Abs(Math.Log10(_singleMachinePrecision))); - _numberOfDecimalPlacesForDoubles = (int)Math.Floor(Math.Abs(Math.Log10(_doubleMachinePrecision))); - } - - /// - /// Gets the number of fully significant decimal places for floats. - /// - /// The number of decimal places for floats. - public static int NumberOfDecimalPlacesForFloats - { - get - { - return _numberOfDecimalPlacesForFloats; - } - } - - /// - /// Gets the number of fully significant decimal places for doubles. - /// - /// The number of decimal places for doubles. - public static int NumberOfDecimalPlacesForDoubles - { - get - { - return _numberOfDecimalPlacesForDoubles; - } - } + private static readonly float DefaultSingleRelativeAccuracy = (float)(SinglePrecision * 10); /// /// Returns the magnitude of the number. @@ -213,7 +145,6 @@ namespace MathNet.Numerics #if PORTABLE var truncated = (int)Truncate(magnitude); #else - var truncated = (int)Math.Truncate(magnitude); #endif @@ -246,7 +177,6 @@ namespace MathNet.Numerics #if PORTABLE var truncated = (int)Truncate(magnitude); #else - var truncated = (int)Math.Truncate(magnitude); #endif @@ -263,7 +193,7 @@ namespace MathNet.Numerics /// /// The value. /// The value of the number. - public static double GetMagnitudeScaledValue(this double value) + public static double ScaleUnitMagnitude(this double value) { if (value.Equals(0.0)) { @@ -281,7 +211,7 @@ namespace MathNet.Numerics /// /// The resulting long value. /// - private static long GetLongFromDouble(double value) + private static long AsInt64(double value) { #if PORTABLE return DoubleToInt64Bits(value); @@ -297,10 +227,10 @@ namespace MathNet.Numerics /// /// The input double value. /// A long value which is roughly the equivalent of the double value. - private static long GetDirectionalLongFromDouble(double value) + private static long AsDirectionalInt64(double value) { // Convert in the normal way. - long result = GetLongFromDouble(value); + long result = AsInt64(value); // Now find out where we're at in the range // If the value is larger/equal to zero then we can just return the value @@ -315,7 +245,7 @@ namespace MathNet.Numerics /// /// The input float value. /// An int value which is roughly the equivalent of the double value. - private static int GetDirectionalIntFromFloat(float value) + private static int AsDirectionalInt32(float value) { // Convert in the normal way. int result = FloatToInt32Bits(value); @@ -354,7 +284,7 @@ namespace MathNet.Numerics // double < 0 --> long < 0, increasing in absolute magnitude as the double // gets closer to zero! // i.e. 0 - double.epsilon will give the largest long value! - long intValue = GetLongFromDouble(value); + long intValue = AsInt64(value); if (intValue < 0) { intValue -= count; @@ -407,7 +337,7 @@ namespace MathNet.Numerics // double < 0 --> long < 0, increasing in absolute magnitude as the double // gets closer to zero! // i.e. 0 - double.epsilon will give the largest long value! - long intValue = GetLongFromDouble(value); + long intValue = AsInt64(value); // If the value is zero then we'd really like the value to be -0. So we'll make it -0 // and then everything else should work out. @@ -517,7 +447,7 @@ namespace MathNet.Numerics /// Zero if || is smaller than 2^(-53) = 1.11e-16, otherwise. public static double CoerceZero(this double a) { - return CoerceZero(a, _doubleMachinePrecision); + return CoerceZero(a, DoublePrecision); } /// @@ -561,7 +491,7 @@ namespace MathNet.Numerics // double < 0 --> long < 0, increasing in absolute magnitude as the double // gets closer to zero! // i.e. 0 - double.epsilon will give the largest long value! - long intValue = GetLongFromDouble(value); + long intValue = AsInt64(value); #if PORTABLE // We need to protect against over- and under-flow of the intValue when @@ -721,18 +651,18 @@ namespace MathNet.Numerics // so return the ulps counts for the difference. if (value.Equals(0)) { - topRangeEnd = GetLongFromDouble(relativeDifference); + topRangeEnd = AsInt64(relativeDifference); bottomRangeEnd = topRangeEnd; return; } // Calculate the ulps for the maximum and minimum values // Note that these can overflow - long max = GetDirectionalLongFromDouble(value + (relativeDifference * Math.Abs(value))); - long min = GetDirectionalLongFromDouble(value - (relativeDifference * Math.Abs(value))); + long max = AsDirectionalInt64(value + (relativeDifference * Math.Abs(value))); + long min = AsDirectionalInt64(value - (relativeDifference * Math.Abs(value))); // Calculate the ulps from the value - long intValue = GetDirectionalLongFromDouble(value); + long intValue = AsDirectionalInt64(value); // Determine the ranges topRangeEnd = Math.Abs(max - intValue); @@ -773,8 +703,8 @@ namespace MathNet.Numerics // Calculate the ulps for the maximum and minimum values // Note that these can overflow - long intA = GetDirectionalLongFromDouble(a); - long intB = GetDirectionalLongFromDouble(b); + long intA = AsDirectionalInt64(a); + long intB = AsDirectionalInt64(b); // Now find the number of values between the two doubles. This should not overflow // given that there are more long values than there are double values @@ -790,7 +720,7 @@ namespace MathNet.Numerics public static bool AlmostEqual(this double a, double b) { double diff = a - b; - return AlmostEqualWithError(a, b, diff, _defaultDoubleRelativeAccuracy); + return AlmostEqualWithError(a, b, diff, DefaultDoubleRelativeAccuracy); } /// @@ -802,7 +732,7 @@ namespace MathNet.Numerics public static bool AlmostEqual(this float a, float b) { double diff = a - b; - return AlmostEqualWithError(a, b, diff, _defaultSingleRelativeAccuracy); + return AlmostEqualWithError(a, b, diff, DefaultSingleRelativeAccuracy); } /// @@ -814,7 +744,7 @@ namespace MathNet.Numerics public static bool AlmostEqual(this Complex a, Complex b) { double diff = a.NormOfDifference(b); - return AlmostEqualWithError(a.Norm(), b.Norm(), diff, _defaultDoubleRelativeAccuracy); + return AlmostEqualWithError(a.Norm(), b.Norm(), diff, DefaultDoubleRelativeAccuracy); } /// @@ -826,7 +756,7 @@ namespace MathNet.Numerics public static bool AlmostEqual(this Complex32 a, Complex32 b) { double diff = ((IPrecisionSupport)a).NormOfDifference(b); - return AlmostEqualWithError(((IPrecisionSupport)a).Norm(), ((IPrecisionSupport)b).Norm(), diff, _defaultSingleRelativeAccuracy); + return AlmostEqualWithError(((IPrecisionSupport)a).Norm(), ((IPrecisionSupport)b).Norm(), diff, DefaultSingleRelativeAccuracy); } /// /// Checks whether two structures with precision support are almost equal. @@ -839,7 +769,7 @@ namespace MathNet.Numerics where T : IPrecisionSupport { double diff = a.NormOfDifference(b); - return AlmostEqualWithError(a.Norm(), b.Norm(), diff, _defaultDoubleRelativeAccuracy); + return AlmostEqualWithError(a.Norm(), b.Norm(), diff, DefaultDoubleRelativeAccuracy); } /// @@ -1058,7 +988,7 @@ namespace MathNet.Numerics return false; } - if (Math.Abs(a) < _doubleMachinePrecision || Math.Abs(b) < _doubleMachinePrecision) + if (Math.Abs(a) < DoublePrecision || Math.Abs(b) < DoublePrecision) { return AlmostEqualWithAbsoluteError(a, b, diff, maximumError); } @@ -1183,7 +1113,7 @@ namespace MathNet.Numerics return a == b; } - if (Math.Abs(a) < _doubleMachinePrecision || Math.Abs(b) < _doubleMachinePrecision) + if (Math.Abs(a) < DoublePrecision || Math.Abs(b) < DoublePrecision) { return AlmostEqualInAbsoluteDecimalPlaces(a, b, decimalPlaces); } @@ -1239,7 +1169,7 @@ namespace MathNet.Numerics return a == b; } - if (Math.Abs(a) < _singleMachinePrecision || Math.Abs(b) < _singleMachinePrecision) + if (Math.Abs(a) < SinglePrecision || Math.Abs(b) < SinglePrecision) { return AlmostEqualInAbsoluteDecimalPlaces(a, b, decimalPlaces); } @@ -1490,10 +1420,10 @@ namespace MathNet.Numerics } // Get the first double and convert it to an integer value (by using the binary representation) - long firstUlong = GetDirectionalLongFromDouble(a); + long firstUlong = AsDirectionalInt64(a); // Get the second double and convert it to an integer value (by using the binary representation) - long secondUlong = GetDirectionalLongFromDouble(b); + long secondUlong = AsDirectionalInt64(b); // Now compare the values. // Note that this comparison can overflow so we'll approach this differently @@ -1536,10 +1466,10 @@ namespace MathNet.Numerics } // Get the first float and convert it to an integer value (by using the binary representation) - int firstUlong = GetDirectionalIntFromFloat(a); + int firstUlong = AsDirectionalInt32(a); // Get the second float and convert it to an integer value (by using the binary representation) - int secondUlong = GetDirectionalIntFromFloat(b); + int secondUlong = AsDirectionalInt32(b); // Now compare the values. // Note that this comparison can overflow so we'll approach this differently diff --git a/src/Numerics/RootFinding/Brent.cs b/src/Numerics/RootFinding/Brent.cs index 0ef4897b..41c103b6 100644 --- a/src/Numerics/RootFinding/Brent.cs +++ b/src/Numerics/RootFinding/Brent.cs @@ -96,7 +96,7 @@ namespace MathNet.Numerics.RootFinding } // convergence check - double xAcc1 = 2.0*Precision.DoubleMachinePrecision*Math.Abs(root) + 0.5*accuracy; + double xAcc1 = 2.0*Precision.DoublePrecision*Math.Abs(root) + 0.5*accuracy; double xMidOld = xMid; xMid = (upperBound - root)/2.0; diff --git a/src/Numerics/SpecialFunctions/Beta.cs b/src/Numerics/SpecialFunctions/Beta.cs index c0ae018c..c3ed407e 100644 --- a/src/Numerics/SpecialFunctions/Beta.cs +++ b/src/Numerics/SpecialFunctions/Beta.cs @@ -122,7 +122,7 @@ namespace MathNet.Numerics /* Continued fraction representation */ const int maxIterations = 100; - var eps = Precision.DoubleMachinePrecision; + var eps = Precision.DoublePrecision; var fpmin = 0.0.Increment() / eps; if (symmetryTransformation) diff --git a/src/UnitTests/PrecisionTest.cs b/src/UnitTests/PrecisionTest.cs index d830d841..b39be434 100644 --- a/src/UnitTests/PrecisionTest.cs +++ b/src/UnitTests/PrecisionTest.cs @@ -1181,49 +1181,49 @@ namespace MathNet.Numerics.UnitTests { // compare zero and negative zero Assert.AreEqual(0, Precision.CompareToInDecimalPlaces(0, -0, 1)); - Assert.AreEqual(0, Precision.CompareToInDecimalPlaces(0, -0, Precision.NumberOfDecimalPlacesForDoubles)); - Assert.AreEqual(0, Precision.CompareToInDecimalPlaces(0, -0, Precision.NumberOfDecimalPlacesForFloats)); + Assert.AreEqual(0, Precision.CompareToInDecimalPlaces(0, -0, Precision.DoubleDecimalPlaces)); + Assert.AreEqual(0, Precision.CompareToInDecimalPlaces(0, -0, Precision.SingleDecimalPlaces)); // compare two nearby numbers - Assert.AreEqual(-1, 1.0.CompareToInDecimalPlaces(1.0 + 10*_doublePrecision, Precision.NumberOfDecimalPlacesForDoubles)); - Assert.AreEqual(0, 1.0.CompareToInDecimalPlaces(1.0 + _doublePrecision, Precision.NumberOfDecimalPlacesForDoubles)); - Assert.AreEqual(0, 1.0.CompareToInDecimalPlaces(1.0 - _doublePrecision, Precision.NumberOfDecimalPlacesForDoubles)); - Assert.AreEqual(1, 1.0.CompareToInDecimalPlaces(1.0 - 10*_doublePrecision, Precision.NumberOfDecimalPlacesForDoubles)); + Assert.AreEqual(-1, 1.0.CompareToInDecimalPlaces(1.0 + 10*_doublePrecision, Precision.DoubleDecimalPlaces)); + Assert.AreEqual(0, 1.0.CompareToInDecimalPlaces(1.0 + _doublePrecision, Precision.DoubleDecimalPlaces)); + Assert.AreEqual(0, 1.0.CompareToInDecimalPlaces(1.0 - _doublePrecision, Precision.DoubleDecimalPlaces)); + Assert.AreEqual(1, 1.0.CompareToInDecimalPlaces(1.0 - 10*_doublePrecision, Precision.DoubleDecimalPlaces)); // compare with the two numbers reversed in compare order - Assert.AreEqual(1, (1.0 + 10*_doublePrecision).CompareToInDecimalPlaces(1.0, Precision.NumberOfDecimalPlacesForDoubles)); - Assert.AreEqual(0, (1.0 + _doublePrecision).CompareToInDecimalPlaces(1.0, Precision.NumberOfDecimalPlacesForDoubles)); - Assert.AreEqual(0, (1.0 - _doublePrecision).CompareToInDecimalPlaces(1.0, Precision.NumberOfDecimalPlacesForDoubles)); - Assert.AreEqual(-1, (1.0 - 10*_doublePrecision).CompareToInDecimalPlaces(1.0, Precision.NumberOfDecimalPlacesForDoubles)); + Assert.AreEqual(1, (1.0 + 10*_doublePrecision).CompareToInDecimalPlaces(1.0, Precision.DoubleDecimalPlaces)); + Assert.AreEqual(0, (1.0 + _doublePrecision).CompareToInDecimalPlaces(1.0, Precision.DoubleDecimalPlaces)); + Assert.AreEqual(0, (1.0 - _doublePrecision).CompareToInDecimalPlaces(1.0, Precision.DoubleDecimalPlaces)); + Assert.AreEqual(-1, (1.0 - 10*_doublePrecision).CompareToInDecimalPlaces(1.0, Precision.DoubleDecimalPlaces)); // compare two slightly more different numbers - Assert.AreEqual(-1, 1.0.CompareToInDecimalPlaces(1.0 + (50*_doublePrecision), Precision.NumberOfDecimalPlacesForDoubles)); - Assert.AreEqual(0, 1.0.CompareToInDecimalPlaces(1.0 + (50*_doublePrecision), Precision.NumberOfDecimalPlacesForDoubles - 2)); - Assert.AreEqual(0, 1.0.CompareToInDecimalPlaces(1.0 - (50*_doublePrecision), Precision.NumberOfDecimalPlacesForDoubles - 2)); - Assert.AreEqual(1, 1.0.CompareToInDecimalPlaces(1.0 - (50*_doublePrecision), Precision.NumberOfDecimalPlacesForDoubles)); + Assert.AreEqual(-1, 1.0.CompareToInDecimalPlaces(1.0 + (50*_doublePrecision), Precision.DoubleDecimalPlaces)); + Assert.AreEqual(0, 1.0.CompareToInDecimalPlaces(1.0 + (50*_doublePrecision), Precision.DoubleDecimalPlaces - 2)); + Assert.AreEqual(0, 1.0.CompareToInDecimalPlaces(1.0 - (50*_doublePrecision), Precision.DoubleDecimalPlaces - 2)); + Assert.AreEqual(1, 1.0.CompareToInDecimalPlaces(1.0 - (50*_doublePrecision), Precision.DoubleDecimalPlaces)); // compare different numbers - Assert.AreEqual(1, 2.0.CompareToInDecimalPlaces(1.0, Precision.NumberOfDecimalPlacesForDoubles)); - Assert.AreEqual(-1, 1.0.CompareToInDecimalPlaces(2.0, Precision.NumberOfDecimalPlacesForDoubles)); + Assert.AreEqual(1, 2.0.CompareToInDecimalPlaces(1.0, Precision.DoubleDecimalPlaces)); + Assert.AreEqual(-1, 1.0.CompareToInDecimalPlaces(2.0, Precision.DoubleDecimalPlaces)); // compare different numbers with large tolerance - Assert.AreEqual(-1, 1.0.CompareToInDecimalPlaces(1.0 + (1e5 * _doublePrecision), Precision.NumberOfDecimalPlacesForDoubles)); + Assert.AreEqual(-1, 1.0.CompareToInDecimalPlaces(1.0 + (1e5 * _doublePrecision), Precision.DoubleDecimalPlaces)); Assert.AreEqual(0, 1.0.CompareToInDecimalPlaces(1.0 - (1e5 * _doublePrecision), 10)); - Assert.AreEqual(1, 1.0.CompareToInDecimalPlaces(1.0 - (1e5 * _doublePrecision), Precision.NumberOfDecimalPlacesForDoubles)); + Assert.AreEqual(1, 1.0.CompareToInDecimalPlaces(1.0 - (1e5 * _doublePrecision), Precision.DoubleDecimalPlaces)); // compare inf & inf - Assert.AreEqual(0, double.PositiveInfinity.CompareToInDecimalPlaces(double.PositiveInfinity, Precision.NumberOfDecimalPlacesForDoubles)); - Assert.AreEqual(0, double.NegativeInfinity.CompareToInDecimalPlaces(double.NegativeInfinity, Precision.NumberOfDecimalPlacesForDoubles)); + Assert.AreEqual(0, double.PositiveInfinity.CompareToInDecimalPlaces(double.PositiveInfinity, Precision.DoubleDecimalPlaces)); + Assert.AreEqual(0, double.NegativeInfinity.CompareToInDecimalPlaces(double.NegativeInfinity, Precision.DoubleDecimalPlaces)); // compare -inf and inf - Assert.AreEqual(1, double.PositiveInfinity.CompareToInDecimalPlaces(double.NegativeInfinity, Precision.NumberOfDecimalPlacesForDoubles)); - Assert.AreEqual(-1, double.NegativeInfinity.CompareToInDecimalPlaces(double.PositiveInfinity, Precision.NumberOfDecimalPlacesForDoubles)); + Assert.AreEqual(1, double.PositiveInfinity.CompareToInDecimalPlaces(double.NegativeInfinity, Precision.DoubleDecimalPlaces)); + Assert.AreEqual(-1, double.NegativeInfinity.CompareToInDecimalPlaces(double.PositiveInfinity, Precision.DoubleDecimalPlaces)); // compare inf and non-inf - Assert.AreEqual(1, double.PositiveInfinity.CompareToInDecimalPlaces(1.0, Precision.NumberOfDecimalPlacesForDoubles)); - Assert.AreEqual(-1, 1.0.CompareToInDecimalPlaces(double.PositiveInfinity, Precision.NumberOfDecimalPlacesForDoubles)); - Assert.AreEqual(-1, double.NegativeInfinity.CompareToInDecimalPlaces(1.0, Precision.NumberOfDecimalPlacesForDoubles)); - Assert.AreEqual(1, 1.0.CompareToInDecimalPlaces(double.NegativeInfinity, Precision.NumberOfDecimalPlacesForDoubles)); + Assert.AreEqual(1, double.PositiveInfinity.CompareToInDecimalPlaces(1.0, Precision.DoubleDecimalPlaces)); + Assert.AreEqual(-1, 1.0.CompareToInDecimalPlaces(double.PositiveInfinity, Precision.DoubleDecimalPlaces)); + Assert.AreEqual(-1, double.NegativeInfinity.CompareToInDecimalPlaces(1.0, Precision.DoubleDecimalPlaces)); + Assert.AreEqual(1, 1.0.CompareToInDecimalPlaces(double.NegativeInfinity, Precision.DoubleDecimalPlaces)); } } }