Skip to content

Commit d8727a0

Browse files
Sumegh-gitsimonbyrne
authored andcommitted
Added Float16 fix in bessel.jl (#144)
1 parent 02a173f commit d8727a0

File tree

2 files changed

+28
-11
lines changed

2 files changed

+28
-11
lines changed

src/bessel.jl

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -131,8 +131,9 @@ for afn in (:airyai, :airyaiprime, :airybi, :airybiprime,
131131
:airyaix, :airyaiprimex, :airybix, :airybiprimex)
132132
@eval begin
133133
$afn(z::Complex) = $afn(float(z))
134-
$afn(z::Complex{<:AbstractFloat}) = throw(MethodError($afn,(z,)))
134+
$afn(z::Complex{Float16}) = Complex{Float16}($afn(Complex{Float32}(z)))
135135
$afn(z::Complex{Float32}) = Complex{Float32}($afn(Complex{Float64}(z)))
136+
$afn(z::Complex{<:AbstractFloat}) = throw(MethodError($afn,(z,)))
136137
end
137138
if afn in (:airyaix, :airyaiprimex)
138139
@eval $afn(x::Real) = x < 0 ? throw(DomainError(x, "`x` must be nonnegative.")) : real($afn(complex(float(x))))
@@ -158,11 +159,13 @@ for jy in ("j","y"), nu in (0,1)
158159
@eval begin
159160
$bjynu(x::Float64) = nan_dom_err(ccall(($jynu,libm), Float64, (Float64,), x), x)
160161
$bjynu(x::Float32) = nan_dom_err(ccall(($jynuf,libm), Float32, (Float32,), x), x)
162+
$bjynu(x::Float16) = Float16($bjynu(Float32(x)))
161163
end
162164
else
163165
@eval begin
164166
$bjynu(x::Float64) = ccall(($jynu,libm), Float64, (Float64,), x)
165167
$bjynu(x::Float32) = ccall(($jynuf,libm), Float32, (Float32,), x)
168+
$bjynu(x::Float16) = Float16($bjynu(Float32(x)))
166169
end
167170
end
168171
@eval begin
@@ -499,8 +502,9 @@ for f in ("i", "ix", "j", "jx", "k", "kx", "y", "yx")
499502
Tf = promote_type(float(typeof(nu)),float(typeof(real(z))))
500503
$bfn(Tf(nu), Complex{Tf}(z))
501504
end
502-
$bfn(k::T, z::Complex{T}) where {T<:AbstractFloat} = throw(MethodError($bfn,(k,z)))
505+
$bfn(nu::Float16, x::Complex{Float16}) = Complex{Float16}($bfn(Float32(nu), Complex{Float32}(x)))
503506
$bfn(nu::Float32, x::Complex{Float32}) = Complex{Float32}($bfn(Float64(nu), Complex{Float64}(x)))
507+
$bfn(k::T, z::Complex{T}) where {T<:AbstractFloat} = throw(MethodError($bfn,(k,z)))
504508
end
505509
end
506510

@@ -515,9 +519,9 @@ for bfn in (:besselh, :besselhx)
515519
Tf = promote_type(float(typeof(nu)),float(typeof(real(z))))
516520
$bfn(Tf(nu), k, Complex{Tf}(z))
517521
end
518-
519-
$bfn(nu::T, k::Integer, z::Complex{T}) where {T<:AbstractFloat} = throw(MethodError($bfn,(nu,k,z)))
522+
$bfn(nu::Float16, k::Integer, x::Complex{Float16}) = Complex{Float16}($bfn(Float32(nu), k, Complex{Float32}(x)))
520523
$bfn(nu::Float32, k::Integer, x::Complex{Float32}) = Complex{Float32}($bfn(Float64(nu), k, Complex{Float64}(x)))
524+
$bfn(nu::T, k::Integer, z::Complex{T}) where {T<:AbstractFloat} = throw(MethodError($bfn,(nu,k,z)))
521525
end
522526
end
523527

test/runtests.jl

Lines changed: 20 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ end
9393
@test_throws MethodError ellipe(BigFloat(-1))
9494
@test_throws DomainError ellipe(Float16(2.0))
9595
@test_throws DomainError ellipe(Float32(2.5))
96-
end
96+
end
9797
@testset "sine and cosine integrals" begin
9898
# Computed via wolframalpha.com: SinIntegral[SetPrecision[Table[x,{x, 1,20,1}],20]] and CosIntegral[SetPrecision[Table[x,{x, 1,20,1}],20]]
9999
sinintvals = [0.9460830703671830149, 1.605412976802694849, 1.848652527999468256, 1.75820313894905306, 1.54993124494467414, 1.4246875512805065, 1.4545966142480936, 1.5741868217069421, 1.665040075829602, 1.658347594218874, 1.578306806945727416, 1.504971241526373371, 1.499361722862824564, 1.556211050077665054, 1.618194443708368739, 1.631302268270032886, 1.590136415870701122, 1.536608096861185462, 1.518630031769363932, 1.548241701043439840]
@@ -135,13 +135,13 @@ end
135135
@test_throws AmosException airyai(200im)
136136
@test_throws AmosException airybi(200)
137137

138-
for T in [Float32, Float64, Complex{Float32},Complex{Float64}]
138+
for T in [Float16, Float32, Float64,Complex{Float16}, Complex{Float32},Complex{Float64}]
139139
@test airyai(T(1.8)) 0.0470362168668458052247
140140
@test airyaiprime(T(1.8)) -0.0685247801186109345638
141141
@test airybi(T(1.8)) 2.595869356743906290060
142142
@test airybiprime(T(1.8)) 2.98554005084659907283
143143
end
144-
for T in [Complex{Float32}, Complex{Float64}]
144+
for T in [Complex{Float16}, Complex{Float32}, Complex{Float64}]
145145
z = convert(T,1.8 + 1.0im)
146146
@test airyaix(z) airyai(z) * exp(2/3 * z * sqrt(z))
147147
@test airyaiprimex(z) airyaiprime(z) * exp(2/3 * z * sqrt(z))
@@ -172,6 +172,8 @@ end
172172
@testset "$z, $o" for (z, o, f) in bessel_funcs
173173
@test z(Float32(2.0)) z(Float64(2.0))
174174
@test o(Float32(2.0)) o(Float64(2.0))
175+
@test z(Float16(2.0)) z(Float64(2.0))
176+
@test o(Float16(2.0)) o(Float64(2.0))
175177
@test z(2) z(2.0)
176178
@test o(2) o(2.0)
177179
@test z(2.0 + im) f(0, 2.0 + im)
@@ -187,6 +189,8 @@ end
187189
true_h133 = 0.30906272225525164362 - 0.53854161610503161800im
188190
@test besselh(3,1,3) true_h133
189191
@test besselh(-3,1,3) -true_h133
192+
@test besselh(Float32(3),1,Float32(3)) true_h133
193+
@test besselh(Float16(3),1,Float16(3)) true_h133
190194
@test besselh(3,2,3) conj(true_h133)
191195
@test besselh(-3,2,3) -conj(true_h133)
192196
@testset "Error throwing" begin
@@ -204,12 +208,15 @@ end
204208
@test besseli(3,-3) -true_i33
205209
@test besseli(-3,-3) -true_i33
206210
@test besseli(Float32(-3),Complex{Float32}(-3,0)) -true_i33
211+
@test besseli(Float16(-3),Complex{Float16}(-3,0)) -true_i33
207212
true_im3p1_3 = 0.84371226532586351965
208213
@test besseli(-3.1,3) true_im3p1_3
209214
for i in [-5 -3 -1 1 3 5]
210215
@test besseli(i,0) == 0.0
211216
@test besseli(i,Float32(0)) == 0
212217
@test besseli(i,Complex{Float32}(0)) == 0
218+
@test besseli(i,Float16(0)) == 0
219+
@test besseli(i,Complex{Float16}(0)) == 0
213220
end
214221
@testset "Error throwing" begin
215222
@test_throws AmosException besseli(1,1000)
@@ -225,7 +232,7 @@ end
225232
for i in [-5 -3 -1 1 3 5]
226233
@test besselj(i,0) == 0
227234
@test besselj(i,Float32(0)) == 0
228-
@test besselj(i,Complex{Float32}(0)) == 0
235+
@test besselj(i,Complex{Float32}(0)) == 0.0
229236
end
230237

231238
j33 = besselj(3,3.)
@@ -257,6 +264,7 @@ end
257264

258265
true_jm3p1_3 = -0.45024252862270713882
259266
@test besselj(-3.1,3) true_jm3p1_3
267+
@test besselj(Float16(-3.1),Float16(3)) true_jm3p1_3
260268

261269
@testset "Error throwing" begin
262270
@test_throws DomainError besselj(0.1, -0.4)
@@ -268,6 +276,8 @@ end
268276
@testset "besselk" begin
269277
true_k33 = 0.12217037575718356792
270278
@test besselk(3,3) true_k33
279+
@test besselk(Float32(3),Float32(3)) true_k33
280+
@test besselk(Float16(3),Float16(3)) true_k33
271281
@test besselk(-3,3) true_k33
272282
true_k3m3 = -0.1221703757571835679 - 3.0151549516807985776im
273283
@test besselk(3,complex(-3)) true_k3m3
@@ -311,7 +321,7 @@ end
311321
end
312322

313323
@testset "besselhx" begin
314-
for elty in [Complex{Float32},Complex{Float64}]
324+
for elty in [Complex{Float16},Complex{Float32},Complex{Float64}]
315325
z = convert(elty, 1.0 + 1.9im)
316326
@test besselhx(1.0, 1, z) convert(elty,-0.5949634147786144 - 0.18451272807835967im)
317327
@test besselhx(Float32(1.0), 1, z) convert(elty,-0.5949634147786144 - 0.18451272807835967im)
@@ -341,6 +351,10 @@ end
341351
@test besselix(i,Float32(0)) == 0
342352
@test besseljx(i,Complex{Float32}(0)) == 0
343353
@test besselix(i,Complex{Float32}(0)) == 0
354+
@test besseljx(i,Float16(0)) == 0
355+
@test besselix(i,Float16(0)) == 0
356+
@test besseljx(i,Complex{Float16}(0)) == 0
357+
@test besselix(i,Complex{Float16}(0)) == 0
344358
end
345359
@testset "Error throwing" begin
346360
@test_throws AmosException hankelh1x(1, 0)
@@ -358,6 +372,7 @@ end
358372
@testset "$f" for f in (besselj,bessely,besseli,besselk,hankelh1,hankelh2)
359373
@test f(0,1) f(0,Complex{Float64}(1))
360374
@test f(0,1) f(0,Complex{Float32}(1))
375+
@test f(0,1) f(0,Complex{Float16}(1))
361376
end
362377
end
363378
end
@@ -596,7 +611,6 @@ end
596611
end
597612

598613
@test sprint(showerror, AmosException(1)) == "AmosException with id 1: input error."
599-
600614
# Used to check method existence below
601615
struct NotAFloat <: AbstractFloat
602616
end
@@ -664,7 +678,6 @@ end
664678
@test lgamma(-Inf*im) === -Inf - Inf*im
665679
@test lgamma(Inf + Inf*im) === lgamma(NaN + 0im) === lgamma(NaN*im) === NaN + NaN*im
666680
end
667-
668681
@testset "Other float types" begin
669682
let x = one(Float16)
670683
@test gamma(x) one(Float16)

0 commit comments

Comments
 (0)