This is the mail archive of the gcc@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]

Re: Is comparing against 0.0 safe?


> 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

Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]