newbie: precision question

Steven D'Aprano steve at REMOVE-THIS-cybersource.com.au
Fri Mar 20 23:34:18 EDT 2009


On Sat, 21 Mar 2009 04:12:48 +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).

Floats in Python don't have infinite precision.

Ints (or longs) can have infinite precision. Try calculating (say) 
1234567**315*24689 and you should get 1923 digits. By the way, you can do 
calculations on ints with tens of thousands of digits *much* faster than 
you can print those same ints: on my computer, calculating all 75199 
digits of 1234567**12345 takes less than an eye-blink, while printing it 
takes ages.

Floats, on the other hand, are effectively the same as double on the C 
compiler for your platform.

Does this answer your question?



-- 
Steven



More information about the Python-list mailing list