NaN and maximum/minimum?


#1

Probably a stupid question… but why is:

julia> maximum([1,NaN]) 
NaN
julia> minimum([1,NaN])
NaN
julia> 1 > NaN
false
julia> 1 < NaN
false

…I have a rectangular matrix of (possibly complex) vectors, of which 2 elements have zero length – so I replace them with NaN for plotting reasons. Then I want to find the non-NaN element in this matrix of vectors with the largest real part.


#2

I found the explanation https://stackoverflow.com/a/1573715/2891936 interesting.

To get your results you will have to filter or replace the NaN values.
maximum(filter(!isnan,a)) #allocates
maximum(x->isnan(x) ? -Inf : x,a) #non-allocating


#3

NaN is specified by the IEEE Standard so that any comparison with a NaN results is false (including NaN==NaN).

Since 1 <= NaN === false and NaN <= 1 === false, minmax(1, NaN) == (NaN, NaN). That’s why the minimum and maximum functions behave as you noted.


#4

Thanks. I tried to use missing instead, but the plot routine complained that it was not a Float. Not that I know whether that had worked better with maximum, though…