2 //===--------------------------- complex ----------------------------------===//
4 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
5 // See https://llvm.org/LICENSE.txt for license information.
6 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
8 //===----------------------------------------------------------------------===//
10 #ifndef _LIBCPP_COMPLEX
11 #define _LIBCPP_COMPLEX
25 complex(const T& re = T(), const T& im = T()); // constexpr in C++14
26 complex(const complex&); // constexpr in C++14
27 template<class X> complex(const complex<X>&); // constexpr in C++14
29 T real() const; // constexpr in C++14
30 T imag() const; // constexpr in C++14
35 complex<T>& operator= (const T&);
36 complex<T>& operator+=(const T&);
37 complex<T>& operator-=(const T&);
38 complex<T>& operator*=(const T&);
39 complex<T>& operator/=(const T&);
41 complex& operator=(const complex&);
42 template<class X> complex<T>& operator= (const complex<X>&);
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>&);
53 typedef float value_type;
55 constexpr complex(float re = 0.0f, float im = 0.0f);
56 explicit constexpr complex(const complex<double>&);
57 explicit constexpr complex(const complex<long double>&);
59 constexpr float real() const;
61 constexpr float imag() const;
64 complex<float>& operator= (float);
65 complex<float>& operator+=(float);
66 complex<float>& operator-=(float);
67 complex<float>& operator*=(float);
68 complex<float>& operator/=(float);
70 complex<float>& operator=(const complex<float>&);
71 template<class X> complex<float>& operator= (const complex<X>&);
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>&);
82 typedef double value_type;
84 constexpr complex(double re = 0.0, double im = 0.0);
85 constexpr complex(const complex<float>&);
86 explicit constexpr complex(const complex<long double>&);
88 constexpr double real() const;
90 constexpr double imag() const;
93 complex<double>& operator= (double);
94 complex<double>& operator+=(double);
95 complex<double>& operator-=(double);
96 complex<double>& operator*=(double);
97 complex<double>& operator/=(double);
98 complex<double>& operator=(const complex<double>&);
100 template<class X> complex<double>& operator= (const complex<X>&);
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>&);
108 class complex<long double>
111 typedef long double value_type;
113 constexpr complex(long double re = 0.0L, long double im = 0.0L);
114 constexpr complex(const complex<float>&);
115 constexpr complex(const complex<double>&);
117 constexpr long double real() const;
118 void real(long double);
119 constexpr long double imag() const;
120 void imag(long double);
122 complex<long double>& operator=(const complex<long double>&);
123 complex<long double>& operator= (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);
129 template<class X> complex<long double>& operator= (const complex<X>&);
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>&);
137 template<class T> complex<T> operator+(const complex<T>&, const complex<T>&);
138 template<class T> complex<T> operator+(const complex<T>&, const T&);
139 template<class T> complex<T> operator+(const T&, const complex<T>&);
140 template<class T> complex<T> operator-(const complex<T>&, const complex<T>&);
141 template<class T> complex<T> operator-(const complex<T>&, const T&);
142 template<class T> complex<T> operator-(const T&, const complex<T>&);
143 template<class T> complex<T> operator*(const complex<T>&, const complex<T>&);
144 template<class T> complex<T> operator*(const complex<T>&, const T&);
145 template<class T> complex<T> operator*(const T&, const complex<T>&);
146 template<class T> complex<T> operator/(const complex<T>&, const complex<T>&);
147 template<class T> complex<T> operator/(const complex<T>&, const T&);
148 template<class T> complex<T> operator/(const T&, const complex<T>&);
149 template<class T> complex<T> operator+(const complex<T>&);
150 template<class T> complex<T> operator-(const complex<T>&);
151 template<class T> bool operator==(const complex<T>&, const complex<T>&); // constexpr in C++14
152 template<class T> bool operator==(const complex<T>&, const T&); // constexpr in C++14
153 template<class T> bool operator==(const T&, const complex<T>&); // constexpr in C++14
154 template<class T> bool operator!=(const complex<T>&, const complex<T>&); // constexpr in C++14
155 template<class T> bool operator!=(const complex<T>&, const T&); // constexpr in C++14
156 template<class T> bool operator!=(const T&, const complex<T>&); // constexpr in C++14
158 template<class T, class charT, class traits>
159 basic_istream<charT, traits>&
160 operator>>(basic_istream<charT, traits>&, complex<T>&);
161 template<class T, class charT, class traits>
162 basic_ostream<charT, traits>&
163 operator<<(basic_ostream<charT, traits>&, const complex<T>&);
167 template<class T> T real(const complex<T>&); // constexpr in C++14
168 long double real(long double); // constexpr in C++14
169 double real(double); // constexpr in C++14
170 template<Integral T> double real(T); // constexpr in C++14
171 float real(float); // constexpr in C++14
173 template<class T> T imag(const complex<T>&); // constexpr in C++14
174 long double imag(long double); // constexpr in C++14
175 double imag(double); // constexpr in C++14
176 template<Integral T> double imag(T); // constexpr in C++14
177 float imag(float); // constexpr in C++14
179 template<class T> T abs(const complex<T>&);
181 template<class T> T arg(const complex<T>&);
182 long double arg(long double);
184 template<Integral T> double arg(T);
187 template<class T> T norm(const complex<T>&);
188 long double norm(long double);
190 template<Integral T> double norm(T);
193 template<class T> complex<T> conj(const complex<T>&);
194 complex<long double> conj(long double);
195 complex<double> conj(double);
196 template<Integral T> complex<double> conj(T);
197 complex<float> conj(float);
199 template<class T> complex<T> proj(const complex<T>&);
200 complex<long double> proj(long double);
201 complex<double> proj(double);
202 template<Integral T> complex<double> proj(T);
203 complex<float> proj(float);
205 template<class T> complex<T> polar(const T&, const T& = T());
207 // 26.3.8 transcendentals:
208 template<class T> complex<T> acos(const complex<T>&);
209 template<class T> complex<T> asin(const complex<T>&);
210 template<class T> complex<T> atan(const complex<T>&);
211 template<class T> complex<T> acosh(const complex<T>&);
212 template<class T> complex<T> asinh(const complex<T>&);
213 template<class T> complex<T> atanh(const complex<T>&);
214 template<class T> complex<T> cos (const complex<T>&);
215 template<class T> complex<T> cosh (const complex<T>&);
216 template<class T> complex<T> exp (const complex<T>&);
217 template<class T> complex<T> log (const complex<T>&);
218 template<class T> complex<T> log10(const complex<T>&);
220 template<class T> complex<T> pow(const complex<T>&, const T&);
221 template<class T> complex<T> pow(const complex<T>&, const complex<T>&);
222 template<class T> complex<T> pow(const T&, const complex<T>&);
224 template<class T> complex<T> sin (const complex<T>&);
225 template<class T> complex<T> sinh (const complex<T>&);
226 template<class T> complex<T> sqrt (const complex<T>&);
227 template<class T> complex<T> tan (const complex<T>&);
228 template<class T> complex<T> tanh (const complex<T>&);
230 template<class T, class charT, class traits>
231 basic_istream<charT, traits>&
232 operator>>(basic_istream<charT, traits>& is, complex<T>& x);
234 template<class T, class charT, class traits>
235 basic_ostream<charT, traits>&
236 operator<<(basic_ostream<charT, traits>& o, const complex<T>& x);
243 #include <type_traits>
249 #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
250 #pragma GCC system_header
253 _LIBCPP_BEGIN_NAMESPACE_STD
255 template<class _Tp> class _LIBCPP_TEMPLATE_VIS complex;
257 template<class _Tp> complex<_Tp> operator*(const complex<_Tp>& __z, const complex<_Tp>& __w);
258 template<class _Tp> complex<_Tp> operator/(const complex<_Tp>& __x, const complex<_Tp>& __y);
261 class _LIBCPP_TEMPLATE_VIS complex
264 typedef _Tp value_type;
269 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
270 complex(const value_type& __re = value_type(), const value_type& __im = value_type())
271 : __re_(__re), __im_(__im) {}
272 template<class _Xp> _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
273 complex(const complex<_Xp>& __c)
274 : __re_(__c.real()), __im_(__c.imag()) {}
276 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11 value_type real() const {return __re_;}
277 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11 value_type imag() const {return __im_;}
279 _LIBCPP_INLINE_VISIBILITY void real(value_type __re) {__re_ = __re;}
280 _LIBCPP_INLINE_VISIBILITY void imag(value_type __im) {__im_ = __im;}
282 _LIBCPP_INLINE_VISIBILITY complex& operator= (const value_type& __re)
283 {__re_ = __re; __im_ = value_type(); return *this;}
284 _LIBCPP_INLINE_VISIBILITY complex& operator+=(const value_type& __re) {__re_ += __re; 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; __im_ *= __re; return *this;}
287 _LIBCPP_INLINE_VISIBILITY complex& operator/=(const value_type& __re) {__re_ /= __re; __im_ /= __re; return *this;}
289 template<class _Xp> _LIBCPP_INLINE_VISIBILITY complex& operator= (const complex<_Xp>& __c)
295 template<class _Xp> _LIBCPP_INLINE_VISIBILITY complex& operator+=(const complex<_Xp>& __c)
301 template<class _Xp> _LIBCPP_INLINE_VISIBILITY complex& operator-=(const complex<_Xp>& __c)
307 template<class _Xp> _LIBCPP_INLINE_VISIBILITY complex& operator*=(const complex<_Xp>& __c)
309 *this = *this * complex(__c.real(), __c.imag());
312 template<class _Xp> _LIBCPP_INLINE_VISIBILITY complex& operator/=(const complex<_Xp>& __c)
314 *this = *this / complex(__c.real(), __c.imag());
319 template<> class _LIBCPP_TEMPLATE_VIS complex<double>;
320 template<> class _LIBCPP_TEMPLATE_VIS complex<long double>;
323 class _LIBCPP_TEMPLATE_VIS complex<float>
328 typedef float value_type;
330 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR complex(float __re = 0.0f, float __im = 0.0f)
331 : __re_(__re), __im_(__im) {}
332 _LIBCPP_INLINE_VISIBILITY
333 explicit _LIBCPP_CONSTEXPR complex(const complex<double>& __c);
334 _LIBCPP_INLINE_VISIBILITY
335 explicit _LIBCPP_CONSTEXPR complex(const complex<long double>& __c);
337 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR float real() const {return __re_;}
338 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR float imag() const {return __im_;}
340 _LIBCPP_INLINE_VISIBILITY void real(value_type __re) {__re_ = __re;}
341 _LIBCPP_INLINE_VISIBILITY void imag(value_type __im) {__im_ = __im;}
343 _LIBCPP_INLINE_VISIBILITY complex& operator= (float __re)
344 {__re_ = __re; __im_ = value_type(); return *this;}
345 _LIBCPP_INLINE_VISIBILITY complex& operator+=(float __re) {__re_ += __re; return *this;}
346 _LIBCPP_INLINE_VISIBILITY complex& operator-=(float __re) {__re_ -= __re; return *this;}
347 _LIBCPP_INLINE_VISIBILITY complex& operator*=(float __re) {__re_ *= __re; __im_ *= __re; return *this;}
348 _LIBCPP_INLINE_VISIBILITY complex& operator/=(float __re) {__re_ /= __re; __im_ /= __re; return *this;}
350 template<class _Xp> _LIBCPP_INLINE_VISIBILITY complex& operator= (const complex<_Xp>& __c)
356 template<class _Xp> _LIBCPP_INLINE_VISIBILITY complex& operator+=(const complex<_Xp>& __c)
362 template<class _Xp> _LIBCPP_INLINE_VISIBILITY complex& operator-=(const complex<_Xp>& __c)
368 template<class _Xp> _LIBCPP_INLINE_VISIBILITY complex& operator*=(const complex<_Xp>& __c)
370 *this = *this * complex(__c.real(), __c.imag());
373 template<class _Xp> _LIBCPP_INLINE_VISIBILITY complex& operator/=(const complex<_Xp>& __c)
375 *this = *this / complex(__c.real(), __c.imag());
381 class _LIBCPP_TEMPLATE_VIS complex<double>
386 typedef double value_type;
388 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR complex(double __re = 0.0, double __im = 0.0)
389 : __re_(__re), __im_(__im) {}
390 _LIBCPP_INLINE_VISIBILITY
391 _LIBCPP_CONSTEXPR complex(const complex<float>& __c);
392 _LIBCPP_INLINE_VISIBILITY
393 explicit _LIBCPP_CONSTEXPR complex(const complex<long double>& __c);
395 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR double real() const {return __re_;}
396 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR double imag() const {return __im_;}
398 _LIBCPP_INLINE_VISIBILITY void real(value_type __re) {__re_ = __re;}
399 _LIBCPP_INLINE_VISIBILITY void imag(value_type __im) {__im_ = __im;}
401 _LIBCPP_INLINE_VISIBILITY complex& operator= (double __re)
402 {__re_ = __re; __im_ = value_type(); return *this;}
403 _LIBCPP_INLINE_VISIBILITY complex& operator+=(double __re) {__re_ += __re; return *this;}
404 _LIBCPP_INLINE_VISIBILITY complex& operator-=(double __re) {__re_ -= __re; return *this;}
405 _LIBCPP_INLINE_VISIBILITY complex& operator*=(double __re) {__re_ *= __re; __im_ *= __re; return *this;}
406 _LIBCPP_INLINE_VISIBILITY complex& operator/=(double __re) {__re_ /= __re; __im_ /= __re; return *this;}
408 template<class _Xp> _LIBCPP_INLINE_VISIBILITY complex& operator= (const complex<_Xp>& __c)
414 template<class _Xp> _LIBCPP_INLINE_VISIBILITY complex& operator+=(const complex<_Xp>& __c)
420 template<class _Xp> _LIBCPP_INLINE_VISIBILITY complex& operator-=(const complex<_Xp>& __c)
426 template<class _Xp> _LIBCPP_INLINE_VISIBILITY complex& operator*=(const complex<_Xp>& __c)
428 *this = *this * complex(__c.real(), __c.imag());
431 template<class _Xp> _LIBCPP_INLINE_VISIBILITY complex& operator/=(const complex<_Xp>& __c)
433 *this = *this / complex(__c.real(), __c.imag());
439 class _LIBCPP_TEMPLATE_VIS complex<long double>
444 typedef long double value_type;
446 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR complex(long double __re = 0.0L, long double __im = 0.0L)
447 : __re_(__re), __im_(__im) {}
448 _LIBCPP_INLINE_VISIBILITY
449 _LIBCPP_CONSTEXPR complex(const complex<float>& __c);
450 _LIBCPP_INLINE_VISIBILITY
451 _LIBCPP_CONSTEXPR complex(const complex<double>& __c);
453 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR long double real() const {return __re_;}
454 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR long double imag() const {return __im_;}
456 _LIBCPP_INLINE_VISIBILITY void real(value_type __re) {__re_ = __re;}
457 _LIBCPP_INLINE_VISIBILITY void imag(value_type __im) {__im_ = __im;}
459 _LIBCPP_INLINE_VISIBILITY complex& operator= (long double __re)
460 {__re_ = __re; __im_ = value_type(); return *this;}
461 _LIBCPP_INLINE_VISIBILITY complex& operator+=(long double __re) {__re_ += __re; 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; __im_ *= __re; return *this;}
464 _LIBCPP_INLINE_VISIBILITY complex& operator/=(long double __re) {__re_ /= __re; __im_ /= __re; return *this;}
466 template<class _Xp> _LIBCPP_INLINE_VISIBILITY complex& operator= (const complex<_Xp>& __c)
472 template<class _Xp> _LIBCPP_INLINE_VISIBILITY complex& operator+=(const complex<_Xp>& __c)
478 template<class _Xp> _LIBCPP_INLINE_VISIBILITY complex& operator-=(const complex<_Xp>& __c)
484 template<class _Xp> _LIBCPP_INLINE_VISIBILITY complex& operator*=(const complex<_Xp>& __c)
486 *this = *this * complex(__c.real(), __c.imag());
489 template<class _Xp> _LIBCPP_INLINE_VISIBILITY complex& operator/=(const complex<_Xp>& __c)
491 *this = *this / complex(__c.real(), __c.imag());
498 complex<float>::complex(const complex<double>& __c)
499 : __re_(__c.real()), __im_(__c.imag()) {}
503 complex<float>::complex(const complex<long double>& __c)
504 : __re_(__c.real()), __im_(__c.imag()) {}
508 complex<double>::complex(const complex<float>& __c)
509 : __re_(__c.real()), __im_(__c.imag()) {}
513 complex<double>::complex(const complex<long double>& __c)
514 : __re_(__c.real()), __im_(__c.imag()) {}
518 complex<long double>::complex(const complex<float>& __c)
519 : __re_(__c.real()), __im_(__c.imag()) {}
523 complex<long double>::complex(const complex<double>& __c)
524 : __re_(__c.real()), __im_(__c.imag()) {}
529 inline _LIBCPP_INLINE_VISIBILITY
531 operator+(const complex<_Tp>& __x, const complex<_Tp>& __y)
533 complex<_Tp> __t(__x);
539 inline _LIBCPP_INLINE_VISIBILITY
541 operator+(const complex<_Tp>& __x, const _Tp& __y)
543 complex<_Tp> __t(__x);
549 inline _LIBCPP_INLINE_VISIBILITY
551 operator+(const _Tp& __x, const complex<_Tp>& __y)
553 complex<_Tp> __t(__y);
559 inline _LIBCPP_INLINE_VISIBILITY
561 operator-(const complex<_Tp>& __x, const complex<_Tp>& __y)
563 complex<_Tp> __t(__x);
569 inline _LIBCPP_INLINE_VISIBILITY
571 operator-(const complex<_Tp>& __x, const _Tp& __y)
573 complex<_Tp> __t(__x);
579 inline _LIBCPP_INLINE_VISIBILITY
581 operator-(const _Tp& __x, const complex<_Tp>& __y)
583 complex<_Tp> __t(-__y);
590 operator*(const complex<_Tp>& __z, const complex<_Tp>& __w)
592 _Tp __a = __z.real();
593 _Tp __b = __z.imag();
594 _Tp __c = __w.real();
595 _Tp __d = __w.imag();
596 _Tp __ac = __a * __c;
597 _Tp __bd = __b * __d;
598 _Tp __ad = __a * __d;
599 _Tp __bc = __b * __c;
600 _Tp __x = __ac - __bd;
601 _Tp __y = __ad + __bc;
602 if (__libcpp_isnan_or_builtin(__x) && __libcpp_isnan_or_builtin(__y))
604 bool __recalc = false;
605 if (__libcpp_isinf_or_builtin(__a) || __libcpp_isinf_or_builtin(__b))
607 __a = copysign(__libcpp_isinf_or_builtin(__a) ? _Tp(1) : _Tp(0), __a);
608 __b = copysign(__libcpp_isinf_or_builtin(__b) ? _Tp(1) : _Tp(0), __b);
609 if (__libcpp_isnan_or_builtin(__c))
610 __c = copysign(_Tp(0), __c);
611 if (__libcpp_isnan_or_builtin(__d))
612 __d = copysign(_Tp(0), __d);
615 if (__libcpp_isinf_or_builtin(__c) || __libcpp_isinf_or_builtin(__d))
617 __c = copysign(__libcpp_isinf_or_builtin(__c) ? _Tp(1) : _Tp(0), __c);
618 __d = copysign(__libcpp_isinf_or_builtin(__d) ? _Tp(1) : _Tp(0), __d);
619 if (__libcpp_isnan_or_builtin(__a))
620 __a = copysign(_Tp(0), __a);
621 if (__libcpp_isnan_or_builtin(__b))
622 __b = copysign(_Tp(0), __b);
625 if (!__recalc && (__libcpp_isinf_or_builtin(__ac) || __libcpp_isinf_or_builtin(__bd) ||
626 __libcpp_isinf_or_builtin(__ad) || __libcpp_isinf_or_builtin(__bc)))
628 if (__libcpp_isnan_or_builtin(__a))
629 __a = copysign(_Tp(0), __a);
630 if (__libcpp_isnan_or_builtin(__b))
631 __b = copysign(_Tp(0), __b);
632 if (__libcpp_isnan_or_builtin(__c))
633 __c = copysign(_Tp(0), __c);
634 if (__libcpp_isnan_or_builtin(__d))
635 __d = copysign(_Tp(0), __d);
640 __x = _Tp(INFINITY) * (__a * __c - __b * __d);
641 __y = _Tp(INFINITY) * (__a * __d + __b * __c);
644 return complex<_Tp>(__x, __y);
648 inline _LIBCPP_INLINE_VISIBILITY
650 operator*(const complex<_Tp>& __x, const _Tp& __y)
652 complex<_Tp> __t(__x);
658 inline _LIBCPP_INLINE_VISIBILITY
660 operator*(const _Tp& __x, const complex<_Tp>& __y)
662 complex<_Tp> __t(__y);
669 operator/(const complex<_Tp>& __z, const complex<_Tp>& __w)
672 _Tp __a = __z.real();
673 _Tp __b = __z.imag();
674 _Tp __c = __w.real();
675 _Tp __d = __w.imag();
676 _Tp __logbw = logb(fmax(fabs(__c), fabs(__d)));
677 if (__libcpp_isfinite_or_builtin(__logbw))
679 __ilogbw = static_cast<int>(__logbw);
680 __c = scalbn(__c, -__ilogbw);
681 __d = scalbn(__d, -__ilogbw);
683 _Tp __denom = __c * __c + __d * __d;
684 _Tp __x = scalbn((__a * __c + __b * __d) / __denom, -__ilogbw);
685 _Tp __y = scalbn((__b * __c - __a * __d) / __denom, -__ilogbw);
686 if (__libcpp_isnan_or_builtin(__x) && __libcpp_isnan_or_builtin(__y))
688 if ((__denom == _Tp(0)) && (!__libcpp_isnan_or_builtin(__a) || !__libcpp_isnan_or_builtin(__b)))
690 __x = copysign(_Tp(INFINITY), __c) * __a;
691 __y = copysign(_Tp(INFINITY), __c) * __b;
693 else if ((__libcpp_isinf_or_builtin(__a) || __libcpp_isinf_or_builtin(__b)) && __libcpp_isfinite_or_builtin(__c) && __libcpp_isfinite_or_builtin(__d))
695 __a = copysign(__libcpp_isinf_or_builtin(__a) ? _Tp(1) : _Tp(0), __a);
696 __b = copysign(__libcpp_isinf_or_builtin(__b) ? _Tp(1) : _Tp(0), __b);
697 __x = _Tp(INFINITY) * (__a * __c + __b * __d);
698 __y = _Tp(INFINITY) * (__b * __c - __a * __d);
700 else if (__libcpp_isinf_or_builtin(__logbw) && __logbw > _Tp(0) && __libcpp_isfinite_or_builtin(__a) && __libcpp_isfinite_or_builtin(__b))
702 __c = copysign(__libcpp_isinf_or_builtin(__c) ? _Tp(1) : _Tp(0), __c);
703 __d = copysign(__libcpp_isinf_or_builtin(__d) ? _Tp(1) : _Tp(0), __d);
704 __x = _Tp(0) * (__a * __c + __b * __d);
705 __y = _Tp(0) * (__b * __c - __a * __d);
708 return complex<_Tp>(__x, __y);
712 inline _LIBCPP_INLINE_VISIBILITY
714 operator/(const complex<_Tp>& __x, const _Tp& __y)
716 return complex<_Tp>(__x.real() / __y, __x.imag() / __y);
720 inline _LIBCPP_INLINE_VISIBILITY
722 operator/(const _Tp& __x, const complex<_Tp>& __y)
724 complex<_Tp> __t(__x);
730 inline _LIBCPP_INLINE_VISIBILITY
732 operator+(const complex<_Tp>& __x)
738 inline _LIBCPP_INLINE_VISIBILITY
740 operator-(const complex<_Tp>& __x)
742 return complex<_Tp>(-__x.real(), -__x.imag());
746 inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
748 operator==(const complex<_Tp>& __x, const complex<_Tp>& __y)
750 return __x.real() == __y.real() && __x.imag() == __y.imag();
754 inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
756 operator==(const complex<_Tp>& __x, const _Tp& __y)
758 return __x.real() == __y && __x.imag() == 0;
762 inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
764 operator==(const _Tp& __x, const complex<_Tp>& __y)
766 return __x == __y.real() && 0 == __y.imag();
770 inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
772 operator!=(const complex<_Tp>& __x, const complex<_Tp>& __y)
774 return !(__x == __y);
778 inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
780 operator!=(const complex<_Tp>& __x, const _Tp& __y)
782 return !(__x == __y);
786 inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
788 operator!=(const _Tp& __x, const complex<_Tp>& __y)
790 return !(__x == __y);
795 template <class _Tp, bool = is_integral<_Tp>::value,
796 bool = is_floating_point<_Tp>::value
798 struct __libcpp_complex_overload_traits {};
802 struct __libcpp_complex_overload_traits<_Tp, true, false>
804 typedef double _ValueType;
805 typedef complex<double> _ComplexType;
808 // Floating point types
810 struct __libcpp_complex_overload_traits<_Tp, false, true>
812 typedef _Tp _ValueType;
813 typedef complex<_Tp> _ComplexType;
819 inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
821 real(const complex<_Tp>& __c)
827 inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
828 typename __libcpp_complex_overload_traits<_Tp>::_ValueType
837 inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
839 imag(const complex<_Tp>& __c)
845 inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
846 typename __libcpp_complex_overload_traits<_Tp>::_ValueType
855 inline _LIBCPP_INLINE_VISIBILITY
857 abs(const complex<_Tp>& __c)
859 return hypot(__c.real(), __c.imag());
865 inline _LIBCPP_INLINE_VISIBILITY
867 arg(const complex<_Tp>& __c)
869 return atan2(__c.imag(), __c.real());
873 inline _LIBCPP_INLINE_VISIBILITY
875 is_same<_Tp, long double>::value,
880 return atan2l(0.L, __re);
884 inline _LIBCPP_INLINE_VISIBILITY
887 is_integral<_Tp>::value || is_same<_Tp, double>::value,
892 return atan2(0., __re);
896 inline _LIBCPP_INLINE_VISIBILITY
898 is_same<_Tp, float>::value,
903 return atan2f(0.F, __re);
909 inline _LIBCPP_INLINE_VISIBILITY
911 norm(const complex<_Tp>& __c)
913 if (__libcpp_isinf_or_builtin(__c.real()))
914 return abs(__c.real());
915 if (__libcpp_isinf_or_builtin(__c.imag()))
916 return abs(__c.imag());
917 return __c.real() * __c.real() + __c.imag() * __c.imag();
921 inline _LIBCPP_INLINE_VISIBILITY
922 typename __libcpp_complex_overload_traits<_Tp>::_ValueType
925 typedef typename __libcpp_complex_overload_traits<_Tp>::_ValueType _ValueType;
926 return static_cast<_ValueType>(__re) * __re;
932 inline _LIBCPP_INLINE_VISIBILITY
934 conj(const complex<_Tp>& __c)
936 return complex<_Tp>(__c.real(), -__c.imag());
940 inline _LIBCPP_INLINE_VISIBILITY
941 typename __libcpp_complex_overload_traits<_Tp>::_ComplexType
944 typedef typename __libcpp_complex_overload_traits<_Tp>::_ComplexType _ComplexType;
945 return _ComplexType(__re);
953 inline _LIBCPP_INLINE_VISIBILITY
955 proj(const complex<_Tp>& __c)
957 std::complex<_Tp> __r = __c;
958 if (__libcpp_isinf_or_builtin(__c.real()) || __libcpp_isinf_or_builtin(__c.imag()))
959 __r = complex<_Tp>(INFINITY, copysign(_Tp(0), __c.imag()));
964 inline _LIBCPP_INLINE_VISIBILITY
967 is_floating_point<_Tp>::value,
968 typename __libcpp_complex_overload_traits<_Tp>::_ComplexType
972 if (__libcpp_isinf_or_builtin(__re))
974 return complex<_Tp>(__re);
978 inline _LIBCPP_INLINE_VISIBILITY
981 is_integral<_Tp>::value,
982 typename __libcpp_complex_overload_traits<_Tp>::_ComplexType
986 typedef typename __libcpp_complex_overload_traits<_Tp>::_ComplexType _ComplexType;
987 return _ComplexType(__re);
994 polar(const _Tp& __rho, const _Tp& __theta = _Tp())
996 if (__libcpp_isnan_or_builtin(__rho) || signbit(__rho))
997 return complex<_Tp>(_Tp(NAN), _Tp(NAN));
998 if (__libcpp_isnan_or_builtin(__theta))
1000 if (__libcpp_isinf_or_builtin(__rho))
1001 return complex<_Tp>(__rho, __theta);
1002 return complex<_Tp>(__theta, __theta);
1004 if (__libcpp_isinf_or_builtin(__theta))
1006 if (__libcpp_isinf_or_builtin(__rho))
1007 return complex<_Tp>(__rho, _Tp(NAN));
1008 return complex<_Tp>(_Tp(NAN), _Tp(NAN));
1010 _Tp __x = __rho * cos(__theta);
1011 if (__libcpp_isnan_or_builtin(__x))
1013 _Tp __y = __rho * sin(__theta);
1014 if (__libcpp_isnan_or_builtin(__y))
1016 return complex<_Tp>(__x, __y);
1022 inline _LIBCPP_INLINE_VISIBILITY
1024 log(const complex<_Tp>& __x)
1026 return complex<_Tp>(log(abs(__x)), arg(__x));
1032 inline _LIBCPP_INLINE_VISIBILITY
1034 log10(const complex<_Tp>& __x)
1036 return log(__x) / log(_Tp(10));
1043 sqrt(const complex<_Tp>& __x)
1045 if (__libcpp_isinf_or_builtin(__x.imag()))
1046 return complex<_Tp>(_Tp(INFINITY), __x.imag());
1047 if (__libcpp_isinf_or_builtin(__x.real()))
1049 if (__x.real() > _Tp(0))
1050 return complex<_Tp>(__x.real(), __libcpp_isnan_or_builtin(__x.imag()) ? __x.imag() : copysign(_Tp(0), __x.imag()));
1051 return complex<_Tp>(__libcpp_isnan_or_builtin(__x.imag()) ? __x.imag() : _Tp(0), copysign(__x.real(), __x.imag()));
1053 return polar(sqrt(abs(__x)), arg(__x) / _Tp(2));
1060 exp(const complex<_Tp>& __x)
1062 _Tp __i = __x.imag();
1063 if (__libcpp_isinf_or_builtin(__x.real()))
1065 if (__x.real() < _Tp(0))
1067 if (!__libcpp_isfinite_or_builtin(__i))
1070 else if (__i == 0 || !__libcpp_isfinite_or_builtin(__i))
1072 if (__libcpp_isinf_or_builtin(__i))
1074 return complex<_Tp>(__x.real(), __i);
1077 else if (__libcpp_isnan_or_builtin(__x.real()) && __x.imag() == 0)
1079 _Tp __e = exp(__x.real());
1080 return complex<_Tp>(__e * cos(__i), __e * sin(__i));
1086 inline _LIBCPP_INLINE_VISIBILITY
1088 pow(const complex<_Tp>& __x, const complex<_Tp>& __y)
1090 return exp(__y * log(__x));
1093 template<class _Tp, class _Up>
1094 inline _LIBCPP_INLINE_VISIBILITY
1095 complex<typename __promote<_Tp, _Up>::type>
1096 pow(const complex<_Tp>& __x, const complex<_Up>& __y)
1098 typedef complex<typename __promote<_Tp, _Up>::type> result_type;
1099 return _VSTD::pow(result_type(__x), result_type(__y));
1102 template<class _Tp, class _Up>
1103 inline _LIBCPP_INLINE_VISIBILITY
1106 is_arithmetic<_Up>::value,
1107 complex<typename __promote<_Tp, _Up>::type>
1109 pow(const complex<_Tp>& __x, const _Up& __y)
1111 typedef complex<typename __promote<_Tp, _Up>::type> result_type;
1112 return _VSTD::pow(result_type(__x), result_type(__y));
1115 template<class _Tp, class _Up>
1116 inline _LIBCPP_INLINE_VISIBILITY
1119 is_arithmetic<_Tp>::value,
1120 complex<typename __promote<_Tp, _Up>::type>
1122 pow(const _Tp& __x, const complex<_Up>& __y)
1124 typedef complex<typename __promote<_Tp, _Up>::type> result_type;
1125 return _VSTD::pow(result_type(__x), result_type(__y));
1128 // __sqr, computes pow(x, 2)
1131 inline _LIBCPP_INLINE_VISIBILITY
1133 __sqr(const complex<_Tp>& __x)
1135 return complex<_Tp>((__x.real() - __x.imag()) * (__x.real() + __x.imag()),
1136 _Tp(2) * __x.real() * __x.imag());
1143 asinh(const complex<_Tp>& __x)
1145 const _Tp __pi(atan2(+0., -0.));
1146 if (__libcpp_isinf_or_builtin(__x.real()))
1148 if (__libcpp_isnan_or_builtin(__x.imag()))
1150 if (__libcpp_isinf_or_builtin(__x.imag()))
1151 return complex<_Tp>(__x.real(), copysign(__pi * _Tp(0.25), __x.imag()));
1152 return complex<_Tp>(__x.real(), copysign(_Tp(0), __x.imag()));
1154 if (__libcpp_isnan_or_builtin(__x.real()))
1156 if (__libcpp_isinf_or_builtin(__x.imag()))
1157 return complex<_Tp>(__x.imag(), __x.real());
1158 if (__x.imag() == 0)
1160 return complex<_Tp>(__x.real(), __x.real());
1162 if (__libcpp_isinf_or_builtin(__x.imag()))
1163 return complex<_Tp>(copysign(__x.imag(), __x.real()), copysign(__pi/_Tp(2), __x.imag()));
1164 complex<_Tp> __z = log(__x + sqrt(__sqr(__x) + _Tp(1)));
1165 return complex<_Tp>(copysign(__z.real(), __x.real()), copysign(__z.imag(), __x.imag()));
1172 acosh(const complex<_Tp>& __x)
1174 const _Tp __pi(atan2(+0., -0.));
1175 if (__libcpp_isinf_or_builtin(__x.real()))
1177 if (__libcpp_isnan_or_builtin(__x.imag()))
1178 return complex<_Tp>(abs(__x.real()), __x.imag());
1179 if (__libcpp_isinf_or_builtin(__x.imag()))
1182 return complex<_Tp>(__x.real(), copysign(__pi * _Tp(0.25), __x.imag()));
1184 return complex<_Tp>(-__x.real(), copysign(__pi * _Tp(0.75), __x.imag()));
1187 return complex<_Tp>(-__x.real(), copysign(__pi, __x.imag()));
1188 return complex<_Tp>(__x.real(), copysign(_Tp(0), __x.imag()));
1190 if (__libcpp_isnan_or_builtin(__x.real()))
1192 if (__libcpp_isinf_or_builtin(__x.imag()))
1193 return complex<_Tp>(abs(__x.imag()), __x.real());
1194 return complex<_Tp>(__x.real(), __x.real());
1196 if (__libcpp_isinf_or_builtin(__x.imag()))
1197 return complex<_Tp>(abs(__x.imag()), copysign(__pi/_Tp(2), __x.imag()));
1198 complex<_Tp> __z = log(__x + sqrt(__sqr(__x) - _Tp(1)));
1199 return complex<_Tp>(copysign(__z.real(), _Tp(0)), copysign(__z.imag(), __x.imag()));
1206 atanh(const complex<_Tp>& __x)
1208 const _Tp __pi(atan2(+0., -0.));
1209 if (__libcpp_isinf_or_builtin(__x.imag()))
1211 return complex<_Tp>(copysign(_Tp(0), __x.real()), copysign(__pi/_Tp(2), __x.imag()));
1213 if (__libcpp_isnan_or_builtin(__x.imag()))
1215 if (__libcpp_isinf_or_builtin(__x.real()) || __x.real() == 0)
1216 return complex<_Tp>(copysign(_Tp(0), __x.real()), __x.imag());
1217 return complex<_Tp>(__x.imag(), __x.imag());
1219 if (__libcpp_isnan_or_builtin(__x.real()))
1221 return complex<_Tp>(__x.real(), __x.real());
1223 if (__libcpp_isinf_or_builtin(__x.real()))
1225 return complex<_Tp>(copysign(_Tp(0), __x.real()), copysign(__pi/_Tp(2), __x.imag()));
1227 if (abs(__x.real()) == _Tp(1) && __x.imag() == _Tp(0))
1229 return complex<_Tp>(copysign(_Tp(INFINITY), __x.real()), copysign(_Tp(0), __x.imag()));
1231 complex<_Tp> __z = log((_Tp(1) + __x) / (_Tp(1) - __x)) / _Tp(2);
1232 return complex<_Tp>(copysign(__z.real(), __x.real()), copysign(__z.imag(), __x.imag()));
1239 sinh(const complex<_Tp>& __x)
1241 if (__libcpp_isinf_or_builtin(__x.real()) && !__libcpp_isfinite_or_builtin(__x.imag()))
1242 return complex<_Tp>(__x.real(), _Tp(NAN));
1243 if (__x.real() == 0 && !__libcpp_isfinite_or_builtin(__x.imag()))
1244 return complex<_Tp>(__x.real(), _Tp(NAN));
1245 if (__x.imag() == 0 && !__libcpp_isfinite_or_builtin(__x.real()))
1247 return complex<_Tp>(sinh(__x.real()) * cos(__x.imag()), cosh(__x.real()) * sin(__x.imag()));
1254 cosh(const complex<_Tp>& __x)
1256 if (__libcpp_isinf_or_builtin(__x.real()) && !__libcpp_isfinite_or_builtin(__x.imag()))
1257 return complex<_Tp>(abs(__x.real()), _Tp(NAN));
1258 if (__x.real() == 0 && !__libcpp_isfinite_or_builtin(__x.imag()))
1259 return complex<_Tp>(_Tp(NAN), __x.real());
1260 if (__x.real() == 0 && __x.imag() == 0)
1261 return complex<_Tp>(_Tp(1), __x.imag());
1262 if (__x.imag() == 0 && !__libcpp_isfinite_or_builtin(__x.real()))
1263 return complex<_Tp>(abs(__x.real()), __x.imag());
1264 return complex<_Tp>(cosh(__x.real()) * cos(__x.imag()), sinh(__x.real()) * sin(__x.imag()));
1271 tanh(const complex<_Tp>& __x)
1273 if (__libcpp_isinf_or_builtin(__x.real()))
1275 if (!__libcpp_isfinite_or_builtin(__x.imag()))
1276 return complex<_Tp>(_Tp(1), _Tp(0));
1277 return complex<_Tp>(_Tp(1), copysign(_Tp(0), sin(_Tp(2) * __x.imag())));
1279 if (__libcpp_isnan_or_builtin(__x.real()) && __x.imag() == 0)
1281 _Tp __2r(_Tp(2) * __x.real());
1282 _Tp __2i(_Tp(2) * __x.imag());
1283 _Tp __d(cosh(__2r) + cos(__2i));
1284 _Tp __2rsh(sinh(__2r));
1285 if (__libcpp_isinf_or_builtin(__2rsh) && __libcpp_isinf_or_builtin(__d))
1286 return complex<_Tp>(__2rsh > _Tp(0) ? _Tp(1) : _Tp(-1),
1287 __2i > _Tp(0) ? _Tp(0) : _Tp(-0.));
1288 return complex<_Tp>(__2rsh/__d, sin(__2i)/__d);
1295 asin(const complex<_Tp>& __x)
1297 complex<_Tp> __z = asinh(complex<_Tp>(-__x.imag(), __x.real()));
1298 return complex<_Tp>(__z.imag(), -__z.real());
1305 acos(const complex<_Tp>& __x)
1307 const _Tp __pi(atan2(+0., -0.));
1308 if (__libcpp_isinf_or_builtin(__x.real()))
1310 if (__libcpp_isnan_or_builtin(__x.imag()))
1311 return complex<_Tp>(__x.imag(), __x.real());
1312 if (__libcpp_isinf_or_builtin(__x.imag()))
1314 if (__x.real() < _Tp(0))
1315 return complex<_Tp>(_Tp(0.75) * __pi, -__x.imag());
1316 return complex<_Tp>(_Tp(0.25) * __pi, -__x.imag());
1318 if (__x.real() < _Tp(0))
1319 return complex<_Tp>(__pi, signbit(__x.imag()) ? -__x.real() : __x.real());
1320 return complex<_Tp>(_Tp(0), signbit(__x.imag()) ? __x.real() : -__x.real());
1322 if (__libcpp_isnan_or_builtin(__x.real()))
1324 if (__libcpp_isinf_or_builtin(__x.imag()))
1325 return complex<_Tp>(__x.real(), -__x.imag());
1326 return complex<_Tp>(__x.real(), __x.real());
1328 if (__libcpp_isinf_or_builtin(__x.imag()))
1329 return complex<_Tp>(__pi/_Tp(2), -__x.imag());
1330 if (__x.real() == 0 && (__x.imag() == 0 || isnan(__x.imag())))
1331 return complex<_Tp>(__pi/_Tp(2), -__x.imag());
1332 complex<_Tp> __z = log(__x + sqrt(__sqr(__x) - _Tp(1)));
1333 if (signbit(__x.imag()))
1334 return complex<_Tp>(abs(__z.imag()), abs(__z.real()));
1335 return complex<_Tp>(abs(__z.imag()), -abs(__z.real()));
1342 atan(const complex<_Tp>& __x)
1344 complex<_Tp> __z = atanh(complex<_Tp>(-__x.imag(), __x.real()));
1345 return complex<_Tp>(__z.imag(), -__z.real());
1352 sin(const complex<_Tp>& __x)
1354 complex<_Tp> __z = sinh(complex<_Tp>(-__x.imag(), __x.real()));
1355 return complex<_Tp>(__z.imag(), -__z.real());
1361 inline _LIBCPP_INLINE_VISIBILITY
1363 cos(const complex<_Tp>& __x)
1365 return cosh(complex<_Tp>(-__x.imag(), __x.real()));
1372 tan(const complex<_Tp>& __x)
1374 complex<_Tp> __z = tanh(complex<_Tp>(-__x.imag(), __x.real()));
1375 return complex<_Tp>(__z.imag(), -__z.real());
1378 template<class _Tp, class _CharT, class _Traits>
1379 basic_istream<_CharT, _Traits>&
1380 operator>>(basic_istream<_CharT, _Traits>& __is, complex<_Tp>& __x)
1385 if (__is.peek() == _CharT('('))
1393 _CharT __c = __is.peek();
1394 if (__c == _CharT(','))
1403 if (__c == _CharT(')'))
1406 __x = complex<_Tp>(__r, __i);
1409 __is.setstate(ios_base::failbit);
1412 __is.setstate(ios_base::failbit);
1414 else if (__c == _CharT(')'))
1417 __x = complex<_Tp>(__r, _Tp(0));
1420 __is.setstate(ios_base::failbit);
1423 __is.setstate(ios_base::failbit);
1430 __x = complex<_Tp>(__r, _Tp(0));
1432 __is.setstate(ios_base::failbit);
1436 __is.setstate(ios_base::failbit);
1440 template<class _Tp, class _CharT, class _Traits>
1441 basic_ostream<_CharT, _Traits>&
1442 operator<<(basic_ostream<_CharT, _Traits>& __os, const complex<_Tp>& __x)
1444 basic_ostringstream<_CharT, _Traits> __s;
1445 __s.flags(__os.flags());
1446 __s.imbue(__os.getloc());
1447 __s.precision(__os.precision());
1448 __s << '(' << __x.real() << ',' << __x.imag() << ')';
1449 return __os << __s.str();
1452 #if _LIBCPP_STD_VER > 11
1453 // Literal suffix for complex number literals [complex.literals]
1454 inline namespace literals
1456 inline namespace complex_literals
1458 constexpr complex<long double> operator""il(long double __im)
1460 return { 0.0l, __im };
1463 constexpr complex<long double> operator""il(unsigned long long __im)
1465 return { 0.0l, static_cast<long double>(__im) };
1469 constexpr complex<double> operator""i(long double __im)
1471 return { 0.0, static_cast<double>(__im) };
1474 constexpr complex<double> operator""i(unsigned long long __im)
1476 return { 0.0, static_cast<double>(__im) };
1480 constexpr complex<float> operator""if(long double __im)
1482 return { 0.0f, static_cast<float>(__im) };
1485 constexpr complex<float> operator""if(unsigned long long __im)
1487 return { 0.0f, static_cast<float>(__im) };
1493 _LIBCPP_END_NAMESPACE_STD
1495 #endif // _LIBCPP_COMPLEX