Skip to content

Commit 49e3964

Browse files
committed
Make sure to compute 1-x after converting to Float64 in beta_inc
and beta_inc_inv to avoid exception for some values of x.
1 parent c4b0b83 commit 49e3964

File tree

2 files changed

+23
-8
lines changed

2 files changed

+23
-8
lines changed

src/beta_inc.jl

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -725,11 +725,14 @@ External links: [DLMF](https://dlmf.nist.gov/8.17.1), [Wikipedia](https://en.wik
725725
726726
See also: [`beta_inc_inv`](@ref)
727727
"""
728-
function beta_inc(a::Real, b::Real, x::Real, y::Real=1-x)
728+
function beta_inc(a::Real, b::Real, x::Real)
729+
return _beta_inc(map(float, promote(a, b, x))...)
730+
end
731+
function beta_inc(a::Real, b::Real, x::Real, y::Real)
729732
return _beta_inc(map(float, promote(a, b, x, y))...)
730733
end
731734

732-
function _beta_inc(a::Float64, b::Float64, x::Float64, y::Float64)
735+
function _beta_inc(a::Float64, b::Float64, x::Float64, y::Float64=1-x)
733736
p = 0.0
734737
q = 0.0
735738
# lambda = a - (a+b)*x
@@ -883,6 +886,10 @@ function _beta_inc(a::Float64, b::Float64, x::Float64, y::Float64)
883886
return ind ? (q, p) : (p, q)
884887
end
885888

889+
function _beta_inc(a::T, b::T, x::T) where {T<:Union{Float16, Float32}}
890+
p, q = _beta_inc(Float64(a), Float64(b), Float64(x))
891+
T(p), T(q)
892+
end
886893
function _beta_inc(a::T, b::T, x::T, y::T) where {T<:Union{Float16, Float32}}
887894
p, q = _beta_inc(Float64(a), Float64(b), Float64(x), Float64(y))
888895
T(p), T(q)
@@ -901,11 +908,14 @@ of the regularized incomplete beta function ``I_{x}(a, b)``.
901908
902909
See also: [`beta_inc`](@ref)
903910
"""
904-
function beta_inc_inv(a::Real, b::Real, p::Real, q::Real=1-p)
911+
function beta_inc_inv(a::Real, b::Real, p::Real)
912+
return _beta_inc_inv(map(float, promote(a, b, p))...)
913+
end
914+
function beta_inc_inv(a::Real, b::Real, p::Real, q::Real)
905915
return _beta_inc_inv(map(float, promote(a, b, p, q))...)
906916
end
907917

908-
function _beta_inc_inv(a::Float64, b::Float64, p::Float64, q::Float64)
918+
function _beta_inc_inv(a::Float64, b::Float64, p::Float64, q::Float64=1-p)
909919
#change tail if necessary
910920
if p > 0.5
911921
y, x = _beta_inc_inv(b, a, q, p)
@@ -1002,6 +1012,10 @@ function _beta_inc_inv(a::Float64, b::Float64, p::Float64, q::Float64)
10021012
end
10031013
end
10041014

1015+
function _beta_inc_inv(a::T, b::T, p::T) where {T<:Union{Float16, Float32}}
1016+
x, y = _beta_inc_inv(Float64(a), Float64(b), Float64(p))
1017+
T(x), T(y)
1018+
end
10051019
function _beta_inc_inv(a::T, b::T, p::T, q::T) where {T<:Union{Float16, Float32}}
10061020
x, y = _beta_inc_inv(Float64(a), Float64(b), Float64(p), Float64(q))
10071021
T(x), T(y)

test/beta_inc.jl

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -212,10 +212,11 @@
212212

213213
# test promotions and return types
214214
for T in (Float16, Float32, Float64)
215-
x = rand(T)
216-
for a in (1, randexp(T)), b in (1, randexp(T))
217-
@test beta_inc(a, b, x) isa Tuple{T,T}
218-
@test beta_inc(a, b, x, 1 - x) === beta_inc(a, b, x)
215+
for x in (T(0.1), rand(T))
216+
for a in (1, randexp(T)), b in (1, randexp(T))
217+
@test beta_inc(a, b, x) isa Tuple{T,T}
218+
@test T.(beta_inc(a, b, x, 1 - Float64(x))) === beta_inc(a, b, x)
219+
end
219220
end
220221
end
221222
a = randexp()

0 commit comments

Comments
 (0)