- Trending Categories
- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP
- Physics
- Chemistry
- Biology
- Mathematics
- English
- Economics
- Psychology
- Social Studies
- Fashion Studies
- Legal Studies

- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who

# How can I represent an infinite number in Python?

In this article, we will show you how to represent an **infinite **number in python.

**Infinity **is an undefined number that can be either positive or negative. A number is used to represent infinity; the sum of two numeric values may be a numeric but distinct pattern; it may have a negative or positive value.

All arithmetic operations on an infinite value, whether sum, subtraction, multiplication or any other operation, always result in an infinite number.

In the field of computer science, infinity is commonly used to evaluate and optimize algorithms that do calculations on a huge scale.

## Infinity in Python

The reason why infinity is a float data type rather than an *int *data type is due to the way integers is represented in Python. An integer number is represented in binary, for example, the value **7** is represented as **0111**.

There is no way to represent infinity as an integer in Python. This is a fundamental feature of several other popular programming languages. However, because Python is a dynamically typed language, you can express infinity with** float(inf)** as an **integer**.

As a result, we cannot represent infinity in Python, or we might say that there is no way to represent infinity as an integer. However, **float (inf)** can be used as an integer.

Positive Infinity: inf Negative Infinity: -inf

## Representing Inifinite number using float(‘inf’)

Because infinity can be both positive and negative, it can be expressed as a **float('inf')** and a **float('-inf')** respectively.

### Algorithm (Steps)

Following are the Algorithm/steps to be followed to perform the desired task −

Use the

**float('inf')**to get a positive infinite integer value and create a variable to store it.Print the positive infinity value.

Use the

**float('-inf')**to get a negative infinite integer value and create a variable to store it.Print the negative infinity value.

### Example

The following program returns the positive Infinity value using **float('inf')** −

# getting a positive infinite integer value using the float(inf) function positiveInfinity = float('inf') # printing a positive infinite integer value print('Positive Infinity value = ', positiveInfinity) # getting a negative infinite integer value negativeInfinity = float('-inf') # printing a negative infinite integer value print('Negative Infinity value = ', negativeInfinity)

### Output

On executing, the above program will generate the following output −

Positive Infinity value = inf Negative Infinity value = -inf

## Representing Inifinite number using the math module

We can use the math module to represent an infinite value, however, it only works with Python 3.5 or higher. Because infinite can be both positive and negative, it is denoted by **math.inf** and **-math.inf** respectively.

### Example

# importing math module import math # printing a positive infinite integer value using math.inf print('Positive Infinity value = ', math.inf) # printing a negative infinite integer value using -math.inf print('Negative Infinity value = ', -math.inf)

### Output

On executing, the above program will generate the following output −

Positive Infinity value = inf Negative Infinity value = -inf

## Representing Inifinite number using the Decimal() function

We use **Decimal('Infinity')** for positive infinite and **Decimal('-Infinity')** for negative infinite to represent infinite using the decimal module.

### Example

# importing Decimal from the decimal module from decimal import Decimal # printing a positive infinite integer value using the Decimal() function print('Positive Infinity value = ', Decimal('Infinity')) # printing a negative infinite integer value using the Decimal() function print('Negative Infinity value = ', Decimal('-Infinity'))

### Output

On executing, the above program will generate the following output −

Positive Infinity value = Infinity Negative Infinity value = -Infinity

## Representing Inifinite number using NumPy module

Another way to represent infinite in Python is via the *NumPy *module, where **np.inf **and -np.inf represent positive and negative infinite, respectively.

NumPy is a Python library designed to work efficiently with arrays in Python. It is fast, simple to learn, and efficient in storage.

### Example

# importing NumPy module import numpy as np # printing a positive infinite integer value using numpy.inf print('Positive Infinity value = ', np.inf) # printing a negative infinite integer value using -numpy.inf print('Negative Infinity value = ', -np.inf)

### Output

On executing, the above program will generate the following output −

Positive Infinity value = inf Negative Infinity value = -inf

## Check If a Number Is Infinite in Python

To determine whether a given number is infinite or not, use the math library's** isinf()** method, which returns a **boolean **value.

### Example

# importing numpy and math modules import numpy as np import math # creating a positive infinite integer using numpy.inf x = np.inf # creating a negative infinite integer using -numpy.inf y = -np.inf # finite integer z = 40 # checking whether x is infinite number using isinf() function print(math.isinf(x)) # checking whether y is infinite number using isinf() function print(math.isinf(y)) # checking whether z is infinite number using isinf() function print(math.isinf(z))

### Output

On executing, the above program will generate the following output −

True True False

## Comparison of infinite and finite values in python

The idea of comparing an infinite value to a finite value is as easy as it gets. Because **positive infinity** is always **greater **than any natural number and** negative infinity** is always smaller than negative numbers

### Example

# importing numpy module import numpy as np # creating a positive infinite integer using numpy.inf x = np.inf # creating a negative infinite integer using -numpy.inf y = -np.inf # positive finite integer z = 40 # negative finite integer k = -40 # creating a function to compare two numbers(positive and negative infinity) # by accepting 2 numbers as arguments def compareNumbers(p, q): # checking if p is greater than q i,e, a first number greater than the second if p>q: # printing True if it is greater print("True") else: # printing False if it is Not greater(less) print("False") # calling the compareNumbers() by passing any of the 2 # above defined variables for comparison compareNumbers(x, y) compareNumbers(x, z) compareNumbers(y, k) compareNumbers(x, k) compareNumbers(y, z)

### Output

On executing, the above program will generate the following output −

True True False True False

## Conclusion

In this article, we learnt how to represent infinity in Python using several ways. We also learned how to compare infinite and finite values and how to determine whether a given integer is infinite or not.

- Related Articles
- How can I represent immutable vectors in Python?
- How can I represent python tuple in JSON format?
- How we can come out of an infinite loop in Python?
- How to stop an infinite loop safely in Python?
- Index into an Infinite String in Python
- How i can replace number with string using Python?
- How can I delete an element in Selenium using Python?
- How can I display an image using cv2 in Python?
- How can we represent $frac{7}{6}$ on the number line?
- How to create an infinite loop in C#?
- How to run an infinite loop in Tkinter?
- In MySQL, how can we represent the time value as an integer?
- How to represent rational numbers in number line ? Represent $frac{-4}{3}$ in number line.
- How can I convert a Python tuple to an Array?
- How can I write an SQL IN query with a Python tuple?