Знак бесконечность в python

How to Represent an Infinite Number in Python?

Infinity is an undefined number which can be negative or positive. A number is used as infinity; sometimes, the sum of two numeric values may be a numeric but different pattern; it may be a negative or positive value.

It is used to compare the solution in algorithms for the best solution. Generally, a value set at initial may be positive or negative infinity; we have to take care that no input value is bigger or smaller.

Infinity in Python

In Python, there is no way or method to represent infinity as an integer. This matches the fundamental characteristic of many other popular programming languages. But due to python being dynamically typed language, you can use float(inf) as an integer to represent it as infinity.

Читайте также:  Vertical centering content css

Therefore in python, we cannot represent infinity, or we can say that there is no way to show the infinity as an integer. But we can use float (inf) as an integer.

In Python, positive infinity and negative infinity can be represented by:

Python Program to Define Positive and Negative Infinity Number

# Define Positive infinity number ptive_inf = float('inf') print('Positive Infinity: ',ptive_inf) # Define Negative infinity number ntive_inf = float('-inf') print('Negative Infinity: ',ntive_inf)
Positive Infinity: inf Negative Infinity: -inf

Represent Positive and Negative Infinity Using the Math Module

To determine the infinity number, you can use the python math module.

Note: This will work with only python 3.5 or higher version of python.

# Import math module import math # Positive infinity ptive_inf = math.inf print('Positive Infinity: ',ptive_inf) # Negative infinity ntive_inf = -math.inf print('Negative Infinity: ',ntive_inf)
Positive Infinity: inf Negative Infinity: -inf

Positive infinity number is greatest, and the negative infinity number is the smallest of all numbers.

In comparison, positive infinity is always bigger than every natural number.

(i.e. 0,1,2. +∞. Positive integer and +infinity.)

In comparison, negative infinity is smaller than the negative number.

(i.e., ∞-. -2,-1,0, 1,.2,3. – negative infinity and -integer.)

# Define Positive infinity number ptive_inf = float('inf') if 99999999999999999 > ptive_inf: print('Numer is greater than Positive infinity') else: print('Positive infinity is greater') # Define Negative infinity number ntive_inf = float('-inf') if -99999999999999999 > ptive_inf: print('Numer is smaller than Negative infinity') else: print('Negative infinity is smaller')
Positive infinity is greater Negative infinity is smaller

Python Program to Check If the Number Is Infinite

import math # Define positive infinity number ptive_inf = float('inf') print('Variable is Infinity: ',math.isinf(ptive_inf)) # Define negative infinity number ntive_inf = float('-inf') print('Variable is Infinity: ',math.isinf(ntive_inf)) Click and drag to move
Variable is Infinity: True Variable is Infinity: True

In the above example, we are using the using isinf method of math library to check if the number is infinity or not.

Represent Positive and Negative Infinity Using the Decimal Module

You can define positive and negative Infinity by using the decimal module.

from decimal import Decimal import math # Define positive infinity ptive_inf = Decimal('Infinity') print('Variable is Infinity: ',math.isinf(ptive_inf)) # Define negative infinity ntive_inf = Decimal('-Infinity') print('Variable Infinity: ',math.isinf(ntive_inf)) Click and drag to move
Variable is Infinity: True Variable Infinity: True

Represent Positive and Negative Infinity Using Numpy Library

You can define positive and negative Infinity by using the inf module of the NumPy library

import numpy as np import math # Define positive Infinity number ptive_inf = np.inf # Check is infinity number print('Positive infinity number: ',ptive_inf) # Define negative Infinity number ntive_inf = -np.inf # Check is infinity number print('Negative infinity number: ',ntive_inf)
Positive infinity number: inf Negative infinity number: -inf

Check If Positive Infinity Number Is Equal to Negative Infinity Number

You can simple check if the positive infinity number is equal to negative infinity number using simple “==” operator, and the output is always false.

import math # Define positive infinity number ptive_inf = float('inf') # Define negative infinity number ntive_inf = float('-inf') print('Positive infinity equal to Negative infinity: ',ptive_inf == ntive_inf)
Positive infinity equal to Negative infinity: False

Arithmetic operations on infinity number will give an infinite number

If you perform any arithmetic operation with positive or negative infinity numbers, the result will always be an endless number.

# Define positive infinity number ptive_inf = float('inf') # Multiply Positive infinity number by 5 print('Multiplication : ',ptive_inf * 5) # Addition to Positive infinity Number print('Addition : ',ptive_inf + 5) # Subtraction to Positive infinity Number print('Subtraction : ',ptive_inf - 5) # Division to Positive infinity Number print('Division : ',ptive_inf / 5) # Define Negative infinity number ntive_inf = float('-inf') # Multiply Negative infinity number by 5 print('Multiplication : ',ntive_inf * 5) # Addition to Negative infinity Number print('Addition : ',ntive_inf + 5) # Subtraction to Negative infinity Number print('Subtraction : ',ntive_inf - 5) # Division to Negative infinity Number print('Division : ',ntive_inf / 5)
Multiplication : inf Addition : inf Subtraction : inf Division : inf Multiplication : -inf Addition : -inf Subtraction : -inf Division : -inf 
  • Learn Python Programming
  • Addition of two numbers in Python
  • Null Object in Python
  • pip is not recognized
  • Python Comment
  • Python Factorial
  • Python String Replace
  • Python Max() Function
  • Python Pass Statement
  • Python zip()
  • Install Opencv Python PIP Windows
  • Python String Title() Method
  • String Index Out of Range Python
  • Area of Circle in Python
  • Python slice() function
  • Python Sort Dictionary by Key or Value
  • indentationerror: unindent does not match any outer indentation level in Python
  • Remove Punctuation Python
  • Python Infinity
  • Pangram Program in Python

Источник

How to Represent Infinity in Python?

Python Certification Course: Master the essentials

There are multiple ways to represent Infinity in Python. Some of the common ways to represent Infinity in Python are, using float() , using Python’s Math Module, using Python’s Decimal Module, and using Python’s Numpy Library. But first, let’s understand what is infinity in Python.

What is Infinity in Python?

Infinity in Python is an indeterminate number that can be either positive or negative. But can you guess where can we use such a number? Infinity in Python can be used in algorithms to compare solutions for the best answer. Consider the case where we want to find the maximum number in an array. Initially, we have to keep our answer as small as possible so that we can get our best answer. Let’s understand this with an example.

Consider the array as 1,2,3,4,5 and we set our initial ans as 0 . For each element of the array, we will compare the ans and the current element. And if the current element is greater than ans, we will update ans as the current element. Doing this on the above array will give us ans = 5.

example of infinity in python

The highlighted cell in the above image is our ans, this is valid for the current array. But now let’s change the array to -5,-4,-3,-2,-1 Again we set the initial ans to 0 , and if we try to perform the above approach ans will be equal to 0 at the end of the program.

example of infinity in python2

But as you can see in the array the maximum number is -1 . So the initial value of ans will decide whether we will get the correct answer or not. In the case of finding the maximum number we need our initial ans to be as small as possible, hence we can use infinity in Python to do so.

Representing Infinity as an Integer in Python

There are multiple ways of representing infinity in Python, let’s understand them one by one.

Method 1 : Using float(‘inf’) and float(‘-inf’)

We can use the float() function in Python to represent the positive infinity and negative infinity.
For positive infinity, we need to pass ‘inf’ to the float() function and for negative infinity, we need to pass ‘-inf’ to the float() function.
Let’s see the implementation of the above method,

You can learn more about the float() function in Python here float() in Python | Python float()

Method 2 : Using Python’s Math Module

We can also use the math module to represent infinity in Python, but it only works with Python 3.5 or higher. Since infinity in Python can be both positive and negative, it is denoted by math.inf and -math.inf .
Let’s see the implementation of the above method,

You can learn more about the math module in Python here Math Module in Python

Method 3: Using Python’s Decimal Module

We can also use the Decimal module to Python to represent infinity in Python. For positive infinity, we can use the Decimal(‘Infinity’) and for negative infinity, you guessed it right we can use the Decimal(‘-Infinity’).

Let’s see the implementation of the above method,

Method 4: Using Python’s Numpy Library

Another approach to represent infinity in Python is by using the NumPy module where numpy.inf and -numpy.inf represent positive and negative infinite, respectively.
Let’s see the implementation of the above method,

Mathematical Operations on Infinity in Python

Have you wondered what if we perform mathematical operations on infinity in Python? These operations can be done in two ways. Let’s also understand these.

Mathematical Operations with Two Infinity in Python

Addition Operation

As you can see in the output the addition of two positive infinity is infinity, the addition of two negative infinity is -infinity, and the addition of positive and negative infinity is not a number (nan).

Subtraction Operation

As you can see in the output that the subtraction of two positive infinity is not a number (nan), the subtraction of two negative infinity is also not a number (nan), and the subtraction of positive and negative infinity is infinity.

Multiplication Operation

As you can see in the output that the multiplication of two positive infinity is infinity, the multiplication of two negative infinity is also infinity, and the multiplication of positive and negative infinity is -infinity.

Division Operation

As you can see in the output the division of two positive infinity is not a number (nan) , the division of two negative infinity is also not a number (nan), and the division of positive and negative infinity is also not a number (nan).

Mathematical Operation with One Infinity in Python and One Finite Value

Addition Operation

The addition of a finite number to infinity in Python will result in infinity.

Subtraction Operation

The subtraction of a finite number from infinity in Python will result in infinity.

Multiplication Operation

The multiplication of infinity in Python with a finite number will result in infinity.

Division Operation

Division of infinity in Python with a finite number will result in infinity.

So we can conclude that if we perform the mathematical operations with one infinity in Python and one finite value, the result will be infinity.

Checking If a Number is Infinite or Not

We can use the isinf() method to check whether a number is an infinity or not. This method is present in the math library and it returns true if the provided number is infinity else it returns false. Let’s see the implementation for the above.

As you can see in the output x and y are infinity in Python, while a and b are finite values.

Comparing Infinite Values to Finite Values

The idea of comparing an infinite value to a finite value is very simple. Because positive infinity is always greater than any number and negative infinity is always less than any number. Let’s see the code for the above.

Here x is positive infinity and y is -infinity. And a and b are two finite values with values 999999999999 and -999999999999 respectively. As you can see in the output, x is greater than both a and b. And y is less than both a and b.

Conclusion

  • Infinity in Python is an indeterminate number that can be either positive or negative.
  • Infinity in Python can be represented using the float() method, using the math library, using the Decimal library, and using the numpy library.
  • Mathematical operations on infinity in Python results in either infinity or not a number (nan).
  • We can check whether a number is infinite or not by using the isinf() method present in the math library.
  • A positive infinity is always greater than any number and negative infinity is always less than any number.

Источник

Оцените статью