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

Ron Adam ron3200 at gmail.com
Sat Jan 24 10:11:40 CET 2015



On 01/23/2015 11:02 PM, Andrew Barnert wrote:
>>> I guess the key question is if someone would want both an relative
>>> tolerance and an absolute tolerance, aside from the zero issue.

> Which already raises whether they'd want to min, max, average, or sum
> the two. And frankly I have no idea.

Today I experimented with implementing is_close by using a parabola equation.

     y = a(x-h)**2 + k

Note: The close area is outside the curve of the parabola.  The distance 
between the point u and v, correspond to the y value, and the x value 
corresponds to the relative distance from the vertex.

def is_parabola_close(u, v, rtol, atol=0):
     if u == v:
         return True
     if u * v < 0:
         return False
     x = (u + v) * .5
     y = (1.0/x*rtol) * x**2 + atol
     return abs(u - v) <= y


This line:
     y = (1.0/x*rtol) * x**2 + atol

Reduces to:
     y = rtol * x + atol

Which looks familiar.  LOL

It turns out the relative distance from the vertex means the x distance 
corresponds to the focus, and the y distance matches the width, for all 
values of x and y.

I thought this was interesting even though it didn't give the result I 
visualised.

I'm going to add a "size" keyword to the function to make the vertex of the 
parabola independent from the distance of the two points. ;-)

I'm not sure it helps the PEP much though.

Cheers,
    Ron











More information about the Python-ideas mailing list