Help me please : Rounding-down numbers

Erik Max Francis max at alcyone.com
Thu Mar 27 07:33:15 CET 2003


Bengt Richter wrote:

> On Wed, 26 Mar 2003 18:19:14 -0800, Erik Max Francis <max at alcyone.com>
> wrote:
>
> > Note that math.floor and int don't have quite the same behavior
> > (glossing over the obvious difference that the former returns a
> > float)
> > for the case of negative numbers.
>
> E.g.,
> 
>  >>> map(int,[-1.9, -1.5, -1.1,  -0.5, 0.0, 0.5, 1.1, 1.5, 1.9])
>  [-1, -1, -1, 0, 0, 0, 1, 1, 1]
>  >>> from math import floor
>  >>> map(floor,[-1.9, -1.5, -1.1,  -0.5, 0.0, 0.5, 1.1, 1.5, 1.9])
>  [-2.0, -2.0, -2.0, -1.0, 0.0, 0.0, 1.0, 1.0, 1.0]

Indeed.  Technically, float(int(x)) rounds toward zero, whereas
math.floor(x) rounds toward negative infinity.

-- 
 Erik Max Francis / max at alcyone.com / http://www.alcyone.com/max/
 __ San Jose, CA, USA / 37 20 N 121 53 W / &tSftDotIotE
/  \ If love is the answer, could you rephrase the question?
\__/ Lily Tomlin
    CatCam / http://www.catcam.com/
 What do your pets do all day while you're at work?  Find out.




More information about the Python-list mailing list