如何在 C 中处理复数?
如何在 C 中处理复数?我看到有一个 complex.h
头文件,但它没有提供有关如何使用它的太多信息。如何有效地访问实部和虚部?是否有获取模块和阶段的本机函数?
How can I work with complex numbers in C? I see there is a complex.h
header file, but it doesn't give me much information about how to use it. How to access real and imaginary parts in an efficient way? Is there native functions to get module and phase?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
这段代码会对您有所帮助,而且它的含义是相当不言自明的:
with:
creal(z1)
: 获取实部(对于 floatcrealf(z1)
,对于 long doublecreall(z1)
)cimag(z1)
: 获取虚部(对于 floatcimagf(z1)
,对于 long doublecimagl(z1)
)处理复数时要记住的另一个要点是
cos()
、exp()
和等函数>sqrt()
必须替换为其复杂形式,例如ccos()
、cexp()
、csqrt()
。This code will help you, and it's fairly self-explanatory:
with:
creal(z1)
: get the real part (for floatcrealf(z1)
, for long doublecreall(z1)
)cimag(z1)
: get the imaginary part (for floatcimagf(z1)
, for long doublecimagl(z1)
)Another important point to remember when working with complex numbers is that functions like
cos()
,exp()
andsqrt()
must be replaced with their complex forms, e.g.ccos()
,cexp()
,csqrt()
.从 C99 标准开始,复杂类型就出现在 C 语言中(GCC 的
-std=c99
选项)。一些编译器甚至可以在更早期的模式下实现复杂类型,但这是非标准和不可移植的扩展(例如IBM XL,GCC,可能是intel,...)。您可以从 http://en.wikipedia.org/wiki/Complex.h 开始- 它给出了来自complex.h的函数的描述
本手册http://pubs.opengroup.org/onlinepubs/009604499/basedefs/complex.h.html 还提供了一些有关宏的信息。
要声明复杂变量,请使用
或
要给复杂变量赋值,请使用
complex.h
中的_Complex_I
宏:(实际上,这里可能存在一些问题 < code>(0,-0i) 复数的单半中的数字和 NaN)
模块是
cabs(a)
/cabsl(c)
/cabsf(b)
;实部为creal(a)
,虚部为cimag(a)
。carg(a)
用于复杂的参数。要直接访问(读/写)真实的 imag 部分,您可以使用这个不可移植 GCC 扩展:
Complex types are in the C language since C99 standard (
-std=c99
option of GCC). Some compilers may implement complex types even in more earlier modes, but this is non-standard and non-portable extension (e.g. IBM XL, GCC, may be intel,... ).You can start from http://en.wikipedia.org/wiki/Complex.h - it gives a description of functions from complex.h
This manual http://pubs.opengroup.org/onlinepubs/009604499/basedefs/complex.h.html also gives some info about macros.
To declare a complex variable, use
or
To give a value into complex, use
_Complex_I
macro fromcomplex.h
:(actually there can be some problems here with
(0,-0i)
numbers and NaNs in single half of complex)Module is
cabs(a)
/cabsl(c)
/cabsf(b)
; Real part iscreal(a)
, Imaginary iscimag(a)
.carg(a)
is for complex argument.To directly access (read/write) real an imag part you may use this unportable GCC-extension:
为了方便起见,可以包含用于类型生成宏的
tgmath.h
库。它为所有类型的变量创建与双精度版本相同的函数名称。例如,它定义了一个sqrt()
宏,该宏扩展为sqrtf()
、sqrt()
或sqrtl()
函数,具体取决于提供的参数类型。这样就不需要记住不同类型的变量对应的函数名了!
For convenience, one may include
tgmath.h
library for the type generate macros. It creates the same function name as the double version for all type of variable. For example, For example, it defines asqrt()
macro that expands to thesqrtf()
,sqrt()
, orsqrtl()
function, depending on the type of argument provided.So one don't need to remember the corresponding function name for different type of variables!
由于计算负二次根的需要,复数的概念被引入数学中。复数概念被各个工程领域所采用。
如今,复数广泛应用于物理、电子、机械、天文学等高级工程领域......
负平方根示例的实部和虚部:
The notion of complex numbers was introduced in mathematics, from the need of calculating negative quadratic roots. Complex number concept was taken by a variety of engineering fields.
Today that complex numbers are widely used in advanced engineering domains such as physics, electronics, mechanics, astronomy, etc...
Real and imaginary part, of a negative square root example:
要提取复值表达式
z
的实部,请使用__real__ z
表示法。同样,使用
z
上的__imag__
属性来提取虚部。例如;
r 是复数“z”的实部
i 是复数“z”的虚部
To extract the real part of a complex-valued expression
z
, use the notation as__real__ z
.Similarly, use
__imag__
attribute on thez
to extract the imaginary part.For example;
r is the real part of the complex number "z"
i is the imaginary part of the complex number "z"