Skip to content

Commit

Permalink
log1pexp, #52
Browse files Browse the repository at this point in the history
log1pexp, use float
  • Loading branch information
Jorge Fernandez-de-Cossio-Diaz committed May 21, 2019
1 parent 96aecb4 commit 509a371
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 2 deletions.
2 changes: 1 addition & 1 deletion src/basicfuns.jl
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ Return `log(1+exp(x))` evaluated carefully for largish `x`.
This is also called the ["softplus"](https://en.wikipedia.org/wiki/Rectifier_(neural_networks))
transformation, being a smooth approximation to `max(0,x)`. Its inverse is [`logexpm1`](@ref).
"""
log1pexp(x::Real) = x < 18.0 ? log1p(exp(x)) : x < 33.3 ? x + exp(-x) : oftype(exp(-x), x)
log1pexp(x::Real) = x -37. ? exp(x) : x 18. ? log1p(exp(x)) : x 33.3 ? x + exp(-x) : float(x)
log1pexp(x::Float32) = x < 9.0f0 ? log1p(exp(x)) : x < 16.0f0 ? x + exp(-x) : oftype(exp(-x), x)

"""
Expand Down
4 changes: 3 additions & 1 deletion test/basicfuns.jl
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,13 @@ end
@test log1pexp(-2.0) log(1.0 + exp(-2.0))
@test log1pexp(10000) 10000.0
@test log1pexp(-10000) 0.0

@test log1pexp(2f0) log(1f0 + exp(2f0))
@test log1pexp(-2f0) log(1f0 + exp(-2f0))
@test log1pexp(10000f0) 10000f0
@test log1pexp(-10000f0) 0f0
@inferred log1pexp(0)
@inferred log1pexp(0.)
@inferred log1pexp(0.0f0)
end

@testset "log1mexp" begin
Expand Down

0 comments on commit 509a371

Please sign in to comment.