Skip to content

Commit

Permalink
fuzzyEqual handle zero better
Browse files Browse the repository at this point in the history
  • Loading branch information
jjcherry56 committed Mar 25, 2021
1 parent 01d35d7 commit 35eeb79
Showing 1 changed file with 11 additions and 3 deletions.
14 changes: 11 additions & 3 deletions util/Fuzzy.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 35eeb79

Please sign in to comment.