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: round() produces incorrect results with certain values
Type: behavior Stage: resolved
Components: Library (Lib) Versions: Python 3.9
process
Status: closed Resolution: not a bug
Dependencies: Superseder:
Assigned To: Nosy List: MrBlueSkies, eric.smith, mark.dickinson, serhiy.storchaka, steven.daprano
Priority: normal Keywords:

Created on 2021-04-21 08:15 by MrBlueSkies, last changed 2022-04-11 14:59 by admin. This issue is now closed.

Messages (6)
msg391499 - (view) Author: Olli (MrBlueSkies) Date: 2021-04-21 08:15
When rounding numbers with round(), middle values at with even base number are rounded in wrong direction.

Python 3.9.1 (default, Feb  3 2021, 07:38:02) 
[Clang 12.0.0 (clang-1200.0.32.29)] on darwin

MacBook Pro (13-inch, 2017, Two Thunderbolt 3 ports)
macOS 11.2.3 (20D91), Darwin 20.3.0

to reproduce (wrong results marked with "*"):

floats:

>>> for i in range(0, 10):
...     i /= 2
...     print(i, '->', round(i))
... 
0.0 -> 0
0.5 -> 0 *
1.0 -> 1
1.5 -> 2
2.0 -> 2
2.5 -> 2 *
3.0 -> 3
3.5 -> 4
4.0 -> 4
4.5 -> 4 *

and for ints:

>>> for i in range(50, 1000, 50):
...     print(i, '->', round(int(i), -2))
... 
50 -> 0 *
100 -> 100
150 -> 200
200 -> 200
250 -> 200 *
300 -> 300
350 -> 400
400 -> 400
450 -> 400 *
500 -> 500
550 -> 600
600 -> 600
650 -> 600 *
700 -> 700
750 -> 800
800 -> 800
850 -> 800 *
900 -> 900
950 -> 1000
msg391500 - (view) Author: Serhiy Storchaka (serhiy.storchaka) * (Python committer) Date: 2021-04-21 08:19
It all works as documented. https://docs.python.org/3/library/functions.html#round
msg391503 - (view) Author: Olli (MrBlueSkies) Date: 2021-04-21 09:27
Just because incorrect results are documented, that doesn't mean the current implementation could not be improved. 

More over, Python 2.7.16 on the same machine produces expected results:

Python 2.7.16 (default, Dec 21 2020, 23:00:36) 
[GCC Apple LLVM 12.0.0 (clang-1200.0.30.4) [+internal-os, ptrauth-isa=sign+stri on darwin

>>> for i in range(0, 10):
...     i = float(i) / 2
...     print i, '->', round(i)
... 
0.0 -> 0.0
0.5 -> 1.0
1.0 -> 1.0
1.5 -> 2.0
2.0 -> 2.0
2.5 -> 3.0
3.0 -> 3.0
3.5 -> 4.0
4.0 -> 4.0
4.5 -> 5.0

While exactly same code on Python 3.9.1 on same platform:

Python 3.9.1 (default, Feb  3 2021, 07:38:02) 
[Clang 12.0.0 (clang-1200.0.32.29)] on darwin

>>> for i in range(0, 10):
...     i = float(i) / 2
...     print(i, '->', round(i))
... 
0.0 -> 0
0.5 -> 0
1.0 -> 1
1.5 -> 2
2.0 -> 2
2.5 -> 2
3.0 -> 3
3.5 -> 4
4.0 -> 4
4.5 -> 4
msg391505 - (view) Author: Serhiy Storchaka (serhiy.storchaka) * (Python committer) Date: 2021-04-21 09:34
It is correct because it works as documented. Please read the documentation carefully.

It was intentional change in Python 3. See https://docs.python.org/3/whatsnew/3.0.html#builtins.
msg391508 - (view) Author: Eric V. Smith (eric.smith) * (Python committer) Date: 2021-04-21 10:20
I agree this is working as designed and documented, so I'm closing this.

Olli: You might want to read https://en.wikipedia.org/wiki/Rounding#Round_half_to_even
msg391511 - (view) Author: Steven D'Aprano (steven.daprano) * (Python committer) Date: 2021-04-21 10:39
I concur with Eric that this should be closed.

They're not "incorrect results", they are correct results, or at least *better* results. We did not invent the so-called Banker's Rounding technique, it has been the standard used by the American Society for Testing and Materials (ASTM) since 1940, and was in common use even before that.

https://infogalactic.com/info/Rounding#History

(Despite the name, there's very little evidence that it has been used by bankers.)

I am sorry that you expected different results and a worse rounding technique. The method of rounding taught in high schools is worse because it leads to bias. I'm sorry that you have been taught a poor rounding technique, but not everyone expects the same results you do.

See previous issue #41598 and likely others.
History
Date User Action Args
2022-04-11 14:59:44adminsetgithub: 88069
2021-04-21 10:39:57steven.dapranosetnosy: + steven.daprano
messages: + msg391511
2021-04-21 10:20:32eric.smithsetstatus: open -> closed

nosy: + eric.smith
messages: + msg391508

resolution: not a bug
stage: resolved
2021-04-21 09:46:02mark.dickinsonsetnosy: + mark.dickinson
2021-04-21 09:34:11serhiy.storchakasetmessages: + msg391505
2021-04-21 09:27:10MrBlueSkiessetmessages: + msg391503
2021-04-21 08:19:40serhiy.storchakasetnosy: + serhiy.storchaka
messages: + msg391500
2021-04-21 08:15:02MrBlueSkiescreate