0

Rather than using an epsilon for float comparison, can you reliably compare two floats for equivalency by rounding them to the desired precision?

For example:

round($float, 3) === round($otherFloat, 3)
user1842104
  • 181
  • 5
  • if your ok compering, round(float), and not the original float, then its fine –  Aug 21 '15 at 00:36
  • That is what I am asking. For the record, I didn't ask how should I compare floating point numbers, this is not a duplicate question. – user1842104 Aug 21 '15 at 17:58

1 Answers1

1

No. If your numbers are just barely on opposite sides of the value where the function will round up instead of down (a half-integer if you're round to the nearest integer), then they will round to different numbers no matter how close together they are.

Brick
  • 3,998
  • 8
  • 27
  • 47