newbie: precision question

Steve Holden steve at holdenweb.com
Sat Mar 21 04:47:39 CET 2009


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.
> 
Longs are essentially unbounded integers. You can store a larger number
in a long than you can in a floating-point number! Fortunately the
Python developers have gone to great lengths to ensure you do not need
to know more than this to use them.

What infinite precision type? If you mean decimal, that doesn't have
hardware support and so is much slower than floating-point, which uses
the underlying hardware of the CPU to perform arithmetic.

> Also, when I do
> 
>>>> math.pi - (math.sqrt(math.pi))**2.
> 
> I get
> 
>>>> 4.4408920985006262e-016
> 
Which is to say, something very small. This is a result of what are
known as "rounding errors". See

  http://en.wikipedia.org/wiki/Rounding_error

It's because there is a bound to the precision of the floating-point
representations used for arithmetic.

> 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).

Suppose we were to deal with a simple decimal computer that stored
numbers only to two decimal places. If we computed the square root of
sixteen we would get an exact result (4.00) which, when squared, would
give us back exactly sixteen (or 16.00).

The story would be somewhat different were I to try to take the square
root of twelve: the most accurate result I could represent would be
3.46. when I square that I get 11.9716, or (to two decimal places)
11.97. So 12 - (sqrt(12)**2) != 0.

Python uses doubles for all floating-point calculations.

I hope this has cleared up your questions.

regards
 Steve
-- 
Steve Holden           +1 571 484 6266   +1 800 494 3119
Holden Web LLC                 http://www.holdenweb.com/
Want to know? Come to PyCon - soon! http://us.pycon.org/




More information about the Python-list mailing list