0

There is an array with large input elements named A. I want to define another array, named B, that its elements are obtained by the exponential of the negative of elements in A. I use the following code: B=np.exp(-A) The resulted elements in B are very small that in python they are shown equal to zero. How I can define the precision so that I don't get zero for the elements?

Thanks for your guidance in advance, Zahra

2
  • Please provide sample input and expected output data. Commented Jan 10, 2020 at 13:30
  • If there is more than 17-18 digits they're not going to be accurate anyway and this is due to how they are stored under the IEEE-754 “double precision” EDIT: link for python3.7 instead of 3.2 Commented Jan 10, 2020 at 13:33

1 Answer 1

2

You can try specifying the data type.

a = np.array([1000], dtype='f8') # 64-bit float
b = np.exp(-a)
b
>>> array([0.])

a = np.array([1000], dtype='f16') # 128 bit float
b = np.exp(-a)
b
>>> array([5.0759589e-435], dtype=float128)

Note the comment of @Plopp, depending on how large your values of a are this may not be enough

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

1 Comment

Thank you for your guide. I tried it but the elements' values are too large and based on @Ploppa's comment, putting dtype='f16' didn't work. Is there any other way?

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.