Rom Rom - 2 months ago 9

Python Question

i have this problem with my Python code :

`from math import *`

m = pow(complex(1,2)*complex(3,0) + complex(1,0),complex(-1,0));

TypeError: can't convert complex to float

Does anyone know how to solve this problem ?

Thanks a lot !

Answer

The problem is that you are using `from math import *`

. This shadows the built-in `pow`

with a version that doesn't support complex numbers.

```
>>> pow(1+1j, 1)
(1+1j)
>>> import math
>>> math.pow(1+1j,2)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: can't convert complex to float
```

`from <x> import *`

is usually considered bad practice, and cases like this are why.

Instead, you should use `import math`

, and reference all your math functions as, e.g. `math.sqrt`

Alternatively, you can use `**`

instead of `pow`

:

```
>>> 1j ** 2
(-1+0j)
```

Source (Stackoverflow)

Comments