Skip to content

Commit df2438c

Browse files
authored
Restrict lfact to the domain of factorial (#21321)
1 parent 7c1e5af commit df2438c

File tree

2 files changed

+8
-3
lines changed

2 files changed

+8
-3
lines changed

base/special/gamma.jl

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,9 +27,11 @@ lgamma_r(x::Number) = lgamma(x), 1 # lgamma does not take abs for non-real x
2727
"""
2828
lfact(x)
2929
30-
Compute the logarithmic factorial of `x`
30+
Compute the logarithmic factorial of a nonnegative integer `x`.
31+
Equivalent to [`lgamma`](@ref) of `x + 1`, but `lgamma` extends this function
32+
to non-integer `x`.
3133
"""
32-
lfact(x::Real) = (x<=1 ? zero(float(x)) : lgamma(x+oneunit(x)))
34+
lfact(x::Integer) = x < 0 ? throw(DomainError()) : lgamma(x + oneunit(x))
3335

3436
"""
3537
lgamma(x)

test/math.jl

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -424,8 +424,11 @@ relerrc(z, x) = max(relerr(real(z),real(x)), relerr(imag(z),imag(x)))
424424
for x in (3.2, 2+1im, 3//2, 3.2+0.1im)
425425
@test factorial(x) == gamma(1+x)
426426
end
427-
@test lfact(1) == 0
427+
@test lfact(0) == lfact(1) == 0
428428
@test lfact(2) == lgamma(3)
429+
# Ensure that the domain of lfact matches that of factorial (issue #21318)
430+
@test_throws DomainError lfact(-3)
431+
@test_throws MethodError lfact(1.0)
429432
end
430433

431434
# lgamma test cases (from Wolfram Alpha)

0 commit comments

Comments
 (0)