1

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?

Aditya369
  • 834
  • 8
  • 20

2 Answers2

1
np.log1p(1+x)

That gives you log(2+x). Change it to np.log1p(x).

wim
  • 338,267
  • 99
  • 616
  • 750
0

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
print(xp1.ln()/x)

This library seems to be much more stable than numpy.

Aditya369
  • 834
  • 8
  • 20