mirror of
https://github.com/qgis/QGIS.git
synced 2025-10-24 00:04:44 -04:00
qgis.h: proposes a generic templated method from qgsDoubleNear and qgsNumberNear
This commit is contained in:
parent
7a1937536a
commit
b5c5029e93
@ -2555,6 +2555,7 @@ Compare two doubles, treating nan values as equal
|
|||||||
.. versionadded:: 3.20
|
.. versionadded:: 3.20
|
||||||
%End
|
%End
|
||||||
|
|
||||||
|
|
||||||
bool qgsDoubleNear( double a, double b, double epsilon = 4 * DBL_EPSILON );
|
bool qgsDoubleNear( double a, double b, double epsilon = 4 * DBL_EPSILON );
|
||||||
%Docstring
|
%Docstring
|
||||||
Compare two doubles (but allow some difference)
|
Compare two doubles (but allow some difference)
|
||||||
|
|||||||
@ -2555,6 +2555,7 @@ Compare two doubles, treating nan values as equal
|
|||||||
.. versionadded:: 3.20
|
.. versionadded:: 3.20
|
||||||
%End
|
%End
|
||||||
|
|
||||||
|
|
||||||
bool qgsDoubleNear( double a, double b, double epsilon = 4 * DBL_EPSILON );
|
bool qgsDoubleNear( double a, double b, double epsilon = 4 * DBL_EPSILON );
|
||||||
%Docstring
|
%Docstring
|
||||||
Compare two doubles (but allow some difference)
|
Compare two doubles (but allow some difference)
|
||||||
|
|||||||
@ -4419,6 +4419,28 @@ inline bool qgsNanCompatibleEquals( double a, double b )
|
|||||||
return a == b;
|
return a == b;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#ifndef SIP_RUN
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Compare two numbers of type T (but allow some difference)
|
||||||
|
* \param a first number
|
||||||
|
* \param b second number
|
||||||
|
* \param epsilon maximum difference allowable between numbers
|
||||||
|
* \since 3.36
|
||||||
|
*/
|
||||||
|
template<typename T>
|
||||||
|
inline bool qgsNumberNear( T a, T b, T epsilon = std::numeric_limits<T>::epsilon() * 4 )
|
||||||
|
{
|
||||||
|
const bool aIsNan = std::isnan( a );
|
||||||
|
const bool bIsNan = std::isnan( b );
|
||||||
|
if ( aIsNan || bIsNan )
|
||||||
|
return aIsNan && bIsNan;
|
||||||
|
|
||||||
|
const T diff = a - b;
|
||||||
|
return diff >= -epsilon && diff <= epsilon;
|
||||||
|
}
|
||||||
|
#endif
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Compare two doubles (but allow some difference)
|
* Compare two doubles (but allow some difference)
|
||||||
* \param a first double
|
* \param a first double
|
||||||
@ -4427,13 +4449,7 @@ inline bool qgsNanCompatibleEquals( double a, double b )
|
|||||||
*/
|
*/
|
||||||
inline bool qgsDoubleNear( double a, double b, double epsilon = 4 * std::numeric_limits<double>::epsilon() )
|
inline bool qgsDoubleNear( double a, double b, double epsilon = 4 * std::numeric_limits<double>::epsilon() )
|
||||||
{
|
{
|
||||||
const bool aIsNan = std::isnan( a );
|
return qgsNumberNear<double>( a, b, epsilon );
|
||||||
const bool bIsNan = std::isnan( b );
|
|
||||||
if ( aIsNan || bIsNan )
|
|
||||||
return aIsNan && bIsNan;
|
|
||||||
|
|
||||||
const double diff = a - b;
|
|
||||||
return diff >= -epsilon && diff <= epsilon;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -4444,13 +4460,7 @@ inline bool qgsDoubleNear( double a, double b, double epsilon = 4 * std::numeric
|
|||||||
*/
|
*/
|
||||||
inline bool qgsFloatNear( float a, float b, float epsilon = 4 * FLT_EPSILON )
|
inline bool qgsFloatNear( float a, float b, float epsilon = 4 * FLT_EPSILON )
|
||||||
{
|
{
|
||||||
const bool aIsNan = std::isnan( a );
|
return qgsNumberNear<float>( a, b, epsilon );
|
||||||
const bool bIsNan = std::isnan( b );
|
|
||||||
if ( aIsNan || bIsNan )
|
|
||||||
return aIsNan && bIsNan;
|
|
||||||
|
|
||||||
const float diff = a - b;
|
|
||||||
return diff >= -epsilon && diff <= epsilon;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
//! Compare two doubles using specified number of significant digits
|
//! Compare two doubles using specified number of significant digits
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user