Log1p in Base vs Base.Math.JuliaLibm



?log1p says that

There is an experimental variant in the Base.Math.JuliaLibm module, which is typically faster and more accurate.

So why isn’t that one used in Base — does it have some problems? What should one know about the trade-offs between the two implementations?


I think the concern was that it was insufficiently tested? https://github.com/JuliaLang/julia/pull/10008


@stevengj: I did some checking and Base.Math.JuliaLibm.log1p is better. Writeup here: https://tpapp.github.io/post/log1p/


I don’t have a disqus account so I’m commenting here on the blog post:

The reason \log(1+x) is inaccurate when x is small is because 1+x will round the result to fit in a floating point representation. For example, 1.0 + 1E-16 = 1.0. Thus, instead of getting the correct answer, ~1E-16, you’ll just get 0. This page goes into a bit more detail about this function and others.


@jebej: this is well known, and was not the point of the blog post. The latter is about the error of the two implementations currently in Julia Base.


If there are no objection, I will probably submit a PR making Base.Math.JuliaLibm.log1p the default, and add the BigNum calculations as tests.


Is there a speed difference between the 2?


I updated the blog post with benchmarks. TL;DR: Base.Math.JuliaLibm.log1p is about 30-40% faster, except for very small values, where it is 10-20% slower. On average, there should be a performance gain.


Isn’t the main point of log1p to handle very small values? A shame to get slower for those.

In any case, a PR would be a better place to discuss this.