非常教程

C参考手册

数值 | Numerics

carg

在头文件<math.h>中定义

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)z用沿着负实轴的分支来计算参数(也称为相位角)。

4)式泛型宏:如果z具有类型long double complexlong double imaginarylong doublecargl被调用。如果z有类型float complexfloat imaginary或者floatcargf被调用。如果z有类型double complexdouble imaginarydouble,或任何整数类型,carg被调用。

参数

z

-

复杂的论点

返回值

如果没有错误发生,则返回z间隔(-π;π)的相位角。

处理错误和特殊情况,就像该函数被实现为atan2(cimag(z), creal(z))

#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

参考

  • C11标准(ISO / IEC 9899:2011):
    • 7.3.9.1 carg函数(p:196)
    • 7.25类型通用数学<tgmath.h>(p:373-375)
    • G.7类型 - 通用数学<tgmath.h>(p:545)
  • C99标准(ISO / IEC 9899:1999):
    • 7.3.9.1 carg函数(p:178)
    • 7.22类型通用数学<tgmath.h>(p:335-337)
    • G.7类型 - 通用数学<tgmath.h>(p:480)
C

C 语言是一门通用计算机编程语言,应用广泛。C 语言的设计目标是提供一种能以简易的方式编译、处理低级存储器、产生少量的机器码以及不需要任何运行环境支持便能运行的编程语言。