Skip to content

Commit

Permalink
Fix rational division by zero (#40551)
Browse files Browse the repository at this point in the history
* Fix rational division by zero
* Replace `BigFloat` by `BigInt`

Co-authored-by: Sebastian Stock <[email protected]>
  • Loading branch information
blegat and sostock authored Apr 21, 2021
1 parent 7f7efb1 commit 2307f80
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 1 deletion.
2 changes: 1 addition & 1 deletion base/gmp.jl
Original file line number Diff line number Diff line change
Expand Up @@ -931,7 +931,7 @@ function Base.://(x::Rational{BigInt}, y::Rational{BigInt})
if iszero(x.num)
throw(DivideError())
end
return (isneg(x.num) ? -one(BigFloat) : one(BigFloat)) // y.num
return (isneg(x.num) ? -one(BigInt) : one(BigInt)) // y.num
end
zq = _MPQ()
ccall((:__gmpq_div, :libgmp), Cvoid,
Expand Down
12 changes: 12 additions & 0 deletions test/gmp.jl
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,18 @@ ee = typemax(Int64)
@test big(typeof(complex(x, x))) == typeof(big(complex(x, x)))
end
end
@testset "division" begin
oz = big(1 // 0)
zo = big(0 // 1)

@test_throws DivideError() oz / oz
@test oz == oz / one(oz)
@test -oz == oz / (-one(oz))
@test zero(oz) == one(oz) / oz
@test_throws DivideError() zo / zo
@test one(zo) / zo == big(1//0)
@test -one(zo) / zo == big(-1//0)
end
end
@testset "div, fld, mod, rem" begin
for i = -10:10, j = [-10:-1; 1:10]
Expand Down

0 comments on commit 2307f80

Please sign in to comment.