newbie: precision question

L

Lada Kugis

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

I get

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
 
P

Paul Watson

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


I get


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'.
<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.
<type 'float'>

Do you see how small a number the result represents
0.00000000000000044408920985006262

Have you tried this in any other programming languages?
 
S

Steven D'Aprano

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


I get


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?
 
S

Steve Holden

Lada said:
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


I get
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
 
L

Lada Kugis

On 21 Mar 2009 03:34:18 GMT, Steven D'Aprano

Hello Steven,
thanks for answering on such short notice,
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.

Yes, I've tried things like(gives a large, really mind-boggingly huge big number :)

So, if I understood you right, integers can have infinite precision
and floating points are up to 16 digits (if I remember anything they
thought me in my C classes) because they cannot be represented exactly
in binary form. C representation, can of course vary according to
platform, compiler etc.

Normal integers are up to 10 digits, after which they become long
integers, right ?

But if integers can be exactly represented, then why do they need two
types of integers (long and ... uhmm, let's say, normal). I mean,
their error will always be zero, no matter what kind they're of.



Lada
 
A

AggieDan04

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


I get


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

Mensanator

On 21 Mar 2009 03:34:18 GMT, Steven D'Aprano


Hello Steven,
thanks for answering on such short notice,





Yes, I've tried things like>>> print 2**10000

I usually use the gmpy library as it tends to be better
than native Python and includes a lot of stuff Python
doesn't have.

Incredible as it seems, you CAN get carried away with
such capability. This function, from my Collatz library:

# a = (i-1)*9**(k-1) + (9**(k-1) - 1)/2 + 1
# return 2**(6*a - 1) - 1

grows VERY quickly. At (6,1) the number has 55,338 decimal
digits (takes several pages of 11x17 paper printed at 6 point font).
I can't even do (11,1) because I get an "Outrageous Exponet"
exception (attemps to create 2 raised to the power of over 2
billion).
(gives a large, really mind-boggingly huge big number :)

So, if I understood you right, integers can have infinite precision

Keep in mind you are still limited by how much RAM you have,
although with today's computers that's not an issue...most of the
time.
"Arbitrary" is probably a better term than "infinite".
and floating points are up to 16 digits

Some libraries (such as gmpy) can give you arbitrary
precision floats if you need them. But gmpy also has
arbitrary precision rationals. These are worth checking out.
In my usage, all calculations are done in rationals, which
prevents any rounding errors. Only when I need it displayed
do I convert it to an arbitrary precision float.
(if I remember anything they
thought me in my C classes) because they cannot be represented exactly
in binary form. C representation, can of course vary according to
platform, compiler etc.

Normal integers are up to 10 digits, after which they become long
integers, right ?

The conversion is automatic, you usually don't have to be concerned
with overflow with integers.
 
M

MRAB

Lada Kugis wrote:
[snip]
Normal integers are up to 10 digits, after which they become long
integers, right ?

But if integers can be exactly represented, then why do they need two
types of integers (long and ... uhmm, let's say, normal). I mean,
their error will always be zero, no matter what kind they're of.
'int' is limited to, say, 32 bits, but is faster. 'long' is slower, but
virtually unlimited. The decision was made that with the speed of modern
CPUs we could simplify things by forgetting about 'int' and using just
'long', though renamed to 'int', in Python 3.x.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,211
Messages
2,571,100
Members
47,695
Latest member
KayleneBee

Latest Threads

Top