This is the mail archive of the
gcc-patches@gcc.gnu.org
mailing list for the GCC project.
FAQ entry on rounding
- To: egcs-patches at egcs dot cygnus dot com
- Subject: FAQ entry on rounding
- From: "Martin v. Loewis" <martin at mira dot isdn dot cs dot tu-berlin dot de>
- Date: Thu, 25 Nov 1999 21:09:35 +0100
Before answering the second report on "mis-computations" regarding
float/double today in a row, I decided to add a FAQ section on it,
which you can reference as
http://egcs.cygnus.com/faq.html#rounding
I committed it right-away so I can use it. If there is anything wrong
with the text, let me know or correct it yourself.
Thanks,
Martin
Index: faq.html
===================================================================
RCS file: /cvs/gcc/wwwdocs/htdocs/faq.html,v
retrieving revision 1.150
diff -u -r1.150 faq.html
--- faq.html 1999/11/02 19:30:36 1.150
+++ faq.html 1999/11/25 20:03:17
@@ -66,6 +66,7 @@
<li><a href="#octave">Octave 2.0.13 does not compile</a></li>
<li><a href="#stdin">Why can't I initialize a static variable with <tt>stdin</tt>?</a></li>
<li><a href="#macarg">Why can't I use #if here?</a></li>
+ <li><a href="#rounding">Problems with floating point computations</a></li>
</ol></li>
<li><a href="#misc">Miscellaneous</a>
@@ -846,6 +847,35 @@
</pre>
<p>This is a bit more typing, but I personally think it's better style
in addition to being more portable.
+
+<h2><a name="rounding">Problems with floating point computations</a></h2>
+
+<p>In a number of cases, gcc appears to perform floating point
+computations incorrectly. For example, the program</p>
+<pre>
+#include <iostream.h>
+
+main() {
+
+ double min = 0.0;
+ double max = 0.5;
+ double width = 0.01;
+ cout << (int)(((max - min) / width) - 1) << endl;
+
+}
+</pre>
+<p>might print 50 on some systems and optimization levels, and 51 on
+others.</p>
+
+<p>The is the result of <em>rounding</em>: The computer cannot
+represent all real numbers exactly, so it has to use
+approximations. When computing with approximation, the computer needs
+to round to the nearest representable number.</p>
+
+<p>This is not a bug in the compiler, but an inherent limitation of
+the float and double types. Please study
+<a href="http://www.validgh.com/goldberg/paper.ps">this paper</a>
+for more information.</p>
<hr>
<a name="misc"></a>