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

--- Comment #5 from Steve Kargl <sgk at troutmask dot apl.washington.edu> 2011-01-03 18:01:11 UTC ---
On Mon, Jan 03, 2011 at 05:12:10PM +0000, babelart at yahoo dot com wrote:
> http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47146
> 
> 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
> 

Compile the code with -Wall and fix all the warnings.

#include <stdio.h>
int
main (void)
{
   float elapsed;
   elapsed = 0.3894949;  /* Note, the rhs is a double! */
   printf("Float 100.0 * 0.3894949=%d\n", 100.0 * elapsed );
   printf("Float 100 * 0.3894949=%d\n", 100 * elapsed );
   return 0;
}

troutmask:kargl[208] cc -o z -Wall a.c
a.c: In function 'main':
a.c:7: warning: format '%d' expects type 'int', but argument 2 has type
'double'
a.c:8: warning: format '%d' expects type 'int', but argument 2 has type
'double'

Your printf statements are using the first 4 bytes of
the 8 byte double argument.


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