Skip to content

Commit 66f9db4

Browse files
committed
Use promote_eltype_op for ./, .\, and .^ (fixes #14725)
1 parent d341677 commit 66f9db4

File tree

3 files changed

+21
-12
lines changed

3 files changed

+21
-12
lines changed

base/arraymath.jl

+6-12
Original file line numberDiff line numberDiff line change
@@ -43,18 +43,12 @@ promote_array_type{S<:Integer}(F, ::Type{S}, ::Type{Bool}) = S
4343
promote_array_type(F, ::Type{Bool}, ::Type{Bool}) = promote_op(F, Bool, Bool)
4444

4545
# Handle operations that return different types
46-
./(x::Number, Y::AbstractArray) =
47-
reshape([ x ./ y for y in Y ], size(Y))
48-
./(X::AbstractArray, y::Number) =
49-
reshape([ x ./ y for x in X ], size(X))
50-
.\(x::Number, Y::AbstractArray) =
51-
reshape([ x .\ y for y in Y ], size(Y))
52-
.\(X::AbstractArray, y::Number) =
53-
reshape([ x .\ y for x in X ], size(X))
54-
.^(x::Number, Y::AbstractArray) =
55-
reshape([ x ^ y for y in Y ], size(Y))
56-
.^(X::AbstractArray, y::Number ) =
57-
reshape([ x ^ y for x in X ], size(X))
46+
for f in (:./, :.\, :.^)
47+
@eval ($f)(x::Number, Y::AbstractArray) =
48+
reshape((promote_eltype_op($f, x, Y))[ ($f)(x, y) for y in Y ], size(Y))
49+
@eval ($f)(X::AbstractArray, y::Number) =
50+
reshape((promote_eltype_op($f, X, y))[ ($f)(x, y) for x in X ], size(X))
51+
end
5852

5953
for f in (:+, :-, :div, :mod, :&, :|, :$)
6054
@eval begin

base/complex.jl

+4
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,10 @@ promote_op{T<:Real,S<:Real}(op, ::Type{Complex{T}}, ::Type{S}) =
3232
Complex{promote_op(op,T,S)}
3333
promote_op{T<:Real,S<:Real}(op, ::Type{T}, ::Type{Complex{S}}) =
3434
Complex{promote_op(op,T,S)}
35+
promote_op{T<:Integer,S<:Integer}(::typeof(^), ::Type{T}, ::Type{Complex{S}}) =
36+
Complex{Float64}
37+
promote_op{T<:Integer,S<:Integer}(::typeof(.^), ::Type{T}, ::Type{Complex{S}}) =
38+
Complex{Float64}
3539

3640
widen{T}(::Type{Complex{T}}) = Complex{widen(T)}
3741

test/broadcast.jl

+11
Original file line numberDiff line numberDiff line change
@@ -116,3 +116,14 @@ rt = Base.return_types(broadcast, Tuple{Function, Array{Float64, 3}, Array{Int,
116116
@test length(rt) == 1 && rt[1] == Array{Float64, 3}
117117
rt = Base.return_types(broadcast!, Tuple{Function, Array{Float64, 3}, Array{Float64, 3}, Array{Int, 1}})
118118
@test length(rt) == 1 && rt[1] == Array{Float64, 3}
119+
120+
# issue 14725
121+
let a = Number[2, 2.0, 4//2, 2+0im] / 2
122+
@test eltype(a) == Number
123+
end
124+
let a = Real[2, 2.0, 4//2] / 2
125+
@test eltype(a) == Real
126+
end
127+
let a = Real[2, 2.0, 4//2] / 2.0
128+
@test eltype(a) <: Real # promotion to Float64 would also be possible
129+
end

0 commit comments

Comments
 (0)