Namespaces

Types in MathNet.Numerics

Type Precision

Namespace MathNet.Numerics

Utilities for working with floating point numbers.

Useful links:

Static Functions

Properties

Public static methods

bool AlmostEqual(this Complex32 a, Complex32 b)

Checks whether two Compex numbers are almost equal.
Parameters
Complex32 a

The first number

Complex32 b

The second number

Return
bool

true if the two values differ by no more than 10 * 2^(-52); false otherwise.

bool AlmostEqual(this Complex a, Complex b)

Checks whether two Compex numbers are almost equal.
Parameters
Complex a

The first number

Complex b

The second number

Return
bool

true if the two values differ by no more than 10 * 2^(-52); false otherwise.

bool AlmostEqual(this float a, float b)

Checks whether two real numbers are almost equal.
Parameters
float a

The first number

float b

The second number

Return
bool

true if the two values differ by no more than 10 * 2^(-52); false otherwise.

bool AlmostEqual(this double a, double b)

Checks whether two real numbers are almost equal.
Parameters
double a

The first number

double b

The second number

Return
bool

true if the two values differ by no more than 10 * 2^(-52); false otherwise.

bool AlmostEqual(this float a, float b, int maxNumbersBetween)

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

The first value.

float b

The second value.

int maxNumbersBetween

The maximum number of floating point values between the two values. Must be 1 or larger.

Return
bool

if both floats are equal to each other within the specified tolerance; otherwise .

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
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.

Return
bool

if both doubles are equal to each other within the specified tolerance; otherwise .

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

Checks whether two structures with precision support are almost equal.
Parameters
T a

The first structure

T b

The second structure

Return
bool

true if the two values differ by no more than 10 * 2^(-52); false otherwise.

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

Compares two floats 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
float a

The first value.

float b

The second value.

int decimalPlaces

The number of decimal places.

Return
bool

if both doubles are equal to each other within the specified number of decimal places; otherwise .

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
double a

The first value.

double b

The second value.

int decimalPlaces

The number of decimal places.

Return
bool

if both doubles are equal to each other within the specified number of decimal places; otherwise .

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
IList<double> a

The first value list.

IList<double> b

The second value list.

double maximumError

The accuracy required for being almost equal.

Return
bool

if both doubles are almost equal up to the specified maximum error, otherwise.

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
IList<T> a

The first structure list.

IList<T> b

The second structure list.

double maximumError

The accuracy required for being almost equal.

Return
bool

if both doubles are almost equal up to the specified maximum error, otherwise.

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
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.

Return
bool

if both doubles are almost equal up to the specified maximum absolute error, otherwise.

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
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.

Return
bool

if both doubles are almost equal up to the specified maximum error, otherwise.

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

Compares two complex and determines if they are equal within the specified maximum error.
Parameters
float a

The first value.

float b

The second value.

double maximumError

The accuracy required for being almost equal.

Return
bool

if both complex are almost equal up to the specified maximum error, otherwise.

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

Compares two complex and determines if they are equal within the specified maximum error.
Parameters
Complex a

The first value.

Complex b

The second value.

double maximumError

The accuracy required for being almost equal.

Return
bool

if both complex are almost equal up to the specified maximum error, otherwise.

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

Compares two doubles and determines if they are equal within the specified maximum error.
Parameters
double a

The first value.

double b

The second value.

double maximumError

The accuracy required for being almost equal.

Return
bool

if both doubles are almost equal up to the specified maximum error, otherwise.

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
T a

The first structure.

T b

The second structure.

double maximumError

The accuracy required for being almost equal.

Return
bool

if both doubles are almost equal up to the specified maximum relative error, otherwise.

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
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.

Return
bool

if both doubles are almost equal up to the specified maximum relative error, otherwise.

double CoerceZero(this double a)

Forces small numbers near zero to zero.
Parameters
double a

The real number to coerce to zero, if it is almost zero.

Return
double

Zero if | a | is smaller than 2^(-53) = 1.11e-16, a otherwise.

double CoerceZero(this double a, double maximumAbsoluteError)

Forces small numbers near zero to zero, according to the specified absolute accuracy.
Parameters
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.

Return
double

Zero if | a | is smaller than maximumAbsoluteError , a otherwise.

double CoerceZero(this double a, long maxNumbersBetween)

Forces small numbers near zero to zero, according to the specified absolute accuracy.
Parameters
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.

Return
double

Zero if | a | is fewer than maxNumbersBetween numbers from zero, a otherwise.

double CoerceZero(this double a, int maxNumbersBetween)

Forces small numbers near zero to zero, according to the specified absolute accuracy.
Parameters
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.

Return
double

Zero if | a | is fewer than maxNumbersBetween numbers from zero, a otherwise.

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

Compares two doubles and determines which double is bigger.
Parameters
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.

Return
int

-1 a is smaller than b by more than the maxNumbersBetween tolerance.
0 a is equal to b within the maxNumbersBetween tolerance.
1 a is bigger than b by more than the maxNumbersBetween tolerance.

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

Compares two doubles and determines which double is bigger.
Parameters
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.

Return
int

-1 a is smaller than b by more than a magnitude equal to decimalPlaces.
0 a is equal to b within a magnitude equal to decimalPlaces.
1 a is bigger than b by more than a magnitude equal to decimalPlaces.

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
double value

The value which should be decremented.

int count

How many times the number should be decremented.

Return
double

The next smaller floating point value.

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
double value

The value which should be decremented.

Return
double

The next smaller floating point value.

double EpsilonOf(this double value)

Evaluates the minimum distance to the next distinguishable number near the argument value.
Evaluates the epsilon. The more common positive epsilon is equal to two times this negative epsilon.
Parameters
double value

The value used to determine the minimum distance.

Return
double

Relative Epsilon (positive double or NaN).

double GetMagnitudeScaledValue(this double value)

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

The value.

Return
double

The value of the number.

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
double value

The value which needs to be incremented.

Return
double

The next larger floating point 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
double value

The value which needs to be incremented.

int count

How many times the number should be incremented.

Return
double

The next larger floating point value.

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

Compares two doubles and determines if the first value is larger than the second value to within the tolerance or not. Equality comparison is based on the binary representation.
Parameters
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.

Return
bool

true if the first value is larger than the second value; otherwise false.

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

Compares two doubles and determines if the first value is larger than the second value 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
double a

The first value.

double b

The second value.

int decimalPlaces

The number of decimal places.

Return
bool

true if the first value is larger than the second value; otherwise false.

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

Compares two doubles and determines if the first value is smaller than the second value to within the tolerance or not. Equality comparison is based on the binary representation.
Parameters
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.

Return
bool

true if the first value is smaller than the second value; otherwise false.

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

Compares two floats and determines if the first value is smaller than the second value to within the tolerance or not. Equality comparison is based on the binary representation.
Parameters
float a

The first value.

float 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.

Return
bool

true if the first value is smaller than the second value; otherwise false.

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

Compares two floats and determines if the first value is smaller than the second value 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 th decimalPlaces g. if decimalPlaces == 2, then 0.01 will equal between 0.005 and 0.015, but not 0.02 and not 0.00

Parameters
float a

The first value.

float b

The second value.

int decimalPlaces

The number of decimal places.

Return
bool

true if the first value is smaller than the second value; otherwise false.

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

Compares two doubles and determines if the first value is smaller than the second value 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 th decimalPlaces g. if decimalPlaces == 2, then 0.01 will equal between 0.005 and 0.015, but not 0.02 and not 0.00

Parameters
double a

The first value.

double b

The second value.

int decimalPlaces

The number of decimal places.

Return
bool

true if the first value is smaller than the second value; otherwise false.

int Magnitude(this float value)

Returns the magnitude of the number.
Parameters
float value

The value.

Return
int

The magnitude of the number.

int Magnitude(this double value)

Returns the magnitude of the number.
Parameters
double value

The value.

Return
int

The magnitude of the number.

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
double value

The value.

long maxNumbersBetween

The ulps difference.

Return
double

The maximum floating point number which is maxNumbersBetween larger than the given value.

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
double value

The value.

long maxNumbersBetween

The ulps difference.

Return
double

The minimum floating point number which is maxNumbersBetween smaller than the given value.

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
double a

The first parameter.

double b

The second parameter.

Return
ulong

The number of floating point values between a and b.

double PositiveEpsilonOf(this double value)

Evaluates the minimum distance to the next distinguishable number near the argument value.
Evaluates the epsilon. See also EpsilonOf
Parameters
double value

The value used to determine the minimum distance.

Return
double

Relative Epsilon (positive double or NaN)

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

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

Public properties

double DoubleMachinePrecision get;

Gets the maximum relative precision of a double.
Value:

int NumberOfDecimalPlacesForDoubles get;

Gets the number of decimal places for doubles.
Value:

int NumberOfDecimalPlacesForFloats get;

Gets the number of decimal places for floats.
Value:

double SingleMachinePrecision get;

Gets the maximum relative precision of a single.
Value: