Create an account


Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[Tut] How to Make an Integer Larger Than Any Other Integer in Python?

#1
How to Make an Integer Larger Than Any Other Integer in Python?

This article discusses a situation where you are required to implement an integer object which, when compared using the greater than operator >, to any other existing integer value in the program, will always return True.

Before diving into the possible solutions, we must take note of the following:

Python 2 consists of plain integers and long integers. However, in Python 3, plain and long integers have been merged, and integer in python 3 is the same as long in python 2. Therefore an integer value has no maximum and minimum limits in python 3.

value1=9223372036854775807
print("value1=",value1)
print(type(value1)) value2=value1+1
print("value2=",value2)
print(type(value2))

Output in Python 2:

value1= 9223372036854775807
<type 'int'>
value2= 9223372036854775808L
<type 'long'>

Output in Python 3:

value1= 9223372036854775807 <class 'int'> value2= 9223372036854775808 <class 'int'> 

Note:

In a 64 bit environment, the sys.maxint constant returns the maximum possible plain integer value in python2 which is “9223372036854775807”. Anything higher than this value will be automatically converted to a long type. However, the sys.maxint constant has been removed in python3 since there is no longer a limit to the value of integers. In Python 3 sys.maxsize can be used as an integer larger than any other practical list or string index in a program. The value returned by the sys.maxsize constant depends on the system/platform it is run upon. This means that for a 32-bit platform the value would be 2**31 – 1 = 2147483647, while in a 64-bit platform the value would be 2**63 – 1=9223372036854775807. 

Now let us explore how we can use the greatest integer value in our program:

Method 1: Using a Custom Class 


Everything in python is an “Object”. Also, integers in python3 have no limits. Thus it is safe to say that integers in python are objects with no maximum and minimum limits. Therefore a probable workaround for our problem statement is to create a custom class and return an object that would be greater than any other object in the program.  

import functools
import sys @functools.total_ordering
class AlwaysGreater(object): def __le__(self, other): return False class MaximumInteger(AlwaysGreater, int): def __repr__(self): return 'MaximumInteger()' obj=MaximumInteger()
print(isinstance(obj,int))
print("1. Is obj greater than sys.maxsize?",obj > sys.maxsize)
print("2. Sorting list: [100,0,obj,922337036854775808,sys.maxsize] in ascending order: ")
print(sorted([100,0,obj,9223372036854775808,sys.maxsize]))

Output:

True
1. Is obj greater than sys.maxsize? True 2. Sorting list: [100,0,obj,922337036854775808,sys.maxsize] in ascending order: [0, 100, 9223372036854775807, 9223372036854775808, MaximumInteger()]

Method 2: Using Python Infinity


Positive Infinity in python is an undefined number which is greater than any other value in the program. To represent any number in a program that is higher than all other numbers in the program we can use the python Infinity.

The following code represents the above concept:

import sys
value=float('Inf')
print("1. Is value greater sys.maxsize? :",value>sys.maxsize)
print("2. Sorting list: [100,0,value,922337036854775808,sys.maxsize] in ascending order: ")
print(sorted([100,0,value,9223372036854775808,sys.maxsize]))

Output:

1. Is value greater sys.maxsize? : True 2. Sorting list: [100,0,value,922337036854775808,sys.maxsize] in ascending order: [0, 100, 9223372036854775807, 9223372036854775808, inf]

Disclaimer: As of now there is no way of representing python infinity as an integer value. However, since python float values can be used to represent an infinite integer. Hence int(float(‘Inf’)) will lead to OverflowError: cannot convert float infinity to integer.

You can read more about python infinity here.

Method 3: Using infinity.Infinity 


Another work around for obtaining the largest integer value is using Konsta Vesterinen’s all-in-one infinity value for Python which can be compared to any object. The only issue with this method is it does not inherit from int. To overcome this problem we can create a subclass and then make it inherit from int as given below:

from infinity import Infinity
import sys class IntInfinity(Infinity, int): pass print(isinstance(IntInfinity(), int))
obj = IntInfinity()
print("1. Is obj greater than sys.maxsize?", obj > sys.maxsize)
print("2. Sorting list: [100,0,obj,922337036854775808,sys.maxsize] in ascending order: ")
print(sorted([100, 0,obj, 9223372036854775808, sys.maxsize]))

Output:

True
1. Is obj greater than sys.maxsize? True
2. Sorting list: [100,0,obj,922337036854775808,sys.maxsize] in ascending order:
[0, 100, 2147483647, 9223372036854775808, inf]

Conclusion


It is important to note that integers in python are unbounded. Even the values of sys.maxsize depend upon the platform they are being executed upon as mentioned earlier. This means that sys.maxsize + 1 > sys.maxsize. The proposed methods are probable workarounds for making an integer larger than any other integer in the program.

I hope you found this blog article useful and it helped you. Stay tuned for future updates.



https://www.sickgaming.net/blog/2020/08/...in-python/
Reply



Possibly Related Threads…
Thread Author Replies Views Last Post
  [Tut] Python to .exe – How to Make a Python Script Executable? xSicKxBot 0 1,262 03-13-2023, 01:31 AM
Last Post: xSicKxBot
  [Tut] How to Convert Octal String to Integer in Python xSicKxBot 0 1,297 12-04-2022, 08:39 AM
Last Post: xSicKxBot
  [Tut] Hex String to Hex Integer in Python xSicKxBot 0 1,129 10-30-2022, 12:57 PM
Last Post: xSicKxBot
  [Tut] How to Print a String and an Integer xSicKxBot 0 1,278 08-25-2022, 10:37 AM
Last Post: xSicKxBot
  [Tut] How to Find Number of Digits in an Integer? xSicKxBot 0 1,233 06-23-2022, 04:06 AM
Last Post: xSicKxBot
  [Tut] How to Convert an Integer List to a Float List in Python xSicKxBot 0 1,324 12-15-2020, 01:03 AM
Last Post: xSicKxBot
  [Tut] How to Convert a Float List to an Integer List in Python xSicKxBot 0 1,255 11-11-2020, 06:35 PM
Last Post: xSicKxBot
  [Tut] Python Small Integer Caching: == versus is xSicKxBot 0 1,325 08-24-2020, 03:36 PM
Last Post: xSicKxBot
  [Tut] Python Tuple to Integer xSicKxBot 0 1,389 07-03-2020, 09:13 AM
Last Post: xSicKxBot

Forum Jump:


Users browsing this thread:

Forum software by © MyBB Theme © iAndrew 2016