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 explicit _LIBCPP_CONSTEXPR complex(const complex<double>& __c);
336 explicit _LIBCPP_CONSTEXPR complex(const complex<long double>& __c);
338 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR float real() const {return __re_;}
339 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR float imag() const {return __im_;}
341 _LIBCPP_INLINE_VISIBILITY void real(value_type __re) {__re_ = __re;}
342 _LIBCPP_INLINE_VISIBILITY void imag(value_type __im) {__im_ = __im;}
344 _LIBCPP_INLINE_VISIBILITY complex& operator= (float __re)
345 {__re_ = __re; __im_ = value_type(); return *this;}
346 _LIBCPP_INLINE_VISIBILITY complex& operator+=(float __re) {__re_ += __re; return *this;}
347 _LIBCPP_INLINE_VISIBILITY complex& operator-=(float __re) {__re_ -= __re; return *this;}
348 _LIBCPP_INLINE_VISIBILITY complex& operator*=(float __re) {__re_ *= __re; __im_ *= __re; return *this;}
349 _LIBCPP_INLINE_VISIBILITY complex& operator/=(float __re) {__re_ /= __re; __im_ /= __re; return *this;}
351 template<class _Xp> _LIBCPP_INLINE_VISIBILITY complex& operator= (const complex<_Xp>& __c)
357 template<class _Xp> _LIBCPP_INLINE_VISIBILITY complex& operator+=(const complex<_Xp>& __c)
363 template<class _Xp> _LIBCPP_INLINE_VISIBILITY complex& operator-=(const complex<_Xp>& __c)
369 template<class _Xp> _LIBCPP_INLINE_VISIBILITY complex& operator*=(const complex<_Xp>& __c)
371 *this = *this * complex(__c.real(), __c.imag());
374 template<class _Xp> _LIBCPP_INLINE_VISIBILITY complex& operator/=(const complex<_Xp>& __c)
376 *this = *this / complex(__c.real(), __c.imag());
382 class _LIBCPP_TYPE_VIS_ONLY complex<double>
387 typedef double value_type;
389 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR complex(double __re = 0.0, double __im = 0.0)
390 : __re_(__re), __im_(__im) {}
391 _LIBCPP_CONSTEXPR complex(const complex<float>& __c);
392 explicit _LIBCPP_CONSTEXPR complex(const complex<long double>& __c);
394 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR double real() const {return __re_;}
395 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR double imag() const {return __im_;}
397 _LIBCPP_INLINE_VISIBILITY void real(value_type __re) {__re_ = __re;}
398 _LIBCPP_INLINE_VISIBILITY void imag(value_type __im) {__im_ = __im;}
400 _LIBCPP_INLINE_VISIBILITY complex& operator= (double __re)
401 {__re_ = __re; __im_ = value_type(); return *this;}
402 _LIBCPP_INLINE_VISIBILITY complex& operator+=(double __re) {__re_ += __re; return *this;}
403 _LIBCPP_INLINE_VISIBILITY complex& operator-=(double __re) {__re_ -= __re; return *this;}
404 _LIBCPP_INLINE_VISIBILITY complex& operator*=(double __re) {__re_ *= __re; __im_ *= __re; return *this;}
405 _LIBCPP_INLINE_VISIBILITY complex& operator/=(double __re) {__re_ /= __re; __im_ /= __re; return *this;}
407 template<class _Xp> _LIBCPP_INLINE_VISIBILITY complex& operator= (const complex<_Xp>& __c)
413 template<class _Xp> _LIBCPP_INLINE_VISIBILITY complex& operator+=(const complex<_Xp>& __c)
419 template<class _Xp> _LIBCPP_INLINE_VISIBILITY complex& operator-=(const complex<_Xp>& __c)
425 template<class _Xp> _LIBCPP_INLINE_VISIBILITY complex& operator*=(const complex<_Xp>& __c)
427 *this = *this * complex(__c.real(), __c.imag());
430 template<class _Xp> _LIBCPP_INLINE_VISIBILITY complex& operator/=(const complex<_Xp>& __c)
432 *this = *this / complex(__c.real(), __c.imag());
438 class _LIBCPP_TYPE_VIS_ONLY complex<long double>
443 typedef long double value_type;
445 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR complex(long double __re = 0.0L, long double __im = 0.0L)
446 : __re_(__re), __im_(__im) {}
447 _LIBCPP_CONSTEXPR complex(const complex<float>& __c);
448 _LIBCPP_CONSTEXPR complex(const complex<double>& __c);
450 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR long double real() const {return __re_;}
451 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR long double imag() const {return __im_;}
453 _LIBCPP_INLINE_VISIBILITY void real(value_type __re) {__re_ = __re;}
454 _LIBCPP_INLINE_VISIBILITY void imag(value_type __im) {__im_ = __im;}
456 _LIBCPP_INLINE_VISIBILITY complex& operator= (long double __re)
457 {__re_ = __re; __im_ = value_type(); return *this;}
458 _LIBCPP_INLINE_VISIBILITY complex& operator+=(long double __re) {__re_ += __re; return *this;}
459 _LIBCPP_INLINE_VISIBILITY complex& operator-=(long double __re) {__re_ -= __re; return *this;}
460 _LIBCPP_INLINE_VISIBILITY complex& operator*=(long double __re) {__re_ *= __re; __im_ *= __re; return *this;}
461 _LIBCPP_INLINE_VISIBILITY complex& operator/=(long double __re) {__re_ /= __re; __im_ /= __re; return *this;}
463 template<class _Xp> _LIBCPP_INLINE_VISIBILITY complex& operator= (const complex<_Xp>& __c)
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)
483 *this = *this * complex(__c.real(), __c.imag());
486 template<class _Xp> _LIBCPP_INLINE_VISIBILITY complex& operator/=(const complex<_Xp>& __c)
488 *this = *this / complex(__c.real(), __c.imag());
493 inline _LIBCPP_INLINE_VISIBILITY
495 complex<float>::complex(const complex<double>& __c)
496 : __re_(__c.real()), __im_(__c.imag()) {}
498 inline _LIBCPP_INLINE_VISIBILITY
500 complex<float>::complex(const complex<long double>& __c)
501 : __re_(__c.real()), __im_(__c.imag()) {}
503 inline _LIBCPP_INLINE_VISIBILITY
505 complex<double>::complex(const complex<float>& __c)
506 : __re_(__c.real()), __im_(__c.imag()) {}
508 inline _LIBCPP_INLINE_VISIBILITY
510 complex<double>::complex(const complex<long double>& __c)
511 : __re_(__c.real()), __im_(__c.imag()) {}
513 inline _LIBCPP_INLINE_VISIBILITY
515 complex<long double>::complex(const complex<float>& __c)
516 : __re_(__c.real()), __im_(__c.imag()) {}
518 inline _LIBCPP_INLINE_VISIBILITY
520 complex<long double>::complex(const complex<double>& __c)
521 : __re_(__c.real()), __im_(__c.imag()) {}
526 inline _LIBCPP_INLINE_VISIBILITY
528 operator+(const complex<_Tp>& __x, const complex<_Tp>& __y)
530 complex<_Tp> __t(__x);
536 inline _LIBCPP_INLINE_VISIBILITY
538 operator+(const complex<_Tp>& __x, const _Tp& __y)
540 complex<_Tp> __t(__x);
546 inline _LIBCPP_INLINE_VISIBILITY
548 operator+(const _Tp& __x, const complex<_Tp>& __y)
550 complex<_Tp> __t(__y);
556 inline _LIBCPP_INLINE_VISIBILITY
558 operator-(const complex<_Tp>& __x, const complex<_Tp>& __y)
560 complex<_Tp> __t(__x);
566 inline _LIBCPP_INLINE_VISIBILITY
568 operator-(const complex<_Tp>& __x, const _Tp& __y)
570 complex<_Tp> __t(__x);
576 inline _LIBCPP_INLINE_VISIBILITY
578 operator-(const _Tp& __x, const complex<_Tp>& __y)
580 complex<_Tp> __t(-__y);
587 operator*(const complex<_Tp>& __z, const complex<_Tp>& __w)
589 _Tp __a = __z.real();
590 _Tp __b = __z.imag();
591 _Tp __c = __w.real();
592 _Tp __d = __w.imag();
593 _Tp __ac = __a * __c;
594 _Tp __bd = __b * __d;
595 _Tp __ad = __a * __d;
596 _Tp __bc = __b * __c;
597 _Tp __x = __ac - __bd;
598 _Tp __y = __ad + __bc;
599 if (isnan(__x) && isnan(__y))
601 bool __recalc = false;
602 if (isinf(__a) || isinf(__b))
604 __a = copysign(isinf(__a) ? _Tp(1) : _Tp(0), __a);
605 __b = copysign(isinf(__b) ? _Tp(1) : _Tp(0), __b);
607 __c = copysign(_Tp(0), __c);
609 __d = copysign(_Tp(0), __d);
612 if (isinf(__c) || isinf(__d))
614 __c = copysign(isinf(__c) ? _Tp(1) : _Tp(0), __c);
615 __d = copysign(isinf(__d) ? _Tp(1) : _Tp(0), __d);
617 __a = copysign(_Tp(0), __a);
619 __b = copysign(_Tp(0), __b);
622 if (!__recalc && (isinf(__ac) || isinf(__bd) ||
623 isinf(__ad) || isinf(__bc)))
626 __a = copysign(_Tp(0), __a);
628 __b = copysign(_Tp(0), __b);
630 __c = copysign(_Tp(0), __c);
632 __d = copysign(_Tp(0), __d);
637 __x = _Tp(INFINITY) * (__a * __c - __b * __d);
638 __y = _Tp(INFINITY) * (__a * __d + __b * __c);
641 return complex<_Tp>(__x, __y);
645 inline _LIBCPP_INLINE_VISIBILITY
647 operator*(const complex<_Tp>& __x, const _Tp& __y)
649 complex<_Tp> __t(__x);
655 inline _LIBCPP_INLINE_VISIBILITY
657 operator*(const _Tp& __x, const complex<_Tp>& __y)
659 complex<_Tp> __t(__y);
666 operator/(const complex<_Tp>& __z, const complex<_Tp>& __w)
669 _Tp __a = __z.real();
670 _Tp __b = __z.imag();
671 _Tp __c = __w.real();
672 _Tp __d = __w.imag();
673 _Tp __logbw = logb(fmax(fabs(__c), fabs(__d)));
674 if (isfinite(__logbw))
676 __ilogbw = static_cast<int>(__logbw);
677 __c = scalbn(__c, -__ilogbw);
678 __d = scalbn(__d, -__ilogbw);
680 _Tp __denom = __c * __c + __d * __d;
681 _Tp __x = scalbn((__a * __c + __b * __d) / __denom, -__ilogbw);
682 _Tp __y = scalbn((__b * __c - __a * __d) / __denom, -__ilogbw);
683 if (isnan(__x) && isnan(__y))
685 if ((__denom == _Tp(0)) && (!isnan(__a) || !isnan(__b)))
687 __x = copysign(_Tp(INFINITY), __c) * __a;
688 __y = copysign(_Tp(INFINITY), __c) * __b;
690 else if ((isinf(__a) || isinf(__b)) && isfinite(__c) && isfinite(__d))
692 __a = copysign(isinf(__a) ? _Tp(1) : _Tp(0), __a);
693 __b = copysign(isinf(__b) ? _Tp(1) : _Tp(0), __b);
694 __x = _Tp(INFINITY) * (__a * __c + __b * __d);
695 __y = _Tp(INFINITY) * (__b * __c - __a * __d);
697 else if (isinf(__logbw) && __logbw > _Tp(0) && isfinite(__a) && isfinite(__b))
699 __c = copysign(isinf(__c) ? _Tp(1) : _Tp(0), __c);
700 __d = copysign(isinf(__d) ? _Tp(1) : _Tp(0), __d);
701 __x = _Tp(0) * (__a * __c + __b * __d);
702 __y = _Tp(0) * (__b * __c - __a * __d);
705 return complex<_Tp>(__x, __y);
709 inline _LIBCPP_INLINE_VISIBILITY
711 operator/(const complex<_Tp>& __x, const _Tp& __y)
713 return complex<_Tp>(__x.real() / __y, __x.imag() / __y);
717 inline _LIBCPP_INLINE_VISIBILITY
719 operator/(const _Tp& __x, const complex<_Tp>& __y)
721 complex<_Tp> __t(__x);
727 inline _LIBCPP_INLINE_VISIBILITY
729 operator+(const complex<_Tp>& __x)
735 inline _LIBCPP_INLINE_VISIBILITY
737 operator-(const complex<_Tp>& __x)
739 return complex<_Tp>(-__x.real(), -__x.imag());
743 inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
745 operator==(const complex<_Tp>& __x, const complex<_Tp>& __y)
747 return __x.real() == __y.real() && __x.imag() == __y.imag();
751 inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
753 operator==(const complex<_Tp>& __x, const _Tp& __y)
755 return __x.real() == __y && __x.imag() == 0;
759 inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
761 operator==(const _Tp& __x, const complex<_Tp>& __y)
763 return __x == __y.real() && 0 == __y.imag();
767 inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
769 operator!=(const complex<_Tp>& __x, const complex<_Tp>& __y)
771 return !(__x == __y);
775 inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
777 operator!=(const complex<_Tp>& __x, const _Tp& __y)
779 return !(__x == __y);
783 inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
785 operator!=(const _Tp& __x, const complex<_Tp>& __y)
787 return !(__x == __y);
795 inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
797 real(const complex<_Tp>& __c)
802 inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
804 real(long double __re)
809 inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
817 inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
820 is_integral<_Tp>::value,
828 inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
838 inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
840 imag(const complex<_Tp>& __c)
845 inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
847 imag(long double __re)
852 inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
860 inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
863 is_integral<_Tp>::value,
871 inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
881 inline _LIBCPP_INLINE_VISIBILITY
883 abs(const complex<_Tp>& __c)
885 return hypot(__c.real(), __c.imag());
891 inline _LIBCPP_INLINE_VISIBILITY
893 arg(const complex<_Tp>& __c)
895 return atan2(__c.imag(), __c.real());
898 inline _LIBCPP_INLINE_VISIBILITY
900 arg(long double __re)
902 return atan2l(0.L, __re);
905 inline _LIBCPP_INLINE_VISIBILITY
909 return atan2(0., __re);
913 inline _LIBCPP_INLINE_VISIBILITY
916 is_integral<_Tp>::value,
921 return atan2(0., __re);
924 inline _LIBCPP_INLINE_VISIBILITY
928 return atan2f(0.F, __re);
934 inline _LIBCPP_INLINE_VISIBILITY
936 norm(const complex<_Tp>& __c)
938 if (isinf(__c.real()))
939 return abs(__c.real());
940 if (isinf(__c.imag()))
941 return abs(__c.imag());
942 return __c.real() * __c.real() + __c.imag() * __c.imag();
945 inline _LIBCPP_INLINE_VISIBILITY
947 norm(long double __re)
952 inline _LIBCPP_INLINE_VISIBILITY
960 inline _LIBCPP_INLINE_VISIBILITY
963 is_integral<_Tp>::value,
968 return (double)__re * __re;
971 inline _LIBCPP_INLINE_VISIBILITY
981 inline _LIBCPP_INLINE_VISIBILITY
983 conj(const complex<_Tp>& __c)
985 return complex<_Tp>(__c.real(), -__c.imag());
988 inline _LIBCPP_INLINE_VISIBILITY
990 conj(long double __re)
992 return complex<long double>(__re);
995 inline _LIBCPP_INLINE_VISIBILITY
999 return complex<double>(__re);
1003 inline _LIBCPP_INLINE_VISIBILITY
1006 is_integral<_Tp>::value,
1011 return complex<double>(__re);
1014 inline _LIBCPP_INLINE_VISIBILITY
1018 return complex<float>(__re);
1024 inline _LIBCPP_INLINE_VISIBILITY
1026 proj(const complex<_Tp>& __c)
1028 std::complex<_Tp> __r = __c;
1029 if (isinf(__c.real()) || isinf(__c.imag()))
1030 __r = complex<_Tp>(INFINITY, copysign(_Tp(0), __c.imag()));
1034 inline _LIBCPP_INLINE_VISIBILITY
1035 complex<long double>
1036 proj(long double __re)
1040 return complex<long double>(__re);
1043 inline _LIBCPP_INLINE_VISIBILITY
1049 return complex<double>(__re);
1053 inline _LIBCPP_INLINE_VISIBILITY
1056 is_integral<_Tp>::value,
1061 return complex<double>(__re);
1064 inline _LIBCPP_INLINE_VISIBILITY
1070 return complex<float>(__re);
1077 polar(const _Tp& __rho, const _Tp& __theta = _Tp(0))
1079 if (isnan(__rho) || signbit(__rho))
1080 return complex<_Tp>(_Tp(NAN), _Tp(NAN));
1084 return complex<_Tp>(__rho, __theta);
1085 return complex<_Tp>(__theta, __theta);
1090 return complex<_Tp>(__rho, _Tp(NAN));
1091 return complex<_Tp>(_Tp(NAN), _Tp(NAN));
1093 _Tp __x = __rho * cos(__theta);
1096 _Tp __y = __rho * sin(__theta);
1099 return complex<_Tp>(__x, __y);
1105 inline _LIBCPP_INLINE_VISIBILITY
1107 log(const complex<_Tp>& __x)
1109 return complex<_Tp>(log(abs(__x)), arg(__x));
1115 inline _LIBCPP_INLINE_VISIBILITY
1117 log10(const complex<_Tp>& __x)
1119 return log(__x) / log(_Tp(10));
1126 sqrt(const complex<_Tp>& __x)
1128 if (isinf(__x.imag()))
1129 return complex<_Tp>(_Tp(INFINITY), __x.imag());
1130 if (isinf(__x.real()))
1132 if (__x.real() > _Tp(0))
1133 return complex<_Tp>(__x.real(), isnan(__x.imag()) ? __x.imag() : copysign(_Tp(0), __x.imag()));
1134 return complex<_Tp>(isnan(__x.imag()) ? __x.imag() : _Tp(0), copysign(__x.real(), __x.imag()));
1136 return polar(sqrt(abs(__x)), arg(__x) / _Tp(2));
1143 exp(const complex<_Tp>& __x)
1145 _Tp __i = __x.imag();
1146 if (isinf(__x.real()))
1148 if (__x.real() < _Tp(0))
1153 else if (__i == 0 || !isfinite(__i))
1157 return complex<_Tp>(__x.real(), __i);
1160 else if (isnan(__x.real()) && __x.imag() == 0)
1162 _Tp __e = exp(__x.real());
1163 return complex<_Tp>(__e * cos(__i), __e * sin(__i));
1169 inline _LIBCPP_INLINE_VISIBILITY
1171 pow(const complex<_Tp>& __x, const complex<_Tp>& __y)
1173 return exp(__y * log(__x));
1176 template<class _Tp, class _Up>
1177 inline _LIBCPP_INLINE_VISIBILITY
1178 complex<typename __promote<_Tp, _Up>::type>
1179 pow(const complex<_Tp>& __x, const complex<_Up>& __y)
1181 typedef complex<typename __promote<_Tp, _Up>::type> result_type;
1182 return _VSTD::pow(result_type(__x), result_type(__y));
1185 template<class _Tp, class _Up>
1186 inline _LIBCPP_INLINE_VISIBILITY
1189 is_arithmetic<_Up>::value,
1190 complex<typename __promote<_Tp, _Up>::type>
1192 pow(const complex<_Tp>& __x, const _Up& __y)
1194 typedef complex<typename __promote<_Tp, _Up>::type> result_type;
1195 return _VSTD::pow(result_type(__x), result_type(__y));
1198 template<class _Tp, class _Up>
1199 inline _LIBCPP_INLINE_VISIBILITY
1202 is_arithmetic<_Tp>::value,
1203 complex<typename __promote<_Tp, _Up>::type>
1205 pow(const _Tp& __x, const complex<_Up>& __y)
1207 typedef complex<typename __promote<_Tp, _Up>::type> result_type;
1208 return _VSTD::pow(result_type(__x), result_type(__y));
1215 asinh(const complex<_Tp>& __x)
1217 const _Tp __pi(atan2(+0., -0.));
1218 if (isinf(__x.real()))
1220 if (isnan(__x.imag()))
1222 if (isinf(__x.imag()))
1223 return complex<_Tp>(__x.real(), copysign(__pi * _Tp(0.25), __x.imag()));
1224 return complex<_Tp>(__x.real(), copysign(_Tp(0), __x.imag()));
1226 if (isnan(__x.real()))
1228 if (isinf(__x.imag()))
1229 return complex<_Tp>(__x.imag(), __x.real());
1230 if (__x.imag() == 0)
1232 return complex<_Tp>(__x.real(), __x.real());
1234 if (isinf(__x.imag()))
1235 return complex<_Tp>(copysign(__x.imag(), __x.real()), copysign(__pi/_Tp(2), __x.imag()));
1236 complex<_Tp> __z = log(__x + sqrt(pow(__x, _Tp(2)) + _Tp(1)));
1237 return complex<_Tp>(copysign(__z.real(), __x.real()), copysign(__z.imag(), __x.imag()));
1244 acosh(const complex<_Tp>& __x)
1246 const _Tp __pi(atan2(+0., -0.));
1247 if (isinf(__x.real()))
1249 if (isnan(__x.imag()))
1250 return complex<_Tp>(abs(__x.real()), __x.imag());
1251 if (isinf(__x.imag()))
1254 return complex<_Tp>(__x.real(), copysign(__pi * _Tp(0.25), __x.imag()));
1256 return complex<_Tp>(-__x.real(), copysign(__pi * _Tp(0.75), __x.imag()));
1259 return complex<_Tp>(-__x.real(), copysign(__pi, __x.imag()));
1260 return complex<_Tp>(__x.real(), copysign(_Tp(0), __x.imag()));
1262 if (isnan(__x.real()))
1264 if (isinf(__x.imag()))
1265 return complex<_Tp>(abs(__x.imag()), __x.real());
1266 return complex<_Tp>(__x.real(), __x.real());
1268 if (isinf(__x.imag()))
1269 return complex<_Tp>(abs(__x.imag()), copysign(__pi/_Tp(2), __x.imag()));
1270 complex<_Tp> __z = log(__x + sqrt(pow(__x, _Tp(2)) - _Tp(1)));
1271 return complex<_Tp>(copysign(__z.real(), _Tp(0)), copysign(__z.imag(), __x.imag()));
1278 atanh(const complex<_Tp>& __x)
1280 const _Tp __pi(atan2(+0., -0.));
1281 if (isinf(__x.imag()))
1283 return complex<_Tp>(copysign(_Tp(0), __x.real()), copysign(__pi/_Tp(2), __x.imag()));
1285 if (isnan(__x.imag()))
1287 if (isinf(__x.real()) || __x.real() == 0)
1288 return complex<_Tp>(copysign(_Tp(0), __x.real()), __x.imag());
1289 return complex<_Tp>(__x.imag(), __x.imag());
1291 if (isnan(__x.real()))
1293 return complex<_Tp>(__x.real(), __x.real());
1295 if (isinf(__x.real()))
1297 return complex<_Tp>(copysign(_Tp(0), __x.real()), copysign(__pi/_Tp(2), __x.imag()));
1299 if (abs(__x.real()) == _Tp(1) && __x.imag() == _Tp(0))
1301 return complex<_Tp>(copysign(_Tp(INFINITY), __x.real()), copysign(_Tp(0), __x.imag()));
1303 complex<_Tp> __z = log((_Tp(1) + __x) / (_Tp(1) - __x)) / _Tp(2);
1304 return complex<_Tp>(copysign(__z.real(), __x.real()), copysign(__z.imag(), __x.imag()));
1311 sinh(const complex<_Tp>& __x)
1313 if (isinf(__x.real()) && !isfinite(__x.imag()))
1314 return complex<_Tp>(__x.real(), _Tp(NAN));
1315 if (__x.real() == 0 && !isfinite(__x.imag()))
1316 return complex<_Tp>(__x.real(), _Tp(NAN));
1317 if (__x.imag() == 0 && !isfinite(__x.real()))
1319 return complex<_Tp>(sinh(__x.real()) * cos(__x.imag()), cosh(__x.real()) * sin(__x.imag()));
1326 cosh(const complex<_Tp>& __x)
1328 if (isinf(__x.real()) && !isfinite(__x.imag()))
1329 return complex<_Tp>(abs(__x.real()), _Tp(NAN));
1330 if (__x.real() == 0 && !isfinite(__x.imag()))
1331 return complex<_Tp>(_Tp(NAN), __x.real());
1332 if (__x.real() == 0 && __x.imag() == 0)
1333 return complex<_Tp>(_Tp(1), __x.imag());
1334 if (__x.imag() == 0 && !isfinite(__x.real()))
1335 return complex<_Tp>(abs(__x.real()), __x.imag());
1336 return complex<_Tp>(cosh(__x.real()) * cos(__x.imag()), sinh(__x.real()) * sin(__x.imag()));
1343 tanh(const complex<_Tp>& __x)
1345 if (isinf(__x.real()))
1347 if (!isfinite(__x.imag()))
1348 return complex<_Tp>(_Tp(1), _Tp(0));
1349 return complex<_Tp>(_Tp(1), copysign(_Tp(0), sin(_Tp(2) * __x.imag())));
1351 if (isnan(__x.real()) && __x.imag() == 0)
1353 _Tp __2r(_Tp(2) * __x.real());
1354 _Tp __2i(_Tp(2) * __x.imag());
1355 _Tp __d(cosh(__2r) + cos(__2i));
1356 _Tp __2rsh(sinh(__2r));
1357 if (isinf(__2rsh) && isinf(__d))
1358 return complex<_Tp>(__2rsh > _Tp(0) ? _Tp(1) : _Tp(-1),
1359 __2i > _Tp(0) ? _Tp(0) : _Tp(-0.));
1360 return complex<_Tp>(__2rsh/__d, sin(__2i)/__d);
1367 asin(const complex<_Tp>& __x)
1369 complex<_Tp> __z = asinh(complex<_Tp>(-__x.imag(), __x.real()));
1370 return complex<_Tp>(__z.imag(), -__z.real());
1377 acos(const complex<_Tp>& __x)
1379 const _Tp __pi(atan2(+0., -0.));
1380 if (isinf(__x.real()))
1382 if (isnan(__x.imag()))
1383 return complex<_Tp>(__x.imag(), __x.real());
1384 if (isinf(__x.imag()))
1386 if (__x.real() < _Tp(0))
1387 return complex<_Tp>(_Tp(0.75) * __pi, -__x.imag());
1388 return complex<_Tp>(_Tp(0.25) * __pi, -__x.imag());
1390 if (__x.real() < _Tp(0))
1391 return complex<_Tp>(__pi, signbit(__x.imag()) ? -__x.real() : __x.real());
1392 return complex<_Tp>(_Tp(0), signbit(__x.imag()) ? __x.real() : -__x.real());
1394 if (isnan(__x.real()))
1396 if (isinf(__x.imag()))
1397 return complex<_Tp>(__x.real(), -__x.imag());
1398 return complex<_Tp>(__x.real(), __x.real());
1400 if (isinf(__x.imag()))
1401 return complex<_Tp>(__pi/_Tp(2), -__x.imag());
1402 if (__x.real() == 0)
1403 return complex<_Tp>(__pi/_Tp(2), -__x.imag());
1404 complex<_Tp> __z = log(__x + sqrt(pow(__x, _Tp(2)) - _Tp(1)));
1405 if (signbit(__x.imag()))
1406 return complex<_Tp>(abs(__z.imag()), abs(__z.real()));
1407 return complex<_Tp>(abs(__z.imag()), -abs(__z.real()));
1414 atan(const complex<_Tp>& __x)
1416 complex<_Tp> __z = atanh(complex<_Tp>(-__x.imag(), __x.real()));
1417 return complex<_Tp>(__z.imag(), -__z.real());
1424 sin(const complex<_Tp>& __x)
1426 complex<_Tp> __z = sinh(complex<_Tp>(-__x.imag(), __x.real()));
1427 return complex<_Tp>(__z.imag(), -__z.real());
1433 inline _LIBCPP_INLINE_VISIBILITY
1435 cos(const complex<_Tp>& __x)
1437 return cosh(complex<_Tp>(-__x.imag(), __x.real()));
1444 tan(const complex<_Tp>& __x)
1446 complex<_Tp> __z = tanh(complex<_Tp>(-__x.imag(), __x.real()));
1447 return complex<_Tp>(__z.imag(), -__z.real());
1450 template<class _Tp, class _CharT, class _Traits>
1451 basic_istream<_CharT, _Traits>&
1452 operator>>(basic_istream<_CharT, _Traits>& __is, complex<_Tp>& __x)
1457 if (__is.peek() == _CharT('('))
1465 _CharT __c = __is.peek();
1466 if (__c == _CharT(','))
1475 if (__c == _CharT(')'))
1478 __x = complex<_Tp>(__r, __i);
1481 __is.setstate(ios_base::failbit);
1484 __is.setstate(ios_base::failbit);
1486 else if (__c == _CharT(')'))
1489 __x = complex<_Tp>(__r, _Tp(0));
1492 __is.setstate(ios_base::failbit);
1495 __is.setstate(ios_base::failbit);
1502 __x = complex<_Tp>(__r, _Tp(0));
1504 __is.setstate(ios_base::failbit);
1508 __is.setstate(ios_base::failbit);
1512 template<class _Tp, class _CharT, class _Traits>
1513 basic_ostream<_CharT, _Traits>&
1514 operator<<(basic_ostream<_CharT, _Traits>& __os, const complex<_Tp>& __x)
1516 basic_ostringstream<_CharT, _Traits> __s;
1517 __s.flags(__os.flags());
1518 __s.imbue(__os.getloc());
1519 __s.precision(__os.precision());
1520 __s << '(' << __x.real() << ',' << __x.imag() << ')';
1521 return __os << __s.str();
1524 #if _LIBCPP_STD_VER > 11
1525 // Literal suffix for complex number literals [complex.literals]
1526 inline namespace literals
1528 inline namespace complex_literals
1530 constexpr complex<long double> operator""il(long double __im)
1532 return { 0.0l, __im };
1535 constexpr complex<long double> operator""il(unsigned long long __im)
1537 return { 0.0l, static_cast<long double>(__im) };
1541 constexpr complex<double> operator""i(long double __im)
1543 return { 0.0, static_cast<double>(__im) };
1546 constexpr complex<double> operator""i(unsigned long long __im)
1548 return { 0.0, static_cast<double>(__im) };
1552 constexpr complex<float> operator""if(long double __im)
1554 return { 0.0f, static_cast<float>(__im) };
1557 constexpr complex<float> operator""if(unsigned long long __im)
1559 return { 0.0f, static_cast<float>(__im) };
1565 _LIBCPP_END_NAMESPACE_STD
1567 #endif // _LIBCPP_COMPLEX