I noticed that when working with double complex
numbers in C, when a real number with a negative 0 imaginary part is passed to carg()
-M_PI
is returned rather than 0 or M_PI
. Why is that? I found this out because another function that used carg()
returned the wrong sign for the imaginary part (the function that gets broken by this behavior of carg()
is newlibs cpow).
Here is a MWE to show the behavior:
#include <stdio.h>
#include <complex.h>
#include <math.h>
int main(void) {
double complex a = 2;
printf("creal(a): %f\n", creal(a));
printf("cimag(a): %f\n", cimag(a));
printf("carg(a): %f\n", carg(a));
printf("\n");
printf("creal(-a): %f\n", creal(-a));
printf("cimag(-a): %f\n", cimag(-a));
printf("carg(-a): %f\n", carg(-a));
printf("\n");
a = 0;
printf("creal(-a): %f\n", creal(-a));
printf("cimag(-a): %f\n", cimag(-a));
printf("carg(-a): %f\n", carg(-a));
}
and here is the result after compiling with gcc 14.2.0:
creal(a): 2.000000
cimag(a): 0.000000
carg(a): 0.000000
creal(-a): -2.000000
cimag(-a): -0.000000
carg(-a): -3.141593
creal(-a): -0.000000
cimag(-a): -0.000000
carg(-a): -3.141593
The phase angle of a complex number x + yi, which is what carg
calculates, is defined as atan y/x.
Because of this, the C standard defines carg(x + yi)
as atan2(y,x)
in section G.6p3.
Section F.10.1.4 of the C standard dictates that atan2
returns the following values for certain edge cases:
atan2(±0, −0)
returns ± πatan2(±0, +0)
returns ±0.atan2(±0, x)
returns ± π for x < 0.atan2(±0, x)
returns ±0 for x > 0.atan2(y, ±0)
returns − π /2 for y < 0.atan2(y, ±0)
returns π /2 for y > 0.atan2(±y, − ∞)
returns ± π for finite y > 0.atan2(±y, + ∞)
returns ±0 for finite y > 0.atan2(± ∞, x)
returns ± π /2 for finite x.atan2(± ∞, − ∞)
returns ±3 π /4.atan2(± ∞, + ∞)
returns ± π /4.
This is consistent with the output you see.
Section 7.3.3p1 goes into more detail on this:
Some of the functions below have branch cuts, across which the function is discontinuous. For implementations with a signed zero (including all IEC 60559 implementations) that follow the specifications of annex G, the sign of zero distinguishes one side of a cut from another so the function is continuous (except for format limitations) as the cut is approached from either side. For example, for the square root function, which has a branch cut along the negative real axis, the top of the cut, with imaginary part +0, maps to the positive imaginary axis, and the bottom of the cut, with imaginary part −0, maps to the negative imaginary axis