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

Nick Coghlan ncoghlan at gmail.com
Fri Feb 6 14:48:36 CET 2015


On 6 February 2015 at 23:35, Paul Moore <p.f.moore at gmail.com> wrote:
> On 6 February 2015 at 12:28, Antoine Pitrou <solipsis at pitrou.net> wrote:
>> Ok, more simply then: does is_close_to(0.0, 0.0) return True?
>
> From the formula in the PEP
>
>    """abs(a-b) <= max( rel_tolerance * min(abs(a), abs(b), abs_tolerance )"""
>
> yes it does. More generally, is_close_to(x, x) is always true. That's
> a key requirement - that "closeness" includes equality.
>
> I think the "weirdness" around zero is simply that there's no x for
> which is_close_to(x, 0) and x != 0. Which TBH isn't really all that
> weird :-)

Right, by default it degrades to exact equality when one of the
operands is zero, just as it will degrade to exact equality when the
relative tolerance is set to zero. This should probably be stated
explicitly in the PEP, since it isn't immediately obvious from the
formal definition.

+1 on the PEP from me - it meets the goal I suggested of being clearly
better for comparing floating point values than standard equality, and
manages to hide most of the surprising complexity of floating point
comparisons.

Regards,
Nick.

-- 
Nick Coghlan   |   ncoghlan at gmail.com   |   Brisbane, Australia


More information about the Python-ideas mailing list