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& = T());
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>
250 #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
251 #pragma GCC system_header
254 _LIBCPP_BEGIN_NAMESPACE_STD
256 template<class _Tp> class _LIBCPP_TEMPLATE_VIS complex;
258 template<class _Tp> complex<_Tp> operator*(const complex<_Tp>& __z, const complex<_Tp>& __w);
259 template<class _Tp> complex<_Tp> operator/(const complex<_Tp>& __x, const complex<_Tp>& __y);
262 class _LIBCPP_TEMPLATE_VIS complex
265 typedef _Tp value_type;
270 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
271 complex(const value_type& __re = value_type(), const value_type& __im = value_type())
272 : __re_(__re), __im_(__im) {}
273 template<class _Xp> _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
274 complex(const complex<_Xp>& __c)
275 : __re_(__c.real()), __im_(__c.imag()) {}
277 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11 value_type real() const {return __re_;}
278 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11 value_type imag() const {return __im_;}
280 _LIBCPP_INLINE_VISIBILITY void real(value_type __re) {__re_ = __re;}
281 _LIBCPP_INLINE_VISIBILITY void imag(value_type __im) {__im_ = __im;}
283 _LIBCPP_INLINE_VISIBILITY complex& operator= (const value_type& __re)
284 {__re_ = __re; __im_ = value_type(); return *this;}
285 _LIBCPP_INLINE_VISIBILITY complex& operator+=(const value_type& __re) {__re_ += __re; return *this;}
286 _LIBCPP_INLINE_VISIBILITY complex& operator-=(const value_type& __re) {__re_ -= __re; return *this;}
287 _LIBCPP_INLINE_VISIBILITY complex& operator*=(const value_type& __re) {__re_ *= __re; __im_ *= __re; return *this;}
288 _LIBCPP_INLINE_VISIBILITY complex& operator/=(const value_type& __re) {__re_ /= __re; __im_ /= __re; return *this;}
290 template<class _Xp> _LIBCPP_INLINE_VISIBILITY complex& operator= (const complex<_Xp>& __c)
296 template<class _Xp> _LIBCPP_INLINE_VISIBILITY complex& operator+=(const complex<_Xp>& __c)
302 template<class _Xp> _LIBCPP_INLINE_VISIBILITY complex& operator-=(const complex<_Xp>& __c)
308 template<class _Xp> _LIBCPP_INLINE_VISIBILITY complex& operator*=(const complex<_Xp>& __c)
310 *this = *this * complex(__c.real(), __c.imag());
313 template<class _Xp> _LIBCPP_INLINE_VISIBILITY complex& operator/=(const complex<_Xp>& __c)
315 *this = *this / complex(__c.real(), __c.imag());
320 template<> class _LIBCPP_TEMPLATE_VIS complex<double>;
321 template<> class _LIBCPP_TEMPLATE_VIS complex<long double>;
324 class _LIBCPP_TEMPLATE_VIS complex<float>
329 typedef float value_type;
331 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR complex(float __re = 0.0f, float __im = 0.0f)
332 : __re_(__re), __im_(__im) {}
333 _LIBCPP_INLINE_VISIBILITY
334 explicit _LIBCPP_CONSTEXPR complex(const complex<double>& __c);
335 _LIBCPP_INLINE_VISIBILITY
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_TEMPLATE_VIS 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_INLINE_VISIBILITY
392 _LIBCPP_CONSTEXPR complex(const complex<float>& __c);
393 _LIBCPP_INLINE_VISIBILITY
394 explicit _LIBCPP_CONSTEXPR complex(const complex<long double>& __c);
396 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR double real() const {return __re_;}
397 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR double imag() const {return __im_;}
399 _LIBCPP_INLINE_VISIBILITY void real(value_type __re) {__re_ = __re;}
400 _LIBCPP_INLINE_VISIBILITY void imag(value_type __im) {__im_ = __im;}
402 _LIBCPP_INLINE_VISIBILITY complex& operator= (double __re)
403 {__re_ = __re; __im_ = value_type(); return *this;}
404 _LIBCPP_INLINE_VISIBILITY complex& operator+=(double __re) {__re_ += __re; return *this;}
405 _LIBCPP_INLINE_VISIBILITY complex& operator-=(double __re) {__re_ -= __re; return *this;}
406 _LIBCPP_INLINE_VISIBILITY complex& operator*=(double __re) {__re_ *= __re; __im_ *= __re; return *this;}
407 _LIBCPP_INLINE_VISIBILITY complex& operator/=(double __re) {__re_ /= __re; __im_ /= __re; return *this;}
409 template<class _Xp> _LIBCPP_INLINE_VISIBILITY complex& operator= (const complex<_Xp>& __c)
415 template<class _Xp> _LIBCPP_INLINE_VISIBILITY complex& operator+=(const complex<_Xp>& __c)
421 template<class _Xp> _LIBCPP_INLINE_VISIBILITY complex& operator-=(const complex<_Xp>& __c)
427 template<class _Xp> _LIBCPP_INLINE_VISIBILITY complex& operator*=(const complex<_Xp>& __c)
429 *this = *this * complex(__c.real(), __c.imag());
432 template<class _Xp> _LIBCPP_INLINE_VISIBILITY complex& operator/=(const complex<_Xp>& __c)
434 *this = *this / complex(__c.real(), __c.imag());
440 class _LIBCPP_TEMPLATE_VIS complex<long double>
445 typedef long double value_type;
447 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR complex(long double __re = 0.0L, long double __im = 0.0L)
448 : __re_(__re), __im_(__im) {}
449 _LIBCPP_INLINE_VISIBILITY
450 _LIBCPP_CONSTEXPR complex(const complex<float>& __c);
451 _LIBCPP_INLINE_VISIBILITY
452 _LIBCPP_CONSTEXPR complex(const complex<double>& __c);
454 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR long double real() const {return __re_;}
455 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR long double imag() const {return __im_;}
457 _LIBCPP_INLINE_VISIBILITY void real(value_type __re) {__re_ = __re;}
458 _LIBCPP_INLINE_VISIBILITY void imag(value_type __im) {__im_ = __im;}
460 _LIBCPP_INLINE_VISIBILITY complex& operator= (long double __re)
461 {__re_ = __re; __im_ = value_type(); return *this;}
462 _LIBCPP_INLINE_VISIBILITY complex& operator+=(long double __re) {__re_ += __re; return *this;}
463 _LIBCPP_INLINE_VISIBILITY complex& operator-=(long double __re) {__re_ -= __re; return *this;}
464 _LIBCPP_INLINE_VISIBILITY complex& operator*=(long double __re) {__re_ *= __re; __im_ *= __re; return *this;}
465 _LIBCPP_INLINE_VISIBILITY complex& operator/=(long double __re) {__re_ /= __re; __im_ /= __re; return *this;}
467 template<class _Xp> _LIBCPP_INLINE_VISIBILITY complex& operator= (const complex<_Xp>& __c)
473 template<class _Xp> _LIBCPP_INLINE_VISIBILITY complex& operator+=(const complex<_Xp>& __c)
479 template<class _Xp> _LIBCPP_INLINE_VISIBILITY complex& operator-=(const complex<_Xp>& __c)
485 template<class _Xp> _LIBCPP_INLINE_VISIBILITY complex& operator*=(const complex<_Xp>& __c)
487 *this = *this * complex(__c.real(), __c.imag());
490 template<class _Xp> _LIBCPP_INLINE_VISIBILITY complex& operator/=(const complex<_Xp>& __c)
492 *this = *this / complex(__c.real(), __c.imag());
499 complex<float>::complex(const complex<double>& __c)
500 : __re_(__c.real()), __im_(__c.imag()) {}
504 complex<float>::complex(const complex<long double>& __c)
505 : __re_(__c.real()), __im_(__c.imag()) {}
509 complex<double>::complex(const complex<float>& __c)
510 : __re_(__c.real()), __im_(__c.imag()) {}
514 complex<double>::complex(const complex<long double>& __c)
515 : __re_(__c.real()), __im_(__c.imag()) {}
519 complex<long double>::complex(const complex<float>& __c)
520 : __re_(__c.real()), __im_(__c.imag()) {}
524 complex<long double>::complex(const complex<double>& __c)
525 : __re_(__c.real()), __im_(__c.imag()) {}
530 inline _LIBCPP_INLINE_VISIBILITY
532 operator+(const complex<_Tp>& __x, const complex<_Tp>& __y)
534 complex<_Tp> __t(__x);
540 inline _LIBCPP_INLINE_VISIBILITY
542 operator+(const complex<_Tp>& __x, const _Tp& __y)
544 complex<_Tp> __t(__x);
550 inline _LIBCPP_INLINE_VISIBILITY
552 operator+(const _Tp& __x, const complex<_Tp>& __y)
554 complex<_Tp> __t(__y);
560 inline _LIBCPP_INLINE_VISIBILITY
562 operator-(const complex<_Tp>& __x, const complex<_Tp>& __y)
564 complex<_Tp> __t(__x);
570 inline _LIBCPP_INLINE_VISIBILITY
572 operator-(const complex<_Tp>& __x, const _Tp& __y)
574 complex<_Tp> __t(__x);
580 inline _LIBCPP_INLINE_VISIBILITY
582 operator-(const _Tp& __x, const complex<_Tp>& __y)
584 complex<_Tp> __t(-__y);
591 operator*(const complex<_Tp>& __z, const complex<_Tp>& __w)
593 _Tp __a = __z.real();
594 _Tp __b = __z.imag();
595 _Tp __c = __w.real();
596 _Tp __d = __w.imag();
597 _Tp __ac = __a * __c;
598 _Tp __bd = __b * __d;
599 _Tp __ad = __a * __d;
600 _Tp __bc = __b * __c;
601 _Tp __x = __ac - __bd;
602 _Tp __y = __ad + __bc;
603 if (__libcpp_isnan_or_builtin(__x) && __libcpp_isnan_or_builtin(__y))
605 bool __recalc = false;
606 if (__libcpp_isinf_or_builtin(__a) || __libcpp_isinf_or_builtin(__b))
608 __a = copysign(__libcpp_isinf_or_builtin(__a) ? _Tp(1) : _Tp(0), __a);
609 __b = copysign(__libcpp_isinf_or_builtin(__b) ? _Tp(1) : _Tp(0), __b);
610 if (__libcpp_isnan_or_builtin(__c))
611 __c = copysign(_Tp(0), __c);
612 if (__libcpp_isnan_or_builtin(__d))
613 __d = copysign(_Tp(0), __d);
616 if (__libcpp_isinf_or_builtin(__c) || __libcpp_isinf_or_builtin(__d))
618 __c = copysign(__libcpp_isinf_or_builtin(__c) ? _Tp(1) : _Tp(0), __c);
619 __d = copysign(__libcpp_isinf_or_builtin(__d) ? _Tp(1) : _Tp(0), __d);
620 if (__libcpp_isnan_or_builtin(__a))
621 __a = copysign(_Tp(0), __a);
622 if (__libcpp_isnan_or_builtin(__b))
623 __b = copysign(_Tp(0), __b);
626 if (!__recalc && (__libcpp_isinf_or_builtin(__ac) || __libcpp_isinf_or_builtin(__bd) ||
627 __libcpp_isinf_or_builtin(__ad) || __libcpp_isinf_or_builtin(__bc)))
629 if (__libcpp_isnan_or_builtin(__a))
630 __a = copysign(_Tp(0), __a);
631 if (__libcpp_isnan_or_builtin(__b))
632 __b = copysign(_Tp(0), __b);
633 if (__libcpp_isnan_or_builtin(__c))
634 __c = copysign(_Tp(0), __c);
635 if (__libcpp_isnan_or_builtin(__d))
636 __d = copysign(_Tp(0), __d);
641 __x = _Tp(INFINITY) * (__a * __c - __b * __d);
642 __y = _Tp(INFINITY) * (__a * __d + __b * __c);
645 return complex<_Tp>(__x, __y);
649 inline _LIBCPP_INLINE_VISIBILITY
651 operator*(const complex<_Tp>& __x, const _Tp& __y)
653 complex<_Tp> __t(__x);
659 inline _LIBCPP_INLINE_VISIBILITY
661 operator*(const _Tp& __x, const complex<_Tp>& __y)
663 complex<_Tp> __t(__y);
670 operator/(const complex<_Tp>& __z, const complex<_Tp>& __w)
673 _Tp __a = __z.real();
674 _Tp __b = __z.imag();
675 _Tp __c = __w.real();
676 _Tp __d = __w.imag();
677 _Tp __logbw = logb(fmax(fabs(__c), fabs(__d)));
678 if (__libcpp_isfinite_or_builtin(__logbw))
680 __ilogbw = static_cast<int>(__logbw);
681 __c = scalbn(__c, -__ilogbw);
682 __d = scalbn(__d, -__ilogbw);
684 _Tp __denom = __c * __c + __d * __d;
685 _Tp __x = scalbn((__a * __c + __b * __d) / __denom, -__ilogbw);
686 _Tp __y = scalbn((__b * __c - __a * __d) / __denom, -__ilogbw);
687 if (__libcpp_isnan_or_builtin(__x) && __libcpp_isnan_or_builtin(__y))
689 if ((__denom == _Tp(0)) && (!__libcpp_isnan_or_builtin(__a) || !__libcpp_isnan_or_builtin(__b)))
691 __x = copysign(_Tp(INFINITY), __c) * __a;
692 __y = copysign(_Tp(INFINITY), __c) * __b;
694 else if ((__libcpp_isinf_or_builtin(__a) || __libcpp_isinf_or_builtin(__b)) && __libcpp_isfinite_or_builtin(__c) && __libcpp_isfinite_or_builtin(__d))
696 __a = copysign(__libcpp_isinf_or_builtin(__a) ? _Tp(1) : _Tp(0), __a);
697 __b = copysign(__libcpp_isinf_or_builtin(__b) ? _Tp(1) : _Tp(0), __b);
698 __x = _Tp(INFINITY) * (__a * __c + __b * __d);
699 __y = _Tp(INFINITY) * (__b * __c - __a * __d);
701 else if (__libcpp_isinf_or_builtin(__logbw) && __logbw > _Tp(0) && __libcpp_isfinite_or_builtin(__a) && __libcpp_isfinite_or_builtin(__b))
703 __c = copysign(__libcpp_isinf_or_builtin(__c) ? _Tp(1) : _Tp(0), __c);
704 __d = copysign(__libcpp_isinf_or_builtin(__d) ? _Tp(1) : _Tp(0), __d);
705 __x = _Tp(0) * (__a * __c + __b * __d);
706 __y = _Tp(0) * (__b * __c - __a * __d);
709 return complex<_Tp>(__x, __y);
713 inline _LIBCPP_INLINE_VISIBILITY
715 operator/(const complex<_Tp>& __x, const _Tp& __y)
717 return complex<_Tp>(__x.real() / __y, __x.imag() / __y);
721 inline _LIBCPP_INLINE_VISIBILITY
723 operator/(const _Tp& __x, const complex<_Tp>& __y)
725 complex<_Tp> __t(__x);
731 inline _LIBCPP_INLINE_VISIBILITY
733 operator+(const complex<_Tp>& __x)
739 inline _LIBCPP_INLINE_VISIBILITY
741 operator-(const complex<_Tp>& __x)
743 return complex<_Tp>(-__x.real(), -__x.imag());
747 inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
749 operator==(const complex<_Tp>& __x, const complex<_Tp>& __y)
751 return __x.real() == __y.real() && __x.imag() == __y.imag();
755 inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
757 operator==(const complex<_Tp>& __x, const _Tp& __y)
759 return __x.real() == __y && __x.imag() == 0;
763 inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
765 operator==(const _Tp& __x, const complex<_Tp>& __y)
767 return __x == __y.real() && 0 == __y.imag();
771 inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
773 operator!=(const complex<_Tp>& __x, const complex<_Tp>& __y)
775 return !(__x == __y);
779 inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
781 operator!=(const complex<_Tp>& __x, const _Tp& __y)
783 return !(__x == __y);
787 inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
789 operator!=(const _Tp& __x, const complex<_Tp>& __y)
791 return !(__x == __y);
796 template <class _Tp, bool = is_integral<_Tp>::value,
797 bool = is_floating_point<_Tp>::value
799 struct __libcpp_complex_overload_traits {};
803 struct __libcpp_complex_overload_traits<_Tp, true, false>
805 typedef double _ValueType;
806 typedef complex<double> _ComplexType;
809 // Floating point types
811 struct __libcpp_complex_overload_traits<_Tp, false, true>
813 typedef _Tp _ValueType;
814 typedef complex<_Tp> _ComplexType;
820 inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
822 real(const complex<_Tp>& __c)
828 inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
829 typename __libcpp_complex_overload_traits<_Tp>::_ValueType
838 inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
840 imag(const complex<_Tp>& __c)
846 inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
847 typename __libcpp_complex_overload_traits<_Tp>::_ValueType
856 inline _LIBCPP_INLINE_VISIBILITY
858 abs(const complex<_Tp>& __c)
860 return hypot(__c.real(), __c.imag());
866 inline _LIBCPP_INLINE_VISIBILITY
868 arg(const complex<_Tp>& __c)
870 return atan2(__c.imag(), __c.real());
874 inline _LIBCPP_INLINE_VISIBILITY
876 is_same<_Tp, long double>::value,
881 return atan2l(0.L, __re);
885 inline _LIBCPP_INLINE_VISIBILITY
888 is_integral<_Tp>::value || is_same<_Tp, double>::value,
893 return atan2(0., __re);
897 inline _LIBCPP_INLINE_VISIBILITY
899 is_same<_Tp, float>::value,
904 return atan2f(0.F, __re);
910 inline _LIBCPP_INLINE_VISIBILITY
912 norm(const complex<_Tp>& __c)
914 if (__libcpp_isinf_or_builtin(__c.real()))
915 return abs(__c.real());
916 if (__libcpp_isinf_or_builtin(__c.imag()))
917 return abs(__c.imag());
918 return __c.real() * __c.real() + __c.imag() * __c.imag();
922 inline _LIBCPP_INLINE_VISIBILITY
923 typename __libcpp_complex_overload_traits<_Tp>::_ValueType
926 typedef typename __libcpp_complex_overload_traits<_Tp>::_ValueType _ValueType;
927 return static_cast<_ValueType>(__re) * __re;
933 inline _LIBCPP_INLINE_VISIBILITY
935 conj(const complex<_Tp>& __c)
937 return complex<_Tp>(__c.real(), -__c.imag());
941 inline _LIBCPP_INLINE_VISIBILITY
942 typename __libcpp_complex_overload_traits<_Tp>::_ComplexType
945 typedef typename __libcpp_complex_overload_traits<_Tp>::_ComplexType _ComplexType;
946 return _ComplexType(__re);
954 inline _LIBCPP_INLINE_VISIBILITY
956 proj(const complex<_Tp>& __c)
958 std::complex<_Tp> __r = __c;
959 if (__libcpp_isinf_or_builtin(__c.real()) || __libcpp_isinf_or_builtin(__c.imag()))
960 __r = complex<_Tp>(INFINITY, copysign(_Tp(0), __c.imag()));
965 inline _LIBCPP_INLINE_VISIBILITY
968 is_floating_point<_Tp>::value,
969 typename __libcpp_complex_overload_traits<_Tp>::_ComplexType
973 if (__libcpp_isinf_or_builtin(__re))
975 return complex<_Tp>(__re);
979 inline _LIBCPP_INLINE_VISIBILITY
982 is_integral<_Tp>::value,
983 typename __libcpp_complex_overload_traits<_Tp>::_ComplexType
987 typedef typename __libcpp_complex_overload_traits<_Tp>::_ComplexType _ComplexType;
988 return _ComplexType(__re);
995 polar(const _Tp& __rho, const _Tp& __theta = _Tp())
997 if (__libcpp_isnan_or_builtin(__rho) || signbit(__rho))
998 return complex<_Tp>(_Tp(NAN), _Tp(NAN));
999 if (__libcpp_isnan_or_builtin(__theta))
1001 if (__libcpp_isinf_or_builtin(__rho))
1002 return complex<_Tp>(__rho, __theta);
1003 return complex<_Tp>(__theta, __theta);
1005 if (__libcpp_isinf_or_builtin(__theta))
1007 if (__libcpp_isinf_or_builtin(__rho))
1008 return complex<_Tp>(__rho, _Tp(NAN));
1009 return complex<_Tp>(_Tp(NAN), _Tp(NAN));
1011 _Tp __x = __rho * cos(__theta);
1012 if (__libcpp_isnan_or_builtin(__x))
1014 _Tp __y = __rho * sin(__theta);
1015 if (__libcpp_isnan_or_builtin(__y))
1017 return complex<_Tp>(__x, __y);
1023 inline _LIBCPP_INLINE_VISIBILITY
1025 log(const complex<_Tp>& __x)
1027 return complex<_Tp>(log(abs(__x)), arg(__x));
1033 inline _LIBCPP_INLINE_VISIBILITY
1035 log10(const complex<_Tp>& __x)
1037 return log(__x) / log(_Tp(10));
1044 sqrt(const complex<_Tp>& __x)
1046 if (__libcpp_isinf_or_builtin(__x.imag()))
1047 return complex<_Tp>(_Tp(INFINITY), __x.imag());
1048 if (__libcpp_isinf_or_builtin(__x.real()))
1050 if (__x.real() > _Tp(0))
1051 return complex<_Tp>(__x.real(), __libcpp_isnan_or_builtin(__x.imag()) ? __x.imag() : copysign(_Tp(0), __x.imag()));
1052 return complex<_Tp>(__libcpp_isnan_or_builtin(__x.imag()) ? __x.imag() : _Tp(0), copysign(__x.real(), __x.imag()));
1054 return polar(sqrt(abs(__x)), arg(__x) / _Tp(2));
1061 exp(const complex<_Tp>& __x)
1063 _Tp __i = __x.imag();
1064 if (__libcpp_isinf_or_builtin(__x.real()))
1066 if (__x.real() < _Tp(0))
1068 if (!__libcpp_isfinite_or_builtin(__i))
1071 else if (__i == 0 || !__libcpp_isfinite_or_builtin(__i))
1073 if (__libcpp_isinf_or_builtin(__i))
1075 return complex<_Tp>(__x.real(), __i);
1078 else if (__libcpp_isnan_or_builtin(__x.real()) && __x.imag() == 0)
1080 _Tp __e = exp(__x.real());
1081 return complex<_Tp>(__e * cos(__i), __e * sin(__i));
1087 inline _LIBCPP_INLINE_VISIBILITY
1089 pow(const complex<_Tp>& __x, const complex<_Tp>& __y)
1091 return exp(__y * log(__x));
1094 template<class _Tp, class _Up>
1095 inline _LIBCPP_INLINE_VISIBILITY
1096 complex<typename __promote<_Tp, _Up>::type>
1097 pow(const complex<_Tp>& __x, const complex<_Up>& __y)
1099 typedef complex<typename __promote<_Tp, _Up>::type> result_type;
1100 return _VSTD::pow(result_type(__x), result_type(__y));
1103 template<class _Tp, class _Up>
1104 inline _LIBCPP_INLINE_VISIBILITY
1107 is_arithmetic<_Up>::value,
1108 complex<typename __promote<_Tp, _Up>::type>
1110 pow(const complex<_Tp>& __x, const _Up& __y)
1112 typedef complex<typename __promote<_Tp, _Up>::type> result_type;
1113 return _VSTD::pow(result_type(__x), result_type(__y));
1116 template<class _Tp, class _Up>
1117 inline _LIBCPP_INLINE_VISIBILITY
1120 is_arithmetic<_Tp>::value,
1121 complex<typename __promote<_Tp, _Up>::type>
1123 pow(const _Tp& __x, const complex<_Up>& __y)
1125 typedef complex<typename __promote<_Tp, _Up>::type> result_type;
1126 return _VSTD::pow(result_type(__x), result_type(__y));
1129 // __sqr, computes pow(x, 2)
1132 inline _LIBCPP_INLINE_VISIBILITY
1134 __sqr(const complex<_Tp>& __x)
1136 return complex<_Tp>((__x.real() - __x.imag()) * (__x.real() + __x.imag()),
1137 _Tp(2) * __x.real() * __x.imag());
1144 asinh(const complex<_Tp>& __x)
1146 const _Tp __pi(atan2(+0., -0.));
1147 if (__libcpp_isinf_or_builtin(__x.real()))
1149 if (__libcpp_isnan_or_builtin(__x.imag()))
1151 if (__libcpp_isinf_or_builtin(__x.imag()))
1152 return complex<_Tp>(__x.real(), copysign(__pi * _Tp(0.25), __x.imag()));
1153 return complex<_Tp>(__x.real(), copysign(_Tp(0), __x.imag()));
1155 if (__libcpp_isnan_or_builtin(__x.real()))
1157 if (__libcpp_isinf_or_builtin(__x.imag()))
1158 return complex<_Tp>(__x.imag(), __x.real());
1159 if (__x.imag() == 0)
1161 return complex<_Tp>(__x.real(), __x.real());
1163 if (__libcpp_isinf_or_builtin(__x.imag()))
1164 return complex<_Tp>(copysign(__x.imag(), __x.real()), copysign(__pi/_Tp(2), __x.imag()));
1165 complex<_Tp> __z = log(__x + sqrt(__sqr(__x) + _Tp(1)));
1166 return complex<_Tp>(copysign(__z.real(), __x.real()), copysign(__z.imag(), __x.imag()));
1173 acosh(const complex<_Tp>& __x)
1175 const _Tp __pi(atan2(+0., -0.));
1176 if (__libcpp_isinf_or_builtin(__x.real()))
1178 if (__libcpp_isnan_or_builtin(__x.imag()))
1179 return complex<_Tp>(abs(__x.real()), __x.imag());
1180 if (__libcpp_isinf_or_builtin(__x.imag()))
1183 return complex<_Tp>(__x.real(), copysign(__pi * _Tp(0.25), __x.imag()));
1185 return complex<_Tp>(-__x.real(), copysign(__pi * _Tp(0.75), __x.imag()));
1188 return complex<_Tp>(-__x.real(), copysign(__pi, __x.imag()));
1189 return complex<_Tp>(__x.real(), copysign(_Tp(0), __x.imag()));
1191 if (__libcpp_isnan_or_builtin(__x.real()))
1193 if (__libcpp_isinf_or_builtin(__x.imag()))
1194 return complex<_Tp>(abs(__x.imag()), __x.real());
1195 return complex<_Tp>(__x.real(), __x.real());
1197 if (__libcpp_isinf_or_builtin(__x.imag()))
1198 return complex<_Tp>(abs(__x.imag()), copysign(__pi/_Tp(2), __x.imag()));
1199 complex<_Tp> __z = log(__x + sqrt(__sqr(__x) - _Tp(1)));
1200 return complex<_Tp>(copysign(__z.real(), _Tp(0)), copysign(__z.imag(), __x.imag()));
1207 atanh(const complex<_Tp>& __x)
1209 const _Tp __pi(atan2(+0., -0.));
1210 if (__libcpp_isinf_or_builtin(__x.imag()))
1212 return complex<_Tp>(copysign(_Tp(0), __x.real()), copysign(__pi/_Tp(2), __x.imag()));
1214 if (__libcpp_isnan_or_builtin(__x.imag()))
1216 if (__libcpp_isinf_or_builtin(__x.real()) || __x.real() == 0)
1217 return complex<_Tp>(copysign(_Tp(0), __x.real()), __x.imag());
1218 return complex<_Tp>(__x.imag(), __x.imag());
1220 if (__libcpp_isnan_or_builtin(__x.real()))
1222 return complex<_Tp>(__x.real(), __x.real());
1224 if (__libcpp_isinf_or_builtin(__x.real()))
1226 return complex<_Tp>(copysign(_Tp(0), __x.real()), copysign(__pi/_Tp(2), __x.imag()));
1228 if (abs(__x.real()) == _Tp(1) && __x.imag() == _Tp(0))
1230 return complex<_Tp>(copysign(_Tp(INFINITY), __x.real()), copysign(_Tp(0), __x.imag()));
1232 complex<_Tp> __z = log((_Tp(1) + __x) / (_Tp(1) - __x)) / _Tp(2);
1233 return complex<_Tp>(copysign(__z.real(), __x.real()), copysign(__z.imag(), __x.imag()));
1240 sinh(const complex<_Tp>& __x)
1242 if (__libcpp_isinf_or_builtin(__x.real()) && !__libcpp_isfinite_or_builtin(__x.imag()))
1243 return complex<_Tp>(__x.real(), _Tp(NAN));
1244 if (__x.real() == 0 && !__libcpp_isfinite_or_builtin(__x.imag()))
1245 return complex<_Tp>(__x.real(), _Tp(NAN));
1246 if (__x.imag() == 0 && !__libcpp_isfinite_or_builtin(__x.real()))
1248 return complex<_Tp>(sinh(__x.real()) * cos(__x.imag()), cosh(__x.real()) * sin(__x.imag()));
1255 cosh(const complex<_Tp>& __x)
1257 if (__libcpp_isinf_or_builtin(__x.real()) && !__libcpp_isfinite_or_builtin(__x.imag()))
1258 return complex<_Tp>(abs(__x.real()), _Tp(NAN));
1259 if (__x.real() == 0 && !__libcpp_isfinite_or_builtin(__x.imag()))
1260 return complex<_Tp>(_Tp(NAN), __x.real());
1261 if (__x.real() == 0 && __x.imag() == 0)
1262 return complex<_Tp>(_Tp(1), __x.imag());
1263 if (__x.imag() == 0 && !__libcpp_isfinite_or_builtin(__x.real()))
1264 return complex<_Tp>(abs(__x.real()), __x.imag());
1265 return complex<_Tp>(cosh(__x.real()) * cos(__x.imag()), sinh(__x.real()) * sin(__x.imag()));
1272 tanh(const complex<_Tp>& __x)
1274 if (__libcpp_isinf_or_builtin(__x.real()))
1276 if (!__libcpp_isfinite_or_builtin(__x.imag()))
1277 return complex<_Tp>(_Tp(1), _Tp(0));
1278 return complex<_Tp>(_Tp(1), copysign(_Tp(0), sin(_Tp(2) * __x.imag())));
1280 if (__libcpp_isnan_or_builtin(__x.real()) && __x.imag() == 0)
1282 _Tp __2r(_Tp(2) * __x.real());
1283 _Tp __2i(_Tp(2) * __x.imag());
1284 _Tp __d(cosh(__2r) + cos(__2i));
1285 _Tp __2rsh(sinh(__2r));
1286 if (__libcpp_isinf_or_builtin(__2rsh) && __libcpp_isinf_or_builtin(__d))
1287 return complex<_Tp>(__2rsh > _Tp(0) ? _Tp(1) : _Tp(-1),
1288 __2i > _Tp(0) ? _Tp(0) : _Tp(-0.));
1289 return complex<_Tp>(__2rsh/__d, sin(__2i)/__d);
1296 asin(const complex<_Tp>& __x)
1298 complex<_Tp> __z = asinh(complex<_Tp>(-__x.imag(), __x.real()));
1299 return complex<_Tp>(__z.imag(), -__z.real());
1306 acos(const complex<_Tp>& __x)
1308 const _Tp __pi(atan2(+0., -0.));
1309 if (__libcpp_isinf_or_builtin(__x.real()))
1311 if (__libcpp_isnan_or_builtin(__x.imag()))
1312 return complex<_Tp>(__x.imag(), __x.real());
1313 if (__libcpp_isinf_or_builtin(__x.imag()))
1315 if (__x.real() < _Tp(0))
1316 return complex<_Tp>(_Tp(0.75) * __pi, -__x.imag());
1317 return complex<_Tp>(_Tp(0.25) * __pi, -__x.imag());
1319 if (__x.real() < _Tp(0))
1320 return complex<_Tp>(__pi, signbit(__x.imag()) ? -__x.real() : __x.real());
1321 return complex<_Tp>(_Tp(0), signbit(__x.imag()) ? __x.real() : -__x.real());
1323 if (__libcpp_isnan_or_builtin(__x.real()))
1325 if (__libcpp_isinf_or_builtin(__x.imag()))
1326 return complex<_Tp>(__x.real(), -__x.imag());
1327 return complex<_Tp>(__x.real(), __x.real());
1329 if (__libcpp_isinf_or_builtin(__x.imag()))
1330 return complex<_Tp>(__pi/_Tp(2), -__x.imag());
1331 if (__x.real() == 0 && (__x.imag() == 0 || isnan(__x.imag())))
1332 return complex<_Tp>(__pi/_Tp(2), -__x.imag());
1333 complex<_Tp> __z = log(__x + sqrt(__sqr(__x) - _Tp(1)));
1334 if (signbit(__x.imag()))
1335 return complex<_Tp>(abs(__z.imag()), abs(__z.real()));
1336 return complex<_Tp>(abs(__z.imag()), -abs(__z.real()));
1343 atan(const complex<_Tp>& __x)
1345 complex<_Tp> __z = atanh(complex<_Tp>(-__x.imag(), __x.real()));
1346 return complex<_Tp>(__z.imag(), -__z.real());
1353 sin(const complex<_Tp>& __x)
1355 complex<_Tp> __z = sinh(complex<_Tp>(-__x.imag(), __x.real()));
1356 return complex<_Tp>(__z.imag(), -__z.real());
1362 inline _LIBCPP_INLINE_VISIBILITY
1364 cos(const complex<_Tp>& __x)
1366 return cosh(complex<_Tp>(-__x.imag(), __x.real()));
1373 tan(const complex<_Tp>& __x)
1375 complex<_Tp> __z = tanh(complex<_Tp>(-__x.imag(), __x.real()));
1376 return complex<_Tp>(__z.imag(), -__z.real());
1379 template<class _Tp, class _CharT, class _Traits>
1380 basic_istream<_CharT, _Traits>&
1381 operator>>(basic_istream<_CharT, _Traits>& __is, complex<_Tp>& __x)
1386 if (__is.peek() == _CharT('('))
1394 _CharT __c = __is.peek();
1395 if (__c == _CharT(','))
1404 if (__c == _CharT(')'))
1407 __x = complex<_Tp>(__r, __i);
1410 __is.setstate(ios_base::failbit);
1413 __is.setstate(ios_base::failbit);
1415 else if (__c == _CharT(')'))
1418 __x = complex<_Tp>(__r, _Tp(0));
1421 __is.setstate(ios_base::failbit);
1424 __is.setstate(ios_base::failbit);
1431 __x = complex<_Tp>(__r, _Tp(0));
1433 __is.setstate(ios_base::failbit);
1437 __is.setstate(ios_base::failbit);
1441 template<class _Tp, class _CharT, class _Traits>
1442 basic_ostream<_CharT, _Traits>&
1443 operator<<(basic_ostream<_CharT, _Traits>& __os, const complex<_Tp>& __x)
1445 basic_ostringstream<_CharT, _Traits> __s;
1446 __s.flags(__os.flags());
1447 __s.imbue(__os.getloc());
1448 __s.precision(__os.precision());
1449 __s << '(' << __x.real() << ',' << __x.imag() << ')';
1450 return __os << __s.str();
1453 #if _LIBCPP_STD_VER > 11
1454 // Literal suffix for complex number literals [complex.literals]
1455 inline namespace literals
1457 inline namespace complex_literals
1459 constexpr complex<long double> operator""il(long double __im)
1461 return { 0.0l, __im };
1464 constexpr complex<long double> operator""il(unsigned long long __im)
1466 return { 0.0l, static_cast<long double>(__im) };
1470 constexpr complex<double> operator""i(long double __im)
1472 return { 0.0, static_cast<double>(__im) };
1475 constexpr complex<double> operator""i(unsigned long long __im)
1477 return { 0.0, static_cast<double>(__im) };
1481 constexpr complex<float> operator""if(long double __im)
1483 return { 0.0f, static_cast<float>(__im) };
1486 constexpr complex<float> operator""if(unsigned long long __im)
1488 return { 0.0f, static_cast<float>(__im) };
1494 _LIBCPP_END_NAMESPACE_STD
1496 #endif // _LIBCPP_COMPLEX