Skip to content

Commit be0bae2

Browse files
authored
Use ternary operator instead of ifelse (#26)
1 parent 39223ba commit be0bae2

File tree

1 file changed

+4
-12
lines changed

1 file changed

+4
-12
lines changed

src/basicfuns.jl

Lines changed: 4 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ julia> xlogx(0)
1111
"""
1212
function xlogx(x::Number)
1313
result = x * log(x)
14-
ifelse(iszero(x), zero(result), result)
14+
return iszero(x) ? zero(result) : result
1515
end
1616

1717
"""
@@ -26,7 +26,7 @@ julia> xlogy(0, 0)
2626
"""
2727
function xlogy(x::Number, y::Number)
2828
result = x * log(y)
29-
ifelse(iszero(x) && !isnan(y), zero(result), result)
29+
return iszero(x) && !isnan(y) ? zero(result) : result
3030
end
3131

3232
# The following bounds are precomputed versions of the following abstract
@@ -60,15 +60,7 @@ logistic(x::Real) = inv(exp(-x) + one(x))
6060
function logistic(x::Union{Float16, Float32, Float64})
6161
e = exp(x)
6262
lower, upper = _logistic_bounds(x)
63-
ifelse(
64-
x < lower,
65-
zero(x),
66-
ifelse(
67-
x > upper,
68-
one(x),
69-
e / (one(x) + e)
70-
)
71-
)
63+
return x < lower ? zero(x) : x > upper ? one(x) : e / (one(x) + e)
7264
end
7365

7466
"""
@@ -210,7 +202,7 @@ non-finite values.
210202
"""
211203
function logaddexp(x::Real, y::Real)
212204
# ensure Δ = 0 if x = y = ± Inf
213-
Δ = ifelse(x == y, zero(x - y), abs(x - y))
205+
Δ = x == y ? zero(x - y) : abs(x - y)
214206
max(x, y) + log1pexp(-Δ)
215207
end
216208

0 commit comments

Comments
 (0)