Skip to content

Commit 8d3a42b

Browse files
committed
Merge pull request #11277 from JuliaLang/ksh/mathtests
Better error messages for airy and tests for bessel and gamma
2 parents a65dfe5 + 89f2138 commit 8d3a42b

File tree

2 files changed

+36
-5
lines changed

2 files changed

+36
-5
lines changed

base/special/bessel.jl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ function airy(k::Int, z::Complex128)
6868
elseif k == 2 || k == 3
6969
return _biry(z, id, Int32(1))
7070
else
71-
error("invalid argument")
71+
throw(ArgumentError("k must be between 0 and 3"))
7272
end
7373
end
7474

@@ -98,7 +98,7 @@ function airyx(k::Int, z::Complex128)
9898
elseif k == 2 || k == 3
9999
return _biry(z, id, Int32(2))
100100
else
101-
error("invalid argument")
101+
throw(ArgumentError("k must be between 0 and 3"))
102102
end
103103
end
104104

test/math.jl

Lines changed: 34 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -125,11 +125,27 @@ end
125125
@test_approx_eq airybiprime(1.8) 2.98554005084659907283
126126
@test_throws Base.Math.AmosException airy(200im)
127127
@test_throws Base.Math.AmosException airybi(200)
128+
@test_throws ArgumentError airy(5,one(Complex128))
128129
z = 1.8 + 1.0im
129130
@test_approx_eq airyx(0, z) airy(0, z) * exp(2/3 * z * sqrt(z))
130131
@test_approx_eq airyx(1, z) airy(1, z) * exp(2/3 * z * sqrt(z))
131132
@test_approx_eq airyx(2, z) airy(2, z) * exp(-abs(real(2/3 * z * sqrt(z))))
132133
@test_approx_eq airyx(3, z) airy(3, z) * exp(-abs(real(2/3 * z * sqrt(z))))
134+
@test_throws ArgumentError airyx(5,z)
135+
136+
# bessely0, bessely1, besselj0, besselj1
137+
@test_approx_eq besselj0(Float32(2.0)) besselj0(Float64(2.0))
138+
@test_approx_eq besselj1(Float32(2.0)) besselj1(Float64(2.0))
139+
@test_approx_eq bessely0(Float32(2.0)) bessely0(Float64(2.0))
140+
@test_approx_eq bessely1(Float32(2.0)) bessely1(Float64(2.0))
141+
@test_approx_eq besselj0(2) besselj0(2.0)
142+
@test_approx_eq besselj1(2) besselj1(2.0)
143+
@test_approx_eq bessely0(2) bessely0(2.0)
144+
@test_approx_eq bessely1(2) bessely1(2.0)
145+
@test_approx_eq besselj0(2.0 + im) besselj(0, 2.0 + im)
146+
@test_approx_eq besselj1(2.0 + im) besselj(1, 2.0 + im)
147+
@test_approx_eq bessely0(2.0 + im) bessely(0, 2.0 + im)
148+
@test_approx_eq bessely1(2.0 + im) bessely(1, 2.0 + im)
133149

134150
# besselh
135151
true_h133 = 0.30906272225525164362 - 0.53854161610503161800im
@@ -147,12 +163,15 @@ true_i33 = 0.95975362949600785698
147163
@test_approx_eq besseli(3,-3) -true_i33
148164
@test_approx_eq besseli(-3,-3) -true_i33
149165
@test_throws Base.Math.AmosException besseli(1,1000)
166+
@test_throws DomainError besseli(0.4,-1.0)
150167

151168
# besselj
152169
@test besselj(0,0) == 1
153170
for i = 1:5
154171
@test besselj(i,0) == 0
155172
@test besselj(-i,0) == 0
173+
@test besselj(-i,Float32(0)) == 0
174+
@test besselj(-i,Float32(0)) == 0
156175
end
157176

158177
j33 = besselj(3,3.)
@@ -190,11 +209,15 @@ true_k3m3 = -0.1221703757571835679 - 3.0151549516807985776im
190209
# bessely
191210
y33 = bessely(3,3.)
192211
@test bessely(3,3) == y33
212+
@test bessely(3.,3.) == y33
213+
@test_approx_eq bessely(3,Float32(3.)) y33
193214
@test_approx_eq bessely(-3,3) -y33
194215
@test_approx_eq y33 -0.53854161610503161800
195216
@test_throws DomainError bessely(3,-3)
196217
@test_approx_eq bessely(3,complex(-3)) 0.53854161610503161800 - 0.61812544451050328724im
197218
@test_throws Base.Math.AmosException bessely(200.5,0.1)
219+
@test_throws DomainError bessely(0.4,-1.0)
220+
@test_throws DomainError bessely(0.4,Float32(-1.0))
198221

199222
# issue #6653
200223
for f in (besselj,bessely,besseli,besselk,hankelh1,hankelh2)
@@ -218,6 +241,10 @@ end
218241
@test_throws Base.Math.AmosException besseljx(-1, 0)
219242
@test besselkx(1, 0) == Inf
220243
@test_throws Base.Math.AmosException besselyx(1, 0)
244+
@test_throws DomainError besselix(0.4,-1.0)
245+
@test_throws DomainError besseljx(0.4, -1.0)
246+
@test_throws DomainError besselkx(0.4,-1.0)
247+
@test_throws DomainError besselyx(0.4,-1.0)
221248

222249
# beta, lbeta
223250
@test_approx_eq beta(3/2,7/2) 5π/128
@@ -233,9 +260,11 @@ if Base.Math.libm == "libopenlibm"
233260
else
234261
@test_approx_eq gamma(Float64[1:25;]) gamma(1:25)
235262
end
236-
@test_approx_eq gamma(1/2) sqrt(π)
237-
@test_approx_eq gamma(-1/2) -2sqrt(π)
238-
@test_approx_eq lgamma(-1/2) log(abs(gamma(-1/2)))
263+
for elty in (Float32, Float64)
264+
@test_approx_eq gamma(convert(elty,1/2)) convert(elty,sqrt(π))
265+
@test_approx_eq gamma(convert(elty,-1/2)) convert(elty,-2sqrt(π))
266+
@test_approx_eq lgamma(convert(elty,-1/2)) convert(elty,log(abs(gamma(-1/2))))
267+
end
239268
@test_approx_eq lgamma(1.4+3.7im) -3.7094025330996841898 + 2.4568090502768651184im
240269
@test_approx_eq lgamma(1.4+3.7im) log(gamma(1.4+3.7im))
241270
@test_approx_eq lgamma(-4.2+0im) lgamma(-4.2)-pi*im
@@ -296,6 +325,7 @@ end
296325
@test_approx_eq zeta(0) -0.5
297326
@test_approx_eq zeta(2) pi^2/6
298327
@test_approx_eq zeta(4) pi^4/90
328+
@test_approx_eq zeta(one(Float32)) Float32(zeta(one(Float64)))
299329

300330
# quadgk
301331
@test_approx_eq quadgk(cos, 0,0.7,1)[1] sin(1)
@@ -353,6 +383,7 @@ end
353383
@test isa([polygamma(3,x) for x in [1.0]], Vector{Float64})
354384
@test 1e-13 > errc(zeta(2 + 1im, -1.1), zeta(2 + 1im, -1.1+0im))
355385
@test 1e-13 > errc(zeta(2 + 1im, -1.1), -1525.8095173321060982383023516086563741006869909580583246557 + 1719.4753293650912305811325486980742946107143330321249869576im)
386+
@test_approx_eq polygamma(3,5) polygamma(3,5.)
356387

357388
@test @evalpoly(2,3,4,5,6) == 3+2*(4+2*(5+2*6)) == @evalpoly(2+0im,3,4,5,6)
358389
@test let evalcounts=0

0 commit comments

Comments
 (0)