Why does Python's calculation of $0^0$ include the digits of pi?

161 Views Asked by At

When you run the following Python code, which is equivalent to $\lim_{x \to 0^+} x^x$,

x = 0.0000000001; print(x ** x)

The output will correctly approach 1. (The output is 0.9999999976974149).

But when you change:

x = -0.0000000001

Which would be equivalent to $\lim_{x \to 0^-} x^x$, the following output is produced:

(1.000000002302585-3.141592660823578e-10j)

This is also correct but why do I see an approximation of $\pi$? Surely this can't be a coincidence. Is it the side product of an algorithm that's being used? Or is there a purely mathematical reason for it?

Edit: I only tested it in python. I don't know if similar outputs arise in other programming languages.

2

There are 2 best solutions below

2
On BEST ANSWER

Expanding slightly on my comment: If $x < 0$ is real and $|x| \ll 1$, we have

\begin{align} x^x & = e^{x \ln x} \\ & = e^{x [\ln |x| + \ln (-1)]} \\ & = e^{x(\ln |x| + \pi i)} \\ & \approx 1+x(\ln |x| + \pi i) \\ & = 1 + x\ln |x| + x \pi i \end{align}

Here, we have $\ln |x| = \ln 10^{-10} = -10 \ln 10 \approx -23.026$, so $x\ln |x| \approx 2.3026 \times 10^{-9}$, and

\begin{align} x^x & \approx 1 + 2.3026 \times 10^{-9} - 10^{-10}\pi i \\ & \approx 1.0000000023026 - 0.000000000314159 i \end{align}

which reproduces the output of your code.


Note that when we have $x = 10^{-10}$ (the positive side), your result is approximately $1-10^{-9} \ln 10$. You may not have noticed this because in one case that small amount was subtracted from $1$, and in the other case it was added to $1$.

3
On

Assuming the software calculates an approximation for $x=-10^{-k}$ and complex exponentiation used

$\ln(x)=-k\ln(10)+i\pi+2in\pi\quad$ with principal value $n=0$ used then:

$x^x=\exp\Big(-10^{-k}(-k\ln(10)+i\pi)\Big)=1+k10^{-k}\ln(10)-\pi10^{-k}+o(10^{-k})$

by order $1$ Taylor expansion $e^u=1+u+o(u)$

The displayed result suggest $k=10$ was used, this result in the approximation with $16$ digits:

$x^x\approx 1.000000002302585 - 3.141592653589793e^{-10} i$