Skip to content

Commit c8cc1b5

Browse files
authored
fix exp(NaN16) and add tests (#42555)
* fix exp(NaN16) and add tests
1 parent d31eef5 commit c8cc1b5

File tree

2 files changed

+21
-39
lines changed

2 files changed

+21
-39
lines changed

base/special/exp.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -281,7 +281,7 @@ end
281281
small_part = expb_kernel(base, r)
282282
if !(abs(x) <= SUBNORM_EXP(base, T))
283283
x > MAX_EXP(base, T) && return Inf16
284-
N<=Int32(-24) && return zero(Float16)
284+
x < MIN_EXP(base, T) && return zero(Float16)
285285
end
286286
twopk = reinterpret(T, (N+Int32(127)) << Int32(23))
287287
return Float16(twopk*small_part)

test/math.jl

Lines changed: 20 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -308,47 +308,29 @@ end
308308
end
309309
end
310310

311-
@testset "exp function" for T in (Float64, Float32)
312-
@testset "$T accuracy" begin
313-
X = map(T, vcat(-10:0.0002:10, -80:0.001:80, 2.0^-27, 2.0^-28, 2.0^-14, 2.0^-13))
314-
for x in X
315-
y, yb = exp(x), exp(big(x))
316-
@test abs(y-yb) <= 1.0*eps(T(yb))
317-
end
318-
end
319-
@testset "$T edge cases" begin
320-
@test isnan_type(T, exp(T(NaN)))
321-
@test exp(T(-Inf)) === T(0.0)
322-
@test exp(T(Inf)) === T(Inf)
323-
@test exp(T(0.0)) === T(1.0) # exact
324-
@test exp(T(5000.0)) === T(Inf)
325-
@test exp(T(-5000.0)) === T(0.0)
326-
end
327-
end
328-
329-
@testset "exp10 function" begin
330-
@testset "accuracy" begin
331-
X = map(Float64, vcat(-10:0.00021:10, -35:0.0023:100, -300:0.001:300))
332-
for x in X
333-
y, yb = exp10(x), exp10(big(x))
334-
@test abs(y-yb) <= 1.2*eps(Float64(yb))
311+
@testset "exponential functions" for T in (Float64, Float32, Float16)
312+
for (func, invfunc) in ((exp2, log2), (exp, log), (exp10, log10))
313+
@testset "$T $func accuracy" begin
314+
minval, maxval = invfunc(floatmin(T)),prevfloat(invfunc(floatmax(T)))
315+
# Test range and extensively test numbers near 0.
316+
X = Iterators.flatten((minval:T(.1):maxval,
317+
minval/100:T(.0021):maxval/100,
318+
minval/10000:T(.000021):maxval/10000,
319+
nextfloat(zero(T)) ))
320+
for x in X
321+
y, yb = func(x), func(widen(x))
322+
@test abs(y-yb) <= 1.2*eps(T(yb))
323+
end
335324
end
336-
X = map(Float32, vcat(-10:0.00021:10, -35:0.0023:35, -35:0.001:35))
337-
for x in X
338-
y, yb = exp10(x), exp10(big(x))
339-
@test abs(y-yb) <= 1.2*eps(Float32(yb))
325+
@testset "$T $func edge cases" begin
326+
@test func(T(-Inf)) === T(0.0)
327+
@test func(T(Inf)) === T(Inf)
328+
@test func(T(NaN)) === T(NaN)
329+
@test func(T(0.0)) === T(1.0) # exact
330+
@test func(T(5000.0)) === T(Inf)
331+
@test func(T(-5000.0)) === T(0.0)
340332
end
341333
end
342-
@testset "$T edge cases" for T in (Float64, Float32)
343-
@test isnan_type(T, exp10(T(NaN)))
344-
@test exp10(T(-Inf)) === T(0.0)
345-
@test exp10(T(Inf)) === T(Inf)
346-
@test exp10(T(0.0)) === T(1.0) # exact
347-
@test exp10(T(1.0)) === T(10.0)
348-
@test exp10(T(3.0)) === T(1000.0)
349-
@test exp10(T(5000.0)) === T(Inf)
350-
@test exp10(T(-5000.0)) === T(0.0)
351-
end
352334
end
353335

354336
@testset "test abstractarray trig functions" begin

0 commit comments

Comments
 (0)