Skip to content

Commit

Permalink
Merge pull request #325 from devmotion/dw/logabsbeta
Browse files Browse the repository at this point in the history
Fix `logabsbeta` for BigFloat
  • Loading branch information
andreasnoack authored Jun 4, 2021
2 parents 734f242 + 9e7461f commit 66eb6c7
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 1 deletion.
4 changes: 3 additions & 1 deletion src/beta_inc.jl
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,10 @@ const exparg_p = log(prevfloat(floatmax(Float64)))
Computes ``log(\\Gamma(b)/\\Gamma(a+b))`` when b >= 8
"""
loggammadiv(a::Number, b::Number) = _loggammadiv(float(a), float(b))
loggammadiv(a::Number, b::Number) = _loggammadiv(promote(float(a), float(b))...)

# TODO: Add a proper implementation
_loggammadiv(a::Number, b::Number) = loggamma(b) - loggamma(a + b) # handle e.g. BigFloat (used by `logabsbeta`)
_loggammadiv(a::T, b::T) where {T<:Base.IEEEFloat} = T(_loggammadiv(Float64(a), Float64(b))) # handle Float16, Float32
function _loggammadiv(a::Float64, b::Float64)
@assert b >= 8
Expand Down
1 change: 1 addition & 0 deletions test/beta_inc.jl
Original file line number Diff line number Diff line change
Expand Up @@ -227,6 +227,7 @@
@test beta_inc(a, b, x, 1 - x) == beta_inc(a, Float64(b), Float64(x), 1 - x)

@test SpecialFunctions.loggammadiv(13.89, 21.0001) log(gamma(big(21.0001))/gamma(big(21.0001)+big(13.89)))
@test SpecialFunctions.loggammadiv(big(13.89), big(21.0001)) log(gamma(big(21.0001))/gamma(big(21.0001)+big(13.89)))
@test SpecialFunctions.stirling_corr(11.99, 100.1) SpecialFunctions.stirling_error(11.99) + SpecialFunctions.stirling_error(100.1) - SpecialFunctions.stirling_error(11.99 + 100.1)
end

Expand Down
18 changes: 18 additions & 0 deletions test/gamma.jl
Original file line number Diff line number Diff line change
Expand Up @@ -274,6 +274,24 @@ end
@test logabsbeta(1e8, 0.5)[1] log(0.00017724538531210809) rtol=1e-14
end

@testset "BigFloat" begin
@test beta(big(3), big(5)) inv(big(105))
@test beta(big(3//2), big(7//2)) 5 * big(π) / 128
@test beta(big(1e4), big(3//2)) 8.86193693673874630607029e-7 rtol=1e-14
@test beta(big(1e8), big(1//2)) 0.00017724538531210809 rtol=1e-14

@test logbeta(big(5), big(4)) log(beta(big(5), big(4)))
@test logbeta(big(5.0), big(4)) log(beta(big(5), big(4)))
@test logbeta(big(1e4), big(3//2)) log(8.86193693673874630607029e-7) rtol=1e-14
@test logbeta(big(1e8), big(1//2)) log(0.00017724538531210809) rtol=1e-14

@test logabsbeta(-big(2), big(2))[1] -log(big(2))
@test logabsbeta(-big(2.0), big(2))[1] -log(big(2))
@test logabsbeta(-big(2), big(2//1))[1] -log(big(2))
@test logabsbeta(big(1e4), big(3//2))[1] log(8.86193693673874630607029e-7) rtol=1e-14
@test logabsbeta(big(1e8), big(1//2))[1] log(0.00017724538531210809) rtol=1e-14
end

@test beta(-1/2, 3) beta(-1/2 + 0im, 3 + 0im) -16/3
@test logabsbeta(-1/2, 3)[1] log(16/3)
@test beta(Float32(5), Float32(4)) == beta(Float32(4), Float32(5))
Expand Down

0 comments on commit 66eb6c7

Please sign in to comment.