QtlMovie 1.12 (A specialized FFmpeg front-end)
QtlNumUtils.h File Reference

Declare some utilities functions for numerics (integers and floats). More...

Functions

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

Detailed Description

Declare some utilities functions for numerics (integers and floats).

Qtl, Qt utility library.

Function Documentation

◆ 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
TA floating point type (eg. float or double).
Parameters
[in]t1First value to compare.
[in]t2Second value to compare.
[in]precisionAllowed 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
TA floating point type (eg. float or double).
Parameters
[in]tValue to check.
[in]precisionAllowed precision in the comparison (defaults to 0.001).
Returns
True if t is zero regarding the given precision false otherwise.
See also
qtlFloatEqual()