[Python-Dev] trunc()

Christian Heimes lists at cheimes.de
Fri Jan 25 15:11:22 CET 2008


Paul Moore wrote:
> On 24/01/2008, Jeffrey Yasskin <jyasskin at gmail.com> wrote:
>> int has to be a builtin because it's a fundamental type. trunc()
>> followed round() into the builtins. I have no opinion on whether ceil
>> and floor should move there; it probably depends on how often they're
>> used.
> 
> Suggestion:
> 
> - int() has to stay in builtins for obvious reasons.
> - put *all* of trunc, ceil, floor, round into math.
> - make int(float) an error

Slightly different suggestion:
 - define int(float) as int(trunc(float))

In my humble opinion lots of people expect int(-2.5) == -2 and int(2.5)
== 2. Or in other words: int(float) chops of the digits after the dot.

As far as I can see float.__int__ already behaves like int(trunc(float))

>>> for n in (2.4, 2.6, -2.4, -2.6):
...     print(n, (math.floor(n), math.ceil(n), round(n), trunc(n), int(n)))
...
Python 3:0

 2.4 ( 2,  3,  2,  2,  2)
 2.6 ( 2,  3,  3,  2,  2)
-2.4 (-3, -2, -2, -2, -2)
-2.6 (-3, -2, -3, -2, -2)

Python 2.6:
 2.4 ( 2.0,  3.0,  2.0,  2,  2)
 2.6 ( 2.0,  3.0,  3.0,  2,  2)
-2.4 (-3.0, -2.0, -2.0, -2, -2)
-2.6 (-3.0, -2.0, -3.0, -2, -2)

Christian



More information about the Python-Dev mailing list