-0.0
is the negative zero, as specified by the IEEE 754 standard.
If you're curious about how such a value might arise, the following article does a good job of explaining it: http://www.savrola.com/resources/negative_zero.html
As to not taking an epsilon value, this is how Float.compare
is designed work (it's an exact comparison, not an approximate one). There's nothing to stop you from having another comparison function that does take an epsilon and does perform an approximate comparison.
Both exact and approximate comparisons of floating-point numbers have their uses.
As to your actual code, it suffers from a number of issues:
- it isn't a three-way comparison like
Float.compare
;
- it doesn't handle
NaN
s;
- it is generally better to specify the epsilon as a relative value, not as an absolute one, so that it scales with
f1
and f2
(see this article for a discussion).
My point here isn't to criticise your code but to show that writing good floating-point code is harder than it first looks.