round off

John Baxter news.collectivize at scandaroon.com
Tue Nov 4 16:58:46 EST 2003


In article <mailman.441.1067976033.702.python-list at python.org>,
 "W. Vanweersch" <n.vanweersch at hccnet.nl> wrote:

> Hello,
> 
> The following looks strange to me in Python:
> 7/-3 gives -3
> 
> It looks like Python rounds off -2.33333 to -3! is this correct??
> Isn't it -2?

If you like, but not here. (It's really truncation, not rounding.)


It can be unexpected.  It keeps this sort of thing working:

>>> (-7 / 3)* 3 + (-7 % 3)
-7

Truncate / toward - infinity.  If it truncated toward zero, then you 
have to mess with the sign of the result of %, and other odd things 
happen.

  --John

-- 
Email to above address discarded by provider's server.  Don't bother sending.




More information about the Python-list mailing list