Issue 36082: The built-in round() function giving a wrong output (original) (raw)

Issue36082

Created on 2019-02-22 17:02 by Goodester, last changed 2022-04-11 14:59 by admin. This issue is now closed.

Messages (4)
msg336329 - (view) Author: Juho Pesonen (Goodester) Date: 2019-02-22 17:02
As the title says I have got some wrong outputs with the round() built-in function. The bug occurs only with certain numbers though. (I've had problems only with the number 2.5, but I assume that there is more than one number that gives wrong output.) Here are some outputs with the round built-in function: >>>round(2.5) 2 >>>round(2.51) 3 >>>round(3.5) 4 >>>round(1.5) 2 As you can see the number 2.5 gives 2 instead of 3.
msg336330 - (view) Author: Karthikeyan Singaravelan (xtreak) * (Python committer) Date: 2019-02-22 17:07
https://docs.python.org/3/whatsnew/3.0.html > The round() function rounding strategy and return type have changed. Exact halfway cases are now rounded to the nearest even result instead of away from zero. (For example, round(2.5) now returns 2 rather than 3.) round(x[, n]) now delegates to x.__round__([n]) instead of always returning a float. It generally returns an integer when called with a single argument and a value of the same type as x when called with two arguments.
msg336331 - (view) Author: SilentGhost (SilentGhost) * (Python triager) Date: 2019-02-22 17:08
This is the documented behaviour. It might seem counter-intuitive, but it's is not likely to change.
msg336333 - (view) Author: Rémi Lapeyre (remi.lapeyre) * Date: 2019-02-22 17:24
This is called banker rounding and is done on purpose to limit the accumulation of errors when summing a list of rounded integers. You can read https://en.m.wikipedia.org/wiki/Rounding#Round_half_to_even and look at IEEE 754 for more information.
History
Date User Action Args
2022-04-11 14:59:11 admin set github: 80263
2019-02-22 17:24:47 remi.lapeyre set nosy: + remi.lapeyremessages: +
2019-02-22 17:08:55 SilentGhost set status: open -> closednosy: + SilentGhostmessages: + resolution: not a bugstage: resolved
2019-02-22 17:07:34 xtreak set nosy: + xtreakmessages: +
2019-02-22 17:02:23 Goodester create