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 3 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
7 changes: 5 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 Down
5 changes: 5 additions & 0 deletions test/ranges.jl
Original file line number Diff line number Diff line change
Expand Up @@ -1612,6 +1612,11 @@ end
@test x isa StepRangeLen{Float64,Base.TwicePrecision{Float64},Base.TwicePrecision{Float64}}
end

@testset "Issue #44292" begin
x = @inferred range(0, step=0.2, length=5)
@test x isa StepRangeLen{Float64,Base.TwicePrecision{Float64},Base.TwicePrecision{Float64}}
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