Skip to content

Commit

Permalink
Restrict lfact to the domain of factorial (#21321)
Browse files Browse the repository at this point in the history
  • Loading branch information
ararslan authored Apr 10, 2017
1 parent 7c1e5af commit df2438c
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 3 deletions.
6 changes: 4 additions & 2 deletions base/special/gamma.jl
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,11 @@ lgamma_r(x::Number) = lgamma(x), 1 # lgamma does not take abs for non-real x
"""
lfact(x)
Compute the logarithmic factorial of `x`
Compute the logarithmic factorial of a nonnegative integer `x`.
Equivalent to [`lgamma`](@ref) of `x + 1`, but `lgamma` extends this function
to non-integer `x`.
"""
lfact(x::Real) = (x<=1 ? zero(float(x)) : lgamma(x+oneunit(x)))
lfact(x::Integer) = x < 0 ? throw(DomainError()) : lgamma(x + oneunit(x))

"""
lgamma(x)
Expand Down
5 changes: 4 additions & 1 deletion test/math.jl
Original file line number Diff line number Diff line change
Expand Up @@ -424,8 +424,11 @@ relerrc(z, x) = max(relerr(real(z),real(x)), relerr(imag(z),imag(x)))
for x in (3.2, 2+1im, 3//2, 3.2+0.1im)
@test factorial(x) == gamma(1+x)
end
@test lfact(1) == 0
@test lfact(0) == lfact(1) == 0
@test lfact(2) == lgamma(3)
# Ensure that the domain of lfact matches that of factorial (issue #21318)
@test_throws DomainError lfact(-3)
@test_throws MethodError lfact(1.0)
end

# lgamma test cases (from Wolfram Alpha)
Expand Down

0 comments on commit df2438c

Please sign in to comment.