##############################################################
TL;DR: we are discussing about the problem that NaNs in max
, min
and sorting function causes to Python code. Examples:
>>> b = [float("nan"), 9, 0 ,1, 2]
>>> max(b)
nan
>>> min(b)
nan
>>> a = [5, float("nan"), 9, 0 ,1, 2]
>>> sorted(a)
[5, nan, 0, 1, 2, 9]
Generally, this problem is caused by any unorderable object, ie an object that returns always False
with >
and <
operators.
My proposal is that min
and max
should ignore unorderable objects, and sorting functions should emit a warning if an unorderable object is in the iterable. This should not break any old code.
##############################################################
@mdickinson @oscarbenjamin @steven.daprano @pf_moore @tiran
I’m forking this discussion from the minmax topic. The discussion started from this post.
@avayert Replying to this post, I want to say that my proposal does not break IEEE 754 at all.
Indeed, IEEE 754 says that NaNs are unorderable. That means that they are not greater or lower of any other number, NaNs included. This means that min
and max
should never return them.
So, on the contrary, is Python that actually breaks IEEE 754, since, if the iterable starts with an unorderable object, like NaN, they returns NaN:
>>> b = [float("nan"), 9, 0 ,1, 2]
>>> max(b)
nan
>>> min(b)
nan
For sorting, currently, if a NaN is in the middle of a iterable, it remains in that position. For example:
>>> a = [5, float("nan"), 9, 0 ,1, 2]
>>> sorted(a)
[5, nan, 0, 1, 2, 9]
This means that 5 < nan
, nan < 0
and 5 < 0
. All wrong. So again, is Python that does not adhere to IEEE 754.
IMHO, to completely adhere to IEEE 754:
-
min
andmax
should ignore unorderable objects - sorting functions should emit a warning if an unorderable object is in the iterable
- a function
math.total_ordering
should be implemented, so it can be passed as key to sorting functions
This will not break any existing code.