The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.
|
 |
|
Dev Shed Forums
> Programming Languages
> Python Programming
|
Big Integers
Discuss Big Integers in the Python Programming forum on Dev Shed. Big Integers Python Programming forum discussing coding techniques, tips and tricks, and Zope related information. Python was designed from the ground up to be a completely object-oriented programming language.
|
|
 |
|
|
|
|

Dev Shed Forums Sponsor:
|
|
|

September 14th, 2012, 04:12 AM
|
|
Registered User
|
|
Join Date: Sep 2012
Posts: 5
Time spent in forums: 53 m
Reputation Power: 0
|
|
Big Integers
IndentationError: unexpected indent
>>> for i in range(0,128):
... x = 1 << i
... print "%03d: 0x%X" % (i, x)
and eventually the last print show this ...
124: 0x10000000000000000000000000000000
125: 0x20000000000000000000000000000000
126: 0x40000000000000000000000000000000
127: 0x80000000000000000000000000000000
I am used to 32 and 64 bit integers, but here Python seems to be happily dealing with 128 bit integers. I was completely supprised that this works, it mus mean Python is storing values in some very wierd way. Can someone tell me whats going on here and how this works
|

September 14th, 2012, 04:44 AM
|
|
Contributing User
|
|
Join Date: Feb 2004
Location: San Francisco Bay
|
|
|
Integers in Python are theoretically unbounded (though of course, they are actually bounded by the memory available on the machine). Technically, there are separate 'int' and 'long' types, but the distinction is essentially transparent, and you usually don't need to be aware of it.
|

September 14th, 2012, 05:02 AM
|
|
Registered User
|
|
Join Date: Sep 2012
Posts: 5
Time spent in forums: 53 m
Reputation Power: 0
|
|
|
Big Integers
Thanks, yes I have a "C" mind set. I am writing code to test hardware that is 96 bits wide. Python is great being able to read 3 32 bit values and lets me do ...
bits = ((x<<64) | (y <<32) | z)
I was blown away when that worked. Great language
|

September 15th, 2012, 01:10 AM
|
|
Contributing User
|
|
Join Date: Feb 2004
Location: San Francisco Bay
|
|
Quote: | Originally Posted by julianh I was blown away when that worked. Great language | Python is famously one of the most programmer-friendly (and machine-unfriendly) programming languages out there. I see it in a way as a reaction to the crap that a lot of other languages make you put up with, though I don't know if that's any sort of official rationale. (I heard Guido van Rossum speak on Python a few years ago, and as I recall, he didn't spend time bashing other languages.)
|

September 19th, 2012, 02:50 AM
|
|
Registered User
|
|
Join Date: Sep 2012
Posts: 5
Time spent in forums: 53 m
Reputation Power: 0
|
|
Big Integers
Interesting man/machine trade off you mention. Do all man friendly languages need to be difficult for a computer to execute ? On the one hand after many years of "C" programming my way of thinking has adapted and it still feels easier for me than Python which is unfamiliar. I am also reminded of the time I spent programming in Lisp on Symbolics hardware designed to execute it directly. Lisp is a pretty awsome language if you are into expert systems.
|

September 19th, 2012, 04:12 AM
|
 |
Banned ;)
|
|
Join Date: Nov 2001
Location: Woodland Hills, Los Angeles County, California, USA
|
|
Until about version 2.2 or so, python used to distinguish between int and long (bignum) types:
In older versions of python, it was possible to overflow x because it was an integer type and it would throw an OverflowError exception. y is a long/bignum variable and could be incremented as long as there's enough memory in the machine.
In newer versions of python (2.2 and above), the conversion happens automatically, so you can't overflow an int anymore. If the value can't be represented as an int, it will automatically promote it to a long type. So there's no need to add an 'L' at the end to initialize a variable as a long type any more.
The modification was discussed here:
http://www.python.org/dev/peps/pep-0237/
__________________
Up the Irons
What Would Jimi Do? Smash amps. Burn guitar. Take the groupies home.
"Death Before Dishonour, my Friends!!" - Bruce D ickinson, Iron Maiden Aug 20, 2005 @ OzzFest
Down with Sharon Osbourne
Last edited by Scorpions4ever : September 19th, 2012 at 04:21 AM.
|
Developer Shed Advertisers and Affiliates
| Thread Tools |
Search this Thread |
|
|
|
| Display Modes |
Rate This Thread |
Linear Mode
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|
|