Skip to content

Commit 2307f80

Browse files
blegatsostock
andauthored
Fix rational division by zero (#40551)
* Fix rational division by zero * Replace `BigFloat` by `BigInt` Co-authored-by: Sebastian Stock <[email protected]>
1 parent 7f7efb1 commit 2307f80

File tree

2 files changed

+13
-1
lines changed

2 files changed

+13
-1
lines changed

base/gmp.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -931,7 +931,7 @@ function Base.://(x::Rational{BigInt}, y::Rational{BigInt})
931931
if iszero(x.num)
932932
throw(DivideError())
933933
end
934-
return (isneg(x.num) ? -one(BigFloat) : one(BigFloat)) // y.num
934+
return (isneg(x.num) ? -one(BigInt) : one(BigInt)) // y.num
935935
end
936936
zq = _MPQ()
937937
ccall((:__gmpq_div, :libgmp), Cvoid,

test/gmp.jl

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,18 @@ ee = typemax(Int64)
6969
@test big(typeof(complex(x, x))) == typeof(big(complex(x, x)))
7070
end
7171
end
72+
@testset "division" begin
73+
oz = big(1 // 0)
74+
zo = big(0 // 1)
75+
76+
@test_throws DivideError() oz / oz
77+
@test oz == oz / one(oz)
78+
@test -oz == oz / (-one(oz))
79+
@test zero(oz) == one(oz) / oz
80+
@test_throws DivideError() zo / zo
81+
@test one(zo) / zo == big(1//0)
82+
@test -one(zo) / zo == big(-1//0)
83+
end
7284
end
7385
@testset "div, fld, mod, rem" begin
7486
for i = -10:10, j = [-10:-1; 1:10]

0 commit comments

Comments
 (0)