max(float('nan'), 1)
evaluates to nan
max(1, float('nan'))
evaluates to 1
Is it the intended behavior?
Thanks for the answers.
max
raises an exception when the iterable is empty. Why wouldn't Python's max
raise an exception when nan
is present? Or at least do something useful, like return nan
or ignore nan
. The current behavior is very unsafe and seems completely unreasonable.
I found an even more surprising consequence of this behavior, so I just posted a related question.
max(0.5, float('nan'), 1)
returns 1. – Platinomax
result depends on the order of parameters, which is a bit unexpected to me, even if it only happened in one example. But in fact it works on your example too:max(float('nan'), 1, 0.5)
returns nan. – Dignitaryabc
andabc
aren't actually the same object (note that in some implementations they actually might be the same object, let's ignore this). of course,max('abc', 'abc')
will return one of these literals, but which one is arbitrary and depends on the order. Precisely as you'd expect, I think? – Dignitary