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') ?

<div class="execute"></div><div class="code-mirror  language-python" contenteditable="plaintext-only" spellcheck="false" style="outline: none; overflow-wrap: break-word; overflow-y: auto; white-space: pre-wrap;"><span class="token comment"># getting a positive infinite integer value using the float(inf) function</span>
positiveInfinity <span class="token operator">=</span> <span class="token builtin">float</span><span class="token punctuation">(</span><span class="token string">'inf'</span><span class="token punctuation">)</span>

<span class="token comment"># printing a positive infinite integer value</span>
<span class="token keyword">print</span><span class="token punctuation">(</span><span class="token string">'Positive Infinity value = '</span><span class="token punctuation">,</span> positiveInfinity<span class="token punctuation">)</span>

<span class="token comment"># getting a negative infinite integer value</span>
negativeInfinity <span class="token operator">=</span> <span class="token builtin">float</span><span class="token punctuation">(</span><span class="token string">'-inf'</span><span class="token punctuation">)</span>

<span class="token comment"># printing a negative infinite integer value</span>
<span class="token keyword">print</span><span class="token punctuation">(</span><span class="token string">'Negative Infinity value = '</span><span class="token punctuation">,</span> negativeInfinity<span class="token punctuation">)</span>
</div><div class="output-wrapper"><div class="console-close"></div><div class="code-output"></div></div>

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

<div class="execute"></div><div class="code-mirror  language-python" contenteditable="plaintext-only" spellcheck="false" style="outline: none; overflow-wrap: break-word; overflow-y: auto; white-space: pre-wrap;"><span class="token comment"># importing math module</span>
<span class="token keyword">import</span> math

<span class="token comment"># printing a positive infinite integer value using math.inf</span>
<span class="token keyword">print</span><span class="token punctuation">(</span><span class="token string">'Positive Infinity value = '</span><span class="token punctuation">,</span> math<span class="token punctuation">.</span>inf<span class="token punctuation">)</span>

<span class="token comment"># printing a negative infinite integer value using -math.inf</span>
<span class="token keyword">print</span><span class="token punctuation">(</span><span class="token string">'Negative Infinity value = '</span><span class="token punctuation">,</span> <span class="token operator">-</span>math<span class="token punctuation">.</span>inf<span class="token punctuation">)</span>
</div><div class="output-wrapper"><div class="console-close"></div><div class="code-output"></div></div>

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

<div class="execute"></div><div class="code-mirror  language-python" contenteditable="plaintext-only" spellcheck="false" style="outline: none; overflow-wrap: break-word; overflow-y: auto; white-space: pre-wrap;"><span class="token comment"># importing Decimal from the decimal module</span>
<span class="token keyword">from</span> decimal <span class="token keyword">import</span> Decimal

<span class="token comment"># printing a positive infinite integer value using the Decimal() function</span>
<span class="token keyword">print</span><span class="token punctuation">(</span><span class="token string">'Positive Infinity value = '</span><span class="token punctuation">,</span> Decimal<span class="token punctuation">(</span><span class="token string">'Infinity'</span><span class="token punctuation">)</span><span class="token punctuation">)</span>

<span class="token comment"># printing a negative infinite integer value using the Decimal() function</span>
<span class="token keyword">print</span><span class="token punctuation">(</span><span class="token string">'Negative Infinity value = '</span><span class="token punctuation">,</span> Decimal<span class="token punctuation">(</span><span class="token string">'-Infinity'</span><span class="token punctuation">)</span><span class="token punctuation">)</span>
</div><div class="output-wrapper"><div class="console-close"></div><div class="code-output"></div></div>

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

<div class="execute"></div><div class="code-mirror  language-python" contenteditable="plaintext-only" spellcheck="false" style="outline: none; overflow-wrap: break-word; overflow-y: auto; white-space: pre-wrap;"><span class="token comment"># importing NumPy module</span>
<span class="token keyword">import</span> numpy <span class="token keyword">as</span> np

<span class="token comment"># printing a positive infinite integer value using numpy.inf</span>
<span class="token keyword">print</span><span class="token punctuation">(</span><span class="token string">'Positive Infinity value = '</span><span class="token punctuation">,</span> np<span class="token punctuation">.</span>inf<span class="token punctuation">)</span>

<span class="token comment"># printing a negative infinite integer value using -numpy.inf</span>
<span class="token keyword">print</span><span class="token punctuation">(</span><span class="token string">'Negative Infinity value = '</span><span class="token punctuation">,</span> <span class="token operator">-</span>np<span class="token punctuation">.</span>inf<span class="token punctuation">)</span>
</div><div class="output-wrapper"><div class="console-close"></div><div class="code-output"></div></div>

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

<div class="execute"></div><div class="code-mirror  language-python" contenteditable="plaintext-only" spellcheck="false" style="outline: none; overflow-wrap: break-word; overflow-y: auto; white-space: pre-wrap;"><span class="token comment"># importing numpy and math modules</span>
<span class="token keyword">import</span> numpy <span class="token keyword">as</span> np
<span class="token keyword">import</span> math

<span class="token comment"># creating a positive infinite integer using numpy.inf</span>
x <span class="token operator">=</span> np<span class="token punctuation">.</span>inf

<span class="token comment"># creating a negative infinite integer using -numpy.inf</span>
y <span class="token operator">=</span> <span class="token operator">-</span>np<span class="token punctuation">.</span>inf

<span class="token comment"># finite integer</span>
z <span class="token operator">=</span> <span class="token number">40</span>

<span class="token comment"># checking whether x is infinite number using isinf() function</span>
<span class="token keyword">print</span><span class="token punctuation">(</span>math<span class="token punctuation">.</span>isinf<span class="token punctuation">(</span>x<span class="token punctuation">)</span><span class="token punctuation">)</span>

<span class="token comment"># checking whether y is infinite number using isinf() function</span>
<span class="token keyword">print</span><span class="token punctuation">(</span>math<span class="token punctuation">.</span>isinf<span class="token punctuation">(</span>y<span class="token punctuation">)</span><span class="token punctuation">)</span>

<span class="token comment"># checking whether z is infinite number using isinf() function</span>
<span class="token keyword">print</span><span class="token punctuation">(</span>math<span class="token punctuation">.</span>isinf<span class="token punctuation">(</span>z<span class="token punctuation">)</span><span class="token punctuation">)</span>
</div><div class="output-wrapper"><div class="console-close"></div><div class="code-output"></div></div>

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

<div class="execute"></div><div class="code-mirror  language-python" contenteditable="plaintext-only" spellcheck="false" style="outline: none; overflow-wrap: break-word; overflow-y: auto; white-space: pre-wrap;"><span class="token comment"># importing numpy module</span>
<span class="token keyword">import</span> numpy <span class="token keyword">as</span> np
<span class="token comment"># creating a positive infinite integer using numpy.inf</span>
x <span class="token operator">=</span> np<span class="token punctuation">.</span>inf
<span class="token comment"># creating a negative infinite integer using -numpy.inf</span>
y <span class="token operator">=</span> <span class="token operator">-</span>np<span class="token punctuation">.</span>inf
<span class="token comment"># positive finite integer</span>
z <span class="token operator">=</span> <span class="token number">40</span>
<span class="token comment"># negative finite integer</span>
k <span class="token operator">=</span> <span class="token operator">-</span><span class="token number">40</span>
<span class="token comment"># creating a function to compare two numbers(positive and negative infinity)</span>
<span class="token comment"># by accepting 2 numbers as arguments</span>
<span class="token keyword">def</span> <span class="token function">compareNumbers</span><span class="token punctuation">(</span>p<span class="token punctuation">,</span> q<span class="token punctuation">)</span><span class="token punctuation">:</span>
   <span class="token comment"># checking if p is greater than q i,e, a first number greater than the second</span>
   <span class="token keyword">if</span> p<span class="token operator">></span>q<span class="token punctuation">:</span>
   <span class="token comment"># printing True if it is greater</span>
     <span class="token keyword">print</span><span class="token punctuation">(</span><span class="token string">"True"</span><span class="token punctuation">)</span>
   <span class="token keyword">else</span><span class="token punctuation">:</span>
   <span class="token comment"># printing False if it is Not greater(less)</span>
     <span class="token keyword">print</span><span class="token punctuation">(</span><span class="token string">"False"</span><span class="token punctuation">)</span>
<span class="token comment"># calling the compareNumbers() by passing any of the 2</span>
<span class="token comment"># above defined variables for comparison</span>
compareNumbers<span class="token punctuation">(</span>x<span class="token punctuation">,</span> y<span class="token punctuation">)</span>
compareNumbers<span class="token punctuation">(</span>x<span class="token punctuation">,</span> z<span class="token punctuation">)</span>
compareNumbers<span class="token punctuation">(</span>y<span class="token punctuation">,</span> k<span class="token punctuation">)</span>
compareNumbers<span class="token punctuation">(</span>x<span class="token punctuation">,</span> k<span class="token punctuation">)</span>
compareNumbers<span class="token punctuation">(</span>y<span class="token punctuation">,</span> z<span class="token punctuation">)</span>
</div><div class="output-wrapper"><div class="console-close"></div><div class="code-output"></div></div>

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.

Updated on: 2022-11-09T07:36:38+05:30

12K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements