Skip to content

Commit

Permalink
generic threshold
Browse files Browse the repository at this point in the history
  • Loading branch information
cossio committed Mar 8, 2022
1 parent 9085ef7 commit ce16e33
Showing 1 changed file with 7 additions and 11 deletions.
18 changes: 7 additions & 11 deletions src/basicfuns.jl
Original file line number Diff line number Diff line change
Expand Up @@ -159,20 +159,16 @@ See:
Note: different than Maechler (2012), also uses bounds specific to Float32 and Float16.
"""
function log1pexp(x::Real)
if x > _log1pexp_thresh(x)
return float(x)
threshold = _log1pexp_threshold(x)
if x > threshold # log1pexp(x) ≈ x for large x
return oftype(threshold, x)
else
t = log1p(exp(-abs(x)))
return x 0 ? t : t + x
end
return log1p(exp(x))
end
end

# threshold `thresh` such that `log1pexp(x) == x` for `x > thresh`
_log1pexp_thresh(::Float64) = 33.27106466687738
_log1pexp_thresh(::Float32) = 14.556091f0
_log1pexp_thresh(::Float16) = Float16(6.24)
_log1pexp_thresh(::BigFloat) = 172.5936479594263820448907982430859654507995334557035582760493223638550118704546
_log1pexp_thresh(::Real) = _log1pexp_thresh(0.0)
# returns `threshold` such that `log1pexp(x) ≈ x` for `x > threshold`
@generated _log1pexp_threshold(::T) where {T<:Real} = -log(expm1(eps(float(T))/2))

"""
$(SIGNATURES)
Expand Down

0 comments on commit ce16e33

Please sign in to comment.