Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Throw an error in case of division by zero instead of crashing hard #1828

Merged
merged 1 commit into from
Oct 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading