diff --git a/base/special/exp.jl b/base/special/exp.jl index 7e7f9b100b439..d9afd4c7134c9 100644 --- a/base/special/exp.jl +++ b/base/special/exp.jl @@ -254,6 +254,8 @@ end end @inline function exp_impl_fast(x::Float64, base) T = Float64 + x >= MAX_EXP(base, T) && return Inf + x <= -SUBNORM_EXP(base, T) && return 0.0 N_float = muladd(x, LogBo256INV(base, T), MAGIC_ROUND_CONST(T)) N = reinterpret(UInt64, N_float) % Int32 N_float -= MAGIC_ROUND_CONST(T) #N_float now equals round(x*LogBo256INV(base, T)) @@ -288,6 +290,8 @@ end @inline function exp_impl_fast(x::Float32, base) T = Float32 + x >= MAX_EXP(base, T) && return Inf32 + x <= -SUBNORM_EXP(base, T) && return 0f0 N_float = round(x*LogBINV(base, T)) N = unsafe_trunc(Int32, N_float) r = muladd(N_float, LogBU(base, T), x) diff --git a/test/fastmath.jl b/test/fastmath.jl index edaab1c6eb0cf..e93fb93330b4f 100644 --- a/test/fastmath.jl +++ b/test/fastmath.jl @@ -249,3 +249,13 @@ end @test (@fastmath "a" * "b") == "ab" @test (@fastmath "a" ^ 2) == "aa" end + + +@testset "exp overflow and underflow" begin + for T in (Float32,Float64) + for func in (@fastmath exp2,exp,exp10) + @test func(T(2000)) == T(Inf) + @test func(T(-2000)) == T(0) + end + end +end