Skip to content

Commit 89ac601

Browse files
musmararslan
authored andcommitted
More descriptive DomainErrors (#41)
1 parent 2191e75 commit 89ac601

File tree

7 files changed

+33
-38
lines changed

7 files changed

+33
-38
lines changed

REQUIRE

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
11
julia 0.6
2+
Compat 0.30

src/SpecialFunctions.jl

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@ __precompile__()
22

33
module SpecialFunctions
44

5+
using Compat
6+
57
if isdefined(Base, :airyai) && VERSION < v"0.7.0-DEV.986" #22763
68
import Base: airyai, airyaix, airyaiprime, airyaiprimex,
79
airybi, airybix, airybiprime, airybiprimex,
@@ -64,13 +66,6 @@ else
6466
const openspecfun = "libopenspecfun"
6567
end
6668

67-
# Avoids a deprecation warning for the 0-arg case on 0.7
68-
if VERSION < v"0.7.0-DEV.924"
69-
const DomainErrorNoArgs = DomainError()
70-
else
71-
const DomainErrorNoArgs = DomainError(nothing)
72-
end
73-
7469
include("bessel.jl")
7570
include("erf.jl")
7671
include("sincosint.jl")

src/bessel.jl

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,7 @@ for afn in (:airyai, :airyaiprime, :airybi, :airybiprime,
116116
$afn(z::Complex64) = Complex64($afn(Complex128(z)))
117117
end
118118
if afn in (:airyaix, :airyaiprimex)
119-
@eval $afn(x::Real) = x < 0 ? throw(DomainErrorNoArgs) : real($afn(complex(float(x))))
119+
@eval $afn(x::Real) = x < 0 ? throw(DomainError(x, "`x` must be nonnegative.")) : real($afn(complex(float(x))))
120120
else
121121
@eval $afn(x::Real) = real($afn(complex(float(x))))
122122
end
@@ -328,13 +328,13 @@ besselkx(nu::Float64, z::Complex128) = _besselk(abs(nu), z, Int32(2))
328328

329329
function bessely(nu::Cint, x::Float64)
330330
if x < 0
331-
throw(DomainErrorNoArgs)
331+
throw(DomainError(x, "`x` must be nonnegative."))
332332
end
333333
ccall((:yn, libm), Float64, (Cint, Float64), nu, x)
334334
end
335335
function bessely(nu::Cint, x::Float32)
336336
if x < 0
337-
throw(DomainErrorNoArgs)
337+
throw(DomainError(x, "`x` must be nonnegative."))
338338
end
339339
ccall((:ynf, libm), Float32, (Cint, Float32), nu, x)
340340
end
@@ -362,7 +362,7 @@ Modified Bessel function of the first kind of order `nu`, ``I_\\nu(x)``.
362362
"""
363363
function besseli(nu::Real, x::AbstractFloat)
364364
if x < 0 && !isinteger(nu)
365-
throw(DomainErrorNoArgs)
365+
throw(DomainError(x, "`x` must be nonnegative and `nu` must be an integer."))
366366
end
367367
real(besseli(float(nu), complex(x)))
368368
end
@@ -374,7 +374,7 @@ Scaled modified Bessel function of the first kind of order `nu`, ``I_\\nu(x) e^{
374374
"""
375375
function besselix(nu::Real, x::AbstractFloat)
376376
if x < 0 && !isinteger(nu)
377-
throw(DomainErrorNoArgs)
377+
throw(DomainError(x, "`x` must be nonnegative and `nu` must be an integer."))
378378
end
379379
real(besselix(float(nu), complex(x)))
380380
end
@@ -390,7 +390,7 @@ function besselj(nu::Real, x::AbstractFloat)
390390
return besselj(Cint(nu), x)
391391
end
392392
elseif x < 0
393-
throw(DomainErrorNoArgs)
393+
throw(DomainError(x, "`x` must be nonnegative."))
394394
end
395395
real(besselj(float(nu), complex(x)))
396396
end
@@ -402,7 +402,7 @@ Scaled Bessel function of the first kind of order `nu`, ``J_\\nu(x) e^{- | \\ope
402402
"""
403403
function besseljx(nu::Real, x::AbstractFloat)
404404
if x < 0 && !isinteger(nu)
405-
throw(DomainErrorNoArgs)
405+
throw(DomainError(x, "`x` must be nonnegative and `nu` must be an integer."))
406406
end
407407
real(besseljx(float(nu), complex(x)))
408408
end
@@ -414,7 +414,7 @@ Modified Bessel function of the second kind of order `nu`, ``K_\\nu(x)``.
414414
"""
415415
function besselk(nu::Real, x::AbstractFloat)
416416
if x < 0
417-
throw(DomainErrorNoArgs)
417+
throw(DomainError(x, "`x` must be nonnegative."))
418418
elseif x == 0
419419
return oftype(x, Inf)
420420
end
@@ -428,7 +428,7 @@ Scaled modified Bessel function of the second kind of order `nu`, ``K_\\nu(x) e^
428428
"""
429429
function besselkx(nu::Real, x::AbstractFloat)
430430
if x < 0
431-
throw(DomainErrorNoArgs)
431+
throw(DomainError(x, "`x` must be nonnegative."))
432432
elseif x == 0
433433
return oftype(x, Inf)
434434
end
@@ -442,7 +442,7 @@ Bessel function of the second kind of order `nu`, ``Y_\\nu(x)``.
442442
"""
443443
function bessely(nu::Real, x::AbstractFloat)
444444
if x < 0
445-
throw(DomainErrorNoArgs)
445+
throw(DomainError(x, "`x` must be nonnegative."))
446446
elseif isinteger(nu) && typemin(Cint) <= nu <= typemax(Cint)
447447
return bessely(Cint(nu), x)
448448
end
@@ -457,7 +457,7 @@ Scaled Bessel function of the second kind of order `nu`,
457457
"""
458458
function besselyx(nu::Real, x::AbstractFloat)
459459
if x < 0
460-
throw(DomainErrorNoArgs)
460+
throw(DomainError(x, "`x` must be nonnegative."))
461461
end
462462
real(besselyx(float(nu), complex(x)))
463463
end
@@ -527,7 +527,7 @@ Bessel function of the second kind of order 0, ``Y_0(x)``.
527527
"""
528528
function bessely0(x::BigFloat)
529529
if x < 0
530-
throw(DomainErrorNoArgs)
530+
throw(DomainError(x, "`x` must be nonnegative."))
531531
end
532532
z = BigFloat()
533533
ccall((:mpfr_y0, :libmpfr), Int32, (Ptr{BigFloat}, Ptr{BigFloat}, Int32), &z, &x, ROUNDING_MODE[])
@@ -541,7 +541,7 @@ Bessel function of the second kind of order 1, ``Y_1(x)``.
541541
"""
542542
function bessely1(x::BigFloat)
543543
if x < 0
544-
throw(DomainErrorNoArgs)
544+
throw(DomainError(x, "`x` must be nonnegative."))
545545
end
546546
z = BigFloat()
547547
ccall((:mpfr_y1, :libmpfr), Int32, (Ptr{BigFloat}, Ptr{BigFloat}, Int32), &z, &x, ROUNDING_MODE[])
@@ -550,7 +550,7 @@ end
550550

551551
function bessely(n::Integer, x::BigFloat)
552552
if x < 0
553-
throw(DomainErrorNoArgs)
553+
throw(DomainError(x, "`x` must be nonnegative."))
554554
end
555555
z = BigFloat()
556556
ccall((:mpfr_yn, :libmpfr), Int32, (Ptr{BigFloat}, Clong, Ptr{BigFloat}, Int32), &z, n, &x, ROUNDING_MODE[])

src/deprecated.jl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ function _airy(k::Integer, z::Complex128)
1212
elseif k == 2 || k == 3
1313
return _biry(z, id, Int32(1))
1414
else
15-
throw(ArgumentError("k must be between 0 and 3"))
15+
throw(DomainError(k, "`k` must be between 0 and 3."))
1616
end
1717
end
1818
function _airyx(k::Integer, z::Complex128)
@@ -23,7 +23,7 @@ function _airyx(k::Integer, z::Complex128)
2323
elseif k == 2 || k == 3
2424
return _biry(z, id, Int32(2))
2525
else
26-
throw(ArgumentError("k must be between 0 and 3"))
26+
throw(DomainError(k, "`k` must be between 0 and 3."))
2727
end
2828
end
2929

src/erf.jl

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ function erfinv(x::Float64)
9393
elseif x == -1.0
9494
return -Inf
9595
end
96-
throw(DomainErrorNoArgs)
96+
throw(DomainError(a, "`abs(x)` cannot be greater than 1."))
9797
elseif a <= 0.75 # Table 17 in Blair et al.
9898
t = x*x - 0.5625
9999
return x * @horner(t, 0.16030_49558_44066_229311e2,
@@ -161,7 +161,7 @@ function erfinv(x::Float32)
161161
elseif x == -1.0f0
162162
return -Inf32
163163
end
164-
throw(DomainErrorNoArgs)
164+
throw(DomainError(a, "`abs(x)` cannot be greater than 1."))
165165
elseif a <= 0.75f0 # Table 10 in Blair et al.
166166
t = x*x - 0.5625f0
167167
return x * @horner(t, -0.13095_99674_22f2,
@@ -213,7 +213,7 @@ function erfcinv(y::Float64)
213213
if y == 0.0
214214
return Inf
215215
end
216-
throw(DomainErrorNoArgs)
216+
throw(DomainError(y, "`y` must be nonnegative."))
217217
elseif y >= 1e-100 # Table 57 in Blair et al.
218218
t = 1.0 / sqrt(-log(y))
219219
return @horner(t, 0.10501_31152_37334_38116e-3,
@@ -265,7 +265,7 @@ function erfcinv(y::Float32)
265265
if y == 0.0f0
266266
return Inf32
267267
end
268-
throw(DomainErrorNoArgs)
268+
throw(DomainError(y, "`y` must be nonnegative."))
269269
else # Table 50 in Blair et al.
270270
t = 1.0f0 / sqrt(-log(y))
271271
return @horner(t, 0.15504_70003_116f0,
@@ -274,10 +274,9 @@ function erfcinv(y::Float32)
274274
-0.11280_81391_617f1,
275275
0.68054_42468_25f0,
276276
-0.16444_15679_1f0) /
277-
(t *
278-
@horner(t, 0.15502_48498_22f0,
279-
0.13852_28141_995f1,
280-
0.1f1))
277+
(t * @horner(t, 0.15502_48498_22f0,
278+
0.13852_28141_995f1,
279+
0.1f1))
281280
end
282281
end
283282

src/gamma.jl

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ signflip(m::Integer, z) = iseven(m) ? z : -z
9494
# This function computes the coefficients of the polynomial q_m(y),
9595
# returning an array of the coefficients of 1, y, y^2, ...,
9696
function cotderiv_q(m::Int)
97-
m < 0 && throw(ArgumentError("$m < 0 not allowed"))
97+
m < 0 && throw(DomainError(m, "`m` must be nonnegative."))
9898
m == 0 && return [1.0]
9999
m == 1 && return [1.0, 1.0]
100100
q₋ = cotderiv_q(m-1)
@@ -127,7 +127,7 @@ function cotderiv(m::Integer, z)
127127
isinf(imag(z)) && return zero(z)
128128
if m <= 0
129129
m == 0 && return π * cot*z)
130-
throw(DomainErrorNoArgs)
130+
throw(DomainError(m, "`m` must be nonnegative."))
131131
end
132132
if m <= length(cotderiv_Q)
133133
q = cotderiv_Q[m]
@@ -235,10 +235,10 @@ function zeta(s::ComplexOrReal{Float64}, z::ComplexOrReal{Float64})
235235
end
236236
x > 0 && imag(z) == 0 && imag(s) == 0 && return oftype(ζ, Inf)
237237
end
238-
throw(DomainErrorNoArgs) # nothing clever to return
238+
throw(DomainError(s, "`s` must be finite.")) # nothing clever to return
239239
end
240240
if isnan(x)
241-
if imag(z)==0 && imag(s)==0
241+
if imag(z) == 0 && imag(s) == 0
242242
return oftype(ζ, x)
243243
else
244244
return oftype(ζ, Complex(x,x))
@@ -258,7 +258,7 @@ function zeta(s::ComplexOrReal{Float64}, z::ComplexOrReal{Float64})
258258
# shift using recurrence formula
259259
xf = floor(x)
260260
nx = Int(xf)
261-
n = ceil(Int,cutoff - nx)
261+
n = ceil(Int, cutoff - nx)
262262
minus_s = -s
263263
if nx < 0 # x < 0
264264
# need to use (-z)^(-s) recurrence to be correct for real z < 0
@@ -339,7 +339,7 @@ function polygamma(m::Integer, z::ComplexOrReal{Float64})
339339
# "negapolygamma" function in the literature, and there are
340340
# multiple possible definitions arising from different integration
341341
# constants. We throw a DomainError since the definition is unclear.
342-
real(m) < 0 && throw(DomainErrorNoArgs)
342+
real(m) < 0 && throw(DomainError(m, "`real(m)` must be nonnegative, since the definition in this case is ambiguous."))
343343

344344
s = Float64(m+1)
345345
# It is safe to convert any integer (including `BigInt`) to a float here

src/sincosint.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,7 @@ function cosint(x::Float64)
120120
r01, r02 = 0.6162109375, 0.29454812071623379711E-3
121121
r11, r12 = 3.3837890625, 0.39136005118642639785E-3
122122
if x < 0.0
123-
throw(DomainErrorNoArgs)
123+
throw(DomainError(x, "`x` must be nonnegative."))
124124
elseif x 3.0
125125
return log(x/r0) + ((x - r01) - r02) * (x + r0) *
126126
@horner(t, -0.24607411378767540707E0,

0 commit comments

Comments
 (0)