Skip to content

Commit

Permalink
Improve support for constructing zero-step float ranges (#29056)
Browse files Browse the repository at this point in the history
* Improve support for constructing zero-step float ranges

Fixes `0.0 * (1:4)`, for example. From https://github.com/JuliaLang/julia/issues/29052#issuecomment-418825887

* simplify

(cherry picked from commit 26b6a58)
  • Loading branch information
mbauman authored and KristofferC committed Sep 17, 2018
1 parent 3212800 commit f952754
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 1 deletion.
2 changes: 1 addition & 1 deletion base/twiceprecision.jl
Original file line number Diff line number Diff line change
Expand Up @@ -357,7 +357,7 @@ StepRangeLen(ref::TwicePrecision{T}, step::TwicePrecision{T},

# Construct range for rational start=start_n/den, step=step_n/den
function floatrange(::Type{T}, start_n::Integer, step_n::Integer, len::Integer, den::Integer) where T
if len < 2
if len < 2 || step_n == 0
return steprangelen_hp(T, (start_n, den), (step_n, den), 0, Int(len), 1)
end
# index of smallest-magnitude value
Expand Down
34 changes: 34 additions & 0 deletions test/ranges.jl
Original file line number Diff line number Diff line change
Expand Up @@ -1372,6 +1372,40 @@ end # module NonStandardIntegerRangeTest
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]
@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:-1:1)) == [5.0, 5.0, 5.0, 5.0]
end
@test @inferred(r .+ (4.0:-1:1)) == [5.0, 5.0, 5.0, 5.0]
@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

@testset "allocation of TwicePrecision call" begin
0:286.493442:360
0:286:360
Expand Down

0 comments on commit f952754

Please sign in to comment.