From ecb15470ac614b60e5828fbb226e2040a2b2ef2a Mon Sep 17 00:00:00 2001 From: Eryn Wells Date: Sun, 3 Aug 2014 12:22:30 -0700 Subject: [PATCH] Some tweaks to floating point utility methods - Implement NearlyEqual - NearZero and TooFar take const Double& - NearZero and NearlyEqual are templated to take Double& and Double&& (and anything else that can be subtracted and/or passed into std::fabs. --- src/basics.h | 18 +++++++++++++----- 1 file changed, 13 insertions(+), 5 deletions(-) diff --git a/src/basics.h b/src/basics.h index 1b5a535..1ef6f2a 100644 --- a/src/basics.h +++ b/src/basics.h @@ -39,27 +39,35 @@ const Double MAX_DISTANCE = 1.0e7; * @param [in] value The value to check * @returns `true` if the value is close enough to zero */ +template inline bool -NearZero(Double& value) +NearZero(const T value) { return std::fabs(value) < EPSILON; } /** - * Same as NearZero, but for temporary values. + * Determine if two values are "close enough" to be considered equal. Subtracts + * one from the other and checks if the result is near zero. * * @see NearZero + * + * @param [in] left The left parameter + * @param [in] right The right parameter + * @returns `true` if the values are close enough to be equal */ +template inline bool -NearZero(Double&& value) +NearlyEqual(const T left, + const T right) { - return std::fabs(value) < EPSILON; + return NearZero(left - right); } inline bool -TooFar(Double& value) +TooFar(const Double& value) { return value > MAX_DISTANCE; }