Skip to content

Commit

Permalink
Merge pull request #1432 from AayushSabharwal/as/nan-handling
Browse files Browse the repository at this point in the history
fix: fix `NaN` handling in `ssqrt`, `scbrt`, `slog`
  • Loading branch information
ChrisRackauckas authored Feb 8, 2025
2 parents f8108e4 + b6e060a commit 5f51b84
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 0 deletions.
3 changes: 3 additions & 0 deletions src/solver/solve_helpers.jl
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ function ssqrt(n)
n = unwrap(n)

if n isa Real
isnan(n) && return n
n > 0 && return sqrt(n)
return sqrt(complex(n))
end
Expand All @@ -43,6 +44,7 @@ function scbrt(n)
n = unwrap(n)

if n isa Real
isnan(n) && return n
return cbrt(n)
end

Expand All @@ -61,6 +63,7 @@ function slog(n)
n = unwrap(n)

if n isa Real
isnan(n) && return n
return n > 0 ? log(n) : log(complex(n))
end

Expand Down
7 changes: 7 additions & 0 deletions test/solver.jl
Original file line number Diff line number Diff line change
Expand Up @@ -633,3 +633,10 @@ using LambertW
#product
@test correctAns(symbolic_solve((x^2-4)*(x+1)~0,x),[-2.0,-1.0,2.0])
end

@testset "`NaN` handling in `$fn`" for fn in [ssqrt, slog, scbrt]
x = fn(NaN)
@test x isa Real && isnan(x)
x = fn(NaN + NaN * im)
@test x isa Complex && isnan(x)
end

0 comments on commit 5f51b84

Please sign in to comment.