This is the mail archive of the gcc-help@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]
Other format: [Raw text]

Re: Float compare bug?


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/


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