This is the mail archive of the
mailing list for the GCC project.
Re: gcc 4.6.1 double to long cast on 32bit systems
- From: Ian Lance Taylor <iant at google dot com>
- To: Karsten Ahnert <karsten dot ahnert at ambrosys dot de>
- Cc: gcc at gcc dot gnu dot org
- Date: Wed, 1 Aug 2012 14:21:00 -0700
- Subject: Re: gcc 4.6.1 double to long cast on 32bit systems
- References: <5019972B.email@example.com>
On Wed, Aug 1, 2012 at 1:52 PM, Karsten Ahnert
> 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 firstname.lastname@example.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.