Skip to content

Commit

Permalink
fix #28 (#29)
Browse files Browse the repository at this point in the history
  • Loading branch information
putianyi889 authored Dec 23, 2024
1 parent 58035db commit 93e155d
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 1 deletion.
14 changes: 14 additions & 0 deletions docs/src/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -57,3 +57,17 @@ where `some` can be `el`, `base` and `precision`.
```@repl 1
precisiontype(convert_precisiontype(Int128, Int8(1)//Int8(2)))
```

## Notable behaviours

### Ranges
Ranges in Julia are not consistently processed:
```@repl 1
r = StepRange(1,1,5)
Float64.(r) |> typeof
map(Float64,r) |> typeof
```
We adapt `_to_eltype` to the return type of `Base.map`:
```@repl 1
_to_eltype(Float64, StepRange{Int,Int})
```
4 changes: 3 additions & 1 deletion src/EltypeExtensions.jl
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,6 @@ $(repr("text/plain", Matrix{Float64}))
```
"""
convert_eltype(::Type{T}, A::S) where {T,S} = convert(_to_eltype(T, S), A)
convert_eltype(::Type{T}, A::AbstractArray) where T = convert(AbstractArray{T}, A)
convert_eltype(::Type{T}, A::AbstractRange) where T = map(T, A)
convert_eltype(::Type{T}, A::AbstractUnitRange) where T<:Integer = convert(AbstractUnitRange{T}, A)
convert_eltype(::Type{T}, A::Tuple) where T = convert.(T, A)
Expand Down Expand Up @@ -86,9 +85,12 @@ _to_eltype(::Type{T}, ::Type{<:CartesianIndices}) where T = Array{T}

@static if VERSION >= v"1.7"
_to_eltype(::Type{T}, ::Type{<:StepRangeLen}) where T<:Real = StepRangeLen{T,_to_eltype(T,TwicePrecision),_to_eltype(T,TwicePrecision),Int}
_to_eltype(::Type{T}, ::Type{<:StepRange}) where T<:AbstractFloat = StepRangeLen{T,_to_eltype(T,TwicePrecision),_to_eltype(T,TwicePrecision),Int}
else
_to_eltype(::Type{T}, ::Type{<:StepRangeLen}) where T<:Real = StepRangeLen{T,_to_eltype(T,TwicePrecision),_to_eltype(T,TwicePrecision)}
_to_eltype(::Type{T}, ::Type{<:StepRange}) where {T<:AbstractFloat} = StepRangeLen{T,_to_eltype(T,TwicePrecision),_to_eltype(T,TwicePrecision)}
end
_to_eltype(::Type{T}, ::Type{<:StepRange}) where {T<:Real} = StepRange{T,T} # technically, it is better to preserve the step type. The current implementation follows from `Base.map(::Type{T}, r::StepRange) where {T<:Real}`.
_to_eltype(::Type{T}, ::Type{<:UnitRange}) where T<:Integer = UnitRange{T}
_to_eltype(::Type{T}, ::Type{<:UnitRange}) where T<:Real = _to_eltype(T, StepRangeLen)

Expand Down
4 changes: 4 additions & 0 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,10 @@ end
testelconvert(Int8, r)
testelconvert(Float64, r)

r = 1:1:5
testelconvert(Int8, r)
testelconvert(Float64, r)

inds = CartesianIndex(1,1):CartesianIndex(3,3)
testelconvert(CartesianIndex{2}, inds)
testelconvert(Tuple, inds)
Expand Down

0 comments on commit 93e155d

Please sign in to comment.