Skip to content

Commit 9d0caf9

Browse files
authored
Define gamma and lgamma for Float16 (#132)
Also make other AbstractFloat inputs result in a MethodError.
1 parent 6e49782 commit 9d0caf9

File tree

2 files changed

+21
-2
lines changed

2 files changed

+21
-2
lines changed

src/gamma.jl

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -555,8 +555,10 @@ export gamma, lgamma, beta, lbeta, lfactorial
555555

556556
## from base/special/gamma.jl
557557

558-
gamma(x::Float64) = nan_dom_err(ccall((:tgamma,libm), Float64, (Float64,), x), x)
559-
gamma(x::Float32) = nan_dom_err(ccall((:tgammaf,libm), Float32, (Float32,), x), x)
558+
gamma(x::Float64) = nan_dom_err(ccall((:tgamma, libm), Float64, (Float64,), x), x)
559+
gamma(x::Float32) = nan_dom_err(ccall((:tgammaf, libm), Float32, (Float32,), x), x)
560+
gamma(x::Float16) = Float16(gamma(Float32(x)))
561+
gamma(x::AbstractFloat) = throw(MethodError(gamma, x))
560562

561563
"""
562564
gamma(x)
@@ -760,7 +762,9 @@ end
760762

761763
@inline lgamma(x::Float64) = nan_dom_err(ccall((:lgamma, libm), Float64, (Float64,), x), x)
762764
@inline lgamma(x::Float32) = nan_dom_err(ccall((:lgammaf, libm), Float32, (Float32,), x), x)
765+
@inline lgamma(x::Float16) = Float16(lgamma(Float32(x)))
763766
@inline lgamma(x::Real) = lgamma(float(x))
767+
lgamma(x::AbstractFloat) = throw(MethodError(lgamma, x))
764768

765769
## from base/numbers.jl
766770

test/runtests.jl

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -565,6 +565,10 @@ end
565565

566566
@test sprint(showerror, AmosException(1)) == "AmosException with id 1: input error."
567567

568+
# Used to check method existence below
569+
struct NotAFloat <: AbstractFloat
570+
end
571+
568572
@testset "gamma and friends" begin
569573
@testset "gamma, lgamma (complex argument)" begin
570574
if Base.Math.libm == "libopenlibm"
@@ -628,6 +632,17 @@ end
628632
@test lgamma(-Inf*im) === -Inf - Inf*im
629633
@test lgamma(Inf + Inf*im) === lgamma(NaN + 0im) === lgamma(NaN*im) === NaN + NaN*im
630634
end
635+
636+
@testset "Other float types" begin
637+
let x = one(Float16)
638+
@test gamma(x) one(Float16)
639+
@test gamma(x) isa Float16
640+
@test lgamma(x) zero(Float16)
641+
@test lgamma(x) isa Float16
642+
end
643+
@test_throws MethodError gamma(NotAFloat())
644+
@test_throws MethodError lgamma(NotAFloat())
645+
end
631646
end
632647

633648
@testset "beta, lbeta" begin

0 commit comments

Comments
 (0)