# NaN and maximum/minimum?

**URL:** <https://discourse.julialang.org/t/nan-and-maximum-minimum/17481>\
**Category:** New to Julia\
**Created:** [November 13, 2018, 3:56pm UTC](https://discourse.julialang.org/t/nan-and-maximum-minimum/17481 "2018-11-13T15:56:33Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![BLI](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/bli/32/37206_2.png) [@BLI](https://discourse.julialang.org/u/BLI)\
**Post date:** [November 13, 2018, 3:56pm UTC](https://discourse.julialang.org/t/nan-and-maximum-minimum/17481/1 "2018-11-13T15:56:33Z")

</div>

Probably a stupid question… but _why_ is:

```julia
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.

---

<div class="post-metadata">

**Author:** ![Daniel\_Berge](https://avatars.discourse-cdn.com/v4/letter/d/eb9ed0/32.png) [@Daniel\_Berge](https://discourse.julialang.org/u/Daniel_Berge)\
**Post date:** [November 13, 2018, 4:14pm UTC](https://discourse.julialang.org/t/nan-and-maximum-minimum/17481/2 "2018-11-13T16:14:29Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![JeffreySarnoff](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jeffreysarnoff/32/1980_2.png) [@JeffreySarnoff](https://discourse.julialang.org/u/JeffreySarnoff)\
**Post date:** [November 13, 2018, 4:30pm UTC](https://discourse.julialang.org/t/nan-and-maximum-minimum/17481/3 "2018-11-13T16:30:41Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![BLI](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/bli/32/37206_2.png) [@BLI](https://discourse.julialang.org/u/BLI)\
**Post date:** [November 13, 2018, 4:38pm UTC](https://discourse.julialang.org/t/nan-and-maximum-minimum/17481/4 "2018-11-13T16:38:25Z")

</div>

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…
