Re: Float compare bug?

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



John Gonzaga <jgonzaga@xxxxxxxxxxx> 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 of Archives]     [Linux C Programming]     [Linux Kernel]     [eCos]     [Fedora Development]     [Fedora Announce]     [Autoconf]     [The DWARVES Debugging Tools]     [Yosemite Campsites]     [Yosemite News]     [Linux GCC]

  Powered by Linux