Hi, I'm having some troubles with long.
from numpy import log log(8463186938969424928L) 43.5822574833 log(10454852688145851272L) <type 'exceptions.AttributeError'>: 'long' object has no attribute 'log'
Thoughts?
On Jan 26, 2008 12:39 AM, Tom Johnson <tjhnson@gmail.com> wrote:
Hi, I'm having some troubles with long.
from numpy import log log(8463186938969424928L) 43.5822574833 log(10454852688145851272L) <type 'exceptions.AttributeError'>: 'long' object has no attribute 'log'
Numpy uses the standard C functions, so can't represent very large integers exactly. However, given the precision of the log function, it might be reasonable to truncate the digits and write the Python long as a float before conversion. That's what Python does. In [6]: import math In [7]: math.log(10454852688145851272L) Out[7]: 43.793597916587672 In [8]: float(10454852688145851272L) Out[8]: 1.045485268814585e+19 Chuck
Tom Johnson wrote:
Hi, I'm having some troubles with long.
from numpy import log log(8463186938969424928L)
43.5822574833
log(10454852688145851272L)
<type 'exceptions.AttributeError'>: 'long' object has no attribute 'log'
The problem is that the latter long integer is too big to fit into an int64 (long long) and so it converts it to an object array. The default behavior of log on object arrays is to look for a method on each element of the array called log and call that. Your best bet is to convert to double before calling log log(float(10454852688145851272L)) Travis O.
On Sat, Jan 26, 2008 at 5:49 AM, Travis E. Oliphant <oliphant@enthought.com> wrote:
Tom Johnson wrote:
Hi, I'm having some troubles with long.
from numpy import log log(8463186938969424928L)
43.5822574833
log(10454852688145851272L)
<type 'exceptions.AttributeError'>: 'long' object has no attribute 'log'
The problem is that the latter long integer is too big to fit into an int64 (long long) and so it converts it to an object array. The default behavior of log on object arrays is to look for a method on each element of the array called log and call that.
Your best bet is to convert to double before calling log
log(float(10454852688145851272L))
Travis O.
Related, I understand that problem which occurs below...
x = 8463186938969424928L y = 10454852688145851272L import numpy z = numpy.float_(3) x * z 2.53895608169e+19 y * z TypeError: unsupported operand type(s) for *: 'long' and 'numpy.float64' numpy.float_(y) * z 3.13645580644e+19 y * float(z) 3.1364558064437551e+19
A couple points.... 1) With log, we get an AttributeError...with multiplication, we get a TypeError. I know the mechanism which causes the problem is different but the fundamental problem (too large of longs) is the same in both cases. Can this be improved upon? 2) The extra digits from python floats are nice....can numpy have these as well? 3) I think it is safe to say that many people cannot know ahead of time if their longs will be larger than 64bit. This whole situation seems unstable to me...code that seems to be working will work, and then when the longs (from python) get too large we get a variety of different exceptions. So, I wonder aloud: Is this being handled is the nicest/preferred way? I'd be happy if my extremely large longs were automatically converted to numpy.float64_....even if we don't have as many significant digits as the equivalent pure python result. At least with this method, I will not have code "randomly" breaking. Either that, or am I required to be extremely careful about mixing types.
participants (3)

Charles R Harris

Tom Johnson

Travis E. Oliphant