[Python-ideas] PEP 485: A Function for testing approximate equality

Ethan Furman ethan at stoneleaf.us
Fri Jan 23 23:14:29 CET 2015


On 01/23/2015 01:30 PM, Nathaniel Smith wrote:
> On Fri, Jan 23, 2015 at 12:40 AM, Chris Barker wrote:

> So for reference, it looks like the differences from numpy are:
> 
> 1) kwarg names: "tol" and "abs_tol" versus "atol", "rtol". Numpy's
> names seem fine to me, but if you want the longer ones then probably
> "rel_tol", "abs_tol" would be better?

Longer names are good for us non-maths folks. ;)  rel_tol and abs_tol look good to me.


> 2) use of max() instead of + to combine the relative and absolute
> tolerance. I understand that you find the + conceptually offensive,
> but I'm not really sure why -- max() is maybe a bit better, but it
> seems like much of a muchness to me in practice. (Sure, like you say
> further down, the total error using + might end up being higher by a
> factor of two or so -- but either people are specifying the tolerances
> they want, in which case they can say what they mean either way, or
> else they're just accepting the defaults, in which case they don't
> care.) It might be worth switching to + just for compatibility.

That makes no sense to me.  I'm not sure taking the max does either, though, as phrases like "you can be off by 5% or 30
units, whichever is [smaller | greater]" comes to mind.


> One option would be to add a zero_tol argument, which is an absolute
> tolerance that is only applied if expected == 0.

Seems reasonable.

> I'd strongly consider expanding the scope of this PEP a bit so that
> it's proposing both a relative/absolute-error-based function *and* a
> ULP-difference function. There was a plausible-looking one using
> struct posted in the other thread, it would cover a wider variety of
> cases, and having both functions next to each other in the docs would
> provide a good opportunity to explain why the differences and which
> might be preferred in which situation.

Also seems reasonable.

So, in the interest of keeping things insane ;) how about this signature?

  def close_to(noi, target, min_tol, max_tol, rel_tol, zero_tol):
      """
      returns True if noi is within tolerance of target

      noi: Number Of Interest - result of calulations
      target: the number we are trying to get
      min_tol: used with rel_tol to determine actual tolerance
      max_tol: used with rel_tol to determine actual tolerance
      zero_tol: an absolute tolerance if target == 0 (otherwise rel_tol is used an as zero_tol)
      """

--
~Ethan~

-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 836 bytes
Desc: OpenPGP digital signature
URL: <http://mail.python.org/pipermail/python-ideas/attachments/20150123/9b401dc8/attachment-0001.sig>


More information about the Python-ideas mailing list