# Is there an easy way to change the eltype of ranges?

**URL:** https://discourse.julialang.org/t/is-there-an-easy-way-to-change-the-eltype-of-ranges/54791
**Category:** General Usage
**Tags:** question
**Created:** [February 7, 2021, 1:23pm UTC](https://discourse.julialang.org/t/is-there-an-easy-way-to-change-the-eltype-of-ranges/54791 "2021-02-07T13:23:45Z")
**Posts on this page:** 7
**Page:** 1

<div class="post-metadata">

### Author: ![jishnub](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jishnub/32/33620_2.png) [@jishnub](https://discourse.julialang.org/u/jishnub)
#### Post date: [February 7, 2021, 1:23pm UTC](https://discourse.julialang.org/t/is-there-an-easy-way-to-change-the-eltype-of-ranges/54791/1 "2021-02-07T13:23:46Z")

</div>

For example:

```julia
julia> r = 2.0:3.0
2.0:1.0:3.0

julia> eltype(r)
Float64

julia> Float32.(r)
2-element Array{Float32,1}:
 2.0
 3.0

```

This creates a `Vector` from the range. Is there a way to obtain another range (I don’t care about potential accuracy loss in the conversion)? I am looking for something a bit less verbose than calling `range` directly:

```julia
julia> range(Float32(first(r)), Float32(last(r)), length = length(r))
2.0f0:1.0f0:3.0f0

```

---

<div class="post-metadata">

### Author: ![giordano](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/giordano/32/2166_2.png) [@giordano](https://discourse.julialang.org/u/giordano)
#### Post date: [February 7, 2021, 1:26pm UTC](https://discourse.julialang.org/t/is-there-an-easy-way-to-change-the-eltype-of-ranges/54791/2 "2021-02-07T13:26:43Z")

</div>

Maybe

```julia
julia> StepRangeLen{Float32, Base.TwicePrecision{Float32}, Base.TwicePrecision{Float32}}(r)
2.0f0:1.0f0:3.0f0

```

? Not much better than your solution though

---

<div class="post-metadata">

### Author: ![FPGro](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/fpgro/32/20822_2.png) [@FPGro](https://discourse.julialang.org/u/FPGro)
#### Post date: [February 7, 2021, 2:21pm UTC](https://discourse.julialang.org/t/is-there-an-easy-way-to-change-the-eltype-of-ranges/54791/3 "2021-02-07T14:21:56Z")

</div>

```julia
julia> 2.0f0:1.0f0:3.0f0
2.0f0:1.0f0:3.0f0

```

?

---

<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 7, 2021, 4:03pm UTC](https://discourse.julialang.org/t/is-there-an-easy-way-to-change-the-eltype-of-ranges/54791/4 "2021-02-07T16:03:37Z")

</div>

There’s a decent argument `Float32.(r)` should just return a range. I’m not sure how breaking this would be in practice.

---

<div class="post-metadata">

### Author: ![jling](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jling/32/212909_2.png) [@jling](https://discourse.julialang.org/u/jling)
#### Post date: [February 7, 2021, 4:20pm UTC](https://discourse.julialang.org/t/is-there-an-easy-way-to-change-the-eltype-of-ranges/54791/5 "2021-02-07T16:20:03Z")

</div>

think about what `map(identity, r)` should do and then if `identity.(r)` should return the same result. If these two should both (semantically) give you `collect(r)`, then `Float32.(r)` shouldn’t return a range IMO.

---

<div class="post-metadata">

### Author: ![mcabbott](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mcabbott/32/6603_2.png) [@mcabbott](https://discourse.julialang.org/u/mcabbott)
#### Post date: [February 7, 2021, 4:30pm UTC](https://discourse.julialang.org/t/is-there-an-easy-way-to-change-the-eltype-of-ranges/54791/6 "2021-02-07T16:30:36Z")

</div>

Here’s what `map` does:

```julia
julia> map(Float32, r)
2.0f0:1.0f0:3.0f0

julia> map(identity, r)
2-element Vector{Float64}:
 2.0
 3.0

julia> map(Int, r)
2-element Vector{Int64}:
 2
 3

```

---

<div class="post-metadata">

### Author: ![jishnub](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jishnub/32/33620_2.png) [@jishnub](https://discourse.julialang.org/u/jishnub)
#### Post date: [February 7, 2021, 6:17pm UTC](https://discourse.julialang.org/t/is-there-an-easy-way-to-change-the-eltype-of-ranges/54791/7 "2021-02-07T18:17:57Z")

</div>

`map(Float32, r)` appears to be exactly what I wanted for this problem. `map` seems to have special methods for converting the `eltype` of ranges by calling appropriate constructors. This is perhaps not a general solution though.
