newbie: precision question

AggieDan04 danb_83 at yahoo.com
Sat Mar 21 00:03:31 EDT 2009


On Mar 20, 10:12 pm, Lada Kugis <lada.kugis@@gmail.com> 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

An "int" is limited to 32 or 64 bits.  A "long" can be as big as you
need it to be.

People didn't like OverflowErrors very much, so Python 2.2 allowed
operations on ints to return longs, and Python 3.0 will phase out
"int" entirely, but "long" is being renamed to "int".

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

Because "float" isn't an infinite-precision type.  Therefore, it has
rounding errors.

Doesn't even have anything to do with the binary number system.  You
may familar with decimal calculators in which (1/3) * 3 = 0.33333333 *
3 = 0.99999999 != 1.  Same kind of thing.

> (Btw, I always thought floating points had precision
> up to 8 significant digits, doubles 16).

Single and double precision are *both* floating point.  It's just that
C and Python disagree on which of those the name "float" refers to.



More information about the Python-list mailing list