Skip to content

Commit 9c08b03

Browse files
committed
lambertw: simplify handling non-finite vals
1 parent 94e44f9 commit 9c08b03

File tree

1 file changed

+4
-6
lines changed

1 file changed

+4
-6
lines changed

src/lambertw.jl

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -52,16 +52,14 @@ function _lambertw(x::Real, k, maxits)
5252
end
5353

5454
# Real x, k = 0
55-
# This appears to be inferrable with T=Float64 and T=BigFloat, including if x=Inf.
5655
# There is a magic number here. It could be noted, or possibly removed.
5756
# In particular, the fancy initial condition selection does not seem to help speed.
58-
function lambertw_branch_zero(x::T, maxits)::T where T<:Real
59-
isnan(x) && return(NaN)
60-
x == Inf && return Inf # appears to return convert(BigFloat, Inf) for x == BigFloat(Inf)
57+
function lambertw_branch_zero(x::T, maxits) where T<:Real
58+
isfinite(x) || return x
6159
one_t = one(T)
6260
oneoe = -one_t / convert(T, MathConstants.e) # The branch point
6361
x == oneoe && return -one_t
64-
oneoe <= x || throw(DomainError(x))
62+
oneoe < x || throw(DomainError(x))
6563
itwo_t = 1 / convert(T, 2)
6664
if x > one_t
6765
lx = log(x)
@@ -77,7 +75,7 @@ end
7775
function lambertw_branch_one(x::T, maxits) where T<:Real
7876
oneoe = -one(T) / convert(T, MathConstants.e)
7977
x == oneoe && return -one(T) # W approaches -1 as x -> -1/e from above
80-
oneoe <= x || throw(DomainError(x)) # branch domain exludes x < -1/e
78+
oneoe < x || throw(DomainError(x)) # branch domain exludes x < -1/e
8179
x == zero(T) && return -convert(T, Inf) # W decreases w/o bound as x -> 0 from below
8280
x < zero(T) || throw(DomainError(x))
8381
return lambertw_root_finding(x, log(-x), maxits)

0 commit comments

Comments
 (0)