- 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 库 - 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
广告