Namespaces

Types

Type Precision

Namespace MathNet.Numerics

Utilities for working with floating point numbers.

Useful links:

Methods

Properties

Public static methods

bool AlmostEqual(this double a, double b, long maxNumbersBetween)

Compares two doubles and determines if they are equal to within the tolerance or not. Equality comparison is based on the binary representation.

Determines the 'number' of floating point numbers between two values (i.e. the number of discrete steps between the two numbers) and then checks if that is within the specified tolerance. So if a tolerance of 1 is passed then the result will be true only if the two numbers have the same binary representation OR if they are two adjacent numbers that only differ by one step.

The comparison method used is explained in http://www.cygnus-software.com/papers/comparingfloats/comparingfloats.htm . The article at http://www.extremeoptimization.com/resources/Articles/FPDotNetConceptsAndFormats.aspx explains how to transform the C code to .NET enabled code without using pointers and unsafe code.

Parameters
return bool if both doubles are equal to each other within the specified tolerance; otherwise .
double a The first value.
double b The second value.
long maxNumbersBetween The maximum number of floating point values between the two values. Must be 1 or larger.

bool AlmostEqual(this double a, double b)

Checks whether two real numbers are almost equal.
Parameters
return bool true if the two values differ by no more than 10 * 2^(-52); false otherwise.
double a The first number
double b The second number

bool AlmostEqual<T>(this T a, T b)

Checks whether two structures with precision support are almost equal.
Parameters
return bool true if the two values differ by no more than 10 * 2^(-52); false otherwise.
T a The first structure
T b The second structure

bool AlmostEqualInDecimalPlaces(this double a, double b, int decimalPlaces)

Compares two doubles and determines if they are equal to within the specified number of decimal places or not. If the numbers are very close to zero an absolute difference is compared, otherwise the relative difference is compared.

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 decimalPlaces == 2, then 0.01 will equal between 0.005 and 0.015, but not 0.02 and not 0.00

Parameters
return bool if both doubles are equal to each other within the specified number of decimal places; otherwise .
double a The first value.
double b The second value.
int decimalPlaces The number of decimal places.

bool AlmostEqualListWithError(this IList<double> a, IList<double> b, double maximumError)

Compares two lists of doubles and determines if they are equal within the specified maximum error.
Parameters
return bool if both doubles are almost equal up to the specified maximum error, otherwise.
IList<double> a The first value list.
IList<double> b The second value list.
double maximumError The accuracy required for being almost equal.

bool AlmostEqualListWithError<T>(this IList<T> a, IList<T> b, double maximumError)

Compares two lists of structures with precision support and determines if they are equal within the specified maximum error.
Parameters
return bool if both doubles are almost equal up to the specified maximum error, otherwise.
IList<T> a The first structure list.
IList<T> b The second structure list.
double maximumError The accuracy required for being almost equal.

bool AlmostEqualWithAbsoluteError(this double a, double b, double diff, double maximumAbsoluteError)

Compares two doubles and determines if they are equal within the specified maximum absolute error.
Parameters
return bool if both doubles are almost equal up to the specified maximum absolute error, otherwise.
double a The first value.
double b The second value.
double diff The difference of the two values (according to some norm).
double maximumAbsoluteError The absolute accuracy required for being almost equal.

bool AlmostEqualWithError(this double a, double b, double maximumError)

Compares two doubles and determines if they are equal within the specified maximum error.
Parameters
return bool if both doubles are almost equal up to the specified maximum error, otherwise.
double a The first value.
double b The second value.
double maximumError The accuracy required for being almost equal.

bool AlmostEqualWithError(this double a, double b, double diff, double maximumError)

Compares two doubles and determines if they are equal within the specified maximum error.
Parameters
return bool if both doubles are almost equal up to the specified maximum error, otherwise.
double a The first value.
double b The second value.
double diff The difference of the two values (according to some norm).
double maximumError The accuracy required for being almost equal.

bool AlmostEqualWithError<T>(this T a, T b, double maximumError)

Compares two structure with precision support and determines if they are equal within the specified maximum relative error.
Parameters
return bool if both doubles are almost equal up to the specified maximum relative error, otherwise.
T a The first structure.
T b The second structure.
double maximumError The accuracy required for being almost equal.

bool AlmostEqualWithRelativeError(this double a, double b, double diff, double maximumRelativeError)

Compares two doubles and determines if they are equal within the specified maximum relative error.
Parameters
return bool if both doubles are almost equal up to the specified maximum relative error, otherwise.
double a The first value.
double b The second value.
double diff The difference of the two values (according to some norm).
double maximumRelativeError The relative accuracy required for being almost equal.

bool AlmostZero(this double a, double maximumAbsoluteError)

True if the given number is almost equal to zero, according to the specified absolute accuracy.
Parameters
return bool True if |a| is smaller than maximumAbsoluteError, False otherwise.
double a The real number to check for being almost zero.
double maximumAbsoluteError The absolute threshold for a to consider it as zero.

bool AlmostZero(this double a, long maxNumbersBetween)

True if the given number is almost equal to zero, according to the specified absolute accuracy.
Parameters
return bool True if |a| is less than maxNumbersBetween steps from zero, False otherwise.
double a The real number to check for being almost zero.
long maxNumbersBetween The maximum number of floating point values between the two values. Must be 1 or larger.

bool AlmostZero(this double a)

True if the given number is almost equal to zero.
Parameters
return bool True if |a| is smaller than 10*2^(-52) = 0.22e-14, False otherwise.
double a The real number to check for being almost zero.

double CoerceZero(this double a, int maxNumbersBetween)

Forces small numbers near zero to zero, according to the specified absolute accuracy.
Parameters
return double Zero if |a| is fewer than maxNumbersBetween numbers from zero, a otherwise.
double a The real number to coerce to zero, if it is almost zero.
int maxNumbersBetween The maximum count of numbers between the zero and the number a.

double CoerceZero(this double a)

Forces small numbers near zero to zero.
Parameters
return double Zero if |a| is smaller than 10*2^(-52) = 0.22e-14, a otherwise.
double a The real number to coerce to zero, if it is almost zero.

double CoerceZero(this double a, double maximumAbsoluteError)

Forces small numbers near zero to zero, according to the specified absolute accuracy.
Parameters
return double Zero if |a| is smaller than maximumAbsoluteError, a otherwise.
double a The real number to coerce to zero, if it is almost zero.
double maximumAbsoluteError The absolute threshold for a to consider it as zero.

double CoerceZero(this double a, long maxNumbersBetween)

Forces small numbers near zero to zero, according to the specified absolute accuracy.
Parameters
return double Zero if |a| is fewer than maxNumbersBetween numbers from zero, a otherwise.
double a The real number to coerce to zero, if it is almost zero.
long maxNumbersBetween The maximum count of numbers between the zero and the number a.

int CompareTo(this double a, double b, long maxNumbersBetween)

Compares two doubles and determines which double is bigger.
Parameters
return int
-1a is smaller than b by more than the maxNumbersBetween tolerance.
0a is equal to b within the maxNumbersBetween tolerance.
1a is bigger than b by more than the maxNumbersBetween tolerance.
double a The first value.
double b The second value.
long maxNumbersBetween The maximum error in terms of Units in Last Place (ulps), i.e. the maximum number of decimals that may be different. Must be 1 or larger.

int CompareToInDecimalPlaces(this double a, double b, int decimalPlaces)

Compares two doubles and determines which double is bigger.
Parameters
return int
-1a is smaller than b by more than a magnitude equal to decimalPlaces.
0a is equal to b within a magnitude equal to decimalPlaces.
1a is bigger than b by more than a magnitude equal to decimalPlaces.
double a The first value.
double b The second value.
int decimalPlaces The number of decimal places on which the values must be compared. Must be 1 or larger.

double Decrement(this double value, int count)

Decrements a floating point number to the next smaller number representable by the data type.
The decrementation step length depends on the provided value. Decrement(double.MinValue) will return negative infinity.
Parameters
return double The next smaller floating point value.
double value The value which should be decremented.
int count How many times the number should be decremented.

double Decrement(this double value)

Decrements a floating point number to the next smaller number representable by the data type.
The decrementation step length depends on the provided value. Decrement(double.MinValue) will return negative infinity.
Parameters
return double The next smaller floating point value.
double value The value which should be decremented.

double GetMagnitudeScaledValue(this double value)

Returns the number divided by it's magnitude, effectively returning a number between -10 and 10.
Parameters
return double The value of the number.
double value The value.

double Increment(this double value, int count)

Increments a floating point number to the next bigger number representable by the data type.
The incrementation step length depends on the provided value. Increment(double.MaxValue) will return positive infinity.
Parameters
return double The next larger floating point value.
double value The value which needs to be incremented.
int count How many times the number should be incremented.

double Increment(this double value)

Increments a floating point number to the next bigger number representable by the data type.
The incrementation step length depends on the provided value. Increment(double.MaxValue) will return positive infinity.
Parameters
return double The next larger floating point value.
double value The value which needs to be incremented.

bool IsLarger(this double a, double b, long maxNumbersBetween)

Compares two doubles and determines if the first value is larger than the secondvalue to within the tolerance or not. Equality comparison is based on the binary representation.
Parameters
return bool true if the first value is larger than the second value; otherwise false.
double a The first value.
double b The second value.
long maxNumbersBetween The maximum number of floating point values for which the two values are considered equal. Must be 1 or larger.

bool IsLargerWithDecimalPlaces(this double a, double b, int decimalPlaces)

Compares two doubles and determines if the first value is larger than the secondvalue to within the specified number of decimal places or not.

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 decimalPlaces == 2, then 0.01 will equal between 0.005 and 0.015, but not 0.02 and not 0.00

Parameters
return bool true if the first value is larger than the second value; otherwise false.
double a The first value.
double b The second value.
int decimalPlaces The number of decimal places.

bool IsSmaller(this double a, double b, long maxNumbersBetween)

Compares two doubles and determines if the first value is smaller than the secondvalue to within the tolerance or not. Equality comparison is based on the binary representation.
Parameters
return bool true if the first value is smaller than the second value; otherwise false.
double a The first value.
double b The second value.
long maxNumbersBetween The maximum number of floating point values for which the two values are considered equal. Must be 1 or larger.

bool IsSmallerWithDecimalPlaces(this double a, double b, int decimalPlaces)

Compares two doubles and determines if the first value is smaller than the secondvalue to within the specified number of decimal places or not.

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 thdecimalPlacesg. if decimalPlaces == 2, then 0.01 will equal between 0.005 and 0.015, but not 0.02 and not 0.00

Parameters
return bool true if the first value is smaller than the second value; otherwise false.
double a The first value.
double b The second value.
int decimalPlaces The number of decimal places.

int Magnitude(this double value)

Returns the magnitude of the number.
Parameters
return int The magnitude of the number.
double value The value.

double MaximumMatchingFloatingPointNumber(this double value, long maxNumbersBetween)

Returns the floating point number that will match the value with the tolerance on the maximum size (i.e. the result is always bigger than the value)
Parameters
return double The maximum floating point number which is maxNumbersBetween larger than the given value.
double value The value.
long maxNumbersBetween The ulps difference.

double MinimumMatchingFloatingPointNumber(this double value, long maxNumbersBetween)

Returns the floating point number that will match the value with the tolerance on the minimum size (i.e. the result is always smaller than the value)
Parameters
return double The minimum floating point number which is maxNumbersBetween smaller than the given value.
double value The value.
long maxNumbersBetween The ulps difference.

ulong NumbersBetween(this double a, double b)

Evaluates the count of numbers between two double numbers
The second number is included in the number, thus two equal numbers evaluate to zero and two neighbor numbers evaluate to one. Therefore, what is returned is actually the count of numbers between plus 1.
Parameters
return ulong The number of floating point values between a and b.
double a The first parameter.
double b The second parameter.

void RangeOfMatchingFloatingPointNumbers(this double value, long maxNumbersBetween, Double& bottomRangeEnd, Double& topRangeEnd)

Parameters
double value
long maxNumbersBetween
Double& bottomRangeEnd
Double& topRangeEnd

void RangeOfMatchingNumbers(this double value, double relativeDifference, Int64& bottomRangeEnd, Int64& topRangeEnd)

Parameters
double value
double relativeDifference
Int64& bottomRangeEnd
Int64& topRangeEnd

Public properties

double DoubleMachinePrecision get;

Gets the maximum relative precision of a double.
return double
Value:

int NumberOfDecimalPlacesForDoubles get;

Gets the number of decimal places for doubles.
return int
Value:

int NumberOfDecimalPlacesForFloats get;

Gets the number of decimal places for floats.
return int
Value:

double SingleMachinePrecision get;

Gets the maximum relative precision of a single.
return double
Value: