Re: newbie: precision question
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. -- http://mail.python.org/mailman/listinfo/python-list
Re: newbie: precision question
On Mar 20, 11:02�pm, Lada Kugis wrote: > On 21 Mar 2009 03:34:18 GMT, Steven D'Aprano > > wrote: > > 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>>> print 2**1 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. > > 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 > > > > > > >Floats, on the other hand, are effectively the same as double on the C > >compiler for your platform. > > >Does this answer your question? -- http://mail.python.org/mailman/listinfo/python-list
Re: newbie: precision question
On 21 Mar 2009 03:34:18 GMT, Steven D'Aprano wrote: 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 >>> print 2**1 (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 > >Floats, on the other hand, are effectively the same as double on the C >compiler for your platform. > >Does this answer your question? -- http://mail.python.org/mailman/listinfo/python-list
Re: newbie: precision question
On Mar 20, 10:12 pm, 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 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. * 3 = 0. != 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. -- http://mail.python.org/mailman/listinfo/python-list
Re: newbie: precision question
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/ -- http://mail.python.org/mailman/listinfo/python-list
Re: newbie: precision question
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 -- http://mail.python.org/mailman/listinfo/python-list
Re: newbie: precision question
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) 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.1 >>> type(1.0/3) Do you see how small a number the result represents >>> print "%2.32f" % (4.4408920985006262e-016) 0.00044408920985006262 Have you tried this in any other programming languages? -- http://mail.python.org/mailman/listinfo/python-list
Re: newbie: precision question
I think you are looking for this: >>> (math.pi - (math.sqrt(math.pi)))**2 1.8745410610157363 >>> simple, multiplication and division have a higher precedence over addition and subtraction -Alex Goretoy http://www.goretoy.com -- http://mail.python.org/mailman/listinfo/python-list
newbie: precision question
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 -- http://mail.python.org/mailman/listinfo/python-list