Some
programming languages provide a complex data type for
complex number
In mathematics, a complex number is an element of a number system that extends the real numbers with a specific element denoted , called the imaginary unit and satisfying the equation i^= -1; every complex number can be expressed in the for ...
storage and arithmetic as a built-in (
primitive)
data type
In computer science and computer programming, a data type (or simply type) is a collection or grouping of data values, usually specified by a set of possible values, a set of allowed operations on these values, and/or a representation of these ...
.
Complex-number arithmetic
A complex variable or value is usually represented as a pair of
floating-point
In computing, floating-point arithmetic (FP) is arithmetic on subsets of real numbers formed by a ''significand'' (a Sign (mathematics), signed sequence of a fixed number of digits in some Radix, base) multiplied by an integer power of that ba ...
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
In computing, a compiler is a computer program that Translator (computing), translates computer code written in one programming language (the ''source'' language) into another language (the ''target'' language). The name "compiler" is primaril ...
into a sequence of floating-point
machine instructions or into
library
A library is a collection of Book, books, and possibly other Document, materials and Media (communication), media, that is accessible for use by its members and members of allied institutions. Libraries provide physical (hard copies) or electron ...
calls. Those languages may also provide support for other operations, such as formatting, equality testing, etc. As in
mathematics
Mathematics is a field of study that discovers and organizes methods, Mathematical theory, theories and theorems that are developed and Mathematical proof, proved for the needs of empirical sciences and mathematics itself. There are many ar ...
, those languages often interpret a floating-point value as equivalent to a complex value with a zero imaginary part.
Language support
* The
FORTRAN COMPLEX type.
* The
C99 standard of the C programming language includes complex data types and complex-math functions in the standard library header
< complex.h>
.
* The
C++ standard library provides a
complex
template class as well as complex-math functions in the
header.
* The
Go programming language has built-in types
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".
* The
Perl
Perl is a high-level, general-purpose, interpreted, dynamic programming language. Though Perl is not officially an acronym, there are various backronyms in use, including "Practical Extraction and Reporting Language".
Perl was developed ...
core module provides support for complex numbers.
*
Python provides the built-in
complex
type. Imaginary number literals can be specified by appending a "j". Complex-math functions are provided in the standard library module
cmath
.
*
Ruby
Ruby is a pinkish-red-to-blood-red-colored gemstone, a variety of the mineral corundum ( aluminium oxide). Ruby is one of the most popular traditional jewelry gems and is very durable. Other varieties of gem-quality corundum are called sapph ...
provides a class in the standard library module .
*
OCaml
OCaml ( , formerly Objective Caml) is a General-purpose programming language, general-purpose, High-level programming language, high-level, Comparison of multi-paradigm programming languages, multi-paradigm programming language which extends the ...
supports complex numbers with the standard library module .
*
Haskell
Haskell () is a general-purpose, statically typed, purely functional programming language with type inference and lazy evaluation. Designed for teaching, research, and industrial applications, Haskell pioneered several programming language ...
supports complex numbers with the standard library module (previously called ).
*
Mercury provides complex numbers with full operator overloading support in the extras distribution, using .
*
Java
Java is one of the Greater Sunda Islands in Indonesia. It is bordered by the Indian Ocean to the south and the Java Sea (a part of Pacific Ocean) to the north. With a population of 156.9 million people (including Madura) in mid 2024, proje ...
does not have a standard complex number class, but there exist a number of incompatible free implementations of a complex number class:
** The
Apache Commons Math library provides complex numbers for Java with its class.
** Th
JSciencelibrary has a number class.
** Th
JASlibrary allows the use of complex numbers.
*
Netlibhas a complex number class for Java.
*
javafastcomplexalso adds complex number support for Java.
*
jcomplexnumberis a project on implementation of complex number in Java.
*
JLinAlgincludes complex numbers with arbitrary precision.
*
Common Lisp
Common Lisp (CL) is a dialect of the Lisp programming language, published in American National Standards Institute (ANSI) standard document ''ANSI INCITS 226-1994 (S2018)'' (formerly ''X3.226-1994 (R1999)''). The Common Lisp HyperSpec, a hyperli ...
: The ANSI Common Lisp standard supports complex numbers of floats, rationals and arbitrary precision integers. Its basic mathematical functions are defined for complex numbers, where applicable. For example the square root of −1 is a complex number:
? (sqrt -1)
# C(0 1) ; the result of (sqrt -1)
*
Scheme: Complex numbers and functions (e.g.
sin
) are included in the language specification. Their implementation is however optional in the R5RS standard, while in R6RS is mandatory.
* The
.NET Framework provide
System.Numerics.Complexsince version 4.0.
* The
smart BASIC for
iOS naturally supports complex numbers in notation . 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
*
Julia includes predefined types for both complex and rational numbers since at least version 0.3.
*
R provides a
complex
basic data type.
*
Rust does not provide a built-in complex data type, but multiple crates, that add support for such types, exist.
*
Swift
Swift or SWIFT most commonly refers to:
* SWIFT, an international organization facilitating transactions between banks
** SWIFT code
* Swift (programming language)
* Swift (bird), a family of birds
It may also refer to:
Organizations
* SWIF ...
does not have a standard complex number data type, bu
Swift Numericswhich is maintained by the language maintainers offer
ComplexModule
Integration in programming
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.
Similarly, Haskell includes the
Data.Complex
module, offering complex arithmetic with real and imaginary parts represented as floating-point numbers.
Such integration simplifies scientific and engineering computations that require complex number calculations.
References
{{DEFAULTSORT:Complex Data Type
Data types