Is comparing against 0.0 safe?

Brad Lucier lucier@math.purdue.edu
Tue Feb 15 14:59:00 GMT 2000


>         When using -Wfloat-equal, I'm getting one warning from gcc
> which says: "warning: comparing floating point with == or != is
> unsafe".
...
> I was wondering if comparing a float or double against 0.0 is okay
> for all platforms and floating point representations.
> 
> If so, I'd like to change the warning to allow this.

It's no more safe or unsafe than comparing against any other number.
The discussion of this relatively new warning is at

http://gcc.gnu.org/ml/gcc-bugs/1999-06n/msg00414.html
http://gcc.gnu.org/ml/gcc-patches/1999-09n/msg01249.html
http://gcc.gnu.org/ml/gcc-patches/1999-09n/msg01264.html
http://gcc.gnu.org/ml/gcc-patches/1999-09n/msg01265.html

Originally, the proposer suggested that it be turned off when comparing
against a constant.  (He mentioned that it triggered when intentionally
comparing against 0.0 in his own code.)  I don't see what good the warning
does, (it warns against something, but lets other things that are just
as "bad" go through),  so I can't recommend whether to disable it for
constants,  I'd say not, until we get more feedback about how this has
been used.

> Otherwise, perhaps I should change the expression to be (ut + st > 0).

I would suggest going with this approach.

Brad Lucier


More information about the Gcc mailing list