Skip to content

Commit

Permalink
Add specialized * for Rational and Integer (#35483)
Browse files Browse the repository at this point in the history
  • Loading branch information
garborg authored Apr 15, 2020
1 parent 0cd77dd commit 07b7237
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 0 deletions.
5 changes: 5 additions & 0 deletions base/rational.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
5 changes: 5 additions & 0 deletions test/rational.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

2 comments on commit 07b7237

@nanosoldier
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Executing the daily package evaluation, I will reply here when finished:

@nanosoldier runtests(ALL, isdaily = true)

@nanosoldier
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Your package evaluation job has completed - possible new issues were detected. A full report can be found here. cc @maleadt

Please sign in to comment.