2
$\begingroup$

I remember from computer science classes an example where the same floating-point calculation would diverge to infinity. From memory, it was something like this:

import time

def computation(f):

    f /= 7
    f += 1
    f *= 7
    f -= 7

    return f

f = 10
while True:
    f = computation(f)
    print(f)

But now I cannot reproduce it , and I cannot find it with an online search either. How can I find an example that illustrates the issues of cumulative rounding error in floating point calculations?

$\endgroup$
0

2 Answers 2

1
$\begingroup$

This is what you might be looking for.

```python
import time

def computation(f):
    f /= 7
    f += 1
    f *= 7
    f -= 7
    return f

f = 1e16
while True:
    f = computation(f)
    print(f)
    time.sleep(0.1)
```

Why this happens:

In floating-point arithmetic: $f/7$ and $f*7$ precision loss means these may not perfectly cancel each other. Performing $f /= 7$, means that the floating-point representation can store results slightly off from expected. This is more noticeable in very large or small numbers.

This is amplified in iterative computations. It won’t immediately result in a divergence to infinity, but the error can grow significantly over many iterations.

Explanation:

  1. Floating-point division: Dividing by 7 results in a decimal approximation and so might introduce small inaccuracies, especially for very large numbers like $10^{16}$.
  2. Reaccumulation of errors: Each iteration accumulates small inaccuracies. These will eventually cause noticeable deviations.
  3. Precision loss with large numbers: starting with large values like $f = 10^{16}$, the floating-point representation cannot store the exact result of each division/multiplication, and so drifting occurs.

A good resource for exploring floating-point issues in detail is the online:

Also, check the IEEE 754 Standard for Floating-Point Arithmetic

$\endgroup$
3
  • $\begingroup$ Thank you! I ran the code with 40 digits of precision and do not find a divergence nor a discrepancy. $\endgroup$
    – emonigma
    Commented Sep 28 at 12:26
  • 1
    $\begingroup$ You say “drifting occurs”. Does it really? $\endgroup$
    – gnasher729
    Commented Sep 29 at 20:59
  • $\begingroup$ Python uses double-precision floating-point numbers (based on the IEEE 754 standard) so, rather than use the above simplistic example, try (part) def computation(f): large_value = 1e+16 small_value = 1e-10 f += small_value f -= large_value # Subtract a large value f += large_value # Add back the large value return f f = 1. There are plenty of examples of floating-point errors, which is why it is still taught. Being aware of the pitfalls helps look for ways to prevent errors. $\endgroup$
    – DrJay
    Commented Sep 30 at 10:26
1
$\begingroup$

Pick x. Let u be the value of the lowest bit of x. Then pick y such that y <= x <= y + 7u and y is an exact multiple of 7u.

We have y/7 <= x/7 <= y/7 + u, with y/7 and (y + 7u) / 7 calculated exactly. Adding 1 is an exact operation for most x, then multiplying by 7 is exact and so is subtracting 7. The new result is again between y and y+7u and that stays so forever. So the result may loop between a few values but won’t escape.

You need to handle small x, x just below a power of two, and very large x separately but I think you will get the same result. I wouldn’t be surprised if you get convergence quite quickly.

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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