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

On Wed, Aug 03, 2016 at 11:04:33AM +1200, Greg Ewing wrote:
IEEE-754 specifies the behaviour of min() and max() with a NAN argument. See my previous email.
That's wrong. Decimal has NANs. Other numeric types could potentially have NANs too.
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 :-( -- Steve

On Thu, Aug 04, 2016 at 11:12:36PM +1000, Chris Angelico wrote:
That's what I used to use before math.isnan existed. But I fear some clever clogs deciding that they like NANs but don't like that NANs violate reflexivity, or merely optimizing equality like this: def __eq__(self, other): # optimisation if self is other: return True # more expensive comparison ... In the absence of an isnan() method or function, falling back on x!=x is okay as a last resort, but I wouldn't want to rely on it as the first resort. -- Steve

On Thu, Aug 04, 2016 at 11:12:36PM +1000, Chris Angelico wrote:
That's what I used to use before math.isnan existed. But I fear some clever clogs deciding that they like NANs but don't like that NANs violate reflexivity, or merely optimizing equality like this: def __eq__(self, other): # optimisation if self is other: return True # more expensive comparison ... In the absence of an isnan() method or function, falling back on x!=x is okay as a last resort, but I wouldn't want to rely on it as the first resort. -- Steve
participants (2)
-
Chris Angelico
-
Steven D'Aprano