Commit 1bfb954e authored by gsell's avatar gsell

use std::isnan() in almost_eq()

parent d1603d91
...@@ -222,16 +222,17 @@ namespace cmp_ulp { ...@@ -222,16 +222,17 @@ namespace cmp_ulp {
See: See:
https://randomascii.wordpress.com/2012/02/25/comparing-floating-point-numbers-2012-edition/ https://randomascii.wordpress.com/2012/02/25/comparing-floating-point-numbers-2012-edition/
*/ */
inline bool almost_eq(double A, double B, double maxDiff = 1e-20, int maxUlps = 1000) {
inline bool almost_eq (double A, double B, double maxDiff = 1e-20, int maxUlps = 1000) {
// handle NaN's // handle NaN's
// Note: comparing something with a NaN is always false! if (std::isnan (A) || std::isnan (B)) {
if (A != A || B != B) {
return false; return false;
} }
// Check if the numbers are really close -- needed // Check if the numbers are really close -- needed
// when comparing numbers near zero. // when comparing numbers near zero.
if (std::abs(A - B) <= maxDiff) if (std::abs (A - B) <= maxDiff)
return true; return true;
#pragma GCC diagnostic push #pragma GCC diagnostic push
...@@ -242,15 +243,15 @@ namespace cmp_ulp { ...@@ -242,15 +243,15 @@ namespace cmp_ulp {
// Different signs means they do not match. // Different signs means they do not match.
// Note: a negative floating point number is also negative as integer. // Note: a negative floating point number is also negative as integer.
if ( std::signbit(aInt) != std::signbit(bInt)) if (std::signbit (aInt) != std::signbit (bInt))
return false; return false;
// Find the difference in ULPs. // Find the difference in ULPs.
return (std::abs(aInt - bInt) <= maxUlps); return (std::abs (aInt - bInt) <= maxUlps);
} }
inline bool almost_eq_zero(double A, double maxDiff = 1e-15) { inline bool almost_eq_zero(double A, double maxDiff = 1e-15) {
return (std::abs(A) <= maxDiff); return (std::abs (A) <= maxDiff);
} }
FUNC_EQ(x, y); FUNC_EQ(x, y);
FUNC_EQ_ZERO(x); FUNC_EQ_ZERO(x);
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment