Upon checking Float.compare(f1,f2) I found that it compares f1f2
and returns -1,0,1.
Then it returns -1,0,1 if the values are -0.0, 0.0 or NAN.
What does that mean -0.0?
I would have expected something like
return (Math.abs(f1 - f2) - 0.001f) > 0)
where 0.001 is a given epsilon value.
Thanks.
-0.0is 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.compareis 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:
Float.compare;NaNs;f1andf2(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.