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
32 void real(T); // constexpr in C++20
33 void imag(T); // constexpr in C++20
35 complex<T>& operator= (const T&); // constexpr in C++20
36 complex<T>& operator+=(const T&); // constexpr in C++20
37 complex<T>& operator-=(const T&); // constexpr in C++20
38 complex<T>& operator*=(const T&); // constexpr in C++20
39 complex<T>& operator/=(const T&); // constexpr in C++20
41 complex& operator=(const complex&); // constexpr in C++20
42 template<class X> complex<T>& operator= (const complex<X>&); // constexpr in C++20
43 template<class X> complex<T>& operator+=(const complex<X>&); // constexpr in C++20
44 template<class X> complex<T>& operator-=(const complex<X>&); // constexpr in C++20
45 template<class X> complex<T>& operator*=(const complex<X>&); // constexpr in C++20
46 template<class X> complex<T>& operator/=(const complex<X>&); // constexpr in C++20
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;
60 void real(float); // constexpr in C++20
61 constexpr float imag() const;
62 void imag(float); // constexpr in C++20
64 complex<float>& operator= (float); // constexpr in C++20
65 complex<float>& operator+=(float); // constexpr in C++20
66 complex<float>& operator-=(float); // constexpr in C++20
67 complex<float>& operator*=(float); // constexpr in C++20
68 complex<float>& operator/=(float); // constexpr in C++20
70 complex<float>& operator=(const complex<float>&); // constexpr in C++20
71 template<class X> complex<float>& operator= (const complex<X>&); // constexpr in C++20
72 template<class X> complex<float>& operator+=(const complex<X>&); // constexpr in C++20
73 template<class X> complex<float>& operator-=(const complex<X>&); // constexpr in C++20
74 template<class X> complex<float>& operator*=(const complex<X>&); // constexpr in C++20
75 template<class X> complex<float>& operator/=(const complex<X>&); // constexpr in C++20
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;
89 void real(double); // constexpr in C++20
90 constexpr double imag() const;
91 void imag(double); // constexpr in C++20
93 complex<double>& operator= (double); // constexpr in C++20
94 complex<double>& operator+=(double); // constexpr in C++20
95 complex<double>& operator-=(double); // constexpr in C++20
96 complex<double>& operator*=(double); // constexpr in C++20
97 complex<double>& operator/=(double); // constexpr in C++20
98 complex<double>& operator=(const complex<double>&); // constexpr in C++20
100 template<class X> complex<double>& operator= (const complex<X>&); // constexpr in C++20
101 template<class X> complex<double>& operator+=(const complex<X>&); // constexpr in C++20
102 template<class X> complex<double>& operator-=(const complex<X>&); // constexpr in C++20
103 template<class X> complex<double>& operator*=(const complex<X>&); // constexpr in C++20
104 template<class X> complex<double>& operator/=(const complex<X>&); // constexpr in C++20
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); // constexpr in C++20
119 constexpr long double imag() const;
120 void imag(long double); // constexpr in C++20
122 complex<long double>& operator=(const complex<long double>&); // constexpr in C++20
123 complex<long double>& operator= (long double); // constexpr in C++20
124 complex<long double>& operator+=(long double); // constexpr in C++20
125 complex<long double>& operator-=(long double); // constexpr in C++20
126 complex<long double>& operator*=(long double); // constexpr in C++20
127 complex<long double>& operator/=(long double); // constexpr in C++20
129 template<class X> complex<long double>& operator= (const complex<X>&); // constexpr in C++20
130 template<class X> complex<long double>& operator+=(const complex<X>&); // constexpr in C++20
131 template<class X> complex<long double>& operator-=(const complex<X>&); // constexpr in C++20
132 template<class X> complex<long double>& operator*=(const complex<X>&); // constexpr in C++20
133 template<class X> complex<long double>& operator/=(const complex<X>&); // constexpr in C++20
137 template<class T> complex<T> operator+(const complex<T>&, const complex<T>&); // constexpr in C++20
138 template<class T> complex<T> operator+(const complex<T>&, const T&); // constexpr in C++20
139 template<class T> complex<T> operator+(const T&, const complex<T>&); // constexpr in C++20
140 template<class T> complex<T> operator-(const complex<T>&, const complex<T>&); // constexpr in C++20
141 template<class T> complex<T> operator-(const complex<T>&, const T&); // constexpr in C++20
142 template<class T> complex<T> operator-(const T&, const complex<T>&); // constexpr in C++20
143 template<class T> complex<T> operator*(const complex<T>&, const complex<T>&); // constexpr in C++20
144 template<class T> complex<T> operator*(const complex<T>&, const T&); // constexpr in C++20
145 template<class T> complex<T> operator*(const T&, const complex<T>&); // constexpr in C++20
146 template<class T> complex<T> operator/(const complex<T>&, const complex<T>&); // constexpr in C++20
147 template<class T> complex<T> operator/(const complex<T>&, const T&); // constexpr in C++20
148 template<class T> complex<T> operator/(const T&, const complex<T>&); // constexpr in C++20
149 template<class T> complex<T> operator+(const complex<T>&); // constexpr in C++20
150 template<class T> complex<T> operator-(const complex<T>&); // constexpr in C++20
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, removed in C++20
154 template<class T> bool operator!=(const complex<T>&, const complex<T>&); // constexpr in C++14, removed in C++20
155 template<class T> bool operator!=(const complex<T>&, const T&); // constexpr in C++14, removed in C++20
156 template<class T> bool operator!=(const T&, const complex<T>&); // constexpr in C++14, removed in C++20
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>&); // constexpr in C++20
188 long double norm(long double); // constexpr in C++20
189 double norm(double); // constexpr in C++20
190 template<Integral T> double norm(T); // constexpr in C++20
191 float norm(float); // constexpr in C++20
193 template<class T> complex<T> conj(const complex<T>&); // constexpr in C++20
194 complex<long double> conj(long double); // constexpr in C++20
195 complex<double> conj(double); // constexpr in C++20
196 template<Integral T> complex<double> conj(T); // constexpr in C++20
197 complex<float> conj(float); // constexpr in C++20
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>&);
234 #include <__assert> // all public C++ headers provide the assertion handler
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> _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 complex<_Tp> operator*(const complex<_Tp>& __z, const complex<_Tp>& __w);
254 template<class _Tp> _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 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_SINCE_CXX14
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_SINCE_CXX14
269 complex(const complex<_Xp>& __c)
270 : __re_(__c.real()), __im_(__c.imag()) {}
272 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_SINCE_CXX14 value_type real() const {return __re_;}
273 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_SINCE_CXX14 value_type imag() const {return __im_;}
275 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_SINCE_CXX20 void real(value_type __re) {__re_ = __re;}
276 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_SINCE_CXX20 void imag(value_type __im) {__im_ = __im;}
278 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_SINCE_CXX20 complex& operator= (const value_type& __re)
279 {__re_ = __re; __im_ = value_type(); return *this;}
280 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_SINCE_CXX20 complex& operator+=(const value_type& __re) {__re_ += __re; return *this;}
281 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_SINCE_CXX20 complex& operator-=(const value_type& __re) {__re_ -= __re; return *this;}
282 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_SINCE_CXX20 complex& operator*=(const value_type& __re) {__re_ *= __re; __im_ *= __re; return *this;}
283 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_SINCE_CXX20 complex& operator/=(const value_type& __re) {__re_ /= __re; __im_ /= __re; return *this;}
285 template<class _Xp> _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_SINCE_CXX20 complex& operator= (const complex<_Xp>& __c)
291 template<class _Xp> _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_SINCE_CXX20 complex& operator+=(const complex<_Xp>& __c)
297 template<class _Xp> _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_SINCE_CXX20 complex& operator-=(const complex<_Xp>& __c)
303 template<class _Xp> _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_SINCE_CXX20 complex& operator*=(const complex<_Xp>& __c)
305 *this = *this * complex(__c.real(), __c.imag());
308 template<class _Xp> _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_SINCE_CXX20 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 _LIBCPP_CONSTEXPR_SINCE_CXX20 void real(value_type __re) {__re_ = __re;}
337 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_SINCE_CXX20 void imag(value_type __im) {__im_ = __im;}
339 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_SINCE_CXX20 complex& operator= (float __re)
340 {__re_ = __re; __im_ = value_type(); return *this;}
341 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_SINCE_CXX20 complex& operator+=(float __re) {__re_ += __re; return *this;}
342 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_SINCE_CXX20 complex& operator-=(float __re) {__re_ -= __re; return *this;}
343 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_SINCE_CXX20 complex& operator*=(float __re) {__re_ *= __re; __im_ *= __re; return *this;}
344 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_SINCE_CXX20 complex& operator/=(float __re) {__re_ /= __re; __im_ /= __re; return *this;}
346 template<class _Xp> _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_SINCE_CXX20 complex& operator= (const complex<_Xp>& __c)
352 template<class _Xp> _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_SINCE_CXX20 complex& operator+=(const complex<_Xp>& __c)
358 template<class _Xp> _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_SINCE_CXX20 complex& operator-=(const complex<_Xp>& __c)
364 template<class _Xp> _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_SINCE_CXX20 complex& operator*=(const complex<_Xp>& __c)
366 *this = *this * complex(__c.real(), __c.imag());
369 template<class _Xp> _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_SINCE_CXX20 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 _LIBCPP_CONSTEXPR_SINCE_CXX20 void real(value_type __re) {__re_ = __re;}
395 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_SINCE_CXX20 void imag(value_type __im) {__im_ = __im;}
397 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_SINCE_CXX20 complex& operator= (double __re)
398 {__re_ = __re; __im_ = value_type(); return *this;}
399 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_SINCE_CXX20 complex& operator+=(double __re) {__re_ += __re; return *this;}
400 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_SINCE_CXX20 complex& operator-=(double __re) {__re_ -= __re; return *this;}
401 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_SINCE_CXX20 complex& operator*=(double __re) {__re_ *= __re; __im_ *= __re; return *this;}
402 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_SINCE_CXX20 complex& operator/=(double __re) {__re_ /= __re; __im_ /= __re; return *this;}
404 template<class _Xp> _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_SINCE_CXX20 complex& operator= (const complex<_Xp>& __c)
410 template<class _Xp> _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_SINCE_CXX20 complex& operator+=(const complex<_Xp>& __c)
416 template<class _Xp> _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_SINCE_CXX20 complex& operator-=(const complex<_Xp>& __c)
422 template<class _Xp> _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_SINCE_CXX20 complex& operator*=(const complex<_Xp>& __c)
424 *this = *this * complex(__c.real(), __c.imag());
427 template<class _Xp> _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_SINCE_CXX20 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 _LIBCPP_CONSTEXPR_SINCE_CXX20 void real(value_type __re) {__re_ = __re;}
453 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_SINCE_CXX20 void imag(value_type __im) {__im_ = __im;}
455 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_SINCE_CXX20 complex& operator= (long double __re)
456 {__re_ = __re; __im_ = value_type(); return *this;}
457 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_SINCE_CXX20 complex& operator+=(long double __re) {__re_ += __re; return *this;}
458 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_SINCE_CXX20 complex& operator-=(long double __re) {__re_ -= __re; return *this;}
459 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_SINCE_CXX20 complex& operator*=(long double __re) {__re_ *= __re; __im_ *= __re; return *this;}
460 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_SINCE_CXX20 complex& operator/=(long double __re) {__re_ /= __re; __im_ /= __re; return *this;}
462 template<class _Xp> _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_SINCE_CXX20 complex& operator= (const complex<_Xp>& __c)
468 template<class _Xp> _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_SINCE_CXX20 complex& operator+=(const complex<_Xp>& __c)
474 template<class _Xp> _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_SINCE_CXX20 complex& operator-=(const complex<_Xp>& __c)
480 template<class _Xp> _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_SINCE_CXX20 complex& operator*=(const complex<_Xp>& __c)
482 *this = *this * complex(__c.real(), __c.imag());
485 template<class _Xp> _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_SINCE_CXX20 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 _LIBCPP_CONSTEXPR_SINCE_CXX20
527 operator+(const complex<_Tp>& __x, const complex<_Tp>& __y)
529 complex<_Tp> __t(__x);
535 inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_SINCE_CXX20
537 operator+(const complex<_Tp>& __x, const _Tp& __y)
539 complex<_Tp> __t(__x);
545 inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_SINCE_CXX20
547 operator+(const _Tp& __x, const complex<_Tp>& __y)
549 complex<_Tp> __t(__y);
555 inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_SINCE_CXX20
557 operator-(const complex<_Tp>& __x, const complex<_Tp>& __y)
559 complex<_Tp> __t(__x);
565 inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_SINCE_CXX20
567 operator-(const complex<_Tp>& __x, const _Tp& __y)
569 complex<_Tp> __t(__x);
575 inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_SINCE_CXX20
577 operator-(const _Tp& __x, const complex<_Tp>& __y)
579 complex<_Tp> __t(-__y);
585 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 complex<_Tp>
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();
593 // Avoid floating point operations that are invalid during constant evaluation
594 if (__libcpp_is_constant_evaluated()) {
595 bool __z_zero = __a == _Tp(0) && __b == _Tp(0);
596 bool __w_zero = __c == _Tp(0) && __d == _Tp(0);
597 bool __z_inf = std::__constexpr_isinf(__a) || std::__constexpr_isinf(__b);
598 bool __w_inf = std::__constexpr_isinf(__c) || std::__constexpr_isinf(__d);
599 bool __z_nan = !__z_inf && (
600 (std::__constexpr_isnan(__a) && std::__constexpr_isnan(__b))
601 || (std::__constexpr_isnan(__a) && __b == _Tp(0))
602 || (__a == _Tp(0) && std::__constexpr_isnan(__b))
604 bool __w_nan = !__w_inf && (
605 (std::__constexpr_isnan(__c) && std::__constexpr_isnan(__d))
606 || (std::__constexpr_isnan(__c) && __d == _Tp(0))
607 || (__c == _Tp(0) && std::__constexpr_isnan(__d))
609 if (__z_nan || __w_nan) {
610 return complex<_Tp>(_Tp(numeric_limits<_Tp>::quiet_NaN()), _Tp(0));
612 if (__z_inf || __w_inf) {
613 if (__z_zero || __w_zero) {
614 return complex<_Tp>(_Tp(numeric_limits<_Tp>::quiet_NaN()), _Tp(0));
616 return complex<_Tp>(_Tp(numeric_limits<_Tp>::infinity()), _Tp(numeric_limits<_Tp>::infinity()));
618 bool __z_nonzero_nan = !__z_inf && !__z_nan && (std::__constexpr_isnan(__a) || std::__constexpr_isnan(__b));
619 bool __w_nonzero_nan = !__w_inf && !__w_nan && (std::__constexpr_isnan(__c) || std::__constexpr_isnan(__d));
620 if (__z_nonzero_nan || __w_nonzero_nan) {
621 return complex<_Tp>(_Tp(numeric_limits<_Tp>::quiet_NaN()), _Tp(0));
625 _Tp __ac = __a * __c;
626 _Tp __bd = __b * __d;
627 _Tp __ad = __a * __d;
628 _Tp __bc = __b * __c;
629 _Tp __x = __ac - __bd;
630 _Tp __y = __ad + __bc;
631 if (std::__constexpr_isnan(__x) && std::__constexpr_isnan(__y))
633 bool __recalc = false;
634 if (std::__constexpr_isinf(__a) || std::__constexpr_isinf(__b))
636 __a = std::__constexpr_copysign(std::__constexpr_isinf(__a) ? _Tp(1) : _Tp(0), __a);
637 __b = std::__constexpr_copysign(std::__constexpr_isinf(__b) ? _Tp(1) : _Tp(0), __b);
638 if (std::__constexpr_isnan(__c))
639 __c = std::__constexpr_copysign(_Tp(0), __c);
640 if (std::__constexpr_isnan(__d))
641 __d = std::__constexpr_copysign(_Tp(0), __d);
644 if (std::__constexpr_isinf(__c) || std::__constexpr_isinf(__d))
646 __c = std::__constexpr_copysign(std::__constexpr_isinf(__c) ? _Tp(1) : _Tp(0), __c);
647 __d = std::__constexpr_copysign(std::__constexpr_isinf(__d) ? _Tp(1) : _Tp(0), __d);
648 if (std::__constexpr_isnan(__a))
649 __a = std::__constexpr_copysign(_Tp(0), __a);
650 if (std::__constexpr_isnan(__b))
651 __b = std::__constexpr_copysign(_Tp(0), __b);
654 if (!__recalc && (std::__constexpr_isinf(__ac) || std::__constexpr_isinf(__bd) ||
655 std::__constexpr_isinf(__ad) || std::__constexpr_isinf(__bc)))
657 if (std::__constexpr_isnan(__a))
658 __a = std::__constexpr_copysign(_Tp(0), __a);
659 if (std::__constexpr_isnan(__b))
660 __b = std::__constexpr_copysign(_Tp(0), __b);
661 if (std::__constexpr_isnan(__c))
662 __c = std::__constexpr_copysign(_Tp(0), __c);
663 if (std::__constexpr_isnan(__d))
664 __d = std::__constexpr_copysign(_Tp(0), __d);
669 __x = _Tp(INFINITY) * (__a * __c - __b * __d);
670 __y = _Tp(INFINITY) * (__a * __d + __b * __c);
673 return complex<_Tp>(__x, __y);
677 inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_SINCE_CXX20
679 operator*(const complex<_Tp>& __x, const _Tp& __y)
681 complex<_Tp> __t(__x);
687 inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_SINCE_CXX20
689 operator*(const _Tp& __x, const complex<_Tp>& __y)
691 complex<_Tp> __t(__y);
697 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 complex<_Tp>
698 operator/(const complex<_Tp>& __z, const complex<_Tp>& __w)
701 _Tp __a = __z.real();
702 _Tp __b = __z.imag();
703 _Tp __c = __w.real();
704 _Tp __d = __w.imag();
705 _Tp __logbw = std::__constexpr_logb(std::__constexpr_fmax(std::__constexpr_fabs(__c), std::__constexpr_fabs(__d)));
706 if (std::__constexpr_isfinite(__logbw))
708 __ilogbw = static_cast<int>(__logbw);
709 __c = std::__constexpr_scalbn(__c, -__ilogbw);
710 __d = std::__constexpr_scalbn(__d, -__ilogbw);
713 // Avoid floating point operations that are invalid during constant evaluation
714 if (__libcpp_is_constant_evaluated()) {
715 bool __z_zero = __a == _Tp(0) && __b == _Tp(0);
716 bool __w_zero = __c == _Tp(0) && __d == _Tp(0);
717 bool __z_inf = std::__constexpr_isinf(__a) || std::__constexpr_isinf(__b);
718 bool __w_inf = std::__constexpr_isinf(__c) || std::__constexpr_isinf(__d);
719 bool __z_nan = !__z_inf && (
720 (std::__constexpr_isnan(__a) && std::__constexpr_isnan(__b))
721 || (std::__constexpr_isnan(__a) && __b == _Tp(0))
722 || (__a == _Tp(0) && std::__constexpr_isnan(__b))
724 bool __w_nan = !__w_inf && (
725 (std::__constexpr_isnan(__c) && std::__constexpr_isnan(__d))
726 || (std::__constexpr_isnan(__c) && __d == _Tp(0))
727 || (__c == _Tp(0) && std::__constexpr_isnan(__d))
729 if ((__z_nan || __w_nan) || (__z_inf && __w_inf)) {
730 return complex<_Tp>(_Tp(numeric_limits<_Tp>::quiet_NaN()), _Tp(0));
732 bool __z_nonzero_nan = !__z_inf && !__z_nan && (std::__constexpr_isnan(__a) || std::__constexpr_isnan(__b));
733 bool __w_nonzero_nan = !__w_inf && !__w_nan && (std::__constexpr_isnan(__c) || std::__constexpr_isnan(__d));
734 if (__z_nonzero_nan || __w_nonzero_nan) {
736 return complex<_Tp>(_Tp(numeric_limits<_Tp>::infinity()), _Tp(numeric_limits<_Tp>::infinity()));
738 return complex<_Tp>(_Tp(numeric_limits<_Tp>::quiet_NaN()), _Tp(0));
741 return complex<_Tp>(_Tp(0), _Tp(0));
744 return complex<_Tp>(_Tp(numeric_limits<_Tp>::infinity()), _Tp(numeric_limits<_Tp>::infinity()));
748 return complex<_Tp>(_Tp(numeric_limits<_Tp>::quiet_NaN()), _Tp(0));
750 return complex<_Tp>(_Tp(numeric_limits<_Tp>::infinity()), _Tp(numeric_limits<_Tp>::infinity()));
754 _Tp __denom = __c * __c + __d * __d;
755 _Tp __x = std::__constexpr_scalbn((__a * __c + __b * __d) / __denom, -__ilogbw);
756 _Tp __y = std::__constexpr_scalbn((__b * __c - __a * __d) / __denom, -__ilogbw);
757 if (std::__constexpr_isnan(__x) && std::__constexpr_isnan(__y))
759 if ((__denom == _Tp(0)) && (!std::__constexpr_isnan(__a) || !std::__constexpr_isnan(__b)))
761 __x = std::__constexpr_copysign(_Tp(INFINITY), __c) * __a;
762 __y = std::__constexpr_copysign(_Tp(INFINITY), __c) * __b;
763 } else if ((std::__constexpr_isinf(__a) || std::__constexpr_isinf(__b)) && std::__constexpr_isfinite(__c) &&
764 std::__constexpr_isfinite(__d)) {
765 __a = std::__constexpr_copysign(std::__constexpr_isinf(__a) ? _Tp(1) : _Tp(0), __a);
766 __b = std::__constexpr_copysign(std::__constexpr_isinf(__b) ? _Tp(1) : _Tp(0), __b);
767 __x = _Tp(INFINITY) * (__a * __c + __b * __d);
768 __y = _Tp(INFINITY) * (__b * __c - __a * __d);
769 } else if (std::__constexpr_isinf(__logbw) && __logbw > _Tp(0) && std::__constexpr_isfinite(__a) &&
770 std::__constexpr_isfinite(__b)) {
771 __c = std::__constexpr_copysign(std::__constexpr_isinf(__c) ? _Tp(1) : _Tp(0), __c);
772 __d = std::__constexpr_copysign(std::__constexpr_isinf(__d) ? _Tp(1) : _Tp(0), __d);
773 __x = _Tp(0) * (__a * __c + __b * __d);
774 __y = _Tp(0) * (__b * __c - __a * __d);
777 return complex<_Tp>(__x, __y);
781 inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_SINCE_CXX20
783 operator/(const complex<_Tp>& __x, const _Tp& __y)
785 return complex<_Tp>(__x.real() / __y, __x.imag() / __y);
789 inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_SINCE_CXX20
791 operator/(const _Tp& __x, const complex<_Tp>& __y)
793 complex<_Tp> __t(__x);
799 inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_SINCE_CXX20
801 operator+(const complex<_Tp>& __x)
807 inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_SINCE_CXX20
809 operator-(const complex<_Tp>& __x)
811 return complex<_Tp>(-__x.real(), -__x.imag());
815 inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_SINCE_CXX14
817 operator==(const complex<_Tp>& __x, const complex<_Tp>& __y)
819 return __x.real() == __y.real() && __x.imag() == __y.imag();
823 inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_SINCE_CXX14
825 operator==(const complex<_Tp>& __x, const _Tp& __y)
827 return __x.real() == __y && __x.imag() == 0;
830 #if _LIBCPP_STD_VER <= 17
833 inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_SINCE_CXX14
835 operator==(const _Tp& __x, const complex<_Tp>& __y)
837 return __x == __y.real() && 0 == __y.imag();
841 inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_SINCE_CXX14
843 operator!=(const complex<_Tp>& __x, const complex<_Tp>& __y)
845 return !(__x == __y);
849 inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_SINCE_CXX14
851 operator!=(const complex<_Tp>& __x, const _Tp& __y)
853 return !(__x == __y);
857 inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_SINCE_CXX14
859 operator!=(const _Tp& __x, const complex<_Tp>& __y)
861 return !(__x == __y);
868 template <class _Tp, bool = is_integral<_Tp>::value,
869 bool = is_floating_point<_Tp>::value
871 struct __libcpp_complex_overload_traits {};
875 struct __libcpp_complex_overload_traits<_Tp, true, false>
877 typedef double _ValueType;
878 typedef complex<double> _ComplexType;
881 // Floating point types
883 struct __libcpp_complex_overload_traits<_Tp, false, true>
885 typedef _Tp _ValueType;
886 typedef complex<_Tp> _ComplexType;
892 inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_SINCE_CXX14
894 real(const complex<_Tp>& __c)
900 inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_SINCE_CXX14
901 typename __libcpp_complex_overload_traits<_Tp>::_ValueType
910 inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_SINCE_CXX14
912 imag(const complex<_Tp>& __c)
918 inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_SINCE_CXX14
919 typename __libcpp_complex_overload_traits<_Tp>::_ValueType
928 inline _LIBCPP_INLINE_VISIBILITY
930 abs(const complex<_Tp>& __c)
932 return std::hypot(__c.real(), __c.imag());
938 inline _LIBCPP_INLINE_VISIBILITY
940 arg(const complex<_Tp>& __c)
942 return std::atan2(__c.imag(), __c.real());
946 inline _LIBCPP_INLINE_VISIBILITY
948 is_same<_Tp, long double>::value,
953 return std::atan2l(0.L, __re);
957 inline _LIBCPP_INLINE_VISIBILITY
960 is_integral<_Tp>::value || is_same<_Tp, double>::value,
965 return std::atan2(0., __re);
969 inline _LIBCPP_INLINE_VISIBILITY
971 is_same<_Tp, float>::value,
976 return std::atan2f(0.F, __re);
982 inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_SINCE_CXX20
984 norm(const complex<_Tp>& __c)
986 if (std::__constexpr_isinf(__c.real()))
987 return std::abs(__c.real());
988 if (std::__constexpr_isinf(__c.imag()))
989 return std::abs(__c.imag());
990 return __c.real() * __c.real() + __c.imag() * __c.imag();
994 inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_SINCE_CXX20
995 typename __libcpp_complex_overload_traits<_Tp>::_ValueType
998 typedef typename __libcpp_complex_overload_traits<_Tp>::_ValueType _ValueType;
999 return static_cast<_ValueType>(__re) * __re;
1005 inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_SINCE_CXX20
1007 conj(const complex<_Tp>& __c)
1009 return complex<_Tp>(__c.real(), -__c.imag());
1012 template <class _Tp>
1013 inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_SINCE_CXX20
1014 typename __libcpp_complex_overload_traits<_Tp>::_ComplexType
1017 typedef typename __libcpp_complex_overload_traits<_Tp>::_ComplexType _ComplexType;
1018 return _ComplexType(__re);
1026 inline _LIBCPP_INLINE_VISIBILITY
1028 proj(const complex<_Tp>& __c)
1030 complex<_Tp> __r = __c;
1031 if (std::__constexpr_isinf(__c.real()) || std::__constexpr_isinf(__c.imag()))
1032 __r = complex<_Tp>(INFINITY, std::copysign(_Tp(0), __c.imag()));
1036 template <class _Tp>
1037 inline _LIBCPP_INLINE_VISIBILITY
1040 is_floating_point<_Tp>::value,
1041 typename __libcpp_complex_overload_traits<_Tp>::_ComplexType
1045 if (std::__constexpr_isinf(__re))
1046 __re = std::abs(__re);
1047 return complex<_Tp>(__re);
1050 template <class _Tp>
1051 inline _LIBCPP_INLINE_VISIBILITY
1054 is_integral<_Tp>::value,
1055 typename __libcpp_complex_overload_traits<_Tp>::_ComplexType
1059 typedef typename __libcpp_complex_overload_traits<_Tp>::_ComplexType _ComplexType;
1060 return _ComplexType(__re);
1066 _LIBCPP_HIDE_FROM_ABI complex<_Tp>
1067 polar(const _Tp& __rho, const _Tp& __theta = _Tp())
1069 if (std::__constexpr_isnan(__rho) || std::signbit(__rho))
1070 return complex<_Tp>(_Tp(NAN), _Tp(NAN));
1071 if (std::__constexpr_isnan(__theta))
1073 if (std::__constexpr_isinf(__rho))
1074 return complex<_Tp>(__rho, __theta);
1075 return complex<_Tp>(__theta, __theta);
1077 if (std::__constexpr_isinf(__theta))
1079 if (std::__constexpr_isinf(__rho))
1080 return complex<_Tp>(__rho, _Tp(NAN));
1081 return complex<_Tp>(_Tp(NAN), _Tp(NAN));
1083 _Tp __x = __rho * std::cos(__theta);
1084 if (std::__constexpr_isnan(__x))
1086 _Tp __y = __rho * std::sin(__theta);
1087 if (std::__constexpr_isnan(__y))
1089 return complex<_Tp>(__x, __y);
1095 inline _LIBCPP_INLINE_VISIBILITY
1097 log(const complex<_Tp>& __x)
1099 return complex<_Tp>(std::log(std::abs(__x)), std::arg(__x));
1105 inline _LIBCPP_INLINE_VISIBILITY
1107 log10(const complex<_Tp>& __x)
1109 return std::log(__x) / std::log(_Tp(10));
1115 _LIBCPP_HIDE_FROM_ABI complex<_Tp>
1116 sqrt(const complex<_Tp>& __x)
1118 if (std::__constexpr_isinf(__x.imag()))
1119 return complex<_Tp>(_Tp(INFINITY), __x.imag());
1120 if (std::__constexpr_isinf(__x.real()))
1122 if (__x.real() > _Tp(0))
1123 return complex<_Tp>(__x.real(), std::__constexpr_isnan(__x.imag()) ? __x.imag() : std::copysign(_Tp(0), __x.imag()));
1124 return complex<_Tp>(std::__constexpr_isnan(__x.imag()) ? __x.imag() : _Tp(0), std::copysign(__x.real(), __x.imag()));
1126 return std::polar(std::sqrt(std::abs(__x)), std::arg(__x) / _Tp(2));
1132 _LIBCPP_HIDE_FROM_ABI complex<_Tp>
1133 exp(const complex<_Tp>& __x)
1135 _Tp __i = __x.imag();
1137 return complex<_Tp>(std::exp(__x.real()), std::copysign(_Tp(0), __x.imag()));
1139 if (std::__constexpr_isinf(__x.real()))
1141 if (__x.real() < _Tp(0))
1143 if (!std::__constexpr_isfinite(__i))
1146 else if (__i == 0 || !std::__constexpr_isfinite(__i))
1148 if (std::__constexpr_isinf(__i))
1150 return complex<_Tp>(__x.real(), __i);
1153 _Tp __e = std::exp(__x.real());
1154 return complex<_Tp>(__e * std::cos(__i), __e * std::sin(__i));
1160 inline _LIBCPP_INLINE_VISIBILITY
1162 pow(const complex<_Tp>& __x, const complex<_Tp>& __y)
1164 return std::exp(__y * std::log(__x));
1167 template<class _Tp, class _Up>
1168 inline _LIBCPP_INLINE_VISIBILITY
1169 complex<typename __promote<_Tp, _Up>::type>
1170 pow(const complex<_Tp>& __x, const complex<_Up>& __y)
1172 typedef complex<typename __promote<_Tp, _Up>::type> result_type;
1173 return _VSTD::pow(result_type(__x), result_type(__y));
1176 template<class _Tp, class _Up>
1177 inline _LIBCPP_INLINE_VISIBILITY
1180 is_arithmetic<_Up>::value,
1181 complex<typename __promote<_Tp, _Up>::type>
1183 pow(const complex<_Tp>& __x, const _Up& __y)
1185 typedef complex<typename __promote<_Tp, _Up>::type> result_type;
1186 return _VSTD::pow(result_type(__x), result_type(__y));
1189 template<class _Tp, class _Up>
1190 inline _LIBCPP_INLINE_VISIBILITY
1193 is_arithmetic<_Tp>::value,
1194 complex<typename __promote<_Tp, _Up>::type>
1196 pow(const _Tp& __x, const complex<_Up>& __y)
1198 typedef complex<typename __promote<_Tp, _Up>::type> result_type;
1199 return _VSTD::pow(result_type(__x), result_type(__y));
1202 // __sqr, computes pow(x, 2)
1205 inline _LIBCPP_INLINE_VISIBILITY
1207 __sqr(const complex<_Tp>& __x)
1209 return complex<_Tp>((__x.real() - __x.imag()) * (__x.real() + __x.imag()),
1210 _Tp(2) * __x.real() * __x.imag());
1216 _LIBCPP_HIDE_FROM_ABI complex<_Tp>
1217 asinh(const complex<_Tp>& __x)
1219 const _Tp __pi(atan2(+0., -0.));
1220 if (std::__constexpr_isinf(__x.real()))
1222 if (std::__constexpr_isnan(__x.imag()))
1224 if (std::__constexpr_isinf(__x.imag()))
1225 return complex<_Tp>(__x.real(), std::copysign(__pi * _Tp(0.25), __x.imag()));
1226 return complex<_Tp>(__x.real(), std::copysign(_Tp(0), __x.imag()));
1228 if (std::__constexpr_isnan(__x.real()))
1230 if (std::__constexpr_isinf(__x.imag()))
1231 return complex<_Tp>(__x.imag(), __x.real());
1232 if (__x.imag() == 0)
1234 return complex<_Tp>(__x.real(), __x.real());
1236 if (std::__constexpr_isinf(__x.imag()))
1237 return complex<_Tp>(std::copysign(__x.imag(), __x.real()), std::copysign(__pi/_Tp(2), __x.imag()));
1238 complex<_Tp> __z = std::log(__x + std::sqrt(std::__sqr(__x) + _Tp(1)));
1239 return complex<_Tp>(std::copysign(__z.real(), __x.real()), std::copysign(__z.imag(), __x.imag()));
1245 _LIBCPP_HIDE_FROM_ABI complex<_Tp>
1246 acosh(const complex<_Tp>& __x)
1248 const _Tp __pi(atan2(+0., -0.));
1249 if (std::__constexpr_isinf(__x.real()))
1251 if (std::__constexpr_isnan(__x.imag()))
1252 return complex<_Tp>(std::abs(__x.real()), __x.imag());
1253 if (std::__constexpr_isinf(__x.imag()))
1256 return complex<_Tp>(__x.real(), std::copysign(__pi * _Tp(0.25), __x.imag()));
1258 return complex<_Tp>(-__x.real(), std::copysign(__pi * _Tp(0.75), __x.imag()));
1261 return complex<_Tp>(-__x.real(), std::copysign(__pi, __x.imag()));
1262 return complex<_Tp>(__x.real(), std::copysign(_Tp(0), __x.imag()));
1264 if (std::__constexpr_isnan(__x.real()))
1266 if (std::__constexpr_isinf(__x.imag()))
1267 return complex<_Tp>(std::abs(__x.imag()), __x.real());
1268 return complex<_Tp>(__x.real(), __x.real());
1270 if (std::__constexpr_isinf(__x.imag()))
1271 return complex<_Tp>(std::abs(__x.imag()), std::copysign(__pi/_Tp(2), __x.imag()));
1272 complex<_Tp> __z = std::log(__x + std::sqrt(std::__sqr(__x) - _Tp(1)));
1273 return complex<_Tp>(std::copysign(__z.real(), _Tp(0)), std::copysign(__z.imag(), __x.imag()));
1279 _LIBCPP_HIDE_FROM_ABI complex<_Tp>
1280 atanh(const complex<_Tp>& __x)
1282 const _Tp __pi(atan2(+0., -0.));
1283 if (std::__constexpr_isinf(__x.imag()))
1285 return complex<_Tp>(std::copysign(_Tp(0), __x.real()), std::copysign(__pi/_Tp(2), __x.imag()));
1287 if (std::__constexpr_isnan(__x.imag()))
1289 if (std::__constexpr_isinf(__x.real()) || __x.real() == 0)
1290 return complex<_Tp>(std::copysign(_Tp(0), __x.real()), __x.imag());
1291 return complex<_Tp>(__x.imag(), __x.imag());
1293 if (std::__constexpr_isnan(__x.real()))
1295 return complex<_Tp>(__x.real(), __x.real());
1297 if (std::__constexpr_isinf(__x.real()))
1299 return complex<_Tp>(std::copysign(_Tp(0), __x.real()), std::copysign(__pi/_Tp(2), __x.imag()));
1301 if (std::abs(__x.real()) == _Tp(1) && __x.imag() == _Tp(0))
1303 return complex<_Tp>(std::copysign(_Tp(INFINITY), __x.real()), std::copysign(_Tp(0), __x.imag()));
1305 complex<_Tp> __z = std::log((_Tp(1) + __x) / (_Tp(1) - __x)) / _Tp(2);
1306 return complex<_Tp>(std::copysign(__z.real(), __x.real()), std::copysign(__z.imag(), __x.imag()));
1312 _LIBCPP_HIDE_FROM_ABI complex<_Tp>
1313 sinh(const complex<_Tp>& __x)
1315 if (std::__constexpr_isinf(__x.real()) && !std::__constexpr_isfinite(__x.imag()))
1316 return complex<_Tp>(__x.real(), _Tp(NAN));
1317 if (__x.real() == 0 && !std::__constexpr_isfinite(__x.imag()))
1318 return complex<_Tp>(__x.real(), _Tp(NAN));
1319 if (__x.imag() == 0 && !std::__constexpr_isfinite(__x.real()))
1321 return complex<_Tp>(std::sinh(__x.real()) * std::cos(__x.imag()), std::cosh(__x.real()) * std::sin(__x.imag()));
1327 _LIBCPP_HIDE_FROM_ABI complex<_Tp>
1328 cosh(const complex<_Tp>& __x)
1330 if (std::__constexpr_isinf(__x.real()) && !std::__constexpr_isfinite(__x.imag()))
1331 return complex<_Tp>(std::abs(__x.real()), _Tp(NAN));
1332 if (__x.real() == 0 && !std::__constexpr_isfinite(__x.imag()))
1333 return complex<_Tp>(_Tp(NAN), __x.real());
1334 if (__x.real() == 0 && __x.imag() == 0)
1335 return complex<_Tp>(_Tp(1), __x.imag());
1336 if (__x.imag() == 0 && !std::__constexpr_isfinite(__x.real()))
1337 return complex<_Tp>(std::abs(__x.real()), __x.imag());
1338 return complex<_Tp>(std::cosh(__x.real()) * std::cos(__x.imag()), std::sinh(__x.real()) * std::sin(__x.imag()));
1344 _LIBCPP_HIDE_FROM_ABI complex<_Tp>
1345 tanh(const complex<_Tp>& __x)
1347 if (std::__constexpr_isinf(__x.real()))
1349 if (!std::__constexpr_isfinite(__x.imag()))
1350 return complex<_Tp>(std::copysign(_Tp(1), __x.real()), _Tp(0));
1351 return complex<_Tp>(std::copysign(_Tp(1), __x.real()), std::copysign(_Tp(0), std::sin(_Tp(2) * __x.imag())));
1353 if (std::__constexpr_isnan(__x.real()) && __x.imag() == 0)
1355 _Tp __2r(_Tp(2) * __x.real());
1356 _Tp __2i(_Tp(2) * __x.imag());
1357 _Tp __d(std::cosh(__2r) + std::cos(__2i));
1358 _Tp __2rsh(std::sinh(__2r));
1359 if (std::__constexpr_isinf(__2rsh) && std::__constexpr_isinf(__d))
1360 return complex<_Tp>(__2rsh > _Tp(0) ? _Tp(1) : _Tp(-1),
1361 __2i > _Tp(0) ? _Tp(0) : _Tp(-0.));
1362 return complex<_Tp>(__2rsh/__d, std::sin(__2i)/__d);
1368 _LIBCPP_HIDE_FROM_ABI complex<_Tp>
1369 asin(const complex<_Tp>& __x)
1371 complex<_Tp> __z = std::asinh(complex<_Tp>(-__x.imag(), __x.real()));
1372 return complex<_Tp>(__z.imag(), -__z.real());
1378 _LIBCPP_HIDE_FROM_ABI complex<_Tp>
1379 acos(const complex<_Tp>& __x)
1381 const _Tp __pi(atan2(+0., -0.));
1382 if (std::__constexpr_isinf(__x.real()))
1384 if (std::__constexpr_isnan(__x.imag()))
1385 return complex<_Tp>(__x.imag(), __x.real());
1386 if (std::__constexpr_isinf(__x.imag()))
1388 if (__x.real() < _Tp(0))
1389 return complex<_Tp>(_Tp(0.75) * __pi, -__x.imag());
1390 return complex<_Tp>(_Tp(0.25) * __pi, -__x.imag());
1392 if (__x.real() < _Tp(0))
1393 return complex<_Tp>(__pi, std::signbit(__x.imag()) ? -__x.real() : __x.real());
1394 return complex<_Tp>(_Tp(0), std::signbit(__x.imag()) ? __x.real() : -__x.real());
1396 if (std::__constexpr_isnan(__x.real()))
1398 if (std::__constexpr_isinf(__x.imag()))
1399 return complex<_Tp>(__x.real(), -__x.imag());
1400 return complex<_Tp>(__x.real(), __x.real());
1402 if (std::__constexpr_isinf(__x.imag()))
1403 return complex<_Tp>(__pi/_Tp(2), -__x.imag());
1404 if (__x.real() == 0 && (__x.imag() == 0 || std::isnan(__x.imag())))
1405 return complex<_Tp>(__pi/_Tp(2), -__x.imag());
1406 complex<_Tp> __z = std::log(__x + std::sqrt(std::__sqr(__x) - _Tp(1)));
1407 if (std::signbit(__x.imag()))
1408 return complex<_Tp>(std::abs(__z.imag()), std::abs(__z.real()));
1409 return complex<_Tp>(std::abs(__z.imag()), -std::abs(__z.real()));
1415 _LIBCPP_HIDE_FROM_ABI complex<_Tp>
1416 atan(const complex<_Tp>& __x)
1418 complex<_Tp> __z = std::atanh(complex<_Tp>(-__x.imag(), __x.real()));
1419 return complex<_Tp>(__z.imag(), -__z.real());
1425 _LIBCPP_HIDE_FROM_ABI complex<_Tp>
1426 sin(const complex<_Tp>& __x)
1428 complex<_Tp> __z = std::sinh(complex<_Tp>(-__x.imag(), __x.real()));
1429 return complex<_Tp>(__z.imag(), -__z.real());
1435 inline _LIBCPP_INLINE_VISIBILITY
1437 cos(const complex<_Tp>& __x)
1439 return std::cosh(complex<_Tp>(-__x.imag(), __x.real()));
1445 _LIBCPP_HIDE_FROM_ABI complex<_Tp>
1446 tan(const complex<_Tp>& __x)
1448 complex<_Tp> __z = std::tanh(complex<_Tp>(-__x.imag(), __x.real()));
1449 return complex<_Tp>(__z.imag(), -__z.real());
1452 #if !defined(_LIBCPP_HAS_NO_LOCALIZATION)
1453 template<class _Tp, class _CharT, class _Traits>
1454 _LIBCPP_HIDE_FROM_ABI basic_istream<_CharT, _Traits>&
1455 operator>>(basic_istream<_CharT, _Traits>& __is, complex<_Tp>& __x)
1460 if (__is.peek() == _CharT('('))
1468 _CharT __c = __is.peek();
1469 if (__c == _CharT(','))
1478 if (__c == _CharT(')'))
1481 __x = complex<_Tp>(__r, __i);
1484 __is.setstate(__is.failbit);
1487 __is.setstate(__is.failbit);
1489 else if (__c == _CharT(')'))
1492 __x = complex<_Tp>(__r, _Tp(0));
1495 __is.setstate(__is.failbit);
1498 __is.setstate(__is.failbit);
1505 __x = complex<_Tp>(__r, _Tp(0));
1507 __is.setstate(__is.failbit);
1511 __is.setstate(__is.failbit);
1515 template<class _Tp, class _CharT, class _Traits>
1516 _LIBCPP_HIDE_FROM_ABI basic_ostream<_CharT, _Traits>&
1517 operator<<(basic_ostream<_CharT, _Traits>& __os, const complex<_Tp>& __x)
1519 basic_ostringstream<_CharT, _Traits> __s;
1520 __s.flags(__os.flags());
1521 __s.imbue(__os.getloc());
1522 __s.precision(__os.precision());
1523 __s << '(' << __x.real() << ',' << __x.imag() << ')';
1524 return __os << __s.str();
1526 #endif // !_LIBCPP_HAS_NO_LOCALIZATION
1528 #if _LIBCPP_STD_VER >= 14
1529 // Literal suffix for complex number literals [complex.literals]
1530 inline namespace literals
1532 inline namespace complex_literals
1534 _LIBCPP_HIDE_FROM_ABI constexpr complex<long double> operator""il(long double __im)
1536 return { 0.0l, __im };
1539 _LIBCPP_HIDE_FROM_ABI constexpr complex<long double> operator""il(unsigned long long __im)
1541 return { 0.0l, static_cast<long double>(__im) };
1545 _LIBCPP_HIDE_FROM_ABI constexpr complex<double> operator""i(long double __im)
1547 return { 0.0, static_cast<double>(__im) };
1550 _LIBCPP_HIDE_FROM_ABI constexpr complex<double> operator""i(unsigned long long __im)
1552 return { 0.0, static_cast<double>(__im) };
1556 _LIBCPP_HIDE_FROM_ABI constexpr complex<float> operator""if(long double __im)
1558 return { 0.0f, static_cast<float>(__im) };
1561 _LIBCPP_HIDE_FROM_ABI constexpr complex<float> operator""if(unsigned long long __im)
1563 return { 0.0f, static_cast<float>(__im) };
1565 } // namespace complex_literals
1566 } // namespace literals
1569 _LIBCPP_END_NAMESPACE_STD
1571 #if !defined(_LIBCPP_REMOVE_TRANSITIVE_INCLUDES) && _LIBCPP_STD_VER <= 20
1572 # include <type_traits>
1575 #endif // _LIBCPP_COMPLEX