1

I am trying to plot the logarithmic function: x*log(1+1/x) for very large values of x. I have checked for few terms since the overall plot was not good and the problem seems to be that although this function should never be higher than 1, for some points it becomes larger than 1. I believe it has something to do with accuracy of the log function.

for the following values of x:

[ 5.4380181e+11  3.1688845e+12  4.5195668e+13  7.4634268e+14  1.2442192e+16
  2.0754228e+17  3.4624427e+18  5.787843e+19  9.9925409e+20  2.6069079e+22]

I get for x*np.log(1+1/x):

[ 0.99999999  0.99999989  0.99999774  0.9999925  0.99959801  0.99007829
  1.1261964  0.0  0.0  0.0]

As you can see the 7th term is bigger than 1.

I have tried using log1p instead but that gives non-sensical answers.

I have also tried using np.float128() but I get exactly the same results as above.

Any ideas?

Thanks a lot!

1
  • 3
    What have you tried with log1p? It should be able to give the correct result. Commented Jan 28, 2017 at 11:42

1 Answer 1

1

Read the manual page for log1p. It does not take the same argument as log. I guess that explains the "non-sensical answers".

It works for me:

l = [5.4380181e+11, 3.1688845e+12, 4.5195668e+13, 7.4634268e+14, 1.2442192e+16, 2.0754228e+17, 3.4624427e+18, 5.787843e+19, 9.9925409e+20, 2.6069079e+22]

[n*log1p(1/n) for n in l]

Output:

[0.9999999999990805,
0.9999999999998422,
0.9999999999999888,
0.9999999999999992,
1.0,
1.0,
1.0,
1.0,
1.0,
1.0]

Sign up to request clarification or add additional context in comments.

3 Comments

Hi, thanks a lot! Yes that seems to work now, I feel so stupid. However, I get all 1.0, even using np.float128. Do you know why?
@Andrés Glad to help. No need to feel stupid; all programmers do these kinds of mistakes from time to time. Numpy defaults to 8 digit of precision for floating point output, so what you see is not whats actually there :) The numbers are rounded up to 1.0. Try changing Numpy's default with np.set_printoptions (precision=20) or print them an other way.
Awesome! Thanks a lot!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.