New submission from Mark Dickinson <dicki...@gmail.com>:

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

----------
assignee: georg.brandl
components: Documentation
messages: 94886
nosy: georg.brandl, mark.dickinson
severity: normal
stage: needs patch
status: open
title: Document 2.x -> 3.x round changes in "Whats New" documents.

_______________________________________
Python tracker <rep...@bugs.python.org>
<http://bugs.python.org/issue7261>
_______________________________________
_______________________________________________
Python-bugs-list mailing list
Unsubscribe: 
http://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com

Reply via email to