http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47146
Summary: Floating point to integer conversions Product: gcc Version: 4.3.4 Status: UNCONFIRMED Severity: normal Priority: P3 Component: c AssignedTo: unassig...@gcc.gnu.org ReportedBy: babel...@yahoo.com #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) ); elapsed = 0.3894949 * 100; int32_t ielapsed = 0.3894949 * 100; fprintf( stdout, "Float=%f\n", elapsed ); fprintf( stdout, "Integer=%d\n", ielapsed ); fprintf( stdout, "------------------------------\n" ); elapsed = 0.39; fprintf( stdout, "Float 0.39=%f\n", elapsed ); fprintf( stdout, "Float 0.39 * 100.0=%f\n", elapsed * 100.0 ); fprintf( stdout, "Integer Cast 0.39 * 100.0=%d\n", (int32_t) (elapsed * 100.0) ); elapsed = 0.39 * 100; ielapsed = 0.39 * 100; fprintf( stdout, "Float=%f\n", elapsed ); fprintf( stdout, "Integer=%d\n", ielapsed ); exit( 0 ); } The above code generates the following: 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=38.999999 Integer Cast 0.39 * 100.0=38 Float=39.000000 Integer=39 Casting and rounding seems to be a problem. The compiler build options are: Using built-in specs. Target: i486-linux-gnu Configured with: ../src/configure -v --with-pkgversion='Debian 4.3.4-6' --with-bugurl=file:///usr/share/doc/gcc-4.3/README.Bugs --enable-languages=c,c++,fortran,objc,obj-c++ --prefix=/usr --enable-shared --enable-multiarch --enable-linker-build-id --with-system-zlib --libexecdir=/usr/lib --without-included-gettext --enable-threads=posix --enable-nls --with-gxx-include-dir=/usr/include/c++/4.3 --program-suffix=-4.3 --enable-clocale=gnu --enable-libstdcxx-debug --enable-objc-gc --enable-mpfr --enable-targets=all --with-tune=generic --enable-checking=release --build=i486-linux-gnu --host=i486-linux-gnu --target=i486-linux-gnu Thread model: posix gcc version 4.3.4 (Debian 4.3.4-6) The system is; Linux devel 2.6.30-2-686 #1 SMP Sat Sep 26 01:16:22 UTC 2009 i686 GNU/Linux The processor is: processor : 0 vendor_id : AuthenticAMD cpu family : 15 model : 107 model name : AMD Athlon(tm) 64 X2 Dual Core Processor 5200+ stepping : 2 cpu MHz : 2712.740 cache size : 512 KB