maths: use our abs implementation for relatively_equal

this simplifies calling logic for integral, floating, and coord types
This commit is contained in:
Danny Robson 2018-04-11 18:24:52 +10:00
parent 34a274bc92
commit 366b1f7879

View File

@ -89,15 +89,21 @@ namespace util {
///////////////////////////////////////////////////////////////////////////
// Comparisons
inline bool
relatively_equal (float truth, float test, float percentage)
///
/// check that a query value is within a specified relative percentage of
/// a ground truth value.
///
/// eg: relatively_equal(355/113.f,M_PI,1e-2f);
template <typename ValueT, typename PercentageT>
auto
relatively_equal (ValueT truth, ValueT test, PercentageT percentage)
{
// we want to do 1 - b / a, but a might be zero. if we have FE_INVALID
// enabled then we'll pretty quickly throw an exception.
//
// instead we use |a - b | / (1 + |truth|). note that it's not as
// accurate when the test values aren't close to 1.
return std::abs (truth - test) / (1 + std::abs (truth)) < percentage;
return abs (truth - test) / (1 + abs (truth)) < percentage;
}
//-------------------------------------------------------------------------