Float precision and float equality

Mark Dickinson dickinsm at gmail.com
Mon Dec 7 08:23:21 EST 2009


On Dec 6, 7:34 pm, Anton81 <gerenu... at googlemail.com> wrote:
> I do some linear algebra and whenever the prefactor of a vector turns
> out to be zero, I want to remove it.

Hmm.  Comparing against zero is something of a special case.  So you'd
almost certainly be doing an 'if abs(x) < tol: ...' check, but the
question is what value to use for tol, and that (again) depends on
what you're doing.  Perhaps 'tol' could be something like 'eps *
scale', where 'eps' is an indication of the size of relative error
you're prepared to admit (eps = 1e-12 might be reasonable;  to allow
for rounding errors, it should be something comfortably larger than
the machine epsilon sys.float_info.epsilon, which is likely to be
around 2e-16 for a typical machine), and 'scale' is something closely
related to the scale of your problem: in your example, perhaps scale
could be the largest of all the prefactors you have, or some sort of
average of all the prefactors.  There's really no one-size-fits-all
easy answer here.

> I'd like to keep the system comfortable. So basically I should write a
> new class for numbers that has it's own __eq__ operator?

That's probably not a good idea, for the reasons that Carl Banks
already enumerated.

Mark



More information about the Python-list mailing list