大多数C程序通过使用complex.h头文件来处理复数运算和操作。此头文件已添加到C99 Standard中。
C++标准库具有标头,该标头将复数实现为模板类complex
与
Macro Name | Expands To |
---|---|
complex | _Complex |
imaginary | _Imaginary |
_Complex_I | (const float _Complex) i |
_Imaginary_I | (const float _Imaginary) i |
I | _Imaginary_I(_Complex_I if _Imaginary_I is absent) |
下面的程序有助于创建复数。
范例1:
// C program to show the working
// of complex.h library
#include
#include
int main(void)
{
double real = 1.3,
imag = 4.9;
double complex z
= CMPLX(real, imag);
printf(
"z = %.1f% + .1fi\n",
creal(z), cimag(z));
}
输出:
z = 1.3+4.9i
解释:
- cmplx()函数通过将实部和虚部作为参数来创建复数对象。此函数返回复数的对象。
- creal()函数返回复数的实部
- cimag()函数返回复数的虚部
- 如果我们的实部和虚部是float类型,则使用cmplxf()函数生成复数,并使用crealf() , cimagf()函数获取实部和虚部。
- 如果我们的实部和虚部都是long double类型,则可以使用cmplxl()函数生成复数,而要获得实部和虚部,可以使用creall()和cimagl()函数。
示例2:我们还可以使用宏I创建复数对象。
// C program to create a complex
// number using macro I
#include
#include
int main(void)
{
double complex
z
= 3.2 + 4.1 * I;
// Creates complex number
// with 3.2 and 4.1 as
// real and imaginary parts
printf(
"z = %.1f% + .1fi\n",
creal(z), cimag(z));
}
输出:
z = 3.2+4.1i
与
Function | Description |
---|---|
float cabsf(float complex arg) double cabs(double complex arg) long double cabsl(long double complex arg) |
It returns the absolute value of complex argument |
float complex cacosf(float complex arg) double complex cacos(double complex arg) long double complex cacosl(long double complex arg) |
It returns the complex arc cosine values of complex argument |
float complex cacoshf(float complex arg) double complex cacosh(double complex arg) long double complex cacoshl(long double complex arg) |
It returns the complex arc hyperbolic cosine values of complex argument |
float cargf(float complex arg) double carg(double complex arg) long double cargl(long double complex arg) |
It returns the phase angle of complex argument (in radians). |
float complex casinf(float complex arg) double complex casin(double complex arg) long double complex casinl(long double complex arg) |
It returns the complex arc sine values of complex argument |
float complex casinhf(float complex arg) double complex casin(double complex arg) long double complex casinl(long double complex arg) |
It returns the complex arc hyperbolic sine values of complex argument |
float complex catanf(float complex arg) double complex catan(double complex arg) long double complex catanl(long double complex arg) |
It returns the complex arc tangent values of complex argument |
float complex catanhf(float complex arg) double complex catan(double complex arg) long double complex catanl(long double complex arg) |
It returns the complex arc hyperbolic tangent values of complex argument |
float complex ccosf(float complex arg) double complex ccos(double complex arg) long double complex ccosl(long double complex arg) |
It returns the complex cosine values of complex argument |
float complex cexpf(float complex arg) double complex cexp(double complex arg) long double complex cexpl(long double complex arg) |
It returns the complex value earg where e is the natural logarithm base |
float crealf(float complex arg) double creal(double complex arg) long double creall(long double complex arg) |
It returns the real part of the complex argument |
float cimagf(float complex arg) double cimag(double complex arg) long double cimagl(long double complex arg) |
It returns the imaginary part of the complex argument |
float complex clogf(float complex arg) double complex clog(double complex arg) long double complex cimagl(long double complex arg) |
It returns the imaginary part of the complex argument |
float complex conjf(float complex arg) double complex conj(double complex arg) long double complex conjl(long double complex arg) |
It returns the conjugate of complex argument |
float complex cpowf(float complex a, long double complex b) | It returns the complex value of ab |
float complex csqrtf(float complex arg) double complex csqrt(double complex arg) long double complex csqrtl(long double complex arg) |
It returns the complex square root of argument |
示例3:程序查找复数的共轭。
#include
#include
int main(void)
{
double real = 1.3,
imag = 4.9;
double complex z
= CMPLX(real, imag);
double complex
conj_f
= conjf(z);
printf("z = %.1f% + .1fi\n",
creal(conj_f),
cimag(conj_f));
}
输出:
z = 1.3-4.9i
示例4:查找复数的绝对值的程序。
#include
#include
int main(void)
{
double real = 1.3,
imag = 4.9;
double complex z
= CMPLX(real, imag);
printf("Absolute value = %.1f",
cabsf(z));
}
输出:
Absolute value = 5.1
示例4:程序来查找复数的相角。
#include
#include
int main(void)
{
double real = 1.3,
imag = 4.9;
double complex z
= CMPLX(real, imag);
printf(
"Phase Angle = %.1f radians\n",
cargf(z));
}
输出:
Phase Angle = 1.3 radians
想要从精选的最佳视频中学习和练习问题,请查看《基础知识到高级C的C基础课程》。