diff --git a/base/gmp.jl b/base/gmp.jl index ead80b56541a6..7cfcbd21e29da 100644 --- a/base/gmp.jl +++ b/base/gmp.jl @@ -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) diff --git a/test/gmp.jl b/test/gmp.jl index 8bfe90ec7d9e4..13413abe55f9d 100644 --- a/test/gmp.jl +++ b/test/gmp.jl @@ -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")