Skip to content

Commit

Permalink
Fix #40846, wrong results from Float64 .* StepRange (#40847)
Browse files Browse the repository at this point in the history
  • Loading branch information
mcabbott authored May 19, 2021
1 parent 6f867eb commit 613de2c
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 3 deletions.
4 changes: 2 additions & 2 deletions base/broadcast.jl
Original file line number Diff line number Diff line change
Expand Up @@ -1159,14 +1159,14 @@ broadcasted(::DefaultArrayStyle{1}, ::typeof(*), x::Number, r::StepRangeLen{T})
StepRangeLen{typeof(x*T(r.ref))}(x*r.ref, x*r.step, length(r), r.offset)
broadcasted(::DefaultArrayStyle{1}, ::typeof(*), x::Number, r::LinRange) = LinRange(x * r.start, x * r.stop, r.len)
broadcasted(::DefaultArrayStyle{1}, ::typeof(*), x::AbstractFloat, r::OrdinalRange) =
Base.range_start_step_length(x*first(r), x, length(r)) # 0.2 .* (-2:2) needs TwicePrecision
Base.range_start_step_length(x*first(r), x*step(r), length(r)) # 0.2 .* (-2:2) needs TwicePrecision
# separate in case of noncommutative multiplication:
broadcasted(::DefaultArrayStyle{1}, ::typeof(*), r::AbstractRange, x::Number) = StepRangeLen(first(r)*x, step(r)*x, length(r))
broadcasted(::DefaultArrayStyle{1}, ::typeof(*), r::StepRangeLen{T}, x::Number) where {T} =
StepRangeLen{typeof(T(r.ref)*x)}(r.ref*x, r.step*x, length(r), r.offset)
broadcasted(::DefaultArrayStyle{1}, ::typeof(*), r::LinRange, x::Number) = LinRange(r.start * x, r.stop * x, r.len)
broadcasted(::DefaultArrayStyle{1}, ::typeof(*), r::OrdinalRange, x::AbstractFloat) =
Base.range_start_step_length(first(r)*x, x, length(r))
Base.range_start_step_length(first(r)*x, step(r)*x, length(r))

broadcasted(::DefaultArrayStyle{1}, ::typeof(/), r::AbstractRange, x::Number) = range(first(r)/x, step=step(r)/x, length=length(r))
broadcasted(::DefaultArrayStyle{1}, ::typeof(/), r::StepRangeLen{T}, x::Number) where {T} =
Expand Down
21 changes: 20 additions & 1 deletion test/ranges.jl
Original file line number Diff line number Diff line change
Expand Up @@ -1581,6 +1581,19 @@ end
@test @inferred(r ./ Inf) == [0.0, 0.0, 0.0, 0.0]

@test eval(Meta.parse(repr(0 * r))) == [0.0, 0.0, 0.0, 0.0]

# Not constant-valued, but related methods:
@test @inferred(-1 * r) == [-1,-2,-3,-4]
@test @inferred(r * -1) == [-1,-2,-3,-4]
@test @inferred(r / -1) == [-1,-2,-3,-4]

@test @inferred(-1.0 .* r) == [-1,-2,-3,-4]
@test @inferred(r .* -1.0) == [-1,-2,-3,-4]
@test @inferred(r ./ -1.0) == [-1,-2,-3,-4]

@test @inferred(-1 * reverse(r)) == [-4,-3,-2,-1]
@test @inferred(-1.0 .* reverse(r)) == [-4,-3,-2,-1]
@test @inferred(reverse(r) ./ -1.0) == [-4,-3,-2,-1]
end

@test_broken @inferred(range(0, step=0, length=4)) == [0, 0, 0, 0]
Expand Down Expand Up @@ -1896,8 +1909,14 @@ end
@test_throws BoundsError r[true:true:true]
end

@testset "PR 40320 nanosoldier" begin
@testset "PR 40320 fixes" begin
# found by nanosoldier
@test 0.2 * (-2:2) == -0.4:0.2:0.4 # from tests of AbstractFFTs, needs Base.TwicePrecision
@test 0.2f0 * (-2:2) == Float32.(-0.4:0.2:0.4) # likewise needs Float64
@test 0.2 * (-2:1:2) == -0.4:0.2:0.4

# https://github.com/JuliaLang/julia/issues/40846
@test 0.1 .* (3:-1:1) [0.3, 0.2, 0.1]
@test (10:-1:1) * 0.1 == 1:-0.1:0.1
@test 0.2 * (-2:2:2) == [-0.4, 0, 0.4]
end

0 comments on commit 613de2c

Please sign in to comment.