2 //===--------------------------- complex ----------------------------------===//
4 // The LLVM Compiler Infrastructure
6 // This file is dual licensed under the MIT and the University of Illinois Open
7 // Source Licenses. See LICENSE.TXT for details.
9 //===----------------------------------------------------------------------===//
11 #ifndef _LIBCPP_COMPLEX
12 #define _LIBCPP_COMPLEX
26 complex(const T& re = T(), const T& im = T()); // constexpr in C++14
27 complex(const complex&); // constexpr in C++14
28 template<class X> complex(const complex<X>&); // constexpr in C++14
30 T real() const; // constexpr in C++14
31 T imag() const; // constexpr in C++14
36 complex<T>& operator= (const T&);
37 complex<T>& operator+=(const T&);
38 complex<T>& operator-=(const T&);
39 complex<T>& operator*=(const T&);
40 complex<T>& operator/=(const T&);
42 complex& operator=(const complex&);
43 template<class X> complex<T>& operator= (const complex<X>&);
44 template<class X> complex<T>& operator+=(const complex<X>&);
45 template<class X> complex<T>& operator-=(const complex<X>&);
46 template<class X> complex<T>& operator*=(const complex<X>&);
47 template<class X> complex<T>& operator/=(const complex<X>&);
54 typedef float value_type;
56 constexpr complex(float re = 0.0f, float im = 0.0f);
57 explicit constexpr complex(const complex<double>&);
58 explicit constexpr complex(const complex<long double>&);
60 constexpr float real() const;
62 constexpr float imag() const;
65 complex<float>& operator= (float);
66 complex<float>& operator+=(float);
67 complex<float>& operator-=(float);
68 complex<float>& operator*=(float);
69 complex<float>& operator/=(float);
71 complex<float>& operator=(const complex<float>&);
72 template<class X> complex<float>& operator= (const complex<X>&);
73 template<class X> complex<float>& operator+=(const complex<X>&);
74 template<class X> complex<float>& operator-=(const complex<X>&);
75 template<class X> complex<float>& operator*=(const complex<X>&);
76 template<class X> complex<float>& operator/=(const complex<X>&);
83 typedef double value_type;
85 constexpr complex(double re = 0.0, double im = 0.0);
86 constexpr complex(const complex<float>&);
87 explicit constexpr complex(const complex<long double>&);
89 constexpr double real() const;
91 constexpr double imag() const;
94 complex<double>& operator= (double);
95 complex<double>& operator+=(double);
96 complex<double>& operator-=(double);
97 complex<double>& operator*=(double);
98 complex<double>& operator/=(double);
99 complex<double>& operator=(const complex<double>&);
101 template<class X> complex<double>& operator= (const complex<X>&);
102 template<class X> complex<double>& operator+=(const complex<X>&);
103 template<class X> complex<double>& operator-=(const complex<X>&);
104 template<class X> complex<double>& operator*=(const complex<X>&);
105 template<class X> complex<double>& operator/=(const complex<X>&);
109 class complex<long double>
112 typedef long double value_type;
114 constexpr complex(long double re = 0.0L, long double im = 0.0L);
115 constexpr complex(const complex<float>&);
116 constexpr complex(const complex<double>&);
118 constexpr long double real() const;
119 void real(long double);
120 constexpr long double imag() const;
121 void imag(long double);
123 complex<long double>& operator=(const complex<long double>&);
124 complex<long double>& operator= (long double);
125 complex<long double>& operator+=(long double);
126 complex<long double>& operator-=(long double);
127 complex<long double>& operator*=(long double);
128 complex<long double>& operator/=(long double);
130 template<class X> complex<long double>& operator= (const complex<X>&);
131 template<class X> complex<long double>& operator+=(const complex<X>&);
132 template<class X> complex<long double>& operator-=(const complex<X>&);
133 template<class X> complex<long double>& operator*=(const complex<X>&);
134 template<class X> complex<long double>& operator/=(const complex<X>&);
138 template<class T> complex<T> operator+(const complex<T>&, const complex<T>&);
139 template<class T> complex<T> operator+(const complex<T>&, const T&);
140 template<class T> complex<T> operator+(const T&, const complex<T>&);
141 template<class T> complex<T> operator-(const complex<T>&, const complex<T>&);
142 template<class T> complex<T> operator-(const complex<T>&, const T&);
143 template<class T> complex<T> operator-(const T&, const complex<T>&);
144 template<class T> complex<T> operator*(const complex<T>&, const complex<T>&);
145 template<class T> complex<T> operator*(const complex<T>&, const T&);
146 template<class T> complex<T> operator*(const T&, const complex<T>&);
147 template<class T> complex<T> operator/(const complex<T>&, const complex<T>&);
148 template<class T> complex<T> operator/(const complex<T>&, const T&);
149 template<class T> complex<T> operator/(const T&, const complex<T>&);
150 template<class T> complex<T> operator+(const complex<T>&);
151 template<class T> complex<T> operator-(const complex<T>&);
152 template<class T> bool operator==(const complex<T>&, const complex<T>&); // constexpr in C++14
153 template<class T> bool operator==(const complex<T>&, const T&); // constexpr in C++14
154 template<class T> bool operator==(const T&, const complex<T>&); // constexpr in C++14
155 template<class T> bool operator!=(const complex<T>&, const complex<T>&); // constexpr in C++14
156 template<class T> bool operator!=(const complex<T>&, const T&); // constexpr in C++14
157 template<class T> bool operator!=(const T&, const complex<T>&); // constexpr in C++14
159 template<class T, class charT, class traits>
160 basic_istream<charT, traits>&
161 operator>>(basic_istream<charT, traits>&, complex<T>&);
162 template<class T, class charT, class traits>
163 basic_ostream<charT, traits>&
164 operator<<(basic_ostream<charT, traits>&, const complex<T>&);
168 template<class T> T real(const complex<T>&); // constexpr in C++14
169 long double real(long double); // constexpr in C++14
170 double real(double); // constexpr in C++14
171 template<Integral T> double real(T); // constexpr in C++14
172 float real(float); // constexpr in C++14
174 template<class T> T imag(const complex<T>&); // constexpr in C++14
175 long double imag(long double); // constexpr in C++14
176 double imag(double); // constexpr in C++14
177 template<Integral T> double imag(T); // constexpr in C++14
178 float imag(float); // constexpr in C++14
180 template<class T> T abs(const complex<T>&);
182 template<class T> T arg(const complex<T>&);
183 long double arg(long double);
185 template<Integral T> double arg(T);
188 template<class T> T norm(const complex<T>&);
189 long double norm(long double);
191 template<Integral T> double norm(T);
194 template<class T> complex<T> conj(const complex<T>&);
195 complex<long double> conj(long double);
196 complex<double> conj(double);
197 template<Integral T> complex<double> conj(T);
198 complex<float> conj(float);
200 template<class T> complex<T> proj(const complex<T>&);
201 complex<long double> proj(long double);
202 complex<double> proj(double);
203 template<Integral T> complex<double> proj(T);
204 complex<float> proj(float);
206 template<class T> complex<T> polar(const T&, const T& = 0);
208 // 26.3.8 transcendentals:
209 template<class T> complex<T> acos(const complex<T>&);
210 template<class T> complex<T> asin(const complex<T>&);
211 template<class T> complex<T> atan(const complex<T>&);
212 template<class T> complex<T> acosh(const complex<T>&);
213 template<class T> complex<T> asinh(const complex<T>&);
214 template<class T> complex<T> atanh(const complex<T>&);
215 template<class T> complex<T> cos (const complex<T>&);
216 template<class T> complex<T> cosh (const complex<T>&);
217 template<class T> complex<T> exp (const complex<T>&);
218 template<class T> complex<T> log (const complex<T>&);
219 template<class T> complex<T> log10(const complex<T>&);
221 template<class T> complex<T> pow(const complex<T>&, const T&);
222 template<class T> complex<T> pow(const complex<T>&, const complex<T>&);
223 template<class T> complex<T> pow(const T&, const complex<T>&);
225 template<class T> complex<T> sin (const complex<T>&);
226 template<class T> complex<T> sinh (const complex<T>&);
227 template<class T> complex<T> sqrt (const complex<T>&);
228 template<class T> complex<T> tan (const complex<T>&);
229 template<class T> complex<T> tanh (const complex<T>&);
231 template<class T, class charT, class traits>
232 basic_istream<charT, traits>&
233 operator>>(basic_istream<charT, traits>& is, complex<T>& x);
235 template<class T, class charT, class traits>
236 basic_ostream<charT, traits>&
237 operator<<(basic_ostream<charT, traits>& o, const complex<T>& x);
244 #include <type_traits>
248 #if defined(_LIBCPP_NO_EXCEPTIONS)
252 #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
253 #pragma GCC system_header
256 _LIBCPP_BEGIN_NAMESPACE_STD
258 template<class _Tp> class _LIBCPP_TYPE_VIS_ONLY complex;
260 template<class _Tp> complex<_Tp> operator*(const complex<_Tp>& __z, const complex<_Tp>& __w);
261 template<class _Tp> complex<_Tp> operator/(const complex<_Tp>& __x, const complex<_Tp>& __y);
264 class _LIBCPP_TYPE_VIS_ONLY complex
267 typedef _Tp value_type;
272 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
273 complex(const value_type& __re = value_type(), const value_type& __im = value_type())
274 : __re_(__re), __im_(__im) {}
275 template<class _Xp> _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
276 complex(const complex<_Xp>& __c)
277 : __re_(__c.real()), __im_(__c.imag()) {}
279 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11 value_type real() const {return __re_;}
280 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11 value_type imag() const {return __im_;}
282 _LIBCPP_INLINE_VISIBILITY void real(value_type __re) {__re_ = __re;}
283 _LIBCPP_INLINE_VISIBILITY void imag(value_type __im) {__im_ = __im;}
285 _LIBCPP_INLINE_VISIBILITY complex& operator= (const value_type& __re)
286 {__re_ = __re; __im_ = value_type(); return *this;}
287 _LIBCPP_INLINE_VISIBILITY complex& operator+=(const value_type& __re) {__re_ += __re; return *this;}
288 _LIBCPP_INLINE_VISIBILITY complex& operator-=(const value_type& __re) {__re_ -= __re; return *this;}
289 _LIBCPP_INLINE_VISIBILITY complex& operator*=(const value_type& __re) {__re_ *= __re; __im_ *= __re; return *this;}
290 _LIBCPP_INLINE_VISIBILITY complex& operator/=(const value_type& __re) {__re_ /= __re; __im_ /= __re; return *this;}
292 template<class _Xp> _LIBCPP_INLINE_VISIBILITY complex& operator= (const complex<_Xp>& __c)
298 template<class _Xp> _LIBCPP_INLINE_VISIBILITY complex& operator+=(const complex<_Xp>& __c)
304 template<class _Xp> _LIBCPP_INLINE_VISIBILITY complex& operator-=(const complex<_Xp>& __c)
310 template<class _Xp> _LIBCPP_INLINE_VISIBILITY complex& operator*=(const complex<_Xp>& __c)
312 *this = *this * complex(__c.real(), __c.imag());
315 template<class _Xp> _LIBCPP_INLINE_VISIBILITY complex& operator/=(const complex<_Xp>& __c)
317 *this = *this / complex(__c.real(), __c.imag());
322 template<> class _LIBCPP_TYPE_VIS_ONLY complex<double>;
323 template<> class _LIBCPP_TYPE_VIS_ONLY complex<long double>;
326 class _LIBCPP_TYPE_VIS_ONLY complex<float>
331 typedef float value_type;
333 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR complex(float __re = 0.0f, float __im = 0.0f)
334 : __re_(__re), __im_(__im) {}
335 _LIBCPP_INLINE_VISIBILITY
336 explicit _LIBCPP_CONSTEXPR complex(const complex<double>& __c);
337 _LIBCPP_INLINE_VISIBILITY
338 explicit _LIBCPP_CONSTEXPR complex(const complex<long double>& __c);
340 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR float real() const {return __re_;}
341 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR float imag() const {return __im_;}
343 _LIBCPP_INLINE_VISIBILITY void real(value_type __re) {__re_ = __re;}
344 _LIBCPP_INLINE_VISIBILITY void imag(value_type __im) {__im_ = __im;}
346 _LIBCPP_INLINE_VISIBILITY complex& operator= (float __re)
347 {__re_ = __re; __im_ = value_type(); return *this;}
348 _LIBCPP_INLINE_VISIBILITY complex& operator+=(float __re) {__re_ += __re; return *this;}
349 _LIBCPP_INLINE_VISIBILITY complex& operator-=(float __re) {__re_ -= __re; return *this;}
350 _LIBCPP_INLINE_VISIBILITY complex& operator*=(float __re) {__re_ *= __re; __im_ *= __re; return *this;}
351 _LIBCPP_INLINE_VISIBILITY complex& operator/=(float __re) {__re_ /= __re; __im_ /= __re; return *this;}
353 template<class _Xp> _LIBCPP_INLINE_VISIBILITY complex& operator= (const complex<_Xp>& __c)
359 template<class _Xp> _LIBCPP_INLINE_VISIBILITY complex& operator+=(const complex<_Xp>& __c)
365 template<class _Xp> _LIBCPP_INLINE_VISIBILITY complex& operator-=(const complex<_Xp>& __c)
371 template<class _Xp> _LIBCPP_INLINE_VISIBILITY complex& operator*=(const complex<_Xp>& __c)
373 *this = *this * complex(__c.real(), __c.imag());
376 template<class _Xp> _LIBCPP_INLINE_VISIBILITY complex& operator/=(const complex<_Xp>& __c)
378 *this = *this / complex(__c.real(), __c.imag());
384 class _LIBCPP_TYPE_VIS_ONLY complex<double>
389 typedef double value_type;
391 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR complex(double __re = 0.0, double __im = 0.0)
392 : __re_(__re), __im_(__im) {}
393 _LIBCPP_INLINE_VISIBILITY
394 _LIBCPP_CONSTEXPR complex(const complex<float>& __c);
395 _LIBCPP_INLINE_VISIBILITY
396 explicit _LIBCPP_CONSTEXPR complex(const complex<long double>& __c);
398 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR double real() const {return __re_;}
399 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR double imag() const {return __im_;}
401 _LIBCPP_INLINE_VISIBILITY void real(value_type __re) {__re_ = __re;}
402 _LIBCPP_INLINE_VISIBILITY void imag(value_type __im) {__im_ = __im;}
404 _LIBCPP_INLINE_VISIBILITY complex& operator= (double __re)
405 {__re_ = __re; __im_ = value_type(); return *this;}
406 _LIBCPP_INLINE_VISIBILITY complex& operator+=(double __re) {__re_ += __re; return *this;}
407 _LIBCPP_INLINE_VISIBILITY complex& operator-=(double __re) {__re_ -= __re; return *this;}
408 _LIBCPP_INLINE_VISIBILITY complex& operator*=(double __re) {__re_ *= __re; __im_ *= __re; return *this;}
409 _LIBCPP_INLINE_VISIBILITY complex& operator/=(double __re) {__re_ /= __re; __im_ /= __re; return *this;}
411 template<class _Xp> _LIBCPP_INLINE_VISIBILITY complex& operator= (const complex<_Xp>& __c)
417 template<class _Xp> _LIBCPP_INLINE_VISIBILITY complex& operator+=(const complex<_Xp>& __c)
423 template<class _Xp> _LIBCPP_INLINE_VISIBILITY complex& operator-=(const complex<_Xp>& __c)
429 template<class _Xp> _LIBCPP_INLINE_VISIBILITY complex& operator*=(const complex<_Xp>& __c)
431 *this = *this * complex(__c.real(), __c.imag());
434 template<class _Xp> _LIBCPP_INLINE_VISIBILITY complex& operator/=(const complex<_Xp>& __c)
436 *this = *this / complex(__c.real(), __c.imag());
442 class _LIBCPP_TYPE_VIS_ONLY complex<long double>
447 typedef long double value_type;
449 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR complex(long double __re = 0.0L, long double __im = 0.0L)
450 : __re_(__re), __im_(__im) {}
451 _LIBCPP_INLINE_VISIBILITY
452 _LIBCPP_CONSTEXPR complex(const complex<float>& __c);
453 _LIBCPP_INLINE_VISIBILITY
454 _LIBCPP_CONSTEXPR complex(const complex<double>& __c);
456 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR long double real() const {return __re_;}
457 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR long double imag() const {return __im_;}
459 _LIBCPP_INLINE_VISIBILITY void real(value_type __re) {__re_ = __re;}
460 _LIBCPP_INLINE_VISIBILITY void imag(value_type __im) {__im_ = __im;}
462 _LIBCPP_INLINE_VISIBILITY complex& operator= (long double __re)
463 {__re_ = __re; __im_ = value_type(); return *this;}
464 _LIBCPP_INLINE_VISIBILITY complex& operator+=(long double __re) {__re_ += __re; return *this;}
465 _LIBCPP_INLINE_VISIBILITY complex& operator-=(long double __re) {__re_ -= __re; return *this;}
466 _LIBCPP_INLINE_VISIBILITY complex& operator*=(long double __re) {__re_ *= __re; __im_ *= __re; return *this;}
467 _LIBCPP_INLINE_VISIBILITY complex& operator/=(long double __re) {__re_ /= __re; __im_ /= __re; return *this;}
469 template<class _Xp> _LIBCPP_INLINE_VISIBILITY complex& operator= (const complex<_Xp>& __c)
475 template<class _Xp> _LIBCPP_INLINE_VISIBILITY complex& operator+=(const complex<_Xp>& __c)
481 template<class _Xp> _LIBCPP_INLINE_VISIBILITY complex& operator-=(const complex<_Xp>& __c)
487 template<class _Xp> _LIBCPP_INLINE_VISIBILITY complex& operator*=(const complex<_Xp>& __c)
489 *this = *this * complex(__c.real(), __c.imag());
492 template<class _Xp> _LIBCPP_INLINE_VISIBILITY complex& operator/=(const complex<_Xp>& __c)
494 *this = *this / complex(__c.real(), __c.imag());
501 complex<float>::complex(const complex<double>& __c)
502 : __re_(__c.real()), __im_(__c.imag()) {}
506 complex<float>::complex(const complex<long double>& __c)
507 : __re_(__c.real()), __im_(__c.imag()) {}
511 complex<double>::complex(const complex<float>& __c)
512 : __re_(__c.real()), __im_(__c.imag()) {}
516 complex<double>::complex(const complex<long double>& __c)
517 : __re_(__c.real()), __im_(__c.imag()) {}
521 complex<long double>::complex(const complex<float>& __c)
522 : __re_(__c.real()), __im_(__c.imag()) {}
526 complex<long double>::complex(const complex<double>& __c)
527 : __re_(__c.real()), __im_(__c.imag()) {}
532 inline _LIBCPP_INLINE_VISIBILITY
534 operator+(const complex<_Tp>& __x, const complex<_Tp>& __y)
536 complex<_Tp> __t(__x);
542 inline _LIBCPP_INLINE_VISIBILITY
544 operator+(const complex<_Tp>& __x, const _Tp& __y)
546 complex<_Tp> __t(__x);
552 inline _LIBCPP_INLINE_VISIBILITY
554 operator+(const _Tp& __x, const complex<_Tp>& __y)
556 complex<_Tp> __t(__y);
562 inline _LIBCPP_INLINE_VISIBILITY
564 operator-(const complex<_Tp>& __x, const complex<_Tp>& __y)
566 complex<_Tp> __t(__x);
572 inline _LIBCPP_INLINE_VISIBILITY
574 operator-(const complex<_Tp>& __x, const _Tp& __y)
576 complex<_Tp> __t(__x);
582 inline _LIBCPP_INLINE_VISIBILITY
584 operator-(const _Tp& __x, const complex<_Tp>& __y)
586 complex<_Tp> __t(-__y);
593 operator*(const complex<_Tp>& __z, const complex<_Tp>& __w)
595 _Tp __a = __z.real();
596 _Tp __b = __z.imag();
597 _Tp __c = __w.real();
598 _Tp __d = __w.imag();
599 _Tp __ac = __a * __c;
600 _Tp __bd = __b * __d;
601 _Tp __ad = __a * __d;
602 _Tp __bc = __b * __c;
603 _Tp __x = __ac - __bd;
604 _Tp __y = __ad + __bc;
605 if (isnan(__x) && isnan(__y))
607 bool __recalc = false;
608 if (isinf(__a) || isinf(__b))
610 __a = copysign(isinf(__a) ? _Tp(1) : _Tp(0), __a);
611 __b = copysign(isinf(__b) ? _Tp(1) : _Tp(0), __b);
613 __c = copysign(_Tp(0), __c);
615 __d = copysign(_Tp(0), __d);
618 if (isinf(__c) || isinf(__d))
620 __c = copysign(isinf(__c) ? _Tp(1) : _Tp(0), __c);
621 __d = copysign(isinf(__d) ? _Tp(1) : _Tp(0), __d);
623 __a = copysign(_Tp(0), __a);
625 __b = copysign(_Tp(0), __b);
628 if (!__recalc && (isinf(__ac) || isinf(__bd) ||
629 isinf(__ad) || isinf(__bc)))
632 __a = copysign(_Tp(0), __a);
634 __b = copysign(_Tp(0), __b);
636 __c = copysign(_Tp(0), __c);
638 __d = copysign(_Tp(0), __d);
643 __x = _Tp(INFINITY) * (__a * __c - __b * __d);
644 __y = _Tp(INFINITY) * (__a * __d + __b * __c);
647 return complex<_Tp>(__x, __y);
651 inline _LIBCPP_INLINE_VISIBILITY
653 operator*(const complex<_Tp>& __x, const _Tp& __y)
655 complex<_Tp> __t(__x);
661 inline _LIBCPP_INLINE_VISIBILITY
663 operator*(const _Tp& __x, const complex<_Tp>& __y)
665 complex<_Tp> __t(__y);
672 operator/(const complex<_Tp>& __z, const complex<_Tp>& __w)
675 _Tp __a = __z.real();
676 _Tp __b = __z.imag();
677 _Tp __c = __w.real();
678 _Tp __d = __w.imag();
679 _Tp __logbw = logb(fmax(fabs(__c), fabs(__d)));
680 if (isfinite(__logbw))
682 __ilogbw = static_cast<int>(__logbw);
683 __c = scalbn(__c, -__ilogbw);
684 __d = scalbn(__d, -__ilogbw);
686 _Tp __denom = __c * __c + __d * __d;
687 _Tp __x = scalbn((__a * __c + __b * __d) / __denom, -__ilogbw);
688 _Tp __y = scalbn((__b * __c - __a * __d) / __denom, -__ilogbw);
689 if (isnan(__x) && isnan(__y))
691 if ((__denom == _Tp(0)) && (!isnan(__a) || !isnan(__b)))
693 __x = copysign(_Tp(INFINITY), __c) * __a;
694 __y = copysign(_Tp(INFINITY), __c) * __b;
696 else if ((isinf(__a) || isinf(__b)) && isfinite(__c) && isfinite(__d))
698 __a = copysign(isinf(__a) ? _Tp(1) : _Tp(0), __a);
699 __b = copysign(isinf(__b) ? _Tp(1) : _Tp(0), __b);
700 __x = _Tp(INFINITY) * (__a * __c + __b * __d);
701 __y = _Tp(INFINITY) * (__b * __c - __a * __d);
703 else if (isinf(__logbw) && __logbw > _Tp(0) && isfinite(__a) && isfinite(__b))
705 __c = copysign(isinf(__c) ? _Tp(1) : _Tp(0), __c);
706 __d = copysign(isinf(__d) ? _Tp(1) : _Tp(0), __d);
707 __x = _Tp(0) * (__a * __c + __b * __d);
708 __y = _Tp(0) * (__b * __c - __a * __d);
711 return complex<_Tp>(__x, __y);
715 inline _LIBCPP_INLINE_VISIBILITY
717 operator/(const complex<_Tp>& __x, const _Tp& __y)
719 return complex<_Tp>(__x.real() / __y, __x.imag() / __y);
723 inline _LIBCPP_INLINE_VISIBILITY
725 operator/(const _Tp& __x, const complex<_Tp>& __y)
727 complex<_Tp> __t(__x);
733 inline _LIBCPP_INLINE_VISIBILITY
735 operator+(const complex<_Tp>& __x)
741 inline _LIBCPP_INLINE_VISIBILITY
743 operator-(const complex<_Tp>& __x)
745 return complex<_Tp>(-__x.real(), -__x.imag());
749 inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
751 operator==(const complex<_Tp>& __x, const complex<_Tp>& __y)
753 return __x.real() == __y.real() && __x.imag() == __y.imag();
757 inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
759 operator==(const complex<_Tp>& __x, const _Tp& __y)
761 return __x.real() == __y && __x.imag() == 0;
765 inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
767 operator==(const _Tp& __x, const complex<_Tp>& __y)
769 return __x == __y.real() && 0 == __y.imag();
773 inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
775 operator!=(const complex<_Tp>& __x, const complex<_Tp>& __y)
777 return !(__x == __y);
781 inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
783 operator!=(const complex<_Tp>& __x, const _Tp& __y)
785 return !(__x == __y);
789 inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
791 operator!=(const _Tp& __x, const complex<_Tp>& __y)
793 return !(__x == __y);
801 inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
803 real(const complex<_Tp>& __c)
808 inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
810 real(long double __re)
815 inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
823 inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
826 is_integral<_Tp>::value,
834 inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
844 inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
846 imag(const complex<_Tp>& __c)
851 inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
853 imag(long double __re)
858 inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
866 inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
869 is_integral<_Tp>::value,
877 inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
887 inline _LIBCPP_INLINE_VISIBILITY
889 abs(const complex<_Tp>& __c)
891 return hypot(__c.real(), __c.imag());
897 inline _LIBCPP_INLINE_VISIBILITY
899 arg(const complex<_Tp>& __c)
901 return atan2(__c.imag(), __c.real());
904 inline _LIBCPP_INLINE_VISIBILITY
906 arg(long double __re)
908 return atan2l(0.L, __re);
911 inline _LIBCPP_INLINE_VISIBILITY
915 return atan2(0., __re);
919 inline _LIBCPP_INLINE_VISIBILITY
922 is_integral<_Tp>::value,
927 return atan2(0., __re);
930 inline _LIBCPP_INLINE_VISIBILITY
934 return atan2f(0.F, __re);
940 inline _LIBCPP_INLINE_VISIBILITY
942 norm(const complex<_Tp>& __c)
944 if (isinf(__c.real()))
945 return abs(__c.real());
946 if (isinf(__c.imag()))
947 return abs(__c.imag());
948 return __c.real() * __c.real() + __c.imag() * __c.imag();
951 inline _LIBCPP_INLINE_VISIBILITY
953 norm(long double __re)
958 inline _LIBCPP_INLINE_VISIBILITY
966 inline _LIBCPP_INLINE_VISIBILITY
969 is_integral<_Tp>::value,
974 return (double)__re * __re;
977 inline _LIBCPP_INLINE_VISIBILITY
987 inline _LIBCPP_INLINE_VISIBILITY
989 conj(const complex<_Tp>& __c)
991 return complex<_Tp>(__c.real(), -__c.imag());
994 inline _LIBCPP_INLINE_VISIBILITY
996 conj(long double __re)
998 return complex<long double>(__re);
1001 inline _LIBCPP_INLINE_VISIBILITY
1005 return complex<double>(__re);
1009 inline _LIBCPP_INLINE_VISIBILITY
1012 is_integral<_Tp>::value,
1017 return complex<double>(__re);
1020 inline _LIBCPP_INLINE_VISIBILITY
1024 return complex<float>(__re);
1030 inline _LIBCPP_INLINE_VISIBILITY
1032 proj(const complex<_Tp>& __c)
1034 std::complex<_Tp> __r = __c;
1035 if (isinf(__c.real()) || isinf(__c.imag()))
1036 __r = complex<_Tp>(INFINITY, copysign(_Tp(0), __c.imag()));
1040 inline _LIBCPP_INLINE_VISIBILITY
1041 complex<long double>
1042 proj(long double __re)
1046 return complex<long double>(__re);
1049 inline _LIBCPP_INLINE_VISIBILITY
1055 return complex<double>(__re);
1059 inline _LIBCPP_INLINE_VISIBILITY
1062 is_integral<_Tp>::value,
1067 return complex<double>(__re);
1070 inline _LIBCPP_INLINE_VISIBILITY
1076 return complex<float>(__re);
1083 polar(const _Tp& __rho, const _Tp& __theta = _Tp(0))
1085 if (isnan(__rho) || signbit(__rho))
1086 return complex<_Tp>(_Tp(NAN), _Tp(NAN));
1090 return complex<_Tp>(__rho, __theta);
1091 return complex<_Tp>(__theta, __theta);
1096 return complex<_Tp>(__rho, _Tp(NAN));
1097 return complex<_Tp>(_Tp(NAN), _Tp(NAN));
1099 _Tp __x = __rho * cos(__theta);
1102 _Tp __y = __rho * sin(__theta);
1105 return complex<_Tp>(__x, __y);
1111 inline _LIBCPP_INLINE_VISIBILITY
1113 log(const complex<_Tp>& __x)
1115 return complex<_Tp>(log(abs(__x)), arg(__x));
1121 inline _LIBCPP_INLINE_VISIBILITY
1123 log10(const complex<_Tp>& __x)
1125 return log(__x) / log(_Tp(10));
1132 sqrt(const complex<_Tp>& __x)
1134 if (isinf(__x.imag()))
1135 return complex<_Tp>(_Tp(INFINITY), __x.imag());
1136 if (isinf(__x.real()))
1138 if (__x.real() > _Tp(0))
1139 return complex<_Tp>(__x.real(), isnan(__x.imag()) ? __x.imag() : copysign(_Tp(0), __x.imag()));
1140 return complex<_Tp>(isnan(__x.imag()) ? __x.imag() : _Tp(0), copysign(__x.real(), __x.imag()));
1142 return polar(sqrt(abs(__x)), arg(__x) / _Tp(2));
1149 exp(const complex<_Tp>& __x)
1151 _Tp __i = __x.imag();
1152 if (isinf(__x.real()))
1154 if (__x.real() < _Tp(0))
1159 else if (__i == 0 || !isfinite(__i))
1163 return complex<_Tp>(__x.real(), __i);
1166 else if (isnan(__x.real()) && __x.imag() == 0)
1168 _Tp __e = exp(__x.real());
1169 return complex<_Tp>(__e * cos(__i), __e * sin(__i));
1175 inline _LIBCPP_INLINE_VISIBILITY
1177 pow(const complex<_Tp>& __x, const complex<_Tp>& __y)
1179 return exp(__y * log(__x));
1182 template<class _Tp, class _Up>
1183 inline _LIBCPP_INLINE_VISIBILITY
1184 complex<typename __promote<_Tp, _Up>::type>
1185 pow(const complex<_Tp>& __x, const complex<_Up>& __y)
1187 typedef complex<typename __promote<_Tp, _Up>::type> result_type;
1188 return _VSTD::pow(result_type(__x), result_type(__y));
1191 template<class _Tp, class _Up>
1192 inline _LIBCPP_INLINE_VISIBILITY
1195 is_arithmetic<_Up>::value,
1196 complex<typename __promote<_Tp, _Up>::type>
1198 pow(const complex<_Tp>& __x, const _Up& __y)
1200 typedef complex<typename __promote<_Tp, _Up>::type> result_type;
1201 return _VSTD::pow(result_type(__x), result_type(__y));
1204 template<class _Tp, class _Up>
1205 inline _LIBCPP_INLINE_VISIBILITY
1208 is_arithmetic<_Tp>::value,
1209 complex<typename __promote<_Tp, _Up>::type>
1211 pow(const _Tp& __x, const complex<_Up>& __y)
1213 typedef complex<typename __promote<_Tp, _Up>::type> result_type;
1214 return _VSTD::pow(result_type(__x), result_type(__y));
1221 asinh(const complex<_Tp>& __x)
1223 const _Tp __pi(atan2(+0., -0.));
1224 if (isinf(__x.real()))
1226 if (isnan(__x.imag()))
1228 if (isinf(__x.imag()))
1229 return complex<_Tp>(__x.real(), copysign(__pi * _Tp(0.25), __x.imag()));
1230 return complex<_Tp>(__x.real(), copysign(_Tp(0), __x.imag()));
1232 if (isnan(__x.real()))
1234 if (isinf(__x.imag()))
1235 return complex<_Tp>(__x.imag(), __x.real());
1236 if (__x.imag() == 0)
1238 return complex<_Tp>(__x.real(), __x.real());
1240 if (isinf(__x.imag()))
1241 return complex<_Tp>(copysign(__x.imag(), __x.real()), copysign(__pi/_Tp(2), __x.imag()));
1242 complex<_Tp> __z = log(__x + sqrt(pow(__x, _Tp(2)) + _Tp(1)));
1243 return complex<_Tp>(copysign(__z.real(), __x.real()), copysign(__z.imag(), __x.imag()));
1250 acosh(const complex<_Tp>& __x)
1252 const _Tp __pi(atan2(+0., -0.));
1253 if (isinf(__x.real()))
1255 if (isnan(__x.imag()))
1256 return complex<_Tp>(abs(__x.real()), __x.imag());
1257 if (isinf(__x.imag()))
1260 return complex<_Tp>(__x.real(), copysign(__pi * _Tp(0.25), __x.imag()));
1262 return complex<_Tp>(-__x.real(), copysign(__pi * _Tp(0.75), __x.imag()));
1265 return complex<_Tp>(-__x.real(), copysign(__pi, __x.imag()));
1266 return complex<_Tp>(__x.real(), copysign(_Tp(0), __x.imag()));
1268 if (isnan(__x.real()))
1270 if (isinf(__x.imag()))
1271 return complex<_Tp>(abs(__x.imag()), __x.real());
1272 return complex<_Tp>(__x.real(), __x.real());
1274 if (isinf(__x.imag()))
1275 return complex<_Tp>(abs(__x.imag()), copysign(__pi/_Tp(2), __x.imag()));
1276 complex<_Tp> __z = log(__x + sqrt(pow(__x, _Tp(2)) - _Tp(1)));
1277 return complex<_Tp>(copysign(__z.real(), _Tp(0)), copysign(__z.imag(), __x.imag()));
1284 atanh(const complex<_Tp>& __x)
1286 const _Tp __pi(atan2(+0., -0.));
1287 if (isinf(__x.imag()))
1289 return complex<_Tp>(copysign(_Tp(0), __x.real()), copysign(__pi/_Tp(2), __x.imag()));
1291 if (isnan(__x.imag()))
1293 if (isinf(__x.real()) || __x.real() == 0)
1294 return complex<_Tp>(copysign(_Tp(0), __x.real()), __x.imag());
1295 return complex<_Tp>(__x.imag(), __x.imag());
1297 if (isnan(__x.real()))
1299 return complex<_Tp>(__x.real(), __x.real());
1301 if (isinf(__x.real()))
1303 return complex<_Tp>(copysign(_Tp(0), __x.real()), copysign(__pi/_Tp(2), __x.imag()));
1305 if (abs(__x.real()) == _Tp(1) && __x.imag() == _Tp(0))
1307 return complex<_Tp>(copysign(_Tp(INFINITY), __x.real()), copysign(_Tp(0), __x.imag()));
1309 complex<_Tp> __z = log((_Tp(1) + __x) / (_Tp(1) - __x)) / _Tp(2);
1310 return complex<_Tp>(copysign(__z.real(), __x.real()), copysign(__z.imag(), __x.imag()));
1317 sinh(const complex<_Tp>& __x)
1319 if (isinf(__x.real()) && !isfinite(__x.imag()))
1320 return complex<_Tp>(__x.real(), _Tp(NAN));
1321 if (__x.real() == 0 && !isfinite(__x.imag()))
1322 return complex<_Tp>(__x.real(), _Tp(NAN));
1323 if (__x.imag() == 0 && !isfinite(__x.real()))
1325 return complex<_Tp>(sinh(__x.real()) * cos(__x.imag()), cosh(__x.real()) * sin(__x.imag()));
1332 cosh(const complex<_Tp>& __x)
1334 if (isinf(__x.real()) && !isfinite(__x.imag()))
1335 return complex<_Tp>(abs(__x.real()), _Tp(NAN));
1336 if (__x.real() == 0 && !isfinite(__x.imag()))
1337 return complex<_Tp>(_Tp(NAN), __x.real());
1338 if (__x.real() == 0 && __x.imag() == 0)
1339 return complex<_Tp>(_Tp(1), __x.imag());
1340 if (__x.imag() == 0 && !isfinite(__x.real()))
1341 return complex<_Tp>(abs(__x.real()), __x.imag());
1342 return complex<_Tp>(cosh(__x.real()) * cos(__x.imag()), sinh(__x.real()) * sin(__x.imag()));
1349 tanh(const complex<_Tp>& __x)
1351 if (isinf(__x.real()))
1353 if (!isfinite(__x.imag()))
1354 return complex<_Tp>(_Tp(1), _Tp(0));
1355 return complex<_Tp>(_Tp(1), copysign(_Tp(0), sin(_Tp(2) * __x.imag())));
1357 if (isnan(__x.real()) && __x.imag() == 0)
1359 _Tp __2r(_Tp(2) * __x.real());
1360 _Tp __2i(_Tp(2) * __x.imag());
1361 _Tp __d(cosh(__2r) + cos(__2i));
1362 _Tp __2rsh(sinh(__2r));
1363 if (isinf(__2rsh) && isinf(__d))
1364 return complex<_Tp>(__2rsh > _Tp(0) ? _Tp(1) : _Tp(-1),
1365 __2i > _Tp(0) ? _Tp(0) : _Tp(-0.));
1366 return complex<_Tp>(__2rsh/__d, sin(__2i)/__d);
1373 asin(const complex<_Tp>& __x)
1375 complex<_Tp> __z = asinh(complex<_Tp>(-__x.imag(), __x.real()));
1376 return complex<_Tp>(__z.imag(), -__z.real());
1383 acos(const complex<_Tp>& __x)
1385 const _Tp __pi(atan2(+0., -0.));
1386 if (isinf(__x.real()))
1388 if (isnan(__x.imag()))
1389 return complex<_Tp>(__x.imag(), __x.real());
1390 if (isinf(__x.imag()))
1392 if (__x.real() < _Tp(0))
1393 return complex<_Tp>(_Tp(0.75) * __pi, -__x.imag());
1394 return complex<_Tp>(_Tp(0.25) * __pi, -__x.imag());
1396 if (__x.real() < _Tp(0))
1397 return complex<_Tp>(__pi, signbit(__x.imag()) ? -__x.real() : __x.real());
1398 return complex<_Tp>(_Tp(0), signbit(__x.imag()) ? __x.real() : -__x.real());
1400 if (isnan(__x.real()))
1402 if (isinf(__x.imag()))
1403 return complex<_Tp>(__x.real(), -__x.imag());
1404 return complex<_Tp>(__x.real(), __x.real());
1406 if (isinf(__x.imag()))
1407 return complex<_Tp>(__pi/_Tp(2), -__x.imag());
1408 if (__x.real() == 0 && (__x.imag() == 0 || isnan(__x.imag())))
1409 return complex<_Tp>(__pi/_Tp(2), -__x.imag());
1410 complex<_Tp> __z = log(__x + sqrt(pow(__x, _Tp(2)) - _Tp(1)));
1411 if (signbit(__x.imag()))
1412 return complex<_Tp>(abs(__z.imag()), abs(__z.real()));
1413 return complex<_Tp>(abs(__z.imag()), -abs(__z.real()));
1420 atan(const complex<_Tp>& __x)
1422 complex<_Tp> __z = atanh(complex<_Tp>(-__x.imag(), __x.real()));
1423 return complex<_Tp>(__z.imag(), -__z.real());
1430 sin(const complex<_Tp>& __x)
1432 complex<_Tp> __z = sinh(complex<_Tp>(-__x.imag(), __x.real()));
1433 return complex<_Tp>(__z.imag(), -__z.real());
1439 inline _LIBCPP_INLINE_VISIBILITY
1441 cos(const complex<_Tp>& __x)
1443 return cosh(complex<_Tp>(-__x.imag(), __x.real()));
1450 tan(const complex<_Tp>& __x)
1452 complex<_Tp> __z = tanh(complex<_Tp>(-__x.imag(), __x.real()));
1453 return complex<_Tp>(__z.imag(), -__z.real());
1456 template<class _Tp, class _CharT, class _Traits>
1457 basic_istream<_CharT, _Traits>&
1458 operator>>(basic_istream<_CharT, _Traits>& __is, complex<_Tp>& __x)
1463 if (__is.peek() == _CharT('('))
1471 _CharT __c = __is.peek();
1472 if (__c == _CharT(','))
1481 if (__c == _CharT(')'))
1484 __x = complex<_Tp>(__r, __i);
1487 __is.setstate(ios_base::failbit);
1490 __is.setstate(ios_base::failbit);
1492 else if (__c == _CharT(')'))
1495 __x = complex<_Tp>(__r, _Tp(0));
1498 __is.setstate(ios_base::failbit);
1501 __is.setstate(ios_base::failbit);
1508 __x = complex<_Tp>(__r, _Tp(0));
1510 __is.setstate(ios_base::failbit);
1514 __is.setstate(ios_base::failbit);
1518 template<class _Tp, class _CharT, class _Traits>
1519 basic_ostream<_CharT, _Traits>&
1520 operator<<(basic_ostream<_CharT, _Traits>& __os, const complex<_Tp>& __x)
1522 basic_ostringstream<_CharT, _Traits> __s;
1523 __s.flags(__os.flags());
1524 __s.imbue(__os.getloc());
1525 __s.precision(__os.precision());
1526 __s << '(' << __x.real() << ',' << __x.imag() << ')';
1527 return __os << __s.str();
1530 #if _LIBCPP_STD_VER > 11
1531 // Literal suffix for complex number literals [complex.literals]
1532 inline namespace literals
1534 inline namespace complex_literals
1536 constexpr complex<long double> operator""il(long double __im)
1538 return { 0.0l, __im };
1541 constexpr complex<long double> operator""il(unsigned long long __im)
1543 return { 0.0l, static_cast<long double>(__im) };
1547 constexpr complex<double> operator""i(long double __im)
1549 return { 0.0, static_cast<double>(__im) };
1552 constexpr complex<double> operator""i(unsigned long long __im)
1554 return { 0.0, static_cast<double>(__im) };
1558 constexpr complex<float> operator""if(long double __im)
1560 return { 0.0f, static_cast<float>(__im) };
1563 constexpr complex<float> operator""if(unsigned long long __im)
1565 return { 0.0f, static_cast<float>(__im) };
1571 _LIBCPP_END_NAMESPACE_STD
1573 #endif // _LIBCPP_COMPLEX