1

I'm using function: numpy.log(1+numpy.exp(z))

for small values of z (1-705) it gives identity result(1-705 {as expected}), but for larger value of z from 710+ it gives infinity, and throw error "runtimeWarning: overflow encountered in exp"

2
  • For all practical purposes, I'd say log(1 + exp(705)) is approximately 705. The +1 makes nearly no difference because e^705 is just so huge. You can just catch the error and set the value to be equal to z. Commented Mar 9, 2017 at 19:14
  • I'm aware of that, but the problem is with exp(x), for larger value>709 it gives infinity @Praveen Commented Mar 9, 2017 at 19:20

2 Answers 2

4

For large z you could use

z + log(exp(-z) + 1)

which is mathematically but not numerically the same thing.

In code:

(z + np.log(np.exp(-z) + 1)) if z > 0 else np.log(1 + np.exp(z))

If you need a vectorised version:

np.maximum(z, 0) + np.log(np.exp(-np.absolute(z)) + 1)

As @Praveen points out there is a function np.log1p which calculates log(1+x) but is more accurate if |x| is small. I recommend using it in the above.

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

1 Comment

Consider using np.log1p instead of np.log to compute np.log(np.exp(-z) + 1)...
0

What is happening is that you are overflowing the register such that it is overwriting itself. You have exceeded the maximum value that can be stored in the register. You will need to use a different datatype that will most likely not be compatible with exp(.). You might need a custom function that works with 64-bit Integers.

Comments

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.