UserPreferences

ErrorAndPrecision


Other pages: Bio/Geo/CS250, MainModelingWiki

1. What is error?

Integers (when used appropriately) are exact, but floating-point numbers aren't. They're more like centerpoints of intervals, and the "true" value of the variable in question is likely to be anywhere in that interval.

When you do calculations using floating-point variables, you should keep in mind that the result of the calculation is seful only if you keep in mind that other values in the interval could as easily have been used, giving you a different result. All the different likely values of the different floating-point variables could go into the calculation and give a range of result values -- an interval. Important questions: how big is the result's error interval, and how can we keep it small?

2. Sources of error

3. Examples

3.1. Sometimes errors don't accumulate

import random
sum = 0.0
for i in range(1000):
  r = 2.0 * (random.random() - 0.5)
  print r,
  sum += r
  if i % 10 == 0:
    print "    ", sum
  else:
    print

3.2. Sometimes they do accumulate

import random
import math
sum = 0.0
for i in range(1000):
  r = 2.0 * (random.random() - 0.5)
  sum += math.sqrt(r**2)
print sum
Note that even if in the long term errors don't accumulate, in the short term they may be quite important. Always keep in mind the temporal scale, even if you're just observing how your simulation behaves.

3.3. Large relative error with subtraction of similar numbers

Say a = 10 (+/- 1), and b = 11 (+/- 1).

3.4. Adding numbers that differ greatly in magnitude

x = 1.0
for i in range(20):
  print "1.0 +", x, "=", 1.0 + x
  x /= 10.0
A consequence
def test(err):
  return (1-(1-err))/((1-1)+err)
for i in range(30):
  x = 10**(-i)
  print "It should be 1, but it's", test(x)

4. Estimating error

5. Tips