This is the mail archive of the gcc-bugs@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

[Bug c/47146] Floating point to integer conversions


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.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]