Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to make an integer larger than any other integer?

Note: while the accepted answer achieves the result I wanted, and @ecatmur answer provides a more comprehensive option, I feel it's very important to emphasize that my use case is a bad idea in the first place. This is explained very well in @Jason Orendorff answer below.

Note: this question is not a duplicate of the question about sys.maxint. It has nothing to do with sys.maxint; even in python 2 where sys.maxint is available, it does NOT represent largest integer (see the accepted answer).

I need to create an integer that's larger than any other integer, meaning an int object which returns True when compared to any other int object using >. Use case: library function expects an integer, and the only easy way to force a certain behavior is to pass a very large integer.

In python 2, I can use sys.maxint (edit: I was wrong). In python 3, math.inf is the closest equivalent, but I can't convert it to int.

like image 418
max Avatar asked Oct 04 '16 03:10

max


People also ask

What's bigger than an integer?

If you need to hold an integer larger than the Integer data type can hold, you can use the Long data type instead. Long variables can hold numbers from -9,223,372,036,854,775,808 through 9,223,372,036,854,775,807. Operations with Long are slightly slower than with Integer .

What is bigger than integer in Python?

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.

What is bigger than integer in Java?

byte 1 byte -128 to 127. short 2 bytes -32,768 to 32,767. int 4 bytes -2,147,483,648 to 2,147,483,647. long 8 bytes -9,223,372,036,854,775,808 to 9,223,372,036,854,775,80.


2 Answers

Since python integers are unbounded, you have to do this with a custom class:

import functools  @functools.total_ordering class NeverSmaller(object):     def __le__(self, other):         return False  class ReallyMaxInt(NeverSmaller, int):     def __repr__(self):         return 'ReallyMaxInt()' 

Here I've used a mix-in class NeverSmaller rather than direct decoration of ReallyMaxInt, because on Python 3 the action of functools.total_ordering would have been prevented by existing ordering methods inherited from int.

Usage demo:

>>> N = ReallyMaxInt() >>> N > sys.maxsize True >>> isinstance(N, int) True >>> sorted([1, N, 0, 9999, sys.maxsize]) [0, 1, 9999, 9223372036854775807, ReallyMaxInt()] 

Note that in python2, sys.maxint + 1 is bigger than sys.maxint, so you can't rely on that.

Disclaimer: This is an integer in the OO sense, it is not an integer in the mathematical sense. Consequently, arithmetic operations inherited from the parent class int may not behave sensibly. If this causes any issues for your intended use case, then they can be disabled by implementing __add__ and friends to just error out.

like image 78
wim Avatar answered Sep 30 '22 11:09

wim


Konsta Vesterinen's infinity.Infinity would work (pypi), except that it doesn't inherit from int, but you can subclass it:

from infinity import Infinity class IntInfinity(Infinity, int):     pass assert isinstance(IntInfinity(), int) assert IntInfinity() > 1e100 

Another package that implements "infinity" values is Extremes, which was salvaged from the rejected PEP 326; again, you'd need to subclass from extremes.Max and int.

like image 26
ecatmur Avatar answered Sep 30 '22 11:09

ecatmur