- C 標準庫
- C 庫 - 首頁
- C 庫 - <assert.h>
- C 庫 - <complex.h>
- C 庫 - <ctype.h>
- C 庫 - <errno.h>
- C 庫 - <fenv.h>
- C 庫 - <float.h>
- C 庫 - <inttypes.h>
- C 庫 - <iso646.h>
- C 庫 - <limits.h>
- C 庫 - <locale.h>
- C 庫 - <math.h>
- C 庫 - <setjmp.h>
- C 庫 - <signal.h>
- C 庫 - <stdalign.h>
- C 庫 - <stdarg.h>
- C 庫 - <stdbool.h>
- C 庫 - <stddef.h>
- C 庫 - <stdio.h>
- C 庫 - <stdlib.h>
- C 庫 - <string.h>
- C 庫 - <tgmath.h>
- C 庫 - <time.h>
- C 庫 - <wctype.h>
- C 標準庫資源
- C 庫 - 快速指南
- C 庫 - 有用資源
- C 庫 - 討論
- C 程式設計資源
- C 程式設計 - 教程
- C - 有用資源
C 庫 - creal() 函式
C 的complex 庫 creal() 函式通常用於提取複數的實部。複數由兩個部分組成:實部和虛部 (imag)。
如果實部是“float”型別,我們可以使用crealf()獲取實部,對於長雙精度型,使用creall()。
語法
以下是 creal() 函式的 C 庫語法:
double creal( double complex z );
引數
此函式接受單個引數:
-
Z - 它代表一個複數。
返回值
此函式返回複數 (z) 的實部。
示例 1
以下是一個基本的 C 程式,演示瞭如何使用 creal() 獲取複數 (z) 的實部。
#include <stdio.h>
#include <complex.h>
int main(void){
double complex z = 1.0 + 2.0 * I;
printf("The real part of z: %.1f\n", creal(z));
}
輸出
以下是輸出:
The real part of z: 1.0
示例 2
讓我們來看另一個例子,我們使用 creal() 函式來獲取生成的複數的實部。
#include <stdio.h>
#include <complex.h>
int main() {
double real = 3.0;
double imag = 4.0;
// Use the CMPLX function to create complex number
double complex z = CMPLX(real, imag);
printf("The complex number is: %.2f + %.2fi\n", creal(z), cimag(z));
// obtain the real part
// use creal()
printf("The real part of z: %.1f\n", creal(z));
return 0;
}
輸出
以下是輸出:
The complex number is: 3.00 + 4.00i The real part of z: 3.0
示例 3
以下 creal() 函式的示例將演示如何將其與複數一起使用。
#include <stdio.h>
#include <complex.h>
int main(void) {
double complex z = 10.0 + 5.0 * I;
// Get the real part
double realNum = creal(z);
// Print the result
printf("The Complex number: %.2f + %.2fi\n", creal(z), cimag(z));
printf("The Real part: %.2f\n", realNum);
return 0;
}
輸出
以下是輸出:
The Complex number: 10.00 + 5.00i The Real part: 10.00
c_library_complex_h.htm
廣告