From b1c01f2140e0c657531ed23bf6194c7e47f6b4e4 Mon Sep 17 00:00:00 2001 From: vbd Date: Thu, 23 Aug 2007 14:21:58 +0000 Subject: [PATCH] staffan : * added epsilonEqualRelative for better equality checing of doubles --- src/INTERP_KERNEL/VectorUtils.hxx | 17 +++++++++++++++-- 1 file changed, 15 insertions(+), 2 deletions(-) diff --git a/src/INTERP_KERNEL/VectorUtils.hxx b/src/INTERP_KERNEL/VectorUtils.hxx index f8e58d9d8..a85525de0 100644 --- a/src/INTERP_KERNEL/VectorUtils.hxx +++ b/src/INTERP_KERNEL/VectorUtils.hxx @@ -55,9 +55,22 @@ namespace INTERP_UTILS inline bool epsilonEqual(const double x, const double y, const double errTol = 1.0e-12) { - return std::abs(x - y) < errTol; + return std::abs(x - y) <= errTol; + } + + inline bool epsilonEqualRelative(const double x, const double y, const double relTol = 1.0e-6, const double absTol = 1.0e-17) + { + // necessary for comparing values close to zero + // in order to avoid division + if(std::abs(x - y) < absTol) + { + return true; + } + + const double relError = std::abs((x - y) / std::max(x, y)); + + return relError <= relTol; } - }; -- 2.39.2