Complex Number Library
C++ complex number library
complex <float>
complex <double> complex <long double>
#include <complex>
template <class T> class complex ;
complex<T> is a class that supports complex numbers. A complex number has a real part and an imaginary part. The complex class supports equality, comparison and basic arithmetic operations. In addition, mathematical functions such as exponentiation, logarithmic, power, and square root are also available.
template <class T>
class complex { public: complex (T = 0 , T = 0); template <class X> complex (const complex<X>&); T real () const; T imag () const; template <class X> complex<T>& operator= (const complex<X>&); template <class X> complex<T>& operator+= (const complex<X>&); template <class X> complex<T>& operator-= (const complex<X>&); template <class X> complex<T>& operator*= (const complex<X>&); template <class X> complex<T>& operator/= (const complex<X>&); }; // Non-member Operators template<class T> complex<T> operator+ (const complex<T>&, const complex<T>&); template<class T> complex<T> operator+ (const complex<T>&, T); template<class T> complex<T> operator+ (T, const complex<T>&); template<class T> complex<T> operator- (const complex<T>&, const complex<T>&); template<class T> complex<T> operator- (const complex<T>&, T); template<classT> complex<T> operator- (T, const complex<T>&); template<class T> complex<T> operator* (const complex<T>&, const complex<T>&); template<class T> complex<T> operator* (const complex<T>&, T); template<class T> complex<T> operator* (T, const complex<T>&); template<class T> complex<T> operator/ (const complex<T>&, const complex<T>&); template<class T> complex<T> operator/ (const complex<T>&, T); template<class T> complex<T> operator/ (T, const complex<T>&); template<class T> complex<T> operator+ (const complex<T>&); template<class T> complex<T> operator- (const complex<T>&); template<class T> bool operator== (const complex<T>&, const complex<T>&); template<class T> bool operator== (const complex<T>&, T); template<class T> bool operator== (T, const complex<T>&); template<class T> bool operator!= (const complex<T>&, const complex<T>&); template<class T> bool operator!= (const complex<T>&, T); template<class T> bool operator!= (T, const complex<T>&); template <class X> istream& operator>> (istream&, complex<X>&); template <class X> ostream& operator<< (ostream&, const complex<X>&); // Values template<class T> T real (const complex<T>&); template<class T> T imag (const complex<T>&); template<class T> T abs (const complex<T>&); template<class T> T arg (const complex<T>&); template<class T> T norm (const complex<T>&); template<class T> complex<T> conj (const complex<T>&); template<class T> complex<T> polar (T, T); // Transcendentals template<class T> complex<T> acos (const complex<T>&); template<class T> complex<T> asin (const complex<T>&); template<class T> complex<T> atan (const complex<T>&); template<class T> complex<T> atan2 (const complex<T>&, const complex<T>&); template<class T> complex<T> atan2 (const complex<T>&, T); template<class T> complex<T> atan2 (T, const complex<T>&); template<class T> complex<T> cos (const complex<T>&); template<class T> complex<T> cosh (const complex<T>&); template<class T> complex<T> exp (const complex<T>&); template<class T> complex<T> log (const complex<T>&); template<class T> complex<T> log10 (const complex<T>&); template<class T> complex<T> pow (const complex<T>&, int); template<class T> complex<T> pow (const complex<T>&, T); template<class T> complex<T> pow (const complex<T>&, const complex<T>&); template<class T> complex<T> pow (T, const complex<T>&); template<class T> complex<T> sin (const complex<T>&); template<class T> complex<T> sinh (const complex<T>&); template<class T> complex<T> sqrt (const complex<T>&); template<class T> complex<T> tan (const complex<T>&); template<class T> complex<T> tanh (const complex<T>&);
complex (const T& re_arg = 0, const T& im_arg = 0);
Constructs an object of class complex, initializing re_arg to the real part and im_arg to the imaginary part.
template <class X> complex (const complex<X>&);
Copy constructor. Constructs a complex number from another complex number.
template <class X> complex<T> operator= (const complex<X>& c);
Assigns c to itself.
template <class X> complex<T> operator+= (const complex<X>& c);
Adds c to itself, then returns the result.
template <class X> complex<T> operator-= (const complex<X>& c);
Subtracts c from itself, then returns the result.
template <class X> complex<T> operator*= (const complex<X>& c);
Multiplies itelf by c then returns the result.
template <class X> complex<T> operator/= (const complex<X>& c);
Divides itself by c, then returns the result.
T imag() const;
Returns the imaginary part of the complex number.
T real() const;
Returns the real part of the complex number.
template<class T> complex<T> operator+ (const complex<T>& lhs,const complex<T>& rhs); template<class T> complex<T> operator+ (const complex<T>& lhs, T rhs); template<class T> complex<T> operator+ (T lhs, const complex<T>& rhs);
Returns the sum of lhs and rhs.
template<class T> complex<T> operator- (const complex<T>& lhs,const complex<T>& rhs); template<class T> complex<T> operator- (const complex<T>& lhs, T rhs); template<class T> complex<T> operator- (T lhs, const complex<T>& rhs);
Returns the difference of lhs and rhs.
template<class T> complex<T> operator* (const complex<T>& lhs,const complex<T>& rhs); template<class T> complex<T> operator* (const complex<T>& lhs, T rhs); template<class T> complex<T> operator* (T lhs, const complex<T>& rhs);
Returns the product of lhs and rhs.
template<class T> complex<T> operator/ (const complex<T>& lhs,const complex<T>& rhs); template<class T> complex<T> operator/ (const complex<T>& lhs, T rhs); template<class T> complex<T> operator/ (T lhs, const complex<T>& rhs);
Returns the quotient of lhs divided by rhs.
template<class T> complex<T> operator+ (const complex<T>& rhs);
Returns rhs.
template<class T> complex<T> operator- (const complex<T>& lhs);
Returns complex<T>(-lhs.real(), -lhs.imag()).
template<class T> bool operator== (const complex<T>& x, const complex<T>& y);
Returns true if the real and imaginary parts of x and y are equal.
template<class T> bool operator== (const complex<T>& x, T y);
Returns true if y is equal to the real part of x and the imaginary part of x is equal to 0.
template<class T> bool operator== (T x, const complex<T>& y);
Returns true if x is equal to the real part of y and the imaginary part of y is equal to 0.
template<class T> bool operator!= (const complex<T>& x, const complex<T>& y);
Returns true if either the real or the imaginary part of x and y are not equal.
template<class T> bool operator!= (const complex<T>& x, T y);
Returns true if y is not equal to the real part of x or the imaginary part of x is not equal to 0.
template<class T> bool operator!= (T x, const complex<T>& y);
Returns true if x is not equal to the real part of y or the imaginary part of y is not equal to 0.
template <class X> istream& operator>> (istream& is, complex<X>& x);
Reads a complex number x into the input stream is. x may be of the form u, (u), or (u,v) where u is the real part and v is the imaginary part. If bad input is encountered, the ios::badbit flag is set.
template <class X> ostream& operator<< (ostream& os, const complex<X>& x);
Returns os << "(" << x.real() << "," << x.imag() << ")".
template<class T> T abs(const complex<T>& c);
Returns the absolute value or magnitude of c (the square root of the norm).
template<class T> complex<T> acos (const complex<T>& c);
Returns the arccosine of c.
template<class T> T arg (const complex<T>& c);
Returns the phase angle of c.
template<class T> complex<T> asin (const complex<T>& c);
Returns the arcsine of c.
template<class T> complex<T> atan (const complex<T>& c);
Returns the arctangent of c.
template<class T> complex<T> atan2 (T a, const complex<T>& b);
Returns the arctangent of a/b.
template<class T> complex<T> atan2 (const complex<T>& a, T b);
Returns the arctangent of a/b.
template<class T> complex<T> atan2 (const complex<T>& a, const complex<T>& b);
Returns the arctangent of a/b.
template<class T> complex<T> conj (const complex<T>& c);
Returns the conjugate of c.
template<class T> complex<T> cos (const complex<T>& c);
Returns the cosine of c.
template<class T> complex<T> cosh (const complex<T>& c);
Returns the hyperbolic cosine of c.
template<class T> complex<T> exp (const complex<T>& x);
Returns e raised to the x power.
template<class T> T imag (const complex<T>& c) const;
Returns the imaginary part of c.
template<class T> complex<T> log (const complex<T>& x);
Returns the natural logarithm of x.
template<class T> complex<T> log10 (const complex<T>& x);
Returns the logarithm base 10 of x.
template<class T> T norm (const complex<T>& c);
Returns the squared magnitude of c. (The sum of the squares of the real and imaginary parts.)
template<class T> complex<T> polar (const T& m, const T& a);
Returns the complex value of a complex number whose magnitude is m and phase angle is a, measured in radians.
template<class T> complex<T> pow (const complex<T>& x, int y); template<class T> complex<T> pow (const complex<T>& x, T y); template<class T> complex<T> pow (const complex<T>& x, const complex<T>& y); template<class T> complex<T> pow (T x, const complex<T>& y);
Returns x raised to the y power.
template<class T> T real (const complex<T>& c);
Returns the real part of c.
template<class T> complex<T> sin (const complex<T>& c);
Returns the sine of c.
template<class T> complex<T> sinh (const complex<T>& c);
Returns the hyperbolic sine of c.
template<class T> complex<T> sqrt (const complex<T>& x);
Returns the square root of x.
template<class T> complex<T> tan (const complex<T>& x);
Returns the tangent of x.
template<class T> complex<T> tanh (const complex<T>& x);
Returns the hyperbolic tangent of x.
// // complex.cpp // #include <complex> #include <iostream.h> int main() { complex<double> a(1.2, 3.4); complex<double> b(-9.8, -7.6); a += b; a /= sin(b) * cos(a); b *= log(a) + pow(b, a); cout << "a = " << a << ", b = " << b << endl; return 0; } Output : a = (1.42804e-06,-0.0002873), b = (58.2199,69.7354)
On compilers that don't support member function templates, the arithmetic operators will not work on any arbitrary type. (They will work only on float, double and long doubles.) You also will only be able to perform binary arithmetic on types that are the same.
Compilers that don't support non-converting constructors will permit unsafe downcasts (i.e., long double to double, double to float, long double to float).