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.

Title: round() does not
Type: behavior Stage:
Components: Windows Versions: Python 2.5
Status: closed Resolution: not a bug
Dependencies: Superseder:
Assigned To: Nosy List: georg.brandl, gvanrossum, travelgirl
Priority: normal Keywords:

Created on 2007-12-05 19:58 by travelgirl, last changed 2022-04-11 14:56 by admin. This issue is now closed.

File name Uploaded Description Edit
python bug.jpg travelgirl, 2007-12-05 19:58
Messages (5)
msg58225 - (view) Author: travelgirl (travelgirl) Date: 2007-12-05 19:58 states, as an example, round(_, 2)
should round to the second decimal place.  makes sense.  when i
attempted it with different numbers, round() returned what looks like a
binary-coded decimal error (see the jpg enclosed), and did not return
just two digits, but 15 digits.  the same error was produced for
round(_, 3).  when the number was changed to 1 digit, the return result
rounded from another direction.

i'd guess this isn't a feature :)
msg58227 - (view) Author: Georg Brandl (georg.brandl) * (Python committer) Date: 2007-12-05 20:31
Not really a feature, but not a bug. See
for an explanation.
msg58230 - (view) Author: travelgirl (travelgirl) Date: 2007-12-05 21:01
interesting, and expected as the BCD calculation.  however, the round function should be able to handle that correctly, dealing with the number in a string fashion as opposed a mathematical one, to display the requested decimal places.  slower, to be sure, but hella accurate.

that's what the bug was about.  i understand the issues with BCD.  i don't understand how a function to display a number can get the display so wrong, even when the BCD number itself "fails" to the 15th place.  round to 2 places, you should still display 14.78, not 14.7799999999999999x

just my take.  thanks for getting back to me.

00 caren
george davis creek, north fork
msg58231 - (view) Author: Georg Brandl (georg.brandl) * (Python committer) Date: 2007-12-05 21:09
round() does not return a string, but a floating point number, the best
approximation to the "real" result that is achievable with the limited
precision of binary floating point.

That result is then formatted to a string for display by the interactive
interpreter using the repr() function, which formats a float with (I
think) 17 decimal digits.

So, the point here is that repr() produces "too many" digits for your
liking ;) You can use str() to cut off at fewer digits, which will
produce the "correct" string in most cases.
msg58235 - (view) Author: Guido van Rossum (gvanrossum) * (Python committer) Date: 2007-12-06 00:40
travelgirl: the 'decimal' module is designed to deal with this. Note
that there's a difference between BCD (which can represent decimal
numbers exactly) and binary (which can't, in general).
Date User Action Args
2022-04-11 14:56:28adminsetgithub: 45900
2007-12-06 00:40:39gvanrossumsetnosy: + gvanrossum
messages: + msg58235
2007-12-05 21:09:25georg.brandlsetmessages: + msg58231
2007-12-05 21:01:33travelgirlsetmessages: + msg58230
2007-12-05 20:31:01georg.brandlsetstatus: open -> closed
resolution: not a bug
messages: + msg58227
nosy: + georg.brandl
2007-12-05 19:58:11travelgirlcreate