Some
programming languages provide a complex data type for
complex number storage and arithmetic as a built-in (primitive)
data type
In computer science and computer programming, a data type (or simply type) is a set of possible values and a set of allowed operations on it. A data type tells the compiler or interpreter how the programmer intends to use the data. Most progra ...
.
In some programming environments the term ''complex data type'' (in contrast to
primitive data types) is a synonym for the
composite data type.
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 that represents real numbers approximately, using an integer with a fixed precision, called the significand, scaled by an integer exponent of a fixed base. For example, 12.345 can b ...
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
Mathematics is an area of knowledge that includes the topics of numbers, formulas and related structures, shapes and the spaces in which they are contained, and quantities and their changes. These topics are represented in modern mathematics ...
, 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).
* The
Perl 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 provides a class in the standard library module .
*
OCaml
OCaml ( , formerly Objective Caml) is a general-purpose programming language, general-purpose, multi-paradigm programming language which extends the Caml dialect of ML (programming language), ML with object-oriented programming, object-oriented ...
supports complex numbers with the standard library module .
*
Haskell supports complex numbers with the standard library module (previously called ).
*
Mercury
Mercury commonly refers to:
* Mercury (planet), the nearest planet to the Sun
* Mercury (element), a metallic chemical element with the symbol Hg
* Mercury (mythology), a Roman god
Mercury or The Mercury may also refer to:
Companies
* Merc ...
provides complex numbers with full operator overloading support in the extras distribution, using .
*
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 The Apache Commons is a project of the Apache Software Foundation, formerly under the Jakarta Project. The purpose of the Commons is to provide reusable, open source Java software. The Commons is composed of three parts: proper, sandbox, and dorm ...
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 ANSI standard document ''ANSI INCITS 226-1994 (S20018)'' (formerly ''X3.226-1994 (R1999)''). The Common Lisp HyperSpec, a hyperlinked HTML version, has been derived fro ...
: 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
The .NET Framework (pronounced as "''dot net"'') is a proprietary software framework developed by Microsoft that runs primarily on Microsoft Windows. It was the predominant implementation of the Common Language Infrastructure (CLI) until bein ...
provide
System.Numerics.Complexsince 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)
2i
*
Julia includes predefined types for both complex and rational numbers since at least version 0.3
*
R provides a
complex
basic data type
References
{{DEFAULTSORT:Complex Data Type
Data types