# Iterator over Floats

**URL:** <https://discourse.julialang.org/t/iterator-over-floats/55110>\
**Category:** Numerics\
**Tags:** announcement\
**Created:** [February 12, 2021, 1:47am UTC](https://discourse.julialang.org/t/iterator-over-floats/55110 "2021-02-12T01:47:50Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Oscar\_Smith](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/oscar_smith/32/25343_2.png) [@Oscar\_Smith](https://discourse.julialang.org/u/Oscar_Smith)\
**Post date:** [February 12, 2021, 1:47am UTC](https://discourse.julialang.org/t/iterator-over-floats/55110/1 "2021-02-12T01:47:50Z")

</div>

So I’ve been working on special functions, and to help with that I’ve created this class that implements an Iterator of Float values.

```julia
struct FloatIterator{T}<:AbstractVector{T}
    min::T
    max::T
    FloatIterator{T}(min, max) where T = min > max ? error("max less than min") : new{T}(min,max)
end
FloatIterator{T}() where T =FloatIterator{T}(T(-Inf),T(Inf))
Base.iterate(it::FloatIterator) = (it.min, nextfloat(it.min))
function Base.iterate(it::FloatIterator{T}, el) where T
    return el == it.max ? nothing : (el, nextfloat(el))
end
function Base.size(it::FloatIterator{T}) where T
    if isless(it.max, 0.0)
        size = reinterpret(Base.uinttype(T), it.max) - reinterpret(Base.uinttype(T), it.min)
    elseif isless(-0.0, it.min)
        size = reinterpret(Base.uinttype(T), it.max) - reinterpret(Base.uinttype(T), it.min)
    else
        size = reinterpret(Base.uinttype(T), it.max)
        size += reinterpret(Base.uinttype(T), it.min) - reinterpret(Base.uinttype(T), T(-0.0))
    end
    return (size+1,)
end
Base.getindex(it::FloatIterator{T}, i::Int) where T = nextfloat(it.min, i-1)
Base.show(io::IO, mime::MIME"text/plain", it::FloatIterator) = show(io, it)
Base.show(io::IO, it::FloatIterator) = print(io, "FloatIterator(", it.min, ", ", it.max, ")")

```

It takes about `330μs` to loop through all Float16, and `24s` to loop through all `Float32`.

Let me know if you think this should end up in a package somewhere or if similar functionality already exists!

---

<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:** [February 12, 2021, 8:21am UTC](https://discourse.julialang.org/t/iterator-over-floats/55110/2 "2021-02-12T08:21:03Z")

</div>

for convenience, unless it goes against best Iterator API practice

```julia
FloatIterator(min::T, max::T) where T = FloatIterator{T}(min, max)

```
