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: maths float error
Type: behavior Stage:
Components: Versions: Python 3.2
process
Status: closed Resolution: not a bug
Dependencies: Superseder:
Assigned To: Nosy List: ipcctv, mark.dickinson
Priority: normal Keywords:

Created on 2010-11-28 15:50 by ipcctv, last changed 2022-04-11 14:57 by admin. This issue is now closed.

Files
File name Uploaded Description Edit
tip.py ipcctv, 2010-11-28 15:50 Code snippet of float problem
Messages (2)
msg122667 - (view) Author: Tom Quaile (ipcctv) Date: 2010-11-28 15:50
Using IDLE 3.2a4
Apologies in advance for probably wasting your time. If I'm wrong, just ignore me. I'm very new to Python. Is this a bug, my processor or me? I'm sending this in as I see it's an alpha release.

If the user supplies 100 as the answer, I would have expected a result of 115 for a 15% tip. I get 114.999999999.
msg122676 - (view) Author: Mark Dickinson (mark.dickinson) * (Python committer) Date: 2010-11-28 16:26
> I get 114.999999999.

How many '9's are there on what you're getting?  If you really get '114.999999999' then something surprising is happening.  If, on the other hand, you get '114.99999999999999', then it's all as expected. :-)

The issue is one that affects the vast majority of popular programming languages, namely that numbers are stored internally in binary, so a number like 1.15 isn't exactly representable---you end up storing a (very good) approximation to 1.15 instead.  And then when you multiply by 100, you end up with a (very good) approximation to 115.0.

I highly recommend reading the floating-point section of the Python tutorial for an explanation of what's going on under the hood.

http://docs.python.org/dev/py3k/tutorial/floatingpoint.html
History
Date User Action Args
2022-04-11 14:57:09adminsetgithub: 54773
2010-11-28 16:26:24mark.dickinsonsetstatus: open -> closed
resolution: not a bug
messages: + msg122676
2010-11-28 16:19:33mark.dickinsonsetnosy: + mark.dickinson
2010-11-28 15:50:33ipcctvcreate