Common mathematical functions | ||||
Floating-point environment(C99) | ||||
Pseudo-random number generation | ||||
Complex number arithmetic(C99) | ||||
Type-generic math(C99) | ||||
Bit manipulation(C23) | ||||
Checked integer arithmetic(C23) |
Types and the imaginary constant | |||||||||||||||||||||||||||||||
|
| ||||||||||||||||||||||||||||||
Manipulation | |||||||||||||||||||||||||||||||
Power and exponential functions | |||||||||||||||||||||||||||||||
Trigonometric functions | |||||||||||||||||||||||||||||||
Hyperbolic functions | |||||||||||||||||||||||||||||||
If the macro constant | (since C11) |
The C programming language, as of C99, supports complex number math with the three built-in typesdouble _Complex,float _Complex, andlongdouble _Complex (see_Complex). When the header<complex.h> is included, the three complex number types are also accessible asdoublecomplex,floatcomplex,longdoublecomplex.
In addition to the complex types, the three imaginary types may be supported:double _Imaginary,float _Imaginary, andlongdouble _Imaginary (see_Imaginary). When the header<complex.h> is included, the three imaginary types are also accessible asdoubleimaginary,floatimaginary, andlongdoubleimaginary.
Standard arithmetic operators+,-,*,/ can be used with real, complex, and imaginary types in any combination.
A compiler that defines | (since C99) (until C11) |
Imaginary numbers are supported if | (since C11) |
Defined in header <complex.h> | ||
Types | ||
(C99) | imaginary type macro (keyword macro)[edit] | |
(C99) | complex type macro (keyword macro)[edit] | |
The imaginary constant | ||
(C99) | the imaginary unit constant i (macro constant)[edit] | |
(C99) | the complex unit constant i (macro constant)[edit] | |
(C99) | the complex or imaginary unit constant i (macro constant)[edit] | |
Manipulation | ||
(C11)(C11)(C11) | constructs a complex number from real and imaginary parts (function macro)[edit] | |
(C99)(C99)(C99) | computes the real part of a complex number (function)[edit] | |
(C99)(C99)(C99) | computes the imaginary part a complex number (function)[edit] | |
(C99)(C99)(C99) | computes the magnitude of a complex number (function)[edit] | |
(C99)(C99)(C99) | computes the phase angle of a complex number (function)[edit] | |
(C99)(C99)(C99) | computes the complex conjugate (function)[edit] | |
(C99)(C99)(C99) | computes the projection on Riemann sphere (function)[edit] | |
Exponential functions | ||
(C99)(C99)(C99) | computes the complex base-e exponential (function)[edit] | |
(C99)(C99)(C99) | computes the complex natural logarithm (function)[edit] | |
Power functions | ||
(C99)(C99)(C99) | computes the complex power function (function)[edit] | |
(C99)(C99)(C99) | computes the complex square root (function)[edit] | |
Trigonometric functions | ||
(C99)(C99)(C99) | computes the complex sine (function)[edit] | |
(C99)(C99)(C99) | computes the complex cosine (function)[edit] | |
(C99)(C99)(C99) | computes the complex tangent (function)[edit] | |
(C99)(C99)(C99) | computes the complex arc sine (function)[edit] | |
(C99)(C99)(C99) | computes the complex arc cosine (function)[edit] | |
(C99)(C99)(C99) | computes the complex arc tangent (function)[edit] | |
Hyperbolic functions | ||
(C99)(C99)(C99) | computes the complex hyperbolic sine (function)[edit] | |
(C99)(C99)(C99) | computes the complex hyperbolic cosine (function)[edit] | |
(C99)(C99)(C99) | computes the complex hyperbolic tangent (function)[edit] | |
(C99)(C99)(C99) | computes the complex arc hyperbolic sine (function)[edit] | |
(C99)(C99)(C99) | computes the complex arc hyperbolic cosine (function)[edit] | |
(C99)(C99)(C99) | computes the complex arc hyperbolic tangent (function)[edit] |
The following function names arepotentially(since C23) reserved for future addition to<complex.h> and are not available for use in the programs that include that header:cerf,cerfc,cexp2,cexpm1,clog10,clog1p,clog2,clgamma,ctgamma,csinpi,ccospi,ctanpi,casinpi,cacospi,catanpi,ccompoundn,cpown,cpowr,crootn,crsqrt,cexp10m1,cexp10,cexp2m1,clog10p1,clog2p1,clogp1(since C23), along with their -f
and -l
suffixed variants.
Although the C standard names the inverse hyperbolic 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.
A complex or imaginary number is infinite if one of its parts is infinite, even if the other part is NaN.
A complex or imaginary number is finite if both parts are neither infinities nor NaNs.
A complex or imaginary number is a zero if both parts are positive or negative zeroes.
While MSVC does provide a<complex.h>
header, it does not implement complex numbers as native types, but asstructs, which are incompatible with standard C complex types and do not support the+,-,*,/ operators.
#include <complex.h>#include <stdio.h>#include <tgmath.h> int main(void){doublecomplex z1= I* I;// imaginary unit squaredprintf("I * I = %.1f%+.1fi\n",creal(z1),cimag(z1)); doublecomplex z2=pow(I,2);// imaginary unit squaredprintf("pow(I, 2) = %.1f%+.1fi\n",creal(z2),cimag(z2)); double PI=acos(-1);doublecomplex z3=exp(I* PI);// Euler's formulaprintf("exp(I*PI) = %.1f%+.1fi\n",creal(z3),cimag(z3)); doublecomplex z4=1+2* I, z5=1-2* I;// conjugatesprintf("(1+2i)*(1-2i) = %.1f%+.1fi\n",creal(z4* z5),cimag(z4* z5));}
Output:
I * I = -1.0+0.0ipow(I, 2) = -1.0+0.0iexp(I*PI) = -1.0+0.0i(1+2i)*(1-2i) = 5.0+0.0i
Extended content |
---|
|
C++ documentation forComplex number arithmetic |