0

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.

mskfisher
  • 3,291
  • 4
  • 35
  • 48
Bick
  • 17,833
  • 52
  • 146
  • 251

2 Answers2

10

-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:

  1. it isn't a three-way comparison like Float.compare;
  2. it doesn't handle NaNs;
  3. 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.

NPE
  • 486,780
  • 108
  • 951
  • 1,012
  • but as As I understan floats cannot be compared with == due to rounding errors . when we do floattobits() (inside the original float.copare - doenst it do sort of string compare with the two results? – Bick Aug 04 '11 at 12:23
  • @user450602: Both exact and approximate comparisons of floating-point numbers have their uses. It all depends on what you're doing. – NPE Aug 04 '11 at 12:25
  • Non taken. anyhow, regarding No.3 and the relative epsilon. I think I didnt explained my question well. I would have thought that Float.compare be a better function if it took (f1,f2,epsilon) as parameters. the rounding errors (which I have learnt about here http://stackoverflow.com/questions/6724031/how-can-a-primitive-float-value-be-0-0-what-does-that-mean would be avoided better ) . dont you think? – Bick Aug 04 '11 at 12:47
  • @user450602: see http://www.cygnus-software.com/papers/comparingfloats/comparingfloats.htm for a discussion of absolute vs relative error – NPE Aug 04 '11 at 12:51
3

Floating point arithmetic is tricky. This article throws some light on the basics.

-0 is signed zero:

In ordinary arithmetic, −0 = +0 = 0. However, in computing, some number representations allow for the existence of two zeros, often denoted by −0 (negative zero) and +0 (positive zero).
[...]
The IEEE 754 standard for floating point arithmetic (presently used by most computers and programming languages that support floating point numbers) requires both +0 and −0. The zeroes can be considered as a variant of the extended real number line such that 1/−0 = −∞ and 1/+0 = +∞, division by zero is only undefined for ±0/±0 and ±∞/±∞.

Nivas
  • 18,126
  • 4
  • 62
  • 76