Browse Source

Precision: rework comparisons, similar to equality before

optimization-1
Christoph Ruegg 13 years ago
parent
commit
be4583d133
  1. 31
      src/Numerics/LinearAlgebra/Complex/Solvers/ResidualStopCriterium.cs
  2. 35
      src/Numerics/LinearAlgebra/Complex32/Solvers/ResidualStopCriterium.cs
  3. 31
      src/Numerics/LinearAlgebra/Double/Solvers/ResidualStopCriterium.cs
  4. 35
      src/Numerics/LinearAlgebra/Single/Solvers/ResidualStopCriterium.cs
  5. 1
      src/Numerics/Numerics.csproj
  6. 679
      src/Numerics/Precision.Comparison.cs
  7. 6
      src/Numerics/Precision.Equality.cs
  8. 312
      src/Numerics/Precision.cs
  9. 6
      src/UnitTests/LinearAlgebraTests/Complex/Solvers/Iterative/BiCgStabTest.cs
  10. 6
      src/UnitTests/LinearAlgebraTests/Complex/Solvers/Iterative/GpBiCgTest.cs
  11. 6
      src/UnitTests/LinearAlgebraTests/Complex/Solvers/Iterative/MlkBiCgStabTest.cs
  12. 6
      src/UnitTests/LinearAlgebraTests/Complex/Solvers/Iterative/TFQMRTest.cs
  13. 10
      src/UnitTests/LinearAlgebraTests/Complex32/Solvers/Iterative/BiCgStabTest.cs
  14. 6
      src/UnitTests/LinearAlgebraTests/Complex32/Solvers/Iterative/GpBiCgTest.cs
  15. 6
      src/UnitTests/LinearAlgebraTests/Complex32/Solvers/Iterative/MlkBiCgStabTest.cs
  16. 6
      src/UnitTests/LinearAlgebraTests/Complex32/Solvers/Iterative/TFQMRTest.cs
  17. 10
      src/UnitTests/LinearAlgebraTests/Double/Solvers/Iterative/BiCgStabTest.cs
  18. 10
      src/UnitTests/LinearAlgebraTests/Double/Solvers/Iterative/GpBiCgTest.cs
  19. 10
      src/UnitTests/LinearAlgebraTests/Double/Solvers/Iterative/MlkBiCgStabTest.cs
  20. 6
      src/UnitTests/LinearAlgebraTests/Double/Solvers/Iterative/TFQMRTest.cs
  21. 6
      src/UnitTests/LinearAlgebraTests/Single/Solvers/Iterative/BiCgStabTest.cs
  22. 6
      src/UnitTests/LinearAlgebraTests/Single/Solvers/Iterative/GpBiCgTest.cs
  23. 6
      src/UnitTests/LinearAlgebraTests/Single/Solvers/Iterative/MlkBiCgStabTest.cs
  24. 6
      src/UnitTests/LinearAlgebraTests/Single/Solvers/Iterative/TFQMRTest.cs
  25. 356
      src/UnitTests/PrecisionTest.cs

31
src/Numerics/LinearAlgebra/Complex/Solvers/ResidualStopCriterium.cs

@ -211,14 +211,24 @@ namespace MathNet.Numerics.LinearAlgebra.Complex.Solvers
throw new ArgumentException(Resources.ArgumentVectorsSameLength, "residualVector");
}
// Store the infinity norms of both the solution and residual vectors
// These values will be used to calculate the relative drop in residuals
// later on.
var residualNorm = residualVector.InfinityNorm();
// This is criterium 1 from Templates for the solution of linear systems.
// The problem with this criterium is that it's not limiting enough. For now
// we won't use it. Later on we might get back to it.
// return mMaximumResidual * (System.Math.Abs(mMatrixNorm) * System.Math.Abs(solutionNorm) + System.Math.Abs(mVectorNorm));
// For now use criterium 2 from Templates for the solution of linear systems. See page 60.
// Check the residuals by calculating:
// ||r_i|| <= stop_tol * ||b||
var stopCriterium = ComputeStopCriterium(sourceVector.InfinityNorm());
var stopCriterium = _maximum * sourceVector.InfinityNorm();
// First check that we have real numbers not NaN's.
// NaN's can occur when the iterative process diverges so we
@ -232,8 +242,7 @@ namespace MathNet.Numerics.LinearAlgebra.Complex.Solvers
// ||r_i|| <= stop_tol * ||b||
// Stop the calculation if it's clearly smaller than the tolerance
var decimalMagnitude = Math.Abs(stopCriterium.Magnitude()) + 1;
if (residualNorm.IsSmallerDecimal(stopCriterium, decimalMagnitude))
if (residualNorm < stopCriterium)
{
if (_lastIteration <= iterationNumber)
{
@ -251,22 +260,6 @@ namespace MathNet.Numerics.LinearAlgebra.Complex.Solvers
return _status;
}
/// <summary>
/// Calculate stop criterium
/// </summary>
/// <param name="solutionNorm">Solution vector norm</param>
/// <returns>Criterium value</returns>
double ComputeStopCriterium(double solutionNorm)
{
// This is criterium 1 from Templates for the solution of linear systems.
// The problem with this criterium is that it's not limiting enough. For now
// we won't use it. Later on we might get back to it.
// return mMaximumResidual * (System.Math.Abs(mMatrixNorm) * System.Math.Abs(solutionNorm) + System.Math.Abs(mVectorNorm));
// For now use criterium 2 from Templates for the solution of linear systems. See page 60.
return _maximum*Math.Abs(solutionNorm);
}
/// <summary>
/// Gets the current calculation status.
/// </summary>

35
src/Numerics/LinearAlgebra/Complex32/Solvers/ResidualStopCriterium.cs

@ -206,19 +206,29 @@ namespace MathNet.Numerics.LinearAlgebra.Complex32.Solvers
throw new ArgumentException(Resources.ArgumentVectorsSameLength, "residualVector");
}
// Store the infinity norms of both the solution and residual vectors
// These values will be used to calculate the relative drop in residuals
// later on.
var residualNorm = (float) residualVector.InfinityNorm();
var residualNorm = residualVector.InfinityNorm();
// This is criterium 1 from Templates for the solution of linear systems.
// The problem with this criterium is that it's not limiting enough. For now
// we won't use it. Later on we might get back to it.
// return mMaximumResidual * (System.Math.Abs(mMatrixNorm) * System.Math.Abs(solutionNorm) + System.Math.Abs(mVectorNorm));
// For now use criterium 2 from Templates for the solution of linear systems. See page 60.
// Check the residuals by calculating:
// ||r_i|| <= stop_tol * ||b||
var stopCriterium = ComputeStopCriterium((float) sourceVector.InfinityNorm());
var stopCriterium = _maximum * sourceVector.InfinityNorm();
// First check that we have real numbers not NaN's.
// NaN's can occur when the iterative process diverges so we
// stop if that is the case.
if (float.IsNaN(stopCriterium) || float.IsNaN(residualNorm))
if (double.IsNaN(stopCriterium) || double.IsNaN(residualNorm))
{
_iterationCount = 0;
_status = IterationStatus.Diverged;
@ -227,8 +237,7 @@ namespace MathNet.Numerics.LinearAlgebra.Complex32.Solvers
// ||r_i|| <= stop_tol * ||b||
// Stop the calculation if it's clearly smaller than the tolerance
var decimalMagnitude = Math.Abs(stopCriterium.Magnitude()) + 1;
if (residualNorm.IsSmallerDecimal(stopCriterium, decimalMagnitude))
if (residualNorm < stopCriterium)
{
if (_lastIteration <= iterationNumber)
{
@ -246,22 +255,6 @@ namespace MathNet.Numerics.LinearAlgebra.Complex32.Solvers
return _status;
}
/// <summary>
/// Calculate stop criterium
/// </summary>
/// <param name="solutionNorm">Solution vector norm</param>
/// <returns>Criterium value</returns>
float ComputeStopCriterium(float solutionNorm)
{
// This is criterium 1 from Templates for the solution of linear systems.
// The problem with this criterium is that it's not limiting enough. For now
// we won't use it. Later on we might get back to it.
// return mMaximumResidual * (System.Math.Abs(mMatrixNorm) * System.Math.Abs(solutionNorm) + System.Math.Abs(mVectorNorm));
// For now use criterium 2 from Templates for the solution of linear systems. See page 60.
return _maximum*Math.Abs(solutionNorm);
}
/// <summary>
/// Gets the current calculation status.
/// </summary>

31
src/Numerics/LinearAlgebra/Double/Solvers/ResidualStopCriterium.cs

@ -204,14 +204,24 @@ namespace MathNet.Numerics.LinearAlgebra.Double.Solvers
throw new ArgumentException(Resources.ArgumentVectorsSameLength, "residualVector");
}
// Store the infinity norms of both the solution and residual vectors
// These values will be used to calculate the relative drop in residuals
// later on.
var residualNorm = residualVector.InfinityNorm();
// This is criterium 1 from Templates for the solution of linear systems.
// The problem with this criterium is that it's not limiting enough. For now
// we won't use it. Later on we might get back to it.
// return mMaximumResidual * (System.Math.Abs(mMatrixNorm) * System.Math.Abs(solutionNorm) + System.Math.Abs(mVectorNorm));
// For now use criterium 2 from Templates for the solution of linear systems. See page 60.
// Check the residuals by calculating:
// ||r_i|| <= stop_tol * ||b||
var stopCriterium = ComputeStopCriterium(sourceVector.InfinityNorm());
var stopCriterium = _maximum * sourceVector.InfinityNorm();
// First check that we have real numbers not NaN's.
// NaN's can occur when the iterative process diverges so we
@ -225,8 +235,7 @@ namespace MathNet.Numerics.LinearAlgebra.Double.Solvers
// ||r_i|| <= stop_tol * ||b||
// Stop the calculation if it's clearly smaller than the tolerance
var decimalMagnitude = Math.Abs(stopCriterium.Magnitude()) + 1;
if (residualNorm.IsSmallerDecimal(stopCriterium, decimalMagnitude))
if (residualNorm < stopCriterium)
{
if (_lastIteration <= iterationNumber)
{
@ -244,22 +253,6 @@ namespace MathNet.Numerics.LinearAlgebra.Double.Solvers
return _status;
}
/// <summary>
/// Calculate stop criterium
/// </summary>
/// <param name="solutionNorm">Solution vector norm</param>
/// <returns>Criterium value</returns>
double ComputeStopCriterium(double solutionNorm)
{
// This is criterium 1 from Templates for the solution of linear systems.
// The problem with this criterium is that it's not limiting enough. For now
// we won't use it. Later on we might get back to it.
// return mMaximumResidual * (System.Math.Abs(mMatrixNorm) * System.Math.Abs(solutionNorm) + System.Math.Abs(mVectorNorm));
// For now use criterium 2 from Templates for the solution of linear systems. See page 60.
return _maximum*Math.Abs(solutionNorm);
}
/// <summary>
/// Gets the current calculation status.
/// </summary>

35
src/Numerics/LinearAlgebra/Single/Solvers/ResidualStopCriterium.cs

@ -204,19 +204,29 @@ namespace MathNet.Numerics.LinearAlgebra.Single.Solvers
throw new ArgumentException(Resources.ArgumentVectorsSameLength, "residualVector");
}
// Store the infinity norms of both the solution and residual vectors
// These values will be used to calculate the relative drop in residuals
// later on.
var residualNorm = (float) residualVector.InfinityNorm();
var residualNorm = residualVector.InfinityNorm();
// This is criterium 1 from Templates for the solution of linear systems.
// The problem with this criterium is that it's not limiting enough. For now
// we won't use it. Later on we might get back to it.
// return mMaximumResidual * (System.Math.Abs(mMatrixNorm) * System.Math.Abs(solutionNorm) + System.Math.Abs(mVectorNorm));
// For now use criterium 2 from Templates for the solution of linear systems. See page 60.
// Check the residuals by calculating:
// ||r_i|| <= stop_tol * ||b||
var stopCriterium = ComputeStopCriterium((float) sourceVector.InfinityNorm());
var stopCriterium = _maximum*sourceVector.InfinityNorm();
// First check that we have real numbers not NaN's.
// NaN's can occur when the iterative process diverges so we
// stop if that is the case.
if (float.IsNaN(stopCriterium) || float.IsNaN(residualNorm))
if (double.IsNaN(stopCriterium) || double.IsNaN(residualNorm))
{
_iterationCount = 0;
_status = IterationStatus.Diverged;
@ -225,8 +235,7 @@ namespace MathNet.Numerics.LinearAlgebra.Single.Solvers
// ||r_i|| <= stop_tol * ||b||
// Stop the calculation if it's clearly smaller than the tolerance
var decimalMagnitude = Math.Abs(stopCriterium.Magnitude()) + 1;
if (residualNorm.IsSmallerDecimal(stopCriterium, decimalMagnitude))
if (residualNorm < stopCriterium)
{
if (_lastIteration <= iterationNumber)
{
@ -244,22 +253,6 @@ namespace MathNet.Numerics.LinearAlgebra.Single.Solvers
return _status;
}
/// <summary>
/// Calculate stop criterium
/// </summary>
/// <param name="solutionNorm">Solution vector norm</param>
/// <returns>Criterium value</returns>
float ComputeStopCriterium(float solutionNorm)
{
// This is criterium 1 from Templates for the solution of linear systems.
// The problem with this criterium is that it's not limiting enough. For now
// we won't use it. Later on we might get back to it.
// return mMaximumResidual * (System.Math.Abs(mMatrixNorm) * System.Math.Abs(solutionNorm) + System.Math.Abs(mVectorNorm));
// For now use criterium 2 from Templates for the solution of linear systems. See page 60.
return _maximum*Math.Abs(solutionNorm);
}
/// <summary>
/// Gets the current calculation status.
/// </summary>

1
src/Numerics/Numerics.csproj

@ -86,6 +86,7 @@
<Reference Include="System.Xml" />
</ItemGroup>
<ItemGroup>
<Compile Include="Precision.Comparison.cs" />
<Compile Include="Precision.Equality.cs" />
<Compile Include="Distributions\Bernoulli.cs" />
<Compile Include="Distributions\Beta.cs" />

679
src/Numerics/Precision.Comparison.cs

@ -0,0 +1,679 @@
// <copyright file="Precision.Comparison.cs" company="Math.NET">
// Math.NET Numerics, part of the Math.NET Project
// http://numerics.mathdotnet.com
// http://github.com/mathnet/mathnet-numerics
// http://mathnetnumerics.codeplex.com
//
// Copyright (c) 2009-2013 Math.NET
//
// Permission is hereby granted, free of charge, to any person
// obtaining a copy of this software and associated documentation
// files (the "Software"), to deal in the Software without
// restriction, including without limitation the rights to use,
// copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the
// Software is furnished to do so, subject to the following
// conditions:
//
// The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
// OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
// HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
// WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
// OTHER DEALINGS IN THE SOFTWARE.
// </copyright>
namespace MathNet.Numerics
{
public static partial class Precision
{
/// <summary>
/// Compares two doubles and determines which double is bigger.
/// a &lt; b -> -1; a ~= b (almost equal according to parameter) -> 0; a &gt; b -> +1.
/// </summary>
/// <param name="a">The first value.</param>
/// <param name="b">The second value.</param>
/// <param name="maximumAbsoluteError">The absolute accuracy required for being almost equal.</param>
public static int CompareTo(this double a, double b, double maximumAbsoluteError)
{
// NANs are equal to nothing,
// not even themselves, and thus they're not bigger or
// smaller than anything either
if (double.IsNaN(a) || double.IsNaN(b))
{
return a.CompareTo(b);
}
// If A or B are infinity (positive or negative) then
// only return true if first is smaller
if (double.IsInfinity(a) || double.IsInfinity(b))
{
return a.CompareTo(b);
}
// If the numbers are equal to within the number of decimal places
// then there's technically no difference
if (AlmostEqual(a, b, maximumAbsoluteError))
{
return 0;
}
// The numbers differ by more than the decimal places, so
// we can check the normal way to see if the first is
// larger than the second.
return a.CompareTo(b);
}
/// <summary>
/// Compares two doubles and determines which double is bigger.
/// a &lt; b -> -1; a ~= b (almost equal according to parameter) -> 0; a &gt; b -> +1.
/// </summary>
/// <param name="a">The first value.</param>
/// <param name="b">The second value.</param>
/// <param name="decimalPlaces">The number of decimal places on which the values must be compared. Must be 1 or larger.</param>
public static int CompareTo(this double a, double b, int decimalPlaces)
{
// NANs are equal to nothing,
// not even themselves, and thus they're not bigger or
// smaller than anything either
if (double.IsNaN(a) || double.IsNaN(b))
{
return a.CompareTo(b);
}
// If A or B are infinity (positive or negative) then
// only return true if first is smaller
if (double.IsInfinity(a) || double.IsInfinity(b))
{
return a.CompareTo(b);
}
// If the numbers are equal to within the number of decimal places
// then there's technically no difference
if (AlmostEqual(a, b, decimalPlaces))
{
return 0;
}
// The numbers differ by more than the decimal places, so
// we can check the normal way to see if the first is
// larger than the second.
return a.CompareTo(b);
}
/// <summary>
/// Compares two doubles and determines which double is bigger.
/// a &lt; b -> -1; a ~= b (almost equal according to parameter) -> 0; a &gt; b -> +1.
/// </summary>
/// <param name="a">The first value.</param>
/// <param name="b">The second value.</param>
/// <param name="maximumError">The relative accuracy required for being almost equal.</param>
public static int CompareToRelative(this double a, double b, double maximumError)
{
// NANs are equal to nothing,
// not even themselves, and thus they're not bigger or
// smaller than anything either
if (double.IsNaN(a) || double.IsNaN(b))
{
return a.CompareTo(b);
}
// If A or B are infinity (positive or negative) then
// only return true if first is smaller
if (double.IsInfinity(a) || double.IsInfinity(b))
{
return a.CompareTo(b);
}
// If the numbers are equal to within the number of decimal places
// then there's technically no difference
if (AlmostEqualRelative(a, b, maximumError))
{
return 0;
}
// The numbers differ by more than the decimal places, so
// we can check the normal way to see if the first is
// larger than the second.
return a.CompareTo(b);
}
/// <summary>
/// Compares two doubles and determines which double is bigger.
/// a &lt; b -> -1; a ~= b (almost equal according to parameter) -> 0; a &gt; b -> +1.
/// </summary>
/// <param name="a">The first value.</param>
/// <param name="b">The second value.</param>
/// <param name="decimalPlaces">The number of decimal places on which the values must be compared. Must be 1 or larger.</param>
public static int CompareToRelative(this double a, double b, int decimalPlaces)
{
// NANs are equal to nothing,
// not even themselves, and thus they're not bigger or
// smaller than anything either
if (double.IsNaN(a) || double.IsNaN(b))
{
return a.CompareTo(b);
}
// If A or B are infinity (positive or negative) then
// only return true if first is smaller
if (double.IsInfinity(a) || double.IsInfinity(b))
{
return a.CompareTo(b);
}
// If the numbers are equal to within the number of decimal places
// then there's technically no difference
if (AlmostEqualRelative(a, b, decimalPlaces))
{
return 0;
}
// The numbers differ by more than the decimal places, so
// we can check the normal way to see if the first is
// larger than the second.
return a.CompareTo(b);
}
/// <summary>
/// Compares two doubles and determines which double is bigger.
/// a &lt; b -> -1; a ~= b (almost equal according to parameter) -> 0; a &gt; b -> +1.
/// </summary>
/// <param name="a">The first value.</param>
/// <param name="b">The second value.</param>
/// <param name="maxNumbersBetween">The maximum error in terms of Units in Last Place (<c>ulps</c>), i.e. the maximum number of decimals that may be different. Must be 1 or larger.</param>
public static int CompareToNumbersBetween(this double a, double b, long maxNumbersBetween)
{
// NANs are equal to nothing,
// not even themselves, and thus they're not bigger or
// smaller than anything either
if (double.IsNaN(a) || double.IsNaN(b))
{
return a.CompareTo(b);
}
// If A or B are infinity (positive or negative) then
// only return true if first is smaller
if (double.IsInfinity(a) || double.IsInfinity(b))
{
return a.CompareTo(b);
}
// If the numbers are equal to within the tolerance then
// there's technically no difference
if (AlmostEqualNumbersBetween(a, b, maxNumbersBetween))
{
return 0;
}
return a.CompareTo(b);
}
/// <summary>
/// Compares two doubles and determines if the <c>first</c> value is larger than the <c>second</c>
/// value to within the specified number of decimal places or not.
/// </summary>
/// <remarks>
/// <para>
/// The values are equal if the difference between the two numbers is smaller than 10^(-numberOfDecimalPlaces). We divide by
/// two so that we have half the range on each side of the numbers, e.g. if <paramref name="decimalPlaces"/> == 2, then 0.01 will equal between
/// 0.005 and 0.015, but not 0.02 and not 0.00
/// </para>
/// </remarks>
/// <param name="a">The first value.</param>
/// <param name="b">The second value.</param>
/// <param name="decimalPlaces">The number of decimal places.</param>
/// <returns><c>true</c> if the first value is larger than the second value; otherwise <c>false</c>.</returns>
public static bool IsLarger(this double a, double b, int decimalPlaces)
{
// If A or B are a NAN, return false. NANs are equal to nothing,
// not even themselves, and thus they're not bigger or
// smaller than anything either
if (double.IsNaN(a) || double.IsNaN(b))
{
return false;
}
return CompareTo(a, b, decimalPlaces) > 0;
}
/// <summary>
/// Compares two doubles and determines if the <c>first</c> value is larger than the <c>second</c>
/// value to within the specified number of decimal places or not.
/// </summary>
/// <remarks>
/// <para>
/// The values are equal if the difference between the two numbers is smaller than 10^(-numberOfDecimalPlaces). We divide by
/// two so that we have half the range on each side of the numbers, e.g. if <paramref name="decimalPlaces"/> == 2, then 0.01 will equal between
/// 0.005 and 0.015, but not 0.02 and not 0.00
/// </para>
/// </remarks>
/// <param name="a">The first value.</param>
/// <param name="b">The second value.</param>
/// <param name="decimalPlaces">The number of decimal places.</param>
/// <returns><c>true</c> if the first value is larger than the second value; otherwise <c>false</c>.</returns>
public static bool IsLarger(this float a, float b, int decimalPlaces)
{
// If A or B are a NAN, return false. NANs are equal to nothing,
// not even themselves, and thus they're not bigger or
// smaller than anything either
if (float.IsNaN(a) || float.IsNaN(b))
{
return false;
}
return CompareTo(a, b, decimalPlaces) > 0;
}
/// <summary>
/// Compares two doubles and determines if the <c>first</c> value is larger than the <c>second</c>
/// value to within the specified number of decimal places or not.
/// </summary>
/// <param name="a">The first value.</param>
/// <param name="b">The second value.</param>
/// <param name="maximumAbsoluteError">The absolute accuracy required for being almost equal.</param>
/// <returns><c>true</c> if the first value is larger than the second value; otherwise <c>false</c>.</returns>
public static bool IsLarger(this double a, double b, double maximumAbsoluteError)
{
// If A or B are a NAN, return false. NANs are equal to nothing,
// not even themselves, and thus they're not bigger or
// smaller than anything either
if (double.IsNaN(a) || double.IsNaN(b))
{
return false;
}
return CompareTo(a, b, maximumAbsoluteError) > 0;
}
/// <summary>
/// Compares two doubles and determines if the <c>first</c> value is larger than the <c>second</c>
/// value to within the specified number of decimal places or not.
/// </summary>
/// <param name="a">The first value.</param>
/// <param name="b">The second value.</param>
/// <param name="maximumAbsoluteError">The absolute accuracy required for being almost equal.</param>
/// <returns><c>true</c> if the first value is larger than the second value; otherwise <c>false</c>.</returns>
public static bool IsLarger(this float a, float b, double maximumAbsoluteError)
{
// If A or B are a NAN, return false. NANs are equal to nothing,
// not even themselves, and thus they're not bigger or
// smaller than anything either
if (float.IsNaN(a) || float.IsNaN(b))
{
return false;
}
return CompareTo(a, b, maximumAbsoluteError) > 0;
}
/// <summary>
/// Compares two doubles and determines if the <c>first</c> value is larger than the <c>second</c>
/// value to within the specified number of decimal places or not.
/// </summary>
/// <remarks>
/// <para>
/// The values are equal if the difference between the two numbers is smaller than 10^(-numberOfDecimalPlaces). We divide by
/// two so that we have half the range on each side of the numbers, e.g. if <paramref name="decimalPlaces"/> == 2, then 0.01 will equal between
/// 0.005 and 0.015, but not 0.02 and not 0.00
/// </para>
/// </remarks>
/// <param name="a">The first value.</param>
/// <param name="b">The second value.</param>
/// <param name="decimalPlaces">The number of decimal places.</param>
/// <returns><c>true</c> if the first value is larger than the second value; otherwise <c>false</c>.</returns>
public static bool IsLargerRelative(this double a, double b, int decimalPlaces)
{
// If A or B are a NAN, return false. NANs are equal to nothing,
// not even themselves, and thus they're not bigger or
// smaller than anything either
if (double.IsNaN(a) || double.IsNaN(b))
{
return false;
}
return CompareToRelative(a, b, decimalPlaces) > 0;
}
/// <summary>
/// Compares two doubles and determines if the <c>first</c> value is larger than the <c>second</c>
/// value to within the specified number of decimal places or not.
/// </summary>
/// <remarks>
/// <para>
/// The values are equal if the difference between the two numbers is smaller than 10^(-numberOfDecimalPlaces). We divide by
/// two so that we have half the range on each side of the numbers, e.g. if <paramref name="decimalPlaces"/> == 2, then 0.01 will equal between
/// 0.005 and 0.015, but not 0.02 and not 0.00
/// </para>
/// </remarks>
/// <param name="a">The first value.</param>
/// <param name="b">The second value.</param>
/// <param name="decimalPlaces">The number of decimal places.</param>
/// <returns><c>true</c> if the first value is larger than the second value; otherwise <c>false</c>.</returns>
public static bool IsLargerRelative(this float a, float b, int decimalPlaces)
{
// If A or B are a NAN, return false. NANs are equal to nothing,
// not even themselves, and thus they're not bigger or
// smaller than anything either
if (float.IsNaN(a) || float.IsNaN(b))
{
return false;
}
return CompareToRelative(a, b, decimalPlaces) > 0;
}
/// <summary>
/// Compares two doubles and determines if the <c>first</c> value is larger than the <c>second</c>
/// value to within the specified number of decimal places or not.
/// </summary>
/// <param name="a">The first value.</param>
/// <param name="b">The second value.</param>
/// <param name="maximumError">The relative accuracy required for being almost equal.</param>
/// <returns><c>true</c> if the first value is larger than the second value; otherwise <c>false</c>.</returns>
public static bool IsLargerRelative(this double a, double b, double maximumError)
{
// If A or B are a NAN, return false. NANs are equal to nothing,
// not even themselves, and thus they're not bigger or
// smaller than anything either
if (double.IsNaN(a) || double.IsNaN(b))
{
return false;
}
return CompareToRelative(a, b, maximumError) > 0;
}
/// <summary>
/// Compares two doubles and determines if the <c>first</c> value is larger than the <c>second</c>
/// value to within the specified number of decimal places or not.
/// </summary>
/// <param name="a">The first value.</param>
/// <param name="b">The second value.</param>
/// <param name="maximumError">The relative accuracy required for being almost equal.</param>
/// <returns><c>true</c> if the first value is larger than the second value; otherwise <c>false</c>.</returns>
public static bool IsLargerRelative(this float a, float b, double maximumError)
{
// If A or B are a NAN, return false. NANs are equal to nothing,
// not even themselves, and thus they're not bigger or
// smaller than anything either
if (float.IsNaN(a) || float.IsNaN(b))
{
return false;
}
return CompareToRelative(a, b, maximumError) > 0;
}
/// <summary>
/// Compares two doubles and determines if the <c>first</c> value is larger than the <c>second</c>
/// value to within the tolerance or not. Equality comparison is based on the binary representation.
/// </summary>
/// <param name="a">The first value.</param>
/// <param name="b">The second value.</param>
/// <param name="maxNumbersBetween">The maximum number of floating point values for which the two values are considered equal. Must be 1 or larger.</param>
/// <returns><c>true</c> if the first value is larger than the second value; otherwise <c>false</c>.</returns>
public static bool IsLargerNumbersBetween(this double a, double b, long maxNumbersBetween)
{
// If A or B are a NAN, return false. NANs are equal to nothing,
// not even themselves, and thus they're not bigger or
// smaller than anything either
if (double.IsNaN(a) || double.IsNaN(b))
{
return false;
}
return CompareToNumbersBetween(a, b, maxNumbersBetween) > 0;
}
/// <summary>
/// Compares two doubles and determines if the <c>first</c> value is larger than the <c>second</c>
/// value to within the tolerance or not. Equality comparison is based on the binary representation.
/// </summary>
/// <param name="a">The first value.</param>
/// <param name="b">The second value.</param>
/// <param name="maxNumbersBetween">The maximum number of floating point values for which the two values are considered equal. Must be 1 or larger.</param>
/// <returns><c>true</c> if the first value is larger than the second value; otherwise <c>false</c>.</returns>
public static bool IsLargerNumbersBetween(this float a, float b, long maxNumbersBetween)
{
// If A or B are a NAN, return false. NANs are equal to nothing,
// not even themselves, and thus they're not bigger or
// smaller than anything either
if (float.IsNaN(a) || float.IsNaN(b))
{
return false;
}
return CompareToNumbersBetween(a, b, maxNumbersBetween) > 0;
}
/// <summary>
/// Compares two doubles and determines if the <c>first</c> value is smaller than the <c>second</c>
/// value to within the specified number of decimal places or not.
/// </summary>
/// <remarks>
/// <para>
/// The values are equal if the difference between the two numbers is smaller than 10^(-numberOfDecimalPlaces). We divide by
/// two so that we have half the range on each side of th<paramref name="decimalPlaces"/>g. if <paramref name="decimalPlaces"/> == 2, then 0.01 will equal between
/// 0.005 and 0.015, but not 0.02 and not 0.00
/// </para>
/// </remarks>
/// <param name="a">The first value.</param>
/// <param name="b">The second value.</param>
/// <param name="decimalPlaces">The number of decimal places.</param>
/// <returns><c>true</c> if the first value is smaller than the second value; otherwise <c>false</c>.</returns>
public static bool IsSmaller(this double a, double b, int decimalPlaces)
{
// If A or B are a NAN, return false. NANs are equal to nothing,
// not even themselves, and thus they're not bigger or
// smaller than anything either
if (double.IsNaN(a) || double.IsNaN(b))
{
return false;
}
return CompareTo(a, b, decimalPlaces) < 0;
}
/// <summary>
/// Compares two doubles and determines if the <c>first</c> value is smaller than the <c>second</c>
/// value to within the specified number of decimal places or not.
/// </summary>
/// <remarks>
/// <para>
/// The values are equal if the difference between the two numbers is smaller than 10^(-numberOfDecimalPlaces). We divide by
/// two so that we have half the range on each side of th<paramref name="decimalPlaces"/>g. if <paramref name="decimalPlaces"/> == 2, then 0.01 will equal between
/// 0.005 and 0.015, but not 0.02 and not 0.00
/// </para>
/// </remarks>
/// <param name="a">The first value.</param>
/// <param name="b">The second value.</param>
/// <param name="decimalPlaces">The number of decimal places.</param>
/// <returns><c>true</c> if the first value is smaller than the second value; otherwise <c>false</c>.</returns>
public static bool IsSmaller(this float a, float b, int decimalPlaces)
{
// If A or B are a NAN, return false. NANs are equal to nothing,
// not even themselves, and thus they're not bigger or
// smaller than anything either
if (float.IsNaN(a) || float.IsNaN(b))
{
return false;
}
return CompareTo(a, b, decimalPlaces) < 0;
}
/// <summary>
/// Compares two doubles and determines if the <c>first</c> value is smaller than the <c>second</c>
/// value to within the specified number of decimal places or not.
/// </summary>
/// <param name="a">The first value.</param>
/// <param name="b">The second value.</param>
/// <param name="maximumAbsoluteError">The absolute accuracy required for being almost equal.</param>
/// <returns><c>true</c> if the first value is smaller than the second value; otherwise <c>false</c>.</returns>
public static bool IsSmaller(this double a, double b, double maximumAbsoluteError)
{
// If A or B are a NAN, return false. NANs are equal to nothing,
// not even themselves, and thus they're not bigger or
// smaller than anything either
if (double.IsNaN(a) || double.IsNaN(b))
{
return false;
}
return CompareTo(a, b, maximumAbsoluteError) < 0;
}
/// <summary>
/// Compares two doubles and determines if the <c>first</c> value is smaller than the <c>second</c>
/// value to within the specified number of decimal places or not.
/// </summary>
/// <param name="a">The first value.</param>
/// <param name="b">The second value.</param>
/// <param name="maximumAbsoluteError">The absolute accuracy required for being almost equal.</param>
/// <returns><c>true</c> if the first value is smaller than the second value; otherwise <c>false</c>.</returns>
public static bool IsSmaller(this float a, float b, double maximumAbsoluteError)
{
// If A or B are a NAN, return false. NANs are equal to nothing,
// not even themselves, and thus they're not bigger or
// smaller than anything either
if (float.IsNaN(a) || float.IsNaN(b))
{
return false;
}
return CompareTo(a, b, maximumAbsoluteError) < 0;
}
/// <summary>
/// Compares two doubles and determines if the <c>first</c> value is smaller than the <c>second</c>
/// value to within the specified number of decimal places or not.
/// </summary>
/// <param name="a">The first value.</param>
/// <param name="b">The second value.</param>
/// <param name="decimalPlaces">The number of decimal places.</param>
/// <returns><c>true</c> if the first value is smaller than the second value; otherwise <c>false</c>.</returns>
public static bool IsSmallerRelative(this double a, double b, int decimalPlaces)
{
// If A or B are a NAN, return false. NANs are equal to nothing,
// not even themselves, and thus they're not bigger or
// smaller than anything either
if (double.IsNaN(a) || double.IsNaN(b))
{
return false;
}
return CompareToRelative(a, b, decimalPlaces) < 0;
}
/// <summary>
/// Compares two doubles and determines if the <c>first</c> value is smaller than the <c>second</c>
/// value to within the specified number of decimal places or not.
/// </summary>
/// <param name="a">The first value.</param>
/// <param name="b">The second value.</param>
/// <param name="decimalPlaces">The number of decimal places.</param>
/// <returns><c>true</c> if the first value is smaller than the second value; otherwise <c>false</c>.</returns>
public static bool IsSmallerRelative(this float a, float b, int decimalPlaces)
{
// If A or B are a NAN, return false. NANs are equal to nothing,
// not even themselves, and thus they're not bigger or
// smaller than anything either
if (float.IsNaN(a) || float.IsNaN(b))
{
return false;
}
return CompareToRelative(a, b, decimalPlaces) < 0;
}
/// <summary>
/// Compares two doubles and determines if the <c>first</c> value is smaller than the <c>second</c>
/// value to within the specified number of decimal places or not.
/// </summary>
/// <param name="a">The first value.</param>
/// <param name="b">The second value.</param>
/// <param name="maximumError">The relative accuracy required for being almost equal.</param>
/// <returns><c>true</c> if the first value is smaller than the second value; otherwise <c>false</c>.</returns>
public static bool IsSmallerRelative(this double a, double b, double maximumError)
{
// If A or B are a NAN, return false. NANs are equal to nothing,
// not even themselves, and thus they're not bigger or
// smaller than anything either
if (double.IsNaN(a) || double.IsNaN(b))
{
return false;
}
return CompareToRelative(a, b, maximumError) < 0;
}
/// <summary>
/// Compares two doubles and determines if the <c>first</c> value is smaller than the <c>second</c>
/// value to within the specified number of decimal places or not.
/// </summary>
/// <param name="a">The first value.</param>
/// <param name="b">The second value.</param>
/// <param name="maximumError">The relative accuracy required for being almost equal.</param>
/// <returns><c>true</c> if the first value is smaller than the second value; otherwise <c>false</c>.</returns>
public static bool IsSmallerRelative(this float a, float b, double maximumError)
{
// If A or B are a NAN, return false. NANs are equal to nothing,
// not even themselves, and thus they're not bigger or
// smaller than anything either
if (float.IsNaN(a) || float.IsNaN(b))
{
return false;
}
return CompareToRelative(a, b, maximumError) < 0;
}
/// <summary>
/// Compares two doubles and determines if the <c>first</c> value is smaller than the <c>second</c>
/// value to within the tolerance or not. Equality comparison is based on the binary representation.
/// </summary>
/// <param name="a">The first value.</param>
/// <param name="b">The second value.</param>
/// <param name="maxNumbersBetween">The maximum number of floating point values for which the two values are considered equal. Must be 1 or larger.</param>
/// <returns><c>true</c> if the first value is smaller than the second value; otherwise <c>false</c>.</returns>
public static bool IsSmallerNumbersBetween(this double a, double b, long maxNumbersBetween)
{
// If A or B are a NAN, return false. NANs are equal to nothing,
// not even themselves, and thus they're not bigger or
// smaller than anything either
if (double.IsNaN(a) || double.IsNaN(b))
{
return false;
}
return CompareToNumbersBetween(a, b, maxNumbersBetween) < 0;
}
/// <summary>
/// Compares two doubles and determines if the <c>first</c> value is smaller than the <c>second</c>
/// value to within the tolerance or not. Equality comparison is based on the binary representation.
/// </summary>
/// <param name="a">The first value.</param>
/// <param name="b">The second value.</param>
/// <param name="maxNumbersBetween">The maximum number of floating point values for which the two values are considered equal. Must be 1 or larger.</param>
/// <returns><c>true</c> if the first value is smaller than the second value; otherwise <c>false</c>.</returns>
public static bool IsSmallerNumbersBetween(this float a, float b, long maxNumbersBetween)
{
// If A or B are a NAN, return false. NANs are equal to nothing,
// not even themselves, and thus they're not bigger or
// smaller than anything either
if (float.IsNaN(a) || float.IsNaN(b))
{
return false;
}
return CompareToNumbersBetween(a, b, maxNumbersBetween) < 0;
}
}
}

6
src/Numerics/Precision.Equality.cs

@ -4,7 +4,7 @@
// http://github.com/mathnet/mathnet-numerics
// http://mathnetnumerics.codeplex.com
//
// Copyright (c) 2009-2010 Math.NET
// Copyright (c) 2009-2013 Math.NET
//
// Permission is hereby granted, free of charge, to any person
// obtaining a copy of this software and associated documentation
@ -32,10 +32,6 @@ using System;
using System.Collections.Generic;
using MathNet.Numerics.LinearAlgebra;
#if PORTABLE
using System.Runtime.InteropServices;
#endif
namespace MathNet.Numerics
{

312
src/Numerics/Precision.cs

@ -4,7 +4,7 @@
// http://github.com/mathnet/mathnet-numerics
// http://mathnetnumerics.codeplex.com
//
// Copyright (c) 2009-2010 Math.NET
// Copyright (c) 2009-2013 Math.NET
//
// Permission is hereby granted, free of charge, to any person
// obtaining a copy of this software and associated documentation
@ -36,11 +36,6 @@ using System.Runtime.InteropServices;
namespace MathNet.Numerics
{
#if !NOSYSNUMERICS
using Complex = System.Numerics.Complex;
#endif
/// <summary>
/// Support Interface for Precision Operations (like AlmostEquals).
/// </summary>
@ -710,311 +705,6 @@ namespace MathNet.Numerics
return (a >= b) ? (ulong) (intA - intB) : (ulong) (intB - intA);
}
/// <summary>
/// Compares two doubles and determines if the <c>first</c> value is larger than the <c>second</c>
/// value to within the tolerance or not. Equality comparison is based on the binary representation.
/// </summary>
/// <param name="a">The first value.</param>
/// <param name="b">The second value.</param>
/// <param name="maxNumbersBetween">The maximum number of floating point values for which the two values are considered equal. Must be 1 or larger.</param>
/// <returns><c>true</c> if the first value is larger than the second value; otherwise <c>false</c>.</returns>
public static bool IsLarger(this double a, double b, long maxNumbersBetween)
{
// If A or B are a NAN, return false. NANs are equal to nothing,
// not even themselves, and thus they're not bigger or
// smaller than anything either
if (double.IsNaN(a) || double.IsNaN(b))
{
return false;
}
return CompareTo(a, b, maxNumbersBetween) > 0;
}
/// <summary>
/// Compares two doubles and determines if the <c>first</c> value is larger than the <c>second</c>
/// value to within the tolerance or not. Equality comparison is based on the binary representation.
/// </summary>
/// <param name="a">The first value.</param>
/// <param name="b">The second value.</param>
/// <param name="maxNumbersBetween">The maximum number of floating point values for which the two values are considered equal. Must be 1 or larger.</param>
/// <returns><c>true</c> if the first value is larger than the second value; otherwise <c>false</c>.</returns>
public static bool IsLarger(this float a, float b, long maxNumbersBetween)
{
// If A or B are a NAN, return false. NANs are equal to nothing,
// not even themselves, and thus they're not bigger or
// smaller than anything either
if (float.IsNaN(a) || float.IsNaN(b))
{
return false;
}
return CompareTo(a, b, maxNumbersBetween) > 0;
}
/// <summary>
/// Compares two doubles and determines if the <c>first</c> value is larger than the <c>second</c>
/// value to within the specified number of decimal places or not.
/// </summary>
/// <remarks>
/// <para>
/// The values are equal if the difference between the two numbers is smaller than 10^(-numberOfDecimalPlaces). We divide by
/// two so that we have half the range on each side of the numbers, e.g. if <paramref name="decimalPlaces"/> == 2, then 0.01 will equal between
/// 0.005 and 0.015, but not 0.02 and not 0.00
/// </para>
/// </remarks>
/// <param name="a">The first value.</param>
/// <param name="b">The second value.</param>
/// <param name="decimalPlaces">The number of decimal places.</param>
/// <returns><c>true</c> if the first value is larger than the second value; otherwise <c>false</c>.</returns>
public static bool IsLargerDecimal(this double a, double b, int decimalPlaces)
{
// If A or B are a NAN, return false. NANs are equal to nothing,
// not even themselves, and thus they're not bigger or
// smaller than anything either
if (double.IsNaN(a) || double.IsNaN(b))
{
return false;
}
return CompareToDecimal(a, b, decimalPlaces) > 0;
}
/// <summary>
/// Compares two doubles and determines if the <c>first</c> value is larger than the <c>second</c>
/// value to within the specified number of decimal places or not.
/// </summary>
/// <remarks>
/// <para>
/// The values are equal if the difference between the two numbers is smaller than 10^(-numberOfDecimalPlaces). We divide by
/// two so that we have half the range on each side of the numbers, e.g. if <paramref name="decimalPlaces"/> == 2, then 0.01 will equal between
/// 0.005 and 0.015, but not 0.02 and not 0.00
/// </para>
/// </remarks>
/// <param name="a">The first value.</param>
/// <param name="b">The second value.</param>
/// <param name="decimalPlaces">The number of decimal places.</param>
/// <returns><c>true</c> if the first value is larger than the second value; otherwise <c>false</c>.</returns>
public static bool IsLargerDecimal(this float a, float b, int decimalPlaces)
{
// If A or B are a NAN, return false. NANs are equal to nothing,
// not even themselves, and thus they're not bigger or
// smaller than anything either
if (float.IsNaN(a) || float.IsNaN(b))
{
return false;
}
return CompareToDecimal(a, b, decimalPlaces) > 0;
}
/// <summary>
/// Compares two doubles and determines if the <c>first</c> value is smaller than the <c>second</c>
/// value to within the tolerance or not. Equality comparison is based on the binary representation.
/// </summary>
/// <param name="a">The first value.</param>
/// <param name="b">The second value.</param>
/// <param name="maxNumbersBetween">The maximum number of floating point values for which the two values are considered equal. Must be 1 or larger.</param>
/// <returns><c>true</c> if the first value is smaller than the second value; otherwise <c>false</c>.</returns>
public static bool IsSmaller(this double a, double b, long maxNumbersBetween)
{
// If A or B are a NAN, return false. NANs are equal to nothing,
// not even themselves, and thus they're not bigger or
// smaller than anything either
if (double.IsNaN(a) || double.IsNaN(b))
{
return false;
}
return CompareTo(a, b, maxNumbersBetween) < 0;
}
/// <summary>
/// Compares two floats and determines if the <c>first</c> value is smaller than the <c>second</c>
/// value to within the tolerance or not. Equality comparison is based on the binary representation.
/// </summary>
/// <param name="a">The first value.</param>
/// <param name="b">The second value.</param>
/// <param name="maxNumbersBetween">The maximum number of floating point values for which the two values are considered equal. Must be 1 or larger.</param>
/// <returns><c>true</c> if the first value is smaller than the second value; otherwise <c>false</c>.</returns>
public static bool IsSmaller(this float a, float b, long maxNumbersBetween)
{
// If A or B are a NAN, return false. NANs are equal to nothing,
// not even themselves, and thus they're not bigger or
// smaller than anything either
if (float.IsNaN(a) || float.IsNaN(b))
{
return false;
}
return CompareTo(a, b, maxNumbersBetween) < 0;
}
/// <summary>
/// Compares two doubles and determines if the <c>first</c> value is smaller than the <c>second</c>
/// value to within the specified number of decimal places or not.
/// </summary>
/// <remarks>
/// <para>
/// The values are equal if the difference between the two numbers is smaller than 10^(-numberOfDecimalPlaces). We divide by
/// two so that we have half the range on each side of th<paramref name="decimalPlaces"/>g. if <paramref name="decimalPlaces"/> == 2, then 0.01 will equal between
/// 0.005 and 0.015, but not 0.02 and not 0.00
/// </para>
/// </remarks>
/// <param name="a">The first value.</param>
/// <param name="b">The second value.</param>
/// <param name="decimalPlaces">The number of decimal places.</param>
/// <returns><c>true</c> if the first value is smaller than the second value; otherwise <c>false</c>.</returns>
public static bool IsSmallerDecimal(this double a, double b, int decimalPlaces)
{
// If A or B are a NAN, return false. NANs are equal to nothing,
// not even themselves, and thus they're not bigger or
// smaller than anything either
if (double.IsNaN(a) || double.IsNaN(b))
{
return false;
}
return CompareToDecimal(a, b, decimalPlaces) < 0;
}
///<summary>
/// Compares two floats and determines if the <c>first</c> value is smaller than the <c>second</c>
/// value to within the specified number of decimal places or not.
/// </summary>
/// <remarks>
/// <para>
/// The values are equal if the difference between the two numbers is smaller than 10^(-numberOfDecimalPlaces). We divide by
/// two so that we have half the range on each side of th<paramref name="decimalPlaces"/>g. if <paramref name="decimalPlaces"/> == 2, then 0.01 will equal between
/// 0.005 and 0.015, but not 0.02 and not 0.00
/// </para>
/// </remarks>
/// <param name="a">The first value.</param>
/// <param name="b">The second value.</param>
/// <param name="decimalPlaces">The number of decimal places.</param>
/// <returns><c>true</c> if the first value is smaller than the second value; otherwise <c>false</c>.</returns>
public static bool IsSmallerDecimal(this float a, float b, int decimalPlaces)
{
// If A or B are a NAN, return false. NANs are equal to nothing,
// not even themselves, and thus they're not bigger or
// smaller than anything either
if (float.IsNaN(a) || float.IsNaN(b))
{
return false;
}
return CompareToDecimal(a, b, decimalPlaces) < 0;
}
/// <summary>
/// Compares two doubles and determines which double is bigger.
/// </summary>
/// <param name="a">The first value.</param>
/// <param name="b">The second value.</param>
/// <param name="maxNumbersBetween">The maximum error in terms of Units in Last Place (<c>ulps</c>), i.e. the maximum number of decimals that may be different. Must be 1 or larger.</param>
/// <returns>
/// <list type="table">
/// <listheader>
/// <term>Return value</term>
/// <description>Meaning</description>
/// </listheader>
/// <item>
/// <term>-1</term>
/// <description><paramref name="a"/> is smaller than <paramref name="b"/> by more than the <paramref name="maxNumbersBetween"/> tolerance.</description>
/// </item>
/// <item>
/// <term>0</term>
/// <description><paramref name="a"/> is equal to <paramref name="b"/> within the <paramref name="maxNumbersBetween"/> tolerance.</description>
/// </item>
/// <item>
/// <term>1</term>
/// <description><paramref name="a"/> is bigger than <paramref name="b"/> by more than the <paramref name="maxNumbersBetween"/> tolerance.</description>
/// </item>
/// </list>
/// </returns>
public static int CompareTo(this double a, double b, long maxNumbersBetween)
{
// If A or B are a NAN, return false. NANs are equal to nothing,
// not even themselves, and thus they're not bigger or
// smaller than anything either
if (double.IsNaN(a) || double.IsNaN(b))
{
return a.CompareTo(b);
}
// If A or B are infinity (positive or negative) then
// only return true if first is smaller
if (double.IsInfinity(a) || double.IsInfinity(b))
{
return a.CompareTo(b);
}
// If the numbers are equal to within the tolerance then
// there's technically no difference
if (AlmostEqualNumbersBetween(a, b, maxNumbersBetween))
{
return 0;
}
return a.CompareTo(b);
}
/// <summary>
/// Compares two doubles and determines which double is bigger.
/// </summary>
/// <param name="a">The first value.</param>
/// <param name="b">The second value.</param>
/// <param name="decimalPlaces">The number of decimal places on which the values must be compared. Must be 1 or larger.</param>
/// <returns>
/// <list type="table">
/// <listheader>
/// <term>Return value</term>
/// <description>Meaning</description>
/// </listheader>
/// <item>
/// <term>-1</term>
/// <description><paramref name="a"/> is smaller than <paramref name="b"/> by more than a magnitude equal to <paramref name="decimalPlaces"/>.</description>
/// </item>
/// <item>
/// <term>0</term>
/// <description><paramref name="a"/> is equal to <paramref name="b"/> within a magnitude equal to <paramref name="decimalPlaces"/>.</description>
/// </item>
/// <item>
/// <term>1</term>
/// <description><paramref name="a"/> is bigger than <paramref name="b"/> by more than a magnitude equal to <paramref name="decimalPlaces"/>.</description>
/// </item>
/// </list>
/// </returns>
public static int CompareToDecimal(this double a, double b, int decimalPlaces)
{
// If A or B are a NAN, return false. NANs are equal to nothing,
// not even themselves, and thus they're not bigger or
// smaller than anything either
if (double.IsNaN(a) || double.IsNaN(b))
{
return a.CompareTo(b);
}
// If A or B are infinity (positive or negative) then
// only return true if first is smaller
if (double.IsInfinity(a) || double.IsInfinity(b))
{
return a.CompareTo(b);
}
// If the numbers are equal to within the number of decimal places
// then there's technically no difference
if (AlmostEqualRelative(a, b, decimalPlaces))
{
return 0;
}
// The numbers differ by more than the decimal places, so
// we can check the normal way to see if the first is
// larger than the second.
return a.CompareTo(b);
}
/// <summary>
/// Evaluates the minimum distance to the next distinguishable number near the argument value.
/// </summary>

6
src/UnitTests/LinearAlgebraTests/Complex/Solvers/Iterative/BiCgStabTest.cs

@ -121,7 +121,7 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Complex.Solvers.Iterativ
// Now compare the vectors
for (var i = 0; i < y.Count; i++)
{
Assert.IsTrue((y[i] - z[i]).Magnitude.IsSmaller(ConvergenceBoundary, 1), "#05-" + i);
Assert.GreaterOrEqual(ConvergenceBoundary, (y[i] - z[i]).Magnitude, "#05-" + i);
}
}
@ -164,7 +164,7 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Complex.Solvers.Iterativ
// Now compare the vectors
for (var i = 0; i < y.Count; i++)
{
Assert.IsTrue((y[i] - z[i]).Magnitude.IsSmaller(ConvergenceBoundary, 1), "#05-" + i);
Assert.GreaterOrEqual(ConvergenceBoundary, (y[i] - z[i]).Magnitude, "#05-" + i);
}
}
@ -240,7 +240,7 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Complex.Solvers.Iterativ
// Now compare the vectors
for (var i = 0; i < y.Count; i++)
{
Assert.IsTrue((y[i] - z[i]).Magnitude.IsSmaller(ConvergenceBoundary, 1), "#05-" + i);
Assert.GreaterOrEqual(ConvergenceBoundary, (y[i] - z[i]).Magnitude, "#05-" + i);
}
}

6
src/UnitTests/LinearAlgebraTests/Complex/Solvers/Iterative/GpBiCgTest.cs

@ -121,7 +121,7 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Complex.Solvers.Iterativ
// Now compare the vectors
for (var i = 0; i < y.Count; i++)
{
Assert.IsTrue((y[i] - z[i]).Magnitude.IsSmaller(ConvergenceBoundary, 1), "#05-" + i);
Assert.GreaterOrEqual(ConvergenceBoundary, (y[i] - z[i]).Magnitude, "#05-" + i);
}
}
@ -165,7 +165,7 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Complex.Solvers.Iterativ
// Now compare the vectors
for (var i = 0; i < y.Count; i++)
{
Assert.IsTrue((y[i] - z[i]).Magnitude.IsSmaller(ConvergenceBoundary, 1), "#05-" + i);
Assert.GreaterOrEqual(ConvergenceBoundary, (y[i] - z[i]).Magnitude, "#05-" + i);
}
}
@ -242,7 +242,7 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Complex.Solvers.Iterativ
// Now compare the vectors
for (var i = 0; i < y.Count; i++)
{
Assert.IsTrue((y[i] - z[i]).Magnitude.IsSmaller(ConvergenceBoundary, 1), "#05-" + i);
Assert.GreaterOrEqual(ConvergenceBoundary, (y[i] - z[i]).Magnitude, "#05-" + i);
}
}

6
src/UnitTests/LinearAlgebraTests/Complex/Solvers/Iterative/MlkBiCgStabTest.cs

@ -121,7 +121,7 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Complex.Solvers.Iterativ
// Now compare the vectors
for (var i = 0; i < y.Count; i++)
{
Assert.IsTrue((y[i] - z[i]).Magnitude.IsSmaller(ConvergenceBoundary, 1), "#05-" + i);
Assert.GreaterOrEqual(ConvergenceBoundary, (y[i] - z[i]).Magnitude, "#05-" + i);
}
}
@ -165,7 +165,7 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Complex.Solvers.Iterativ
// Now compare the vectors
for (var i = 0; i < y.Count; i++)
{
Assert.IsTrue((y[i] - z[i]).Magnitude.IsSmaller(ConvergenceBoundary, 1), "#05-" + i);
Assert.GreaterOrEqual(ConvergenceBoundary, (y[i] - z[i]).Magnitude, "#05-" + i);
}
}
@ -242,7 +242,7 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Complex.Solvers.Iterativ
// Now compare the vectors
for (var i = 0; i < y.Count; i++)
{
Assert.IsTrue((y[i] - z[i]).Magnitude.IsSmaller(ConvergenceBoundary, 1), "#05-" + i);
Assert.GreaterOrEqual(ConvergenceBoundary, (y[i] - z[i]).Magnitude, "#05-" + i);
}
}

6
src/UnitTests/LinearAlgebraTests/Complex/Solvers/Iterative/TFQMRTest.cs

@ -121,7 +121,7 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Complex.Solvers.Iterativ
// Now compare the vectors
for (var i = 0; i < y.Count; i++)
{
Assert.IsTrue((y[i] - z[i]).Magnitude.IsSmaller(ConvergenceBoundary, 1), "#05-" + i);
Assert.GreaterOrEqual(ConvergenceBoundary, (y[i] - z[i]).Magnitude, "#05-" + i);
}
}
@ -165,7 +165,7 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Complex.Solvers.Iterativ
// Now compare the vectors
for (var i = 0; i < y.Count; i++)
{
Assert.IsTrue((y[i] - z[i]).Magnitude.IsSmaller(ConvergenceBoundary, 1), "#05-" + i);
Assert.GreaterOrEqual(ConvergenceBoundary, (y[i] - z[i]).Magnitude, "#05-" + i);
}
}
@ -242,7 +242,7 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Complex.Solvers.Iterativ
// Now compare the vectors
for (var i = 0; i < y.Count; i++)
{
Assert.IsTrue((y[i] - z[i]).Magnitude.IsSmaller(ConvergenceBoundary, 1), "#05-" + i);
Assert.GreaterOrEqual(ConvergenceBoundary, (y[i] - z[i]).Magnitude, "#05-" + i);
}
}

10
src/UnitTests/LinearAlgebraTests/Complex32/Solvers/Iterative/BiCgStabTest.cs

@ -117,7 +117,7 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Complex32.Solvers.Iterat
// Now compare the vectors
for (var i = 0; i < y.Count; i++)
{
Assert.IsTrue((y[i] - z[i]).Magnitude.IsSmaller(ConvergenceBoundary, 1), "#05-" + i);
Assert.GreaterOrEqual(ConvergenceBoundary, (y[i] - z[i]).Magnitude, "#05-" + i);
}
}
@ -161,7 +161,7 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Complex32.Solvers.Iterat
// Now compare the vectors
for (var i = 0; i < y.Count; i++)
{
Assert.IsTrue((y[i] - z[i]).Magnitude.IsSmaller(ConvergenceBoundary, 1), "#05-" + i);
Assert.GreaterOrEqual(ConvergenceBoundary, (y[i] - z[i]).Magnitude, "#05-" + i);
}
}
@ -238,11 +238,7 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Complex32.Solvers.Iterat
// Now compare the vectors
for (var i = 0; i < y.Count; i++)
{
#if !PORTABLE
Assert.IsTrue((y[i] - z[i]).Magnitude.IsSmaller(ConvergenceBoundary, 1), "#05-" + i);
#else
Assert.IsTrue((y[i] - z[i]).Magnitude.IsSmaller(ConvergenceBoundary * 10.0f, 1), "#05-" + i);
#endif
Assert.GreaterOrEqual(ConvergenceBoundary, (y[i] - z[i]).Magnitude, "#05-" + i);
}
}

6
src/UnitTests/LinearAlgebraTests/Complex32/Solvers/Iterative/GpBiCgTest.cs

@ -117,7 +117,7 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Complex32.Solvers.Iterat
// Now compare the vectors
for (var i = 0; i < y.Count; i++)
{
Assert.IsTrue((y[i] - z[i]).Magnitude.IsSmaller(ConvergenceBoundary, 1), "#05-" + i);
Assert.GreaterOrEqual(ConvergenceBoundary, (y[i] - z[i]).Magnitude, "#05-" + i);
}
}
@ -161,7 +161,7 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Complex32.Solvers.Iterat
// Now compare the vectors
for (var i = 0; i < y.Count; i++)
{
Assert.IsTrue((y[i] - z[i]).Magnitude.IsSmaller(ConvergenceBoundary, 1), "#05-" + i);
Assert.GreaterOrEqual(ConvergenceBoundary, (y[i] - z[i]).Magnitude, "#05-" + i);
}
}
@ -238,7 +238,7 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Complex32.Solvers.Iterat
// Now compare the vectors
for (var i = 0; i < y.Count; i++)
{
Assert.IsTrue((y[i] - z[i]).Magnitude.IsSmaller(ConvergenceBoundary, 1), "#05-" + i);
Assert.GreaterOrEqual(ConvergenceBoundary, (y[i] - z[i]).Magnitude, "#05-" + i);
}
}

6
src/UnitTests/LinearAlgebraTests/Complex32/Solvers/Iterative/MlkBiCgStabTest.cs

@ -117,7 +117,7 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Complex32.Solvers.Iterat
// Now compare the vectors
for (var i = 0; i < y.Count; i++)
{
Assert.IsTrue((y[i] - z[i]).Magnitude.IsSmaller(ConvergenceBoundary, 1), "#05-" + i);
Assert.GreaterOrEqual(ConvergenceBoundary, (y[i] - z[i]).Magnitude, "#05-" + i);
}
}
@ -161,7 +161,7 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Complex32.Solvers.Iterat
// Now compare the vectors
for (var i = 0; i < y.Count; i++)
{
Assert.IsTrue((y[i] - z[i]).Magnitude.IsSmaller(ConvergenceBoundary, 1), "#05-" + i);
Assert.GreaterOrEqual(ConvergenceBoundary, (y[i] - z[i]).Magnitude, "#05-" + i);
}
}
@ -238,7 +238,7 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Complex32.Solvers.Iterat
// Now compare the vectors
for (var i = 0; i < y.Count; i++)
{
Assert.IsTrue((y[i] - z[i]).Magnitude.IsSmaller(ConvergenceBoundary, 1), "#05-" + i);
Assert.GreaterOrEqual(ConvergenceBoundary, (y[i] - z[i]).Magnitude, "#05-" + i);
}
}

6
src/UnitTests/LinearAlgebraTests/Complex32/Solvers/Iterative/TFQMRTest.cs

@ -117,7 +117,7 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Complex32.Solvers.Iterat
// Now compare the vectors
for (var i = 0; i < y.Count; i++)
{
Assert.IsTrue((y[i] - z[i]).Magnitude.IsSmaller(ConvergenceBoundary, 1), "#05-" + i);
Assert.GreaterOrEqual(ConvergenceBoundary, (y[i] - z[i]).Magnitude, "#05-" + i);
}
}
@ -161,7 +161,7 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Complex32.Solvers.Iterat
// Now compare the vectors
for (var i = 0; i < y.Count; i++)
{
Assert.IsTrue((y[i] - z[i]).Magnitude.IsSmaller(ConvergenceBoundary, 1), "#05-" + i);
Assert.GreaterOrEqual(ConvergenceBoundary, (y[i] - z[i]).Magnitude, "#05-" + i);
}
}
@ -238,7 +238,7 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Complex32.Solvers.Iterat
// Now compare the vectors
for (var i = 0; i < y.Count; i++)
{
Assert.IsTrue((y[i] - z[i]).Magnitude.IsSmaller(1e-4f, 1), "#05-" + i);
Assert.GreaterOrEqual(ConvergenceBoundary, (y[i] - z[i]).Magnitude, "#05-" + i);
}
}

10
src/UnitTests/LinearAlgebraTests/Double/Solvers/Iterative/BiCgStabTest.cs

@ -115,7 +115,7 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Double.Solvers.Iterative
// Now compare the vectors
for (var i = 0; i < y.Count; i++)
{
Assert.IsTrue((y[i] - z[i]).IsSmaller(ConvergenceBoundary, 1), "#05-" + i);
Assert.GreaterOrEqual(ConvergenceBoundary, Math.Abs(y[i] - z[i]), "#05-" + i);
}
}
@ -159,7 +159,7 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Double.Solvers.Iterative
// Now compare the vectors
for (var i = 0; i < y.Count; i++)
{
Assert.IsTrue((y[i] - z[i]).IsSmaller(ConvergenceBoundary, 1), "#05-" + i);
Assert.GreaterOrEqual(ConvergenceBoundary, Math.Abs(y[i] - z[i]), "#05-" + i);
}
}
@ -236,11 +236,7 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Double.Solvers.Iterative
// Now compare the vectors
for (var i = 0; i < y.Count; i++)
{
#if !PORTABLE
Assert.IsTrue(Math.Abs(y[i] - z[i]).IsSmaller(ConvergenceBoundary, 1), "#05-" + i);
#else
Assert.IsTrue(Math.Abs(y[i] - z[i]).IsSmaller(ConvergenceBoundary * 100.0, 1), "#05-" + i);
#endif
Assert.GreaterOrEqual(ConvergenceBoundary, Math.Abs(y[i] - z[i]), "#05-" + i);
}
}

10
src/UnitTests/LinearAlgebraTests/Double/Solvers/Iterative/GpBiCgTest.cs

@ -115,7 +115,7 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Double.Solvers.Iterative
// Now compare the vectors
for (var i = 0; i < y.Count; i++)
{
Assert.IsTrue((y[i] - z[i]).IsSmaller(ConvergenceBoundary, 1), "#05-" + i);
Assert.GreaterOrEqual(ConvergenceBoundary, Math.Abs(y[i] - z[i]), "#05-" + i);
}
}
@ -159,7 +159,7 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Double.Solvers.Iterative
// Now compare the vectors
for (var i = 0; i < y.Count; i++)
{
Assert.IsTrue((y[i] - z[i]).IsSmaller(ConvergenceBoundary, 1), "#05-" + i);
Assert.GreaterOrEqual(ConvergenceBoundary, Math.Abs(y[i] - z[i]), "#05-" + i);
}
}
@ -236,11 +236,7 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Double.Solvers.Iterative
// Now compare the vectors
for (var i = 0; i < y.Count; i++)
{
#if !PORTABLE
Assert.IsTrue(Math.Abs(y[i] - z[i]).IsSmaller(ConvergenceBoundary, 1), "#05-" + i);
#else
Assert.IsTrue(Math.Abs(y[i] - z[i]).IsSmaller(ConvergenceBoundary * 100.0, 1), "#05-" + i);
#endif
Assert.GreaterOrEqual(ConvergenceBoundary, Math.Abs(y[i] - z[i]), "#05-" + i);
}
}

10
src/UnitTests/LinearAlgebraTests/Double/Solvers/Iterative/MlkBiCgStabTest.cs

@ -115,7 +115,7 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Double.Solvers.Iterative
// Now compare the vectors
for (var i = 0; i < y.Count; i++)
{
Assert.IsTrue((y[i] - z[i]).IsSmaller(ConvergenceBoundary, 1), "#05-" + i);
Assert.GreaterOrEqual(ConvergenceBoundary, Math.Abs(y[i] - z[i]), "#05-" + i);
}
}
@ -159,7 +159,7 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Double.Solvers.Iterative
// Now compare the vectors
for (var i = 0; i < y.Count; i++)
{
Assert.IsTrue((y[i] - z[i]).IsSmaller(ConvergenceBoundary, 1), "#05-" + i);
Assert.GreaterOrEqual(ConvergenceBoundary, Math.Abs(y[i] - z[i]), "#05-" + i);
}
}
@ -236,11 +236,7 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Double.Solvers.Iterative
// Now compare the vectors
for (var i = 0; i < y.Count; i++)
{
#if !PORTABLE
Assert.IsTrue(Math.Abs(y[i] - z[i]).IsSmaller(ConvergenceBoundary, 1), "#05-" + i);
#else
Assert.IsTrue(Math.Abs(y[i] - z[i]).IsSmaller(ConvergenceBoundary * 100.0, 1), "#05-" + i);
#endif
Assert.GreaterOrEqual(ConvergenceBoundary, Math.Abs(y[i] - z[i]), "#05-" + i);
}
}

6
src/UnitTests/LinearAlgebraTests/Double/Solvers/Iterative/TFQMRTest.cs

@ -115,7 +115,7 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Double.Solvers.Iterative
// Now compare the vectors
for (var i = 0; i < y.Count; i++)
{
Assert.IsTrue((y[i] - z[i]).IsSmaller(ConvergenceBoundary, 1), "#05-" + i);
Assert.GreaterOrEqual(ConvergenceBoundary, Math.Abs(y[i] - z[i]), "#05-" + i);
}
}
@ -159,7 +159,7 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Double.Solvers.Iterative
// Now compare the vectors
for (var i = 0; i < y.Count; i++)
{
Assert.IsTrue((y[i] - z[i]).IsSmaller(ConvergenceBoundary, 1), "#05-" + i);
Assert.GreaterOrEqual(ConvergenceBoundary, Math.Abs(y[i] - z[i]), "#05-" + i);
}
}
@ -236,7 +236,7 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Double.Solvers.Iterative
// Now compare the vectors
for (var i = 0; i < y.Count; i++)
{
Assert.IsTrue(Math.Abs(y[i] - z[i]).IsSmaller(ConvergenceBoundary, 1), "#05-" + i);
Assert.GreaterOrEqual(ConvergenceBoundary, Math.Abs(y[i] - z[i]), "#05-" + i);
}
}

6
src/UnitTests/LinearAlgebraTests/Single/Solvers/Iterative/BiCgStabTest.cs

@ -115,7 +115,7 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Single.Solvers.Iterative
// Now compare the vectors
for (var i = 0; i < y.Count; i++)
{
Assert.IsTrue((y[i] - z[i]).IsSmaller(ConvergenceBoundary, 1), "#05-" + i);
Assert.GreaterOrEqual(ConvergenceBoundary, Math.Abs(y[i] - z[i]), "#05-" + i);
}
}
@ -159,7 +159,7 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Single.Solvers.Iterative
// Now compare the vectors
for (var i = 0; i < y.Count; i++)
{
Assert.IsTrue((y[i] - z[i]).IsSmaller(ConvergenceBoundary, 1), "#05-" + i);
Assert.GreaterOrEqual(ConvergenceBoundary, Math.Abs(y[i] - z[i]), "#05-" + i);
}
}
@ -236,7 +236,7 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Single.Solvers.Iterative
// Now compare the vectors
for (var i = 0; i < y.Count; i++)
{
Assert.IsTrue(Math.Abs(y[i] - z[i]).IsSmaller(ConvergenceBoundary, 1), "#05-" + i);
Assert.GreaterOrEqual(ConvergenceBoundary, Math.Abs(y[i] - z[i]), "#05-" + i);
}
}

6
src/UnitTests/LinearAlgebraTests/Single/Solvers/Iterative/GpBiCgTest.cs

@ -115,7 +115,7 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Single.Solvers.Iterative
// Now compare the vectors
for (var i = 0; i < y.Count; i++)
{
Assert.IsTrue((y[i] - z[i]).IsSmaller(ConvergenceBoundary, 1), "#05-" + i);
Assert.GreaterOrEqual(ConvergenceBoundary, Math.Abs(y[i] - z[i]), "#05-" + i);
}
}
@ -159,7 +159,7 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Single.Solvers.Iterative
// Now compare the vectors
for (var i = 0; i < y.Count; i++)
{
Assert.IsTrue((y[i] - z[i]).IsSmaller(ConvergenceBoundary, 1), "#05-" + i);
Assert.GreaterOrEqual(ConvergenceBoundary, Math.Abs(y[i] - z[i]), "#05-" + i);
}
}
@ -236,7 +236,7 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Single.Solvers.Iterative
// Now compare the vectors
for (var i = 0; i < y.Count; i++)
{
Assert.IsTrue(Math.Abs(y[i] - z[i]).IsSmaller(ConvergenceBoundary, 1), "#05-" + i);
Assert.GreaterOrEqual(ConvergenceBoundary, Math.Abs(y[i] - z[i]), "#05-" + i);
}
}

6
src/UnitTests/LinearAlgebraTests/Single/Solvers/Iterative/MlkBiCgStabTest.cs

@ -116,7 +116,7 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Single.Solvers.Iterative
// Now compare the vectors
for (var i = 0; i < y.Count; i++)
{
Assert.IsTrue((y[i] - z[i]).IsSmaller(ConvergenceBoundary, 1), "#05-" + i);
Assert.GreaterOrEqual(ConvergenceBoundary, Math.Abs(y[i] - z[i]), "#05-" + i);
}
}
@ -160,7 +160,7 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Single.Solvers.Iterative
// Now compare the vectors
for (var i = 0; i < y.Count; i++)
{
Assert.IsTrue((y[i] - z[i]).IsSmaller(ConvergenceBoundary, 1), "#05-" + i);
Assert.GreaterOrEqual(ConvergenceBoundary, Math.Abs(y[i] - z[i]), "#05-" + i);
}
}
@ -251,7 +251,7 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Single.Solvers.Iterative
// Now compare the vectors
for (var i = 0; i < y.Count; i++)
{
Assert.IsTrue(Math.Abs(y[i] - z[i]).IsSmaller(ConvergenceBoundary, 1), "#04-" + i);
Assert.GreaterOrEqual(ConvergenceBoundary, Math.Abs(y[i] - z[i]), "#05-" + i);
}
return;

6
src/UnitTests/LinearAlgebraTests/Single/Solvers/Iterative/TFQMRTest.cs

@ -115,7 +115,7 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Single.Solvers.Iterative
// Now compare the vectors
for (var i = 0; i < y.Count; i++)
{
Assert.IsTrue((y[i] - z[i]).IsSmaller(ConvergenceBoundary, 1), "#05-" + i);
Assert.GreaterOrEqual(ConvergenceBoundary, Math.Abs(y[i] - z[i]), "#05-" + i);
}
}
@ -159,7 +159,7 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Single.Solvers.Iterative
// Now compare the vectors
for (var i = 0; i < y.Count; i++)
{
Assert.IsTrue((y[i] - z[i]).IsSmaller(ConvergenceBoundary, 1), "#05-" + i);
Assert.GreaterOrEqual(ConvergenceBoundary, Math.Abs(y[i] - z[i]), "#05-" + i);
}
}
@ -236,7 +236,7 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Single.Solvers.Iterative
// Now compare the vectors
for (var i = 0; i < y.Count; i++)
{
Assert.IsTrue(Math.Abs(y[i] - z[i]).IsSmaller(ConvergenceBoundary, 1), "#05-" + i);
Assert.GreaterOrEqual(ConvergenceBoundary, Math.Abs(y[i] - z[i]), "#05-" + i);
}
}

356
src/UnitTests/PrecisionTest.cs

@ -787,53 +787,53 @@ namespace MathNet.Numerics.UnitTests
public void IsLargerWithMaxNumbersBetween()
{
// compare zero and negative zero
Assert.IsFalse(Precision.IsLarger(0, -0, 1));
Assert.IsFalse(Precision.IsLargerNumbersBetween(0, -0, 1));
// compare two nearby numbers
Assert.IsFalse(1.0.IsLarger(1.0 + (3 * _doublePrecision), 1));
Assert.IsFalse(1.0.IsLarger(1.0 + _doublePrecision, 1));
Assert.IsFalse(1.0.IsLarger(1.0 - _doublePrecision, 1));
Assert.IsTrue(1.0.IsLarger(1.0 - (3 * _doublePrecision), 1));
Assert.IsFalse(1.0.IsLargerNumbersBetween(1.0 + (3 * _doublePrecision), 1));
Assert.IsFalse(1.0.IsLargerNumbersBetween(1.0 + _doublePrecision, 1));
Assert.IsFalse(1.0.IsLargerNumbersBetween(1.0 - _doublePrecision, 1));
Assert.IsTrue(1.0.IsLargerNumbersBetween(1.0 - (3 * _doublePrecision), 1));
// compare with the two numbers reversed in compare order
Assert.IsTrue((1.0 + (3 * _doublePrecision)).IsLarger(1.0, 1));
Assert.IsFalse((1.0 + _doublePrecision).IsLarger(1.0, 1));
Assert.IsFalse((1.0 - _doublePrecision).IsLarger(1.0, 1));
Assert.IsFalse((1.0 - (3 * _doublePrecision)).IsLarger(1.0, 1));
Assert.IsTrue((1.0 + (3 * _doublePrecision)).IsLargerNumbersBetween(1.0, 1));
Assert.IsFalse((1.0 + _doublePrecision).IsLargerNumbersBetween(1.0, 1));
Assert.IsFalse((1.0 - _doublePrecision).IsLargerNumbersBetween(1.0, 1));
Assert.IsFalse((1.0 - (3 * _doublePrecision)).IsLargerNumbersBetween(1.0, 1));
// compare two slightly more different numbers
Assert.IsFalse(1.0.IsLarger(1.0 + (10 * _doublePrecision), 1));
Assert.IsFalse(1.0.IsLarger(1.0 + (10 * _doublePrecision), 10));
Assert.IsFalse(1.0.IsLarger(1.0 - (10 * _doublePrecision), 10));
Assert.IsTrue(1.0.IsLarger(1.0 - (10 * _doublePrecision), 1));
Assert.IsFalse(1.0.IsLargerNumbersBetween(1.0 + (10 * _doublePrecision), 1));
Assert.IsFalse(1.0.IsLargerNumbersBetween(1.0 + (10 * _doublePrecision), 10));
Assert.IsFalse(1.0.IsLargerNumbersBetween(1.0 - (10 * _doublePrecision), 10));
Assert.IsTrue(1.0.IsLargerNumbersBetween(1.0 - (10 * _doublePrecision), 1));
// compare different numbers
Assert.IsTrue(2.0.IsLarger(1.0, 1));
Assert.IsFalse(1.0.IsLarger(2.0, 1));
Assert.IsTrue(2.0.IsLargerNumbersBetween(1.0, 1));
Assert.IsFalse(1.0.IsLargerNumbersBetween(2.0, 1));
// compare different numbers with large tolerance
Assert.IsFalse(1.0.IsLarger(1.0 + (1e5 * _doublePrecision), 1));
Assert.IsFalse(1.0.IsLarger(1.0 - (1e5 * _doublePrecision), 200000));
Assert.IsTrue(1.0.IsLarger(1.0 - (1e5 * _doublePrecision), 1));
Assert.IsFalse(1.0.IsLargerNumbersBetween(1.0 + (1e5 * _doublePrecision), 1));
Assert.IsFalse(1.0.IsLargerNumbersBetween(1.0 - (1e5 * _doublePrecision), 200000));
Assert.IsTrue(1.0.IsLargerNumbersBetween(1.0 - (1e5 * _doublePrecision), 1));
// compare inf & inf
Assert.IsFalse(double.PositiveInfinity.IsLarger(double.PositiveInfinity, 1));
Assert.IsFalse(double.NegativeInfinity.IsLarger(double.NegativeInfinity, 1));
Assert.IsFalse(double.PositiveInfinity.IsLargerNumbersBetween(double.PositiveInfinity, 1));
Assert.IsFalse(double.NegativeInfinity.IsLargerNumbersBetween(double.NegativeInfinity, 1));
// compare -inf and inf
Assert.IsTrue(double.PositiveInfinity.IsLarger(double.NegativeInfinity, 1));
Assert.IsFalse(double.NegativeInfinity.IsLarger(double.PositiveInfinity, 1));
Assert.IsTrue(double.PositiveInfinity.IsLargerNumbersBetween(double.NegativeInfinity, 1));
Assert.IsFalse(double.NegativeInfinity.IsLargerNumbersBetween(double.PositiveInfinity, 1));
// compare inf and non-inf
Assert.IsTrue(double.PositiveInfinity.IsLarger(1.0, 1));
Assert.IsFalse(1.0.IsLarger(double.PositiveInfinity, 1));
Assert.IsTrue(double.PositiveInfinity.IsLargerNumbersBetween(1.0, 1));
Assert.IsFalse(1.0.IsLargerNumbersBetween(double.PositiveInfinity, 1));
Assert.IsFalse(double.NegativeInfinity.IsLarger(1.0, 1));
Assert.IsTrue(1.0.IsLarger(double.NegativeInfinity, 1));
Assert.IsFalse(double.NegativeInfinity.IsLargerNumbersBetween(1.0, 1));
Assert.IsTrue(1.0.IsLargerNumbersBetween(double.NegativeInfinity, 1));
// compare tiny numbers with opposite signs
Assert.IsTrue(double.Epsilon.IsLarger(-double.Epsilon, 1));
Assert.IsFalse((-double.Epsilon).IsLarger(double.Epsilon, 1));
Assert.IsTrue(double.Epsilon.IsLargerNumbersBetween(-double.Epsilon, 1));
Assert.IsFalse((-double.Epsilon).IsLargerNumbersBetween(double.Epsilon, 1));
}
/// <summary>
@ -842,39 +842,44 @@ namespace MathNet.Numerics.UnitTests
[Test]
public void IsLargerWithDecimalPlaces()
{
Assert.IsFalse(Precision.IsLargerDecimal(1, double.NaN, 2));
Assert.IsFalse(double.NaN.IsLargerDecimal(2, 2));
Assert.IsFalse(double.NaN.IsLargerDecimal(double.NaN, 2));
Assert.IsFalse(Precision.IsLarger(1, double.NaN, 2));
Assert.IsFalse(double.NaN.IsLarger(2, 2));
Assert.IsFalse(double.NaN.IsLarger(double.NaN, 2));
Assert.IsFalse(double.NegativeInfinity.IsLargerDecimal(2, 2));
Assert.IsTrue(Precision.IsLargerDecimal(1, double.NegativeInfinity, 2));
Assert.IsFalse(double.NegativeInfinity.IsLarger(2, 2));
Assert.IsTrue(Precision.IsLarger(1, double.NegativeInfinity, 2));
Assert.IsTrue(double.PositiveInfinity.IsLargerDecimal(2, 2));
Assert.IsFalse(Precision.IsLargerDecimal(1, double.PositiveInfinity, 2));
Assert.IsTrue(double.PositiveInfinity.IsLarger(2, 2));
Assert.IsFalse(Precision.IsLarger(1, double.PositiveInfinity, 2));
Assert.IsFalse(double.NegativeInfinity.IsLargerDecimal(double.PositiveInfinity, 2));
Assert.IsTrue(double.PositiveInfinity.IsLargerDecimal(double.NegativeInfinity, 2));
Assert.IsFalse(double.NegativeInfinity.IsLarger(double.PositiveInfinity, 2));
Assert.IsTrue(double.PositiveInfinity.IsLarger(double.NegativeInfinity, 2));
Assert.IsFalse(double.PositiveInfinity.IsLargerDecimal(double.PositiveInfinity, 2));
Assert.IsFalse(double.NegativeInfinity.IsLargerDecimal(double.NegativeInfinity, 2));
Assert.IsFalse(double.PositiveInfinity.IsLarger(double.PositiveInfinity, 2));
Assert.IsFalse(double.NegativeInfinity.IsLarger(double.NegativeInfinity, 2));
Assert.IsFalse(1.0.IsLargerDecimal(1.006, 2));
Assert.IsFalse(1.0.IsLargerDecimal(1.004, 2));
Assert.IsFalse(1.0.IsLargerDecimal(0.996, 2));
Assert.IsTrue(1.0.IsLargerDecimal(0.994, 2));
Assert.IsFalse(1.0.IsLarger(1.006, 2));
Assert.IsFalse(1.0.IsLarger(1.004, 2));
Assert.IsFalse(1.0.IsLarger(0.996, 2));
Assert.IsTrue(1.0.IsLarger(0.994, 2));
Assert.IsFalse(100.0.IsLargerDecimal(100.6, 2));
Assert.IsFalse(100.0.IsLargerDecimal(100.4, 2));
Assert.IsFalse(100.0.IsLargerDecimal(99.6, 2));
Assert.IsTrue(100.0.IsLargerDecimal(99.4, 2));
Assert.IsFalse(1.0.IsLargerRelative(1.006, 2));
Assert.IsFalse(1.0.IsLargerRelative(1.004, 2));
Assert.IsFalse(1.0.IsLargerRelative(0.996, 2));
Assert.IsTrue(1.0.IsLargerRelative(0.994, 2));
Assert.IsFalse(100.0.IsLargerRelative(100.6, 2));
Assert.IsFalse(100.0.IsLargerRelative(100.4, 2));
Assert.IsFalse(100.0.IsLargerRelative(99.6, 2));
Assert.IsTrue(100.0.IsLargerRelative(99.4, 2));
var max = 0.4 * Math.Pow(10, _doublePrecision.Magnitude());
Assert.IsFalse(0.0.IsLargerDecimal(max, -_doublePrecision.Magnitude()));
Assert.IsFalse(0.0.IsLargerDecimal(-max, -_doublePrecision.Magnitude()));
Assert.IsFalse(0.0.IsLarger(max, -_doublePrecision.Magnitude()));
Assert.IsFalse(0.0.IsLarger(-max, -_doublePrecision.Magnitude()));
max = 0.6 * Math.Pow(10, _doublePrecision.Magnitude());
Assert.IsFalse(0.0.IsLargerDecimal(max, -_doublePrecision.Magnitude()));
Assert.IsTrue(0.0.IsLargerDecimal(-max, -_doublePrecision.Magnitude()));
Assert.IsFalse(0.0.IsLarger(max, -_doublePrecision.Magnitude()));
Assert.IsTrue(0.0.IsLarger(-max, -_doublePrecision.Magnitude()));
}
/// <summary>
@ -884,53 +889,53 @@ namespace MathNet.Numerics.UnitTests
public void IsSmallerWithMaxNumbersBetween()
{
// compare zero and negative zero
Assert.IsFalse(Precision.IsSmaller(0, -0, 1));
Assert.IsFalse(Precision.IsSmallerNumbersBetween(0, -0, 1));
// compare two nearby numbers
Assert.IsTrue(1.0.IsSmaller(1.0 + (3 * _doublePrecision), 1));
Assert.IsFalse(1.0.IsSmaller(1.0 + _doublePrecision, 1));
Assert.IsFalse(1.0.IsSmaller(1.0 - _doublePrecision, 1));
Assert.IsFalse(1.0.IsSmaller(1.0 - (3 * _doublePrecision), 1));
Assert.IsTrue(1.0.IsSmallerNumbersBetween(1.0 + (3 * _doublePrecision), 1));
Assert.IsFalse(1.0.IsSmallerNumbersBetween(1.0 + _doublePrecision, 1));
Assert.IsFalse(1.0.IsSmallerNumbersBetween(1.0 - _doublePrecision, 1));
Assert.IsFalse(1.0.IsSmallerNumbersBetween(1.0 - (3 * _doublePrecision), 1));
// compare with the two numbers reversed in compare order
Assert.IsFalse((1.0 + (3 * _doublePrecision)).IsSmaller(1.0, 1));
Assert.IsFalse((1.0 + _doublePrecision).IsSmaller(1.0, 1));
Assert.IsFalse((1.0 - _doublePrecision).IsSmaller(1.0, 1));
Assert.IsTrue((1.0 - (3 * _doublePrecision)).IsSmaller(1.0, 1));
Assert.IsFalse((1.0 + (3 * _doublePrecision)).IsSmallerNumbersBetween(1.0, 1));
Assert.IsFalse((1.0 + _doublePrecision).IsSmallerNumbersBetween(1.0, 1));
Assert.IsFalse((1.0 - _doublePrecision).IsSmallerNumbersBetween(1.0, 1));
Assert.IsTrue((1.0 - (3 * _doublePrecision)).IsSmallerNumbersBetween(1.0, 1));
// compare two slightly more different numbers
Assert.IsTrue(1.0.IsSmaller(1.0 + (10 * _doublePrecision), 1));
Assert.IsFalse(1.0.IsSmaller(1.0 + (10 * _doublePrecision), 10));
Assert.IsFalse(1.0.IsSmaller(1.0 - (10 * _doublePrecision), 10));
Assert.IsFalse(1.0.IsSmaller(1.0 - (10 * _doublePrecision), 1));
Assert.IsTrue(1.0.IsSmallerNumbersBetween(1.0 + (10 * _doublePrecision), 1));
Assert.IsFalse(1.0.IsSmallerNumbersBetween(1.0 + (10 * _doublePrecision), 10));
Assert.IsFalse(1.0.IsSmallerNumbersBetween(1.0 - (10 * _doublePrecision), 10));
Assert.IsFalse(1.0.IsSmallerNumbersBetween(1.0 - (10 * _doublePrecision), 1));
// compare different numbers
Assert.IsFalse(2.0.IsSmaller(1.0, 1));
Assert.IsTrue(1.0.IsSmaller(2.0, 1));
Assert.IsFalse(2.0.IsSmallerNumbersBetween(1.0, 1));
Assert.IsTrue(1.0.IsSmallerNumbersBetween(2.0, 1));
// compare different numbers with large tolerance
Assert.IsTrue(1.0.IsSmaller(1.0 + (1e5 * _doublePrecision), 1));
Assert.IsFalse(1.0.IsSmaller(1.0 - (1e5 * _doublePrecision), 200000));
Assert.IsFalse(1.0.IsSmaller(1.0 - (1e5 * _doublePrecision), 1));
Assert.IsTrue(1.0.IsSmallerNumbersBetween(1.0 + (1e5 * _doublePrecision), 1));
Assert.IsFalse(1.0.IsSmallerNumbersBetween(1.0 - (1e5 * _doublePrecision), 200000));
Assert.IsFalse(1.0.IsSmallerNumbersBetween(1.0 - (1e5 * _doublePrecision), 1));
// compare inf & inf
Assert.IsFalse(double.PositiveInfinity.IsSmaller(double.PositiveInfinity, 1));
Assert.IsFalse(double.NegativeInfinity.IsSmaller(double.NegativeInfinity, 1));
Assert.IsFalse(double.PositiveInfinity.IsSmallerNumbersBetween(double.PositiveInfinity, 1));
Assert.IsFalse(double.NegativeInfinity.IsSmallerNumbersBetween(double.NegativeInfinity, 1));
// compare -inf and inf
Assert.IsFalse(double.PositiveInfinity.IsSmaller(double.NegativeInfinity, 1));
Assert.IsTrue(double.NegativeInfinity.IsSmaller(double.PositiveInfinity, 1));
Assert.IsFalse(double.PositiveInfinity.IsSmallerNumbersBetween(double.NegativeInfinity, 1));
Assert.IsTrue(double.NegativeInfinity.IsSmallerNumbersBetween(double.PositiveInfinity, 1));
// compare inf and non-inf
Assert.IsFalse(double.PositiveInfinity.IsSmaller(1.0, 1));
Assert.IsTrue(1.0.IsSmaller(double.PositiveInfinity, 1));
Assert.IsFalse(double.PositiveInfinity.IsSmallerNumbersBetween(1.0, 1));
Assert.IsTrue(1.0.IsSmallerNumbersBetween(double.PositiveInfinity, 1));
Assert.IsTrue(double.NegativeInfinity.IsSmaller(1.0, 1));
Assert.IsFalse(1.0.IsSmaller(double.NegativeInfinity, 1));
Assert.IsTrue(double.NegativeInfinity.IsSmallerNumbersBetween(1.0, 1));
Assert.IsFalse(1.0.IsSmallerNumbersBetween(double.NegativeInfinity, 1));
// compare tiny numbers with opposite signs
Assert.IsFalse(double.Epsilon.IsSmaller(-double.Epsilon, 1));
Assert.IsTrue((-double.Epsilon).IsSmaller(double.Epsilon, 1));
Assert.IsFalse(double.Epsilon.IsSmallerNumbersBetween(-double.Epsilon, 1));
Assert.IsTrue((-double.Epsilon).IsSmallerNumbersBetween(double.Epsilon, 1));
}
/// <summary>
@ -939,39 +944,44 @@ namespace MathNet.Numerics.UnitTests
[Test]
public void IsSmallerWithDecimalPlaces()
{
Assert.IsFalse(Precision.IsSmallerDecimal(1, double.NaN, 2));
Assert.IsFalse(double.NaN.IsSmallerDecimal(2, 2));
Assert.IsFalse(double.NaN.IsSmallerDecimal(double.NaN, 2));
Assert.IsFalse(Precision.IsSmaller(1, double.NaN, 2));
Assert.IsFalse(double.NaN.IsSmaller(2, 2));
Assert.IsFalse(double.NaN.IsSmaller(double.NaN, 2));
Assert.IsTrue(double.NegativeInfinity.IsSmaller(2, 2));
Assert.IsFalse(Precision.IsSmaller(1, double.NegativeInfinity, 2));
Assert.IsTrue(double.NegativeInfinity.IsSmallerDecimal(2, 2));
Assert.IsFalse(Precision.IsSmallerDecimal(1, double.NegativeInfinity, 2));
Assert.IsFalse(double.PositiveInfinity.IsSmaller(2, 2));
Assert.IsTrue(Precision.IsSmaller(1, double.PositiveInfinity, 2));
Assert.IsFalse(double.PositiveInfinity.IsSmallerDecimal(2, 2));
Assert.IsTrue(Precision.IsSmallerDecimal(1, double.PositiveInfinity, 2));
Assert.IsTrue(double.NegativeInfinity.IsSmaller(double.PositiveInfinity, 2));
Assert.IsFalse(double.PositiveInfinity.IsSmaller(double.NegativeInfinity, 2));
Assert.IsTrue(double.NegativeInfinity.IsSmallerDecimal(double.PositiveInfinity, 2));
Assert.IsFalse(double.PositiveInfinity.IsSmallerDecimal(double.NegativeInfinity, 2));
Assert.IsFalse(double.PositiveInfinity.IsSmaller(double.PositiveInfinity, 2));
Assert.IsFalse(double.NegativeInfinity.IsSmaller(double.NegativeInfinity, 2));
Assert.IsFalse(double.PositiveInfinity.IsSmallerDecimal(double.PositiveInfinity, 2));
Assert.IsFalse(double.NegativeInfinity.IsSmallerDecimal(double.NegativeInfinity, 2));
Assert.IsTrue(1.0.IsSmaller(1.006, 2));
Assert.IsFalse(1.0.IsSmaller(1.004, 2));
Assert.IsFalse(1.0.IsSmaller(0.996, 2));
Assert.IsFalse(1.0.IsSmaller(0.994, 2));
Assert.IsTrue(1.0.IsSmallerDecimal(1.006, 2));
Assert.IsFalse(1.0.IsSmallerDecimal(1.004, 2));
Assert.IsFalse(1.0.IsSmallerDecimal(0.996, 2));
Assert.IsFalse(1.0.IsSmallerDecimal(0.994, 2));
Assert.IsTrue(1.0.IsSmallerRelative(1.006, 2));
Assert.IsFalse(1.0.IsSmallerRelative(1.004, 2));
Assert.IsFalse(1.0.IsSmallerRelative(0.996, 2));
Assert.IsFalse(1.0.IsSmallerRelative(0.994, 2));
Assert.IsTrue(100.0.IsSmallerDecimal(100.6, 2));
Assert.IsFalse(100.0.IsSmallerDecimal(100.4, 2));
Assert.IsFalse(100.0.IsSmallerDecimal(99.6, 2));
Assert.IsFalse(100.0.IsSmallerDecimal(99.4, 2));
Assert.IsTrue(100.0.IsSmallerRelative(100.6, 2));
Assert.IsFalse(100.0.IsSmallerRelative(100.4, 2));
Assert.IsFalse(100.0.IsSmallerRelative(99.6, 2));
Assert.IsFalse(100.0.IsSmallerRelative(99.4, 2));
var max = 0.4 * Math.Pow(10, _doublePrecision.Magnitude());
Assert.IsFalse(0.0.IsSmallerDecimal(max, -_doublePrecision.Magnitude()));
Assert.IsFalse(0.0.IsSmallerDecimal(-max, -_doublePrecision.Magnitude()));
Assert.IsFalse(0.0.IsSmaller(max, -_doublePrecision.Magnitude()));
Assert.IsFalse(0.0.IsSmaller(-max, -_doublePrecision.Magnitude()));
max = 0.6 * Math.Pow(10, _doublePrecision.Magnitude());
Assert.IsTrue(0.0.IsSmallerDecimal(max, -_doublePrecision.Magnitude()));
Assert.IsFalse(0.0.IsSmallerDecimal(-max, -_doublePrecision.Magnitude()));
Assert.IsTrue(0.0.IsSmaller(max, -_doublePrecision.Magnitude()));
Assert.IsFalse(0.0.IsSmaller(-max, -_doublePrecision.Magnitude()));
}
/// <summary>
@ -981,7 +991,7 @@ namespace MathNet.Numerics.UnitTests
public void CompareToWithMaxNumbersBetweenWithNegativeNumberThrows()
{
const double Value = 10.0;
Assert.Throws<ArgumentOutOfRangeException>(() => Value.CompareTo(Value, -1));
Assert.Throws<ArgumentOutOfRangeException>(() => Value.CompareToNumbersBetween(Value, -1));
}
/// <summary>
@ -991,7 +1001,7 @@ namespace MathNet.Numerics.UnitTests
public void CompareToWithMaxNumbersBetweenWithZeroNumberThrows()
{
const double Value = 10.0;
Assert.Throws<ArgumentOutOfRangeException>(() => Value.CompareTo(Value, 0));
Assert.Throws<ArgumentOutOfRangeException>(() => Value.CompareToNumbersBetween(Value, 0));
}
/// <summary>
@ -1000,17 +1010,17 @@ namespace MathNet.Numerics.UnitTests
[Test]
public void CompareToWithMaxNumbersBetweenWithInfinityValue()
{
Assert.AreEqual(0, double.PositiveInfinity.CompareTo(double.PositiveInfinity, 1));
Assert.AreEqual(0, double.NegativeInfinity.CompareTo(double.NegativeInfinity, 1));
Assert.AreEqual(0, double.PositiveInfinity.CompareToNumbersBetween(double.PositiveInfinity, 1));
Assert.AreEqual(0, double.NegativeInfinity.CompareToNumbersBetween(double.NegativeInfinity, 1));
Assert.AreEqual(1, double.PositiveInfinity.CompareTo(double.NegativeInfinity, 1));
Assert.AreEqual(-1, double.NegativeInfinity.CompareTo(double.PositiveInfinity, 1));
Assert.AreEqual(1, double.PositiveInfinity.CompareToNumbersBetween(double.NegativeInfinity, 1));
Assert.AreEqual(-1, double.NegativeInfinity.CompareToNumbersBetween(double.PositiveInfinity, 1));
Assert.AreEqual(-1, Precision.CompareTo(1, double.PositiveInfinity, 1));
Assert.AreEqual(1, double.PositiveInfinity.CompareTo(1, 1));
Assert.AreEqual(-1, Precision.CompareToNumbersBetween(1, double.PositiveInfinity, 1));
Assert.AreEqual(1, double.PositiveInfinity.CompareToNumbersBetween(1, 1));
Assert.AreEqual(1, Precision.CompareTo(1, double.NegativeInfinity, 1));
Assert.AreEqual(-1, double.NegativeInfinity.CompareTo(1, 1));
Assert.AreEqual(1, Precision.CompareToNumbersBetween(1, double.NegativeInfinity, 1));
Assert.AreEqual(-1, double.NegativeInfinity.CompareToNumbersBetween(1, 1));
}
/// <summary>
@ -1020,9 +1030,9 @@ namespace MathNet.Numerics.UnitTests
public void CompareToWithMaxNumbersBetweenWithNaNValue()
{
// compare nan & not-nan
Assert.AreEqual(1, Precision.CompareTo(1, double.NaN, 1));
Assert.AreEqual(0, double.NaN.CompareTo(double.NaN, 1));
Assert.AreEqual(-1, double.NaN.CompareTo(1, 1));
Assert.AreEqual(1, Precision.CompareToNumbersBetween(1, double.NaN, 1));
Assert.AreEqual(0, double.NaN.CompareToNumbersBetween(double.NaN, 1));
Assert.AreEqual(-1, double.NaN.CompareToNumbersBetween(1, 1));
}
/// <summary>
@ -1031,8 +1041,8 @@ namespace MathNet.Numerics.UnitTests
[Test]
public void CompareToWithMaxNumbersBetweenForValuesThatOverFlowALong()
{
Assert.AreEqual(1, 2.0.CompareTo(-2.0, 1));
Assert.AreEqual(-1, (-2.0).CompareTo(2.0, 1));
Assert.AreEqual(1, 2.0.CompareToNumbersBetween(-2.0, 1));
Assert.AreEqual(-1, (-2.0).CompareToNumbersBetween(2.0, 1));
}
/// <summary>
@ -1042,53 +1052,53 @@ namespace MathNet.Numerics.UnitTests
public void CompareToWithMaxNumbersBetween()
{
// compare zero and negative zero
Assert.AreEqual(0, Precision.CompareTo(0, -0, 1));
Assert.AreEqual(0, Precision.CompareToNumbersBetween(0, -0, 1));
// compare two nearby numbers
Assert.AreEqual(-1, 1.0.CompareTo(1.0 + (3 * _doublePrecision), 1));
Assert.AreEqual(0, 1.0.CompareTo(1.0 + _doublePrecision, 1));
Assert.AreEqual(0, 1.0.CompareTo(1.0 - _doublePrecision, 1));
Assert.AreEqual(1, 1.0.CompareTo(1.0 - (3 * _doublePrecision), 1));
Assert.AreEqual(-1, 1.0.CompareToNumbersBetween(1.0 + (3 * _doublePrecision), 1));
Assert.AreEqual(0, 1.0.CompareToNumbersBetween(1.0 + _doublePrecision, 1));
Assert.AreEqual(0, 1.0.CompareToNumbersBetween(1.0 - _doublePrecision, 1));
Assert.AreEqual(1, 1.0.CompareToNumbersBetween(1.0 - (3 * _doublePrecision), 1));
// compare with the two numbers reversed in compare order
Assert.AreEqual(1, (1.0 + (3 * _doublePrecision)).CompareTo(1.0, 1));
Assert.AreEqual(0, (1.0 + _doublePrecision).CompareTo(1.0, 1));
Assert.AreEqual(0, (1.0 - _doublePrecision).CompareTo(1.0, 1));
Assert.AreEqual(-1, (1.0 - (3 * _doublePrecision)).CompareTo(1.0, 1));
Assert.AreEqual(1, (1.0 + (3 * _doublePrecision)).CompareToNumbersBetween(1.0, 1));
Assert.AreEqual(0, (1.0 + _doublePrecision).CompareToNumbersBetween(1.0, 1));
Assert.AreEqual(0, (1.0 - _doublePrecision).CompareToNumbersBetween(1.0, 1));
Assert.AreEqual(-1, (1.0 - (3 * _doublePrecision)).CompareToNumbersBetween(1.0, 1));
// compare two slightly more different numbers
Assert.AreEqual(-1, 1.0.CompareTo(1.0 + (10 * _doublePrecision), 1));
Assert.AreEqual(0, 1.0.CompareTo(1.0 + (10 * _doublePrecision), 10));
Assert.AreEqual(0, 1.0.CompareTo(1.0 - (10 * _doublePrecision), 10));
Assert.AreEqual(1, 1.0.CompareTo(1.0 - (10 * _doublePrecision), 1));
Assert.AreEqual(-1, 1.0.CompareToNumbersBetween(1.0 + (10 * _doublePrecision), 1));
Assert.AreEqual(0, 1.0.CompareToNumbersBetween(1.0 + (10 * _doublePrecision), 10));
Assert.AreEqual(0, 1.0.CompareToNumbersBetween(1.0 - (10 * _doublePrecision), 10));
Assert.AreEqual(1, 1.0.CompareToNumbersBetween(1.0 - (10 * _doublePrecision), 1));
// compare different numbers
Assert.AreEqual(1, 2.0.CompareTo(1.0, 1));
Assert.AreEqual(-1, 1.0.CompareTo(2.0, 1));
Assert.AreEqual(1, 2.0.CompareToNumbersBetween(1.0, 1));
Assert.AreEqual(-1, 1.0.CompareToNumbersBetween(2.0, 1));
// compare different numbers with large tolerance
Assert.AreEqual(-1, 1.0.CompareTo(1.0 + (1e5 * _doublePrecision), 1));
Assert.AreEqual(0, 1.0.CompareTo(1.0 - (1e5 * _doublePrecision), 200000));
Assert.AreEqual(1, 1.0.CompareTo(1.0 - (1e5 * _doublePrecision), 1));
Assert.AreEqual(-1, 1.0.CompareToNumbersBetween(1.0 + (1e5 * _doublePrecision), 1));
Assert.AreEqual(0, 1.0.CompareToNumbersBetween(1.0 - (1e5 * _doublePrecision), 200000));
Assert.AreEqual(1, 1.0.CompareToNumbersBetween(1.0 - (1e5 * _doublePrecision), 1));
// compare inf & inf
Assert.AreEqual(0, double.PositiveInfinity.CompareTo(double.PositiveInfinity, 1));
Assert.AreEqual(0, double.NegativeInfinity.CompareTo(double.NegativeInfinity, 1));
Assert.AreEqual(0, double.PositiveInfinity.CompareToNumbersBetween(double.PositiveInfinity, 1));
Assert.AreEqual(0, double.NegativeInfinity.CompareToNumbersBetween(double.NegativeInfinity, 1));
// compare -inf and inf
Assert.AreEqual(1, double.PositiveInfinity.CompareTo(double.NegativeInfinity, 1));
Assert.AreEqual(-1, double.NegativeInfinity.CompareTo(double.PositiveInfinity, 1));
Assert.AreEqual(1, double.PositiveInfinity.CompareToNumbersBetween(double.NegativeInfinity, 1));
Assert.AreEqual(-1, double.NegativeInfinity.CompareToNumbersBetween(double.PositiveInfinity, 1));
// compare inf and non-inf
Assert.AreEqual(1, double.PositiveInfinity.CompareTo(1.0, 1));
Assert.AreEqual(-1, 1.0.CompareTo(double.PositiveInfinity, 1));
Assert.AreEqual(1, double.PositiveInfinity.CompareToNumbersBetween(1.0, 1));
Assert.AreEqual(-1, 1.0.CompareToNumbersBetween(double.PositiveInfinity, 1));
Assert.AreEqual(-1, double.NegativeInfinity.CompareTo(1.0, 1));
Assert.AreEqual(1, 1.0.CompareTo(double.NegativeInfinity, 1));
Assert.AreEqual(-1, double.NegativeInfinity.CompareToNumbersBetween(1.0, 1));
Assert.AreEqual(1, 1.0.CompareToNumbersBetween(double.NegativeInfinity, 1));
// compare tiny numbers with opposite signs
Assert.AreEqual(1, double.Epsilon.CompareTo(-double.Epsilon, 1));
Assert.AreEqual(-1, (-double.Epsilon).CompareTo(double.Epsilon, 1));
Assert.AreEqual(1, double.Epsilon.CompareToNumbersBetween(-double.Epsilon, 1));
Assert.AreEqual(-1, (-double.Epsilon).CompareToNumbersBetween(double.Epsilon, 1));
}
/// <summary>
@ -1098,50 +1108,50 @@ namespace MathNet.Numerics.UnitTests
public void CompareToWithDecimalPlaces()
{
// compare zero and negative zero
Assert.AreEqual(0, Precision.CompareToDecimal(0, -0, 1));
Assert.AreEqual(0, Precision.CompareToDecimal(0, -0, Precision.DoubleDecimalPlaces));
Assert.AreEqual(0, Precision.CompareToDecimal(0, -0, Precision.SingleDecimalPlaces));
Assert.AreEqual(0, Precision.CompareTo(0, -0, 1));
Assert.AreEqual(0, Precision.CompareTo(0, -0, Precision.DoubleDecimalPlaces));
Assert.AreEqual(0, Precision.CompareTo(0, -0, Precision.SingleDecimalPlaces));
// compare two nearby numbers
Assert.AreEqual(-1, 1.0.CompareToDecimal(1.0 + 10*_doublePrecision, Precision.DoubleDecimalPlaces));
Assert.AreEqual(0, 1.0.CompareToDecimal(1.0 + _doublePrecision, Precision.DoubleDecimalPlaces));
Assert.AreEqual(0, 1.0.CompareToDecimal(1.0 - _doublePrecision, Precision.DoubleDecimalPlaces));
Assert.AreEqual(1, 1.0.CompareToDecimal(1.0 - 10*_doublePrecision, Precision.DoubleDecimalPlaces));
Assert.AreEqual(-1, 1.0.CompareTo(1.0 + 10*_doublePrecision, Precision.DoubleDecimalPlaces));
Assert.AreEqual(0, 1.0.CompareTo(1.0 + _doublePrecision, Precision.DoubleDecimalPlaces));
Assert.AreEqual(0, 1.0.CompareTo(1.0 - _doublePrecision, Precision.DoubleDecimalPlaces));
Assert.AreEqual(1, 1.0.CompareTo(1.0 - 10*_doublePrecision, Precision.DoubleDecimalPlaces));
// compare with the two numbers reversed in compare order
Assert.AreEqual(1, (1.0 + 10*_doublePrecision).CompareToDecimal(1.0, Precision.DoubleDecimalPlaces));
Assert.AreEqual(0, (1.0 + _doublePrecision).CompareToDecimal(1.0, Precision.DoubleDecimalPlaces));
Assert.AreEqual(0, (1.0 - _doublePrecision).CompareToDecimal(1.0, Precision.DoubleDecimalPlaces));
Assert.AreEqual(-1, (1.0 - 10*_doublePrecision).CompareToDecimal(1.0, Precision.DoubleDecimalPlaces));
Assert.AreEqual(1, (1.0 + 10*_doublePrecision).CompareTo(1.0, Precision.DoubleDecimalPlaces));
Assert.AreEqual(0, (1.0 + _doublePrecision).CompareTo(1.0, Precision.DoubleDecimalPlaces));
Assert.AreEqual(0, (1.0 - _doublePrecision).CompareTo(1.0, Precision.DoubleDecimalPlaces));
Assert.AreEqual(-1, (1.0 - 10*_doublePrecision).CompareTo(1.0, Precision.DoubleDecimalPlaces));
// compare two slightly more different numbers
Assert.AreEqual(-1, 1.0.CompareToDecimal(1.0 + (50*_doublePrecision), Precision.DoubleDecimalPlaces));
Assert.AreEqual(0, 1.0.CompareToDecimal(1.0 + (50*_doublePrecision), Precision.DoubleDecimalPlaces - 2));
Assert.AreEqual(0, 1.0.CompareToDecimal(1.0 - (50*_doublePrecision), Precision.DoubleDecimalPlaces - 2));
Assert.AreEqual(1, 1.0.CompareToDecimal(1.0 - (50*_doublePrecision), Precision.DoubleDecimalPlaces));
Assert.AreEqual(-1, 1.0.CompareTo(1.0 + (50*_doublePrecision), Precision.DoubleDecimalPlaces));
Assert.AreEqual(0, 1.0.CompareTo(1.0 + (50*_doublePrecision), Precision.DoubleDecimalPlaces - 2));
Assert.AreEqual(0, 1.0.CompareTo(1.0 - (50*_doublePrecision), Precision.DoubleDecimalPlaces - 2));
Assert.AreEqual(1, 1.0.CompareTo(1.0 - (50*_doublePrecision), Precision.DoubleDecimalPlaces));
// compare different numbers
Assert.AreEqual(1, 2.0.CompareToDecimal(1.0, Precision.DoubleDecimalPlaces));
Assert.AreEqual(-1, 1.0.CompareToDecimal(2.0, Precision.DoubleDecimalPlaces));
Assert.AreEqual(1, 2.0.CompareTo(1.0, Precision.DoubleDecimalPlaces));
Assert.AreEqual(-1, 1.0.CompareTo(2.0, Precision.DoubleDecimalPlaces));
// compare different numbers with large tolerance
Assert.AreEqual(-1, 1.0.CompareToDecimal(1.0 + (1e5 * _doublePrecision), Precision.DoubleDecimalPlaces));
Assert.AreEqual(0, 1.0.CompareToDecimal(1.0 - (1e5 * _doublePrecision), 10));
Assert.AreEqual(1, 1.0.CompareToDecimal(1.0 - (1e5 * _doublePrecision), Precision.DoubleDecimalPlaces));
Assert.AreEqual(-1, 1.0.CompareTo(1.0 + (1e5 * _doublePrecision), Precision.DoubleDecimalPlaces));
Assert.AreEqual(0, 1.0.CompareTo(1.0 - (1e5 * _doublePrecision), 10));
Assert.AreEqual(1, 1.0.CompareTo(1.0 - (1e5 * _doublePrecision), Precision.DoubleDecimalPlaces));
// compare inf & inf
Assert.AreEqual(0, double.PositiveInfinity.CompareToDecimal(double.PositiveInfinity, Precision.DoubleDecimalPlaces));
Assert.AreEqual(0, double.NegativeInfinity.CompareToDecimal(double.NegativeInfinity, Precision.DoubleDecimalPlaces));
Assert.AreEqual(0, double.PositiveInfinity.CompareTo(double.PositiveInfinity, Precision.DoubleDecimalPlaces));
Assert.AreEqual(0, double.NegativeInfinity.CompareTo(double.NegativeInfinity, Precision.DoubleDecimalPlaces));
// compare -inf and inf
Assert.AreEqual(1, double.PositiveInfinity.CompareToDecimal(double.NegativeInfinity, Precision.DoubleDecimalPlaces));
Assert.AreEqual(-1, double.NegativeInfinity.CompareToDecimal(double.PositiveInfinity, Precision.DoubleDecimalPlaces));
Assert.AreEqual(1, double.PositiveInfinity.CompareTo(double.NegativeInfinity, Precision.DoubleDecimalPlaces));
Assert.AreEqual(-1, double.NegativeInfinity.CompareTo(double.PositiveInfinity, Precision.DoubleDecimalPlaces));
// compare inf and non-inf
Assert.AreEqual(1, double.PositiveInfinity.CompareToDecimal(1.0, Precision.DoubleDecimalPlaces));
Assert.AreEqual(-1, 1.0.CompareToDecimal(double.PositiveInfinity, Precision.DoubleDecimalPlaces));
Assert.AreEqual(-1, double.NegativeInfinity.CompareToDecimal(1.0, Precision.DoubleDecimalPlaces));
Assert.AreEqual(1, 1.0.CompareToDecimal(double.NegativeInfinity, Precision.DoubleDecimalPlaces));
Assert.AreEqual(1, double.PositiveInfinity.CompareTo(1.0, Precision.DoubleDecimalPlaces));
Assert.AreEqual(-1, 1.0.CompareTo(double.PositiveInfinity, Precision.DoubleDecimalPlaces));
Assert.AreEqual(-1, double.NegativeInfinity.CompareTo(1.0, Precision.DoubleDecimalPlaces));
Assert.AreEqual(1, 1.0.CompareTo(double.NegativeInfinity, Precision.DoubleDecimalPlaces));
}
}
}

Loading…
Cancel
Save