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

David M. Cooke cookedm at physics.mcmaster.ca
Tue Apr 24 18:42:44 EDT 2007


On Apr 23, 2007, at 21:35 , David M. Cooke wrote:
>
> 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.

Just a followup note: gcc (4.0 and 4.2, at least) with -std=c99  
*doesn't* respect this; it still does the platform way (on my MacBook  
Core Duo, that's truncation towards zero). The assembly it generates  
is identical for gcc -std=c99 and gcc -std=c89

-- 
|>|\/|<
/------------------------------------------------------------------\
|David M. Cooke              http://arbutus.physics.mcmaster.ca/dmc/
|cookedm at 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://mail.python.org/pipermail/numpy-discussion/attachments/20070424/35c526cb/attachment.sig>


More information about the NumPy-Discussion mailing list