[Python-ideas] Way to check for floating point "closeness"?

Guido van Rossum guido at python.org
Mon Jan 12 21:04:47 CET 2015


So is 1e-100 close to 1e-50 with the default tolerance? What about 1e+100
and (1e+100 plus one ulp)?

On Mon, Jan 12, 2015 at 9:02 AM, Chris Barker <chris.barker at noaa.gov> wrote:

> Now that we're talking about floating point conveniences (math.nan,
> linspace):
>
> What about putting an
>
> almost_equal(x,y,tol=1e14)
>
> (or close(), or...) in the math module.
>
> AFAICT, in the standard library, there is only:
>
> unittest.TestCase.assertAlmostEqual
>
> but it:
>
> A) is buried in the unittest.TestCase class
>
> B) is an assertion, so you can't use it as a general test (easily)
>
> C) uses number of decimal digits or an absolute delta, but does not
> provide a significant figures comparison, which is most likely what's
> wanted (and a bit harder to write yourself)
>
> numpy provides allclose() (and isclose() ), which is pretty much what I'm
> suggesting.
>
> Anyone else think this would be a good idea to add to the stdlib?
>
> -Chris
>
>
> --
>
> Christopher Barker, Ph.D.
> Oceanographer
>
> Emergency Response Division
> NOAA/NOS/OR&R            (206) 526-6959   voice
> 7600 Sand Point Way NE   (206) 526-6329   fax
> Seattle, WA  98115       (206) 526-6317   main reception
>
> Chris.Barker at noaa.gov
>
> _______________________________________________
> Python-ideas mailing list
> Python-ideas at python.org
> https://mail.python.org/mailman/listinfo/python-ideas
> Code of Conduct: http://python.org/psf/codeofconduct/
>



-- 
--Guido van Rossum (python.org/~guido)
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-ideas/attachments/20150112/462f2540/attachment.html>


More information about the Python-ideas mailing list