Class FloatingPointNumerics
- Namespace
- NUnit.Framework.Constraints
- Assembly
- nunit.framework.dll
Helper routines for working with floating point numbers
public class FloatingPointNumerics
- Inheritance
-
FloatingPointNumerics
- Inherited Members
Remarks
The floating point comparison code is based on this excellent article: http://www.cygnus-software.com/papers/comparingfloats/comparingfloats.htm
"ULP" means Unit in the Last Place and in the context of this library refers to the distance between two adjacent floating point numbers. IEEE floating point numbers can only represent a finite subset of natural numbers, with greater accuracy for smaller numbers and lower accuracy for very large numbers.
If a comparison is allowed "2 ulps" of deviation, that means the _values are allowed to deviate by up to 2 adjacent floating point _values, which might be as low as 0.0000001 for small numbers or as high as 10.0 for large numbers.
Methods
AreAlmostEqualUlps(double, double, long)
Compares two double precision floating point _values for equality
public static bool AreAlmostEqualUlps(double left, double right, long maxUlps)
Parameters
left
doubleFirst double precision floating point value to be compared
right
doubleSecond double precision floating point value t be compared
maxUlps
longMaximum number of representable double precision floating point _values that are allowed to be between the left and the right double precision floating point _values
Returns
- bool
True if both numbers are equal or close to being equal
Remarks
Double precision floating point _values can only represent a limited series of natural numbers. For example, the _values 2.0000000000000000 and 2.0000000000000004 can be stored in a double, but nothing inbetween them.
This comparison will count how many possible double precision floating point _values are between the left and the right number. If the number of possible _values between both numbers is less than or equal to maxUlps, then the numbers are considered as being equal.
Implementation partially follows the code outlined here: http://www.anttirt.net/2007/08/19/proper-floating-point-comparisons/
AreAlmostEqualUlps(float, float, int)
Compares two floating point _values for equality
public static bool AreAlmostEqualUlps(float left, float right, int maxUlps)
Parameters
left
floatFirst floating point value to be compared
right
floatSecond floating point value t be compared
maxUlps
intMaximum number of representable floating point _values that are allowed to be between the left and the right floating point _values
Returns
- bool
True if both numbers are equal or close to being equal
Remarks
Floating point _values can only represent a finite subset of natural numbers. For example, the _values 2.00000000 and 2.00000024 can be stored in a float, but nothing inbetween them.
This comparison will count how many possible floating point _values are between the left and the right number. If the number of possible _values between both numbers is less than or equal to maxUlps, then the numbers are considered as being equal.
Implementation partially follows the code outlined here: http://www.anttirt.net/2007/08/19/proper-floating-point-comparisons/
ReinterpretAsDouble(long)
Reinterprets the memory contents of an integer value as a double precision floating point value
public static double ReinterpretAsDouble(long value)
Parameters
value
longInteger whose memory contents to reinterpret
Returns
- double
The memory contents of the integer interpreted as a double precision floating point value
ReinterpretAsFloat(int)
Reinterprets the memory contents of an integer as a floating point value
public static float ReinterpretAsFloat(int value)
Parameters
value
intInteger value whose memory contents to reinterpret
Returns
- float
The memory contents of the integer value interpreted as a floating point value
ReinterpretAsInt(float)
Reinterprets the memory contents of a floating point value as an integer value
public static int ReinterpretAsInt(float value)
Parameters
value
floatFloating point value whose memory contents to reinterpret
Returns
- int
The memory contents of the floating point value interpreted as an integer
ReinterpretAsLong(double)
Reinterprets the memory contents of a double precision floating point value as an integer value
public static long ReinterpretAsLong(double value)
Parameters
value
doubleDouble precision floating point value whose memory contents to reinterpret
Returns
- long
The memory contents of the double precision floating point value interpreted as an integer