[Numpy-discussion] Testing for close to zero?

Charles R Harris charlesr.harris at gmail.com
Mon Jan 19 23:09:00 EST 2009


On Mon, Jan 19, 2009 at 7:23 PM, Jonathan Taylor <
jonathan.taylor at utoronto.ca> wrote:

> Interesting.  That makes sense and I suppose that also explains why
> there is no function to do this sort of thing for you.
>

A combination of relative and absolute errors is another common solution,
i.e., test against relerr*max(abs(array_of_inputs)) + abserr. In cases like
this relerr is typically eps and abserr tends to be something like 1e-12,
which keeps you from descending towards zero any further than you need to.

Chuck


>
> Jon.
>
> On Mon, Jan 19, 2009 at 3:55 PM, Robert Kern <robert.kern at gmail.com>
> wrote:
> > On Mon, Jan 19, 2009 at 14:43, Jonathan Taylor
> > <jonathan.taylor at utoronto.ca> wrote:
> >> Hi,
> >>
> >> When solving a quadratic equation I get that alpha =
> >> -3.78336776728e-31 which I believe to be far below machine precision:
> >>
> >> finfo(float).eps
> >> 2.2204460492503131e-16
> >>
> >> But an if statement like:
> >>
> >> if alpha == 0:
> >>   ...
> >>
> >> does not catch this.  Is there a better way to check for things that
> >> are essentially zero or should I really be using
> >>
> >> if np.abs(alpha) < finfo(float).eps:
> >>   ...
> >
> > Almost. You should scale eps by some estimate of the size of the
> > problem. Exactly how you should do this depends on the problem,
> > though. Errors accumulate in different ways depending on the
> > operations you perform on the numbers. Multiplying eps by
> > max(abs(array_of_inputs)) is probably a reasonable starting point.
> >
>

> > --
> > Robert Kern
> >
> > "I have come to believe that the whole world is an enigma, a harmless
> > enigma that is made terrible by our own mad attempt to interpret it as
> > though it had an underlying truth."
> >  -- Umberto Eco
> > _______________________________________________
> > Numpy-discussion mailing list
> > Numpy-discussion at scipy.org
> > http://projects.scipy.org/mailman/listinfo/numpy-discussion
> >
> _______________________________________________
> Numpy-discussion mailing list
> Numpy-discussion at scipy.org
> http://projects.scipy.org/mailman/listinfo/numpy-discussion
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/numpy-discussion/attachments/20090119/b6f85616/attachment.html>


More information about the NumPy-Discussion mailing list