# Is there a way to enumerate through all non-NaN values of Float16?

**URL:** <https://discourse.julialang.org/t/is-there-a-way-to-enumerate-through-all-non-nan-values-of-float16/8117>\
**Category:** General Usage\
**Created:** [January 2, 2018, 10:00pm UTC](https://discourse.julialang.org/t/is-there-a-way-to-enumerate-through-all-non-nan-values-of-float16/8117 "2018-01-02T22:00:17Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![xiaodai](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/xiaodai/32/15937_2.png) [@xiaodai](https://discourse.julialang.org/u/xiaodai)\
**Post date:** [January 2, 2018, 10:00pm UTC](https://discourse.julialang.org/t/is-there-a-way-to-enumerate-through-all-non-nan-values-of-float16/8117/1 "2018-01-02T22:00:17Z")

</div>

I can make the following list of Float16 values but it generates NaNs as well. I can also get rid of NaN’s. But is there a ready built way to enumerate through all non-NaN values of Float16?

```julia
FLOAT16_UNIQS = reinterpret.(Float16, UInt16.(0:2^16-1))

```

i have trouble understanding the underlying repreaentation of float 16. On wikipedia is says that it only need 15bit in practice so I must be missing something. Clearly exponents of 0 will always be 0 so alot of values are “wasted” but I can seem to find the pattern on which UInt8 gets converted to NaN when reinterpreted as Float16

---

<div class="post-metadata">

**Author:** ![ggggggggg](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ggggggggg/32/265_2.png) [@ggggggggg](https://discourse.julialang.org/u/ggggggggg)\
**Post date:** [January 2, 2018, 10:17pm UTC](https://discourse.julialang.org/t/is-there-a-way-to-enumerate-through-all-non-nan-values-of-float16/8117/2 "2018-01-02T22:17:39Z")

</div>

I’m not an expert, but I think this works

```julia
julia> a = realmin(T)
Float16(6.104e-5)

julia> floats = []
0-element Array{Any,1}

julia> while true
       push!(floats,a)
       a = nextfloat(a)
       !isfinite(a) && break
       end

julia> length(floats)
30720

```

---

<div class="post-metadata">

**Author:** ![xiaodai](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/xiaodai/32/15937_2.png) [@xiaodai](https://discourse.julialang.org/u/xiaodai)\
**Post date:** [January 2, 2018, 10:24pm UTC](https://discourse.julialang.org/t/is-there-a-way-to-enumerate-through-all-non-nan-values-of-float16/8117/3 "2018-01-02T22:24:10Z")

</div>

There should be 63490 elements? See below

```julia
FLOAT16_UNIQS = reinterpret.(Float16, UInt16.(0:2^16-1))

unique(FLOAT16_UNIQS)

```

---

<div class="post-metadata">

**Author:** ![rdeits](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rdeits/32/286_2.png) [@rdeits](https://discourse.julialang.org/u/rdeits)\
**Post date:** [January 2, 2018, 10:44pm UTC](https://discourse.julialang.org/t/is-there-a-way-to-enumerate-through-all-non-nan-values-of-float16/8117/4 "2018-01-02T22:44:35Z")

</div>

I think you’re missing the negative and subnormal values here, which explains why you get less than half as many results as expected.

---

<div class="post-metadata">

**Author:** ![ggggggggg](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ggggggggg/32/265_2.png) [@ggggggggg](https://discourse.julialang.org/u/ggggggggg)\
**Post date:** [January 2, 2018, 10:51pm UTC](https://discourse.julialang.org/t/is-there-a-way-to-enumerate-through-all-non-nan-values-of-float16/8117/5 "2018-01-02T22:51:25Z")

</div>

This is a bit closer.

```julia
julia> floats = [];

julia> a = -Inf16
-Inf16

julia> while true
                     push!(floats,a)
                     a==Inf16 && break
                     a = nextfloat(a)
                     end

julia> length(floats)
63489

julia> setdiff(floats, FLOAT16_UNIQS)
0-element Array{Any,1}

julia> FLOAT16_UNIQS[find(iszero,FLOAT16_UNIQS)]
2-element Array{Float16,1}:
  0.0
 -0.0

```

So it just missed -0.0 I think.

---

<div class="post-metadata">

**Author:** ![ScottPJones](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/scottpjones/32/146_2.png) [@ScottPJones](https://discourse.julialang.org/u/ScottPJones)\
**Post date:** [January 3, 2018, 2:40am UTC](https://discourse.julialang.org/t/is-there-a-way-to-enumerate-through-all-non-nan-values-of-float16/8117/6 "2018-01-03T02:40:04Z")

</div>

All the 16 bit NaN values are s11111xx xxxxxxxx, where the 10 bits of xx xxxxxxxx must be non-zero (if all zero, it represents +/- Inf).  
Non-NaN values will be in the ranges 0x0000-0x7c00, and 0x8000-0xfc00.
