[Python-bugs-list] int() broken? (PR#370)
M.-A. Lemburg
mal@lemburg.com
Mon, 26 Jun 2000 10:56:27 +0200
wzdd@sesgroup.net wrote:
>
> Full_Name: Nicholas FitzRoy-Dale
> Version: 1.5.2
> OS: Linux (Debian, Red Hat)
> Submission from: cpe-24-192-0-130.vic.bigpond.net.au (24.192.0.130)
>
> The following typescript illustrates the problem. I understand int() truncates
> towards zero - but can't see why it would round down by a whole int value!
>
> The same behaviour is apparent on a Red Hat 6.2 system, where Python was built
> with egcs.
>
> ~$ python
> Python 1.5.2 (#0, Apr 3 2000, 14:46:48) [GCC 2.95.2 20000313 (Debian
> GNU/Linux)] on linux2
> Copyright 1991-1995 Stichting Mathematisch Centrum, Amsterdam
> >>> test=591.92
> >>> test*100
> 59192.0
> >>> int (test*100)
> 59191
> >>>
>
> ... and yet:
> >>> int (test*100+0.1)
> 59192
> >>>
While waiting for Tim Peters to explain the details,
here's the obvious part of the answer:
>>> test = 591.92
>>> '%.17f' % test
'591.91999999999995907'
>>> '%.17f' % (test*100)
'59191.99999999999272404'
>>> '%.17f' % (test*100+0.1)
'59192.09999999999126885'
--
Marc-Andre Lemburg
______________________________________________________________________
Business: http://www.lemburg.com/
Python Pages: http://www.lemburg.com/python/