Some programming languages provide a complex data type for complex number storage and arithmetic as a built-in (primitive) data type.
A complex variable or value is usually represented as a pair of floating-point numbers. Languages that support a complex data type usually provide special syntax for building such values, and extend the basic arithmetic operations ('+', '−', '×', '÷') to act on them. These operations are usually translated by the compiler into a sequence of floating-point machine instructions or into library calls. Those languages may also provide support for other operations, such as formatting, equality testing, etc. As in mathematics, those languages often interpret a floating-point value as equivalent to a complex value with a zero imaginary part.
![]() | This section needs expansion. You can help by adding to it. (November 2015) |
<complex.h>
.complex
template class as well as complex-math functions in the <complex>
header.complex64
(each component is 32-bit float) and complex128
(each component is 64-bit float). Imaginary number literals can be specified by appending an "i".complex
type. Imaginary number literals can be specified by appending a "j". Complex-math functions are provided in the standard library module cmath
.[2] ? (sqrt -1)
# C(0 1) ; the result of (sqrt -1)
sin
) are included in the language specification. Their implementation is however optional in the R5RS standard, while in R6RS is mandatory.a + bi
. Any variable, math operation or function can accept both real and complex numbers as arguments and return real or complex numbers depending on result. For example the square root of −4 is a complex number:PRINT SQRT(-4)
2i
complex
basic data type.[4]Many programming languages provide built-in support or standard libraries for complex data types, enabling direct manipulation of complex numbers in code. These integrations typically define arithmetic operations, comparison rules, and input/output formatting specific to complex numbers. For example, in Python, the complex
type allows arithmetic with complex literals and supports functions from the cmath
module.[5] Similarly, Haskell includes the Data.Complex
module, offering complex arithmetic with real and imaginary parts represented as floating-point numbers.[6] Such integration simplifies scientific and engineering computations that require complex number calculations.