This is the mail archive of the
gcc@gcc.gnu.org
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.3000609@ambrosys.de>
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