Function qt_core::q_float_distance_2_float
source · pub unsafe fn q_float_distance_2_float(a: c_float, b: c_float) -> u32
Expand description
Returns the number of representable floating-point numbers between a and b.
Calls C++ function: quint32 qFloatDistance(float a, float b)
.
Returns the number of representable floating-point numbers between a and b.
This function provides an alternative way of doing approximated comparisons of floating-point numbers similar to qFuzzyCompare(). However, it returns the distance between two numbers, which gives the caller a possibility to choose the accepted error. Errors are relative, so for instance the distance between 1.0E-5 and 1.00001E-5 will give 110, while the distance between 1.0E36 and 1.00001E36 will give 127.
This function is useful if a floating point comparison requires a certain precision. Therefore, if a and b are equal it will return 0. The maximum value it will return for 32-bit floating point numbers is 4,278,190,078. This is the distance between -FLT_MAX
and +FLT_MAX
.
The function does not give meaningful results if any of the arguments are Infinite
or NaN
. You can check for this by calling qIsFinite().
The return value can be considered as the "error", so if you for instance want to compare two 32-bit floating point numbers and all you need is an approximated 24-bit precision, you can use this function like this:
if (qFloatDistance(a, b) < (1 << 7)) { // The last 7 bits are not // significant // precise enough }
This function was introduced in Qt 5.2.
See also qFuzzyCompare().