Skip to content

Commit 99aca9a

Browse files
authored
Avoid an infinite loop in beta_inc for NaN input (#445)
This follows suit with `gamma_inc`, which returns `NaN` if either of its two arguments are `NaN`, by returning `NaN` if any of the four arguments are `NaN`.
1 parent ae35d10 commit 99aca9a

File tree

2 files changed

+5
-1
lines changed

2 files changed

+5
-1
lines changed

src/beta_inc.jl

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -754,7 +754,9 @@ function _beta_inc(a::Float64, b::Float64, x::Float64, y::Float64=1-x)
754754
end
755755
end
756756

757-
if x == 0.0
757+
if isnan(x) || isnan(y) || isnan(a) || isnan(b)
758+
return (NaN, NaN)
759+
elseif x == 0.0
758760
return (0.0, 1.0)
759761
elseif y == 0.0
760762
return (1.0, 0.0)

test/beta_inc.jl

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -304,6 +304,8 @@ end
304304
# See https://github.com/JuliaStats/StatsFuns.jl/issues/133#issuecomment-1069602721
305305
y = 2.0e-280
306306
@test beta_inc(2.0, 1.0, beta_inc_inv(2.0, 1.0, y, 1.0)[1])[1] y
307+
# See https://github.com/JuliaStats/GLM.jl/issues/538#issuecomment-1603447448
308+
@test isequal(beta_inc(6.0, 112.5, NaN), (NaN, NaN))
307309
end
308310

309311
@testset "StatsFuns#145" begin

0 commit comments

Comments
 (0)