3

I'm relatively new to Python, and reading through a tutorial page on the documentation website, I came across this snippet:enter image description here This made me curious, so I decided to type it into a Python file and test it out. When I did this, however, it gave me a different result:

   .1+.1+.1 == .3
=> True

This question may seem trivial, but I'm curious why the actual behavior did not match what the Python documentation said it would do. Any answers?

This behavior occured while using an online interpreter. Running it locally returned False.

16
  • 5
    The keyword here is may not. It depends on the floating point implementation of your platform. Commented Apr 19, 2013 at 15:30
  • 2
    No, they should not. Code is never run in isolation, what comes out depends on what you put in, and running code on Windows can have different results from running the same code on Unix systems, etc. Commented Apr 19, 2013 at 15:32
  • 3
    ieee-754 is a good starting point to understand what can and cannot happen under the hood. Commented Apr 19, 2013 at 15:33
  • 3
    @LeeDanielCrocker, as tempting as that would be, there are cases when floating point comparisons are really valid, for example when you want to know if a value changed from some previous saved value. Commented Apr 19, 2013 at 16:01
  • 3
    This is another one of those questions that seems to be a duplicate of the countless "I don't get floating point" questions, but in fact has a twist. First, the OP has at least done the basic reading, and is even expecting floating point problems. Second, it turns out his environment was really at fault, not a misunderstanding of floats per se. So it's not truly a duplicate in my mind. However, is it worth trying to reopen this question just to get rid of its scarlet letter? That I'm not sure. Commented Apr 23, 2013 at 21:43

2 Answers 2

2

You never said which version of Python you're running, and that can make a huge difference. The arithmetic is likely to be IEEE based doubles which should be consistent from system to system. However CPython is based on underlying C libraries, and those can vary in the way they round floating-point constants as they're input. Other versions of Python will also be dependent on some underlying platform.

Edit: Confirmed. Using the online interpreter given in the question I get:

   '%0.20f' % (.1+.1+.1,)
=> '0.30000000000000004441'
   '%0.20f' % (.3,)
=> '0.30000000000000004441'

Using Python 2.7 on Windows:

>>> '%0.20f' % (.1+.1+.1,)
'0.30000000000000004441'
>>> '%0.20f' % (.3,)
'0.29999999999999998890'

It appears the online interpreter rounds the input differently.

Sign up to request clarification or add additional context in comments.

4 Comments

I'm running Python 3.3 -- does Python 3 handle floating point numbers differently than Python 2?
@jarednielsen, I wouldn't expect so but that's good info, thanks.
As of version 2.7 and above, CPython has its own code for string to float and float to string conversions; the platform shouldn't made a difference.
... and it turns out that it's exactly the platform that's the problem. I retract that last comment. Apologies.
1

First comment is the answer. On my system:

Python 2.7.3 (default, Sep 26 2012, 21:53:58) 
[GCC 4.7.2] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> 0.1+0.1+0.1 == 0.3
False
>>>

from python docs (http://docs.python.org/2/tutorial/floatingpoint.html):

Binary floating-point arithmetic holds many surprises like this. The problem with “0.1” is explained in precise detail below, in the “Representation Error” section. See The Perils of Floating Point for a more complete account of other common surprises.

4 Comments

Thanks. Do you know what factors would cause the code to return True on one computer and False on another?
What does it matter? You won't realistically be able to do anything about the inconsistency, and anyway the proper and correct programming practice is to write the code in a way that doesn't care about the result of things like this.
@jarednielsen added link to article in answer
@KarlKnechtel, I think it's proper to question why your results should deviate from the official documentation no matter what best practice is.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.