C Standard Library
is a header file in the C standard library that supports complex number operations.
Introduced in the C99 standard, it provides a set of types, macros, and functions for defining and manipulating complex numbers.
1. Complex type
The following complex types are defined:
-
float complex: single-precision complex number. -
double complex: double-precision complex number. -
long double complex: long double-precision complex number.
These types are actually macros defined in the C standard library, expanding respectively to _Complex float, _Complex double and _Complex long double.
2. Complex constants
The following macros are defined to represent the imaginary unit i:
-
I: represents the imaginary uniti, which means itsqrt(-1).
Example
double
complex z
=
3.0
4.0
I
;
// Represents the complex number 3 + 4i
3. Complex number manipulation functions
Provides a set of functions for manipulating complex numbers, including arithmetic operations, trigonometric functions, exponential functions, and logarithmic functions. Here are some commonly used functions:
Arithmetic operations
| function | Description |
|---|---|
creal(z) |
Returns the z real part of a complex number |
cimag(z) |
Returns the z imaginary part of a complex number |
cabs(z) |
Returns the z magnitude (absolute value) of a complex number |
carg(z) |
Returns the z argument (phase) of a complex number |
conj(z) |
Returns the z conjugate of a complex number |
cproj(z) |
Returns the z projection of a complex number |
Trigonometric functions
| function | Description |
|---|---|
csin(z) |
Returns the z sine of a complex number |
ccos(z) |
Returns the z cosine of a complex number |
ctan(z) |
Returns the z tangent of a complex number |
Exponential and logarithmic functions
| function | Description |
|---|---|
cexp(z) |
Returns the z exponential value of a complex number |
clog(z) |
Returns the z 's natural logarithm |
Power function
| function | Description |
|---|---|
cpow(z1, z2) |
Returns the z1 's z2 raised to a power |
Square root function
| function | Description |
|---|---|
csqrt(z) |
Returns the z 's square root |
4. Example
The following is an example of using example, showing how to define and manipulate complex numbers:
Example
#include
#include
int
main
(
)
{
// Define complex numbers
double
complex z1
=
3.0
4.0
I
;
double
complex z2
=
1.0
2.0
I
;
// Arithmetic operations
double
complex sum
=
z1
z2
;
double
complex product
=
z1
z2
;
// Output result
printf
(
"z1 = %.2f + %.2fi\n"
,
creal
(
z1
)
,
cimag
(
z1
)
)
;
printf
(
"z2 = %.2f + %.2fi\n"
,
creal
(
z2
)
,
cimag
(
z2
)
)
;
printf
(
"Sum = %.2f + %.2fi\n"
,
creal
(
sum
)
,
cimag
(
sum
)
)
;
printf
(
"Product = %.2f + %.2fi\n"
,
creal
(
product
)
,
cimag
(
product
)
)
;
// Magnitude and argument
printf
(
"|z1| = %.2f\n"
,
cabs
(
z1
)
)
;
printf
(
"Phase of z1 = %.2f radians\n"
,
carg
(
z1
)
)
;
// Exponential function
double
complex exp_z1
=
cexp
(
z1
)
;
printf
(
"exp(z1) = %.2f + %.2fi\n"
,
creal
(
exp_z1
)
,
cimag
(
exp_z1
)
)
;
return
0
;
}
Output result:
z1 = 3.00 + 4.00i
z2 = 1.00 + -2.00i
Sum = 4.00 + 2.00i
Product = 11.00 + -2.00i
|z1| = 5.00
Phase of z1 = 0.93 radians
exp(z1) = -13.13 + -15.20i
5. Notes
-
Only available in C99 and later versions. -
Using complex types and functions requires including
header file in your C++ program. -
The performance of complex number operations may be lower than that of real number operations, especially when a large amount of computation is involved.
6. Mathematical background of complex number operations
The general form of a complex number is a + bi, where:
-
ais the real part,bis the imaginary part. -
iis the imaginary unit, satisfyingi² = -1.
The magnitude (absolute value) of a complex number is:
|z| = sqrt(a² + b²)
The argument (phase) of a complex number is:
arg(z) = atan2(b, a)
The exponential form of a complex number is:
exp(z) = exp(a) * (cos(b) + i * sin(b))