You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
http://docs.python.org/tut/node5.html 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.
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
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.
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).
Activity
travelgirl commentedon Dec 5, 2007
http://docs.python.org/tut/node5.html 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 :)
birkenfeld commentedon Dec 5, 2007
Not really a feature, but not a bug. See
http://www.python.org/doc/faq/general/#why-are-floating-point-calculations-so-inaccurate
for an explanation.
travelgirl commentedon Dec 5, 2007
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
http://www.parkgallery.org
george davis creek, north fork
birkenfeld commentedon Dec 5, 2007
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.
gvanrossum commentedon Dec 6, 2007
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).