9
Python 2.7.3 (v2.7.3:70274d53c1dd, Apr 9 2012, 20:52:43) 
[GCC 4.2.1 (Apple Inc. build 5666) (dot 3)] on darwin
Type "copyright", "credits" or "license()" for more information.
>>> 4.2 - 1.8
2.4000000000000004
>>> 1.20 - 1.18
0.020000000000000018
>>> 5.1 - 4
1.0999999999999996
>>> 5 - 4
1
>>> 5.0 - 4.0
1.0

Why is Python getting its maths wrong?

Karl Knechtel
61.4k14 gold badges131 silver badges193 bronze badges
asked Aug 14, 2012 at 10:56
10
  • 3
    Read the section 14.1 of this link: docs.python.org/tutorial/floatingpoint.html Commented Aug 14, 2012 at 10:58
  • 4
    Python maths is not wrong. You don't understand the representation of floating-point numbers on computers nor the arithmetic of such numbers. Read the resources suggested by other commenters. Commented Aug 14, 2012 at 11:03
  • 6
    Python maths is wrong, mathematically speaking. It's just that being perfectly right all the time is computationally infeasible (numbers like sqrt(2) aren't even representable in a finite amount of space without directly coding them as things like sqrt(2)). The errors in Python's floating point arithmetic have been accepted as "the way it is done" for a very long time, and we just have to live with that as a matter of pragmatism. It is very very important for programmers to be aware of this, but we all have this moment of confusion. Commented Sep 19, 2012 at 6:09
  • 2
    All the delete voters, please don't delete a question that serves as a great signpost to users experiencing similar problems Commented Jun 11, 2013 at 6:04
  • 1
    @david just because Python changed the way they choose to round a number for display doesn't mean the problem went away, just that they're doing a better job of hiding it. Nothing was actually fixed. And in case I wasn't clear, this has nothing to do with being based on C libraries, it's just the way processors work. I agree that many people don't understand the way binary floating point works, but that's unlikely to change as binary floating point is so unintuitive. Commented Apr 28, 2014 at 3:26

2 Answers 2

389

You have reached a new level in computer science, and you are coming of age.

You therefore are now ready for the next step. I have been authorized by the BDFL himself to reveal the following Super Secret document to you. The ancients understood it and deciphered it first, and now, so will you!

The Floating Point Guide

Treat this document with care! Only share this with people you know have reached the same baffling conclusions!


Moderator's Note

This answer is not representative of the expected quality standards on Stack Overflow. However, it has unexpectedly developed a life of its own and is solely preserved for historical significance now.

answered Aug 14, 2012 at 11:04
Sign up to request clarification or add additional context in comments.

8 Comments

what is BDFL.?!
Can you please summarize the link you've included? Link only answers are generally frowned upon.
@MartijnPieters It's a wide topic sure, but it would benefit the site to have some context to the link. I mean, 79 other people could be wrong, but I stand with them in saying this answer should be improved.
@TankorSmash: you are missing the whole point here. Note that any anonymous visitors arriving here (having googled "Python math is wrong") are redirected to the canonical post already, where all that information is already present. This post was never meant to be a serious answer, and it has started a life of its own. Like the parsing HTML with regex answer, this is not something entirely different. Summarising the linked content would materially alter that nature.
|
54

For a painfully-rigorous guide to how floating-point arithmetic works, complete instructions on how to calculate how large the errors in your answers will be, you need:

What Every Computer Scientist Should Know About Floating-Point Arithmetic

Once you've read that, you should go and look for notes on the 8087 specifically, because it implements IEEE 754 poorly and this can cause new exciting problems in your life.

answered Aug 14, 2012 at 12:13

2 Comments

IEEE 754 came to life after 8087. It seems harsh to criticise 8087 for failing to meet a standard that did not exist when it was invented!
The 8087 was used as the basis for IEEE754. The 80387 and its successors then implemented IEEE 754.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.