当前位置: C语言 -- 标准库 -- <complex.h> -- CMPLX

CMPLX宏


概要:
#include <complex.h>
double complex CMPLX(double x, double y);

描述:

该宏是一个函数式宏,可以构造一个double complex类型的复数,该复数实数部分具有参数x的值,虚数部分具有参数y的值。

构造得到的复数应适合初始化具有静态存储期限的对象或者线程存储期限的对象,前提是两个参数都合适。

CMPLX的实现就像实现支持虚数类型一样,其定义如下所示:

#define CMPLX(x, y) ((double complex)((double)(x) + _Imaginary_I*(double)(y)))


参数:
double x

参数为一个double类型的浮点数。

double y

参数为一个double类型的浮点数。


返回值:

该宏返回x + iy


范例:
1 
2 
3 
4 
5 
6 
7 
8 
9 
10 
11 
12 
13 
14 
15 
16 
17 
18 
19 
20 
21 
22 
/*宏CMPLX范例*/

#ifdef __STDC_NO_COMPLEX__
#error "Implementation does not support complex types."
#endif

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

int main(void)
{
    double x = 1.0;
    double y = 2.0;
    double complex z;
    char *ptr = "CMPLX(1.0, 2.0)";

    z = CMPLX(x, y);
    printf("%s = %.2f + %.2fi\n", ptr, creal(z),cimag(z));

    return 0;
}

输出:

CMPLX(1.0, 2.0) = 1.00 + 2.00i

:使用ideone编译。


相关内容:
CMPLXF 构造float complex类型复数的宏。
CMPLXL 构造long double complex类型复数的宏。