Precision loss for BigFloat in logexpm1, logcosh and log1pmx
Assessment
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Newbie friendliness
- 30/100
Research direction
Start by reading the implementations of logexpm1, logcosh, and log1pmx, along with the wrappers named in the issue. Run the provided BigFloat comparison script to reproduce the precision losses and check the existing tests for these functions. Done means the affected functions and wrappers retain precision across the reported input ranges; pull request #136 is already open, so check its work before starting.
Written by the indexing model from the issue text.
Description
All exported single-argument functions accept BigFloat, but a few of them lose a large part of the precision because the implementations contain thresholds or branch choices that are tuned for Float64.
Method
Each function was evaluated at the default 256-bit BigFloat precision on a grid of inputs spanning its domain (tiny, moderate and huge magnitudes). The reference is the naive mathematical definition evaluated at 4096 bits. "Digits lost" is log10(relerr / eps(BigFloat)), so 0 means correctly rounded to within a few ulps and 77 means no correct digits. The condition number of each function was computed as well and subtracted out, so the numbers below are algorithmic loss only, not inherent ill-conditioning (e.g. near roots). Measured on master (5346ca8).
Results
| Function | Digits lost (algorithmic) | Where |
|---|---|---|
xlogx |
0 | |
xexpx |
0 | |
logistic |
0 | |
logit |
0 | |
log1psq |
0 | |
log1pexp |
0 | |
log1mexp |
0 | |
log2mexp |
0 | |
logexpm1 |
up to 61 | 18 < x < 170 |
softplus |
0 | |
invsoftplus |
up to 61 | 18 < x < 170 |
log1pmx |
0.7 at 0.1, 2 at 1e-3, 10 at 1e-10, 30 at 1e-30, all 77 at 1e-100 | small |x| |
logmxp1 |
0 | |
logcosh |
1.4 at 0.1, 6 at 1e-3, 19 at 1e-10, all 77 at 1e-100 | small |x| |
logabssinh |
0 | |
logabstanh |
0 | |
cloglog |
0 | |
cexpexp |
0 | |
loglogistic |
0 | |
logitexp |
up to 61 | -170 < x < -18 |
log1mlogistic |
0 | |
logit1mexp |
up to 61 | -170 < x < -18 |
Causes
Three distinct problems account for every nonzero row:
logexpm1hasFloat64thresholds baked in. Above 18 it switches tox - exp(-x), which drops a term of sizeexp(-2x)/2, and above 33.3 it returnsxunchanged, droppingexp(-x). Both cutoffs are right for 53 bits and wrong for 256. The loss peaks around 61 digits near x = 34 and only fades to zero past x ≈ 170, whereexp(-x)drops below the 256-bit epsilon.invsoftplus,logitexpandlogit1mexpare thin wrappers aroundlogexpm1and inherit the problem.logcoshhas no small-argument kernel for non-hardware floats. The polynomial kernel is gated onFloat16/Float32/Float64; everything else evaluates|x| + log1pexp(-2|x|) - log(2), where the two large terms cancel. The loss grows as about2*log10(1/|x|)digits.log1pmxfalls back tolog1p(x) - x, which loses aboutlog10(1/|x|)digits for small x. (logmxp1's generic fallback is fine because of its branch at x = 1/2.) The analogous question forFloat32andFloat16is tracked in #46.
Possible fixes: precision-aware thresholds in logexpm1 based on eps(x) instead of constants, and a series or direct log(cosh(x)) branch in logcosh and a series for log1pmx for types without a dedicated kernel.
Script
using LogExpFunctions, Printf, ForwardDiff
# (function, reference definition, test points)
L(a) = BigFloat.(a)
small = [1e-100, 1e-30, 1e-10, 1e-3]
cases = [
(xlogx, x -> iszero(x) ? zero(x) : x*log(x), L([0; small; 0.5; 1; 2; 1e10; 1e100])),
(xexpx, x -> x*exp(x), L([-1e100; -1e3; -10; -1; -small; 0; small; 1; 10; 500])),
(logistic, x -> 1/(1+exp(-x)), L([-1e5; -800; -40; -1; -small; 0; small; 1; 40; 800; 1e5])),
(logit, x -> log(x/(1-x)), L([1e-100; 1e-10; 0.1; 0.5; 0.5+1e-20; 0.9; 1-1e-10; 1-1e-15])),
(log1psq, x -> log1p(x^2), L([0; small; 1; 10; 1e10; 1e100])),
(log1pexp, x -> log1p(exp(x)), L([-1e5; -800; -40; -1; 0; 1; 40; 800; 1e5])),
(log1mexp, x -> log1p(-exp(x)), L([-1e5; -800; -40; -1; -0.5; -small])),
(log2mexp, x -> log(2-exp(x)), L([-1e5; -40; -1; -small; 0; small; 0.5; log(big(2))-1e-20])),
(logexpm1, x -> log(expm1(x)), L([small; 0.5; 1; 40; 800; 1e5])),
(softplus, x -> log1p(exp(x)), L([-1e5; -800; -40; -1; 0; 1; 40; 800; 1e5])),
(invsoftplus, x -> log(expm1(x)), L([small; 0.5; 1; 40; 800; 1e5])),
(log1pmx, x -> log1p(x)-x, L([-0.9; -0.5; -small; 0; small; 0.5; 1; 10; 1e10])),
(logmxp1, x -> log(x)-x+1, L([1e-10; 0.1; 0.5; 1-1e-20; 1; 1+1e-20; 2; 10; 1e10])),
(logcosh, x -> log(cosh(x)), L([-1e5; -40; -1; -small; 0; small; 1; 40; 1e5])),
(logabssinh, x -> log(abs(sinh(x))), L([-1e5; -40; -1; -small; small; 1; 40; 1e5])),
(logabstanh, x -> log(abs(tanh(x))), L([-1e5; -40; -1; -small; small; 1; 40; 1e5])),
(cloglog, x -> log(-log(1-x)), L([1e-100; 1e-10; 0.1; 0.5; 0.9; 1-1e-10; 1-1e-20])),
(cexpexp, x -> 1-exp(-exp(x)), L([-1e5; -800; -40; -1; 0; 1; 5; 40])),
(loglogistic, x -> -log1p(exp(-x)), L([-1e5; -800; -40; -1; 0; 1; 40; 800; 1e5])),
(logitexp, x -> log(exp(x)/(1-exp(x))), L([-1e5; -800; -40; -1; -log(big(2)); -0.5; -small])),
(log1mlogistic,x -> -log1p(exp(x)), L([-1e5; -800; -40; -1; 0; 1; 40; 800; 1e5])),
(logit1mexp, x -> log((1-exp(x))/exp(x)), L([-1e5; -800; -40; -1; -log(big(2)); -0.5; -small])),
]
const REFPREC = 4096
println("| function | works | return type | max digits lost | of which inherent (cond) | excess (algorithmic) | worst input |")
println("|---|---|---|---|---|---|---|")
for (f, ref, xs) in cases
worst = -Inf; worstx = nothing; rt = nothing; err = nothing; worstcond = 0.0; worstlost = 0.0
for x in xs
y = try f(x) catch e; err = e; break end
rt = typeof(y)
r = setprecision(REFPREC) do; ref(BigFloat(x)) end
isfinite(r) && !iszero(r) || continue
relerr = Float64(abs(big(y) - r) / abs(r))
lost = relerr == 0 ? 0.0 : log10(relerr / Float64(eps(BigFloat)))
cond = setprecision(REFPREC) do
xb = BigFloat(x); d = ForwardDiff.derivative(ref, xb)
iszero(xb) ? 0.0 : max(0.0, log10(Float64(abs(xb*d/r))))
end
excess = max(0.0, lost - cond)
if excess > worst; worst = excess; worstx = x; worstcond = cond; worstlost = lost; end
end
if err !== nothing
@printf("| `%s` | no (%s) | | | |\n", f, nameof(typeof(err)))
else
wx = worstx === nothing ? "" : @sprintf("%.3g", Float64(worstx))
@printf("| `%s` | yes | %s | %.1f | %.1f | %.1f | %s |\n", f, rt, max(worstlost, 0.0), worstcond, max(worst, 0.0), wx)
end
end
This analysis and the text of this issue were written by Claude Code at my request. I have reviewed it.
- Dominant language
- Julia
- Stars
- 86
- Forks
- 25
- Avg merge
- 4d 7h
- Merged PRs (30d)
- 3
Getting set up
This project ships no dev container, Dockerfile or contributing guide, so setting up is up to you: start from its README, and see our first-contribution guide for the general steps.
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
More from JuliaStats/LogExpFunctions.jl
-
Difficulty 1/5 Under an hour Newbie friendliness 62/100
JuliaStats/LogExpFunctions.jl#134 · 2 comments ·
-
Difficulty 4/5 3-5 days Newbie friendliness 35/100
JuliaStats/LogExpFunctions.jl#102 · 4 comments ·
-
exp-weighted meanOpen
Difficulty 5/5 Over a week Newbie friendliness 25/100
-
log-cumsum-exp ?Open
Difficulty 4/5 3-5 days Newbie friendliness 25/100
-
Add logmeanexpMay be free again A pull request for this issue was closed without being merged. Open
Difficulty 3/5 1-2 days Newbie friendliness 35/100
JuliaStats/LogExpFunctions.jl#81 · 2 comments ·
All issues in JuliaStats/LogExpFunctions.jl
Similar issues
-
Difficulty 2/5 1-3 hours Newbie friendliness 72/100
JuliaDiff/ChainRules.jl#871 · 1 comment ·
Maintainers usually reply within 1 day
-
code bug PowerSystems 6.0
Difficulty 2/5 1-3 hours Newbie friendliness 76/100
Sienna-Platform/PowerSystems.jl#1849 ·
Maintainers usually reply within 7 days
-
Difficulty 2/5 1-3 hours Newbie friendliness 84/100
Maintainers usually reply within 1 day
-
Difficulty 1/5 1-3 hours Newbie friendliness 86/100
-
Difficulty 2/5 1-3 hours Newbie friendliness 70/100
FluxML/Metalhead.jl#407 ·
Maintainers usually reply within 1 day