Search code examples
pythoncomplex-numbers

Why does Python return a complex number for operation x**x if -1 < x < 0?


I am on Windows 10 (64-Bit machine with 32-Bit Python 3.7). In IDLE, if I type:

>>> -0.001**-0.001
-1.0069316688518042

But if I do:

>>> x = -0.001
>>> x**x
(1.006926699847276 -0.0031633639300006526j)

Interestingly, the magnitude of this complex number is the same as the actual answer.

As a proof, I've attached screenshot of the same.

What could be causing this?


Solution

  • In the first case, you are not getting a complex number because ** has higher precedence than - (both in Python and in math), so you are actually doing -(0.001 ** -0.001). Try (-0.001) ** -0.001.

    The complex number is the "correct" answer by the mathematical definition of the power operation.