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

Small dispatch fix for gamma #63

Merged
merged 2 commits into from
Nov 2, 2022
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
7 changes: 3 additions & 4 deletions src/gamma.jl
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# Adapted from Cephes Mathematical Library (MIT license https://en.smath.com/view/CephesMathLibrary/license) by Stephen L. Moshier
gamma(z::Number) = _gamma(float(z))
_gamma(x::Float32) = Float32(_gamma(Float64(x)))
gamma(x::Float64) = _gamma(x)
gamma(x::Float32) = Float32(_gamma(Float64(x)))

function _gamma(x::Float64)
T = Float64
Expand Down Expand Up @@ -50,10 +50,9 @@ function _gamma(x::Float64)
return z * p / q
end


function gamma(n::Integer)
n < 0 && throw(DomainError(n, "`n` must not be negative."))
n == 0 && return Inf*float(n)
n == 0 && return Inf*one(n)
n > 20 && return gamma(float(n))
@inbounds return Float64(factorial(n-1))
end
3 changes: 3 additions & 0 deletions test/gamma_test.jl
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,6 @@ x = rand(10000)*170
@test SpecialFunctions.gamma.(BigFloat.(-x)) ≈ Bessels.gamma.(-x)
@test isnan(Bessels.gamma(NaN))
@test isinf(Bessels.gamma(Inf))

x = [0, 1, 2, 3, 8, 15, 20, 30]
@test SpecialFunctions.gamma.(x) ≈ Bessels.gamma.(x)