Declare some utilities functions for numerics (integers and floats).
More...
|
template<typename T > |
bool | qtlFloatEqual (const T &t1, const T &t2, const T &precision=0.001) |
| Compare two floating point values for equality. More...
|
|
template<typename T > |
bool | qtlFloatZero (const T &t, const T &precision=0.001) |
| Check if a floating point value is zero. More...
|
|
Declare some utilities functions for numerics (integers and floats).
Qtl, Qt utility library.
◆ qtlFloatEqual()
template<typename T >
bool qtlFloatEqual |
( |
const T & |
t1, |
|
|
const T & |
t2, |
|
|
const T & |
precision = 0.001 |
|
) |
| |
|
inline |
Compare two floating point values for equality.
After several rounds of computations, floating point values lose their precision. It is silly to use the strict comparison operator == on floating point values. If the two values differ, for instance, only by the least significant bit of the mantissa, the operator == return false because the two values are more strictly identical. However, it is likely that they are semantically identical and the differing bit is only the result of a loss of precision in the previous computations. In practice, semantically identical values may even differ by more than one bit.
To solve this issue, we consider floating point values as identical when they differ only within a given precision.
- Template Parameters
-
T | A floating point type (eg. float or double). |
- Parameters
-
[in] | t1 | First value to compare. |
[in] | t2 | Second value to compare. |
[in] | precision | Allowed precision in the comparison (defaults to 0.001). |
- Returns
- True if t1 and t2 are identical regarding the given precision and false otherwise.
◆ qtlFloatZero()
template<typename T >
bool qtlFloatZero |
( |
const T & |
t, |
|
|
const T & |
precision = 0.001 |
|
) |
| |
|
inline |
Check if a floating point value is zero.
- Template Parameters
-
T | A floating point type (eg. float or double). |
- Parameters
-
[in] | t | Value to check. |
[in] | precision | Allowed precision in the comparison (defaults to 0.001). |
- Returns
- True if t is zero regarding the given precision false otherwise.
- See also
- qtlFloatEqual()