Stand-alone Python code for log(1+x)

If p is very small, directly computing log(1+p) can be inaccurate. See the C++ version of this page for details.

NB: Starting with version 2.6, Python has a function math.log1p for this purpose.

import math

# compute log(1+x) without loss of precision for small values of x
def log_one_plus_x(x):
    if x <= -1.0:
        raise FloatingPointError, "argument must be > -1"

    if abs(x) > 1e-4:
        # x is large enough that the obvious evaluation is OK
        return math.log(1.0 + x)
    else:
        # Use Taylor approx. 
        # log(1 + x) = x - x^2/2 with error roughly x^3/3
        # Since |x| < 10^-4, |x|^3 < 10^-12, 
        # and the relative error is less than 10^-8
        return (-0.5*x + 1.0)*x

This code is in the public domain. Do whatever you want to with it, no strings attached.

Other versions of this code: C++, C#

More stand-alone numerical code

For daily tips on Python and scientific computing, follow @SciPyTip on Twitter.

Scipytip twitter icon