diff --git a/base/rational.jl b/base/rational.jl index 69d39770b2095..b99acb13bba6f 100644 --- a/base/rational.jl +++ b/base/rational.jl @@ -105,9 +105,33 @@ function //(x::Rational, y::Rational) end //(x::Complex, y::Real) = complex(real(x)//y, imag(x)//y) -//(x::Number, y::Complex) = x*conj(y)//abs2(y) - - +function //(x::Number, y::Complex) + if(iszero((x//abs2(y))) || isinf((x//abs2(y))==1//0)) + return (x//abs2(y)) + end + return (x//abs2(y))*conj(y) +end +function //(x::Number, y::Complex{<:Integer}) + if isinf(real(y)) || isinf(imag(y)) + return 0//1 + end + real_y = real(y) + imag_y = imag(y) + m=max(abs(real_y),abs(imag_y)) + if(m==0) + return 1//0 + end + scaled_a = real_y // m + scaled_b = imag_y // m + denom = (scaled_a^2 + scaled_b^2) + if iszero(denom) + return 1//0 + end + real_part = (x * (((real_y//denom)//m)//m)) + imag_part = (-x * (((imag_y// denom)//m)//m)) + ans = (real_part + imag_part * im) + return ans +end //(X::AbstractArray, y::Number) = X .// y function show(io::IO, x::Rational) diff --git a/test/rational.jl b/test/rational.jl index 90b5414a6fe89..a0f69746c82c9 100644 --- a/test/rational.jl +++ b/test/rational.jl @@ -18,7 +18,12 @@ using Test @test -1//0 == -1//0 @test -7//0 == -1//0 @test (-1//2) // (-2//5) == 5//4 - + @testset "Complex//Number Overflow" begin + for y ∈ (1 // 0, -1 // 0) + @test (7 // complex(y)) == (7 // y) + end + @test Int8(8) // Int8(100)im == 0//1 - 2//25*im + end @test_throws OverflowError -(0x01//0x0f) @test_throws OverflowError -(typemin(Int)//1) @test_throws OverflowError (typemax(Int)//3) + 1