Given that I know the value of $\log(x)$, I would like to calculate the value of $\log(x+1)$ on a computer.

I know that I could use the Taylor expansion of $\log(1+x)$, but that uses $x$ rather than $\log(x)$. The reason I do not want to use $x$ directly is because $\log(x)$ can get low values such as $-1000$, and this will cause an underflow.

My question is if there is a way of directly relating $\log(x)$ to $\log(1+x)$?

  • 1
    $\begingroup$ $\log{(1+x)}=\log{x}+\log{(1+1/x)}=\log{x}+1/x-1/x^2+...$ $\endgroup$ – Paul Feb 2 '16 at 17:20
  • 4
    $\begingroup$ With values of $x$ as low as $e^{-1000}$, you can very very very safely use $\ln(1+x)=x$ ! $\endgroup$ – Yves Daoust Feb 2 '16 at 17:21
  • $\begingroup$ @YvesDaoust Actually if $x = e^{-1000}$, $\log(1+x) = \log(1) = 0$... $\endgroup$ – Ring Ø Feb 2 '16 at 17:28
  • $\begingroup$ @YvesDaoust Which makes me ask: Can the OP's data type represent $e^{-1000}\approx 5\cdot10^{-435}$ at all? That's 100 orders of magnitude smaller than the minimal nonzero double ... $\endgroup$ – Hagen von Eitzen Feb 2 '16 at 17:29
  • $\begingroup$ $e^{-1000}$ is zero in double precision. $\endgroup$ – lisyarus Feb 2 '16 at 17:29

Given $y=\ln x$ and assuming double precision float arithmetic, you can safely approximate

  • $\ln(1+x)\approx0$ for $x<4\cdot 10^{-324}$, i.e., for $y<-745$.
  • $\ln(1+x)\approx x=e^y$ for $x<2^{-53}$ (that is, if $x^2\ll x$), i.e., for $y<-37$
  • $\ln(1+x)\approx \ln x+\frac1x=y+e^{-y}$ for $y>37$
  • and in the intermediate range just go ahead and compute $\ln(1+x)=\ln(1+e^y)$.

Actually, most CPUs have a builtin $\ln(1+x)$ suited for this problem


As $x$ is tiny, the Taylor development of $\log(1+x)$ is perfectly appropriate and the requested relation is

$$\log(1+x)\approx x=e^{\log(x)}.$$

You need to take the antilogarithm of the given logarithm.


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.