`carg`, `cargf`, `cargl` — 복소수 위상각

carg, cargf, cargl — 복소수 위상각

복소수 x + yi를 극좌표로 바꿀 때, 원점에서 그 점을 향하는 각도가 필요해요. cabs가 반지름(rho)을 담당했다면, 이 각도(theta)를 구하는 함수가 carg예요.

출처: cppreference — carg

본문

carg 계열 함수는 복소수 z의 인자(argument, 위상각 phase angle이라고도 해요)를 계산해요.

float       cargf( float complex z );        // (1)  (since C99)
double      carg( double complex z );        // (2)  (since C99)
long double cargl( long double complex z );  // (3)  (since C99)

// Defined in header <tgmath.h>
#define carg( z )                            // (4)  (since C99)

1-3) 음의 실수축을 따라 가지 자름(branch cut)을 두고 z의 인자(위상각)를 계산해요. 4) 타입 제네릭 매크로예요. zlong double complex, long double imaginary, 또는 long double이면 cargl, float complex·float imaginary·float이면 cargf, double complex·double imaginary·double·정수 타입이면 carg가 호출돼요.

매개변수

매개변수 설명
z 복소수 인자

반환값

오류가 없으면 z의 위상각을 구간 [−π; π] 안에서 반환해요.

오류와 특수한 경우는 함수가 atan2(cimag(z), creal(z))로 구현된 것처럼 처리돼요. atan2가 사분면을 부호로 판별하는 방식이라, -1 - 0i 같은 '가지 자름 반대편' 값도 올바른 각도로 나오는 거죠.

예시

여러 복소수의 위상각을 직접 확인해 보는 코드예요.

#include <stdio.h>
#include <complex.h>

int main(void)
{
    double complex z1 = 1.0+0.0*I;
    printf("phase angle of %.1f%+.1fi is %f\n", creal(z1), cimag(z1), carg(z1));

    double complex z2 = 0.0+1.0*I;
    printf("phase angle of %.1f%+.1fi is %f\n", creal(z2), cimag(z2), carg(z2));

    double complex z3 = -1.0+0.0*I;
    printf("phase angle of %.1f%+.1fi is %f\n", creal(z3), cimag(z3), carg(z3));

    double complex z4 = conj(z3); // or CMPLX(-1, -0.0)
    printf("phase angle of %.1f%+.1fi (the other side of the cut) is %f\n",
             creal(z4), cimag(z4), carg(z4));
}

출력:

phase angle of 1.0+0.0i is 0.000000
phase angle of 0.0+1.0i is 1.570796
phase angle of -1.0+0.0i is 3.141593
phase angle of -1.0-0.0i (the other side of the cut) is -3.141593

마지막 줄이 특히 흥미로워요. -1은 위상각 π(3.141593)인데, 허수부가 -0.0-1-0i(가지 자름 반대편)는 (-3.141593)가 나와요. atan2-0.0 부호까지 구분해서 각도를 정한다는 걸 보여주는 예시예요.

표준 참조

  • C11: 7.3.9.1 The carg functions (p: 196), 7.25 (p: 373-375), G.7 (p: 545)
  • C99: 7.3.9.1 (p: 178), 7.22 (p: 335-337), G.7 (p: 480)

더 알아보기

  • 복소수 크기 cabs와 함께 쓰면 직각좌표→극좌표 변환이 돼요.
  • atan2가 두 인자의 부호로 사분면을 판별하는 방식이 carg의 구현 원리예요.
  • cppreference의 carg 원문에서 참조 절을 더 확인할 수 있어요.