Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make sure range(start; step, length) uses TwicePrecision when possible #44313

Merged
merged 5 commits into from
Feb 23, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 10 additions & 2 deletions base/twiceprecision.jl
Original file line number Diff line number Diff line change
Expand Up @@ -408,7 +408,7 @@ function floatrange(a::AbstractFloat, st::AbstractFloat, len::Real, divisor::Abs
steprangelen_hp(T, (a,divisor), (st,divisor), nbitslen(T, len, 1), len, oneunit(len))
end

function (:)(start::T, step::T, stop::T) where T<:Union{Float16,Float32,Float64}
function (:)(start::T, step::T, stop::T) where T<:IEEEFloat
step == 0 && throw(ArgumentError("range step cannot be zero"))
# see if the inputs have exact rational approximations (and if so,
# perform all computations in terms of the rationals)
Expand Down Expand Up @@ -453,7 +453,10 @@ 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)

function range_start_step_length(a::T, st::T, len::Integer) where T<:Union{Float16,Float32,Float64}
range_start_step_length(a, st::IEEEFloat, len::Integer) =
range_start_step_length(oftype(st, a), st, len)

function range_start_step_length(a::T, st::T, len::Integer) where T<:IEEEFloat
len = len + 0 # promote with Int
start_n, start_d = rat(a)
step_n, step_d = rat(st)
Expand All @@ -471,6 +474,11 @@ function range_start_step_length(a::T, st::T, len::Integer) where T<:Union{Float
steprangelen_hp(T, a, st, 0, len, 1)
end

function range_step_stop_length(step::IEEEFloat, stop, len::Integer)
r = range_start_step_length(stop, negate(step), len)
reverse(r)
end

# This assumes that r.step has already been split so that (0:len-1)*r.step.hi is exact
function unsafe_getindex(r::StepRangeLen{T,<:TwicePrecision,<:TwicePrecision}, i::Integer) where T
# Very similar to _getindex_hiprec, but optimized to avoid a 2nd call to add12
Expand Down
12 changes: 12 additions & 0 deletions test/ranges.jl
Original file line number Diff line number Diff line change
Expand Up @@ -1612,6 +1612,18 @@ end
@test x isa StepRangeLen{Float64,Base.TwicePrecision{Float64},Base.TwicePrecision{Float64}}
end

@testset "Issue #44292" begin
let x = @inferred range(0, step=0.2, length=5)
@test x isa StepRangeLen{Float64,Base.TwicePrecision{Float64},Base.TwicePrecision{Float64}}
@test x == [0.0, 0.2, 0.4, 0.6, 0.8]
end

let x = @inferred range(stop=1, step=0.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]
end
end

@testset "Views of ranges" begin
@test view(Base.OneTo(10), Base.OneTo(5)) === Base.OneTo(5)
@test view(1:10, 1:5) === 1:5
Expand Down