Thursday, February 25, 2010

floating point arithmetic 101

There is a pretty fundamental pitfall when comparing floating point precision numbers in a language like C, Python (or even with MachinePrecision numbers in Mathematica): because decimal-to-binary conversion inevitably leads to truncation errors, it is possible to get seemingly incorrect results to numerical comparisons such as:

10.05 - 10.00 > 0.05 (in Python, but interestingly not in Mathematica)

2.000006 - 2.000005 > 0.000001 (in Python AND Mathematica)

I recently fell prey to this basic gotcha. The solution is to add an "epsilon" which determines a threshold level, e.g.

def myGreater(a, b, eps=1e-6):
    return a > b + eps

In Mathematica, such problems can be completely avoided by using arbitrary-precision arithmetic, e.g.

2.000006`20 - 2.000005`20 > 0.000001`20

but at the cost of slower performance.

This article has what appears to be a good discussion of the ins and outs of fpa (one for a *very* rainy day...):

No comments:

Post a Comment