C Programming/complex.h

From Wikibooks, open books for an open world
Jump to: navigation, search

complex.h is a header file that defines functionality for complex arithmetic.[1] These functions use the built-in type complex which was introduced with the C99 revision of C.

This header should not be confused with the C++ standard library header <complex>, which implements complex numbers in a completely different way (as a template class, complex<T>).


Universal macro:

  • complex - alias of _Complex
  • _Complex_I - constant, which has type "const float _Complex" and value of imaginary unit I, I*I=-1

Defined only for `C99 Annex G`-compatible compiler modes - native support of imaginary types (it is very rare because Annex G is mathematically incorrect[2]):

  • imaginary - alias of _Imaginary
  • _Imaginary_I Expands to a constant expression of type const float _Imaginary with the value of the imaginary unit.

Universal alias of I:

  • I - alias of _Imaginary_I if it is defined else - alias of _Complex_I

Application may undefine complex, imaginary, and I macros if they interfere with internal values.


Each function declared in complex.h has three versions, each of which works with a different floating-point type (double, float and long double). Only the double version of each function is listed here; to use the float (or long double) version, append an f (or an l, respectively) to the function's name (e.g., cabsf and cabsl).

Note that all angles are in radians.

Basic operations
  • cabs - computes absolute value
  • carg - computes argument of a complex number
  • cimag - computes imaginary part of a complex number
  • creal - computes real part of a complex number
  • conj - computes complex conjugate
  • cproj - computes complex projection into the Riemann sphere
Exponentiation operations
  • cexp - computes complex exponential
  • clog - computes complex logarithm
  • csqrt - computes complex square root
  • cpow - computes complex power
Trigonometric operations
  • csin - computes complex sine
  • ccos - computes complex cosine
  • ctan - computes complex tangent
  • casin - computes complex arc sine
  • cacos - computes complex arc cosine
  • catan - computes complex arc tangent
Hyperbolic operations
  • csinh - computes complex hyperbolic sine
  • ccosh - computes complex hyperbolic cosine
  • ctanh - computes complex hyperbolic tangent
  • casinh - computes complex hyperbolic arc sine
  • cacosh - computes complex hyperbolic arc cosine
  • catanh - computes complex hyperbolic arc tangent


  1. complex.h: complex arithmetic – Base Definitions Reference, The Single UNIX® Specification, Issue 7 from The Open Group
  2. Dr. Dobb's journal: software tools for the professional programmer, Volume 27 "many results in Annex G are pure invention and mathematically incorrect"