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
239 #if !defined(_LIBCPP_HAS_NO_LOCALIZATION)
240 # include <sstream> // for std::basic_ostringstream
243 #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
244 # pragma GCC system_header
247 _LIBCPP_BEGIN_NAMESPACE_STD
249 template<class _Tp> class _LIBCPP_TEMPLATE_VIS complex;
251 template<class _Tp> _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 complex<_Tp> operator*(const complex<_Tp>& __z, const complex<_Tp>& __w);
252 template<class _Tp> _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 complex<_Tp> operator/(const complex<_Tp>& __x, const complex<_Tp>& __y);
255 class _LIBCPP_TEMPLATE_VIS complex
258 typedef _Tp value_type;
263 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_SINCE_CXX14
264 complex(const value_type& __re = value_type(), const value_type& __im = value_type())
265 : __re_(__re), __im_(__im) {}
266 template<class _Xp> _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_SINCE_CXX14
267 complex(const complex<_Xp>& __c)
268 : __re_(__c.real()), __im_(__c.imag()) {}
270 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_SINCE_CXX14 value_type real() const {return __re_;}
271 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_SINCE_CXX14 value_type imag() const {return __im_;}
273 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_SINCE_CXX20 void real(value_type __re) {__re_ = __re;}
274 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_SINCE_CXX20 void imag(value_type __im) {__im_ = __im;}
276 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_SINCE_CXX20 complex& operator= (const value_type& __re)
277 {__re_ = __re; __im_ = value_type(); return *this;}
278 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_SINCE_CXX20 complex& operator+=(const value_type& __re) {__re_ += __re; return *this;}
279 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_SINCE_CXX20 complex& operator-=(const value_type& __re) {__re_ -= __re; return *this;}
280 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_SINCE_CXX20 complex& operator*=(const value_type& __re) {__re_ *= __re; __im_ *= __re; return *this;}
281 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_SINCE_CXX20 complex& operator/=(const value_type& __re) {__re_ /= __re; __im_ /= __re; return *this;}
283 template<class _Xp> _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_SINCE_CXX20 complex& operator= (const complex<_Xp>& __c)
289 template<class _Xp> _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_SINCE_CXX20 complex& operator+=(const complex<_Xp>& __c)
295 template<class _Xp> _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_SINCE_CXX20 complex& operator-=(const complex<_Xp>& __c)
301 template<class _Xp> _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_SINCE_CXX20 complex& operator*=(const complex<_Xp>& __c)
303 *this = *this * complex(__c.real(), __c.imag());
306 template<class _Xp> _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_SINCE_CXX20 complex& operator/=(const complex<_Xp>& __c)
308 *this = *this / complex(__c.real(), __c.imag());
313 template<> class _LIBCPP_TEMPLATE_VIS complex<double>;
314 template<> class _LIBCPP_TEMPLATE_VIS complex<long double>;
317 class _LIBCPP_TEMPLATE_VIS complex<float>
322 typedef float value_type;
324 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR complex(float __re = 0.0f, float __im = 0.0f)
325 : __re_(__re), __im_(__im) {}
326 _LIBCPP_INLINE_VISIBILITY
327 explicit _LIBCPP_CONSTEXPR complex(const complex<double>& __c);
328 _LIBCPP_INLINE_VISIBILITY
329 explicit _LIBCPP_CONSTEXPR complex(const complex<long double>& __c);
331 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR float real() const {return __re_;}
332 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR float imag() const {return __im_;}
334 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_SINCE_CXX20 void real(value_type __re) {__re_ = __re;}
335 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_SINCE_CXX20 void imag(value_type __im) {__im_ = __im;}
337 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_SINCE_CXX20 complex& operator= (float __re)
338 {__re_ = __re; __im_ = value_type(); return *this;}
339 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_SINCE_CXX20 complex& operator+=(float __re) {__re_ += __re; return *this;}
340 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_SINCE_CXX20 complex& operator-=(float __re) {__re_ -= __re; return *this;}
341 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_SINCE_CXX20 complex& operator*=(float __re) {__re_ *= __re; __im_ *= __re; return *this;}
342 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_SINCE_CXX20 complex& operator/=(float __re) {__re_ /= __re; __im_ /= __re; return *this;}
344 template<class _Xp> _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_SINCE_CXX20 complex& operator= (const complex<_Xp>& __c)
350 template<class _Xp> _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_SINCE_CXX20 complex& operator+=(const complex<_Xp>& __c)
356 template<class _Xp> _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_SINCE_CXX20 complex& operator-=(const complex<_Xp>& __c)
362 template<class _Xp> _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_SINCE_CXX20 complex& operator*=(const complex<_Xp>& __c)
364 *this = *this * complex(__c.real(), __c.imag());
367 template<class _Xp> _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_SINCE_CXX20 complex& operator/=(const complex<_Xp>& __c)
369 *this = *this / complex(__c.real(), __c.imag());
375 class _LIBCPP_TEMPLATE_VIS complex<double>
380 typedef double value_type;
382 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR complex(double __re = 0.0, double __im = 0.0)
383 : __re_(__re), __im_(__im) {}
384 _LIBCPP_INLINE_VISIBILITY
385 _LIBCPP_CONSTEXPR complex(const complex<float>& __c);
386 _LIBCPP_INLINE_VISIBILITY
387 explicit _LIBCPP_CONSTEXPR complex(const complex<long double>& __c);
389 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR double real() const {return __re_;}
390 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR double imag() const {return __im_;}
392 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_SINCE_CXX20 void real(value_type __re) {__re_ = __re;}
393 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_SINCE_CXX20 void imag(value_type __im) {__im_ = __im;}
395 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_SINCE_CXX20 complex& operator= (double __re)
396 {__re_ = __re; __im_ = value_type(); return *this;}
397 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_SINCE_CXX20 complex& operator+=(double __re) {__re_ += __re; return *this;}
398 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_SINCE_CXX20 complex& operator-=(double __re) {__re_ -= __re; return *this;}
399 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_SINCE_CXX20 complex& operator*=(double __re) {__re_ *= __re; __im_ *= __re; return *this;}
400 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_SINCE_CXX20 complex& operator/=(double __re) {__re_ /= __re; __im_ /= __re; return *this;}
402 template<class _Xp> _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_SINCE_CXX20 complex& operator= (const complex<_Xp>& __c)
408 template<class _Xp> _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_SINCE_CXX20 complex& operator+=(const complex<_Xp>& __c)
414 template<class _Xp> _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_SINCE_CXX20 complex& operator-=(const complex<_Xp>& __c)
420 template<class _Xp> _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_SINCE_CXX20 complex& operator*=(const complex<_Xp>& __c)
422 *this = *this * complex(__c.real(), __c.imag());
425 template<class _Xp> _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_SINCE_CXX20 complex& operator/=(const complex<_Xp>& __c)
427 *this = *this / complex(__c.real(), __c.imag());
433 class _LIBCPP_TEMPLATE_VIS complex<long double>
438 typedef long double value_type;
440 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR complex(long double __re = 0.0L, long double __im = 0.0L)
441 : __re_(__re), __im_(__im) {}
442 _LIBCPP_INLINE_VISIBILITY
443 _LIBCPP_CONSTEXPR complex(const complex<float>& __c);
444 _LIBCPP_INLINE_VISIBILITY
445 _LIBCPP_CONSTEXPR complex(const complex<double>& __c);
447 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR long double real() const {return __re_;}
448 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR long double imag() const {return __im_;}
450 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_SINCE_CXX20 void real(value_type __re) {__re_ = __re;}
451 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_SINCE_CXX20 void imag(value_type __im) {__im_ = __im;}
453 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_SINCE_CXX20 complex& operator= (long double __re)
454 {__re_ = __re; __im_ = value_type(); return *this;}
455 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_SINCE_CXX20 complex& operator+=(long double __re) {__re_ += __re; return *this;}
456 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_SINCE_CXX20 complex& operator-=(long double __re) {__re_ -= __re; return *this;}
457 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_SINCE_CXX20 complex& operator*=(long double __re) {__re_ *= __re; __im_ *= __re; return *this;}
458 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_SINCE_CXX20 complex& operator/=(long double __re) {__re_ /= __re; __im_ /= __re; return *this;}
460 template<class _Xp> _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_SINCE_CXX20 complex& operator= (const complex<_Xp>& __c)
466 template<class _Xp> _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_SINCE_CXX20 complex& operator+=(const complex<_Xp>& __c)
472 template<class _Xp> _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_SINCE_CXX20 complex& operator-=(const complex<_Xp>& __c)
478 template<class _Xp> _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_SINCE_CXX20 complex& operator*=(const complex<_Xp>& __c)
480 *this = *this * complex(__c.real(), __c.imag());
483 template<class _Xp> _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_SINCE_CXX20 complex& operator/=(const complex<_Xp>& __c)
485 *this = *this / complex(__c.real(), __c.imag());
492 complex<float>::complex(const complex<double>& __c)
493 : __re_(__c.real()), __im_(__c.imag()) {}
497 complex<float>::complex(const complex<long double>& __c)
498 : __re_(__c.real()), __im_(__c.imag()) {}
502 complex<double>::complex(const complex<float>& __c)
503 : __re_(__c.real()), __im_(__c.imag()) {}
507 complex<double>::complex(const complex<long double>& __c)
508 : __re_(__c.real()), __im_(__c.imag()) {}
512 complex<long double>::complex(const complex<float>& __c)
513 : __re_(__c.real()), __im_(__c.imag()) {}
517 complex<long double>::complex(const complex<double>& __c)
518 : __re_(__c.real()), __im_(__c.imag()) {}
523 inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_SINCE_CXX20
525 operator+(const complex<_Tp>& __x, const complex<_Tp>& __y)
527 complex<_Tp> __t(__x);
533 inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_SINCE_CXX20
535 operator+(const complex<_Tp>& __x, const _Tp& __y)
537 complex<_Tp> __t(__x);
543 inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_SINCE_CXX20
545 operator+(const _Tp& __x, const complex<_Tp>& __y)
547 complex<_Tp> __t(__y);
553 inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_SINCE_CXX20
555 operator-(const complex<_Tp>& __x, const complex<_Tp>& __y)
557 complex<_Tp> __t(__x);
563 inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_SINCE_CXX20
565 operator-(const complex<_Tp>& __x, const _Tp& __y)
567 complex<_Tp> __t(__x);
573 inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_SINCE_CXX20
575 operator-(const _Tp& __x, const complex<_Tp>& __y)
577 complex<_Tp> __t(-__y);
583 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 complex<_Tp>
584 operator*(const complex<_Tp>& __z, const complex<_Tp>& __w)
586 _Tp __a = __z.real();
587 _Tp __b = __z.imag();
588 _Tp __c = __w.real();
589 _Tp __d = __w.imag();
591 // Avoid floating point operations that are invalid during constant evaluation
592 if (__libcpp_is_constant_evaluated()) {
593 bool __z_zero = __a == _Tp(0) && __b == _Tp(0);
594 bool __w_zero = __c == _Tp(0) && __d == _Tp(0);
595 bool __z_inf = std::__constexpr_isinf(__a) || std::__constexpr_isinf(__b);
596 bool __w_inf = std::__constexpr_isinf(__c) || std::__constexpr_isinf(__d);
597 bool __z_nan = !__z_inf && (
598 (std::__constexpr_isnan(__a) && std::__constexpr_isnan(__b))
599 || (std::__constexpr_isnan(__a) && __b == _Tp(0))
600 || (__a == _Tp(0) && std::__constexpr_isnan(__b))
602 bool __w_nan = !__w_inf && (
603 (std::__constexpr_isnan(__c) && std::__constexpr_isnan(__d))
604 || (std::__constexpr_isnan(__c) && __d == _Tp(0))
605 || (__c == _Tp(0) && std::__constexpr_isnan(__d))
607 if (__z_nan || __w_nan) {
608 return complex<_Tp>(_Tp(numeric_limits<_Tp>::quiet_NaN()), _Tp(0));
610 if (__z_inf || __w_inf) {
611 if (__z_zero || __w_zero) {
612 return complex<_Tp>(_Tp(numeric_limits<_Tp>::quiet_NaN()), _Tp(0));
614 return complex<_Tp>(_Tp(numeric_limits<_Tp>::infinity()), _Tp(numeric_limits<_Tp>::infinity()));
616 bool __z_nonzero_nan = !__z_inf && !__z_nan && (std::__constexpr_isnan(__a) || std::__constexpr_isnan(__b));
617 bool __w_nonzero_nan = !__w_inf && !__w_nan && (std::__constexpr_isnan(__c) || std::__constexpr_isnan(__d));
618 if (__z_nonzero_nan || __w_nonzero_nan) {
619 return complex<_Tp>(_Tp(numeric_limits<_Tp>::quiet_NaN()), _Tp(0));
623 _Tp __ac = __a * __c;
624 _Tp __bd = __b * __d;
625 _Tp __ad = __a * __d;
626 _Tp __bc = __b * __c;
627 _Tp __x = __ac - __bd;
628 _Tp __y = __ad + __bc;
629 if (std::__constexpr_isnan(__x) && std::__constexpr_isnan(__y))
631 bool __recalc = false;
632 if (std::__constexpr_isinf(__a) || std::__constexpr_isinf(__b))
634 __a = std::__constexpr_copysign(std::__constexpr_isinf(__a) ? _Tp(1) : _Tp(0), __a);
635 __b = std::__constexpr_copysign(std::__constexpr_isinf(__b) ? _Tp(1) : _Tp(0), __b);
636 if (std::__constexpr_isnan(__c))
637 __c = std::__constexpr_copysign(_Tp(0), __c);
638 if (std::__constexpr_isnan(__d))
639 __d = std::__constexpr_copysign(_Tp(0), __d);
642 if (std::__constexpr_isinf(__c) || std::__constexpr_isinf(__d))
644 __c = std::__constexpr_copysign(std::__constexpr_isinf(__c) ? _Tp(1) : _Tp(0), __c);
645 __d = std::__constexpr_copysign(std::__constexpr_isinf(__d) ? _Tp(1) : _Tp(0), __d);
646 if (std::__constexpr_isnan(__a))
647 __a = std::__constexpr_copysign(_Tp(0), __a);
648 if (std::__constexpr_isnan(__b))
649 __b = std::__constexpr_copysign(_Tp(0), __b);
652 if (!__recalc && (std::__constexpr_isinf(__ac) || std::__constexpr_isinf(__bd) ||
653 std::__constexpr_isinf(__ad) || std::__constexpr_isinf(__bc)))
655 if (std::__constexpr_isnan(__a))
656 __a = std::__constexpr_copysign(_Tp(0), __a);
657 if (std::__constexpr_isnan(__b))
658 __b = std::__constexpr_copysign(_Tp(0), __b);
659 if (std::__constexpr_isnan(__c))
660 __c = std::__constexpr_copysign(_Tp(0), __c);
661 if (std::__constexpr_isnan(__d))
662 __d = std::__constexpr_copysign(_Tp(0), __d);
667 __x = _Tp(INFINITY) * (__a * __c - __b * __d);
668 __y = _Tp(INFINITY) * (__a * __d + __b * __c);
671 return complex<_Tp>(__x, __y);
675 inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_SINCE_CXX20
677 operator*(const complex<_Tp>& __x, const _Tp& __y)
679 complex<_Tp> __t(__x);
685 inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_SINCE_CXX20
687 operator*(const _Tp& __x, const complex<_Tp>& __y)
689 complex<_Tp> __t(__y);
695 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 complex<_Tp>
696 operator/(const complex<_Tp>& __z, const complex<_Tp>& __w)
699 _Tp __a = __z.real();
700 _Tp __b = __z.imag();
701 _Tp __c = __w.real();
702 _Tp __d = __w.imag();
703 _Tp __logbw = std::__constexpr_logb(std::__constexpr_fmax(std::__constexpr_fabs(__c), std::__constexpr_fabs(__d)));
704 if (std::__constexpr_isfinite(__logbw))
706 __ilogbw = static_cast<int>(__logbw);
707 __c = std::__constexpr_scalbn(__c, -__ilogbw);
708 __d = std::__constexpr_scalbn(__d, -__ilogbw);
711 // Avoid floating point operations that are invalid during constant evaluation
712 if (__libcpp_is_constant_evaluated()) {
713 bool __z_zero = __a == _Tp(0) && __b == _Tp(0);
714 bool __w_zero = __c == _Tp(0) && __d == _Tp(0);
715 bool __z_inf = std::__constexpr_isinf(__a) || std::__constexpr_isinf(__b);
716 bool __w_inf = std::__constexpr_isinf(__c) || std::__constexpr_isinf(__d);
717 bool __z_nan = !__z_inf && (
718 (std::__constexpr_isnan(__a) && std::__constexpr_isnan(__b))
719 || (std::__constexpr_isnan(__a) && __b == _Tp(0))
720 || (__a == _Tp(0) && std::__constexpr_isnan(__b))
722 bool __w_nan = !__w_inf && (
723 (std::__constexpr_isnan(__c) && std::__constexpr_isnan(__d))
724 || (std::__constexpr_isnan(__c) && __d == _Tp(0))
725 || (__c == _Tp(0) && std::__constexpr_isnan(__d))
727 if ((__z_nan || __w_nan) || (__z_inf && __w_inf)) {
728 return complex<_Tp>(_Tp(numeric_limits<_Tp>::quiet_NaN()), _Tp(0));
730 bool __z_nonzero_nan = !__z_inf && !__z_nan && (std::__constexpr_isnan(__a) || std::__constexpr_isnan(__b));
731 bool __w_nonzero_nan = !__w_inf && !__w_nan && (std::__constexpr_isnan(__c) || std::__constexpr_isnan(__d));
732 if (__z_nonzero_nan || __w_nonzero_nan) {
734 return complex<_Tp>(_Tp(numeric_limits<_Tp>::infinity()), _Tp(numeric_limits<_Tp>::infinity()));
736 return complex<_Tp>(_Tp(numeric_limits<_Tp>::quiet_NaN()), _Tp(0));
739 return complex<_Tp>(_Tp(0), _Tp(0));
742 return complex<_Tp>(_Tp(numeric_limits<_Tp>::infinity()), _Tp(numeric_limits<_Tp>::infinity()));
746 return complex<_Tp>(_Tp(numeric_limits<_Tp>::quiet_NaN()), _Tp(0));
748 return complex<_Tp>(_Tp(numeric_limits<_Tp>::infinity()), _Tp(numeric_limits<_Tp>::infinity()));
752 _Tp __denom = __c * __c + __d * __d;
753 _Tp __x = std::__constexpr_scalbn((__a * __c + __b * __d) / __denom, -__ilogbw);
754 _Tp __y = std::__constexpr_scalbn((__b * __c - __a * __d) / __denom, -__ilogbw);
755 if (std::__constexpr_isnan(__x) && std::__constexpr_isnan(__y))
757 if ((__denom == _Tp(0)) && (!std::__constexpr_isnan(__a) || !std::__constexpr_isnan(__b)))
759 __x = std::__constexpr_copysign(_Tp(INFINITY), __c) * __a;
760 __y = std::__constexpr_copysign(_Tp(INFINITY), __c) * __b;
761 } else if ((std::__constexpr_isinf(__a) || std::__constexpr_isinf(__b)) && std::__constexpr_isfinite(__c) &&
762 std::__constexpr_isfinite(__d)) {
763 __a = std::__constexpr_copysign(std::__constexpr_isinf(__a) ? _Tp(1) : _Tp(0), __a);
764 __b = std::__constexpr_copysign(std::__constexpr_isinf(__b) ? _Tp(1) : _Tp(0), __b);
765 __x = _Tp(INFINITY) * (__a * __c + __b * __d);
766 __y = _Tp(INFINITY) * (__b * __c - __a * __d);
767 } else if (std::__constexpr_isinf(__logbw) && __logbw > _Tp(0) && std::__constexpr_isfinite(__a) &&
768 std::__constexpr_isfinite(__b)) {
769 __c = std::__constexpr_copysign(std::__constexpr_isinf(__c) ? _Tp(1) : _Tp(0), __c);
770 __d = std::__constexpr_copysign(std::__constexpr_isinf(__d) ? _Tp(1) : _Tp(0), __d);
771 __x = _Tp(0) * (__a * __c + __b * __d);
772 __y = _Tp(0) * (__b * __c - __a * __d);
775 return complex<_Tp>(__x, __y);
779 inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_SINCE_CXX20
781 operator/(const complex<_Tp>& __x, const _Tp& __y)
783 return complex<_Tp>(__x.real() / __y, __x.imag() / __y);
787 inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_SINCE_CXX20
789 operator/(const _Tp& __x, const complex<_Tp>& __y)
791 complex<_Tp> __t(__x);
797 inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_SINCE_CXX20
799 operator+(const complex<_Tp>& __x)
805 inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_SINCE_CXX20
807 operator-(const complex<_Tp>& __x)
809 return complex<_Tp>(-__x.real(), -__x.imag());
813 inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_SINCE_CXX14
815 operator==(const complex<_Tp>& __x, const complex<_Tp>& __y)
817 return __x.real() == __y.real() && __x.imag() == __y.imag();
821 inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_SINCE_CXX14
823 operator==(const complex<_Tp>& __x, const _Tp& __y)
825 return __x.real() == __y && __x.imag() == 0;
828 #if _LIBCPP_STD_VER <= 17
831 inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_SINCE_CXX14
833 operator==(const _Tp& __x, const complex<_Tp>& __y)
835 return __x == __y.real() && 0 == __y.imag();
839 inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_SINCE_CXX14
841 operator!=(const complex<_Tp>& __x, const complex<_Tp>& __y)
843 return !(__x == __y);
847 inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_SINCE_CXX14
849 operator!=(const complex<_Tp>& __x, const _Tp& __y)
851 return !(__x == __y);
855 inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_SINCE_CXX14
857 operator!=(const _Tp& __x, const complex<_Tp>& __y)
859 return !(__x == __y);
866 template <class _Tp, bool = is_integral<_Tp>::value,
867 bool = is_floating_point<_Tp>::value
869 struct __libcpp_complex_overload_traits {};
873 struct __libcpp_complex_overload_traits<_Tp, true, false>
875 typedef double _ValueType;
876 typedef complex<double> _ComplexType;
879 // Floating point types
881 struct __libcpp_complex_overload_traits<_Tp, false, true>
883 typedef _Tp _ValueType;
884 typedef complex<_Tp> _ComplexType;
890 inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_SINCE_CXX14
892 real(const complex<_Tp>& __c)
898 inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_SINCE_CXX14
899 typename __libcpp_complex_overload_traits<_Tp>::_ValueType
908 inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_SINCE_CXX14
910 imag(const complex<_Tp>& __c)
916 inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_SINCE_CXX14
917 typename __libcpp_complex_overload_traits<_Tp>::_ValueType
926 inline _LIBCPP_INLINE_VISIBILITY
928 abs(const complex<_Tp>& __c)
930 return std::hypot(__c.real(), __c.imag());
936 inline _LIBCPP_INLINE_VISIBILITY
938 arg(const complex<_Tp>& __c)
940 return std::atan2(__c.imag(), __c.real());
943 template <class _Tp, __enable_if_t<is_same<_Tp, long double>::value, int> = 0>
944 inline _LIBCPP_INLINE_VISIBILITY
948 return std::atan2l(0.L, __re);
951 template<class _Tp, __enable_if_t<is_integral<_Tp>::value || is_same<_Tp, double>::value, int> = 0>
952 inline _LIBCPP_INLINE_VISIBILITY
956 return std::atan2(0., __re);
959 template <class _Tp, __enable_if_t<is_same<_Tp, float>::value, int> = 0>
960 inline _LIBCPP_INLINE_VISIBILITY
964 return std::atan2f(0.F, __re);
970 inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_SINCE_CXX20
972 norm(const complex<_Tp>& __c)
974 if (std::__constexpr_isinf(__c.real()))
975 return std::abs(__c.real());
976 if (std::__constexpr_isinf(__c.imag()))
977 return std::abs(__c.imag());
978 return __c.real() * __c.real() + __c.imag() * __c.imag();
982 inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_SINCE_CXX20
983 typename __libcpp_complex_overload_traits<_Tp>::_ValueType
986 typedef typename __libcpp_complex_overload_traits<_Tp>::_ValueType _ValueType;
987 return static_cast<_ValueType>(__re) * __re;
993 inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_SINCE_CXX20
995 conj(const complex<_Tp>& __c)
997 return complex<_Tp>(__c.real(), -__c.imag());
1000 template <class _Tp>
1001 inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_SINCE_CXX20
1002 typename __libcpp_complex_overload_traits<_Tp>::_ComplexType
1005 typedef typename __libcpp_complex_overload_traits<_Tp>::_ComplexType _ComplexType;
1006 return _ComplexType(__re);
1014 inline _LIBCPP_INLINE_VISIBILITY
1016 proj(const complex<_Tp>& __c)
1018 complex<_Tp> __r = __c;
1019 if (std::__constexpr_isinf(__c.real()) || std::__constexpr_isinf(__c.imag()))
1020 __r = complex<_Tp>(INFINITY, std::copysign(_Tp(0), __c.imag()));
1024 template <class _Tp, __enable_if_t<is_floating_point<_Tp>::value, int> = 0>
1025 inline _LIBCPP_INLINE_VISIBILITY
1026 typename __libcpp_complex_overload_traits<_Tp>::_ComplexType
1029 if (std::__constexpr_isinf(__re))
1030 __re = std::abs(__re);
1031 return complex<_Tp>(__re);
1034 template <class _Tp, __enable_if_t<is_integral<_Tp>::value, int> = 0>
1035 inline _LIBCPP_INLINE_VISIBILITY
1036 typename __libcpp_complex_overload_traits<_Tp>::_ComplexType
1039 typedef typename __libcpp_complex_overload_traits<_Tp>::_ComplexType _ComplexType;
1040 return _ComplexType(__re);
1046 _LIBCPP_HIDE_FROM_ABI complex<_Tp>
1047 polar(const _Tp& __rho, const _Tp& __theta = _Tp())
1049 if (std::__constexpr_isnan(__rho) || std::signbit(__rho))
1050 return complex<_Tp>(_Tp(NAN), _Tp(NAN));
1051 if (std::__constexpr_isnan(__theta))
1053 if (std::__constexpr_isinf(__rho))
1054 return complex<_Tp>(__rho, __theta);
1055 return complex<_Tp>(__theta, __theta);
1057 if (std::__constexpr_isinf(__theta))
1059 if (std::__constexpr_isinf(__rho))
1060 return complex<_Tp>(__rho, _Tp(NAN));
1061 return complex<_Tp>(_Tp(NAN), _Tp(NAN));
1063 _Tp __x = __rho * std::cos(__theta);
1064 if (std::__constexpr_isnan(__x))
1066 _Tp __y = __rho * std::sin(__theta);
1067 if (std::__constexpr_isnan(__y))
1069 return complex<_Tp>(__x, __y);
1075 inline _LIBCPP_INLINE_VISIBILITY
1077 log(const complex<_Tp>& __x)
1079 return complex<_Tp>(std::log(std::abs(__x)), std::arg(__x));
1085 inline _LIBCPP_INLINE_VISIBILITY
1087 log10(const complex<_Tp>& __x)
1089 return std::log(__x) / std::log(_Tp(10));
1095 _LIBCPP_HIDE_FROM_ABI complex<_Tp>
1096 sqrt(const complex<_Tp>& __x)
1098 if (std::__constexpr_isinf(__x.imag()))
1099 return complex<_Tp>(_Tp(INFINITY), __x.imag());
1100 if (std::__constexpr_isinf(__x.real()))
1102 if (__x.real() > _Tp(0))
1103 return complex<_Tp>(__x.real(), std::__constexpr_isnan(__x.imag()) ? __x.imag() : std::copysign(_Tp(0), __x.imag()));
1104 return complex<_Tp>(std::__constexpr_isnan(__x.imag()) ? __x.imag() : _Tp(0), std::copysign(__x.real(), __x.imag()));
1106 return std::polar(std::sqrt(std::abs(__x)), std::arg(__x) / _Tp(2));
1112 _LIBCPP_HIDE_FROM_ABI complex<_Tp>
1113 exp(const complex<_Tp>& __x)
1115 _Tp __i = __x.imag();
1117 return complex<_Tp>(std::exp(__x.real()), std::copysign(_Tp(0), __x.imag()));
1119 if (std::__constexpr_isinf(__x.real()))
1121 if (__x.real() < _Tp(0))
1123 if (!std::__constexpr_isfinite(__i))
1126 else if (__i == 0 || !std::__constexpr_isfinite(__i))
1128 if (std::__constexpr_isinf(__i))
1130 return complex<_Tp>(__x.real(), __i);
1133 _Tp __e = std::exp(__x.real());
1134 return complex<_Tp>(__e * std::cos(__i), __e * std::sin(__i));
1140 inline _LIBCPP_INLINE_VISIBILITY
1142 pow(const complex<_Tp>& __x, const complex<_Tp>& __y)
1144 return std::exp(__y * std::log(__x));
1147 template<class _Tp, class _Up>
1148 inline _LIBCPP_INLINE_VISIBILITY
1149 complex<typename __promote<_Tp, _Up>::type>
1150 pow(const complex<_Tp>& __x, const complex<_Up>& __y)
1152 typedef complex<typename __promote<_Tp, _Up>::type> result_type;
1153 return _VSTD::pow(result_type(__x), result_type(__y));
1156 template<class _Tp, class _Up, __enable_if_t<is_arithmetic<_Up>::value, int> = 0>
1157 inline _LIBCPP_INLINE_VISIBILITY
1158 complex<typename __promote<_Tp, _Up>::type>
1159 pow(const complex<_Tp>& __x, const _Up& __y)
1161 typedef complex<typename __promote<_Tp, _Up>::type> result_type;
1162 return _VSTD::pow(result_type(__x), result_type(__y));
1165 template<class _Tp, class _Up, __enable_if_t<is_arithmetic<_Tp>::value, int> = 0>
1166 inline _LIBCPP_INLINE_VISIBILITY
1167 complex<typename __promote<_Tp, _Up>::type>
1168 pow(const _Tp& __x, const complex<_Up>& __y)
1170 typedef complex<typename __promote<_Tp, _Up>::type> result_type;
1171 return _VSTD::pow(result_type(__x), result_type(__y));
1174 // __sqr, computes pow(x, 2)
1177 inline _LIBCPP_INLINE_VISIBILITY
1179 __sqr(const complex<_Tp>& __x)
1181 return complex<_Tp>((__x.real() - __x.imag()) * (__x.real() + __x.imag()),
1182 _Tp(2) * __x.real() * __x.imag());
1188 _LIBCPP_HIDE_FROM_ABI complex<_Tp>
1189 asinh(const complex<_Tp>& __x)
1191 const _Tp __pi(atan2(+0., -0.));
1192 if (std::__constexpr_isinf(__x.real()))
1194 if (std::__constexpr_isnan(__x.imag()))
1196 if (std::__constexpr_isinf(__x.imag()))
1197 return complex<_Tp>(__x.real(), std::copysign(__pi * _Tp(0.25), __x.imag()));
1198 return complex<_Tp>(__x.real(), std::copysign(_Tp(0), __x.imag()));
1200 if (std::__constexpr_isnan(__x.real()))
1202 if (std::__constexpr_isinf(__x.imag()))
1203 return complex<_Tp>(__x.imag(), __x.real());
1204 if (__x.imag() == 0)
1206 return complex<_Tp>(__x.real(), __x.real());
1208 if (std::__constexpr_isinf(__x.imag()))
1209 return complex<_Tp>(std::copysign(__x.imag(), __x.real()), std::copysign(__pi/_Tp(2), __x.imag()));
1210 complex<_Tp> __z = std::log(__x + std::sqrt(std::__sqr(__x) + _Tp(1)));
1211 return complex<_Tp>(std::copysign(__z.real(), __x.real()), std::copysign(__z.imag(), __x.imag()));
1217 _LIBCPP_HIDE_FROM_ABI complex<_Tp>
1218 acosh(const complex<_Tp>& __x)
1220 const _Tp __pi(atan2(+0., -0.));
1221 if (std::__constexpr_isinf(__x.real()))
1223 if (std::__constexpr_isnan(__x.imag()))
1224 return complex<_Tp>(std::abs(__x.real()), __x.imag());
1225 if (std::__constexpr_isinf(__x.imag()))
1228 return complex<_Tp>(__x.real(), std::copysign(__pi * _Tp(0.25), __x.imag()));
1230 return complex<_Tp>(-__x.real(), std::copysign(__pi * _Tp(0.75), __x.imag()));
1233 return complex<_Tp>(-__x.real(), std::copysign(__pi, __x.imag()));
1234 return complex<_Tp>(__x.real(), std::copysign(_Tp(0), __x.imag()));
1236 if (std::__constexpr_isnan(__x.real()))
1238 if (std::__constexpr_isinf(__x.imag()))
1239 return complex<_Tp>(std::abs(__x.imag()), __x.real());
1240 return complex<_Tp>(__x.real(), __x.real());
1242 if (std::__constexpr_isinf(__x.imag()))
1243 return complex<_Tp>(std::abs(__x.imag()), std::copysign(__pi/_Tp(2), __x.imag()));
1244 complex<_Tp> __z = std::log(__x + std::sqrt(std::__sqr(__x) - _Tp(1)));
1245 return complex<_Tp>(std::copysign(__z.real(), _Tp(0)), std::copysign(__z.imag(), __x.imag()));
1251 _LIBCPP_HIDE_FROM_ABI complex<_Tp>
1252 atanh(const complex<_Tp>& __x)
1254 const _Tp __pi(atan2(+0., -0.));
1255 if (std::__constexpr_isinf(__x.imag()))
1257 return complex<_Tp>(std::copysign(_Tp(0), __x.real()), std::copysign(__pi/_Tp(2), __x.imag()));
1259 if (std::__constexpr_isnan(__x.imag()))
1261 if (std::__constexpr_isinf(__x.real()) || __x.real() == 0)
1262 return complex<_Tp>(std::copysign(_Tp(0), __x.real()), __x.imag());
1263 return complex<_Tp>(__x.imag(), __x.imag());
1265 if (std::__constexpr_isnan(__x.real()))
1267 return complex<_Tp>(__x.real(), __x.real());
1269 if (std::__constexpr_isinf(__x.real()))
1271 return complex<_Tp>(std::copysign(_Tp(0), __x.real()), std::copysign(__pi/_Tp(2), __x.imag()));
1273 if (std::abs(__x.real()) == _Tp(1) && __x.imag() == _Tp(0))
1275 return complex<_Tp>(std::copysign(_Tp(INFINITY), __x.real()), std::copysign(_Tp(0), __x.imag()));
1277 complex<_Tp> __z = std::log((_Tp(1) + __x) / (_Tp(1) - __x)) / _Tp(2);
1278 return complex<_Tp>(std::copysign(__z.real(), __x.real()), std::copysign(__z.imag(), __x.imag()));
1284 _LIBCPP_HIDE_FROM_ABI complex<_Tp>
1285 sinh(const complex<_Tp>& __x)
1287 if (std::__constexpr_isinf(__x.real()) && !std::__constexpr_isfinite(__x.imag()))
1288 return complex<_Tp>(__x.real(), _Tp(NAN));
1289 if (__x.real() == 0 && !std::__constexpr_isfinite(__x.imag()))
1290 return complex<_Tp>(__x.real(), _Tp(NAN));
1291 if (__x.imag() == 0 && !std::__constexpr_isfinite(__x.real()))
1293 return complex<_Tp>(std::sinh(__x.real()) * std::cos(__x.imag()), std::cosh(__x.real()) * std::sin(__x.imag()));
1299 _LIBCPP_HIDE_FROM_ABI complex<_Tp>
1300 cosh(const complex<_Tp>& __x)
1302 if (std::__constexpr_isinf(__x.real()) && !std::__constexpr_isfinite(__x.imag()))
1303 return complex<_Tp>(std::abs(__x.real()), _Tp(NAN));
1304 if (__x.real() == 0 && !std::__constexpr_isfinite(__x.imag()))
1305 return complex<_Tp>(_Tp(NAN), __x.real());
1306 if (__x.real() == 0 && __x.imag() == 0)
1307 return complex<_Tp>(_Tp(1), __x.imag());
1308 if (__x.imag() == 0 && !std::__constexpr_isfinite(__x.real()))
1309 return complex<_Tp>(std::abs(__x.real()), __x.imag());
1310 return complex<_Tp>(std::cosh(__x.real()) * std::cos(__x.imag()), std::sinh(__x.real()) * std::sin(__x.imag()));
1316 _LIBCPP_HIDE_FROM_ABI complex<_Tp>
1317 tanh(const complex<_Tp>& __x)
1319 if (std::__constexpr_isinf(__x.real()))
1321 if (!std::__constexpr_isfinite(__x.imag()))
1322 return complex<_Tp>(std::copysign(_Tp(1), __x.real()), _Tp(0));
1323 return complex<_Tp>(std::copysign(_Tp(1), __x.real()), std::copysign(_Tp(0), std::sin(_Tp(2) * __x.imag())));
1325 if (std::__constexpr_isnan(__x.real()) && __x.imag() == 0)
1327 _Tp __2r(_Tp(2) * __x.real());
1328 _Tp __2i(_Tp(2) * __x.imag());
1329 _Tp __d(std::cosh(__2r) + std::cos(__2i));
1330 _Tp __2rsh(std::sinh(__2r));
1331 if (std::__constexpr_isinf(__2rsh) && std::__constexpr_isinf(__d))
1332 return complex<_Tp>(__2rsh > _Tp(0) ? _Tp(1) : _Tp(-1),
1333 __2i > _Tp(0) ? _Tp(0) : _Tp(-0.));
1334 return complex<_Tp>(__2rsh/__d, std::sin(__2i)/__d);
1340 _LIBCPP_HIDE_FROM_ABI complex<_Tp>
1341 asin(const complex<_Tp>& __x)
1343 complex<_Tp> __z = std::asinh(complex<_Tp>(-__x.imag(), __x.real()));
1344 return complex<_Tp>(__z.imag(), -__z.real());
1350 _LIBCPP_HIDE_FROM_ABI complex<_Tp>
1351 acos(const complex<_Tp>& __x)
1353 const _Tp __pi(atan2(+0., -0.));
1354 if (std::__constexpr_isinf(__x.real()))
1356 if (std::__constexpr_isnan(__x.imag()))
1357 return complex<_Tp>(__x.imag(), __x.real());
1358 if (std::__constexpr_isinf(__x.imag()))
1360 if (__x.real() < _Tp(0))
1361 return complex<_Tp>(_Tp(0.75) * __pi, -__x.imag());
1362 return complex<_Tp>(_Tp(0.25) * __pi, -__x.imag());
1364 if (__x.real() < _Tp(0))
1365 return complex<_Tp>(__pi, std::signbit(__x.imag()) ? -__x.real() : __x.real());
1366 return complex<_Tp>(_Tp(0), std::signbit(__x.imag()) ? __x.real() : -__x.real());
1368 if (std::__constexpr_isnan(__x.real()))
1370 if (std::__constexpr_isinf(__x.imag()))
1371 return complex<_Tp>(__x.real(), -__x.imag());
1372 return complex<_Tp>(__x.real(), __x.real());
1374 if (std::__constexpr_isinf(__x.imag()))
1375 return complex<_Tp>(__pi/_Tp(2), -__x.imag());
1376 if (__x.real() == 0 && (__x.imag() == 0 || std::isnan(__x.imag())))
1377 return complex<_Tp>(__pi/_Tp(2), -__x.imag());
1378 complex<_Tp> __z = std::log(__x + std::sqrt(std::__sqr(__x) - _Tp(1)));
1379 if (std::signbit(__x.imag()))
1380 return complex<_Tp>(std::abs(__z.imag()), std::abs(__z.real()));
1381 return complex<_Tp>(std::abs(__z.imag()), -std::abs(__z.real()));
1387 _LIBCPP_HIDE_FROM_ABI complex<_Tp>
1388 atan(const complex<_Tp>& __x)
1390 complex<_Tp> __z = std::atanh(complex<_Tp>(-__x.imag(), __x.real()));
1391 return complex<_Tp>(__z.imag(), -__z.real());
1397 _LIBCPP_HIDE_FROM_ABI complex<_Tp>
1398 sin(const complex<_Tp>& __x)
1400 complex<_Tp> __z = std::sinh(complex<_Tp>(-__x.imag(), __x.real()));
1401 return complex<_Tp>(__z.imag(), -__z.real());
1407 inline _LIBCPP_INLINE_VISIBILITY
1409 cos(const complex<_Tp>& __x)
1411 return std::cosh(complex<_Tp>(-__x.imag(), __x.real()));
1417 _LIBCPP_HIDE_FROM_ABI complex<_Tp>
1418 tan(const complex<_Tp>& __x)
1420 complex<_Tp> __z = std::tanh(complex<_Tp>(-__x.imag(), __x.real()));
1421 return complex<_Tp>(__z.imag(), -__z.real());
1424 #if !defined(_LIBCPP_HAS_NO_LOCALIZATION)
1425 template<class _Tp, class _CharT, class _Traits>
1426 _LIBCPP_HIDE_FROM_ABI basic_istream<_CharT, _Traits>&
1427 operator>>(basic_istream<_CharT, _Traits>& __is, complex<_Tp>& __x)
1432 if (__is.peek() == _CharT('('))
1440 _CharT __c = __is.peek();
1441 if (__c == _CharT(','))
1450 if (__c == _CharT(')'))
1453 __x = complex<_Tp>(__r, __i);
1456 __is.setstate(__is.failbit);
1459 __is.setstate(__is.failbit);
1461 else if (__c == _CharT(')'))
1464 __x = complex<_Tp>(__r, _Tp(0));
1467 __is.setstate(__is.failbit);
1470 __is.setstate(__is.failbit);
1477 __x = complex<_Tp>(__r, _Tp(0));
1479 __is.setstate(__is.failbit);
1483 __is.setstate(__is.failbit);
1487 template<class _Tp, class _CharT, class _Traits>
1488 _LIBCPP_HIDE_FROM_ABI basic_ostream<_CharT, _Traits>&
1489 operator<<(basic_ostream<_CharT, _Traits>& __os, const complex<_Tp>& __x)
1491 basic_ostringstream<_CharT, _Traits> __s;
1492 __s.flags(__os.flags());
1493 __s.imbue(__os.getloc());
1494 __s.precision(__os.precision());
1495 __s << '(' << __x.real() << ',' << __x.imag() << ')';
1496 return __os << __s.str();
1498 #endif // !_LIBCPP_HAS_NO_LOCALIZATION
1500 #if _LIBCPP_STD_VER >= 14
1501 // Literal suffix for complex number literals [complex.literals]
1502 inline namespace literals
1504 inline namespace complex_literals
1506 _LIBCPP_HIDE_FROM_ABI constexpr complex<long double> operator""il(long double __im)
1508 return { 0.0l, __im };
1511 _LIBCPP_HIDE_FROM_ABI constexpr complex<long double> operator""il(unsigned long long __im)
1513 return { 0.0l, static_cast<long double>(__im) };
1517 _LIBCPP_HIDE_FROM_ABI constexpr complex<double> operator""i(long double __im)
1519 return { 0.0, static_cast<double>(__im) };
1522 _LIBCPP_HIDE_FROM_ABI constexpr complex<double> operator""i(unsigned long long __im)
1524 return { 0.0, static_cast<double>(__im) };
1528 _LIBCPP_HIDE_FROM_ABI constexpr complex<float> operator""if(long double __im)
1530 return { 0.0f, static_cast<float>(__im) };
1533 _LIBCPP_HIDE_FROM_ABI constexpr complex<float> operator""if(unsigned long long __im)
1535 return { 0.0f, static_cast<float>(__im) };
1537 } // namespace complex_literals
1538 } // namespace literals
1541 _LIBCPP_END_NAMESPACE_STD
1543 #if !defined(_LIBCPP_REMOVE_TRANSITIVE_INCLUDES) && _LIBCPP_STD_VER <= 20
1545 # include <stdexcept>
1546 # include <type_traits>
1549 #endif // _LIBCPP_COMPLEX