This is the mail archive of the
gcc@gcc.gnu.org
mailing list for the GCC project.
Re: Is comparing against 0.0 safe?
- To: ghazi at caip dot rutgers dot edu
- Subject: Re: Is comparing against 0.0 safe?
- From: "Martin v. Loewis" <martin at loewis dot home dot cs dot tu-berlin dot de>
- Date: Tue, 15 Feb 2000 23:25:53 +0100
- CC: egcs at egcs dot cygnus dot com
- References: <200002152113.QAA18757@caip.rutgers.edu>
> 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