Skip to content

Commit f80715a

Browse files
committed
Avoid NaN in beta_inc_inv when p=q=1.
(cherry picked from commit d7c7e8e)
1 parent be5edb6 commit f80715a

File tree

3 files changed

+10
-2
lines changed

3 files changed

+10
-2
lines changed

Project.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ OpenSpecFun_jll = "efe28fd5-8261-553b-a9e1-b2916fc3738e"
1313
ChainRulesCore = "0.9.44, 0.10, 1"
1414
ChainRulesTestUtils = "0.6.8, 0.7, 1"
1515
IrrationalConstants = "0.1"
16-
LogExpFunctions = "0.3"
16+
LogExpFunctions = "0.3.2"
1717
OpenLibm_jll = "0.7, 0.8"
1818
OpenSpecFun_jll = "0.5"
1919
julia = "1.3"

src/beta_inc.jl

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1004,7 +1004,10 @@ function _beta_inc_inv(a::Float64, b::Float64, p::Float64, q::Float64=1-p)
10041004
while true
10051005
p_approx = beta_inc(a, b, x)[1]
10061006
xin = x
1007-
p_approx = (p_approx - p)*min(floatmax(), exp(lb + r*log(xin) + t*log1p(-xin)))
1007+
p_approx = (p_approx - p)*min(
1008+
floatmax(),
1009+
exp(lb + LogExpFunctions.xlogy(r, xin) + LogExpFunctions.xlog1py(t, -xin))
1010+
)
10081011

10091012
if p_approx * p_approx_prev <= 0.0
10101013
prev = max(sq, fpu)

test/beta_inc.jl

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -294,4 +294,9 @@ end
294294
@test beta_inc_inv(a, b, p, 1 - p) === beta_inc_inv(a, b, p)
295295
end
296296
end
297+
298+
@testset "Avoid NaN when p=q=1" begin
299+
@test first(beta_inc_inv(1.0, 1.0, 1e-21)) 1e-21
300+
@test beta_inc_inv(1.0e30, 1.0, 0.49) == (1.0, 0.0)
301+
end
297302
end

0 commit comments

Comments
 (0)