Is comparing against 0.0 safe?

Martin v. Loewis martin@loewis.home.cs.tu-berlin.de
Tue Feb 15 14:33:00 GMT 2000


> 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

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

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.

Regards,
Martin


More information about the Gcc mailing list