HELP: restore my faith in Python

Grant Edwards grant at nowhere.
Mon Mar 6 10:51:29 EST 2000


In article <38C373C4.3EBB62D8 at sightreader.com>, Ken Seehof wrote:

>Seems to me an improvement would be to have python artificially
>add epsilon to floats before converting to int.  This would
>still suck but IMHO it would suck less.
>
>Yeah, I know it's artificial, but it's not rocket science.  Everyone
>knows that int(0.99999999999999999999993427) == 1 is more
>correct than int(0.99999999999999999999993427) == 0.  

Not when the definition of int(x) is to convert x to an integer
by truncation.  Last time I checked, that was the definition.

>I challenge anyone to come up with a real world situation where
>the latter answer is more valuable.

In the real world, all of the other languages I've ever used in
the past 25 years converted float->int via truncation.  Making
Python the exception to the rule would be, IMO, A Bad
Thing(TM).

If you want to round to the nearest integer, then use
int(x+0.5).  That's been idiomatic in programming for longer
than I've been around.

-- 
Grant Edwards                   grante             Yow!  ... The waitress's
                                  at               UNIFORM sheds TARTAR SAUCE
                               visi.com            like an 8" by 10" GLOSSY...



More information about the Python-list mailing list