Skip to content

np.exp raises AttributeError when called with large integer #11407

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
Dominik1123 opened this issue Jun 21, 2018 · 3 comments
Open

np.exp raises AttributeError when called with large integer #11407

Dominik1123 opened this issue Jun 21, 2018 · 3 comments

Comments

@Dominik1123
Copy link

Summary

np.exp raises AttributeError: 'int' object has no attribute 'exp' when called with an integer greater than 2**64-1.

Expected behavior

Return np.inf as for other large integer values (e.g. for 2**63).

Assumption

Such integers are treated internally as object and hence numpy tried to call the exp-function on the instance (similar to np.exp(np.asarray([2], dtype=object))). However this is completely unexpected since in Python 3 all integers have the same type (nonetheless the must be stored differently in memory).

System

$ uname -a
Linux MyPC 4.4.0-128-generic #154-Ubuntu SMP Fri May 25 14:15:18 UTC 2018 x86_64 x86_64 x86_64 GNU/Linux
$ python
Python 3.6.2 (default, Jul 17 2017, 23:14:31) 
[GCC 5.4.0 20160609] on linux

Code Example

>>> from numpy import exp
>>> exp(2**64)  # Would expect np.inf as return value.
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
AttributeError: 'int' object has no attribute 'exp'
>>> exp(2**63)  # Here it does.
__main__:1: RuntimeWarning: overflow encountered in exp
inf
>>> type(2**63) is type(2**64)
True
@eric-wieser
Copy link
Member

Implicit conversion to array strikes again:

>>> np.array(2**63).dtype
dtype('uint64')
>>> np.array(2**64).dtype
dtype('O')

@eric-wieser
Copy link
Member

eric-wieser commented Jun 22, 2018

in Python 3 all integers have the same type

This is very not true in numpy, which has 10 different types of integer. Unfortunately, pythons int can be too big to fit in any of them.

The only sensible way for us to support np.exp(2**64) is to fix np.exp(np.array(1, dtype=object)). There are open PRs about that (#10820 is related, for instance)

@eric-wieser
Copy link
Member

Thanks to #12700, now raises TypeError

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants