Issue 10564: maths float error (original) (raw)
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.
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.