Is comparing against 0.0 safe?

Geoff Keating geoffk@cygnus.com
Tue Feb 15 16:06:00 GMT 2000


"Martin v. Loewis" <martin@loewis.home.cs.tu-berlin.de> writes:

> > 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.
> 
> Depends on what you mean by 'safe'. If you are asking whether it is
> deterministic, then yes. 
> 
> If you are asking whether all program involving comparison with 0
> give the same output on all platforms, then the answer is no:
> 
>   double a = 10.0/3;
>   double b = 4 - 2/3.0;
>   if (a-b == 0)
>     might_succeed_some_systems_and_fail_on_others

On IEEE754 systems, the comparison will always succeed (in
round-to-nearest mode); if you're sufficiently careful (and you have
to be very very careful), you can do this sort of thing safely.

> If you are asking whether there is a program involving comparison with
> 0 is portable, then again, I believe this answer is yes:
> 
>   double a = 0;
>   if (a = 0)
>     should_work_always

Well, yes.  (It also does the right thing if you use `==' :-) ).

> I believe the warning tells says you shouldn't trust floating point
> comparisons, as the other operand may get very close, but fail the
> first operand by a fraction.

Yes.  The warning is for people who are treating floating-point as
approximations to infinitely-precise values; if you are doing this,
then all floating-point comparisons are dangerous unless you allow for
error, and if you are using == or !=, you've usually forgotten to
allow for error.


In fact, does anyone mind if I add this extra documentation?

Index: invoke.texi
===================================================================
RCS file: /cvs/gcc/egcs/gcc/invoke.texi,v
retrieving revision 1.168
diff -p -u -c -r1.168 invoke.texi
cvs server: conflicting specifications of output style
*** invoke.texi	2000/02/11 09:53:36	1.168
--- invoke.texi	2000/02/16 00:04:43
*************** struct s x = @{ 3, 4 @};
*** 1707,1712 ****
--- 1707,1723 ----
  @item -Wfloat-equal
  Warn if floating point values are used in equality comparisons.
  
+ The idea behind this is that sometimes it is convenient (for the
+ programmer) to consider floating-point values as approximations to
+ infinitely precise real numbers.  If you are doing this, then you need
+ to compute (by analysing the code, or in some other way) the maximum
+ error that the computation introduces, and allow for it when performing
+ comparisons (and when producing output, but that's a different problem).
+ In particular, instead of testing for equality, you would check to see
+ whether the two values have ranges that overlap; and this is done with
+ the relational operators, so equality comparisons are probably
+ mistaken.
+ 
  @item -Wtraditional (C only)
  Warn about certain constructs that behave differently in traditional and
  ANSI C.

-- 
- Geoffrey Keating <geoffk@cygnus.com>


More information about the Gcc-patches mailing list