Create an account


Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[Tut] Python Infinity

#1
Python Infinity

<div><p>Infinite “∞” is a term derived from the Latin word <em>infinitas</em> which means “without an end” or “boundless”. Similarly, infinity in python is an undefined value that can either be positive or negative. It comes in handy for measuring and optimizing complex algorithms.&nbsp;</p>
<p><strong><em>The positive infinity is used to denote the greatest of all values while the negative infinity is used to denote the least of all values.</em></strong></p>
<p>Until now there is no way to represent infinity as an integer. However, in python float values can be used to represent infinite integer values.&nbsp;</p>
<p><em>Let’s explore the ways Infinity can be used in python:</em></p>
<h2>Method 1: Using float(inf) and float(-inf)</h2>
<p>Infinity can be a positive or negative value and can be represented by float(inf) and float(-inf) respectively.&nbsp;</p>
<p><em>The following code demonstrates the implementation of positive and negative infinity:</em></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="">infinity_positive = float('Inf')
number = 9999999999999999999999999
if number > infinity_positive: print("number is greater than Infinity!")
else: print("Positive Infinity is the greatest! Even greater than",number) infinity_negative = float('-Inf') if -number &lt; infinity_negative: print("number is lesser than Negative Infinity!") else: print("Negative Infinity is the least! Even smaller than",-number)</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="">Positive Infinity is the greatest! Even greater than 9999999999999999999999999 Negative Infinity is the least! Even smaller than -9999999999999999999999999</pre>
<h2>Method 2: Using the “math” Module</h2>
<p>Python’s math module can also be used to implement infinity. In Python 3.5 and higher math.inf is a predefined constant that is used to return positive infinity while -math.inf constant returns negative infinity. It returns a floating value.</p>
<p><em>The following code demonstrates the implementation of infinity using the math module:</em></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 math
infinity_positive = math.inf
number = 9999999999999999999999999
# Positive Infinity
if number > infinity_positive: print("number is greater than Infinity!")
else: print("Positive Infinity is the greatest! Even greater than",number)
# Negative Infinity
infinity_negative = -math.inf
if -number &lt; infinity_negative: print("number is lesser than Negative Infinity!")
else: print("Negative Infinity is the least! Even smaller than",-number)</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="">Positive Infinity is the greatest! Even greater than 9999999999999999999999999 Negative Infinity is the least! Even smaller than -9999999999999999999999999</pre>
<h2>Method 3: Using the “decimal” module</h2>
<p>Another way of implementing infinity is by using Python’s decimal module. Decimal(‘Infinity’) returns positive infinity while Decimal(‘-Infinity’) returns negative infinity.</p>
<p><em>The following code demonstrates the implementation of infinity using the math module:</em></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 decimal import Decimal
infinity_positive = Decimal('Infinity')
number = 9999999999999999999999999 # Positive Infinity
if number > infinity_positive: print("number is greater than Infinity!")
else: print("Positive Infinity is the greatest! Even greater than",number) # Negative Infinity
infinity_negative = Decimal('-Infinity')
if -number &lt; infinity_negative: print("number is lesser than Negative Infinity!")
else: print("Negative Infinity is the least! Even smaller than",-number)</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="">Positive Infinity is the greatest! Even greater than 9999999999999999999999999 Negative Infinity is the least! Even smaller than -9999999999999999999999999</pre>
<h2>Method 4: Using the “Numpy” library</h2>
<p>Another popular way of implementing Infinity in python is by using Python’s famous Numpy library. Numpy has its own definitions for infinite values. np.inf returns positive infinity while -np.inf returns negative infinity.</p>
<p><em>The following code demonstrates the implementation of infinity using the math module:</em></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 numpy as np
infinity_positive = np.inf
number = 9999999999999999999999999
# Positive Infinity
if number > infinity_positive: print("number is greater than Infinity!")
else: print("Positive Infinity is the greatest! Even greater than",number)
# Negative Infinity
infinity_negative = -np.inf
if -number &lt; infinity_negative: print("number is lesser than Negative Infinity!")
else: print("Negative Infinity is the least! Even smaller than",-number)</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="">Positive Infinity is the greatest! Even greater than 9999999999999999999999999 Negative Infinity is the least! Even smaller than -9999999999999999999999999</pre>
<h2>Infinity Arithmetic</h2>
<p>Generally, most arithmetic operations performed on infinity values result in generation of other infinite values. The following example illustrates this 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="">value = float('inf')
print('Result of Addition : ',value + 15)
print('Result of Subtraction : ',value - 15)
print('Result of Multiplication : ',value * 15)
print('Result of Division : ',value / 15)
#special scenario
print('Multiplication by Zero: ',value * 0)</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="">Result of Addition: inf
Result of Subtraction: inf
Result of Multiplication: inf
Result of Division: inf
Multiplication by Zero: nan</pre>
<p><strong><em>Note:</em></strong></p>
<ul>
<li>Division by zero raises a <strong>ZeroDivisionError</strong> exception instead of yielding a resultant value.</li>
<li>Arithmetic operations on NaN always give NaN. There is no “negative NaN”.</li>
</ul>
<h2>Python Infinity Check</h2>
<p>The <strong><em>isinf()</em></strong> method of the math module is used to check for infinite values in python. The following example demonstrates this 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 numpy as np
import math
num1 = np.inf
num2 = -np.inf
num3 = 25
print("Is num1 an infinite number?: ",math.isinf(num1))
print("Is num3 an infinite number?: ",math.isinf(num2))
print("Is num2 an infinite number?: ",math.isinf(num3)) </pre>
<h2>Creating Arrays with Infinity values</h2>
<p>The following example demonstrates how an array of infinity values can be created:</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 numpy as np
# To create a numpy array with all values initialized to infinity
array_infinity = np.full(5, np.inf)
print('Infinite Array: ',array_infinity) </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="">Infinite Array: [ inf inf inf inf inf]</pre>
<p><strong>Test your knowledge based on the above explanations:</strong></p>
<p><em>What will be the output of the following snippets?</em></p>
<p> <iframe src="https://repl.it/@finxter/PaleBeautifulCable?lite=true" scrolling="no" allowtransparency="true" allowfullscreen="true" sandbox="allow-forms allow-pointer-lock allow-popups allow-same-origin allow-scripts allow-modals" width="100%" height="400px" frameborder="no"></iframe> <iframe src="https://repl.it/@finxter/RuralGracefulIntercept?lite=true" scrolling="no" allowtransparency="true" allowfullscreen="true" sandbox="allow-forms allow-pointer-lock allow-popups allow-same-origin allow-scripts allow-modals" width="100%" height="400px" frameborder="no"></iframe> </p>
<p><strong>Answers:</strong> Run the code to get the answers!</p>
<h2>Conclusion</h2>
<p>Infinity is majorly used in complex algorithmic designs and optimization problems. One such example is the Shortest Path Algorithm where the current distance values have to be compared with the best or the least distance values.</p>
<p>I hope you found this blog article useful and it helps you to get started with the fundamentals of Python Infinity!</p>
<h2>Where to Go From Here?</h2>
<p>Enough theory, let’s get some practice!</p>
<p>To become successful in coding, you need to get out there and solve real problems for real people. That’s how you can become a six-figure earner easily. And that’s how you polish the skills you really need in practice. After all, what’s the use of learning theory that nobody ever needs?</p>
<p><strong>Practice projects is how you sharpen your saw in coding!</strong></p>
<p>Do you want to become a code master by focusing on practical code projects that actually earn you money and solve problems for people?</p>
<p>Then become a Python freelance developer! It’s the best way of approaching the task of improving your Python skills—even if you are a complete beginner.</p>
<p>Join my free webinar <a rel="noreferrer noopener" href="https://blog.finxter.com/webinar-freelancer/" target="_blank">“How to Build Your High-Income Skill Python”</a> and watch how I grew my coding business online and how you can, too—from the comfort of your own home.</p>
<p><a href="https://blog.finxter.com/webinar-freelancer/" target="_blank" rel="noreferrer noopener">Join the free webinar now!</a></p>
</div>


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



Forum Jump:


Users browsing this thread:
1 Guest(s)

Forum software by © MyBB Theme © iAndrew 2016