Skip to content

Commit

Permalink
Avoid an infinite loop in beta_inc for NaN input (#445)
Browse files Browse the repository at this point in the history
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`.
  • Loading branch information
ararslan authored Jun 23, 2023
1 parent ae35d10 commit 99aca9a
Show file tree
Hide file tree
Showing 2 changed files with 5 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 @@ -754,7 +754,9 @@ function _beta_inc(a::Float64, b::Float64, x::Float64, y::Float64=1-x)
end
end

if x == 0.0
if isnan(x) || isnan(y) || isnan(a) || isnan(b)
return (NaN, NaN)
elseif x == 0.0
return (0.0, 1.0)
elseif y == 0.0
return (1.0, 0.0)
Expand Down
2 changes: 2 additions & 0 deletions test/beta_inc.jl
Original file line number Diff line number Diff line change
Expand Up @@ -304,6 +304,8 @@ end
# See https://github.com/JuliaStats/StatsFuns.jl/issues/133#issuecomment-1069602721
y = 2.0e-280
@test beta_inc(2.0, 1.0, beta_inc_inv(2.0, 1.0, y, 1.0)[1])[1] y
# See https://github.com/JuliaStats/GLM.jl/issues/538#issuecomment-1603447448
@test isequal(beta_inc(6.0, 112.5, NaN), (NaN, NaN))
end

@testset "StatsFuns#145" begin
Expand Down

0 comments on commit 99aca9a

Please sign in to comment.