<p>By the <a href="https://en.cppreference.com/w/c/numeric/complex/clog" rel="noreferrer">C standard</a> and the <a href="https://en.cppreference.com/w/cpp/numeric/complex/log" rel="noreferrer">C++ standard</a>:</p>
<ul>
<li><span class="math-container">$\log(0+0i) = -\infty+0 i$</span>.</li>
<li><span class="math-container">$\log(0-0i) = -\infty-0 i$</span>.</li>
<li><span class="math-container">$\log(-0+0i) = -\infty+\pi i$</span>.</li>
<li><span class="math-container">$\log(-0-0i) = -\infty-\pi i$</span>.</li>
</ul>
<p>However, if the floating-point complex zero is understood as the &quot;absolute&quot; zero plus some infinitesimal toward a specific quadrant in the complex plane, there isn't really a justification for the resulting imaginary part. Indeed, it holds that:
<span class="math-container">$$
\lim_{z \to 0}\left(\log z\right)= -\infty+\lim_{z \to 0}\left(\arg z\right) \cdot i
$$</span>
And there is no value for <span class="math-container">$\lim_{z \to 0}\left(\arg z\right)$</span>; it's <span class="math-container">$\mathrm{NaN}$</span>.</p>
<p>So I'd insist <span class="math-container">$\log(\pm 0 \pm 0 i)$</span> should be <span class="math-container">$-\infty + \mathrm{NaN}i$</span>, regardless of the signs of the inputted zero. Why didn't C/C++ do this?</p>

<c><c++><math><numbers><floating-point>
In C/C++, why does the logarithm of zero have a well-defined imaginary part?
CC BY-SA 4.0
2024-12-09T09:02:37.883Z
some discussion here iirc https://people.freebsd.org/~das/kahan86branch.pdf
Moonchild
CC BY-SA 4.0
2024-12-09T13:20:52.643Z
Negative zero is generally treated as "generic negative number that's comically close to zero", and similarly for positive zero.
Someone
CC BY-SA 4.0
2024-12-09T20:52:09.720Z
If you define complex numbers as "an IEEE 754 real part and an IEEE 754 imaginary part", allowing each part independently to be infinity, -infinity or NaN, the result doesn't resemble a very useful superspace of the complex numbers, mathematically speaking. Usually we care about either the one-point compactification (aka the Riemann sphere, which is topologically a sphere), or otherwise we prefer to add infinities like $\infty \cdot e^{i\theta}$ for each angle $\theta$ (topologically a closed disc), or possibly identifying each with $\infty \cdot e^{i(\theta + \pi)}$ (a projective plane).
kaya3
CC BY-SA 4.0
2024-12-09T20:55:52.597Z
If you allow the real and imaginary parts to be infinite and finite respectively (or vice versa), you end up with a lot of infinite points whose arguments are 0, $\pi/2$, $\pi$ or $3\pi/2$, one each with argument $\pi/4$, $3\pi/4$, $5\pi/4$ and $7\pi/4$, and none with any other arguments. So overall, an extension of the complex numbers which allows the real and imaginary parts to independently be infinite or finite, is not mathematically very useful. Then if no mathematicians care what the correct infinite value in that space should be, there is no mathematically correct choice to please them.
kaya3
CC BY-SA 4.0
2024-12-09T20:57:59.483Z
Sure, there's a reasonable justification for NaN over 0 for the imaginary part of $\log 0$, but if this doesn't make any actual calculation more correct, what is the actual benefit? By the way, I've written this as comments because I'm not sure it's the kind of answer you're looking for; though if you do specifically want historical evidence of the designers' intent, it would be best to add the [tag:history] tag.
kaya3
CC BY-SA 4.0
2024-12-09T21:09:14.640Z
As an aside, C's complex type [is quite mathematically frought](https://www.mjr19.org.uk/IT/IEEE_complex.html) even before you reach transcendental functions.
kaya3
CC BY-SA 4.0
2024-12-10T15:04:55.680Z
This convention is consistent with limits along the positive and negative real axes. If you're going to give preferential treatment to the limit along a particular path, this seems like a reasonable choice.
Nate Eldredge
# In C/C++, why does the logarithm of zero have a well-defined imaginary part?
Posted by: username_0 on 2024-12-08
By the [C standard](https://en.cppreference.com/w/c/numeric/complex/clog) and the [C++ standard](https://en.cppreference.com/w/cpp/numeric/complex/log):

* $\log(0+0i) = -\infty+0 i$.
* $\log(0-0i) = -\infty-0 i$.
* $\log(-0+0i) = -\infty+\pi i$.
* $\log(-0-0i) = -\infty-\pi i$.

However, if the floating-point complex zero is understood as the "absolute" zero plus some infinitesimal toward a specific quadrant in the complex plane, there isn't really a justification for the resulting imaginary part. Indeed, it holds that:
$$
\lim\_{z \to 0}\left(\log z\right)= -\infty+\lim\_{z \to 0}\left(\arg z\right) \cdot i
$$
And there is no value for $\lim\_{z \to 0}\left(\arg z\right)$; it's $\mathrm{NaN}$.

So I'd insist $\log(\pm 0 \pm 0 i)$ should be $-\infty + \mathrm{NaN}i$, regardless of the signs of the inputted zero. Why didn't C/C++ do this?
**Tags:** c c++ math numbers floating-point
> **Comments:**
> **username_1**: some discussion here iirc https://people.freebsd.org/~das/kahan86branch.pdf  
> **username_2**: Negative zero is generally treated as "generic negative number that's comically close to zero", and similarly for positive zero.  
> **username_3**: If you define complex numbers as "an IEEE 754 real part and an IEEE 754 imaginary part", allowing each part independently to be infinity, -infinity or NaN, the result doesn't resemble a very useful superspace of the complex numbers, mathematically speaking. Usually we care about either the one-point compactification (aka the Riemann sphere, which is topologically a sphere), or otherwise we prefer to add infinities like $\infty \cdot e^{i\theta}$ for each angle $\theta$ (topologically a closed disc), or possibly identifying each with $\infty \cdot e^{i(\theta + \pi)}$ (a projective plane).  
> **username_3**: If you allow the real and imaginary parts to be infinite and finite respectively (or vice versa), you end up with a lot of infinite points whose arguments are 0, $\pi/2$, $\pi$ or $3\pi/2$, one each with argument $\pi/4$, $3\pi/4$, $5\pi/4$ and $7\pi/4$, and none with any other arguments. So overall, an extension of the complex numbers which allows the real and imaginary parts to independently be infinite or finite, is not mathematically very useful. Then if no mathematicians care what the correct infinite value in that space should be, there is no mathematically correct choice to please them.  
> **username_3**: Sure, there's a reasonable justification for NaN over 0 for the imaginary part of $\log 0$, but if this doesn't make any actual calculation more correct, what is the actual benefit? By the way, I've written this as comments because I'm not sure it's the kind of answer you're looking for; though if you do specifically want historical evidence of the designers' intent, it would be best to add the [tag:history] tag.  
> **username_3**: As an aside, C's complex type [is quite mathematically frought](https://www.mjr19.org.uk/IT/IEEE_complex.html) even before you reach transcendental functions.  
> **username_4**: This convention is consistent with limits along the positive and negative real axes. If you're going to give preferential treatment to the limit along a particular path, this seems like a reasonable choice.  
---
## 0 answers
