{"id":26122,"date":"2021-12-14T08:46:05","date_gmt":"2021-12-14T03:16:05","guid":{"rendered":"https:\/\/python-programs.com\/?p=26122"},"modified":"2021-12-14T08:46:05","modified_gmt":"2021-12-14T03:16:05","slug":"python-program-for-setting-a-variable-value-to-infinity","status":"publish","type":"post","link":"https:\/\/python-programs.com\/python-program-for-setting-a-variable-value-to-infinity\/","title":{"rendered":"Python Program for Setting a Variable Value to Infinity"},"content":{"rendered":"
Your dataset cannot be represented by a single number? In Python, how about setting your variable value to infinity? That’s exactly what we’re going to speak about today!<\/p>\n
We frequently need to initialize a variable in Python with a large positive or large negative value. When comparing variables to determine the minimum or maximum in a set, this is fairly common.<\/p>\n
In Python, positive infinity is considered the largest positive value, whereas negative infinity is considered the largest negative value.<\/p>\n
Python, representation of infinity as an integer:<\/strong> The following is a list of Python infinite representations.<\/p>\n Float is the most simple approach to set a variable to positive or negative infinity without utilizing any modules.<\/p>\n Since infinity can be both positive and negative, it can be expressed as a float(‘inf’) and a float(‘-inf’) respectively.<\/p>\n For Example:<\/strong><\/p>\n Approach:<\/strong><\/p>\n Below is the implementation:<\/strong><\/p>\n Output:<\/strong><\/p>\n The math module in Python can also be used to generate infinite integers.<\/p>\n Approach:<\/strong><\/p>\n Below is the implementation:<\/strong><\/p>\n Output:<\/strong><\/p>\n The decimal module in Python can also be used to generate endless float values. Approach:<\/strong><\/p>\n Below is the implementation:<\/strong><\/p>\n Output:<\/strong><\/p>\n The Numpy module in Python can also be used to represent infinite values. It is denoted as np.inf for positive infinite value and -np.inf for negative infinite value.<\/p>\n Approach:<\/strong><\/p>\n Below is the implementation:<\/strong><\/p>\n Output:<\/strong><\/p>\n Python Checking to See if a Number Is Infinite<\/strong><\/p>\n To determine whether a given integer is infinite or not, use the math library’s isinf() method, which returns a boolean result.<\/p>\n Approach:<\/strong><\/p>\n Below is the implementation:<\/strong><\/p>\n Output:<\/strong><\/p>\n Python Arithmetic Operations on Infinity<\/strong><\/p>\n Adding or removing a value to or from infinity has no effect.<\/p>\n Output:<\/strong><\/p>\n Arithmetic operations involving two infinities<\/strong><\/p>\n Output:<\/strong><\/p>\n Your dataset cannot be represented by a single number? In Python, how about setting your variable value to infinity? That’s exactly what we’re going to speak about today! We frequently need to initialize a variable in Python with a large positive or large negative value. When comparing variables to determine the minimum or maximum in …<\/p>\n
\nThe concept of portraying the infinite as an integer contradicts the definition of infinity. In any programming language as of 2020, there is no mechanism to represent infinity as an integer. Float values, on the other hand, can be used to represent an infinite integer in Python because it is a dynamic language. To represent infinity, use float(‘inf’) as an integer.<\/p>\n1)Using float variables to initialize with infinity:<\/strong><\/h4>\n
\n
# Pass 'inf' as an argument to the float() function to define a positive\r\n# infinite integer and store it in a variable\r\npos_infnty = float('inf')\r\n# Print the positive Infinity\r\nprint('The result of positive Infinity = ', pos_infnty)\r\n\r\n# Pass '-inf' as an argument to the float() function to define a negative\r\n# infinite integer and store it in another variable\r\nneg_infnty = float('-inf')\r\n# Print the negative Infinity\r\nprint('The result of negative Infinity = ', neg_infnty)\r\n<\/pre>\n
The result of positive Infinity = inf\r\nThe result of negative Infinity = -inf<\/pre>\n
Using Math module:<\/strong><\/h4>\n
\n
# Import math module using the import keyword.\r\nimport math\r\n# Get the positive Infinity value by using the math.inf and store it in a variable\r\npos_infnty = math.inf\r\n# Print the positive Infinity\r\nprint('The result of positive Infinity = ', pos_infnty)\r\n\r\n# Get the negative Infinity value by using the -math.inf and store it in another\r\n# variable\r\nneg_infnty = -math.inf\r\n# Print the negative Infinity\r\nprint('The result of negative Infinity = ', neg_infnty)\r\n<\/pre>\n
The result of positive Infinity = inf\r\nThe result of negative Infinity = -inf<\/pre>\n
By using the decimal module in Python<\/strong><\/h4>\n
\nIt is written as Decimal(‘Infinity’) for a positive infinite value and Decimal(‘-Infinity’) for a negative infinite value.<\/p>\n\n
# Import Decimal function from decimal module using the import keyword.\r\nfrom decimal import Decimal\r\n# Pass 'Infinity' as an argument to the Decimal() function to define a positive\r\n# infinite integer and store it in a variable\r\npos_infnty = Decimal('Infinity')\r\n# Print the positive Infinity\r\nprint('The result of positive Infinity = ', pos_infnty)\r\n\r\n# Pass '-Infinity' as an argument to the Decimal() function to define a negative\r\n# infinite integer and store it in another variable\r\nneg_infnty = Decimal('-Infinity')\r\n# Print the negative Infinity\r\nprint('The result of negative Infinity = ', neg_infnty)\r\n<\/pre>\n
The result of positive Infinity = Infinity\r\nThe result of negative Infinity = -Infinity<\/pre>\n
By using the Numpy library in Python<\/strong><\/h4>\n
\n
# Import numpy library as np using the import keyword.\r\nimport numpy as np\r\n# Get the positive Infinity value by using the np.inf and store it in a variable\r\npos_infnty = np.inf\r\n# Print the positive Infinity\r\nprint('The result of positive Infinity = ', pos_infnty)\r\n\r\n# Get the negative Infinity value by using the -np.inf and store it in another\r\n# variable\r\nneg_infnty = -np.inf\r\n# Print the negative Infinity\r\nprint('The result of negative Infinity = ', neg_infnty)\r\n<\/pre>\n
The result of positive Infinity = inf \r\nThe result of negative Infinity = -inf<\/pre>\n
\n
# Import math module using the import keyword.\r\nimport math\r\n# Get the positive Infinity value by using the math.inf and store it in a variable\r\npos_infnty = math.inf\r\n# Print the positive Infinity\r\nprint('The result of positive Infinity = ', pos_infnty)\r\n\r\n# Get the negative Infinity value by using the -math.inf and store it in another\r\n# variable\r\nneg_infnty = -math.inf\r\n# Print the negative Infinity\r\nprint('The result of negative Infinity = ', neg_infnty)\r\n# Check whether the above obtained results are infinite or not using the\r\n# isinf() function and\u00a0print the result.\r\nprint(math.isinf(pos_infnty))\r\nprint(math.isinf(neg_infnty))\r\n# Give some random number as static input and store it in another variable\r\ngvn_numb = 1000\r\n# Check whether the given number is infinite or not using the isinf() function and\r\n# print the result.\r\nprint(math.isinf(gvn_numb))\r\n<\/pre>\n
The result of positive Infinity = inf\r\nThe result of negative Infinity = -inf\r\nTrue\r\nTrue\r\nFalse<\/pre>\n
import math\r\npos_infnty = math.inf\r\nneg_infnty = -math.inf\r\n# Arithmetic operations\r\n# Adding or removing a value to or from infinity has no effect.\r\nrslt = pos_infnty + 500\r\nprint(rslt)\r\nrslt = neg_infnty + 700\r\nprint(rslt)\r\n\r\nrslt = pos_infnty - 500\r\nprint(rslt)\r\nrslt = neg_infnty - 700\r\nprint(rslt)\r\n<\/pre>\n
inf\r\n-inf\r\ninf\r\n-inf<\/pre>\n
import math\r\npos_infnty = math.inf\r\nneg_infnty = -math.inf\r\n# Arithmetic operations involving two infinities\r\n# Addition\r\nrslt = pos_infnty + neg_infnty\r\nprint(\"Addition of positive and negative infinities = \", rslt)\r\n\r\n# Multiplication\r\nrslt = pos_infnty * neg_infnty\r\nprint(\"Multiplication of positive and negative infinities = \", rslt)\r\n\r\n# Division\r\nrslt = pos_infnty \/ neg_infnty\r\nprint(\"Division of positive and negative infinities = \", rslt)\r\n<\/pre>\n
Addition of positive and negative infinities = nan\r\nMultiplication of positive and negative infinities = -inf\r\nDivision of positive and negative infinities = nan<\/pre>\n","protected":false},"excerpt":{"rendered":"