Re: [Python-ideas] Add math.iszero() and math.isequal()?

See PEP 485, which appears to be still a draft: https://www.python.org/dev/peps/pep-0485/ Best, Neil On Tuesday, May 12, 2015 at 3:18:47 AM UTC-4, Mark Summerfield wrote:
From Python 3.2 it is easy to compare floats, e.g.,
iszero = lambda x: hash(x) == hash(0) isequal = lambda a, b: hash(a) == hash(b)
Clearly these are trivial functions (but perphaps math experts could provide better implementations; I'm not proposing the implementations shown, just the functions however they are implemented).
It seems that not everyone is aware of the issues regarding comparing floats for equality and so I still see code that compares floats using == or !=.
If these functions were in the math module it would be convenient (since I find I need them in most non-trivial programs), but also provide a place to document that they should be used rather than == or != for floats. (I guess a similar argument might apply to the cmath module?)
participants (1)
-
Neil Girdhar