How to Represent an Infinite Number in Python?
Infinity is an undefined number which can be negative or positive. A number is used as infinity; sometimes, the sum of two numeric values may be a numeric but different pattern; it may be a negative or positive value.
It is used to compare the solution in algorithms for the best solution. Generally, a value set at initial may be positive or negative infinity; we have to take care that no input value is bigger or smaller.
Infinity in Python
In Python, there is no way or method to represent infinity as an integer. This matches the fundamental characteristic of many other popular programming languages. But due to python being dynamically typed language, you can use float(inf) as an integer to represent it as infinity.
Therefore in python, we cannot represent infinity, or we can say that there is no way to show the infinity as an integer. But we can use float (inf) as an integer.
In Python, positive infinity and negative infinity can be represented by:
- Positive infinity: inf
- Negative infinity: -inf
Python Program to Define Positive and Negative Infinity Number
Example
Represent Positive and Negative Infinity Using the Math Module
To determine the infinity number, you can use the python math module.
Note: This will work with only python 3.5 or higher version of python.
Syntax:
- Positive infinity: math.inf
- Negative infinity: -math.inf
Positive infinity number is greatest, and the negative infinity number is the smallest of all numbers.
In comparison, positive infinity is always bigger than every natural number.
(i.e. 0,1,2. +∞. Positive integer and +infinity.)
In comparison, negative infinity is smaller than the negative number.
(i.e., ∞-. -2,-1,0, 1,.2,3. – negative infinity and -integer.)
Python Program to Check If the Number Is Infinite
In the above example, we are using the using isinf method of math library to check if the number is infinity or not.
Represent Positive and Negative Infinity Using the Decimal Module
You can define positive and negative Infinity by using the decimal module.
Syntax:
- Positive Infinity: Decimal(‘Infinity’)
- Negative Infinity: Decimal(‘-Infinity’)
Represent Positive and Negative Infinity Using Numpy Library
You can define positive and negative Infinity by using the inf module of the NumPy library
Syntax:
- Positive Infinity: np.inf
- Negative Infinity: -np.inf
Check If Positive Infinity Number Is Equal to Negative Infinity Number
You can simple check if the positive infinity number is equal to negative infinity number using simple “==” operator, and the output is always false.
Arithmetic operations on infinity number will give an infinite number
If you perform any arithmetic operation with positive or negative infinity numbers, the result will always be an endless number.
How can I represent an infinite number in Python?
How can I represent an infinite number in python? No matter which number you enter in the program, no number should be greater than this representation of infinity.
![]()
13 Answers 13
In Python, you can do:
In Python 3.5, you can do:
Will always be true. Unless of course, as pointed out, x is also infinity or «nan» («not a number»).
Additionally (Python 2.x ONLY), in a comparison to Ellipsis , float(inf) is lesser, e.g:
would return true.
Since Python 3.5 you can use math.inf :
No one seems to have mentioned about the negative infinity explicitly, so I think I should add it.
For negative infinity:
For positive infinity (just for the sake of completeness):
![]()
I don’t know exactly what you are doing, but float(«inf») gives you a float Infinity, which is greater than any other number.
There is an infinity in the NumPy library: from numpy import inf . To get negative infinity one can simply write -inf .
![]()
Another, less convenient, way to do it is to use Decimal class:
In python2.x there was a dirty hack that served this purpose (NEVER use it unless absolutely necessary):
Thus the check i < » holds True for any integer i .
It has been reasonably deprecated in python3. Now such comparisons end up with
Also if you use SymPy you can use sympy.oo
![]()
Infinity
1. Using float(‘inf’) and float(‘-inf)
2. Using Python’s math module
3. Integer maxsize
4. Using Python’s decimal module
5. Using Numpy Library
![]()
For Positive Infinity
For Negative Infinity
![]()
Representing ∞ in python
float("inf") or float("INF") or float("Inf") or float("inF") or float("infinity") or float("Infinity") creates a float object holding ∞
You can also represent -∞ in python
float("-inf") or float("-INF") or float("-Inf") or float("-infinity") creates a float object holding -∞
Python Infinity: Representation and Usage

Can you think of a value that can neither be defined nor estimated but is so crucial to the programming world? Yes, you got it right! Its infinity. But do you know that until 2020 there was no proper way to represent infinity in python? We generally used a larger/small value or a garbage value to represent infinity.
Python Infinity is a variable type that is greater than every other numeric value in python. Rather than using any garbage large value as a limit, you can use pre-defined keyword for initializing an infinite value in Python.
But now we have many different ways to represent infinity. In this article, we will understand why it is essential in Computer Science and how to express it in python.
How to Represent Infinity in Python?
Infinity can be positive or negative. The result of any operation of any with infinity like sum, product, the division is always infinity. One more important thing is that infinity is still a float value. There is no way to represent infinity as an integer. So, let us now know about all the methods we can represent positive and negative infinity.
a. Using float()
Python is a dynamic language, that is why we can use float() to represent infinity.

b. Using the math module to represent infinity in python
We can use the math module to represent infinity.
c. Using the Decimal module
d. Using the sympy module
e. Using the Numpy Module
For using the numpy library, we first need to import it using –
Performing Operations on Infinity in python
a. Adding and Subtracting a number.
Whatever number we add or subtract from infinity, we will get infinity.
b. Multiply and Divide some number with Infinity
c. Finding Remainder of a infinity when divides with a number
d. Checking if a number is infinity using math.isinf()
We can check if a number is infinity using the isinf() method of math module.
e. Are all infinity same?
Let us now check whether the values from each methods are same or not?
f. Comparing the Values of python infinity with other values using python max function
Positive Infinity is the largest number possible and negative infinity is the smallest number. Let us compare some values with infinity and check whether it is true or not.
We can also do something like this-
g. Changing the type of python infinity into integer
Let us try to change the type from float into integer and see what happens.
Must Read:
Conclusion
The use of infinity in computer science is great. When we want to compare a number with a very large or small number, we generally use infinity. Not only this, it can be used to measure the performance of different algorithms. It is generally used when we perform computations on a very large scale.
Try to run the programs on your side and let us know if you have any queries.
Как указать бесконечность в python
As ironic as it may seem infinity is defined as an undefined number that can either be a positive or negative value. All arithmetic operations performed on an infinite value always lead to an infinite number, say it be sum, subtraction, multiplication, or any other operation.
In the world of computer science, infinity is generally used to measure performance and optimize algorithms that perform computations on a large scale application.
Representing infinity as an Integer in python
The concept of representing infinity as an integer violates the definition of infinity itself. As of 2020, there is no such way to represent infinity as an integer in any programming language so far. But in python, as it is a dynamic language, float values can be used to represent an infinite integer. One can use float(‘inf’) as an integer to represent it as infinity. Below is the list of ways one can represent infinity in Python.
1. Using float(‘inf’) and float(‘-inf’):
As infinity can be both positive and negative they can be represented as a float(‘inf’) and float(‘-inf’) respectively. The below code shows the implementation of the above-discussed content: