# Selecting NaN for different machine precision

**URL:** <https://discourse.julialang.org/t/selecting-nan-for-different-machine-precision/4692>\
**Category:** General Usage\
**Tags:** question\
**Created:** [July 6, 2017, 3:28pm UTC](https://discourse.julialang.org/t/selecting-nan-for-different-machine-precision/4692 "2017-07-06T15:28:42Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![juliohm](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/juliohm/32/215266_2.png) [@juliohm](https://discourse.julialang.org/u/juliohm)\
**Post date:** [July 6, 2017, 3:28pm UTC](https://discourse.julialang.org/t/selecting-nan-for-different-machine-precision/4692/1 "2017-07-06T15:28:42Z")

</div>

Currently I am defining the following function to get type stability a la `one` and `zero` for single and double precision:

```julia
nan(::Type{Float32}) = NaN32
nan(::Type{Float64}) = NaN64

```

Inside of my function I call it with the appropriate type:

```julia
function f(a::T) where {T<:AbstractFloat}
  a > zero(T) ? a : nan(T)
end

```

Is it possible to extend this definition to all `AbstractFloat` types? How one would define a fallback method for other number of bits?

---

<div class="post-metadata">

**Author:** ![joshday](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/joshday/32/368_2.png) [@joshday](https://discourse.julialang.org/u/joshday)\
**Post date:** [July 6, 2017, 3:39pm UTC](https://discourse.julialang.org/t/selecting-nan-for-different-machine-precision/4692/2 "2017-07-06T15:39:20Z")

</div>

Something like this?

```julia
nan{T<:AbstractFloat}(::T) = T(NaN)

```

---

<div class="post-metadata">

**Author:** ![yuyichao](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/yuyichao/32/20_2.png) [@yuyichao](https://discourse.julialang.org/u/yuyichao)\
**Post date:** [July 6, 2017, 3:44pm UTC](https://discourse.julialang.org/t/selecting-nan-for-different-machine-precision/4692/3 "2017-07-06T15:44:32Z")

</div>

[https://github.com/JuliaLang/julia/pull/8776](https://github.com/JuliaLang/julia/pull/8776)

---

<div class="post-metadata">

**Author:** ![juliohm](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/juliohm/32/215266_2.png) [@juliohm](https://discourse.julialang.org/u/juliohm)\
**Post date:** [July 6, 2017, 3:46pm UTC](https://discourse.julialang.org/t/selecting-nan-for-different-machine-precision/4692/4 "2017-07-06T15:46:47Z")

</div>

Thank you @joshday 🙂

---

<div class="post-metadata">

**Author:** ![juliohm](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/juliohm/32/215266_2.png) [@juliohm](https://discourse.julialang.org/u/juliohm)\
**Post date:** [July 6, 2017, 3:49pm UTC](https://discourse.julialang.org/t/selecting-nan-for-different-machine-precision/4692/5 "2017-07-06T15:49:03Z")

</div>

Hi @yuyichao, is there any penalty in using the syntax `T(NaN)`? Is it performing a conversion between types?

Thanks for the link 🙂

---

<div class="post-metadata">

**Author:** ![yuyichao](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/yuyichao/32/20_2.png) [@yuyichao](https://discourse.julialang.org/u/yuyichao)\
**Post date:** [July 6, 2017, 3:52pm UTC](https://discourse.julialang.org/t/selecting-nan-for-different-machine-precision/4692/6 "2017-07-06T15:52:02Z")

</div>

For all base types there’s no difference. If there is ever a situation the two disagrees the convert should be the correct one.

---

<div class="post-metadata">

**Author:** ![juliohm](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/juliohm/32/215266_2.png) [@juliohm](https://discourse.julialang.org/u/juliohm)\
**Post date:** [July 6, 2017, 3:53pm UTC](https://discourse.julialang.org/t/selecting-nan-for-different-machine-precision/4692/7 "2017-07-06T15:53:38Z")

</div>

> [@joshday](#):
>
> nan{T\<:AbstractFloat}(::T) = T(NaN)

Just fixing a small issue, it should look like:

```julia
nan{T<:AbstractFloat}(::Type{T}) = T(NaN)

```

---

<div class="post-metadata">

**Author:** ![stevengj](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stevengj/32/71_2.png) [@stevengj](https://discourse.julialang.org/u/stevengj)\
**Post date:** [July 6, 2017, 4:40pm UTC](https://discourse.julialang.org/t/selecting-nan-for-different-machine-precision/4692/8 "2017-07-06T16:40:01Z")

</div>

> [@juliohm](#):
>
> `nan{T<:AbstractFloat}(::Type{T}) = T(NaN)`

In general, defining synonyms for basic Julia constructs will just make your code harder for others to read. It’s just as compact to use `T(NaN)` as `nan(T)`, so you might as well stick with the former.

---

<div class="post-metadata">

**Author:** ![juliohm](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/juliohm/32/215266_2.png) [@juliohm](https://discourse.julialang.org/u/juliohm)\
**Post date:** [July 6, 2017, 4:47pm UTC](https://discourse.julialang.org/t/selecting-nan-for-different-machine-precision/4692/9 "2017-07-06T16:47:29Z")

</div>

Fully agree @stevengj, I will step back and remove the definition from the package 👍
