Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
int vs long
#1
in Python3 i'd like to determine if an int is large enough that its value would have to be long in Python2.
Tradition is peer pressure from dead people

What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Reply
#2
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
Reply
#3
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)
...
Reply
#4
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.
Tradition is peer pressure from dead people

What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Reply
#5
(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)
Reply


Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020