Skip to content

Commit

Permalink
fix exp(NaN16) and add tests (#42555)
Browse files Browse the repository at this point in the history
* fix exp(NaN16) and add tests
  • Loading branch information
oscardssmith authored Oct 12, 2021
1 parent d31eef5 commit c8cc1b5
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 39 deletions.
2 changes: 1 addition & 1 deletion base/special/exp.jl
Original file line number Diff line number Diff line change
Expand Up @@ -281,7 +281,7 @@ end
small_part = expb_kernel(base, r)
if !(abs(x) <= SUBNORM_EXP(base, T))
x > MAX_EXP(base, T) && return Inf16
N<=Int32(-24) && return zero(Float16)
x < MIN_EXP(base, T) && return zero(Float16)
end
twopk = reinterpret(T, (N+Int32(127)) << Int32(23))
return Float16(twopk*small_part)
Expand Down
58 changes: 20 additions & 38 deletions test/math.jl
Original file line number Diff line number Diff line change
Expand Up @@ -308,47 +308,29 @@ end
end
end

@testset "exp function" for T in (Float64, Float32)
@testset "$T accuracy" begin
X = map(T, vcat(-10:0.0002:10, -80:0.001:80, 2.0^-27, 2.0^-28, 2.0^-14, 2.0^-13))
for x in X
y, yb = exp(x), exp(big(x))
@test abs(y-yb) <= 1.0*eps(T(yb))
end
end
@testset "$T edge cases" begin
@test isnan_type(T, exp(T(NaN)))
@test exp(T(-Inf)) === T(0.0)
@test exp(T(Inf)) === T(Inf)
@test exp(T(0.0)) === T(1.0) # exact
@test exp(T(5000.0)) === T(Inf)
@test exp(T(-5000.0)) === T(0.0)
end
end

@testset "exp10 function" begin
@testset "accuracy" begin
X = map(Float64, vcat(-10:0.00021:10, -35:0.0023:100, -300:0.001:300))
for x in X
y, yb = exp10(x), exp10(big(x))
@test abs(y-yb) <= 1.2*eps(Float64(yb))
@testset "exponential functions" for T in (Float64, Float32, Float16)
for (func, invfunc) in ((exp2, log2), (exp, log), (exp10, log10))
@testset "$T $func accuracy" begin
minval, maxval = invfunc(floatmin(T)),prevfloat(invfunc(floatmax(T)))
# Test range and extensively test numbers near 0.
X = Iterators.flatten((minval:T(.1):maxval,
minval/100:T(.0021):maxval/100,
minval/10000:T(.000021):maxval/10000,
nextfloat(zero(T)) ))
for x in X
y, yb = func(x), func(widen(x))
@test abs(y-yb) <= 1.2*eps(T(yb))
end
end
X = map(Float32, vcat(-10:0.00021:10, -35:0.0023:35, -35:0.001:35))
for x in X
y, yb = exp10(x), exp10(big(x))
@test abs(y-yb) <= 1.2*eps(Float32(yb))
@testset "$T $func edge cases" begin
@test func(T(-Inf)) === T(0.0)
@test func(T(Inf)) === T(Inf)
@test func(T(NaN)) === T(NaN)
@test func(T(0.0)) === T(1.0) # exact
@test func(T(5000.0)) === T(Inf)
@test func(T(-5000.0)) === T(0.0)
end
end
@testset "$T edge cases" for T in (Float64, Float32)
@test isnan_type(T, exp10(T(NaN)))
@test exp10(T(-Inf)) === T(0.0)
@test exp10(T(Inf)) === T(Inf)
@test exp10(T(0.0)) === T(1.0) # exact
@test exp10(T(1.0)) === T(10.0)
@test exp10(T(3.0)) === T(1000.0)
@test exp10(T(5000.0)) === T(Inf)
@test exp10(T(-5000.0)) === T(0.0)
end
end

@testset "test abstractarray trig functions" begin
Expand Down

0 comments on commit c8cc1b5

Please sign in to comment.