# Log1p in Base vs Base.Math.JuliaLibm

**URL:** <https://discourse.julialang.org/t/log1p-in-base-vs-base-math-julialibm/5852>\
**Category:** General Usage\
**Tags:** question\
**Created:** [September 12, 2017, 12:46pm UTC](https://discourse.julialang.org/t/log1p-in-base-vs-base-math-julialibm/5852 "2017-09-12T12:46:09Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![Tamas\_Papp](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tamas_papp/32/25949_2.png) [@Tamas\_Papp](https://discourse.julialang.org/u/Tamas_Papp)\
**Post date:** [September 12, 2017, 12:46pm UTC](https://discourse.julialang.org/t/log1p-in-base-vs-base-math-julialibm/5852/1 "2017-09-12T12:46:09Z")

</div>

`?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?

---

<div class="post-metadata">

**Author:** ![stevengj](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stevengj/32/71_2.png) [@stevengj](https://discourse.julialang.org/u/stevengj)\
**Post date:** [September 12, 2017, 5:51pm UTC](https://discourse.julialang.org/t/log1p-in-base-vs-base-math-julialibm/5852/2 "2017-09-12T17:51:39Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Tamas\_Papp](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tamas_papp/32/25949_2.png) [@Tamas\_Papp](https://discourse.julialang.org/u/Tamas_Papp)\
**Post date:** [September 13, 2017, 10:29am UTC](https://discourse.julialang.org/t/log1p-in-base-vs-base-math-julialibm/5852/3 "2017-09-13T10:29:50Z")

</div>

@stevengj: I did some checking and `Base.Math.JuliaLibm.log1p` is better. Writeup here: [log1p in Julia](https://tpapp.github.io/post/log1p/)

---

<div class="post-metadata">

**Author:** ![jebej](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jebej/32/1784_2.png) [@jebej](https://discourse.julialang.org/u/jebej)\
**Post date:** [September 13, 2017, 11:41pm UTC](https://discourse.julialang.org/t/log1p-in-base-vs-base-math-julialibm/5852/4 "2017-09-13T23:41:19Z")

</div>

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](https://www.johndcook.com/blog/2010/06/07/math-library-functions-that-seem-unnecessary/) goes into a bit more detail about this function and others.

---

<div class="post-metadata">

**Author:** ![Tamas\_Papp](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tamas_papp/32/25949_2.png) [@Tamas\_Papp](https://discourse.julialang.org/u/Tamas_Papp)\
**Post date:** [September 14, 2017, 6:07am UTC](https://discourse.julialang.org/t/log1p-in-base-vs-base-math-julialibm/5852/5 "2017-09-14T06:07:45Z")

</div>

@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`.

---

<div class="post-metadata">

**Author:** ![Tamas\_Papp](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tamas_papp/32/25949_2.png) [@Tamas\_Papp](https://discourse.julialang.org/u/Tamas_Papp)\
**Post date:** [September 14, 2017, 6:07am UTC](https://discourse.julialang.org/t/log1p-in-base-vs-base-math-julialibm/5852/6 "2017-09-14T06:07:54Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![RoyiAvital](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/royiavital/32/571_2.png) [@RoyiAvital](https://discourse.julialang.org/u/RoyiAvital)\
**Post date:** [September 14, 2017, 6:42am UTC](https://discourse.julialang.org/t/log1p-in-base-vs-base-math-julialibm/5852/7 "2017-09-14T06:42:19Z")

</div>

Is there a speed difference between the 2?

---

<div class="post-metadata">

**Author:** ![Tamas\_Papp](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tamas_papp/32/25949_2.png) [@Tamas\_Papp](https://discourse.julialang.org/u/Tamas_Papp)\
**Post date:** [September 14, 2017, 9:42am UTC](https://discourse.julialang.org/t/log1p-in-base-vs-base-math-julialibm/5852/8 "2017-09-14T09:42:58Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![stevengj](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stevengj/32/71_2.png) [@stevengj](https://discourse.julialang.org/u/stevengj)\
**Post date:** [September 16, 2017, 2:52am UTC](https://discourse.julialang.org/t/log1p-in-base-vs-base-math-julialibm/5852/9 "2017-09-16T02:52:47Z")

</div>

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.
