gcc 4.6.1 double to long cast on 32bit systems

Ian Lance Taylor iant@google.com
Wed Aug 1 21:21:00 GMT 2012


On Wed, Aug 1, 2012 at 1:52 PM, Karsten Ahnert
<karsten.ahnert@ambrosys.de> wrote:
>
> I am new to this list. If this is not the correct place for posting the
> question I apologize for any inconvenience.

This question should have gone to gcc-help@gcc.gnu.org.  Please take
any followups to gcc-help.  Thanks.

> The following code produces strange results on a 32 bit Linux system
> with gcc 4.6.1 (compilation with -m32):
>
> double val = 52.30582;
> double d = 3600.0 * 1000.0 * val;
> long l = long( d );
> long l2 = long( ( 3600.0 * 1000.0 * val ) );
> long l3 = (long)( 3600.0 * 1000.0 * val );
> long l4 = long( 3600.0 * 1000.0 * val );

x86 32-bit floating point is weird because by default the compiler
will use the 80-bit x87 floating point registers.  That will cause
unexpected results.  Ways to avoid that are -mfpmath=sse -msse2, or
-fexcess-precision=std, or -ffloat-store.

Ian



More information about the Gcc mailing list