Skip to content

Commit 976dee6

Browse files
authored
Merge pull request #99 from nsajko/logit_accuracy
fix accuracy of `logit`
2 parents b88d17d + a1eb57e commit 976dee6

File tree

2 files changed

+12
-1
lines changed

2 files changed

+12
-1
lines changed

src/basicfuns.jl

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,13 @@ for ``0 < x < 1``.
120120
121121
Its inverse is the [`logistic`](@ref) function.
122122
"""
123-
logit(x::Real) = log(x / (one(x) - x))
123+
function logit(x::Real)
124+
if 4 * x < 1
125+
-log(inv(x) - 1)
126+
else
127+
2 * atanh(2*x - 1)
128+
end
129+
end
124130

125131
"""
126132
$(SIGNATURES)

test/basicfuns.jl

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,11 @@ end
8686
@test logistic(+750.0) === 1.0
8787
@test iszero(logit(0.5))
8888
@test logit(logistic(2)) 2.0
89+
@testset "accuracy of `logit`" begin
90+
for t in (Float16, Float32, Float64)
91+
@test 2 * ulp_error_maximum(logit, range(start = t(0), stop = t(1), length = 500)) < 3
92+
end
93+
end
8994
end
9095

9196
@testset "logcosh and logabssinh" begin

0 commit comments

Comments
 (0)