Fix almost_equal for NaN and Inf
This commit is contained in:
parent
68adb32472
commit
32e3e61fa7
10
float.cpp
10
float.cpp
@ -80,10 +80,14 @@ template <unsigned int E, unsigned int S>
|
|||||||
bool
|
bool
|
||||||
ieee_float<E, S>::almost_equal (floating_t a,
|
ieee_float<E, S>::almost_equal (floating_t a,
|
||||||
floating_t b) {
|
floating_t b) {
|
||||||
static const double epsilon = 0.001;
|
static const floating_t epsilon = 0.001;
|
||||||
|
const floating_t diff = fabs (a - b);
|
||||||
|
|
||||||
return fabs(a - b) <= epsilon * std::fabs (a) ||
|
// * Use an exact equality first so that infinities are not indirectly compared. This would generate NaNs in the diff.
|
||||||
fabs(a - b) <= epsilon * std::fabs (b);
|
// * Do not use gte or lte. This stops an infinite from making infinities on both sides of the inequality.
|
||||||
|
return exactly_equal (a, b) ||
|
||||||
|
diff < epsilon * std::fabs (a) ||
|
||||||
|
diff < epsilon * std::fabs (b);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user