I've been finding a strange behaviour of log
functions in C++ and numpy about the behaviour of log
function handling complex infinite numbers. Specifically, log(inf + inf * 1j)
equals (inf + 0.785398j)
when I expect it to be (inf + nan * 1j)
.
When taking the log of a complex number, the real part is the log of the absolute value of the input and the imaginary part is the phase of the input. Returning 0.785398 as the imaginary part of log(inf + inf * 1j)
means it assumes the inf
s in the real and the imaginary part have the same length.
This assumption does not seem to be consistent with other calculation, for example, inf - inf == nan
, inf / inf == nan
which assumes 2 inf
s do not necessarily have the same values.
Why is the assumption for log(inf + inf * 1j)
different?
Reproducing C++ code:
#include <complex>
#include <limits>
#include <iostream>
int main() {
double inf = std::numeric_limits<double>::infinity();
std::complex<double> b(inf, inf);
std::complex<double> c = std::log(b);
std::cout << c << "\n";
}
Reproducing Python code (numpy):
import numpy as np
a = complex(float('inf'), float('inf'))
print(np.log(a))
EDIT: Thank you for everyone who's involved in the discussion about the historical reason and the mathematical reason. All of you turn this naive question into a really interesting discussion. The provided answers are all of high quality and I wish I can accept more than 1 answers. However, I've decided to accept @simon's answer as it explains in more detail the mathematical reason and provided a link to the document explaining the logic (although I can't fully understand it).