Aditya369 Aditya369 - 1 year ago 113
Python Question

Python Calculate log(1+x)/x for x near 0

Is there a way to correctly calculate the value of log(1+x)/x in python for values of x close to 0? When I do it normally using np.log1p(x)/x, I get 1. I somehow seem to get the correct values when I use np.log(x). Isn't log1p supposed to be more stable?

Answer Source

So I found one answer to this. I used a library called decimal.

from decimal import Decimal
x = Decimal('1e-13')
xp1 = Decimal(1) + x

This library seems to be much more stable than numpy.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download