On Sat, Jul 4, 2020, at 15:57, 2qdxy4rzwzuui...@potatochowder.com wrote:
> > Simplifying the signature, in Python we have:
> > 
> > def min(*iterable):
> >     iterator = iter(iterable)
> >     minimum = next(iterable)
> >     for item in iterator:
> >         if item < minimum:
> >             minimum = item
> >     return minimum
> > 
> > Due to this, min(0, float('nan')) == 0 and same for max. I would hence
> > expect clamp to behave similarly.
> 
> Yuck:  We also have min(float('nan'), 0) == float('nan').
> 
> I'm not sure what I'd expect a hypothetical clamp function to do.
> Someone with actual use cases will have more insight.

IEEE 754-2019 defines minimum and maximum functions that return NaN in all 
cases, and apply a strict ordering to signed zero... however, there is also a 
minimumNumber and maximumNumber which returns the number if the other operand 
is NaN [the asymmetric behavior depending on the order of the operands isn't 
allowed by either]

it might be worthwhile to define a "min2" etc that applies the rules for one of 
these functions when both arguments are floats [and possibly when one argument 
is a float and the other is any numeric type], and then define min(*iterable) 
as:

def min(*iterable):
    iterator = iter(iterable)
    minimum = next(iterable)
    for item in iterator:
        minimum = min2(minimum, item)
    return minimum

I can't find anything about a clamp-like function in IEEE.

It may be worth surveying what other implementations do... to start with:
- Rust has a clamp function that returns NaN if the given number is NaN, and 
"panics" if either boundary is NaN.
- Numpy's clip function effectively accepts NaN or None boundaries as "don't 
care"
- - This appears to be implemented as min(max(x, a), b), with min and max 
themselves having the asymmetric behavior.
- C++'s clamp function seems to be undefined if any of the operands are NaN
- - T must meet the requirements of LessThanComparable in order to use 
overloads (1).
- - However, if NaNs are avoided, T can a be floating-point type. 
_______________________________________________
Python-ideas mailing list -- python-ideas@python.org
To unsubscribe send an email to python-ideas-le...@python.org
https://mail.python.org/mailman3/lists/python-ideas.python.org/
Message archived at 
https://mail.python.org/archives/list/python-ideas@python.org/message/HUJFXYVWWZRNQVM7SAJY2MS3TFFC5P3Y/
Code of Conduct: http://python.org/psf/codeofconduct/

Reply via email to