Skip to content

Commit 57275dd

Browse files
committed
fix float16 for nu arguments
1 parent f1ff167 commit 57275dd

11 files changed

+22
-21
lines changed

src/besseli.jl

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -164,9 +164,10 @@ end
164164
165165
Modified Bessel function of the second kind of order nu, ``I_{nu}(x)``.
166166
"""
167+
# perhaps have two besseli(nu::Real, x::Real) and besseli(nu::AbstractRange, x::Real)
167168
besseli(nu, x::Real) = _besseli(nu, float(x))
168169

169-
_besseli(nu, x::Float16) = Float16(_besseli(nu, Float32(x)))
170+
_besseli(nu::Union{Int16, Float16}, x::Union{Int16, Float16}) = Float16(_besseli(Float32(nu), Float32(x)))
170171

171172
function _besseli(nu::T, x::T) where T <: Union{Float32, Float64}
172173
isinteger(nu) && return _besseli(Int(nu), x)
@@ -239,7 +240,7 @@ Nu must be real.
239240
"""
240241
besselix(nu::Real, x::Real) = _besselix(nu, float(x))
241242

242-
_besselix(nu, x::Float16) = Float16(_besselix(nu, Float32(x)))
243+
_besselix(nu::Union{Int16, Float16}, x::Union{Int16, Float16}) = Float16(_besselix(Float32(nu), Float32(x)))
243244

244245
function _besselix(nu, x::T) where T <: Union{Float32, Float64}
245246
iszero(nu) && return besseli0x(x)

src/besselj.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -208,7 +208,7 @@ nu and x must be real where nu and x can be positive or negative.
208208
"""
209209
besselj(nu, x::Real) = _besselj(nu, float(x))
210210

211-
_besselj(nu, x::Float16) = Float16(_besselj(nu, Float32(x)))
211+
_besselj(nu::Union{Int16, Float16}, x::Union{Int16, Float16}) = Float16(_besselj(Float32(nu), Float32(x)))
212212

213213
function _besselj(nu::T, x::T) where T <: Union{Float32, Float64}
214214
isinteger(nu) && return _besselj(Int(nu), x)

src/besselk.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -189,7 +189,7 @@ Modified Bessel function of the second kind of order nu, ``K_{nu}(x)``.
189189
"""
190190
besselk(nu, x::Real) = _besselk(nu, float(x))
191191

192-
_besselk(nu, x::Float16) = Float16(_besselk(nu, Float32(x)))
192+
_besselk(nu::Union{Int16, Float16}, x::Union{Int16, Float16}) = Float16(_besselk(Float32(nu), Float32(x)))
193193

194194
function _besselk(nu::T, x::T) where T <: Union{Float32, Float64}
195195
isinteger(nu) && return _besselk(Int(nu), x)

src/bessely.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -245,7 +245,7 @@ nu and x must be real where nu and x can be positive or negative.
245245
"""
246246
bessely(nu, x::Real) = _bessely(nu, float(x))
247247

248-
_bessely(nu, x::Float16) = Float16(_bessely(nu, Float32(x)))
248+
_bessely(nu::Union{Int16, Float16}, x::Union{Int16, Float16}) = Float16(_bessely(Float32(nu), Float32(x)))
249249

250250
function _bessely(nu::T, x::T) where T <: Union{Float32, Float64}
251251
isnan(nu) || isnan(x) && return NaN

src/modifiedsphericalbessel.jl

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ Computes `k_{ν}(x)`, the modified second-kind spherical Bessel function, and of
1919
"""
2020
sphericalbesselk(nu::Real, x::Real) = _sphericalbesselk(nu, float(x))
2121

22-
_sphericalbesselk(nu, x::Float16) = Float16(_sphericalbesselk(nu, Float32(x)))
22+
_sphericalbesselk(nu::Union{Int16, Float16}, x::Union{Int16, Float16}) = Float16(_sphericalbesselk(Float32(nu), Float32(x)))
2323

2424
function _sphericalbesselk(nu, x::T) where T <: Union{Float32, Float64}
2525
if ~isfinite(x)
@@ -39,7 +39,7 @@ function _sphericalbesselk(nu, x::T) where T <: Union{Float32, Float64}
3939
_nu = ifelse(nu<zero(nu), -one(nu)-nu, nu)
4040
return sphericalbesselk_int(Int(_nu), x)
4141
else
42-
return inv(SQPIO2(T)*sqrt(x))*besselk(nu+1/2, x)
42+
return inv(SQPIO2(T)*sqrt(x))*besselk(nu+T(1)/2, x)
4343
end
4444
end
4545
sphericalbesselk_cutoff(nu) = nu < 41.5
@@ -65,15 +65,15 @@ Computes `i_{ν}(x)`, the modified first-kind spherical Bessel function.
6565
"""
6666
sphericalbesseli(nu::Real, x::Real) = _sphericalbesseli(nu, float(x))
6767

68-
_sphericalbesseli(nu, x::Float16) = Float16(_sphericalbesseli(nu, Float32(x)))
68+
_sphericalbesseli(nu::Union{Int16, Float16}, x::Union{Int16, Float16}) = Float16(_sphericalbesseli(Float32(nu), Float32(x)))
6969

7070
function _sphericalbesseli(nu, x::T) where T <: Union{Float32, Float64}
7171
isinf(x) && return x
7272
x < zero(x) && throw(DomainError(x, "Complex result returned for real arguments. Complex arguments are currently not supported"))
7373

7474
sphericalbesselj_small_args_cutoff(nu, x::T) && return sphericalbesseli_small_args(nu, x)
7575
isinteger(nu) && return _sphericalbesseli_small_orders(Int(nu), x)
76-
return SQPIO2(T)*besseli(nu+1/2, x) / sqrt(x)
76+
return SQPIO2(T)*besseli(nu+T(1)/2, x) / sqrt(x)
7777
end
7878

7979
function _sphericalbesseli_small_orders(nu::Integer, x::T) where T
@@ -86,7 +86,7 @@ function _sphericalbesseli_small_orders(nu::Integer, x::T) where T
8686
nu_abs == 0 && return sinhx / x
8787
nu_abs == 1 && return (x*coshx - sinhx) / x2
8888
nu_abs == 2 && return (x2*sinhx + 3*(sinhx - x*coshx)) / (x2*x)
89-
return SQPIO2(T)*besseli(nu+1/2, x) / sqrt(x)
89+
return SQPIO2(T)*besseli(nu+T(1)/2, x) / sqrt(x)
9090
end
9191

9292
function sphericalbesseli_small_args(nu, x::T) where T

src/sphericalbessel.jl

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,8 @@ sphericalbesselj(nu::Real, x::Real) = _sphericalbesselj(nu, float(x))
2626

2727
_sphericalbesselj(nu, x::Float32) = Float32(_sphericalbesselj(nu, Float64(x)))
2828

29-
_sphericalbesselj(nu, x::Float16) = Float16(_sphericalbesselj(nu, Float32(x)))
30-
29+
_sphericalbesselj(nu::Union{Int16, Float16}, x::Union{Int16, Float16}) = Float16(_sphericalbesselj(Float32(nu), Float32(x)))
30+
3131
function _sphericalbesselj(nu::Real, x::T) where T <: Float64
3232
x < zero(T) && return throw(DomainError(x, "Complex result returned for real arguments. Complex arguments are currently not supported"))
3333
if ~isfinite(x)
@@ -114,7 +114,7 @@ sphericalbessely(nu::Real, x::Real) = _sphericalbessely(nu, float(x))
114114

115115
_sphericalbessely(nu, x::Float32) = Float32(_sphericalbessely(nu, Float64(x)))
116116

117-
_sphericalbessely(nu, x::Float16) = Float16(_sphericalbessely(nu, Float32(x)))
117+
_sphericalbessely(nu::Union{Int16, Float16}, x::Union{Int16, Float16}) = Float16(_sphericalbessely(Float32(nu), Float32(x)))
118118

119119
function _sphericalbessely(nu::Real, x::T) where T <: Float64
120120
x < zero(T) && return throw(DomainError(x, "Complex result returned for real arguments. Complex arguments are currently not supported"))

test/besseli_test.jl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ end
8080
#Float 64
8181
m = 0:1:200; x = 0.1:0.5:150.0
8282
@test besseli(10, 1.0) isa Float64
83-
@test besseli(10, Float16(1.0)) isa Float16
83+
@test besseli(Int16(10), Float16(1.0)) isa Float16
8484

8585
@test besseli(2, 80.0) isa Float64
8686
@test besseli(112, 80.0) isa Float64
@@ -92,7 +92,7 @@ t = [besseli(m, x) for m in m, x in x]
9292
t = [besselix(m, x) for m in m, x in x]
9393
@test t[10] isa Float64
9494
@test t [SpecialFunctions.besselix(m, x) for m in m, x in x]
95-
@test besselix(10, Float16(1.0)) isa Float16
95+
@test besselix(Int16(10), Float16(1.0)) isa Float16
9696

9797
## Tests for besseli
9898

test/besselj_test.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,7 @@ end
121121
@test besselj(0.5:1:10.5, 40.0) SpecialFunctions.besselj.(0.5:1:10.5, 40.0) rtol=1e-11
122122

123123
# test Float16 and Float32
124-
@test besselj(10, Float16(1.0)) isa Float16
124+
@test besselj(Int16(10), Float16(1.0)) isa Float16
125125
@test besselj(10.2f0, 1.0f0) isa Float32
126126

127127
## test large arguments

test/besselk_test.jl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -131,8 +131,8 @@ end
131131
@test besselk(0.5:1:10.5, 12.0) SpecialFunctions.besselk.(0.5:1:10.5, 12.0) rtol=1e-13
132132

133133
# test Float16
134-
@test besselk(10, Float16(1.0)) isa Float16
135-
@test besselkx(10, Float16(1.0)) isa Float16
134+
@test besselk(Int16(10), Float16(1.0)) isa Float16
135+
@test besselkx(Int16(10), Float16(1.0)) isa Float16
136136

137137
# test Inf
138138
@test iszero(besselk(2, Inf))

test/bessely_test.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,7 @@ end
101101
@test bessely(0.5:1:10.5, 40.0) SpecialFunctions.bessely.(0.5:1:10.5, 40.0) rtol=1e-11
102102

103103
# test Float16
104-
@test bessely(10, Float16(1.0)) isa Float16
104+
@test bessely(Int16(10), Float16(1.0)) isa Float16
105105
@test bessely(10.2f0, 1.0f0) isa Float32
106106

107107
# test limits for small arguments see https://github.com/JuliaMath/Bessels.jl/issues/35

0 commit comments

Comments
 (0)