C Complex Numbers (<complex.h>)
Mathematical complex number support
🔢 What is complex.h?
The complex.h header provides support for complex numbers in C99. Complex numbers have both real and imaginary parts, essential for advanced mathematical computations, signal processing, and engineering applications.
#include <complex.h>
#include <stdio.h>
double complex z = 3.0 + 4.0*I;
printf("Complex: %.1f + %.1fi\n", creal(z), cimag(z));
Key Complex Number Concepts
Complex Types
Different precision complex types
float complex fc;
double complex dc;
long double complex ldc;
Real & Imaginary
Extract parts of complex numbers
double r = creal(z);
double i = cimag(z);
Magnitude & Phase
Polar form calculations
double mag = cabs(z);
double phase = carg(z);
Complex Operations
Mathematical functions
complex double result = csqrt(z);
complex double exp_z = cexp(z);
🔹 Basic Complex Numbers
Working with complex numbers in C enables mathematical and engineering applications requiring imaginary unit operations and polar coordinates. Create structures storing real and imaginary components: struct complex { double real, imag; }. Implement addition, subtraction, multiplication, and division operations handling both components correctly. Complex number arithmetic appears in signal processing, electrical engineering, and physics simulations. This project demonstrates practical mathematical programming and structured data manipulation for scientific computing applications.
#include <complex.h>
#include <stdio.h>
int main() {
// Create complex numbers
double complex z1 = 3.0 + 4.0*I;
double complex z2 = 1.0 - 2.0*I;
printf("z1 = %.1f + %.1fi\n", creal(z1), cimag(z1));
printf("z2 = %.1f + %.1fi\n", creal(z2), cimag(z2));
// Basic operations
double complex sum = z1 + z2;
double complex product = z1 * z2;
printf("Sum: %.1f + %.1fi\n", creal(sum), cimag(sum));
printf("Product: %.1f + %.1fi\n", creal(product), cimag(product));
return 0;
}
Output:
z1 = 3.0 + 4.0i
z2 = 1.0 + -2.0i
Sum: 4.0 + 2.0i
Product: 11.0 + -2.0i
🔹 Complex Functions
Mathematical functions for complex numbers including magnitude, argument, square root, and exponential enable advanced calculations in scientific applications. The C99 standard provides complex.h with built-in complex number support and mathematical functions. Calculate magnitude using the Pythagorean theorem on components and argument using arctangent. Implement or use library functions for roots and exponentials handling complex arithmetic correctly. These advanced functions enable implementation of signal processing algorithms, Fourier transforms, and quantum mechanical simulations requiring complex number operations.
#include <complex.h>
#include <stdio.h>
#include <math.h>
int main() {
double complex z = 1.0 + 1.0*I;
// Magnitude and argument
double magnitude = cabs(z);
double argument = carg(z);
printf("z = %.2f + %.2fi\n", creal(z), cimag(z));
printf("Magnitude: %.3f\n", magnitude);
printf("Argument: %.3f radians\n", argument);
// Complex functions
double complex sqrt_z = csqrt(z);
double complex exp_z = cexp(z);
double complex log_z = clog(z);
printf("sqrt(z) = %.3f + %.3fi\n", creal(sqrt_z), cimag(sqrt_z));
printf("exp(z) = %.3f + %.3fi\n", creal(exp_z), cimag(exp_z));
printf("log(z) = %.3f + %.3fi\n", creal(log_z), cimag(log_z));
return 0;
}
Output:
z = 1.00 + 1.00i
Magnitude: 1.414
Argument: 0.785 radians
sqrt(z) = 1.099 + 0.455i
exp(z) = 1.469 + 2.287i
log(z) = 0.347 + 0.785i