Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>There are basically two major pitfalls people stumble in with floating-point numbers.</p> <ol> <li><p>The problem of scale. Each FP number has an exponent which determines the overall “scale” of the number so you can represent either really small values or really larges ones, though the number of digits you can devote for that is limited. Adding two numbers of different scale will sometimes result in the smaller one being “eaten” since there is no way to fit it into the larger scale.</p> <pre><code>PS&gt; $a = 1; $b = 0.0000000000000000000000001 PS&gt; Write-Host a=$a b=$b a=1 b=1E-25 PS&gt; $a + $b 1 </code></pre> <p>As an analogy for this case you could picture a large swimming pool and a teaspoon of water. Both are of very different sizes, but individually you can easily grasp how much they roughly are. Pouring the teaspoon into the swimming pool, however, will leave you still with roughly a swimming pool full of water.</p> <p>(If the people learning this have trouble with exponential notation, one can also use the values <code>1</code> and <code>100000000000000000000</code> or so.)</p></li> <li><p>Then there is the problem of binary vs. decimal representation. A number like <code>0.1</code> can't be represented exactly with a limited amount of binary digits. Some languages mask this, though:</p> <pre><code>PS&gt; "{0:N50}" -f 0.1 0.10000000000000000000000000000000000000000000000000 </code></pre> <p>But you can “amplify” the representation error by repeatedly adding the numbers together:</p> <pre><code>PS&gt; $sum = 0; for ($i = 0; $i -lt 100; $i++) { $sum += 0.1 }; $sum 9,99999999999998 </code></pre> <p>I can't think of a nice analogy to properly explain this, though. It's basically the same problem why you can represent <sup>1</sup>/<sub>3</sub> only approximately in decimal because to get the exact value you need to repeat the 3 indefinitely at the end of the decimal fraction.</p> <p>Similarly, binary fractions are good for representing halves, quarters, eighths, etc. but things like a tenth will yield an infinitely repeating stream of binary digits.</p></li> <li><p>Then there is another problem, though most people don't stumble into that, unless they're doing huge amounts of numerical stuff. But then, those already know about the problem. Since many floating-point numbers are merely approximations of the exact value this means that for a given approximation <em>f</em> of a real number <em>r</em> there can be infinitely many more real numbers <em>r</em><sub>1</sub>, <em>r</em><sub>2</sub>, ... which map to exactly the same approximation. Those numbers lie in a certain interval. Let's say that <em>r</em><sub>min</sub> is the minimum possible value of <em>r</em> that results in <em>f</em> and <em>r</em><sub>max</sub> the maximum possible value of <em>r</em> for which this holds, then you got an interval [<em>r</em><sub>min</sub>, <em>r</em><sub>max</sub>] where any number in that interval can be your actual number <em>r</em>.</p> <p>Now, if you perform calculations on that number—adding, subtracting, multiplying, etc.—you lose precision. Every number is just an approximation, therefore you're actually performing calculations with <em>intervals</em>. The result is an interval too and the approximation error only ever gets larger, thereby widening the interval. You may get back a single number from that calculation. But that's merely <em>one</em> number from the interval of <em>possible</em> results, taking into account precision of your original operands and the precision loss due to the calculation.</p> <p>That sort of thing is called <a href="http://en.wikipedia.org/wiki/Interval_arithmetic" rel="noreferrer">Interval arithmetic</a> and at least for me it was part of our math course at the university.</p></li> </ol>
    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. 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