NaN and maximum/minimum?

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.

I found the explanation floating point - What is the rationale for all comparisons returning false for IEEE754 NaN values? - Stack Overflow 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

2 Likes

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.

2 Likes

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…