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)
OutputComplex 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)
OutputComplex 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)
OutputError: can't convert complex to float
Similar Reads
Convert String to Float in Python The goal of converting a string to a float in Python is to ensure that numeric text, such as "33.28", can be used in mathematical operations. For example, if a variable contains the value "33.28", we may want to convert it to a float to perform operations like addition or division. Let's explore dif
2 min read
Convert Python String to Float datatype Let us see how to convert a string object into a float object. We can do this by using these functions : float() decimal() Method 1: Using float() python3 # declaring a string str1 = "9.02" print("The initial string : " + str1) print(type(str1)) # converting into float str2 = flo
2 min read
Python - Check for float string Checking for float string refers to determining whether a given string can represent a floating-point number. A float string is a string that, when parsed, represents a valid float value, such as "3.14", "-2.0", or "0.001".For example:"3.14" is a float string."abc" is not a float string.Using try-ex
2 min read
float() in Python Python float() function is used to return a floating-point number from a number or a string representation of a numeric value. Example: Here is a simple example of the Python float() function which takes an integer as the parameter and returns its float value. Python3 # convert integer value to floa
3 min read
How to convert Float to Int in Python? In Python, you can convert a float to an integer using type conversion. This process changes the data type of a value However, such conversions may be lossy, as the decimal part is often discarded.For example:Converting 2.0 (float) to 2 (int) is safe because here, no data is lost.But converting 3.4
5 min read