Skip to content

Commit cfaab24

Browse files
committed
Fix //(x::Number, y::Complex)
1 parent 8549732 commit cfaab24

File tree

2 files changed

+26
-4
lines changed

2 files changed

+26
-4
lines changed

base/rational.jl

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -105,10 +105,19 @@ function //(x::Rational, y::Rational)
105105
end
106106

107107
//(x::Complex, y::Real) = complex(real(x)//y, imag(x)//y)
108-
//(x::Number, y::Complex) = x*conj(y)//abs2(y)
109-
110-
function //(x::Union{Integer, Rational, Complex{<:Union{Rational, Integer}}}, y::Complex{<:Union{Rational, Integer}})
111-
x/y
108+
function //(x::Number, y::Complex{<:Rational})
109+
c, d = reim(y)
110+
if (isinf(c) | isinf(d))
111+
x * conj(zero(y))
112+
else
113+
x * conj(y)
114+
end//abs2(y)
115+
end
116+
function //(x::Number, y::Complex{<:Integer})
117+
c, d = reim(y)
118+
c_r, d_r = divgcd(c, d)
119+
abs2y_r = checked_add(checked_mul(c, c_r), checked_mul(d, d_r))
120+
(x * complex(c_r, checked_neg(d_r)))//abs2y_r
112121
end
113122
function //(x::Integer, y::Complex{<:Integer})
114123
a, c, d = promote(x, reim(y)...)

test/rational.jl

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -215,6 +215,7 @@ end
215215
@test_throws DivideError (0//1) / complex(0, 0)
216216
@test_throws DivideError (1//1) / complex(0, 0)
217217
@test_throws DivideError (1//0) / complex(0, 0)
218+
@test_throws DivideError complex(1//0) // complex(1//0, 1//0)
218219
@test_throws DivideError 1 // complex(0, 0)
219220
@test_throws DivideError 0 // complex(0, 0)
220221
@test_throws DivideError complex(1) // complex(0, 0)
@@ -250,6 +251,14 @@ end
250251
end
251252
end
252253
end
254+
@testset "exact division by an infinite complex number" begin
255+
for y (1 // 0, -1 // 0)
256+
@test (7 // complex(y)) == 0
257+
@test (Rational(7) // complex(y)) == 0
258+
@test (complex(7) // complex(y)) == 0
259+
@test (complex(Rational(7)) // complex(y)) == 0
260+
end
261+
end
253262
end
254263

255264
# check type of constructed rationals
@@ -633,6 +642,10 @@ end
633642
# issue #16282
634643
@test_throws MethodError 3 // 4.5im
635644

645+
# issue #60137
646+
@test_throws MethodError 3.0 // (1 + 0im)
647+
@test_throws MethodError 3.0 // (1//0 + 0im)
648+
636649
# issue #31396
637650
@test round(1//2, RoundNearestTiesUp) === 1//1
638651

0 commit comments

Comments
 (0)