Bug 36737 - Wrong results from floating-point multiplication by 10
Summary: Wrong results from floating-point multiplication by 10
Status: RESOLVED INVALID
Alias: None
Product: gcc
Classification: Unclassified
Component: c (show other bugs)
Version: 4.1.2
: P3 major
Target Milestone: ---
Assignee: Not yet assigned to anyone
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2008-07-05 09:10 UTC by Andrea R.
Modified: 2008-07-05 19:08 UTC (History)
1 user (show)

See Also:
Host: i686-pc-linux-gnu
Target: i686-pc-linux-gnu
Build: i686-pc-linux-gnu
Known to work:
Known to fail:
Last reconfirmed:


Attachments
The program's source code (125 bytes, text/plain)
2008-07-05 09:12 UTC, Andrea R.
Details
The preprocessed source (6.38 KB, text/plain)
2008-07-05 09:14 UTC, Andrea R.
Details

Note You need to log in before you can comment on or make changes to this bug.
Description Andrea R. 2008-07-05 09:10:37 UTC
A C program, compiled without optimization, which does repeated multiplications by 10 on a float number (0.2008) outputs incorrect results.

I know about the "most often reported non-bug", but since the K&R specifies that FLT_DIG (decimal digits of precision) must be at least 6, I still consider this to be a possible bug.

The program was compiled with command line: 'gcc --save-temps ~/mathbug.c'

-----BEGIN PROGRAM OUTPUT-----
nn = 2.008000
nn = 20.079998
nn = 200.799988
nn = 2007.999878
nn = 20079.998047
nn = 200799.984375
nn = 2007999.875000
------END PROGRAM OUTPUT------

-----BEGIN VERSION INFO-----
~> gcc -v
Using built-in specs.
Target: i386-redhat-linux
Configured with: ../configure --prefix=/usr --mandir=/usr/share/man --infodir=/usr/share/info --enable-shared --enable-threads=posix --enable-checking=release --with-system-zlib --enable-__cxa_atexit --disable-libunwind-exceptions --enable-libgcj-multifile --enable-languages=c,c++,objc,obj-c++,java,fortran,ada --enable-java-awt=gtk --disable-dssi --enable-plugin --with-java-home=/usr/lib/jvm/java-1.4.2-gcj-1.4.2.0/jre --with-cpu=generic --host=i386-redhat-linux
Thread model: posix
gcc version 4.1.2 20070626 (Red Hat 4.1.2-14)
~> uname -a
Linux xyzzy.local 2.6.18-92.1.6.el5 #1 SMP Wed Jun 25 12:46:39 EDT 2008 i686 athlon i386 GNU/Linux
------END VERSION INFO------
Comment 1 Andrea R. 2008-07-05 09:12:11 UTC
Created attachment 15860 [details]
The program's source code
Comment 2 Andrea R. 2008-07-05 09:14:43 UTC
Created attachment 15861 [details]
The preprocessed source
Comment 3 Dominique d'Humieres 2008-07-05 09:48:52 UTC
Why do you think the results are incorrect? Did you hear aboud round-off errors? 0.2008 cannot be represented exactly in float (nor in double) and you cannot expect to have more that six significant digits. If you want to get what you expect, use the right format for the output (not fluent enough in C to give it to you).

Comment 4 Andreas Schwab 2008-07-05 09:54:11 UTC
Not a bug.
Comment 5 Andrea R. 2008-07-05 10:06:34 UTC
(In reply to comment #4)

Well it certainly gives wrong results.
Comment 6 Dominique d'Humieres 2008-07-05 10:16:19 UTC
(In reply to comment #5)

> Well it certainly gives wrong results.

No! Read comment #3 and learn the primers about floating point numbers.

Comment 7 Andrew Pinski 2008-07-05 19:08:11 UTC
You should also read http://www.validlab.com/goldberg/paper.pdf .

>Well it certainly gives wrong results.

In this case, the number is not directly representable in float.  You will be able to get better precision with double but it will still not be directly representable.