Namespaces
Variants

CMPLXF, CMPLX, CMPLXL

From cppreference.net
定义于头文件 <complex.h>
float complex CMPLXF ( float real, float imag ) ;
(C11 起)
double complex CMPLX ( double real, double imag ) ;
(C11 起)
long double complex CMPLXL ( long double real, long double imag ) ;
(C11 起)

这些宏中的每一个都会展开为一个表达式,该表达式求值结果为指定复数类型的值,其实部具有 real 的值(转换为指定的参数类型),虚部具有 imag 的值(转换为指定的参数类型)

当表达式 real imag 本身符合要求时,这些表达式适合用作具有静态或线程存储期对象的初始化器。

目录

参数

real - 待返回复数的实部
imag - 待返回复数的虚部

返回值

一个由 real imag 分别作为实部与虚部构成的复数。

注释

这些宏的实现方式如同支持虚数类型(即使实际上不支持且 _Imaginary_I 实际上未定义),其定义方式如下:

#define CMPLX(x, y) ((double complex)((double)(x) + _Imaginary_I * (double)(y)))
#define CMPLXF(x, y) ((float complex)((float)(x) + _Imaginary_I * (float)(y)))
#define CMPLXL(x, y) ((long double complex)((long double)(x) + \
                      _Imaginary_I * (long double)(y)))

示例

#include <stdio.h>
#include <complex.h>
int main(void)
{
    double complex z = CMPLX(0.0, -0.0);
    printf("z = %.1f%+.1fi\n", creal(z), cimag(z));
}

输出:

z = 0.0-0.0i

参考文献

  • C11 标准 (ISO/IEC 9899:2011):
  • 7.3.9.3 CMPLX 宏 (p: 197)

参见

虚数单位常量 i
(宏常量)