Skip to content

Commit b2ed0db

Browse files
committed
lambertw tests: correct approx equality tests
1 parent f0f77b3 commit b2ed0db

File tree

1 file changed

+31
-48
lines changed

1 file changed

+31
-48
lines changed

test/lambertw.jl

Lines changed: 31 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,8 @@
2525

2626
## convert irrationals to float
2727

28-
@test isapprox(@inferred(lambertw(pi)), 1.0736581947961492)
29-
@test isapprox(@inferred(lambertw(pi, 0)), 1.0736581947961492)
28+
@test @inferred(lambertw(pi)) 1.0736581947961492
29+
@test @inferred(lambertw(pi, 0)) 1.0736581947961492
3030

3131
### infinite args or return values
3232

@@ -53,41 +53,36 @@
5353
for (z, k, res) in [(0, 0 , 0), (complex(0, 0), 0 , 0),
5454
(complex(0.0, 0), 0 , 0), (complex(1.0, 0), 0, 0.567143290409783873)]
5555
if Int != Int32
56-
@test isapprox(lambertw(z, k), res)
57-
@test isapprox(lambertw(z), res)
56+
@test lambertw(z, k) res
57+
@test lambertw(z) res
5858
else
59-
@test isapprox(lambertw(z, k), res; rtol = 1e-14)
60-
@test isapprox(lambertw(z), res; rtol = 1e-14)
59+
@test lambertw(z, k) res rtol=1e-14
60+
@test lambertw(z) res rtol=1e-14
6161
end
6262
end
6363

64-
for (z, k) in ((complex(1, 1), 2), (complex(1, 1), 0), (complex(.6, .6), 0),
65-
(complex(.6, -.6), 0))
66-
let w
67-
@test (w = lambertw(z, k) ; true)
68-
@test abs(w*exp(w) - z) < 1e-15
69-
end
64+
@testset "complex z=$z, k=$k" for (z, k) in
65+
((complex(1, 1), 2), (complex(1, 1), 0), (complex(.6, .6), 0),
66+
(complex(.6, -.6), 0))
67+
w = lambertw(z, k)
68+
@test w*exp(w) z atol=1e-15
7069
end
7170

72-
@test abs(lambertw(complex(-3.0, -4.0), 0) - Complex(1.075073066569255, -1.3251023817343588)) < 1e-14
73-
@test abs(lambertw(complex(-3.0, -4.0), 1) - Complex(0.5887666813694675, 2.7118802109452247)) < 1e-14
74-
@test (lambertw(complex(.3, .3), 0); true)
71+
@test lambertw(complex(-3.0, -4.0), 0) Complex(1.075073066569255, -1.3251023817343588) atol=1e-14
72+
@test lambertw(complex(-3.0, -4.0), 1) Complex(0.5887666813694675, 2.7118802109452247) atol=1e-14
73+
@test lambertw(complex(.3, .3)) Complex(0.26763519642648767, 0.1837481231767825)
7574

7675
# bug fix
7776
# The routine will start at -1/e + eps * im, rather than -1/e + 0im,
7877
# otherwise root finding will fail
79-
if Int != Int32
80-
@test abs(lambertw(-inv(MathConstants.e) + 0im, -1)) == 1
81-
else
82-
@test abs(lambertw(-inv(MathConstants.e) + 0im, -1) + 1) < 1e-7
83-
end
78+
@test lambertw(-inv(MathConstants.e) + 0im, -1) -1 atol=1e-7
79+
8480
# lambertw for BigFloat is more precise than Float64. Note
8581
# that 70 digits in test is about 35 digits in W
86-
let W
87-
for z in [BigFloat(1), BigFloat(2), complex(BigFloat(1), BigFloat(1))]
88-
@test (W = lambertw(z); true)
89-
@test abs(z - W * exp(W)) < BigFloat(1)^(-70)
90-
end
82+
@testset "lambertw() for BigFloat z=$z" for z in
83+
[BigFloat(1), BigFloat(2), complex(BigFloat(1), BigFloat(1))]
84+
W = lambertw(z)
85+
@test z W*exp(W) atol=BigFloat(10)^(-70)
9186
end
9287

9388
### ω constant
@@ -117,36 +112,24 @@ end
117112

118113
if Int != Int32
119114

120-
# Test double-precision expansion near branch point using BigFloats
121-
let sp = precision(BigFloat), z = BigFloat(1)/10^12, wo, xdiff
122-
setprecision(2048)
123-
for i in 1:300
124-
innerarg = z - inv(big(MathConstants.e))
115+
@testset "double-precision expansion near branch point using BigFloats" begin
116+
setprecision(2048) do
117+
z = BigFloat(10)^(-12)
118+
for _ in 1:300
119+
innerarg = z - inv(big(MathConstants.e))
125120

126-
# branch k = 0
127-
@test (wo = lambertwbp(Float64(z)); xdiff = abs(-1 + wo - lambertw(innerarg)); true)
128-
if xdiff > 5e-16
129-
println(Float64(z), " ", Float64(xdiff))
130-
end
131-
@test xdiff < 5e-16
121+
@test lambertwbp(Float64(z)) 1 + lambertw(innerarg) atol=5e-16
122+
@test lambertwbp(Float64(z), -1) 1 + lambertw(innerarg, -1) atol=5e-16
123+
z *= 1.1
124+
if z > 0.23 break end
132125

133-
# branch k = -1
134-
@test (wo = lambertwbp(Float64(z), -1); xdiff = abs(-1 + wo - lambertw(innerarg, -1)); true)
135-
if xdiff > 5e-16
136-
println(Float64(z), " ", Float64(xdiff))
137126
end
138-
@test xdiff < 5e-16
139-
z *= 1.1
140-
if z > 0.23 break end
141-
142127
end
143-
setprecision(sp)
144128
end
145129

146130
# test the expansion about branch point for k=-1,
147131
# by comparing to exact BigFloat calculation.
148-
@test @inferred(lambertwbp(1e-20, -1)) - 1 - lambertw(-inv(big(MathConstants.e)) + BigFloat(10)^BigFloat(-20), -1) < 1e-16
149-
150-
@test abs(@inferred(lambertwbp(Complex(.01, .01), -1)) - Complex(-0.2755038208041206, -0.1277888928494641)) < 1e-14
132+
@test @inferred(lambertwbp(1e-20, -1)) 1 + lambertw(-inv(big(MathConstants.e)) + BigFloat(10)^(-20), -1) atol=1e-16
133+
@test @inferred(lambertwbp(Complex(.01, .01), -1)) Complex(-0.2755038208041206, -0.1277888928494641) atol=1e-14
151134

152135
end # if Int != Int32

0 commit comments

Comments
 (0)