Skip to content

Commit dab6277

Browse files
committed
add nan, inf, zero tests
1 parent a90d68a commit dab6277

File tree

2 files changed

+37
-4
lines changed

2 files changed

+37
-4
lines changed

src/sphericalbessel.jl

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,9 +27,11 @@ sphericalbesselj(nu::Real, x::Real) = _sphericalbesselj(nu, float(x))
2727
_sphericalbesselj(nu, x::Float16) = Float16(_sphericalbesselj(nu, Float32(x)))
2828

2929
function _sphericalbesselj(nu::Real, x::T) where T <: Union{Float32, Float64}
30-
isnan(nu) || isnan(x) && return NaN
3130
x < zero(T) && return throw(DomainError(x, "Complex result returned for real arguments. Complex arguments are currently not supported"))
32-
31+
if ~isfinite(x)
32+
isnan(x) && return x
33+
isinf(x) && return zero(x)
34+
end
3335
return nu < zero(T) ? sphericalbesselj_generic(nu, x) : sphericalbesselj_positive_args(nu, x)
3436
end
3537

@@ -56,6 +58,7 @@ end
5658
sphericalbesselj_small_args_cutoff(nu, x::T) where T = x^2 / (4*nu + 110) < eps(T)
5759

5860
function sphericalbesselj_small_args(nu, x::T) where T
61+
iszero(x) && return iszero(nu) ? one(T) : zero(T)
5962
x2 = x^2 / 4
6063
coef = evalpoly(x2, (1, -inv(T(3)/2 + nu), -inv(5 + nu), -inv(T(21)/2 + nu), -inv(18 + nu)))
6164
a = sqrt(T(pi)/2) / (gamma(T(3)/2 + nu) * 2^(nu + T(1)/2))
@@ -110,8 +113,11 @@ sphericalbessely(nu::Real, x::Real) = _sphericalbessely(nu, float(x))
110113
_sphericalbessely(nu, x::Float16) = Float16(_sphericalbessely(nu, Float32(x)))
111114

112115
function _sphericalbessely(nu::Real, x::T) where T <: Union{Float32, Float64}
113-
isnan(nu) || isnan(x) && return NaN
114116
x < zero(T) && return throw(DomainError(x, "Complex result returned for real arguments. Complex arguments are currently not supported"))
117+
if ~isfinite(x)
118+
isnan(x) && return x
119+
isinf(x) && return zero(x)
120+
end
115121
return nu < zero(T) ? sphericalbessely_generic(nu, x) : sphericalbessely_positive_args(nu, x)
116122
end
117123

@@ -149,5 +155,8 @@ function sphericalbessely_forward_recurrence(nu::Integer, x::T) where T
149155
sY0, sY1 = sY1, muladd((2*nu_start + 1)*xinv, sY1, -sY0)
150156
nu_start += 1
151157
end
152-
return sY0, sY1
158+
# need to check if NaN resulted during loop
159+
# this could happen if x is very small and nu is large which eventually results in overflow (-> -Inf)
160+
# NaN inputs were checked in top level function so if sY0 is NaN we should return -infinity
161+
return isnan(sY0) ? (-Inf, -Inf) : (sY0, sY1)
153162
end

test/sphericalbessel_test.jl

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,30 @@ x = 1e-15
99
@test Bessels.sphericalbessely(5.5, x) SpecialFunctions.sphericalbessely(5.5, x)
1010
@test Bessels.sphericalbessely(10, x) SpecialFunctions.sphericalbessely(10, x)
1111

12+
# test zero
13+
@test isone(Bessels.sphericalbesselj(0, 0.0))
14+
@test iszero(Bessels.sphericalbesselj(3, 0.0))
15+
@test iszero(Bessels.sphericalbesselj(10.4, 0.0))
16+
@test iszero(Bessels.sphericalbesselj(100.6, 0.0))
17+
18+
@test Bessels.sphericalbessely(0, 0.0) == -Inf
19+
@test Bessels.sphericalbessely(1.8, 0.0) == -Inf
20+
@test Bessels.sphericalbessely(10, 0.0) == -Inf
21+
@test Bessels.sphericalbessely(290, 0.0) == -Inf
22+
23+
# test Inf
24+
@test iszero(Bessels.sphericalbesselj(1, Inf))
25+
@test iszero(Bessels.sphericalbesselj(10.2, Inf))
26+
@test iszero(Bessels.sphericalbessely(3, Inf))
27+
@test iszero(Bessels.sphericalbessely(4.5, Inf))
28+
29+
# test NaN
30+
@test isnan(Bessels.sphericalbesselj(1.4, NaN))
31+
@test isnan(Bessels.sphericalbesselj(4.0, NaN))
32+
@test isnan(Bessels.sphericalbessely(1.4, NaN))
33+
@test isnan(Bessels.sphericalbessely(4.0, NaN))
34+
35+
1236
for x in 0.5:1.0:100.0, v in [0, 1, 5.5, 8.2, 10]
1337
@test isapprox(Bessels.sphericalbesselj(v, x), SpecialFunctions.sphericalbesselj(v, x), rtol=1e-12)
1438
@test isapprox(Bessels.sphericalbessely(v, x), SpecialFunctions.sphericalbessely(v, x), rtol=1e-12)

0 commit comments

Comments
 (0)