2 //===----------------------------------------------------------------------===//
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>&);
238 #include <type_traits>
241 #if !defined(_LIBCPP_HAS_NO_LOCALIZATION)
242 # include <sstream> // for std::basic_ostringstream
245 #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
246 #pragma GCC system_header
249 _LIBCPP_BEGIN_NAMESPACE_STD
251 template<class _Tp> class _LIBCPP_TEMPLATE_VIS complex;
253 template<class _Tp> complex<_Tp> operator*(const complex<_Tp>& __z, const complex<_Tp>& __w);
254 template<class _Tp> complex<_Tp> operator/(const complex<_Tp>& __x, const complex<_Tp>& __y);
257 class _LIBCPP_TEMPLATE_VIS complex
260 typedef _Tp value_type;
265 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
266 complex(const value_type& __re = value_type(), const value_type& __im = value_type())
267 : __re_(__re), __im_(__im) {}
268 template<class _Xp> _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
269 complex(const complex<_Xp>& __c)
270 : __re_(__c.real()), __im_(__c.imag()) {}
272 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11 value_type real() const {return __re_;}
273 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11 value_type imag() const {return __im_;}
275 _LIBCPP_INLINE_VISIBILITY void real(value_type __re) {__re_ = __re;}
276 _LIBCPP_INLINE_VISIBILITY void imag(value_type __im) {__im_ = __im;}
278 _LIBCPP_INLINE_VISIBILITY complex& operator= (const value_type& __re)
279 {__re_ = __re; __im_ = value_type(); return *this;}
280 _LIBCPP_INLINE_VISIBILITY complex& operator+=(const value_type& __re) {__re_ += __re; return *this;}
281 _LIBCPP_INLINE_VISIBILITY complex& operator-=(const value_type& __re) {__re_ -= __re; return *this;}
282 _LIBCPP_INLINE_VISIBILITY complex& operator*=(const value_type& __re) {__re_ *= __re; __im_ *= __re; return *this;}
283 _LIBCPP_INLINE_VISIBILITY complex& operator/=(const value_type& __re) {__re_ /= __re; __im_ /= __re; return *this;}
285 template<class _Xp> _LIBCPP_INLINE_VISIBILITY complex& operator= (const complex<_Xp>& __c)
291 template<class _Xp> _LIBCPP_INLINE_VISIBILITY complex& operator+=(const complex<_Xp>& __c)
297 template<class _Xp> _LIBCPP_INLINE_VISIBILITY complex& operator-=(const complex<_Xp>& __c)
303 template<class _Xp> _LIBCPP_INLINE_VISIBILITY complex& operator*=(const complex<_Xp>& __c)
305 *this = *this * complex(__c.real(), __c.imag());
308 template<class _Xp> _LIBCPP_INLINE_VISIBILITY complex& operator/=(const complex<_Xp>& __c)
310 *this = *this / complex(__c.real(), __c.imag());
315 template<> class _LIBCPP_TEMPLATE_VIS complex<double>;
316 template<> class _LIBCPP_TEMPLATE_VIS complex<long double>;
319 class _LIBCPP_TEMPLATE_VIS complex<float>
324 typedef float value_type;
326 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR complex(float __re = 0.0f, float __im = 0.0f)
327 : __re_(__re), __im_(__im) {}
328 _LIBCPP_INLINE_VISIBILITY
329 explicit _LIBCPP_CONSTEXPR complex(const complex<double>& __c);
330 _LIBCPP_INLINE_VISIBILITY
331 explicit _LIBCPP_CONSTEXPR complex(const complex<long double>& __c);
333 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR float real() const {return __re_;}
334 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR float imag() const {return __im_;}
336 _LIBCPP_INLINE_VISIBILITY void real(value_type __re) {__re_ = __re;}
337 _LIBCPP_INLINE_VISIBILITY void imag(value_type __im) {__im_ = __im;}
339 _LIBCPP_INLINE_VISIBILITY complex& operator= (float __re)
340 {__re_ = __re; __im_ = value_type(); return *this;}
341 _LIBCPP_INLINE_VISIBILITY complex& operator+=(float __re) {__re_ += __re; return *this;}
342 _LIBCPP_INLINE_VISIBILITY complex& operator-=(float __re) {__re_ -= __re; return *this;}
343 _LIBCPP_INLINE_VISIBILITY complex& operator*=(float __re) {__re_ *= __re; __im_ *= __re; return *this;}
344 _LIBCPP_INLINE_VISIBILITY complex& operator/=(float __re) {__re_ /= __re; __im_ /= __re; return *this;}
346 template<class _Xp> _LIBCPP_INLINE_VISIBILITY complex& operator= (const complex<_Xp>& __c)
352 template<class _Xp> _LIBCPP_INLINE_VISIBILITY complex& operator+=(const complex<_Xp>& __c)
358 template<class _Xp> _LIBCPP_INLINE_VISIBILITY complex& operator-=(const complex<_Xp>& __c)
364 template<class _Xp> _LIBCPP_INLINE_VISIBILITY complex& operator*=(const complex<_Xp>& __c)
366 *this = *this * complex(__c.real(), __c.imag());
369 template<class _Xp> _LIBCPP_INLINE_VISIBILITY complex& operator/=(const complex<_Xp>& __c)
371 *this = *this / complex(__c.real(), __c.imag());
377 class _LIBCPP_TEMPLATE_VIS complex<double>
382 typedef double value_type;
384 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR complex(double __re = 0.0, double __im = 0.0)
385 : __re_(__re), __im_(__im) {}
386 _LIBCPP_INLINE_VISIBILITY
387 _LIBCPP_CONSTEXPR complex(const complex<float>& __c);
388 _LIBCPP_INLINE_VISIBILITY
389 explicit _LIBCPP_CONSTEXPR complex(const complex<long double>& __c);
391 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR double real() const {return __re_;}
392 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR double imag() const {return __im_;}
394 _LIBCPP_INLINE_VISIBILITY void real(value_type __re) {__re_ = __re;}
395 _LIBCPP_INLINE_VISIBILITY void imag(value_type __im) {__im_ = __im;}
397 _LIBCPP_INLINE_VISIBILITY complex& operator= (double __re)
398 {__re_ = __re; __im_ = value_type(); return *this;}
399 _LIBCPP_INLINE_VISIBILITY complex& operator+=(double __re) {__re_ += __re; return *this;}
400 _LIBCPP_INLINE_VISIBILITY complex& operator-=(double __re) {__re_ -= __re; return *this;}
401 _LIBCPP_INLINE_VISIBILITY complex& operator*=(double __re) {__re_ *= __re; __im_ *= __re; return *this;}
402 _LIBCPP_INLINE_VISIBILITY complex& operator/=(double __re) {__re_ /= __re; __im_ /= __re; return *this;}
404 template<class _Xp> _LIBCPP_INLINE_VISIBILITY complex& operator= (const complex<_Xp>& __c)
410 template<class _Xp> _LIBCPP_INLINE_VISIBILITY complex& operator+=(const complex<_Xp>& __c)
416 template<class _Xp> _LIBCPP_INLINE_VISIBILITY complex& operator-=(const complex<_Xp>& __c)
422 template<class _Xp> _LIBCPP_INLINE_VISIBILITY complex& operator*=(const complex<_Xp>& __c)
424 *this = *this * complex(__c.real(), __c.imag());
427 template<class _Xp> _LIBCPP_INLINE_VISIBILITY complex& operator/=(const complex<_Xp>& __c)
429 *this = *this / complex(__c.real(), __c.imag());
435 class _LIBCPP_TEMPLATE_VIS complex<long double>
440 typedef long double value_type;
442 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR complex(long double __re = 0.0L, long double __im = 0.0L)
443 : __re_(__re), __im_(__im) {}
444 _LIBCPP_INLINE_VISIBILITY
445 _LIBCPP_CONSTEXPR complex(const complex<float>& __c);
446 _LIBCPP_INLINE_VISIBILITY
447 _LIBCPP_CONSTEXPR complex(const complex<double>& __c);
449 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR long double real() const {return __re_;}
450 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR long double imag() const {return __im_;}
452 _LIBCPP_INLINE_VISIBILITY void real(value_type __re) {__re_ = __re;}
453 _LIBCPP_INLINE_VISIBILITY void imag(value_type __im) {__im_ = __im;}
455 _LIBCPP_INLINE_VISIBILITY complex& operator= (long double __re)
456 {__re_ = __re; __im_ = value_type(); return *this;}
457 _LIBCPP_INLINE_VISIBILITY complex& operator+=(long double __re) {__re_ += __re; 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; __im_ *= __re; return *this;}
460 _LIBCPP_INLINE_VISIBILITY complex& operator/=(long double __re) {__re_ /= __re; __im_ /= __re; return *this;}
462 template<class _Xp> _LIBCPP_INLINE_VISIBILITY complex& operator= (const complex<_Xp>& __c)
468 template<class _Xp> _LIBCPP_INLINE_VISIBILITY complex& operator+=(const complex<_Xp>& __c)
474 template<class _Xp> _LIBCPP_INLINE_VISIBILITY complex& operator-=(const complex<_Xp>& __c)
480 template<class _Xp> _LIBCPP_INLINE_VISIBILITY complex& operator*=(const complex<_Xp>& __c)
482 *this = *this * complex(__c.real(), __c.imag());
485 template<class _Xp> _LIBCPP_INLINE_VISIBILITY complex& operator/=(const complex<_Xp>& __c)
487 *this = *this / complex(__c.real(), __c.imag());
494 complex<float>::complex(const complex<double>& __c)
495 : __re_(__c.real()), __im_(__c.imag()) {}
499 complex<float>::complex(const complex<long double>& __c)
500 : __re_(__c.real()), __im_(__c.imag()) {}
504 complex<double>::complex(const complex<float>& __c)
505 : __re_(__c.real()), __im_(__c.imag()) {}
509 complex<double>::complex(const complex<long double>& __c)
510 : __re_(__c.real()), __im_(__c.imag()) {}
514 complex<long double>::complex(const complex<float>& __c)
515 : __re_(__c.real()), __im_(__c.imag()) {}
519 complex<long double>::complex(const complex<double>& __c)
520 : __re_(__c.real()), __im_(__c.imag()) {}
525 inline _LIBCPP_INLINE_VISIBILITY
527 operator+(const complex<_Tp>& __x, const complex<_Tp>& __y)
529 complex<_Tp> __t(__x);
535 inline _LIBCPP_INLINE_VISIBILITY
537 operator+(const complex<_Tp>& __x, const _Tp& __y)
539 complex<_Tp> __t(__x);
545 inline _LIBCPP_INLINE_VISIBILITY
547 operator+(const _Tp& __x, const complex<_Tp>& __y)
549 complex<_Tp> __t(__y);
555 inline _LIBCPP_INLINE_VISIBILITY
557 operator-(const complex<_Tp>& __x, const complex<_Tp>& __y)
559 complex<_Tp> __t(__x);
565 inline _LIBCPP_INLINE_VISIBILITY
567 operator-(const complex<_Tp>& __x, const _Tp& __y)
569 complex<_Tp> __t(__x);
575 inline _LIBCPP_INLINE_VISIBILITY
577 operator-(const _Tp& __x, const complex<_Tp>& __y)
579 complex<_Tp> __t(-__y);
586 operator*(const complex<_Tp>& __z, const complex<_Tp>& __w)
588 _Tp __a = __z.real();
589 _Tp __b = __z.imag();
590 _Tp __c = __w.real();
591 _Tp __d = __w.imag();
592 _Tp __ac = __a * __c;
593 _Tp __bd = __b * __d;
594 _Tp __ad = __a * __d;
595 _Tp __bc = __b * __c;
596 _Tp __x = __ac - __bd;
597 _Tp __y = __ad + __bc;
598 if (__libcpp_isnan_or_builtin(__x) && __libcpp_isnan_or_builtin(__y))
600 bool __recalc = false;
601 if (__libcpp_isinf_or_builtin(__a) || __libcpp_isinf_or_builtin(__b))
603 __a = copysign(__libcpp_isinf_or_builtin(__a) ? _Tp(1) : _Tp(0), __a);
604 __b = copysign(__libcpp_isinf_or_builtin(__b) ? _Tp(1) : _Tp(0), __b);
605 if (__libcpp_isnan_or_builtin(__c))
606 __c = copysign(_Tp(0), __c);
607 if (__libcpp_isnan_or_builtin(__d))
608 __d = copysign(_Tp(0), __d);
611 if (__libcpp_isinf_or_builtin(__c) || __libcpp_isinf_or_builtin(__d))
613 __c = copysign(__libcpp_isinf_or_builtin(__c) ? _Tp(1) : _Tp(0), __c);
614 __d = copysign(__libcpp_isinf_or_builtin(__d) ? _Tp(1) : _Tp(0), __d);
615 if (__libcpp_isnan_or_builtin(__a))
616 __a = copysign(_Tp(0), __a);
617 if (__libcpp_isnan_or_builtin(__b))
618 __b = copysign(_Tp(0), __b);
621 if (!__recalc && (__libcpp_isinf_or_builtin(__ac) || __libcpp_isinf_or_builtin(__bd) ||
622 __libcpp_isinf_or_builtin(__ad) || __libcpp_isinf_or_builtin(__bc)))
624 if (__libcpp_isnan_or_builtin(__a))
625 __a = copysign(_Tp(0), __a);
626 if (__libcpp_isnan_or_builtin(__b))
627 __b = copysign(_Tp(0), __b);
628 if (__libcpp_isnan_or_builtin(__c))
629 __c = copysign(_Tp(0), __c);
630 if (__libcpp_isnan_or_builtin(__d))
631 __d = copysign(_Tp(0), __d);
636 __x = _Tp(INFINITY) * (__a * __c - __b * __d);
637 __y = _Tp(INFINITY) * (__a * __d + __b * __c);
640 return complex<_Tp>(__x, __y);
644 inline _LIBCPP_INLINE_VISIBILITY
646 operator*(const complex<_Tp>& __x, const _Tp& __y)
648 complex<_Tp> __t(__x);
654 inline _LIBCPP_INLINE_VISIBILITY
656 operator*(const _Tp& __x, const complex<_Tp>& __y)
658 complex<_Tp> __t(__y);
665 operator/(const complex<_Tp>& __z, const complex<_Tp>& __w)
668 _Tp __a = __z.real();
669 _Tp __b = __z.imag();
670 _Tp __c = __w.real();
671 _Tp __d = __w.imag();
672 _Tp __logbw = logb(fmax(fabs(__c), fabs(__d)));
673 if (__libcpp_isfinite_or_builtin(__logbw))
675 __ilogbw = static_cast<int>(__logbw);
676 __c = scalbn(__c, -__ilogbw);
677 __d = scalbn(__d, -__ilogbw);
679 _Tp __denom = __c * __c + __d * __d;
680 _Tp __x = scalbn((__a * __c + __b * __d) / __denom, -__ilogbw);
681 _Tp __y = scalbn((__b * __c - __a * __d) / __denom, -__ilogbw);
682 if (__libcpp_isnan_or_builtin(__x) && __libcpp_isnan_or_builtin(__y))
684 if ((__denom == _Tp(0)) && (!__libcpp_isnan_or_builtin(__a) || !__libcpp_isnan_or_builtin(__b)))
686 __x = copysign(_Tp(INFINITY), __c) * __a;
687 __y = copysign(_Tp(INFINITY), __c) * __b;
689 else if ((__libcpp_isinf_or_builtin(__a) || __libcpp_isinf_or_builtin(__b)) && __libcpp_isfinite_or_builtin(__c) && __libcpp_isfinite_or_builtin(__d))
691 __a = copysign(__libcpp_isinf_or_builtin(__a) ? _Tp(1) : _Tp(0), __a);
692 __b = copysign(__libcpp_isinf_or_builtin(__b) ? _Tp(1) : _Tp(0), __b);
693 __x = _Tp(INFINITY) * (__a * __c + __b * __d);
694 __y = _Tp(INFINITY) * (__b * __c - __a * __d);
696 else if (__libcpp_isinf_or_builtin(__logbw) && __logbw > _Tp(0) && __libcpp_isfinite_or_builtin(__a) && __libcpp_isfinite_or_builtin(__b))
698 __c = copysign(__libcpp_isinf_or_builtin(__c) ? _Tp(1) : _Tp(0), __c);
699 __d = copysign(__libcpp_isinf_or_builtin(__d) ? _Tp(1) : _Tp(0), __d);
700 __x = _Tp(0) * (__a * __c + __b * __d);
701 __y = _Tp(0) * (__b * __c - __a * __d);
704 return complex<_Tp>(__x, __y);
708 inline _LIBCPP_INLINE_VISIBILITY
710 operator/(const complex<_Tp>& __x, const _Tp& __y)
712 return complex<_Tp>(__x.real() / __y, __x.imag() / __y);
716 inline _LIBCPP_INLINE_VISIBILITY
718 operator/(const _Tp& __x, const complex<_Tp>& __y)
720 complex<_Tp> __t(__x);
726 inline _LIBCPP_INLINE_VISIBILITY
728 operator+(const complex<_Tp>& __x)
734 inline _LIBCPP_INLINE_VISIBILITY
736 operator-(const complex<_Tp>& __x)
738 return complex<_Tp>(-__x.real(), -__x.imag());
742 inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
744 operator==(const complex<_Tp>& __x, const complex<_Tp>& __y)
746 return __x.real() == __y.real() && __x.imag() == __y.imag();
750 inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
752 operator==(const complex<_Tp>& __x, const _Tp& __y)
754 return __x.real() == __y && __x.imag() == 0;
758 inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
760 operator==(const _Tp& __x, const complex<_Tp>& __y)
762 return __x == __y.real() && 0 == __y.imag();
766 inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
768 operator!=(const complex<_Tp>& __x, const complex<_Tp>& __y)
770 return !(__x == __y);
774 inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
776 operator!=(const complex<_Tp>& __x, const _Tp& __y)
778 return !(__x == __y);
782 inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
784 operator!=(const _Tp& __x, const complex<_Tp>& __y)
786 return !(__x == __y);
791 template <class _Tp, bool = is_integral<_Tp>::value,
792 bool = is_floating_point<_Tp>::value
794 struct __libcpp_complex_overload_traits {};
798 struct __libcpp_complex_overload_traits<_Tp, true, false>
800 typedef double _ValueType;
801 typedef complex<double> _ComplexType;
804 // Floating point types
806 struct __libcpp_complex_overload_traits<_Tp, false, true>
808 typedef _Tp _ValueType;
809 typedef complex<_Tp> _ComplexType;
815 inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
817 real(const complex<_Tp>& __c)
823 inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
824 typename __libcpp_complex_overload_traits<_Tp>::_ValueType
833 inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
835 imag(const complex<_Tp>& __c)
841 inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
842 typename __libcpp_complex_overload_traits<_Tp>::_ValueType
851 inline _LIBCPP_INLINE_VISIBILITY
853 abs(const complex<_Tp>& __c)
855 return hypot(__c.real(), __c.imag());
861 inline _LIBCPP_INLINE_VISIBILITY
863 arg(const complex<_Tp>& __c)
865 return atan2(__c.imag(), __c.real());
869 inline _LIBCPP_INLINE_VISIBILITY
871 is_same<_Tp, long double>::value,
876 return atan2l(0.L, __re);
880 inline _LIBCPP_INLINE_VISIBILITY
883 is_integral<_Tp>::value || is_same<_Tp, double>::value,
888 return atan2(0., __re);
892 inline _LIBCPP_INLINE_VISIBILITY
894 is_same<_Tp, float>::value,
899 return atan2f(0.F, __re);
905 inline _LIBCPP_INLINE_VISIBILITY
907 norm(const complex<_Tp>& __c)
909 if (__libcpp_isinf_or_builtin(__c.real()))
910 return abs(__c.real());
911 if (__libcpp_isinf_or_builtin(__c.imag()))
912 return abs(__c.imag());
913 return __c.real() * __c.real() + __c.imag() * __c.imag();
917 inline _LIBCPP_INLINE_VISIBILITY
918 typename __libcpp_complex_overload_traits<_Tp>::_ValueType
921 typedef typename __libcpp_complex_overload_traits<_Tp>::_ValueType _ValueType;
922 return static_cast<_ValueType>(__re) * __re;
928 inline _LIBCPP_INLINE_VISIBILITY
930 conj(const complex<_Tp>& __c)
932 return complex<_Tp>(__c.real(), -__c.imag());
936 inline _LIBCPP_INLINE_VISIBILITY
937 typename __libcpp_complex_overload_traits<_Tp>::_ComplexType
940 typedef typename __libcpp_complex_overload_traits<_Tp>::_ComplexType _ComplexType;
941 return _ComplexType(__re);
949 inline _LIBCPP_INLINE_VISIBILITY
951 proj(const complex<_Tp>& __c)
953 complex<_Tp> __r = __c;
954 if (__libcpp_isinf_or_builtin(__c.real()) || __libcpp_isinf_or_builtin(__c.imag()))
955 __r = complex<_Tp>(INFINITY, copysign(_Tp(0), __c.imag()));
960 inline _LIBCPP_INLINE_VISIBILITY
963 is_floating_point<_Tp>::value,
964 typename __libcpp_complex_overload_traits<_Tp>::_ComplexType
968 if (__libcpp_isinf_or_builtin(__re))
970 return complex<_Tp>(__re);
974 inline _LIBCPP_INLINE_VISIBILITY
977 is_integral<_Tp>::value,
978 typename __libcpp_complex_overload_traits<_Tp>::_ComplexType
982 typedef typename __libcpp_complex_overload_traits<_Tp>::_ComplexType _ComplexType;
983 return _ComplexType(__re);
990 polar(const _Tp& __rho, const _Tp& __theta = _Tp())
992 if (__libcpp_isnan_or_builtin(__rho) || signbit(__rho))
993 return complex<_Tp>(_Tp(NAN), _Tp(NAN));
994 if (__libcpp_isnan_or_builtin(__theta))
996 if (__libcpp_isinf_or_builtin(__rho))
997 return complex<_Tp>(__rho, __theta);
998 return complex<_Tp>(__theta, __theta);
1000 if (__libcpp_isinf_or_builtin(__theta))
1002 if (__libcpp_isinf_or_builtin(__rho))
1003 return complex<_Tp>(__rho, _Tp(NAN));
1004 return complex<_Tp>(_Tp(NAN), _Tp(NAN));
1006 _Tp __x = __rho * cos(__theta);
1007 if (__libcpp_isnan_or_builtin(__x))
1009 _Tp __y = __rho * sin(__theta);
1010 if (__libcpp_isnan_or_builtin(__y))
1012 return complex<_Tp>(__x, __y);
1018 inline _LIBCPP_INLINE_VISIBILITY
1020 log(const complex<_Tp>& __x)
1022 return complex<_Tp>(log(abs(__x)), arg(__x));
1028 inline _LIBCPP_INLINE_VISIBILITY
1030 log10(const complex<_Tp>& __x)
1032 return log(__x) / log(_Tp(10));
1039 sqrt(const complex<_Tp>& __x)
1041 if (__libcpp_isinf_or_builtin(__x.imag()))
1042 return complex<_Tp>(_Tp(INFINITY), __x.imag());
1043 if (__libcpp_isinf_or_builtin(__x.real()))
1045 if (__x.real() > _Tp(0))
1046 return complex<_Tp>(__x.real(), __libcpp_isnan_or_builtin(__x.imag()) ? __x.imag() : copysign(_Tp(0), __x.imag()));
1047 return complex<_Tp>(__libcpp_isnan_or_builtin(__x.imag()) ? __x.imag() : _Tp(0), copysign(__x.real(), __x.imag()));
1049 return polar(sqrt(abs(__x)), arg(__x) / _Tp(2));
1056 exp(const complex<_Tp>& __x)
1058 _Tp __i = __x.imag();
1060 return complex<_Tp>(exp(__x.real()), copysign(_Tp(0), __x.imag()));
1062 if (__libcpp_isinf_or_builtin(__x.real()))
1064 if (__x.real() < _Tp(0))
1066 if (!__libcpp_isfinite_or_builtin(__i))
1069 else if (__i == 0 || !__libcpp_isfinite_or_builtin(__i))
1071 if (__libcpp_isinf_or_builtin(__i))
1073 return complex<_Tp>(__x.real(), __i);
1076 _Tp __e = exp(__x.real());
1077 return complex<_Tp>(__e * cos(__i), __e * sin(__i));
1083 inline _LIBCPP_INLINE_VISIBILITY
1085 pow(const complex<_Tp>& __x, const complex<_Tp>& __y)
1087 return exp(__y * log(__x));
1090 template<class _Tp, class _Up>
1091 inline _LIBCPP_INLINE_VISIBILITY
1092 complex<typename __promote<_Tp, _Up>::type>
1093 pow(const complex<_Tp>& __x, const complex<_Up>& __y)
1095 typedef complex<typename __promote<_Tp, _Up>::type> result_type;
1096 return _VSTD::pow(result_type(__x), result_type(__y));
1099 template<class _Tp, class _Up>
1100 inline _LIBCPP_INLINE_VISIBILITY
1103 is_arithmetic<_Up>::value,
1104 complex<typename __promote<_Tp, _Up>::type>
1106 pow(const complex<_Tp>& __x, const _Up& __y)
1108 typedef complex<typename __promote<_Tp, _Up>::type> result_type;
1109 return _VSTD::pow(result_type(__x), result_type(__y));
1112 template<class _Tp, class _Up>
1113 inline _LIBCPP_INLINE_VISIBILITY
1116 is_arithmetic<_Tp>::value,
1117 complex<typename __promote<_Tp, _Up>::type>
1119 pow(const _Tp& __x, const complex<_Up>& __y)
1121 typedef complex<typename __promote<_Tp, _Up>::type> result_type;
1122 return _VSTD::pow(result_type(__x), result_type(__y));
1125 // __sqr, computes pow(x, 2)
1128 inline _LIBCPP_INLINE_VISIBILITY
1130 __sqr(const complex<_Tp>& __x)
1132 return complex<_Tp>((__x.real() - __x.imag()) * (__x.real() + __x.imag()),
1133 _Tp(2) * __x.real() * __x.imag());
1140 asinh(const complex<_Tp>& __x)
1142 const _Tp __pi(atan2(+0., -0.));
1143 if (__libcpp_isinf_or_builtin(__x.real()))
1145 if (__libcpp_isnan_or_builtin(__x.imag()))
1147 if (__libcpp_isinf_or_builtin(__x.imag()))
1148 return complex<_Tp>(__x.real(), copysign(__pi * _Tp(0.25), __x.imag()));
1149 return complex<_Tp>(__x.real(), copysign(_Tp(0), __x.imag()));
1151 if (__libcpp_isnan_or_builtin(__x.real()))
1153 if (__libcpp_isinf_or_builtin(__x.imag()))
1154 return complex<_Tp>(__x.imag(), __x.real());
1155 if (__x.imag() == 0)
1157 return complex<_Tp>(__x.real(), __x.real());
1159 if (__libcpp_isinf_or_builtin(__x.imag()))
1160 return complex<_Tp>(copysign(__x.imag(), __x.real()), copysign(__pi/_Tp(2), __x.imag()));
1161 complex<_Tp> __z = log(__x + sqrt(__sqr(__x) + _Tp(1)));
1162 return complex<_Tp>(copysign(__z.real(), __x.real()), copysign(__z.imag(), __x.imag()));
1169 acosh(const complex<_Tp>& __x)
1171 const _Tp __pi(atan2(+0., -0.));
1172 if (__libcpp_isinf_or_builtin(__x.real()))
1174 if (__libcpp_isnan_or_builtin(__x.imag()))
1175 return complex<_Tp>(abs(__x.real()), __x.imag());
1176 if (__libcpp_isinf_or_builtin(__x.imag()))
1179 return complex<_Tp>(__x.real(), copysign(__pi * _Tp(0.25), __x.imag()));
1181 return complex<_Tp>(-__x.real(), copysign(__pi * _Tp(0.75), __x.imag()));
1184 return complex<_Tp>(-__x.real(), copysign(__pi, __x.imag()));
1185 return complex<_Tp>(__x.real(), copysign(_Tp(0), __x.imag()));
1187 if (__libcpp_isnan_or_builtin(__x.real()))
1189 if (__libcpp_isinf_or_builtin(__x.imag()))
1190 return complex<_Tp>(abs(__x.imag()), __x.real());
1191 return complex<_Tp>(__x.real(), __x.real());
1193 if (__libcpp_isinf_or_builtin(__x.imag()))
1194 return complex<_Tp>(abs(__x.imag()), copysign(__pi/_Tp(2), __x.imag()));
1195 complex<_Tp> __z = log(__x + sqrt(__sqr(__x) - _Tp(1)));
1196 return complex<_Tp>(copysign(__z.real(), _Tp(0)), copysign(__z.imag(), __x.imag()));
1203 atanh(const complex<_Tp>& __x)
1205 const _Tp __pi(atan2(+0., -0.));
1206 if (__libcpp_isinf_or_builtin(__x.imag()))
1208 return complex<_Tp>(copysign(_Tp(0), __x.real()), copysign(__pi/_Tp(2), __x.imag()));
1210 if (__libcpp_isnan_or_builtin(__x.imag()))
1212 if (__libcpp_isinf_or_builtin(__x.real()) || __x.real() == 0)
1213 return complex<_Tp>(copysign(_Tp(0), __x.real()), __x.imag());
1214 return complex<_Tp>(__x.imag(), __x.imag());
1216 if (__libcpp_isnan_or_builtin(__x.real()))
1218 return complex<_Tp>(__x.real(), __x.real());
1220 if (__libcpp_isinf_or_builtin(__x.real()))
1222 return complex<_Tp>(copysign(_Tp(0), __x.real()), copysign(__pi/_Tp(2), __x.imag()));
1224 if (abs(__x.real()) == _Tp(1) && __x.imag() == _Tp(0))
1226 return complex<_Tp>(copysign(_Tp(INFINITY), __x.real()), copysign(_Tp(0), __x.imag()));
1228 complex<_Tp> __z = log((_Tp(1) + __x) / (_Tp(1) - __x)) / _Tp(2);
1229 return complex<_Tp>(copysign(__z.real(), __x.real()), copysign(__z.imag(), __x.imag()));
1236 sinh(const complex<_Tp>& __x)
1238 if (__libcpp_isinf_or_builtin(__x.real()) && !__libcpp_isfinite_or_builtin(__x.imag()))
1239 return complex<_Tp>(__x.real(), _Tp(NAN));
1240 if (__x.real() == 0 && !__libcpp_isfinite_or_builtin(__x.imag()))
1241 return complex<_Tp>(__x.real(), _Tp(NAN));
1242 if (__x.imag() == 0 && !__libcpp_isfinite_or_builtin(__x.real()))
1244 return complex<_Tp>(sinh(__x.real()) * cos(__x.imag()), cosh(__x.real()) * sin(__x.imag()));
1251 cosh(const complex<_Tp>& __x)
1253 if (__libcpp_isinf_or_builtin(__x.real()) && !__libcpp_isfinite_or_builtin(__x.imag()))
1254 return complex<_Tp>(abs(__x.real()), _Tp(NAN));
1255 if (__x.real() == 0 && !__libcpp_isfinite_or_builtin(__x.imag()))
1256 return complex<_Tp>(_Tp(NAN), __x.real());
1257 if (__x.real() == 0 && __x.imag() == 0)
1258 return complex<_Tp>(_Tp(1), __x.imag());
1259 if (__x.imag() == 0 && !__libcpp_isfinite_or_builtin(__x.real()))
1260 return complex<_Tp>(abs(__x.real()), __x.imag());
1261 return complex<_Tp>(cosh(__x.real()) * cos(__x.imag()), sinh(__x.real()) * sin(__x.imag()));
1268 tanh(const complex<_Tp>& __x)
1270 if (__libcpp_isinf_or_builtin(__x.real()))
1272 if (!__libcpp_isfinite_or_builtin(__x.imag()))
1273 return complex<_Tp>(copysign(_Tp(1), __x.real()), _Tp(0));
1274 return complex<_Tp>(copysign(_Tp(1), __x.real()), copysign(_Tp(0), sin(_Tp(2) * __x.imag())));
1276 if (__libcpp_isnan_or_builtin(__x.real()) && __x.imag() == 0)
1278 _Tp __2r(_Tp(2) * __x.real());
1279 _Tp __2i(_Tp(2) * __x.imag());
1280 _Tp __d(cosh(__2r) + cos(__2i));
1281 _Tp __2rsh(sinh(__2r));
1282 if (__libcpp_isinf_or_builtin(__2rsh) && __libcpp_isinf_or_builtin(__d))
1283 return complex<_Tp>(__2rsh > _Tp(0) ? _Tp(1) : _Tp(-1),
1284 __2i > _Tp(0) ? _Tp(0) : _Tp(-0.));
1285 return complex<_Tp>(__2rsh/__d, sin(__2i)/__d);
1292 asin(const complex<_Tp>& __x)
1294 complex<_Tp> __z = asinh(complex<_Tp>(-__x.imag(), __x.real()));
1295 return complex<_Tp>(__z.imag(), -__z.real());
1302 acos(const complex<_Tp>& __x)
1304 const _Tp __pi(atan2(+0., -0.));
1305 if (__libcpp_isinf_or_builtin(__x.real()))
1307 if (__libcpp_isnan_or_builtin(__x.imag()))
1308 return complex<_Tp>(__x.imag(), __x.real());
1309 if (__libcpp_isinf_or_builtin(__x.imag()))
1311 if (__x.real() < _Tp(0))
1312 return complex<_Tp>(_Tp(0.75) * __pi, -__x.imag());
1313 return complex<_Tp>(_Tp(0.25) * __pi, -__x.imag());
1315 if (__x.real() < _Tp(0))
1316 return complex<_Tp>(__pi, signbit(__x.imag()) ? -__x.real() : __x.real());
1317 return complex<_Tp>(_Tp(0), signbit(__x.imag()) ? __x.real() : -__x.real());
1319 if (__libcpp_isnan_or_builtin(__x.real()))
1321 if (__libcpp_isinf_or_builtin(__x.imag()))
1322 return complex<_Tp>(__x.real(), -__x.imag());
1323 return complex<_Tp>(__x.real(), __x.real());
1325 if (__libcpp_isinf_or_builtin(__x.imag()))
1326 return complex<_Tp>(__pi/_Tp(2), -__x.imag());
1327 if (__x.real() == 0 && (__x.imag() == 0 || isnan(__x.imag())))
1328 return complex<_Tp>(__pi/_Tp(2), -__x.imag());
1329 complex<_Tp> __z = log(__x + sqrt(__sqr(__x) - _Tp(1)));
1330 if (signbit(__x.imag()))
1331 return complex<_Tp>(abs(__z.imag()), abs(__z.real()));
1332 return complex<_Tp>(abs(__z.imag()), -abs(__z.real()));
1339 atan(const complex<_Tp>& __x)
1341 complex<_Tp> __z = atanh(complex<_Tp>(-__x.imag(), __x.real()));
1342 return complex<_Tp>(__z.imag(), -__z.real());
1349 sin(const complex<_Tp>& __x)
1351 complex<_Tp> __z = sinh(complex<_Tp>(-__x.imag(), __x.real()));
1352 return complex<_Tp>(__z.imag(), -__z.real());
1358 inline _LIBCPP_INLINE_VISIBILITY
1360 cos(const complex<_Tp>& __x)
1362 return cosh(complex<_Tp>(-__x.imag(), __x.real()));
1369 tan(const complex<_Tp>& __x)
1371 complex<_Tp> __z = tanh(complex<_Tp>(-__x.imag(), __x.real()));
1372 return complex<_Tp>(__z.imag(), -__z.real());
1375 template<class _Tp, class _CharT, class _Traits>
1376 basic_istream<_CharT, _Traits>&
1377 operator>>(basic_istream<_CharT, _Traits>& __is, complex<_Tp>& __x)
1382 if (__is.peek() == _CharT('('))
1390 _CharT __c = __is.peek();
1391 if (__c == _CharT(','))
1400 if (__c == _CharT(')'))
1403 __x = complex<_Tp>(__r, __i);
1406 __is.setstate(__is.failbit);
1409 __is.setstate(__is.failbit);
1411 else if (__c == _CharT(')'))
1414 __x = complex<_Tp>(__r, _Tp(0));
1417 __is.setstate(__is.failbit);
1420 __is.setstate(__is.failbit);
1427 __x = complex<_Tp>(__r, _Tp(0));
1429 __is.setstate(__is.failbit);
1433 __is.setstate(__is.failbit);
1437 #if !defined(_LIBCPP_HAS_NO_LOCALIZATION)
1438 template<class _Tp, class _CharT, class _Traits>
1439 basic_ostream<_CharT, _Traits>&
1440 operator<<(basic_ostream<_CharT, _Traits>& __os, const complex<_Tp>& __x)
1442 basic_ostringstream<_CharT, _Traits> __s;
1443 __s.flags(__os.flags());
1444 __s.imbue(__os.getloc());
1445 __s.precision(__os.precision());
1446 __s << '(' << __x.real() << ',' << __x.imag() << ')';
1447 return __os << __s.str();
1449 #endif // !_LIBCPP_HAS_NO_LOCALIZATION
1451 #if _LIBCPP_STD_VER > 11
1452 // Literal suffix for complex number literals [complex.literals]
1453 inline namespace literals
1455 inline namespace complex_literals
1457 constexpr complex<long double> operator""il(long double __im)
1459 return { 0.0l, __im };
1462 constexpr complex<long double> operator""il(unsigned long long __im)
1464 return { 0.0l, static_cast<long double>(__im) };
1468 constexpr complex<double> operator""i(long double __im)
1470 return { 0.0, static_cast<double>(__im) };
1473 constexpr complex<double> operator""i(unsigned long long __im)
1475 return { 0.0, static_cast<double>(__im) };
1479 constexpr complex<float> operator""if(long double __im)
1481 return { 0.0f, static_cast<float>(__im) };
1484 constexpr complex<float> operator""if(unsigned long long __im)
1486 return { 0.0f, static_cast<float>(__im) };
1488 } // namespace complex_literals
1489 } // namespace literals
1492 _LIBCPP_END_NAMESPACE_STD
1494 #endif // _LIBCPP_COMPLEX