Thread: Big Integers

    #1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2012
    Posts
    5
    Rep Power
    0

    Question 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
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Intermediate (1500 - 1999 posts)

    Join Date
    Feb 2004
    Location
    San Francisco Bay
    Posts
    1,939
    Rep Power
    1313
    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.
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2012
    Posts
    5
    Rep 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
  6. #4
  7. No Profile Picture
    Contributing User
    Devshed Intermediate (1500 - 1999 posts)

    Join Date
    Feb 2004
    Location
    San Francisco Bay
    Posts
    1,939
    Rep Power
    1313
    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.)
  8. #5
  9. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2012
    Posts
    5
    Rep Power
    0

    Post 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.
  10. #6
  11. Banned ;)
    Devshed Supreme Being (6500+ posts)

    Join Date
    Nov 2001
    Location
    Woodland Hills, Los Angeles County, California, USA
    Posts
    9,592
    Rep Power
    4207
    Until about version 2.2 or so, python used to distinguish between int and long (bignum) types:
    Code:
    x = 1
    y = 1L
    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/
    Last edited by Scorpions4ever; September 19th, 2012 at 04:21 AM.
    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

    "I wouldn't hire a butcher to fix my car. I also wouldn't hire a marketing firm to build my website." - Nilpo

IMN logo majestic logo threadwatch logo seochat tools logo