liveBook · Manning

Complex floating-point type

As explained in Modern C, Third Edition

A complex type is a C arithmetic type representing a complex number with real and imaginary components based on a corresponding real floating-point type.

A complex floating-point type represents a complex number with real and imaginary components based on a corresponding real floating-point type. C groups complex floating-point numbers with unsigned integers, signed integers, and real floating-point numbers as its four principal numeric classes; precision distinguishes the available types. section 5.2 section 5.7.8

Use the standard complex facilities with these constraints:

  • Include <complex.h> for full complex-type support, and inspect __STDC_NO_COMPLEX__ because some C platforms do not support complex types. section 5.3.1
  • Construct values with CMPLX, CMPLXF, or CMPLXL, or combine floating-point values with the reserved imaginary-unit macro I. section 5.3.1
  • Prefer standardized forms such as CMPLX(0.5, 0.5) or 0.5 + 0.5*I; extra-i complex literals such as 0.5i are not standardized. section 5.3.1
  • Avoid exact equality comparisons because floating-point operations can lose contributions across very different magnitudes. section 5.7.8

Constructing and inspecting a value

With <complex.h>, CMPLX(0.5, 0.5) creates a double complex value. For a complex value z, creal(z) and cimag(z), supplied through <tgmath.h>, retrieve its real and imaginary parts; mathematically, z == creal(z) + cimag(z)*I. section 5.3.1 section 5.7.8

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 behave like 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.
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                       
ClassSystematic nameOther nameRank
IntegersUnsignedbool_Bool0
unsigned char1
unsigned short2
unsigned intunsigned3
unsigned long4
unsigned long long5
[Un]signedchar1
Signedsigned char1
signed shortshort2
signed intsigned or int3
signed longlong4
signed long longlong long5
Floating pointRealfloat
double
long double
Complexfloat _Complexfloat complex
double _Complexdouble complex
long double _Complexlong double complex

Browse the base-type overview to compare complex floating-point types with C’s other numeric classes and their precision distinctions. section 5.2

How Modern C, Third Edition covers it

How deep it goes
Defines representation, construction, portability, and component access, but stops short of detailed complex arithmetic.
Examples
Uses CMPLX(0.5, 0.5), 0.5 + 0.5*I, creal, cimag, and the nonstandard 0.5i literal as concrete examples.
What it assumes
Assumes familiarity with C numeric types and floating-point values, while defining complex representation and constant syntax explicitly.
What you can do after
Constructs portable complex constants and extracts their real and imaginary components while recognizing platform and comparison constraints.
How it connects
Connects complex floating-point types to real floating-point types, precision, floating-point equality, headers, and generic component-access macros.

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, Third Edition. The section links point to the parts of the book it draws on.