[Numpy-discussion] Oddity with numpy.int64 integer division

David M. Cooke cookedm@physics.mcmaster...
Mon Apr 23 20:35:20 CDT 2007


On Apr 23, 2007, at 16:41 , Christian Marquardt wrote:
> On Mon, April 23, 2007 22:29, Christian Marquardt wrote:
>> Actually,
>>
>> it happens for normal integers as well:
>>
>>>>> n = np.array([-5, -100, -150])
>>>>> n // 100
>>    array([ 0, -1, -1])
>>>>> -5//100, -100//100, -150//100
>>    (-1, -1, -2)
>
> and finally:
>
>>>> n % 100
>    array([95,  0, 50])
>>>> -5 % 100, -100 % 100, -150 % 100
>    (95, 0, 50)
>
> So plain python / using long provides consistent results across //
> and %, but numpy doesn't...

Python defines x // y as returning the floor of the division, and x %  
y has the same sign as y. However, in C89, it is implementation- 
defined (i.e., portability-pain-in-the-ass) whether the floor or ceil  
is used when the signs of x and y differ. In C99, the result should  
be truncated. From the C99 spec, sec. 6.5.5, #6:
        When integers are divided, the result of the / operator
        is   the   algebraic   quotient  with  any  fractional  part
        discarded.76)  If the quotient  a/b  is  representable,  the
        expression (a/b)*b + a%b shall equal a.

Numpy follows whatever the C compiler decides to use, because of  
speed-vs.-Python compatibility tradeoff.

>
>   Christian.
>
>> On Mon, April 23, 2007 22:20, Christian Marquardt wrote:
>>> Dear all,
>>>
>>> this is odd:
>>>
>>>>>> import numpy as np
>>>>>> fact = 28250000L * 86400L
>>>>>> nn = np.array([-20905000L])
>>>>>> nn
>>>    array([-20905000], dtype=int64)
>>>>>> nn[0] // fact
>>>    0
>>>
>>> But:
>>>
>>>>>> long(nn[0]) // fact
>>>    -1L
>>>
>>> Is this a bug in numpy, or in python's implementation of longs? I  
>>> would
>>> think both should give the same, really... (Python 2.5, numpy
>>> 1.0.3dev3725,
>>> Linux, Intel compilers...)
>>>
>>> Many thanks for any ideas / advice,
>>>
>>>   Christian

-- 
|>|\/|<
/------------------------------------------------------------------\
|David M. Cooke              http://arbutus.physics.mcmaster.ca/dmc/
|cookedm@physics.mcmaster.ca

-------------- next part --------------
A non-text attachment was scrubbed...
Name: PGP.sig
Type: application/pgp-signature
Size: 186 bytes
Desc: This is a digitally signed message part
Url : http://projects.scipy.org/pipermail/numpy-discussion/attachments/20070423/f166593d/attachment.bin 


More information about the Numpy-discussion mailing list