Skip to content

AttributeError when taking sine of huge number #13875

Closed

Description

@cool-RR
import numpy as np
np.sin(70**11)

Error message:

Traceback (most recent call last):                 
  File "a.py", line 2, in <module>                 
    np.sin(70**11)                                 
AttributeError: 'int' object has no attribute 'sin'

Numpy/Python version information:

1.16.4 3.7.2 (tags/v3.7.2:9a3ffc0492, Dec 23 2018, 23:09:28) [MSC v.1916 64 bit (AMD64)]

Activity

  1. eric-wieser commented on Jun 30, 2019

    @eric-wieser
    Member

    Fixed in #12700 to produce a better error message, now gives:

    >>> np.sin(70**11)
    AttributeError: 'int' object has no attribute 'sin'
    
    The above exception was the direct cause of the following exception:
    
    TypeError: loop of ufunc does not support argument 0 of type int which has no callable sin method
    

    Passing a float as np.sin(70.0**11) will work fine

  2. cool-RR commented on Jun 30, 2019

    @cool-RR
    Author

    Maybe I'm missing something, but the "fixed" version still seems like a completely opaque error message.

    Someone runs np.sin(7) and it works, and suddenly for a big number it doesn't work. Shouldn't the error message be something like Input is too large to calculate sin, use float?

  3. seberg commented on Jun 30, 2019

    @seberg
    Member

    Until we get rid of automatic coercion to an object array this is probably about as good as it will get. We could maybe make the error message spell out a bit more things, but we cannot just put in "input is too large" or so, at the point where the error message happens that would just be guessing. Although maybe you can suggest a wording which makes things easier.

  4. cool-RR commented on Jun 30, 2019

    @cool-RR
    Author

    Can't the error catching logic check whether this is an int which is above a certain threshold, and then use the "int is too large" message?

  5. eric-wieser commented on Jun 30, 2019

    @eric-wieser
    Member

    No, because np.sin(np.array(1, dtype=object)) has the same problem.

    The cause here is:

    • 70**11 is not an array, so is coerced to one
    • 70**11 is too large to fit in an int64 array, so an object array types is chosen
    • np.sin(object_array) is only supported if the objects within have a sin method
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions