newbie: precision question

Paul Watson paul.hermeneutic at gmail.com
Sat Mar 21 04:31:26 CET 2009


On Sat, 2009-03-21 at 04:12 +0100, Lada Kugis wrote:
> I'm a newbie learning python, so forgive for, what may seem to some,
> like a stupid question.
> 
> I understand the basic integer and fp type, but what I'm having a
> little trouble are the long type and infinite precision type.
> 
> Also, when I do
> 
> >>> math.pi - (math.sqrt(math.pi))**2.
> 
> I get
> 
> >>>4.4408920985006262e-016
> 
> Please, could someone in just a few words, in newbie speak, explain
> why does that happen ? And what do the types actually mean ? What I
> mean, how can something have infinite precision but still not return
> zero as a result. (Btw, I always thought floating points had precision
> up to 8 significant digits, doubles 16).
> 
> Nice regards
> Lada

The types used here are 'float'.

>>> math.pi
3.1415926535897931
>>> type(math.pi)
<type 'float'>

Floating point is inherently imprecise.  Whatever encoding is used to
represent the number will have a finite size.  That finite size puts a
limit on the precision available.

>>> 1.0/3
0.33333333333333331
>>> type(1.0/3)
<type 'float'>

Do you see how small a number the result represents

>>> print "%2.32f" % (4.4408920985006262e-016)
0.00000000000000044408920985006262

Have you tried this in any other programming languages?




More information about the Python-list mailing list