round in 2.6 and 2.7

macm moura.mario at gmail.com
Thu Dec 23 14:31:37 EST 2010


On Dec 23, 4:57 pm, Hrvoje Niksic <hnik... at xemacs.org> wrote:
> I stumbled upon this.  Python 2.6:
>
> Python 2.6.6 (r266:84292, Sep 15 2010, 15:52:39)
> [GCC 4.4.5] on linux2
> Type "help", "copyright", "credits" or "license" for more information.>>> 9.95
> 9.9499999999999993
> >>> "%.16g" % 9.95
> '9.949999999999999'
> >>> round(9.95, 1)
>
> 10.0
>
> So it seems that Python is going out of its way to intuitively round
> 9.95, while the repr retains the unnecessary digits.  However, with 2.7
> it's exactly the opposite:
>
> Python 2.7.0+ (r27:82500, Sep 15 2010, 18:04:55)
> [GCC 4.4.5] on linux2
> Type "help", "copyright", "credits" or "license" for more information.>>> 9.95
> 9.95
> >>> "%.16g" % 9.95
> '9.949999999999999'
> >>> round(9.95, 1)
>
> 9.9
>
> Is the change to round() expected?


My guess is

use decimal module.

Regards

mario



More information about the Python-list mailing list