Skip to content

Commit

Permalink
fix inf rational (#39063)
Browse files Browse the repository at this point in the history
  • Loading branch information
matbesancon authored Jan 4, 2021
1 parent ee66728 commit 1a333a9
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 1 deletion.
24 changes: 23 additions & 1 deletion base/rational.jl
Original file line number Diff line number Diff line change
Expand Up @@ -278,13 +278,35 @@ function -(x::Rational{T}) where T<:Unsigned
x
end

for (op,chop) in ((:+,:checked_add), (:-,:checked_sub), (:rem,:rem), (:mod,:mod))
function +(x::Rational, y::Rational)
xp, yp = promote(x, y)
if isinf(x) && x == y
return xp
end
xd, yd = divgcd(promote(x.den, y.den)...)
Rational(checked_add(checked_mul(x.num,yd), checked_mul(y.num,xd)), checked_mul(x.den,yd))
end

function -(x::Rational, y::Rational)
xp, yp = promote(x, y)
if isinf(x) && x == -y
return xp
end
xd, yd = divgcd(promote(x.den, y.den)...)
Rational(checked_sub(checked_mul(x.num,yd), checked_mul(y.num,xd)), checked_mul(x.den,yd))
end

for (op,chop) in ((:rem,:rem), (:mod,:mod))
@eval begin
function ($op)(x::Rational, y::Rational)
xd, yd = divgcd(promote(x.den, y.den)...)
Rational(($chop)(checked_mul(x.num,yd), checked_mul(y.num,xd)), checked_mul(x.den,yd))
end
end
end

for (op,chop) in ((:+,:checked_add), (:-,:checked_sub), (:rem,:rem), (:mod,:mod))
@eval begin
function ($op)(x::Rational, y::Integer)
unsafe_rational(($chop)(x.num, checked_mul(x.den, y)), x.den)
end
Expand Down
8 changes: 8 additions & 0 deletions test/rational.jl
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,14 @@ using Test
@test abs(one(Rational{UInt})) === one(Rational{UInt})
@test abs(one(Rational{Int})) === one(Rational{Int})
@test abs(-one(Rational{Int})) === one(Rational{Int})

# inf addition
@test 1//0 + 1//0 == 1//0
@test -1//0 - 1//0 == -1//0
@test_throws DivideError 1//0 - 1//0
@test_throws DivideError -1//0 + 1//0
@test Int128(1)//0 + 1//0 isa Rational{Int128}
@test 1//0 + Int128(1)//0 isa Rational{Int128}
end

@testset "Rational methods" begin
Expand Down

4 comments on commit 1a333a9

@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 benchmark build, I will reply here when finished:

@nanosoldier runbenchmarks(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.

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 benchmark job has completed - successfully executed benchmarks. A full report can be found here. cc @christopher-dG

@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.