Can anyone explain to me why I receive an error(AtrributeError) in NumPy when I do numpy.sin(2**64), but not when I do numpy.sin(2.0**64), numpy.sin(float(2**64)) or even numpy.sin(2)? Where is the root problem in all of this?
On Thu, Aug 6, 2009 at 17:01, Nanime Puloski<npuloski@gmail.com> wrote:
Can anyone explain to me why I receive an error(AtrributeError) in NumPy when I do numpy.sin(2**64), but not when I do numpy.sin(2.0**64), numpy.sin(float(2**64)) or even numpy.sin(2)?
Where is the root problem in all of this?
numpy deals with objects that can be natively represented by the C numerical types on your machine. On your machine 2**64 gives you a Python long object which cannot be converted to one of the native C numerical types, so numpy.sin() treats it as a regular Python object. The default implementation of all of the ufuncs when faced with a Python object it doesn't know about is to look for a method on the object of the same name. long.sin() does not exist, so you get an AttributeError. -- Robert Kern "I have come to believe that the whole world is an enigma, a harmless enigma that is made terrible by our own mad attempt to interpret it as though it had an underlying truth." -- Umberto Eco
On 6-Aug-09, at 6:01 PM, Nanime Puloski wrote:
Can anyone explain to me why I receive an error(AtrributeError) in NumPy when I do numpy.sin(2**64), but not when I do numpy.sin(2.0**64), numpy.sin(float(2**64)) or even numpy.sin(2)?
In [6]: type(2**64) Out[6]: <type 'long'> In [7]: type(2) Out[7]: <type 'int'> In [8]: type(2.0**64) Out[8]: <type 'float'> Probably because 2**64 yields a long, which is an arbitrary precision type in Python, and numpy is having trouble casting it to something it can use (it's too big for numpy.int64). Notice that np.sin(2**63 - 1) works fine while np.sin(2**63) doesn't - 2**63 - 1 is the largest value that an int64 can hold. You're right that it could be approximately represented as a float32 or float64, but numpy won't cast from exact types to inexact types without you telling it to do so. I agree that the error message is less than ideal. David
participants (3)
-
David Warde-Farley
-
Nanime Puloski
-
Robert Kern