complex.h 初探

Cplusplus
回复
头像
vicyang
版主
版主
帖子: 56
注册时间: 2016年07月21日 20:35
联系:

complex.h 初探

帖子 vicyang »

https://gcc.gnu.org/onlinedocs/gcc/Complex.html
https://en.wikipedia.org/wiki/C_mathematical_functions#complex.h

初始化:
complex a;

初始化 + 赋值:
complex b = -1.0fi + 3.0f;

赋值
a = 6.0i;
a = 2.5i + 6.0;
带有 i 或者 j 的部分代表虚部,i 和 j 的作用等价。如果不指出实部,则实部为0.0。

单独获取和打印虚部和实部的值
creal(a)
__real__(a)

cimag(a)
__imag__(a)

printf("%f %f", creal(a), __imag__(a) );

四则运算
看了complex.h , ccos, cpow, csin, csqrt 等函数都有,就是没有plus , add, increase, multiply 之类
一番懵逼之后,发现可以直接 + - * / :speechless3
x = a + a;
x = a - b;
x = b * b;
x = b / a;
a /= 2.0;

complex.h内声明的函数(描述摘自wiki)
Function    Description
Basic operations
cabs computes absolute value (C99)
carg computes argument of a complex number (C99)
cimag computes imaginary part of a complex number (C99)
creal computes real part of a complex number (C99)
conj computes complex conjugate (C99)
cproj computes complex projection into the Riemann sphere (C99)

Exponentiation operations
cexp computes complex exponential (C99)
clog computes complex logarithm (C99)
csqrt computes complex square root (C99)
cpow computes complex power (C99)

Trigonometric operations
csin computes complex sine (C99)
ccos computes complex cosine (C99)
ctan computes complex tangent (C99)
casin computes complex arc sine (C99)
cacos computes complex arc cosine (C99)
catan computes complex arc tangent (C99)

Hyperbolic operations
csinh computes complex hyperbolic sine (C99)
ccosh computes complex hyperbolic cosine (C99)
ctanh computes complex hyperbolic tangent (C99)
casinh computes complex hyperbolic arc sine (C99)
cacosh computes complex hyperbolic arc cosine (C99)
catanh computes complex hyperbolic arc tangent (C99)
这些函数默认的传入和传出类型是 double,单独加f后缀时表示 float, 单独加l后缀表示 long double

实际使用
如果虚部的数字是来自其他浮点型变量,可以 将这个变量 * 1.0i ,示例
complex a;
float v = 5.0, w = 20.0;

a = v * 1.0i + w;
待编辑
回复

在线用户

正浏览此版面之用户: 没有注册用户 和 3 访客