fuzzyEqual handle zero better

This commit is contained in:
James Cherry 2021-03-25 15:29:56 -07:00
parent 01d35d71f3
commit 35eeb79a7b
1 changed files with 11 additions and 3 deletions

View File

@ -26,19 +26,27 @@ namespace sta {
using std::max;
using std::abs;
constexpr static float float_equal_tolerance = 1E-15F;
bool
fuzzyEqual(float v1,
float v2)
{
return v1 == v2
|| abs(v1 - v2) < 1E-6F * max(abs(v1), abs(v2));
if (v1 == v2)
return true;
else if (v1 == 0.0)
return abs(v2) < float_equal_tolerance;
else if (v2 == 0.0)
return abs(v1) < float_equal_tolerance;
else
return abs(v1 - v2) < 1E-6F * max(abs(v1), abs(v2));
}
bool
fuzzyZero(float v)
{
return v == 0.0
|| abs(v) < 1E-15F;
|| abs(v) < float_equal_tolerance;
}
bool