This is the mail archive of the
gcc-bugs@gcc.gnu.org
mailing list for the GCC project.
[Bug c/47146] Floating point to integer conversions
- From: "kargl at gcc dot gnu.org" <gcc-bugzilla at gcc dot gnu dot org>
- To: gcc-bugs at gcc dot gnu dot org
- Date: Sat, 1 Jan 2011 21:15:49 +0000
- Subject: [Bug c/47146] Floating point to integer conversions
- Auto-submitted: auto-generated
- References: <bug-47146-4@http.gcc.gnu.org/bugzilla/>
http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47146
kargl at gcc dot gnu.org changed:
What |Removed |Added
----------------------------------------------------------------------------
Status|UNCONFIRMED |RESOLVED
CC| |kargl at gcc dot gnu.org
Resolution| |INVALID
--- Comment #2 from kargl at gcc dot gnu.org 2011-01-01 21:15:30 UTC ---
(In reply to comment #0)
> #include <stdio.h>
> #include <stdlib.h>
>
> //-----------------------------------------------------------------------------
> int32_t main(int argc, char **argv)
> //-----------------------------------------------------------------------------
> {
> float elapsed = 0.3894949;
>
> fprintf( stdout, "Float 0.3894949=%f\n", elapsed );
> fprintf( stdout, "Float 0.3894949 * 100.0=%f\n", elapsed * 100.0 );
> fprintf( stdout, "Integer Cast 0.3894949 * 100.0=%d\n", (int32_t) (elapsed
> * 100.0) );
>
If you change all your floating point constants to include a f
suffix (e.g., 100.0f, 0.3894949f, etc), then you get
laptop:kargl[149] cc -o z a.c -ffloat-store && ./z
Float 0.3894949=0.389495
Float 0.3894949 * 100.0=38.949490
Integer Cast 0.3894949 * 100.0=38
Float=38.949490
Integer=38
------------------------------
Float 0.39=0.390000
Float 0.39 * 100.0=39.000000
Integer Cast 0.39 * 100.0=39
Float=39.000000
Integer=39
which looks like the desired output. As Jonathan mentions, you
have several intermediate results that are double not float.
Add the fact that 0.39 is not exactly representable, which
your output of "Float 0.39 * 100.0=38.999999" clearly shows,
you've miss interpreted your results.