{"id":116512,"date":"2020-08-10T07:58:07","date_gmt":"2020-08-10T07:58:07","guid":{"rendered":"https:\/\/blog.finxter.com\/?p=11665"},"modified":"2020-08-10T07:58:07","modified_gmt":"2020-08-10T07:58:07","slug":"python-infinity","status":"publish","type":"post","link":"https:\/\/sickgaming.net\/blog\/2020\/08\/10\/python-infinity\/","title":{"rendered":"Python Infinity"},"content":{"rendered":"<p>Infinite \u201c\u221e\u201d is a term derived from the Latin word <em>infinitas<\/em> which means \u201cwithout an end\u201d or \u201cboundless\u201d. 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>\n<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>\n<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>\n<p><em>Let\u2019s explore the ways Infinity can be used in python:<\/em><\/p>\n<h2>Method 1: Using float(inf) and float(-inf)<\/h2>\n<p>Infinity can be a positive or negative value and can be represented by float(inf) and float(-inf) respectively.&nbsp;<\/p>\n<p><em>The following code demonstrates the implementation of positive and negative infinity:<\/em><\/p>\n<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')\nnumber = 9999999999999999999999999\nif number > infinity_positive: print(\"number is greater than Infinity!\")\nelse: 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>\n<p><strong>Output:<\/strong><\/p>\n<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>\n<h2>Method 2: Using the \u201cmath\u201d Module<\/h2>\n<p>Python\u2019s 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>\n<p><em>The following code demonstrates the implementation of infinity using the math module:<\/em><\/p>\n<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\ninfinity_positive = math.inf\nnumber = 9999999999999999999999999\n# Positive Infinity\nif number > infinity_positive: print(\"number is greater than Infinity!\")\nelse: print(\"Positive Infinity is the greatest! Even greater than\",number)\n# Negative Infinity\ninfinity_negative = -math.inf\nif -number &lt; infinity_negative: print(\"number is lesser than Negative Infinity!\")\nelse: print(\"Negative Infinity is the least! Even smaller than\",-number)<\/pre>\n<p><strong>Output:<\/strong><\/p>\n<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>\n<h2>Method 3: Using the \u201cdecimal\u201d module<\/h2>\n<p>Another way of implementing infinity is by using Python\u2019s decimal module. Decimal(\u2018Infinity\u2019) returns positive infinity while Decimal(\u2018-Infinity\u2019) returns negative infinity.<\/p>\n<p><em>The following code demonstrates the implementation of infinity using the math module:<\/em><\/p>\n<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\ninfinity_positive = Decimal('Infinity')\nnumber = 9999999999999999999999999 # Positive Infinity\nif number > infinity_positive: print(\"number is greater than Infinity!\")\nelse: print(\"Positive Infinity is the greatest! Even greater than\",number) # Negative Infinity\ninfinity_negative = Decimal('-Infinity')\nif -number &lt; infinity_negative: print(\"number is lesser than Negative Infinity!\")\nelse: print(\"Negative Infinity is the least! Even smaller than\",-number)<\/pre>\n<p><strong>Output:<\/strong><\/p>\n<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>\n<h2>Method 4: Using the \u201cNumpy\u201d library<\/h2>\n<p>Another popular way of implementing Infinity in python is by using Python\u2019s famous Numpy library. Numpy has its own definitions for infinite values. np.inf returns positive infinity while -np.inf returns negative infinity.<\/p>\n<p><em>The following code demonstrates the implementation of infinity using the math module:<\/em><\/p>\n<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\ninfinity_positive = np.inf\nnumber = 9999999999999999999999999\n# Positive Infinity\nif number > infinity_positive: print(\"number is greater than Infinity!\")\nelse: print(\"Positive Infinity is the greatest! Even greater than\",number)\n# Negative Infinity\ninfinity_negative = -np.inf\nif -number &lt; infinity_negative: print(\"number is lesser than Negative Infinity!\")\nelse: print(\"Negative Infinity is the least! Even smaller than\",-number)<\/pre>\n<p><strong>Output:<\/strong><\/p>\n<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>\n<h2>Infinity Arithmetic<\/h2>\n<p>Generally, most arithmetic operations performed on infinity values result in generation of other infinite values. The following example illustrates this concept :<\/p>\n<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')\nprint('Result of Addition : ',value + 15)\nprint('Result of Subtraction : ',value - 15)\nprint('Result of Multiplication : ',value * 15)\nprint('Result of Division : ',value \/ 15)\n#special scenario\nprint('Multiplication by Zero: ',value * 0)<\/pre>\n<p><strong>Output:<\/strong><\/p>\n<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\nResult of Subtraction: inf\nResult of Multiplication: inf\nResult of Division: inf\nMultiplication by Zero: nan<\/pre>\n<p><strong><em>Note:<\/em><\/strong><\/p>\n<ul>\n<li>Division by zero raises a <strong>ZeroDivisionError<\/strong> exception instead of yielding a resultant value.<\/li>\n<li>Arithmetic operations on NaN always give NaN. There is no &#8220;negative NaN&#8221;.<\/li>\n<\/ul>\n<h2>Python Infinity Check<\/h2>\n<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>\n<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\nimport math\nnum1 = np.inf\nnum2 = -np.inf\nnum3 = 25\nprint(\"Is num1 an infinite number?: \",math.isinf(num1))\nprint(\"Is num3 an infinite number?: \",math.isinf(num2))\nprint(\"Is num2 an infinite number?: \",math.isinf(num3)) <\/pre>\n<h2>Creating Arrays with Infinity values<\/h2>\n<p>The following example demonstrates how an array of infinity values can be created:<\/p>\n<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\n# To create a numpy array with all values initialized to infinity\narray_infinity = np.full(5, np.inf)\nprint('Infinite Array: ',array_infinity) <\/pre>\n<p><strong>Output:<\/strong><\/p>\n<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>\n<p><strong>Test your knowledge based on the above explanations:<\/strong><\/p>\n<p><em>What will be the output of the following snippets?<\/em><\/p>\n<p> <iframe loading=\"lazy\" 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 loading=\"lazy\" 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>\n<p><strong>Answers:<\/strong> Run the code to get the answers!<\/p>\n<h2>Conclusion<\/h2>\n<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>\n<p>I hope you found this blog article useful and it helps you to get started with the fundamentals of Python Infinity!<\/p>\n<h2>Where to Go From Here?<\/h2>\n<p>Enough theory, let\u2019s get some practice!<\/p>\n<p>To become successful in coding, you need to get out there and solve real problems for real people. That\u2019s how you can become a six-figure earner easily. And that\u2019s how you polish the skills you really need in practice. After all, what\u2019s the use of learning theory that nobody ever needs?<\/p>\n<p><strong>Practice projects is how you sharpen your saw in coding!<\/strong><\/p>\n<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>\n<p>Then become a Python freelance developer! It\u2019s the best way of approaching the task of improving your Python skills\u2014even if you are a complete beginner.<\/p>\n<p>Join my free webinar <a rel=\"noreferrer noopener\" href=\"https:\/\/blog.finxter.com\/webinar-freelancer\/\" target=\"_blank\">\u201cHow to Build Your High-Income Skill Python\u201d<\/a> and watch how I grew my coding business online and how you can, too\u2014from the comfort of your own home.<\/p>\n<p><a href=\"https:\/\/blog.finxter.com\/webinar-freelancer\/\" target=\"_blank\" rel=\"noreferrer noopener\">Join the free webinar now!<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Infinite \u201c\u221e\u201d is a term derived from the Latin word infinitas which means \u201cwithout an end\u201d or \u201cboundless\u201d. 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; The positive infinity is used to denote the greatest of all values [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[857],"tags":[73,468,528],"class_list":["post-116512","post","type-post","status-publish","format-standard","hentry","category-python-tut","tag-programming","tag-python","tag-tutorial"],"_links":{"self":[{"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/posts\/116512","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/comments?post=116512"}],"version-history":[{"count":0,"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/posts\/116512\/revisions"}],"wp:attachment":[{"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/media?parent=116512"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/categories?post=116512"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/tags?post=116512"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}