[Tutor] Idle

Danny Yoo dyoo@hkn.eecs.berkeley.edu
Fri Dec 13 20:33:02 2002


On Fri, 13 Dec 2002, Mike P wrote:

> Newbie here. Is the following normal for Idle?
>
> Python 2.2.2 (#37, Oct 14 2002, 17:02:34) [MSC 32 bit (Intel)] on win32
> Type "copyright", "credits" or "license" for more information.
> IDLE 0.8 -- press F1 for help
> >>> .15
> 0.14999999999999999
> >>> 1.15
> 1.1499999999999999
> >>> 11.15
> 11.15
> >>> 111.15
> 111.15000000000001
> >>>

Hi Mike,

Yes, it's normal: Python's just being truthful when it's showing that it
doesn't represent real numbers.  Computers often use an approximation for
real numbers called "floating point" for practical reasons.

If we're interested, we can look at:

    http://www.python.org/doc/tut/node14.html

which covers these "floating point" numbers in more detail.


If we do want Python to print the number out so that it's more digestible
to our eyes, we can str() "string" it:

###
>>> x = 111.15
>>> x
111.15000000000001
>>> str(x)
'111.15'
###

which is probably more what we expect to see.


Good luck to you!