Skip to content

Commit 1b47efb

Browse files
authored
Make sure range(start; step, length) uses TwicePrecision when possible, fixes #44292. (#44313)
1 parent 262c6f1 commit 1b47efb

File tree

2 files changed

+22
-2
lines changed

2 files changed

+22
-2
lines changed

base/twiceprecision.jl

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -408,7 +408,7 @@ function floatrange(a::AbstractFloat, st::AbstractFloat, len::Real, divisor::Abs
408408
steprangelen_hp(T, (a,divisor), (st,divisor), nbitslen(T, len, 1), len, oneunit(len))
409409
end
410410

411-
function (:)(start::T, step::T, stop::T) where T<:Union{Float16,Float32,Float64}
411+
function (:)(start::T, step::T, stop::T) where T<:IEEEFloat
412412
step == 0 && throw(ArgumentError("range step cannot be zero"))
413413
# see if the inputs have exact rational approximations (and if so,
414414
# perform all computations in terms of the rationals)
@@ -453,7 +453,10 @@ end
453453
step(r::StepRangeLen{T,TwicePrecision{T},TwicePrecision{T}}) where {T<:AbstractFloat} = T(r.step)
454454
step(r::StepRangeLen{T,TwicePrecision{T},TwicePrecision{T}}) where {T} = T(r.step)
455455

456-
function range_start_step_length(a::T, st::T, len::Integer) where T<:Union{Float16,Float32,Float64}
456+
range_start_step_length(a, st::IEEEFloat, len::Integer) =
457+
range_start_step_length(oftype(st, a), st, len)
458+
459+
function range_start_step_length(a::T, st::T, len::Integer) where T<:IEEEFloat
457460
len = len + 0 # promote with Int
458461
start_n, start_d = rat(a)
459462
step_n, step_d = rat(st)
@@ -471,6 +474,11 @@ function range_start_step_length(a::T, st::T, len::Integer) where T<:Union{Float
471474
steprangelen_hp(T, a, st, 0, len, 1)
472475
end
473476

477+
function range_step_stop_length(step::IEEEFloat, stop, len::Integer)
478+
r = range_start_step_length(stop, negate(step), len)
479+
reverse(r)
480+
end
481+
474482
# This assumes that r.step has already been split so that (0:len-1)*r.step.hi is exact
475483
function unsafe_getindex(r::StepRangeLen{T,<:TwicePrecision,<:TwicePrecision}, i::Integer) where T
476484
# Very similar to _getindex_hiprec, but optimized to avoid a 2nd call to add12

test/ranges.jl

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1612,6 +1612,18 @@ end
16121612
@test x isa StepRangeLen{Float64,Base.TwicePrecision{Float64},Base.TwicePrecision{Float64}}
16131613
end
16141614

1615+
@testset "Issue #44292" begin
1616+
let x = @inferred range(0, step=0.2, length=5)
1617+
@test x isa StepRangeLen{Float64,Base.TwicePrecision{Float64},Base.TwicePrecision{Float64}}
1618+
@test x == [0.0, 0.2, 0.4, 0.6, 0.8]
1619+
end
1620+
1621+
let x = @inferred range(stop=1, step=0.2, length=5)
1622+
@test x isa StepRangeLen{Float64,Base.TwicePrecision{Float64},Base.TwicePrecision{Float64}}
1623+
@test x == [0.2, 0.4, 0.6, 0.8, 1.0]
1624+
end
1625+
end
1626+
16151627
@testset "Views of ranges" begin
16161628
@test view(Base.OneTo(10), Base.OneTo(5)) === Base.OneTo(5)
16171629
@test view(1:10, 1:5) === 1:5

0 commit comments

Comments
 (0)