From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (qmail 27157 invoked by alias); 1 Jan 2011 15:41:36 -0000 Received: (qmail 27145 invoked by uid 22791); 1 Jan 2011 15:41:34 -0000 X-SWARE-Spam-Status: No, hits=-2.7 required=5.0 tests=ALL_TRUSTED,AWL,BAYES_00,TW_BJ,TW_CX,TW_DC,TW_GX X-Spam-Check-By: sourceware.org Received: from localhost (HELO gcc.gnu.org) (127.0.0.1) by sourceware.org (qpsmtpd/0.43rc1) with ESMTP; Sat, 01 Jan 2011 15:41:30 +0000 From: "babelart at yahoo dot com" To: gcc-bugs@gcc.gnu.org Subject: [Bug c/47146] New: Floating point to integer conversions X-Bugzilla-Reason: CC X-Bugzilla-Type: new X-Bugzilla-Watch-Reason: None X-Bugzilla-Product: gcc X-Bugzilla-Component: c X-Bugzilla-Keywords: X-Bugzilla-Severity: normal X-Bugzilla-Who: babelart at yahoo dot com X-Bugzilla-Status: UNCONFIRMED X-Bugzilla-Priority: P3 X-Bugzilla-Assigned-To: unassigned at gcc dot gnu.org X-Bugzilla-Target-Milestone: --- X-Bugzilla-Changed-Fields: Message-ID: X-Bugzilla-URL: http://gcc.gnu.org/bugzilla/ Auto-Submitted: auto-generated Content-Type: text/plain; charset="UTF-8" MIME-Version: 1.0 Date: Sat, 01 Jan 2011 15:41:00 -0000 Mailing-List: contact gcc-bugs-help@gcc.gnu.org; run by ezmlm Precedence: bulk List-Id: List-Archive: List-Post: List-Help: Sender: gcc-bugs-owner@gcc.gnu.org X-SW-Source: 2011-01/txt/msg00025.txt.bz2 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: unassigned@gcc.gnu.org ReportedBy: babelart@yahoo.com #include #include //----------------------------------------------------------------------------- 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