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: 5*1.2 = 5 or 5*1.2 = 6 ?


-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

>>>>> "Jocelyn" == Jocelyn Fournier <joc@presence-pc.com> writes:

    Jocelyn> Hi, Thank you for your answer.  What kind of
    Jocelyn> optimisations are done in the case where the -O switch is
    Jocelyn> used ?

The problem arises because of the different precisions GCC applies to
the 1.2 literal when compiling with and without optimisation.

By default 1.2 is treated as a double, but when optimizing (by
constant folding) it seems to be treated as float.

If you replace 1.2 by 1.2f it should return 6 on your machine whether
optimizing or not because it just uses single precision then.

But generally, you should use rounding functions when dealing with
float to int conversions to be sure it'll yield the result you expect.

So, 

#define _ISOC99_SOURCE 
...
#include <math.h>
...
   x = round (x * 1.2);

would've been better if you expected 6 as result.

HTH
Claudio
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.0.6 (GNU/Linux)
Comment: Processed by Mailcrypt 3.5.6 <http://mailcrypt.sourceforge.net/>

iD8DBQE9NEEfTpSishmp0ioRAj8JAJ9TlyDtPfwwebCSvwY1F6RwV1AXaQCghVJF
9ke37IUyQFMMU8os3uPs5R4=
=QztW
-----END PGP SIGNATURE-----


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