liveBook · Manning

Complex floating-point type

As explained in Modern C

A complex number is a number expressible as the sum of a real component and an imaginary component.

A complex number combines a real component with an imaginary component. C places complex floating-point numbers alongside unsigned integers, signed integers, and real floating-point numbers; the complex family contains float complex, double complex, and long double complex, distinguished by precision. section 5.2

Writing complex constants

Include <complex.h> before using the complex macros: #include <complex.h> double complex z = 0.5 + 0.5 * I; float complex w = 0.5F + 0.5F * I; double complex pair = CMPLX(0.5, 0.5); I denotes the imaginary unit, with I*I equal to −1. CMPLX combines real and imaginary floating-point values into a double complex value; CMPLXF and CMPLXL select float complex and long double complex. C provides no literal syntax dedicated to complex constants, so these macros supply the practical notation. When real and imaginary operands use different floating-point types, the result is implicitly converted to the wider type. section 5.3.1

Table 5.1. Base types according to the four main type classes. Types with a gray background don’t allow for arithmetic; they are promoted before doing arithmetic. Type char is special since it can be unsigned or signed, depending on the platform. All types in this table are considered to be distinct types, even if they have the same class and precision. (view table figure)

Class

Systematic name

Other name

Rank

Integers Unsigned _Bool bool 0
unsigned char 1
unsigned short 2
unsigned int unsigned 3
unsigned long 4
unsigned long long   5
[Un]signed char 1
  signed char 1
  signed short short 2
Signed signed int signed or int 3
  signed long long 4
  signed long long long long 5
Floating point Real float    
double    
long double    
Complex float _Complex float complex
double _Complex double complex
long double _Complex long double complex

Compare complex types with the other base-type classes, their systematic names, and their precision-related distinctions. section 5.2

How Modern C covers it

How deep it goes
Defines the three complex precisions and demonstrates constant construction, but does not develop complex arithmetic functions.
Examples
Uses CMPLX, CMPLXF, CMPLXL, I, and expressions such as 0.5 + 0.5*I.
What it assumes
Assumes basic C types and floating-point notation while defining real and imaginary components from the beginning.
What you can do after
Apply it via C's complex.h macros to declare complex values and construct constants with selected precision.
How it connects
Connects complex floating-point types with real floating-point types, precision, header files, macros, and implicit conversion.

Also explained in

Compare how 3 books explain complex floating-point type →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Modern C. The section links point to the parts of the book it draws on.