IGLib
1.7.2
The IGLib base library EXTENDED - with other lilbraries and applications.
|
Helper routines for working with floating point numbers More...
Classes | |
struct | DoubleLongUnion |
Union of a double precision floating point variable and a long More... | |
struct | FloatIntUnion |
Union of a floating point variable and an integer More... | |
Static Public Member Functions | |
static bool | AreAlmostEqualUlps (float left, float right, int maxUlps) |
Compares two floating point _values for equality More... | |
static bool | AreAlmostEqualUlps (double left, double right, long maxUlps) |
Compares two double precision floating point _values for equality More... | |
static int | ReinterpretAsInt (float value) |
Reinterprets the memory contents of a floating point value as an integer value More... | |
static long | ReinterpretAsLong (double value) |
Reinterprets the memory contents of a double precision floating point value as an integer value More... | |
static float | ReinterpretAsFloat (int value) |
Reinterprets the memory contents of an integer as a floating point value More... | |
static double | ReinterpretAsDouble (long value) |
Reinterprets the memory contents of an integer value as a double precision floating point value More... | |
Private Member Functions | |
FloatingPointNumerics () | |
Helper routines for working with floating point numbers
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.
|
inlineprivate |
|
inlinestatic |
Compares two floating point _values for equality
left | First floating point value to be compared |
right | Second floating point value t be compared |
maxUlps | Maximum number of representable floating point _values that are allowed to be between the left and the right floating point _values |
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/
References NUnit.Framework.Constraints.FloatingPointNumerics.FloatIntUnion.Float, NUnit.Framework.Constraints.FloatingPointNumerics.FloatIntUnion.Int, and NUnit.Framework.Constraints.FloatingPointNumerics.FloatIntUnion.UInt.
Referenced by NUnit.Framework.Constraints.Numerics.AreEqual(), NUnit.Framework.Constraints.FloatingPointNumericsTests.DoubleEqualityWithUlps(), and NUnit.Framework.Constraints.FloatingPointNumericsTests.FloatEqualityWithUlps().
|
inlinestatic |
Compares two double precision floating point _values for equality
left | First double precision floating point value to be compared |
right | Second double precision floating point value t be compared |
maxUlps | Maximum number of representable double precision floating point _values that are allowed to be between the left and the right double precision floating point _values |
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/
References NUnit.Framework.Constraints.FloatingPointNumerics.DoubleLongUnion.Double, NUnit.Framework.Constraints.FloatingPointNumerics.DoubleLongUnion.Long, and NUnit.Framework.Constraints.FloatingPointNumerics.DoubleLongUnion.ULong.
|
inlinestatic |
Reinterprets the memory contents of a floating point value as an integer value
value | Floating point value whose memory contents to reinterpret |
References NUnit.Framework.Constraints.FloatingPointNumerics.FloatIntUnion.Float.
Referenced by NUnit.Framework.Constraints.FloatingPointNumericsTests.MirroredFloatReinterpretation(), and NUnit.Framework.Constraints.FloatingPointNumericsTests.MirroredIntegerReinterpretation().
|
inlinestatic |
Reinterprets the memory contents of a double precision floating point value as an integer value
value | Double precision floating point value whose memory contents to reinterpret |
References NUnit.Framework.Constraints.FloatingPointNumerics.DoubleLongUnion.Double.
Referenced by NUnit.Framework.Constraints.FloatingPointNumericsTests.MirroredDoubleReinterpretation(), and NUnit.Framework.Constraints.FloatingPointNumericsTests.MirroredLongReinterpretation().
|
inlinestatic |
Reinterprets the memory contents of an integer as a floating point value
value | Integer value whose memory contents to reinterpret |
References NUnit.Framework.Constraints.FloatingPointNumerics.FloatIntUnion.Int.
Referenced by NUnit.Framework.Constraints.FloatingPointNumericsTests.MirroredFloatReinterpretation(), and NUnit.Framework.Constraints.FloatingPointNumericsTests.MirroredIntegerReinterpretation().
|
inlinestatic |
Reinterprets the memory contents of an integer value as a double precision floating point value
value | Integer whose memory contents to reinterpret |
References NUnit.Framework.Constraints.FloatingPointNumerics.DoubleLongUnion.Long.
Referenced by NUnit.Framework.Constraints.FloatingPointNumericsTests.MirroredDoubleReinterpretation(), and NUnit.Framework.Constraints.FloatingPointNumericsTests.MirroredLongReinterpretation().