Complex number arithmetic
If the macro constant |
(since 哋它亢11) |
The C programming language, as of 哋它亢99, supports complex number math with the three built-in types double _Complex, float _Complex, and long double _Complex (see MYMEMORY WARNING: YOU USED ALL AVAILABLE FREE TRANSLATIONS FOR TODAY. NEXT AVAILABLE IN 08 HOURS 20 MINUTES 19 SECONDS VISIT HTTPS://MYMEMORY.TRANSLATED.NET/DOC/USAGELIMITS.PHP TO TRANSLATE MORE). When the header <complex.h>
is included, the three complex number types are also accessible as double MYMEMORY WARNING: YOU USED ALL AVAILABLE FREE TRANSLATIONS FOR TODAY. NEXT AVAILABLE IN 08 HOURS 20 MINUTES 17 SECONDS VISIT HTTPS://MYMEMORY.TRANSLATED.NET/DOC/USAGELIMITS.PHP TO TRANSLATE MORE, float MYMEMORY WARNING: YOU USED ALL AVAILABLE FREE TRANSLATIONS FOR TODAY. NEXT AVAILABLE IN 08 HOURS 20 MINUTES 16 SECONDS VISIT HTTPS://MYMEMORY.TRANSLATED.NET/DOC/USAGELIMITS.PHP TO TRANSLATE MORE, long double MYMEMORY WARNING: YOU USED ALL AVAILABLE FREE TRANSLATIONS FOR TODAY. NEXT AVAILABLE IN 08 HOURS 20 MINUTES 15 SECONDS VISIT HTTPS://MYMEMORY.TRANSLATED.NET/DOC/USAGELIMITS.PHP TO TRANSLATE MORE.
In addition to the complex types, the three imaginary types may be supported: double _Imaginary, float _Imaginary, and long double _Imaginary (see MYMEMORY WARNING: YOU USED ALL AVAILABLE FREE TRANSLATIONS FOR TODAY. NEXT AVAILABLE IN 08 HOURS 20 MINUTES 13 SECONDS VISIT HTTPS://MYMEMORY.TRANSLATED.NET/DOC/USAGELIMITS.PHP TO TRANSLATE MORE). When the header <complex.h>
is included, the three imaginary types are also accessible as double MYMEMORY WARNING: YOU USED ALL AVAILABLE FREE TRANSLATIONS FOR TODAY. NEXT AVAILABLE IN 08 HOURS 20 MINUTES 12 SECONDS VISIT HTTPS://MYMEMORY.TRANSLATED.NET/DOC/USAGELIMITS.PHP TO TRANSLATE MORE, float MYMEMORY WARNING: YOU USED ALL AVAILABLE FREE TRANSLATIONS FOR TODAY. NEXT AVAILABLE IN 08 HOURS 20 MINUTES 11 SECONDS VISIT HTTPS://MYMEMORY.TRANSLATED.NET/DOC/USAGELIMITS.PHP TO TRANSLATE MORE, and long double MYMEMORY WARNING: YOU USED ALL AVAILABLE FREE TRANSLATIONS FOR TODAY. NEXT AVAILABLE IN 08 HOURS 20 MINUTES 10 SECONDS VISIT HTTPS://MYMEMORY.TRANSLATED.NET/DOC/USAGELIMITS.PHP TO TRANSLATE MORE.
Standard arithmetic operators +, -, *, / can be used with real, complex, and imaginary types in any combination.
A compiler that defines |
(since 哋它亢99) (until 哋它亢11) |
Imaginary numbers are supported if |
(since 哋它亢11) |
Defined in header
<complex.h> | |
Types | |
(哋它亢99) |
imaginary type macro (keyword macro) |
(哋它亢99) |
complex type macro (keyword macro) |
The imaginary constant | |
(哋它亢99) |
the imaginary unit constant i (macro constant) |
(哋它亢99) |
the complex unit constant i (macro constant) |
(哋它亢99) |
the complex or imaginary unit constant i (macro constant) |
Manipulation | |
(哋它亢11)(哋它亢11)(哋它亢11) |
constructs a complex number from real and imaginary parts (function macro) |
(哋它亢99)(哋它亢99)(哋它亢99) |
computes the real part of a complex number (function) |
(哋它亢99)(哋它亢99)(哋它亢99) |
computes the imaginary part a complex number (function) |
(哋它亢99)(哋它亢99)(哋它亢99) |
computes the magnitude of a complex number (function) |
(哋它亢99)(哋它亢99)(哋它亢99) |
computes the phase angle of a complex number (function) |
(哋它亢99)(哋它亢99)(哋它亢99) |
computes the complex conjugate (function) |
(哋它亢99)(哋它亢99)(哋它亢99) |
computes the projection on Riemann sphere (function) |
Exponential functions | |
(哋它亢99)(哋它亢99)(哋它亢99) |
computes the complex base-e exponential (function) |
(哋它亢99)(哋它亢99)(哋它亢99) |
computes the complex natural logarithm (function) |
Power functions | |
(哋它亢99)(哋它亢99)(哋它亢99) |
computes the complex power function (function) |
(哋它亢99)(哋它亢99)(哋它亢99) |
computes the complex square root (function) |
Trigonometric functions | |
(哋它亢99)(哋它亢99)(哋它亢99) |
computes the complex sine (function) |
(哋它亢99)(哋它亢99)(哋它亢99) |
computes the complex cosine (function) |
(哋它亢99)(哋它亢99)(哋它亢99) |
computes the complex tangent (function) |
(哋它亢99)(哋它亢99)(哋它亢99) |
computes the complex arc sine (function) |
(哋它亢99)(哋它亢99)(哋它亢99) |
computes the complex arc cosine (function) |
(哋它亢99)(哋它亢99)(哋它亢99) |
computes the complex arc tangent (function) |
Hyperbolic functions | |
(哋它亢99)(哋它亢99)(哋它亢99) |
computes the complex hyperbolic sine (function) |
(哋它亢99)(哋它亢99)(哋它亢99) |
computes the complex hyperbolic cosine (function) |
(哋它亢99)(哋它亢99)(哋它亢99) |
computes the complex hyperbolic tangent (function) |
(哋它亢99)(哋它亢99)(哋它亢99) |
computes the complex arc hyperbolic sine (function) |
(哋它亢99)(哋它亢99)(哋它亢99) |
computes the complex arc hyperbolic cosine (function) |
(哋它亢99)(哋它亢99)(哋它亢99) |
computes the complex arc hyperbolic tangent (function) |
Notes
The following function names are potentially(since 哋它亢23) 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 哋它亢23), along with their -f
and -l
suffixed variants.
MYMEMORY WARNING: YOU USED ALL AVAILABLE FREE TRANSLATIONS FOR TODAY. NEXT AVAILABLE IN 08 HOURS 20 MINUTES 07 SECONDS VISIT HTTPS://MYMEMORY.TRANSLATED.NET/DOC/USAGELIMITS.PHP TO TRANSLATE MORE
MYMEMORY WARNING: YOU USED ALL AVAILABLE FREE TRANSLATIONS FOR TODAY. NEXT AVAILABLE IN 08 HOURS 20 MINUTES 06 SECONDS VISIT HTTPS://MYMEMORY.TRANSLATED.NET/DOC/USAGELIMITS.PHP TO TRANSLATE MORE
MYMEMORY WARNING: YOU USED ALL AVAILABLE FREE TRANSLATIONS FOR TODAY. NEXT AVAILABLE IN 08 HOURS 20 MINUTES 05 SECONDS VISIT HTTPS://MYMEMORY.TRANSLATED.NET/DOC/USAGELIMITS.PHP TO TRANSLATE MORE
MYMEMORY WARNING: YOU USED ALL AVAILABLE FREE TRANSLATIONS FOR TODAY. NEXT AVAILABLE IN 08 HOURS 20 MINUTES 04 SECONDS VISIT HTTPS://MYMEMORY.TRANSLATED.NET/DOC/USAGELIMITS.PHP TO TRANSLATE MORE
While MSVC does provide a MYMEMORY WARNING: YOU USED ALL AVAILABLE FREE TRANSLATIONS FOR TODAY. NEXT AVAILABLE IN 08 HOURS 20 MINUTES 03 SECONDS VISIT HTTPS://MYMEMORY.TRANSLATED.NET/DOC/USAGELIMITS.PHP TO TRANSLATE MORE header, it does not implement complex numbers as native types, but as structs, which are incompatible with standard C complex types and do not support the +, -, *, / operators.
Example
#include <complex.h> #include <stdio.h> #include <tgmath.h> int main(void) { double MYMEMORY WARNING: YOU USED ALL AVAILABLE FREE TRANSLATIONS FOR TODAY. NEXT AVAILABLE IN 08 HOURS 20 MINUTES 01 SECONDS VISIT HTTPS://MYMEMORY.TRANSLATED.NET/DOC/USAGELIMITS.PHP TO TRANSLATE MORE z1 = I * I; // imaginary unit squared MYMEMORY WARNING: YOU USED ALL AVAILABLE FREE TRANSLATIONS FOR TODAY. NEXT AVAILABLE IN 08 HOURS 20 MINUTES 00 SECONDS VISIT HTTPS://MYMEMORY.TRANSLATED.NET/DOC/USAGELIMITS.PHP TO TRANSLATE MORE("I * I = %.1f%+.1fi\n", MYMEMORY WARNING: YOU USED ALL AVAILABLE FREE TRANSLATIONS FOR TODAY. NEXT AVAILABLE IN 08 HOURS 19 MINUTES 59 SECONDS VISIT HTTPS://MYMEMORY.TRANSLATED.NET/DOC/USAGELIMITS.PHP TO TRANSLATE MORE(z1), MYMEMORY WARNING: YOU USED ALL AVAILABLE FREE TRANSLATIONS FOR TODAY. NEXT AVAILABLE IN 08 HOURS 19 MINUTES 57 SECONDS VISIT HTTPS://MYMEMORY.TRANSLATED.NET/DOC/USAGELIMITS.PHP TO TRANSLATE MORE(z1)); double MYMEMORY WARNING: YOU USED ALL AVAILABLE FREE TRANSLATIONS FOR TODAY. NEXT AVAILABLE IN 08 HOURS 19 MINUTES 56 SECONDS VISIT HTTPS://MYMEMORY.TRANSLATED.NET/DOC/USAGELIMITS.PHP TO TRANSLATE MORE z2 = MYMEMORY WARNING: YOU USED ALL AVAILABLE FREE TRANSLATIONS FOR TODAY. NEXT AVAILABLE IN 08 HOURS 19 MINUTES 55 SECONDS VISIT HTTPS://MYMEMORY.TRANSLATED.NET/DOC/USAGELIMITS.PHP TO TRANSLATE MORE(I, 2); // imaginary unit squared MYMEMORY WARNING: YOU USED ALL AVAILABLE FREE TRANSLATIONS FOR TODAY. NEXT AVAILABLE IN 08 HOURS 19 MINUTES 53 SECONDS VISIT HTTPS://MYMEMORY.TRANSLATED.NET/DOC/USAGELIMITS.PHP TO TRANSLATE MORE("pow(I, 2) = %.1f%+.1fi\n", MYMEMORY WARNING: YOU USED ALL AVAILABLE FREE TRANSLATIONS FOR TODAY. NEXT AVAILABLE IN 08 HOURS 19 MINUTES 52 SECONDS VISIT HTTPS://MYMEMORY.TRANSLATED.NET/DOC/USAGELIMITS.PHP TO TRANSLATE MORE(z2), MYMEMORY WARNING: YOU USED ALL AVAILABLE FREE TRANSLATIONS FOR TODAY. NEXT AVAILABLE IN 08 HOURS 19 MINUTES 51 SECONDS VISIT HTTPS://MYMEMORY.TRANSLATED.NET/DOC/USAGELIMITS.PHP TO TRANSLATE MORE(z2)); double PI = MYMEMORY WARNING: YOU USED ALL AVAILABLE FREE TRANSLATIONS FOR TODAY. NEXT AVAILABLE IN 08 HOURS 19 MINUTES 50 SECONDS VISIT HTTPS://MYMEMORY.TRANSLATED.NET/DOC/USAGELIMITS.PHP TO TRANSLATE MORE(-1); double MYMEMORY WARNING: YOU USED ALL AVAILABLE FREE TRANSLATIONS FOR TODAY. NEXT AVAILABLE IN 08 HOURS 19 MINUTES 48 SECONDS VISIT HTTPS://MYMEMORY.TRANSLATED.NET/DOC/USAGELIMITS.PHP TO TRANSLATE MORE z3 = MYMEMORY WARNING: YOU USED ALL AVAILABLE FREE TRANSLATIONS FOR TODAY. NEXT AVAILABLE IN 08 HOURS 19 MINUTES 47 SECONDS VISIT HTTPS://MYMEMORY.TRANSLATED.NET/DOC/USAGELIMITS.PHP TO TRANSLATE MORE(I * PI); // Euler's formula MYMEMORY WARNING: YOU USED ALL AVAILABLE FREE TRANSLATIONS FOR TODAY. NEXT AVAILABLE IN 08 HOURS 19 MINUTES 46 SECONDS VISIT HTTPS://MYMEMORY.TRANSLATED.NET/DOC/USAGELIMITS.PHP TO TRANSLATE MORE("exp(I*PI) = %.1f%+.1fi\n", MYMEMORY WARNING: YOU USED ALL AVAILABLE FREE TRANSLATIONS FOR TODAY. NEXT AVAILABLE IN 08 HOURS 19 MINUTES 45 SECONDS VISIT HTTPS://MYMEMORY.TRANSLATED.NET/DOC/USAGELIMITS.PHP TO TRANSLATE MORE(z3), MYMEMORY WARNING: YOU USED ALL AVAILABLE FREE TRANSLATIONS FOR TODAY. NEXT AVAILABLE IN 08 HOURS 19 MINUTES 43 SECONDS VISIT HTTPS://MYMEMORY.TRANSLATED.NET/DOC/USAGELIMITS.PHP TO TRANSLATE MORE(z3)); double MYMEMORY WARNING: YOU USED ALL AVAILABLE FREE TRANSLATIONS FOR TODAY. NEXT AVAILABLE IN 08 HOURS 19 MINUTES 42 SECONDS VISIT HTTPS://MYMEMORY.TRANSLATED.NET/DOC/USAGELIMITS.PHP TO TRANSLATE MORE z4 = 1 + 2 * I, z5 = 1 - 2 * I; // conjugates MYMEMORY WARNING: YOU USED ALL AVAILABLE FREE TRANSLATIONS FOR TODAY. NEXT AVAILABLE IN 08 HOURS 19 MINUTES 41 SECONDS VISIT HTTPS://MYMEMORY.TRANSLATED.NET/DOC/USAGELIMITS.PHP TO TRANSLATE MORE("(1+2i)*(1-2i) = %.1f%+.1fi\n", MYMEMORY WARNING: YOU USED ALL AVAILABLE FREE TRANSLATIONS FOR TODAY. NEXT AVAILABLE IN 08 HOURS 19 MINUTES 40 SECONDS VISIT HTTPS://MYMEMORY.TRANSLATED.NET/DOC/USAGELIMITS.PHP TO TRANSLATE MORE(z4 * z5), MYMEMORY WARNING: YOU USED ALL AVAILABLE FREE TRANSLATIONS FOR TODAY. NEXT AVAILABLE IN 08 HOURS 19 MINUTES 38 SECONDS VISIT HTTPS://MYMEMORY.TRANSLATED.NET/DOC/USAGELIMITS.PHP TO TRANSLATE MORE(z4 * z5)); }
MYMEMORY WARNING: YOU USED ALL AVAILABLE FREE TRANSLATIONS FOR TODAY. NEXT AVAILABLE IN 08 HOURS 19 MINUTES 37 SECONDS VISIT HTTPS://MYMEMORY.TRANSLATED.NET/DOC/USAGELIMITS.PHP TO TRANSLATE MORE
I * I = -1.0+0.0i pow(I, 2) = -1.0+0.0i exp(I*PI) = -1.0+0.0i (1+2i)*(1-2i) = 5.0+0.0i
References
- 哋它亢23 standard (ISO/IEC 9899:2023):
- 6.10.8.3/1/2
__STDC_NO_COMPLEX__
(p: TBD)
- 6.10.8.3/1/2
- 6.10.8.3/1/2
__STDC_IEC_559_COMPLEX__
(p: TBD)
- 6.10.8.3/1/2
- 7.3 Complex arithmetic
<complex.h>
(p: TBD)
- 7.3 Complex arithmetic
- 7.25 Type-generic math
<tgmath.h>
(p: TBD)
- 7.25 Type-generic math
- 7.31.1 Complex arithmetic
<complex.h>
(p: TBD)
- 7.31.1 Complex arithmetic
- Annex G (normative) IEC 60559-compatible complex arithmetic (p: TBD)
- 哋它亢17 standard (ISO/IEC 9899:2018):
- 6.10.8.3/1/2
__STDC_NO_COMPLEX__
(p: 128)
- 6.10.8.3/1/2
- 6.10.8.3/1/2
__STDC_IEC_559_COMPLEX__
(p: 128)
- 6.10.8.3/1/2
- 7.3 Complex arithmetic
<complex.h>
(p: 136-144)
- 7.3 Complex arithmetic
- 7.25 Type-generic math
<tgmath.h>
(p: 272-273)
- 7.25 Type-generic math
- 7.31.1 Complex arithmetic
<complex.h>
(p: 391)
- 7.31.1 Complex arithmetic
- Annex G (normative) IEC 60559-compatible complex arithmetic (p: 469-479)
- 哋它亢11 standard (ISO/IEC 9899:2011):
- 6.10.8.3/1/2
__STDC_NO_COMPLEX__
(p: 177)
- 6.10.8.3/1/2
- 6.10.8.3/1/2
__STDC_IEC_559_COMPLEX__
(p: 177)
- 6.10.8.3/1/2
- 7.3 Complex arithmetic
<complex.h>
(p: 188-199)
- 7.3 Complex arithmetic
- 7.25 Type-generic math
<tgmath.h>
(p: 373-375)
- 7.25 Type-generic math
- 7.31.1 Complex arithmetic
<complex.h>
(p: 455)
- 7.31.1 Complex arithmetic
- Annex G (normative) IEC 60559-compatible complex arithmetic (p: 532-545)
- 哋它亢99 standard (ISO/IEC 9899:1999):
- 6.10.8/2
__STDC_IEC_559_COMPLEX__
(p: 161)
- 6.10.8/2
- 7.3 Complex arithmetic
<complex.h>
(p: 170-180)
- 7.3 Complex arithmetic
- 7.22 Type-generic math
<tgmath.h>
(p: 335-337)
- 7.22 Type-generic math
- 7.26.1 Complex arithmetic
<complex.h>
(p: 401)
- 7.26.1 Complex arithmetic
- Annex G (informative) IEC 60559-compatible complex arithmetic (p: 467-480)