Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>From all the questions from under <code>floating-accuracy</code> tag on this site any discussion should probably start with a reference to this question: <a href="https://stackoverflow.com/q/10334688/1328439">How dangerous is it to compare floating point values?</a></p> <p>And a reference thereof to "<a href="http://docs.oracle.com/cd/E19957-01/806-3568/ncg_goldberg.html" rel="nofollow noreferrer">What Every Computer Scientist Should Know About Floating Point Arithmetic</a>" by David Goldberg. Here is a short summary.</p> <p><strong>1. Exact floating point results are not portable</strong></p> <p>Floating point arithmetic is neither commutative nor associative. IEEE 754 standard that most compilers and platforms follow does not guarantee exact <em>reproducibility</em> of results. Also results will vary on different processors.</p> <p><strong>2. Floating point comparison does not agree with mathematics</strong></p> <p>Consider the following statement</p> <pre><code> int i = 0; double x = 1.0; while (x != 0.0) { x = x/2 ; i++;} </code></pre> <p>In real numbers this computation should never complete however in floating point it will terminate. The value of <code>i</code> depends on the underlying hardware. Using floating point comparison will make it more difficult to analyze the code.</p> <p><strong>3. Why then is floating point comparison implemented in hardware?</strong></p> <p>There are places where exact floating point equality is necessary. One is normalization of floating point numbers.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload