Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Comparing floating point numbers for depends on the context. Since even changing the order of operations can produce different results, it is important to know how "equal" you want the numbers to be.</p> <p><a href="https://randomascii.wordpress.com/2012/02/25/comparing-floating-point-numbers-2012-edition" rel="noreferrer">Comparing floating point numbers</a> by Bruce Dawson is a good place to start when looking at floating point comparison. </p> <p>The following definitions are from <a href="http://books.google.ca/books?ei=KRkLSYLZEJ7ktQOX3-ChBQ&amp;id=T89QAAAAMAAJ&amp;dq=%22essentially+equal%22+%22approximately+equal%22+knuth&amp;q=%22essentially+equal%22+%22approximately+equal%22+%22definitely+less+than%22&amp;pgis=1#search" rel="noreferrer">The art of computer programming by Knuth</a>: </p> <pre><code>bool approximatelyEqual(float a, float b, float epsilon) { return fabs(a - b) &lt;= ( (fabs(a) &lt; fabs(b) ? fabs(b) : fabs(a)) * epsilon); } bool essentiallyEqual(float a, float b, float epsilon) { return fabs(a - b) &lt;= ( (fabs(a) &gt; fabs(b) ? fabs(b) : fabs(a)) * epsilon); } bool definitelyGreaterThan(float a, float b, float epsilon) { return (a - b) &gt; ( (fabs(a) &lt; fabs(b) ? fabs(b) : fabs(a)) * epsilon); } bool definitelyLessThan(float a, float b, float epsilon) { return (b - a) &gt; ( (fabs(a) &lt; fabs(b) ? fabs(b) : fabs(a)) * epsilon); } </code></pre> <p>Of course, choosing epsilon depends on the context, and determines how equal you want the numbers to be. </p> <p>Another method of comparing floating point numbers is to look at the ULP (units in last place) of the numbers. While not dealing specifically with comparisons, the paper <a href="http://citeseerx.ist.psu.edu/viewdoc/summary?doi=10.1.1.22.6768" rel="noreferrer">What every computer scientist should know about floating point numbers</a> is a good resource for understanding how floating point works and what the pitfalls are, including what ULP is. </p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

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