Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>When comparing floating point numbers for closeness you usually want to measure their relative difference, which is defined as</p> <pre><code>if (abs(x) != 0 || abs(y) != 0) rel_diff (x, y) = abs((x - y) / max(abs(x),abs(y)) else rel_diff(x,y) = max(abs(x),abs(y)) </code></pre> <p>For example, </p> <pre><code>rel_diff(1.12345, 1.12367) = 0.000195787019 rel_diff(112345.0, 112367.0) = 0.000195787019 rel_diff(112345E100, 112367E100) = 0.000195787019 </code></pre> <p>The idea is to measure the number of leading significant digits the numbers have in common; if you take the -log10 of 0.000195787019 you get 3.70821611, which is about the number of leading base 10 digits all the examples have in common.</p> <p>If you need to determine if two floating point numbers are equal you should do something like</p> <pre><code>if (rel_diff(x,y) &lt; error_factor * machine_epsilon()) then print "equal\n"; </code></pre> <p>where machine epsilon is the smallest number that can be held in the mantissa of the floating point hardware being used. Most computer languages have a function call to get this value. error_factor should be based on the number of significant digits you think will be consumed by rounding errors (and others) in the calculations of the numbers x and y. For example, if I knew that x and y were the result of about 1000 summations and did not know any bounds on the numbers being summed, I would set error_factor to about 100.</p> <p>Tried to add these as links but couldn't since this is my first post:</p> <ul> <li>en.wikipedia.org/wiki/Relative_difference</li> <li>en.wikipedia.org/wiki/Machine_epsilon</li> <li>en.wikipedia.org/wiki/Significand (mantissa) </li> <li>en.wikipedia.org/wiki/Rounding_error</li> </ul>
 

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