Complex data type

For the concept sometimes also called "complex data type", see composite data type.

Some programming languages provide a complex data type for complex number storage and arithmetic as a built-in (primitive) data type.

## Complex-number arithmetic[edit]

This section does not cite any sources. Please help improve this section by adding citations to reliable sources. Unsourced material may be challenged and removed. (June 2023) (Learn how and when to remove this message)

|

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.

## Language support[edit]

This section needs expansion. You can help by adding to it. (November 2015)

|

* The FORTRAN COMPLEX type.[1]
* 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 `<complex>` header.
* The Go programming language has built-in types `complex64` (each component is 32-bit float) and `complex128` (each component is 64-bit float).
* The Perl core module Math::Complex 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`.[2]
* Ruby provides a Complex class in the standard library module complex.
* OCaml supports complex numbers with the standard library module Complex.
* Haskell supports complex numbers with the standard library module Data.Complex (previously called Complex).
* Mercury provides complex numbers with full operator overloading support in the extras distribution, using libcomplex_numbers.
* Java 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 Complex class.
* The JScience library has a Complex number class.
* The JAS library allows the use of complex numbers.
* Netlib has a complex number class for Java.
* javafastcomplex also adds complex number support for Java
* jcomplexnumber is a project on implementation of complex number in Java.
* JLinAlg includes complex numbers with arbitrary precision.
* Common Lisp: 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 provides System.Numerics.Complex since version 4.0.
* The smart BASIC for iOS naturally supports complex numbers in notation 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)

* Julia includes predefined types for both complex and rational numbers since at least version 0.3[3]
* R provides a `complex` basic data type[4]
* Swift does not have a standard complex number data type, but Swift Numerics which is maintained by the language maintaners offers ComplexModule.

## References[edit]

1. ^ A guide to Fortran IV programming Daniel D. McCracken - 1972 - 288 pages. "The capability provided by Fortran complex operations is a great savings in programming effort in certain problems. "
2. ^ Python v2.6.5 documentation
3. ^ "Complex and Rational Numbers — Julia Language 0.3.13-pre documentation".
4. ^ "R Data Types". www.w3schools.com. Retrieved 2022-04-26.

* v
* t
* e

Data types

Uninterpreted|

* Bit
* Byte
* Trit
* Tryte
* Word
* Bit array

Numeric|

* Arbitrary-precision or bignum
* Complex
* Decimal
* Fixed point
* Floating point
* Reduced precision
* Minifloat
* Half precision
* bfloat16
* Single precision
* Double precision
* Quadruple precision
* Octuple precision
* Extended precision
* Long double
* Integer
* signedness
* Interval
* Rational

Pointer|

* Address
* physical
* virtual
* Reference

Text|

* Character
* String
* null-terminated

Composite|

* Algebraic data type
* generalized
* Array
* Associative array
* Class
* Dependent
* Equality
* Inductive
* Intersection
* List
* Object
* metaobject
* Option type
* Product
* Record or Struct
* Refinement
* Set
* Union
* tagged

Other|

* Boolean
* Bottom type
* Collection
* Enumerated type
* Exception
* Function type
* Opaque data type
* Recursive data type
* Semaphore
* Stream
* Strongly typed identifier
* Top type
* Type class
* Empty type
* Unit type
* Void

Related
topics|

* Abstract data type
* Boxing
* Data structure
* Generic
* Kind
* metaclass
* Parametric polymorphism
* Primitive data type
* Interface
* Subtyping
* Type constructor
* Type conversion
* Type system
* Type theory
* Variable

*[v]: View this template
*[t]: Discuss this template
*[e]: Edit this template