classification
Title: Function Round does not work properly in some conditions
Type: behavior Stage: committed/rejected
Components: Versions: Python 2.6
process
Status: closed Resolution: invalid
Dependencies: Superseder:
Assigned To: Nosy List: paontis, r.david.murray
Priority: normal Keywords:

Created on 2010-09-17 11:36 by paontis, last changed 2010-09-17 14:35 by paontis. This issue is now closed.

Files
File name Uploaded Description Edit
round.txt paontis, 2010-09-17 11:36 examples of round results
Messages (3)
msg116656 - (view) Author: (paontis) Date: 2010-09-17 11:36
For example round(10.3333, 1) returns 10.300000000000001 and round(1.3333, 2)returns 1.3300000000000001

I exect they return 10.3 and 1.33 rispectively

NOTE: other combinations work fine eg. round(10.3333, 2) or round(1.3333, 3)

See IDLE commands in the attached file round.txt

Used Python 2.6.6 for Windows
msg116669 - (view) Author: R. David Murray (r.david.murray) * (Python committer) Date: 2010-09-17 14:27
Please read

    http://docs.python.org/tutorial/floatingpoint.html

Although your case isn't directly covered there, the root cause is the same.  Floating point can't exactly represent 10.3.

Note that in Python2.7 and 3.x, the repr will be shortened to 10.3, so if you'd tried this there you would never have noticed the underlying issue.

If you need exact decimal arithmetic, use the Decimal module.
msg116670 - (view) Author: (paontis) Date: 2010-09-17 14:35
ok thx very much for the explaination
History
Date User Action Args
2010-09-17 14:35:56paontissetmessages: + msg116670
2010-09-17 14:27:58r.david.murraysetstatus: open -> closed

nosy: + r.david.murray
messages: + msg116669

resolution: invalid
stage: committed/rejected
2010-09-17 11:36:16paontiscreate