This is the mail archive of the
gcc-help@gcc.gnu.org
mailing list for the GCC project.
Re: Float compare bug?
- From: Brian Gough <bjg at network-theory dot co dot uk>
- To: John Gonzaga <jgonzaga at pnicorp dot com>
- Cc: gcc-help at gcc dot gnu dot org
- Date: 29 Jul 2004 09:42:58 +0100
- Subject: Re: Float compare bug?
- References: <B866317C-E0C9-11D8-829E-000A95BA5780@pnicorp.com>
John Gonzaga <jgonzaga@pnicorp.com> writes:
> It seems that:
>
> float test = 7.2;
> if(test >= 7.2)
>
> the condition will return false.
> Now do I have to cast all my constant floats? Is this a bug? Thanks.
It's counterintuitive but (float)7.2 => 7.199999809265136719e+00 in
the machine representation, whereas (double)7.2 => 7.200000000000000178e+00,
so the behavior is correct, i.e. (float)7.2 < (double)7.2.
Generally float(x) != double(x) since there's a loss of information
going from double to float, so the mapping can't be inverted.
You can cast it, or write "test >= 7.2f", or store the variable as a
double instead of float.
--
Brian Gough
Network Theory Ltd,
Publishing "An Introduction to GCC" --- http://www.network-theory.co.uk/