diff --git a/base/rational.jl b/base/rational.jl index 911afb1cb06b6..5d2674f14e89a 100644 --- a/base/rational.jl +++ b/base/rational.jl @@ -279,6 +279,11 @@ function *(x::Rational, y::Rational) xd,yn = divgcd(x.den,y.num) checked_mul(xn,yn) // checked_mul(xd,yd) end +function *(x::Rational, y::Integer) + xd, yn = divgcd(x.den, y) + checked_mul(x.num, yn) // xd +end +*(x::Integer, y::Rational) = *(y, x) /(x::Rational, y::Rational) = x//y /(x::Rational, y::Complex{<:Union{Integer,Rational}}) = x//y inv(x::Rational) = Rational(x.den, x.num) diff --git a/test/rational.jl b/test/rational.jl index 4a5f1ce7c567c..7a8f37e711320 100644 --- a/test/rational.jl +++ b/test/rational.jl @@ -414,11 +414,16 @@ end @test rem(q, i) == q - i*div(q, i) @test mod(q, i) == q - i*fld(q, i) end + @test 1//2 * 3 == 3//2 + @test -3 * (1//2) == -3//2 @test_throws OverflowError UInt(1)//2 - 1 @test_throws OverflowError 1 - UInt(5)//2 @test_throws OverflowError 1//typemax(Int64) + 1 @test_throws OverflowError Int8(1) + Int8(5)//(Int8(127)-Int8(1)) + @test_throws InexactError UInt(1)//2 * -1 + @test_throws OverflowError typemax(Int64)//1 * 2 + @test_throws OverflowError -1//1 * typemin(Int64) @test Int8(1) + Int8(4)//(Int8(127)-Int8(1)) == Int8(65) // Int8(63) @test -Int32(1) // typemax(Int32) - Int32(1) == typemin(Int32) // typemax(Int32)