[Python-Dev] trunc()

Raymond Hettinger python at rcn.com
Fri Jan 25 20:22:12 CET 2008


> If the decision comes to be that int(float) should be blessed
> as a correct way to truncate a float, I'd agree with Raymond 
> that trunc() is just duplication and should be eliminated.

Yay, we've make progress!


> I'd,of course, rather have a spelling that says what it means. :)

I wouldn't fret about this too much.  Intrepreting int(f) as
meaning truncate has a *long* history in *many* programming
languages.  It is a specious argument int(f) is ambiguous.  
No one thinks it means ceil(f).  

Go ask a dozen people if they are surprised that int(3.7) returns 3.
No one will be surprised (even folks who just use Excel or VB). It
is foolhardy to be a purist and rage against the existing art:

SQL: "The INT() function returns its numeric argument with any fractional
digits removed and truncates all digits to the right of the decimal
point."
www.basis.com/onlinedocs/documentation/b3odbc/bbjds_int_function.htm 

VB: "Both the Int and Fix functions remove the fractional part of
Number and return the resulting integer value."
http://msdn2.microsoft.com/en-us/library/xh29swte.aspx

Excel: "The Int function returns the integer portion of a number."
http://www.techonthenet.com/excel/formulas/int.php

These docs suggest where the thinking has gone wrong.  Writing int(f)
doesn't mean "arbritrary select one of round|ceil|floor|trunc as
a way of getting to an integer"; instead, it means "return the
integer portion (non-fractional component) of a number."  The
latter definition seems common and is entirely unambiguous.


Raymond


More information about the Python-Dev mailing list