Skip to content

Commit

Permalink
range uses TwicePrecision when possible (part 2) (#44528)
Browse files Browse the repository at this point in the history
  • Loading branch information
jipolanco authored Mar 11, 2022
1 parent f5d1557 commit 45ab664
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 5 deletions.
18 changes: 15 additions & 3 deletions base/twiceprecision.jl
Original file line number Diff line number Diff line change
Expand Up @@ -453,8 +453,14 @@ end
step(r::StepRangeLen{T,TwicePrecision{T},TwicePrecision{T}}) where {T<:AbstractFloat} = T(r.step)
step(r::StepRangeLen{T,TwicePrecision{T},TwicePrecision{T}}) where {T} = T(r.step)

range_start_step_length(a, st::IEEEFloat, len::Integer) =
range_start_step_length(oftype(st, a), st, len)
range_start_step_length(a::Real, st::IEEEFloat, len::Integer) =
range_start_step_length(promote(a, st)..., len)

range_start_step_length(a::IEEEFloat, st::Real, len::Integer) =
range_start_step_length(promote(a, st)..., len)

range_start_step_length(a::IEEEFloat, st::IEEEFloat, len::Integer) =
range_start_step_length(promote(a, st)..., len)

function range_start_step_length(a::T, st::T, len::Integer) where T<:IEEEFloat
len = len + 0 # promote with Int
Expand All @@ -474,7 +480,13 @@ function range_start_step_length(a::T, st::T, len::Integer) where T<:IEEEFloat
steprangelen_hp(T, a, st, 0, len, 1)
end

function range_step_stop_length(step::IEEEFloat, stop, len::Integer)
range_step_stop_length(step::Real, stop::IEEEFloat, len::Integer) =
range_step_stop_length(promote(step, stop)..., len)

range_step_stop_length(step::IEEEFloat, stop::Real, len::Integer) =
range_step_stop_length(promote(step, stop)..., len)

function range_step_stop_length(step::IEEEFloat, stop::IEEEFloat, len::Integer)
r = range_start_step_length(stop, negate(step), len)
reverse(r)
end
Expand Down
46 changes: 44 additions & 2 deletions test/ranges.jl
Original file line number Diff line number Diff line change
Expand Up @@ -1618,9 +1618,51 @@ end
@test x == [0.0, 0.2, 0.4, 0.6, 0.8]
end

let x = @inferred range(stop=1, step=0.2, length=5)
let x = @inferred range(0.0, step=2, length=5)
@test x isa StepRangeLen{Float64,Base.TwicePrecision{Float64},Base.TwicePrecision{Float64}}
@test x == [0.2, 0.4, 0.6, 0.8, 1.0]
@test x == [0.0, 2.0, 4.0, 6.0, 8.0]
@test x === range(0.0, step=2.0, length=5)
@test x === range(0.0f0, step=2e0, length=5)
@test x === range(0e0, step=2.0f0, length=5)
end

# start::IEEEFloat and step::Complex
let x = @inferred range(2.0, step=1im, length=3)
@test typeof(x) === StepRangeLen{ComplexF64, Float64, Complex{Int}, Int}
@test x == range(2, step=1im, length=3) # compare with integer range
@test x == 2.0 .+ [0im, 1im, 2im]
end

# start::Complex and step::IEEEFloat
let x = @inferred range(2im, step=1.0, length=3)
@test typeof(x) === StepRangeLen{ComplexF64, Complex{Int}, Float64, Int}
@test x == range(2im, step=1, length=3) # compare with integer range
end

# stop::IEEEFloat and step::Complex
let x = @inferred range(stop=2.0, step=1im, length=3)
@test typeof(x) === StepRangeLen{ComplexF64, ComplexF64, Complex{Int}, Int}
@test x == range(stop=2, step=1im, length=3) # compare with integer range
@test x == 2.0 .- [2im, 1im, 0im]
end

# stop::Complex and step::IEEEFloat
let x = @inferred range(stop=2im, step=1.0, length=3)
@test typeof(x) === StepRangeLen{ComplexF64, ComplexF64, Float64, Int}
@test x == range(stop=2im, step=1, length=3) # compare with integer range
end

let x = @inferred range(stop=10, step=2.0, length=5)
@test x isa StepRangeLen{Float64,Base.TwicePrecision{Float64},Base.TwicePrecision{Float64}}
@test x === @inferred range(stop=10.0, step=2.0, length=5)
@test x === @inferred range(stop=10f0, step=2.0, length=5)
@test x === @inferred range(stop=10e0, step=2.0f0, length=5)
@test x == [2, 4, 6, 8, 10]
end

let x = @inferred range(stop=10.0, step=2, length=4)
@test x isa StepRangeLen{Float64,Base.TwicePrecision{Float64},Base.TwicePrecision{Float64}}
@test x == [4.0, 6.0, 8.0, 10.0]
end
end

Expand Down

0 comments on commit 45ab664

Please sign in to comment.