# [NumPy-Tickets] [NumPy] #1882: power function in NumPy returns wrong values withing the dtype range

NumPy Trac numpy-tickets@scipy....
Tue Jun 28 13:43:01 CDT 2011

```#1882: power function in NumPy returns wrong values withing the dtype range
-------------------------+--------------------------------------------------
Reporter:  abx_numpy    |       Owner:  somebody
Type:  defect       |      Status:  new
Priority:  normal       |   Milestone:  Unscheduled
Component:  numpy.core   |     Version:  1.6.0
Keywords:  numpy.power  |
-------------------------+--------------------------------------------------

Old description:

> numpy.power(a,i) returns wrong values even when they are within the range
> determined by dtype. Example:
>
> >>> import math, sys
> >>> import numpy as np
> >>> a = range(41,56,2)
> >>> b = np.array(a); b.dtype
> dtype('int64')
> >>> for x in a:
> ...   print x, ':', x**10 - math.pow(x,10)
> ...
> 41 : 0.0
> 43 : 0.0
> 45 : 0.0
> 47 : 0.0
> 49 : 0.0
> 51 : 0.0
> 53 : 0.0
> 55 : 0.0
> >>> for x in b:
> ...   print x, ':', x**10 - np.power(x,10)
> ...
> 41 : 1
> 43 : 1
> 45 : 1
> 47 : 1
> 49 : 1
> 51 : -7
> 53 : -7
> 55 : -15
> >>> # the resuls are wrong only for odd integers:
> >>> c = np.array([56],dtype=np.int64)
> >>> print c[0], ':', 56**10 - (np.power(c,10))[0]
> (56, ':', 0)
> >>> d = np.array([57],dtype=np.int64)
> >>> print d[0], ':', 57**10 - (np.power(d,10))[0]
> (57, ':', -15)
> >>> # I am on 64-bit machine (MacBookPro, Intel, 10.6)
> >>> 57**10 < sys.maxint
> True
> >>> sys.maxint - (2**63 - 1)
> >>> 0L
> >>> i = int(d[0])
> >>> i**10 - d[0]**10
> 0
> >>> i**10 - (d**10)[0]
> -15
>
> The last two outputs indicate that this is likely the ufunc problem.

New description:

numpy.power(a,i) returns wrong values even when they are within the range
determined by dtype. Example:
{{{
>>> import math, sys
>>> import numpy as np
>>> a = range(41,56,2)
>>> b = np.array(a); b.dtype
dtype('int64')
>>> for x in a:
...   print x, ':', x**10 - math.pow(x,10)
...
41 : 0.0
43 : 0.0
45 : 0.0
47 : 0.0
49 : 0.0
51 : 0.0
53 : 0.0
55 : 0.0
>>> for x in b:
...   print x, ':', x**10 - np.power(x,10)
...
41 : 1
43 : 1
45 : 1
47 : 1
49 : 1
51 : -7
53 : -7
55 : -15
>>> # the resuls are wrong only for odd integers:
>>> c = np.array([56],dtype=np.int64)
>>> print c[0], ':', 56**10 - (np.power(c,10))[0]
(56, ':', 0)
>>> d = np.array([57],dtype=np.int64)
>>> print d[0], ':', 57**10 - (np.power(d,10))[0]
(57, ':', -15)
>>> # I am on 64-bit machine (MacBookPro, Intel, 10.6)
>>> 57**10 < sys.maxint
True
>>> sys.maxint - (2**63 - 1)
>>> 0L
>>> i = int(d[0])
>>> i**10 - d[0]**10
0
>>> i**10 - (d**10)[0]
-15
}}}
The last two outputs indicate that this is likely the ufunc problem.

--

Comment(by pv):

The reason is that `numpy.power` is effectively implemented as:
{{{
np.int64(math.pow(1.0*x, 10))
}}}
This needs to be fixed, though.

--
Ticket URL: <http://projects.scipy.org/numpy/ticket/1882#comment:1>
NumPy <http://projects.scipy.org/numpy>
My example project
```