Message94886
The round builtin function changed in three fairly significant ways
between 2.x and 3.x:
(1) In 2.x, halfway cases are rounded using the
round-half-away-from-zero rule, while in 3.x they're
rounded using round-half-to-even.
(2) The single argument version, round(x), generally returns an
integer in 3.x and a float in 2.x. (For float inputs, this
actually changed between 3.0 and 3.1, not between 2.x and 3.x.).
(3) In 3.x, round(x, n) delegates to x.__round__; in 2.x, x is simply
converted to a float (if possible). (So for example, rounding
a Decimal instance in 2.x gives a float result; in 3.x it uses
Decimal.__round__, which returns another Decimal.)
I think all of this is covered (more-or-less) in the 'built-in
functions' section of the documentation, but I suggest that it would be
worth putting an entry for these changes into the 'What's new in Python
3.0' document as well.
The first change above is particularly evil (i.e., likely to cause
late-discovered bugs), since the value of round(x) will change silently,
just for a few values of x. Note that this change affects
integers, too, so it's applicable beyond the "well you should be
using Decimal then" situations.
[2.5]
>>> int(round(2500, -3))
3000
[3.1]
>>> round(2500, -3)
2000 |
|
Date |
User |
Action |
Args |
2009-11-04 10:33:14 | mark.dickinson | set | recipients:
+ mark.dickinson, georg.brandl |
2009-11-04 10:33:13 | mark.dickinson | set | messageid: <1257330793.87.0.85024867948.issue7261@psf.upfronthosting.co.za> |
2009-11-04 10:33:04 | mark.dickinson | link | issue7261 messages |
2009-11-04 10:33:02 | mark.dickinson | create | |
|