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?

<div><p>This article discusses a situation where you are required to implement an <strong>integer</strong> object which, when compared using the greater than operator <strong>&gt;</strong>, to any other existing integer value in the program, will always return <strong>True</strong>.&nbsp;</p>
<p>Before diving into the possible solutions, we must take note of the following:</p>
<p>Python 2 consists of <strong>plain integers</strong> and <strong>long integers. </strong>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.</p>
<pre class="EnlighterJSRAW" data-enlighter-language="generic" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">value1=9223372036854775807
print("value1=",value1)
print(type(value1)) value2=value1+1
print("value2=",value2)
print(type(value2))</pre>
<p><strong>Output in Python 2:</strong></p>
<pre class="EnlighterJSRAW" data-enlighter-language="generic" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">value1= 9223372036854775807
&lt;type 'int'>
value2= 9223372036854775808L
&lt;type 'long'> </pre>
<p><strong>Output in Python 3:</strong></p>
<pre class="EnlighterJSRAW" data-enlighter-language="generic" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">value1= 9223372036854775807 &lt;class 'int'> value2= 9223372036854775808 &lt;class 'int'> </pre>
<p><strong>Note:</strong></p>
<p>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<strong> </strong>= 2147483647, while in a 64-bit platform the value would be 2**63 – 1=9223372036854775807. </p>
<p>Now let us explore how we can use the greatest integer value in our program:</p>
<h2>Method 1: Using a Custom Class </h2>
<p>Everything in python is an “<em>Object”. </em>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.  </p>
<pre class="EnlighterJSRAW" data-enlighter-language="generic" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">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]))</pre>
<p><strong>Output:</strong></p>
<pre class="EnlighterJSRAW" data-enlighter-language="generic" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">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()]</pre>
<h2>Method 2: Using Python Infinity</h2>
<p>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.</p>
<p>The following code represents the above concept:</p>
<pre class="EnlighterJSRAW" data-enlighter-language="generic" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">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]))</pre>
<p><strong>Output:</strong></p>
<pre class="EnlighterJSRAW" data-enlighter-language="generic" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">1. Is value greater sys.maxsize? : True 2. Sorting list: [100,0,value,922337036854775808,sys.maxsize] in ascending order: [0, 100, 9223372036854775807, 9223372036854775808, inf]</pre>
<p><em>Disclaimer:</em> 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.</p>
<p>You can read more about python infinity <a href="https://blog.finxter.com/python-infinity/">here</a>.</p>
<h2>Method 3: Using infinity.Infinity </h2>
<p>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:</p>
<pre class="EnlighterJSRAW" data-enlighter-language="generic" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">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]))</pre>
<p><strong>Output:</strong></p>
<pre class="EnlighterJSRAW" data-enlighter-language="generic" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">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]</pre>
<h2>Conclusion</h2>
<p>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 &gt; sys.maxsize. The proposed methods are probable workarounds for making an integer larger than any other integer in the program.&nbsp;</p>
<p>I hope you found this blog article useful and it helped you. Stay tuned for future updates.</p>
</div>


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



Forum Jump:


Users browsing this thread:
2 Guest(s)

Forum software by © MyBB Theme © iAndrew 2016