Complex number arithmetic - cppreference.com

##### cppreference.com

##### Search

Create account

##### Namespaces

- Page
- Discussion

##### Variants

##### Views

- View
- Edit
- History

##### Actions

# Complex number arithmetic

From cppreference.com
< c‎ | numeric

The header `<complex.h>` defines macros and declares functions that support complex number arithmetic. Complex values are values of type `double complex`, `float complex`, `long double complex`,

If the macro constant `__STDC_IEC_559_COMPLEX__`(C99) is defined by the compiler, in addition to the complex types, the imaginary types are also supported: `double imaginary`, `float imaginary`, and `long double imaginary`. When a value of imaginary type is converted to a value of complex type, the resulting complex type has its real component set to zero. When a value of complex type is converted to a value of imaginary type, the real component is discarded.

Standard arithmetic operators +, -, *, / can be used with real, complex, and imaginary types in any combination.

If the macro constant `__STDC_NO_COMPLEX__`(C11) is defined by the compiler, the header `<complex.h>` and all of the names listed here are not provided.

If #pragma STDC CX_LIMITED_RANGE on is used, complex multiplication, division, and absolute value may use simplified mathematical formulas, despite the possibility of intermediate overflow.

Defined in header `<complex.h>`
##### Types
imaginary (C99) | imaginary type macro (macro constant) [edit]
complex (C99) | complex type macro (macro constant) [edit]
##### The imaginary constant
Imaginary_I (C99) | the imaginary unit constant i (macro constant) [edit]
Complex_I (C99) | the complex unit constant i (macro constant) [edit]
I (C99) | the complex or imaginary unit constant i (macro constant) [edit]
##### Manipulation
CMPLXCMPLXFCMPLXL (C11)(C11)(C11) | constructs a complex number from real and imaginary parts (function macro) [edit]
crealcrealfcreall (C99)(C99)(C99) | computes the real part of a complex number (function) [edit]
cimagcimagfcimagl (C99)(C99)(C99) | computes the imaginary part a complex number (function) [edit]
cabscabsfcabsl (C99)(C99)(C99) | computes the magnitude of a complex number (function) [edit]
cargcargfcargl (C99)(C99)(C99) | computes the phase angle of a complex number (function) [edit]
conjconjfconjl (C99)(C99)(C99) | computes the complex conjugate (function) [edit]
cprojcprojfcprojl (C99)(C99)(C99) | computes the projection on Riemann sphere (function) [edit]
##### Exponential functions
cexpcexpfcexpl (C99)(C99)(C99) | computes the complex base-e exponential (function) [edit]
clogclogfclogl (C99)(C99)(C99) | computes the complex natural logarithm (function) [edit]
##### Power functions
cpowcpowfcpowl (C99)(C99)(C99) | computes the complex power function (function) [edit]
csqrtcsqrtfcsqrtl (C99)(C99)(C99) | computes the complex square root (function) [edit]
##### Trigonometric functions
csincsinfcsinl (C99)(C99)(C99) | computes the complex sine (function) [edit]
ccosccosfccosl (C99)(C99)(C99) | computes the complex cosine (function) [edit]
ctanctanfctanl (C99)(C99)(C99) | computes the complex tangent (function) [edit]
casincasinfcasinl (C99)(C99)(C99) | computes the complex arc sine (function) [edit]
cacoscacosfcacosl (C99)(C99)(C99) | computes the complex arc cosine (function) [edit]
catancatanfcatanl (C99)(C99)(C99) | computes the complex arc tangent (function) [edit]
##### Hyperbolic functions
csinhcsinhfcsinhl (C99)(C99)(C99) | computes the complex hyperbolic sine (function) [edit]
ccoshccoshfccoshl (C99)(C99)(C99) | computes the complex hyperbolic cosine (function) [edit]
ctanhctanhfctanhl (C99)(C99)(C99) | computes the complex hyperbolic tangent (function) [edit]
casinhcasinhfcasinhl (C99)(C99)(C99) | computes the complex arc hyperbolic sine (function) [edit]
cacoshcacoshfcacoshl (C99)(C99)(C99) | computes the complex arc hyperbolic cosine (function) [edit]
catanhcatanhfcatanhl (C99)(C99)(C99) | computes the complex arc hyperbolic tangent (function) [edit]

### [edit] Notes

Although the C standard names the inverse hyperbolics with "complex arc hyperbolic sine" etc., the inverse functions of the hyperbolic functions are the area functions. Their argument is the area of a hyperbolic sector, not an arc. The correct names are "complex inverse hyperbolic sine" etc. Some authors use "complex area hyperbolic sine" etc.

### [edit] Example

Run this code

    #include <stdio.h>
    #include <complex.h>

    int main(void)
    {
        #ifdef __STDC_IEC_559_COMPLEX__
               printf("_STDC_IEC_559_COMPLEX__ defined\n");
        #else
               printf("_STDC_IEC_559_COMPLEX__ not defined\n");
        #endif
        #ifdef __STDC_NO_COMPLEX__
               printf("__STDC_NO_COMPLEX__ defined\n");
        #else
               printf("__STDC_NO_COMPLEX__ not defined\n");
        #endif
        printf("\n");

        printf("%2zu\n", sizeof(float complex));           /*  8 */
        printf("%2zu\n", sizeof(double complex));          /* 16 */
        printf("%2zu\n", sizeof(long double complex));     /* implementation-defined */

        /* Two macros represent the imaginary unit “i”: */
        /*      _Complex_I                              */
        /*      I                                       */
        printf("%.1f%+.1fi\n", creal(_Complex_I),cimag(_Complex_I));
        printf("%.1f%+.1fi\n", creal(I),cimag(I));

        /* The central property of the imaginary unit:  i*i is -1. */
        printf("%.1f%+.1fi\n", creal(1.0*_Complex_I*1.0*_Complex_I),
                               cimag(1.0*_Complex_I*1.0*_Complex_I));
        printf("%.1f%+.1fi\n", creal(1.0*I*1.0*I),cimag(1.0*I*1.0*I));

        /* Use the macros to define complex constants. */
        double complex z1 = 1.0 + 2.0*I;
        double complex z2 = 2.0 + 4.0*_Complex_I;

        /* Sum and print. */
        double complex z3 = z1+z2;
        printf("%.1f%+.1fi\n", creal(z3),cimag(z3));

        /* When the macro named "I" conflicts in an application, */
        /* define a new macro named "J" or "j".                  */
        #undef I
        #define J _Complex_I
        z1 = 1.5 + 2.5*J;

        return 0;
    }

Possible output:

    _STDC_IEC_559_COMPLEX__ defined
    __STDC_NO_COMPLEX__ not defined

     8
    16
    32
    0.0+1.0i
    0.0+1.0i
    -1.0+0.0i
    -1.0+0.0i
    3.0+6.0i

### [edit] See also

C++ documentation for Complex number arithmetic

Retrieved from "http://en.cppreference.com/mwiki/index.php?title=c/numeric/complex&oldid=71058"