Skip to content
Open
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 20 additions & 0 deletions base/rational.jl
Original file line number Diff line number Diff line change
Expand Up @@ -105,8 +105,28 @@ function //(x::Rational, y::Rational)
end

//(x::Complex, y::Real) = complex(real(x)//y, imag(x)//y)

# TODO Unclear if this method should be defined. It seems to conflict with the docstring that states that "The arguments must be subtypes of Integer, Rational, or composites thereof."
//(x::Number, y::Complex) = x*conj(y)//abs2(y)

function //(x::Union{Integer, Rational, Complex{<:Union{Rational, Integer}}}, y::Complex{<:Union{Rational, Integer}})
x/y
end
function //(x::Integer, y::Complex{<:Integer})
a, c, d = promote(x, reim(y)...)
c_r, d_r = divgcd(c, d)
abs2y_r = checked_add(checked_mul(c, c_r), checked_mul(d, d_r))
complex(checked_mul(a, c_r), checked_neg(checked_mul(a, d_r)))//abs2y_r
end
function //(x::Complex{<:Integer}, y::Complex{<:Integer})
a, b, c, d = promote(reim(x)..., reim(y)...)
c_r, d_r = divgcd(c, d)
abs2y_r = checked_add(checked_mul(c, c_r), checked_mul(d, d_r))
complex(
checked_add(checked_mul(a, c_r), checked_mul(b, d_r)),
checked_add(checked_mul(b, c_r), checked_neg(checked_mul(a, d_r)))
)//abs2y_r
end

//(X::AbstractArray, y::Number) = X .// y

Expand Down
7 changes: 7 additions & 0 deletions test/rational.jl
Original file line number Diff line number Diff line change
Expand Up @@ -215,9 +215,16 @@ end
@test_throws DivideError (0//1) / complex(0, 0)
@test_throws DivideError (1//1) / complex(0, 0)
@test_throws DivideError (1//0) / complex(0, 0)
@test_throws DivideError 1 // complex(0, 0)
@test_throws DivideError 0 // complex(0, 0)
@test_throws DivideError complex(1) // complex(0, 0)
@test_throws DivideError complex(0) // complex(0, 0)

# 1//200 - 1//200*im cannot be represented as Complex{Rational{Int8}}
@test_throws OverflowError (Int8(1)//Int8(1)) / (Int8(100) + Int8(100)im)
@test_throws OverflowError (Int8(1)//Int8(1)) // (Int8(100) + Int8(100)im)
@test_throws OverflowError Int8(1) // (Int8(100) + Int8(100)im)
@test_throws OverflowError complex(Int8(1)) // (Int8(100) + Int8(100)im)

@test Complex(rand_int, 0) == Rational(rand_int)
@test Rational(rand_int) == Complex(rand_int, 0)
Expand Down