Skip to content

Commit

Permalink
Throw an error in case of division by zero instead of crashing hard (N…
Browse files Browse the repository at this point in the history
  • Loading branch information
joschmitt authored Oct 4, 2024
1 parent 6be27f6 commit 0fcc4cd
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 0 deletions.
3 changes: 3 additions & 0 deletions src/julia/Integer.jl
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,7 @@ function divexact(a::Integer, b::Integer; check::Bool=true)
end

function divexact(a::BigInt, b::BigInt; check::Bool=true)
iszero(b) && throw(DivideError())
q = BigInt()
if check
r = BigInt()
Expand All @@ -191,6 +192,7 @@ function divexact(a::BigInt, b::BigInt; check::Bool=true)
end

function divexact(a::BigInt, b::Int; check::Bool=true)
iszero(b) && throw(DivideError())
q = BigInt()
sgn = b < 0
if check
Expand All @@ -206,6 +208,7 @@ function divexact(a::BigInt, b::Int; check::Bool=true)
end

function divexact(a::BigInt, b::UInt; check::Bool=true)
iszero(b) && throw(DivideError())
q = BigInt()
if check
r = BigInt()
Expand Down
7 changes: 7 additions & 0 deletions test/julia/Integers-test.jl
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,13 @@ end
@test_throws ArgumentError divexact(big(10), big(4))
@test_throws ArgumentError divexact(big(10), 4)

@test_throws DivideError divexact(big(10), big(0))
@test_throws DivideError divexact(big(10), big(0), check = false)
@test_throws DivideError divexact(big(10), 0)
@test_throws DivideError divexact(big(10), 0, check = false)
@test_throws DivideError divexact(big(10), UInt(0))
@test_throws DivideError divexact(big(10), UInt(0), check = false)

for iter = 1:1000
a1 = rand(R, -100:100)
a2 = rand(R, -100:100)
Expand Down

0 comments on commit 0fcc4cd

Please sign in to comment.