Skip to content

Commit

Permalink
Improve support for constructing zero-step float ranges
Browse files Browse the repository at this point in the history
  • Loading branch information
mbauman committed Sep 5, 2018
1 parent 8d99356 commit efc0abf
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 0 deletions.
2 changes: 2 additions & 0 deletions base/twiceprecision.jl
Original file line number Diff line number Diff line change
Expand Up @@ -356,6 +356,8 @@ StepRangeLen(ref::TwicePrecision{T}, step::TwicePrecision{T},
function floatrange(::Type{T}, start_n::Integer, step_n::Integer, len::Integer, den::Integer) where T
if len < 2
return steprangelen_hp(T, (start_n, den), (step_n, den), 0, Int(len), 1)
elseif step_n == 0
return steprangelen_hp(T, (start_n, den), (step_n, den), 0, Int(len), 1)
end
# index of smallest-magnitude value
imin = clamp(round(Int, -start_n/step_n+1), 1, Int(len))
Expand Down
32 changes: 32 additions & 0 deletions test/ranges.jl
Original file line number Diff line number Diff line change
Expand Up @@ -1371,3 +1371,35 @@ end # module NonStandardIntegerRangeTest
end
end
end

@testset "constant-valued ranges (issues #10391 and #29052)" begin
for r in ((1:4), (1:1:4), (1.0:4.0))
if eltype(r) === Int
@test_broken @inferred(0 * r) == [0.0, 0.0, 0.0, 0.0]
@test_broken @inferred(0 .* r) == [0.0, 0.0, 0.0, 0.0]
@test_broken @inferred(r .+ (4:-1:1)) == [5.0, 5.0, 5.0, 5.0]
else
@test @inferred(0 * r) == [0.0, 0.0, 0.0, 0.0]
@test @inferred(0 .* r) == [0.0, 0.0, 0.0, 0.0]
@test @inferred(r .+ (4:-1:1)) == [5.0, 5.0, 5.0, 5.0]
@test @inferred(r .+ (4.0:-1:1)) == [5.0, 5.0, 5.0, 5.0]
end
@test @inferred(0.0 * r) == [0.0, 0.0, 0.0, 0.0]
@test @inferred(0.0 .* r) == [0.0, 0.0, 0.0, 0.0]
@test @inferred(r / Inf) == [0.0, 0.0, 0.0, 0.0]
@test @inferred(r ./ Inf) == [0.0, 0.0, 0.0, 0.0]
end

@test_broken @inferred(range(0, step=0, length=4)) == [0, 0, 0, 0]
@test @inferred(range(0, stop=0, length=4)) == [0, 0, 0, 0]
@test @inferred(range(0.0, step=0.0, length=4)) == [0.0, 0.0, 0.0, 0.0]
@test @inferred(range(0.0, stop=0.0, length=4)) == [0.0, 0.0, 0.0, 0.0]
@test @inferred(range(0, step=0.0, length=4)) == [0.0, 0.0, 0.0, 0.0]
@test @inferred(range(0.0, step=0, length=4)) == [0.0, 0.0, 0.0, 0.0]
@test @inferred(range(0, stop=0.0, length=4)) == [0.0, 0.0, 0.0, 0.0]
@test @inferred(range(0.0, stop=0, length=4)) == [0.0, 0.0, 0.0, 0.0]

z4 = 0.0 * (1:4)
@test @inferred(z4 .+ (1:4)) === 1.0:1.0:4.0
@test @inferred(z4 .+ z4) === z4
end

0 comments on commit efc0abf

Please sign in to comment.