Python Forum

Full Version: int vs long
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
in Python3 i'd like to determine if an int is large enough that its value would have to be long in Python2.
but long integers in python 2 are not the same as a double in 'C':
Quote:Long integers have unlimited precision. Floating point numbers are usually implemented using double in C
from: https://docs.python.org/2/library/stdtypes.html
Here is what I got in a python 2 console
Python 2.7.12 (default, Dec  4 2017, 14:50:18) 
[GCC 5.4.0 20160609] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import sys
>>> sys.maxint
9223372036854775807
>>> sys.maxint + 1
9223372036854775808L
>>> sys.maxint + 1 == 2 ** 63
True
>>> - sys.maxint - 1
-9223372036854775808
>>> - sys.maxint - 2
-9223372036854775809L
So a good test seems to be
>>> def int_suffices(n):
...     return - 2 ** 63 <= n < 2 ** 63
...
# OR
>>> def int_suffices(n): return n >> 63 in (-1, 0)
...
i guess i have to consider that a Pyton3 implementation might do it all different than a Pythin2 implementation. i was looking to know a limit to run at for speed, but this could vary by implementation, all else being the same, about the only thing i can count on is that the limit won't be less than 127.
(Jan-29-2018, 05:56 AM)Skaperen Wrote: [ -> ]this could vary by implementation
In python 3, you could perhaps get a more general result with
import sys
BLEN = sys.maxsize.bit_length()
def int_suffices(n):
    return n >> BLEN in (-1, 0)