Open In App

Python Complex to Float

Last Updated : 19 Jan, 2024
Improve
Improve
Like Article
Like
Save
Share
Report

Complex numbers have two parts one that is a regular number (real) and another that is like a special extra part (imaginary). Floats, on the flip side, are straightforward numbers with decimals. So, complex numbers are a bit fancy with both real and imaginary bits, while float are just regular numbers with dots.

Python Complex to Float

Below are some ways and examples by which we can convert complex numbers to float in Python:

Convert Real Part to Float in Python

In this example, a complex number (3 + 4j) is assigned to the variable “complex_number.” The real part of the complex number is then converted to a float.

Python3




complex_number = 3 + 4j
 
real_part_float = float(complex_number.real)
 
print("Complex Number:", complex_number)
print("Real Part as Float:", real_part_float)


Output

Complex Number: (3+4j)
Real Part as Float: 3.0


Converting Imaginary Part to Float in Python

In this example, a complex number (3 + 4j) is assigned to the variable “complex_number.” The imaginary part of the complex number is then converted to a float.

Python3




complex_number = 3 + 4j
 
imag_part_float = float(complex_number.imag)
 
print("Complex Number:", complex_number)
print("Imaginary Part as Float:", imag_part_float)


Output

Complex Number: (3+4j)
Imaginary Part as Float: 4.0


Python Complex Matrix Multiplication with Float Output

In this example, two complex matrices, “matrix_a” and “matrix_b,” are multiplied using NumPy dot product function. The resulting complex matrix is stored in “result_matrix,” and its real part is extracted and converted to a float. The original matrices, the result matrix, and the real part as a float are then printed.

Python3




import numpy as np
 
matrix_a = np.array([[2 + 3j, 1 - 2j], [0.5 + 1j, -2]])
matrix_b = np.array([[1 - 1j, 2 + 2j], [3j, 4]])
 
result_matrix = np.dot(matrix_a, matrix_b)
 
real_part = float(result_matrix.real)
 
print("Matrix A:\n", matrix_a)
print("Matrix B:\n", matrix_b)
print("Result Matrix (Real Part as Float):\n", result_matrix)
print("Real Part as Float:", real_part)


Output

Matrix A:

[[2.+3.j 1.-2.j]
[0.5+1.j -2.+0.j]]

Matrix B:

[[1.-1.j 2.+2.j]
[0.+3.j 4.+0.j]]

Result Matrix (Real Part as Float):

[[ 7.+14.j -2. -8.j]
[ 4. +1.j -9. +2.j]]

Real Part as Float: 7.0

Error While Converting Complex Number to Float

If we try to change a complex number, like 3 + 4j, directly into float using float(), it will result in an error. This is because complex numbers consist of both real and imaginary parts, and converting them to float is not allowed in Python. The error occurs because there is no straightforward way to represent the entire complex number as a single float number.

Python3




# Attempting to convert complex number to float
complex_num = 3 + 4j
try:
    int_representation = float(complex_num)
except TypeError as e:
    print("Error:",e)


Output

Error: can't convert complex to float




Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads