# [Tutor] Need more precise digits

dman dsh8290@rit.edu
Sat, 1 Dec 2001 20:45:36 -0500

```On Sat, Dec 01, 2001 at 04:23:13PM -0800, Steve Arnold wrote:

| So either 1 or 2 needs to be a float; but what's with the 1 in the
| following (python2 on redhat 7.2):
|
| >>> float(1./2)
| 0.5
| >>> float(1./3)
| 0.33333333333333331
| >>> float(.1/3)
| 0.033333333333333333
|
| Isn't the last one correct?

They're all correct, to the given precision.

>>> print "%.40f" % (1.0/3)
0.3333333333333333148296162562473909929395
>>> print "%.40f" % ( .1/3 )
0.0333333333333333328707404064061847748235
>>>

floating point is completely accurate, but is limited (as any
representation is) to representing only certain numbers.  Any time it
is asked to represent a number that it can't, it uses the closes
approximation.  Be aware, though, that repetitive arithmatic compounds
the approximation and increases the "uncertainty" in the result.
(uncertainty is also created any time you measure something -- you can
never measure something 100% perfectly accurately and precisely).

For example :

>>> print "%.40f" % .1
0.1000000000000000055511151231257827021182
>>>
>>> i = 0
>>> for blah in xrange( 1000000 ) : i += .1
...
>>> print "%.40f" % i
100000.0000013328826753422617912292480468750000
>>>
>>> print "%.40f" % ( .1 * 1000000 )
100000.0000000000000000000000000000000000000000
>>>

this is one reason that range() doesn't allow float values for start,
stop, or increment.

-D

--

It took the computational power of three Commodore 64s to fly to the moon.
It takes at least a 486 to run Windows 95.
Something is wrong here.

```