名稱空間
變體
操作

CMPLXF, CMPLX, CMPLXL

來自 cppreference.com
< c‎ | 數值‎ | 複數
定義在標頭檔案 <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 的值(轉換為指定的引數型別)。

只要表示式 realimag 也適合,這些表示式就適合用作具有靜態或執行緒儲存持續時間物件的初始化器。

目錄

[編輯] 引數

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
(宏常量) [編輯]
C++ 文件,關於 complex