This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: Rounding 5,50,500 behaves differently depending on preceding value
Type: behavior Stage: resolved
Components: Interpreter Core Versions: Python 3.10
process
Status: closed Resolution: not a bug
Dependencies: Superseder:
Assigned To: Nosy List: Spencer Brown, adam.ulrich, steven.daprano
Priority: normal Keywords:

Created on 2022-01-22 02:36 by adam.ulrich, last changed 2022-04-11 14:59 by admin. This issue is now closed.

Messages (3)
msg411222 - (view) Author: Adam Ulrich (adam.ulrich) Date: 2022-01-22 02:36
round(250,-2) returns 200
round(350,-2) returns 400
round(450,-2) returns 400
round(550,-2) returns 600
round(5,-1) returns 0
round(15,-1) returns 20
round(500,-3) returns 0
round(1500,-3) returns 2000

expected: round of 5 to consistently rounds up.
msg411223 - (view) Author: Spencer Brown (Spencer Brown) * Date: 2022-01-22 02:58
This is intentional behaviour, if the value is 5 it rounds towards even as documented here: https://docs.python.org/3/library/functions.html#round
The reason is so that if you have a bunch of random data, rounding won't slightly bias the result upward or downward.
msg411227 - (view) Author: Steven D'Aprano (steven.daprano) * (Python committer) Date: 2022-01-22 03:29
As documented, this is not a bug.

"if two multiples are equally close, rounding is done toward the even choice"

https://docs.python.org/3/library/functions.html#round

This is also called "Banker's Rounding" or "Round half to even" and for rounding many values, it minimizes the expected total rounding error.

https://en.wikipedia.org/wiki/Rounding#Round_half_to_even
History
Date User Action Args
2022-04-11 14:59:55adminsetgithub: 90625
2022-01-22 03:29:09steven.dapranosetstatus: open -> closed

nosy: + steven.daprano
messages: + msg411227

resolution: not a bug
stage: resolved
2022-01-22 02:58:28Spencer Brownsetnosy: + Spencer Brown
messages: + msg411223
2022-01-22 02:36:11adam.ulrichcreate