Python cmath.isinf() Method with Examples

cmath.isinf() Method in Python:

The cmath.isinf() method determines whether or not a value is positive or negative infinity.

This method gives the following Boolean value: If the value is infinity, True; otherwise, False.

Syntax:

cmath.isinf(x)

Parameters

x: This is Required. The value used to test for infinity

Return Value:

Returns a bool value that is True if the value is infinite and False otherwise.

Examples:

Example1:

Input:

Given Complex Number = 3+4j

Output:

The result after applying isinf() method to the given complex number (3+4j)  = 
False

Example2:

Input:

Given realpart = 5
Given imaginary part = 2

Output:

Enter real part and complex part of the complex number = 5 2
The result after applying isinf() method to the given complex number (5+2j) = 
False

Note: The above input format is for dynamic input.

cmath.isinf() Method with Examples in Python

Method #1: Using Built-in Functions (Static Input)

Approach:

  • Import cmath module(for complex number operations) using the import keyword.
  • Give the complex number as static input and store it in a variable.
  • Pass the given complex number as an argument to the cmath.isinf() method that determines whether a given complex number’s value is positive or negative infinity.
  • Store it in another variable.
  • Print the above result after applying isinf() method to the given complex number.
  • The Exit of the Program.

Below is the implementation:

# Import cmath module(for complex number operations) using the import keyword.
import cmath
# Give the complex number as static input and store it in a variable.
complexnumb = 3 + 4j
# Pass the given complex number as an argument to the cmath.isinf() method
# that determines whether a given complex number's value is positive or
# negative infinity.
# Store it in another variable.
rslt = cmath.isinf(complexnumb)
# Print the above result after applying isinf() method to the given complex number.
print("The result after applying isinf() method to the given complex number", complexnumb,
      " = ")
print(rslt)

Output:

The result after applying isinf() method to the given complex number (3+4j)  = 
False

Similarly, try for the other examples

import cmath
complexnumb = complex(10 + float('inf'))
rslt = cmath.isinf(complexnumb)
print("The result after applying isinf() method to the given complex number", complexnumb,
      " = ")
print(rslt)

Output:

The result after applying isinf() method to the given complex number (inf+0j)  = 
True

Method #2: Using Built-in Functions (User Input)

Approach:

  • Import cmath module(for complex number operations) using the import keyword.
  • Give the real part and imaginary part of the complex number as user input using map(), int(), split().
  • Store it in two variables.
  • Using a complex() function convert those two variables into a complex number and store them in a variable.
  • Pass the given complex number as an argument to the cmath.isinf() method that determines whether a given complex number’s value is positive or negative infinity.
  • Store it in another variable.
  • Print the above result after applying isinf() method to the given complex number.
  • The Exit of the Program.

Below is the implementation:

# Import cmath module(for complex number operations) using the import keyword.
import cmath
# Give the real part and imaginary part of the complex number as user input
# using map(), int(), split().
# Store it in two variables.
realnumb, imaginarynumb = map(int, input(
    'Enter real part and complex part of the complex number = ').split())
# Using a complex() function convert those two variables into a complex number.
complexnumb = complex(realnumb, imaginarynumb)

# Pass the given complex number as an argument to the cmath.isinf() method
# that determines whether a given complex number's value is positive or
# negative infinity.
# Store it in another variable.
rslt = cmath.isinf(complexnumb)
# Print the above result after applying isinf() method to the given complex number.
print("The result after applying isinf() method to the given complex number", complexnumb,
      " = ")
print(rslt)

Output:

Enter real part and complex part of the complex number = 5 2
The result after applying isinf() method to the given complex number (5+2j) = 
False