Rounding Question

Mikael Olofsson mikael at isy.liu.se
Thu Feb 22 08:37:56 EST 2001


I corrected Remco:
 >  I'm sorry, but this also behaves badly. It rounds 10 up to 20. To get
 >  Jacob's desired functionality, you could modify your idea to
 >  
 >    rounded = number-((number-1)%10)+9
 >  
 >  still assuming only integer input.


Alex Martelli wrote:
 >  ...which of course is totally equivalent to the earlier
 >  proposed
 >      rounded = (number+9) - ((number+9)%10)
 >  since addition is commutative and associative (you can
 >  move the +9 from the right of the expression) AND so
 >  is addition in modulo-arithmetic, so that:
 >  
 >  (X-1)%N==(X%N)+((-1)%N)==(X%N)+((N-1)%N)==(X+N-1)%N

Of course! I just couldn't stay silent when Remco stumbled again.

/Mikael

-----------------------------------------------------------------------
E-Mail:  Mikael Olofsson <mikael at isy.liu.se>
WWW:     http://www.dtr.isy.liu.se/dtr/staff/mikael               
Phone:   +46 - (0)13 - 28 1343
Telefax: +46 - (0)13 - 28 1339
Date:    22-Feb-01
Time:    14:35:06

         /"\
         \ /     ASCII Ribbon Campaign
          X      Against HTML Mail
         / \

This message was sent by XF-Mail.
-----------------------------------------------------------------------



More information about the Python-list mailing list