Ask your own question, for FREE!
MIT 6.00 Intro Computer Science (OCW) 24 Online
OpenStudy (anonymous):

In lecture 5, Prof. Guttag discusses some of the peculiarities of how Python handles floating point numbers. For example assigning x to 0.1 and then asking the interpreter to return the value of x returns 0.10000000000000001 . This happens in Python 2.6.6 but not in 2.7.2 (where just 0.1 is returned). Does anyone know what was changed between these two versions of Python, and why?

OpenStudy (anonymous):

>>> "%.70f" % 0.1 '0.1000000000000000055511151231257827021181583404541015625000000000000000' Try this in 2.6.6 and see what you get. I think it'll probably be the same. I'm using IDLE (I'll assume you are, too). They probably didn't change the implementation of float (that's a standard), so it might be something like they changed how IDLE displays floats. If you execute that command and get a different string, that will be interesting.

OpenStudy (anonymous):

Oh. My point being that under the covers python is probably handling floats the same. It's just how IDLE decides to display them by default might have changed.

OpenStudy (anonymous):

Python 2.6.6 (r266:84292, Sep 13 2010, 15:22:35) [GCC 4.2.1 20070719 [FreeBSD]] on freebsd8 Type "help", "copyright", "credits" or "license" for more information. >>> "%.70f"%0.1 Traceback (most recent call last): File "<stdin>", line 1, in <module> OverflowError: formatted float is too long (precision too large?)

OpenStudy (anonymous):

Dial that 70 back until it gives you something.

OpenStudy (anonymous):

Based on that output, does it look like 2.7.2 support longer floats? Incidentally, what is that command you had my type actually doing? (Dividing a string by a float?)

OpenStudy (anonymous):

>>> "%.66f"%0.1 '0.100000000000000005551115123125782702118158340454101562500000000000'

OpenStudy (anonymous):

lol. No. I don't know exactly what to call it. It's a format string, and the the '%' operator, and then a tuple of replacement parameters for the format string. '%f' will format its argument as a float, and the '.70' tells it to use 70 digits of precision when doing so. And you're giving it 0.1 as the value to format.

OpenStudy (anonymous):

My rough inspection says that those values are the same. That means that the value of 0.1 hasn't changed from python 2.6.6 to 2.7.2. That's a relief. So it's just that when you tell it to display a floating point number with the default formatting, it truncates at a different point. The real value underneath is still the same.

OpenStudy (anonymous):

So "%.66f" <- that's a format string (haven't heard of these before) % <- modulus operator 0.1 <- value to format Where is a tuple involved?

OpenStudy (anonymous):

When the tuple has a single value you can omit the parens. (I don't know if that's in general or just in this case.) To be completely explicit it should be "%.66f" % (0.1) If you had a format string that took lots of arguments it would be more obvious: print "n=%d, i=%d, ratio=%2.2f" % (n, i, float(i)/n) (This syntax will make your debug print statements easier to write.)

OpenStudy (anonymous):

Sorry, I know what a format string is, just getting confused about the vocabulary. So without those replacement parameters the characters in the string would be printed exactly as they are?

OpenStudy (anonymous):

Yes. Actually, it's the % operator that makes it all happen. I guess a string "mod" a tuple is another string (where the formats are replaced by values). A string that's just a string is...just a string.

OpenStudy (anonymous):

Ah, got it. So, the first % operator in the string corresponds to the zeroth element of the tuple. Way off OT there :)

OpenStudy (anonymous):

Yes. If you want to go exploring inside a float http://docs.python.org/library/math.html#math.frexp might help. Like I said float is a standard, and python is just one of many many languages that implements it. The standard hasn't changed, so I don't think python would have changed its handling of them. But I *can* see IDLE maybe deciding to print them differently for the user (in the absence of any specific formatting instructions).

OpenStudy (anonymous):

those floating point results are not 'peculiar' to Python : http://en.wikipedia.org/wiki/Floating_point#Accuracy_problems

OpenStudy (anonymous):

Right - I was confused by getting different answers from different versions of the interpreter.

Can't find your answer? Make a FREE account and ask your own questions, OR help others and earn volunteer hours!

Join our real-time social learning platform and learn together with your friends!
Can't find your answer? Make a FREE account and ask your own questions, OR help others and earn volunteer hours!

Join our real-time social learning platform and learn together with your friends!