Skip to content

Commit

Permalink
Fix -1 for BigInt factorial - Update gmp.jl (#51497)
Browse files Browse the repository at this point in the history
Fixes #51488

---------

Co-authored-by: Rafael Fourquet <[email protected]>
  • Loading branch information
PallHaraldsson and rfourquet committed Oct 29, 2023
1 parent b5b05c8 commit d5c30d8
Show file tree
Hide file tree
Showing 2 changed files with 3 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 @@ -705,7 +705,7 @@ function prod(arr::AbstractArray{BigInt})
foldl(MPZ.mul!, arr; init)
end

factorial(x::BigInt) = isneg(x) ? BigInt(0) : MPZ.fac_ui(x)
factorial(n::BigInt) = !isneg(n) ? MPZ.fac_ui(n) : throw(DomainError(n, "`n` must not be negative."))

function binomial(n::BigInt, k::Integer)
k < 0 && return BigInt(0)
Expand Down
2 changes: 2 additions & 0 deletions test/gmp.jl
Original file line number Diff line number Diff line change
Expand Up @@ -220,6 +220,8 @@ end
end
@testset "combinatorics" begin
@test factorial(BigInt(40)) == parse(BigInt,"815915283247897734345611269596115894272000000000")
@test_throws DomainError factorial(BigInt(-1))
@test_throws DomainError factorial(BigInt(rand(-999:-2)))
@test binomial(BigInt(1), -1) == BigInt(0)
@test binomial(BigInt(1), 2) == BigInt(0)
@test binomial(BigInt(-53), 42) == parse(BigInt,"959509335087854414441273718")
Expand Down

0 comments on commit d5c30d8

Please sign in to comment.