5
$\begingroup$

Why does evalutating $\sum\limits_{k=1}^{100} 0.01$ not result in 1?

What I get is 1.0000000000000007.

I know that the number 0.01 in binary results in an infinite sequence of zeros and ones, which is truncated when stored in a memory as a floating point number. So information is lost and the result of the sum will no longer be exactly 1.

However, with $\sum\limits_{k=1}^{100} \frac{1}{100}$, I do get exactly 1.

Could someone explain why?

$\endgroup$
  • 3
    $\begingroup$ For your own edification, compare the results of Total[Table[0.01, {100}]] and Total[Table[0.01, {100}], Method -> "CompensatedSummation"]. $\endgroup$ – J. M. will be back soon Oct 22 '18 at 6:21
  • $\begingroup$ @J.M.iscomputer-less Interestingly Total seems to perform much better than Sum, even without "CompensatedSummation". It is somehow more intelligent. Consider ListLinePlot[{Table[Sum[0.01, {i, n}] - n/100, {n, 1, 100}], Table[Total[Table[0.01, {n}]] - n/100, {n, 1, 100}]}] $\endgroup$ – kirma Oct 22 '18 at 9:03
11
$\begingroup$

Welcome to the world of Mathematica numerics.

Mathematica offers three arithmetic systems for evaluating numerical expressions

  • Numerics based on exact numbers. Such computations will always give an exact result, but require all the numbers involved to be integers or rational numbers. Your second expression qualifies as an exact computation and, thus, produces the exact result 1.

  • Numerics based on arbitrary precision numbers. This allows you the precision a computation to specified value. To work, such computations require the numbers involved to be integers, rational numbers or arbitrary precision having a precision greater or equal to the specified precision.

  • Numerics based on machine floating point numbers. This is used whenever a real number, which does not qualify as an arbitrary precision number, appears in a computation. Machine numerics give the fastest computation, but since there is no control over precision. For long computations precision will be lost, sometimes drastically (leading to zero precision – i.e., garbage out). Your first expression falls into this category, but retains good precision. However, when you display all the digits in the result the inexactness shows.

In you specific example, since the deviation from the exact result is small, you can recover the exact result with Rationalize

Example

Sum[.01, {100}] // FullForm
1.0000000000000007`

Rationalized example

Sum[.01, {100}] // Rationalize // FullForm

1

$\endgroup$
6
$\begingroup$

Mathematica treats 0.01 as a machine precision floating point number, while it treats 1/100 as an exact rational quantity. Thus the first sum follows all the grody rules of floating point arithmetic that programmers have come to know and hate.

There are ways to make Mathematica use more digits, though.

More information here.

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.