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?
>>> "%.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.
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.
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?)
Dial that 70 back until it gives you something.
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?)
>>> "%.66f"%0.1 '0.100000000000000005551115123125782702118158340454101562500000000000'
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.
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.
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?
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.)
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?
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.
Ah, got it. So, the first % operator in the string corresponds to the zeroth element of the tuple. Way off OT there :)
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).
http://docs.python.org/library/stdtypes.html#string-formatting-operations http://docs.python.org/library/string.html#string-formatting http://docs.python.org/library/string.html#format-string-syntax http://docs.python.org/library/string.html#formatspec
those floating point results are not 'peculiar' to Python : http://en.wikipedia.org/wiki/Floating_point#Accuracy_problems
Right - I was confused by getting different answers from different versions of the interpreter.
Join our real-time social learning platform and learn together with your friends!