Why does max() sometimes return nan and sometimes ignores it?

The reason is that max works by taking the first value as the “max seen so far”, and then checking each other value to see if it is bigger than the max seen so far. But nan is defined so that comparisons with it always return False — that is, nan > 1 is false but 1 > nan is also false.

So if you start with nan as the first value in the array, every subsequent comparison will be check whether some_other_value > nan. This will always be false, so nan will retain its position as “max seen so far”. On the other hand, if nan is not the first value, then when it is reached, the comparison nan > max_so_far will again be false. But in this case that means the current “max seen so far” (which is not nan) will remain the max seen so far, so the nan will always be discarded.

Leave a Comment