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: "babelart at yahoo dot com" <gcc-bugzilla at gcc dot gnu dot org>
- To: gcc-bugs at gcc dot gnu dot org
- Date: Mon, 3 Jan 2011 17:12:16 +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
--- Comment #3 from Pierre Innocent <babelart at yahoo dot com> 2011-01-03 17:12:05 UTC ---
Dear kargl:
Sorry, I was not specific enough. It is the integer conversion that seem
to be wrong,for example the following two lines:
fprintf( stdout, "Float 100.0 * 0.3894949=%d\n", 100.0 * elapsed );
fprintf( stdout, "Float 100 * 0.3894949=%d\n", 100 * elapsed );
both produce the value '-536870912'.
I also downloaded the C99 integer to float conversion test code; and they
generated two many failures. I also believe the compiler should round resulting
integer values when stripping decimals off.
Regards,
Pierre Innocent
--- On Sat, 1/1/11, kargl at gcc dot gnu.org <gcc-bugzilla@gcc.gnu.org> wrote:
> From: kargl at gcc dot gnu.org <gcc-bugzilla@gcc.gnu.org>
> Subject: [Bug c/47146] Floating point to integer conversions
> To: babelart@yahoo.com
> Received: Saturday, January 1, 2011, 4:15 PM
> 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.
>
> --
> Configure bugmail: http://gcc.gnu.org/bugzilla/userprefs.cgi?tab=email
> ------- You are receiving this mail because: -------
> You reported the bug.
>