Professional Documents
Culture Documents
Complex Numbers in Pytho1
Complex Numbers in Pytho1
Output:
5. isfinite() :- Returns true if both real and imaginary part of complex number
are finite, else returns false.
6. isinf() :- Returns true if either real or imaginary part of complex number
is/are infinite, else returns false.
7. isnan() :- Returns true if either real or imaginary part of complex number
is NaN , else returns false.
# Python code to demonstrate the working of
# isnan(), isinf(), isfinite()
# importing "cmath" for complex number operations
import cmath
import math
# Initializing real numbers
x = 1.0
y = 1.0
a = math.inf
b = math.nan
# converting x and y into complex number
z = complex(x,y);
# converting x and a into complex number
w = complex(x,a);
# converting x and b into complex number
v = complex(x,b);
# checking if both numbers are finite
if cmath.isfinite(z):
print ("Complex number is finite")
else : print ("Complex number is infinite")
# checking if either number is/are infinite
if cmath.isinf(w):
print ("Complex number is infinite")
else : print ("Complex number is finite")
# checking if either number is/are infinite
if cmath.isnan(v):
print ("Complex number is NaN")
else : print ("Complex number is not NaN")
Output:
Constants
There are two constants defined in cmath module, “pi”, which returns the
numerical value of pi. The second one is “e” which returns the numerical value
of exponent.
# Python code to demonstrate the working of
# pi and e
# importing "cmath" for complex number operations
import cmath
import math
# printing the value of pi
print ("The value of pi is : ", end="")
print (cmath.pi)
# printing the value of e
print ("The value of exponent is : ", end="")
print (cmath.e)
Output: