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?
Asked
Active
Viewed 494 times
1
-
5for `log(1+x)` you want `np.log1p(x)` not `np.log1p(1+x)`. – hobbs Sep 13 '16 at 18:59
-
You're right. Thank you. – Aditya369 Sep 13 '16 at 19:01
2 Answers
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