std::complex - cppreference.com (2024)

C++

Language
Standard library headers
Concepts
Utilities library
Strings library
Containers library
Algorithms library
Iterators library
Numerics library
Input/output library
Localizations library
Regular expressions library (C++11)
Atomic operations library (C++11)
Thread support library (C++11)
Filesystem library (C++17)
Technical Specifications

Numerics library

Common mathematical functions
Special mathematical functions
Floating-point environment (C++11)
Complex numbers
Numeric arrays
Pseudo-random number generation
Compile-time rational arithmetic (C++11)
Numeric algorithms

gcd

(C++17)

lcm

(C++17)

Generic numeric operations

iota

(C++11)

accumulate

inner_product

adjacent_difference

partial_sum

std::complex

Member functions

complex::complex

complex::operator=

complex::real

complex::imag

complex::operator+=complex::operator-=complex::operator*=complex::operator/=

Non-member functions

operator+operator-

operator+operator-operator*operator/

operator==operator!=

operator<<operator>>

real

imag

abs

arg

norm

conj

proj

(C++11)

polar

operator""ioperator""ifoperator""il

(C++14)(C++14)(C++14)

Exponential functions

exp

log

log10

Power functions

pow

sqrt

Trigonometric functions

sin

cos

tan

asin

(C++11)

acos

(C++11)

atan

(C++11)

Hyperbolic functions

sinh

cosh

tanh

asinh

(C++11)

acosh

(C++11)

atanh

(C++11)

Defined in header <complex>

template< class T >
class complex;

(1)

template<> class complex<float>;

(2)

template<> class complex<double>;

(3)

template<> class complex<long double>;

(4)

The specializations std::complex<float>, std::complex<double>, and std::complex<long double> are LiteralTypes for representing and manipulating complex numbers.

The effect of instantiating the template complex for any other type is unspecified.

Member types

Member type Definition
value_type T

Member functions

(constructor)

constructs a complex number
(public member function)

operator=

assigns the contents
(public member function)

real

accesses the real part of the complex number
(public member function)

imag

accesses the imaginary part of the complex number
(public member function)

operator+=operator-=operator/=operator*=

compound assignment of two complex numbers or a complex and a scalar
(public member function)

Non-member functions

operator+operator-

applies unary operators to complex numbers
(function template)

operator+operator-operator*operator/

performs complex number arithmetics on two complex values or a complex and a scalar
(function template)

operator==operator!=

compares two complex numbers or a complex and a scalar
(function template)

operator<<operator>>

serializes and deserializes a complex number
(function template)

real

returns the real component
(function template)

imag

returns the imaginary component
(function template)

abs(std::complex)

returns the magnitude of a complex number
(function template)

arg

returns the phase angle
(function template)

norm

returns the squared magnitude
(function template)

conj

returns the complex conjugate
(function template)

proj

(C++11)

returns the projection onto the Riemann sphere
(function template)

polar

constructs a complex number from magnitude and phase angle
(function template)
Exponential functions

exp(std::complex)

complex base e exponential
(function template)

log(std::complex)

complex natural logarithm with the branch cuts along the negative real axis
(function template)

log10(std::complex)

complex common logarithm with the branch cuts along the negative real axis
(function template)
Power functions

pow(std::complex)

complex power, one or both arguments may be a complex number
(function template)

sqrt(std::complex)

complex square root in the range of the right half-plane
(function template)
Trigonometric functions

sin(std::complex)

computes sine of a complex number (sin(z))
(function template)

cos(std::complex)

computes cosine of a complex number (cos(z))
(function template)

tan(std::complex)

computes tangent of a complex number (tan(z))
(function template)

asin(std::complex)

(C++11)

computes arc sine of a complex number (arcsin(z))
(function template)

acos(std::complex)

(C++11)

computes arc cosine of a complex number (arccos(z))
(function template)

atan(std::complex)

(C++11)

computes arc tangent of a complex number (arctan(z))
(function template)
Hyperbolic functions

sinh(std::complex)

computes hyperbolic sine of a complex number (sh(z))
(function template)

cosh(std::complex)

computes hyperbolic cosine of a complex number (ch(z))
(function template)

tanh(std::complex)

computes hyperbolic tangent of a complex number
(function template)

asinh(std::complex)

(C++11)

computes area hyperbolic sine of a complex number
(function template)

acosh(std::complex)

(C++11)

computes area hyperbolic cosine of a complex number
(function template)

atanh(std::complex)

(C++11)

computes area hyperbolic tangent of a complex number
(function template)

Non-static data members

For any object z of type complex<T>, reinterpret_cast<T(&)[2]>(z)[0] is the real part of z and reinterpret_cast<T(&)[2]>(z)[1] is the imaginary part of z.

For any pointer to an element of an array of complex<T> named p and any valid array index i, reinterpret_cast<T*>(p)[2*i] is the real part of the complex number p[i], and reinterpret_cast<T*>(p)[2*i + 1] is the imaginary part of the complex number p[i]

These requirements essentially limit implementation of each of the three specializations of std::complex to declaring two and only two non-static data members, of type value_type, with the same member access, which hold the real and the imaginary components, respectively.

The intent of this requirement is to preserve binary compatibility between the C++ library complex number types and the C language complex number types (and arrays thereof), which have an identical object representation requirement.

(since C++11)

Literals

Defined in inline namespace std::literals::complex_literals

operator""ifoperator""ioperator""il

(C++14)

A std::complex literal representing pure imaginary number
(function)

Example

Run this code

#include <iostream>#include <iomanip>#include <complex>#include <cmath>int main(){ using namespace std::complex_literals; std::cout << std::fixed << std::setprecision(1); std::complex<double> z1 = 1i * 1i; // imaginary unit squared std::cout << "i * i = " << z1 << '\n'; std::complex<double> z2 = std::pow(1i, 2); // imaginary unit squared std::cout << "pow(i, 2) = " << z2 << '\n'; double PI = std::acos(-1); std::complex<double> z3 = std::exp(1i * PI); // Euler's formula std::cout << "exp(i * pi) = " << z3 << '\n'; std::complex<double> z4 = 1. + 2i, z5 = 1. - 2i; // conjugates std::cout << "(1+2i)*(1-2i) = " << z4*z5 << '\n';}

Output:

i * i = (-1.0,0.0)pow(i, 2) = (-1.0,0.0)exp(i * pi) = (-1.0,0.0)(1+2i)*(1-2i) = (5.0,0.0)

See also

C documentation for Complex number arithmetic

std::complex - cppreference.com (2024)
Top Articles
Latest Posts
Article information

Author: Edmund Hettinger DC

Last Updated:

Views: 5719

Rating: 4.8 / 5 (78 voted)

Reviews: 85% of readers found this page helpful

Author information

Name: Edmund Hettinger DC

Birthday: 1994-08-17

Address: 2033 Gerhold Pine, Port Jocelyn, VA 12101-5654

Phone: +8524399971620

Job: Central Manufacturing Supervisor

Hobby: Jogging, Metalworking, Tai chi, Shopping, Puzzles, Rock climbing, Crocheting

Introduction: My name is Edmund Hettinger DC, I am a adventurous, colorful, gifted, determined, precious, open, colorful person who loves writing and wants to share my knowledge and understanding with you.