From 366b1f7879a7e7d1b802b1809e203d31a7cec63d Mon Sep 17 00:00:00 2001 From: Danny Robson Date: Wed, 11 Apr 2018 18:24:52 +1000 Subject: [PATCH] maths: use our abs implementation for relatively_equal this simplifies calling logic for integral, floating, and coord types --- maths.hpp | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/maths.hpp b/maths.hpp index fb8f7a45..a9f0fb54 100644 --- a/maths.hpp +++ b/maths.hpp @@ -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 + 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; } //-------------------------------------------------------------------------