[Python-ideas] Consider adding clip or clamp function to math

Chris Angelico rosuav at gmail.com
Thu Aug 4 09:12:36 EDT 2016


On Thu, Aug 4, 2016 at 10:42 PM, Steven D'Aprano <steve at pearwood.info> wrote:
> I would like to see a common "isnan" function or method for both floats
> and Decimal, because I'm sick of writing code like:
>
> try:
>     flag = x.is_nan()  # Decimal?
> except AttributeError:
>     flag = math.isnan(x)  # float
>
>
> Note that the spelling is different too, for extra sadness :-(

def isnan(x):
    return x != x

Might land you a few false positives, but not many. And it's IEEE compliant.

ChrisA


More information about the Python-ideas mailing list