Gentler transformation from real to positive

It still overflows because of the intermediate exp(x) calculation: log(1+exp(710)) === Inf. It’s also subject to underflow: log(1+exp(-37)) == 0.0. It would be better to use log1p(exp(x)), but even that eventually underflows: log1p(exp(-746)) == 0.0. You could define your own special-function implementation for f(x) = log1p(exp(x)), rewritten to avoid spurious overflow — for example, f(x) = x > 0 ? x + log1p(exp(-x)) : log1p(exp(x)) suffices and is equivalent to log(1+exp(x)) in exact arithmetic — but the underflow is unavoidable since f(-746) ≈ 1.03e-324 is not representable as a Float64.

I still don’t understand why you need a bijection in a root-finding context, e.g. why x -> x^2 is not acceptable, since you can flip a negative solution to a positive one a posteriori as I commented above.

1 Like