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>&);
230 // [complex.tuple], tuple interface
231 template<class T> struct tuple_size; // Since C++26
232 template<size_t I, class T> struct tuple_element; // Since C++26
233 template<class T> struct tuple_size<complex<T>>; // Since C++26
234 template<size_t I, class T> struct tuple_element<I, complex<T>>; // Since C++26
235 template<size_t I, class T>
236 constexpr T& get(complex<T>&) noexcept; // Since C++26
237 template<size_t I, class T>
238 constexpr T&& get(complex<T>&&) noexcept; // Since C++26
239 template<size_t I, class T>
240 constexpr const T& get(const complex<T>&) noexcept; // Since C++26
241 template<size_t I, class T>
242 constexpr const T&& get(const complex<T>&&) noexcept; // Since C++26
244 // [complex.literals], complex literals
245 inline namespace literals {
246 inline namespace complex_literals {
247 constexpr complex<long double> operator""il(long double); // Since C++14
248 constexpr complex<long double> operator""il(unsigned long long); // Since C++14
249 constexpr complex<double> operator""i(long double); // Since C++14
250 constexpr complex<double> operator""i(unsigned long long); // Since C++14
251 constexpr complex<float> operator""if(long double); // Since C++14
252 constexpr complex<float> operator""if(unsigned long long); // Since C++14
260 #include <__fwd/complex.h>
261 #include <__fwd/tuple.h>
262 #include <__tuple/tuple_element.h>
263 #include <__tuple/tuple_size.h>
264 #include <__type_traits/conditional.h>
265 #include <__utility/move.h>
269 #if _LIBCPP_HAS_LOCALIZATION
270 # include <sstream> // for std::basic_ostringstream
273 #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
274 # pragma GCC system_header
278 #include <__undef_macros>
280 _LIBCPP_BEGIN_NAMESPACE_STD
283 class _LIBCPP_TEMPLATE_VIS complex;
285 template <class _Tp, __enable_if_t<is_floating_point<_Tp>::value, int> = 0>
286 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 complex<_Tp>
287 operator*(const complex<_Tp>& __z, const complex<_Tp>& __w);
289 template <class _Tp, __enable_if_t<!is_floating_point<_Tp>::value, int> = 0>
290 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 complex<_Tp>
291 operator*(const complex<_Tp>& __z, const complex<_Tp>& __w);
293 template <class _Tp, __enable_if_t<is_floating_point<_Tp>::value, int> = 0>
294 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 complex<_Tp>
295 operator/(const complex<_Tp>& __x, const complex<_Tp>& __y);
297 template <class _Tp, __enable_if_t<!is_floating_point<_Tp>::value, int> = 0>
298 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 complex<_Tp>
299 operator/(const complex<_Tp>& __x, const complex<_Tp>& __y);
302 class _LIBCPP_TEMPLATE_VIS complex {
304 typedef _Tp value_type;
311 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX14
312 complex(const value_type& __re = value_type(), const value_type& __im = value_type())
313 : __re_(__re), __im_(__im) {}
315 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX14 complex(const complex<_Xp>& __c)
316 : __re_(__c.real()), __im_(__c.imag()) {}
318 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX14 value_type real() const { return __re_; }
319 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX14 value_type imag() const { return __im_; }
321 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 void real(value_type __re) { __re_ = __re; }
322 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 void imag(value_type __im) { __im_ = __im; }
324 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 complex& operator=(const value_type& __re) {
326 __im_ = value_type();
329 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 complex& operator+=(const value_type& __re) {
333 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 complex& operator-=(const value_type& __re) {
337 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 complex& operator*=(const value_type& __re) {
342 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 complex& operator/=(const value_type& __re) {
349 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 complex& operator=(const complex<_Xp>& __c) {
355 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 complex& operator+=(const complex<_Xp>& __c) {
361 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 complex& operator-=(const complex<_Xp>& __c) {
367 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 complex& operator*=(const complex<_Xp>& __c) {
368 *this = *this * complex(__c.real(), __c.imag());
372 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 complex& operator/=(const complex<_Xp>& __c) {
373 *this = *this / complex(__c.real(), __c.imag());
377 #if _LIBCPP_STD_VER >= 26
378 template <size_t _Ip, class _Xp>
379 friend _LIBCPP_HIDE_FROM_ABI constexpr _Xp& get(complex<_Xp>&) noexcept;
381 template <size_t _Ip, class _Xp>
382 friend _LIBCPP_HIDE_FROM_ABI constexpr _Xp&& get(complex<_Xp>&&) noexcept;
384 template <size_t _Ip, class _Xp>
385 friend _LIBCPP_HIDE_FROM_ABI constexpr const _Xp& get(const complex<_Xp>&) noexcept;
387 template <size_t _Ip, class _Xp>
388 friend _LIBCPP_HIDE_FROM_ABI constexpr const _Xp&& get(const complex<_Xp>&&) noexcept;
393 class _LIBCPP_TEMPLATE_VIS complex<double>;
395 class _LIBCPP_TEMPLATE_VIS complex<long double>;
397 struct __from_builtin_tag {};
401 __conditional_t<is_same<_Tp, float>::value,
403 __conditional_t<is_same<_Tp, double>::value, _Complex double, _Complex long double> >;
406 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR __complex_t<_Tp> __make_complex(_Tp __re, _Tp __im) {
407 #if __has_builtin(__builtin_complex)
408 return __builtin_complex(__re, __im);
410 return __complex_t<_Tp>{__re, __im};
415 class _LIBCPP_TEMPLATE_VIS complex<float> {
420 typedef float value_type;
422 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR complex(float __re = 0.0f, float __im = 0.0f) : __re_(__re), __im_(__im) {}
424 template <class _Tag, __enable_if_t<_IsSame<_Tag, __from_builtin_tag>::value, int> = 0>
425 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR explicit complex(_Tag, _Complex float __v)
426 : __re_(__real__ __v), __im_(__imag__ __v) {}
428 _LIBCPP_HIDE_FROM_ABI explicit _LIBCPP_CONSTEXPR complex(const complex<double>& __c);
429 _LIBCPP_HIDE_FROM_ABI explicit _LIBCPP_CONSTEXPR complex(const complex<long double>& __c);
431 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR float real() const { return __re_; }
432 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR float imag() const { return __im_; }
434 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 void real(value_type __re) { __re_ = __re; }
435 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 void imag(value_type __im) { __im_ = __im; }
437 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR _Complex float __builtin() const { return std::__make_complex(__re_, __im_); }
438 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX14 void __builtin(_Complex float __f) {
439 __re_ = __real__ __f;
440 __im_ = __imag__ __f;
443 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 complex& operator=(float __re) {
445 __im_ = value_type();
448 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 complex& operator+=(float __re) {
452 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 complex& operator-=(float __re) {
456 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 complex& operator*=(float __re) {
461 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 complex& operator/=(float __re) {
468 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 complex& operator=(const complex<_Xp>& __c) {
474 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 complex& operator+=(const complex<_Xp>& __c) {
480 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 complex& operator-=(const complex<_Xp>& __c) {
486 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 complex& operator*=(const complex<_Xp>& __c) {
487 *this = *this * complex(__c.real(), __c.imag());
491 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 complex& operator/=(const complex<_Xp>& __c) {
492 *this = *this / complex(__c.real(), __c.imag());
496 #if _LIBCPP_STD_VER >= 26
497 template <size_t _Ip, class _Xp>
498 friend _LIBCPP_HIDE_FROM_ABI constexpr _Xp& get(complex<_Xp>&) noexcept;
500 template <size_t _Ip, class _Xp>
501 friend _LIBCPP_HIDE_FROM_ABI constexpr _Xp&& get(complex<_Xp>&&) noexcept;
503 template <size_t _Ip, class _Xp>
504 friend _LIBCPP_HIDE_FROM_ABI constexpr const _Xp& get(const complex<_Xp>&) noexcept;
506 template <size_t _Ip, class _Xp>
507 friend _LIBCPP_HIDE_FROM_ABI constexpr const _Xp&& get(const complex<_Xp>&&) noexcept;
512 class _LIBCPP_TEMPLATE_VIS complex<double> {
517 typedef double value_type;
519 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR complex(double __re = 0.0, double __im = 0.0) : __re_(__re), __im_(__im) {}
521 template <class _Tag, __enable_if_t<_IsSame<_Tag, __from_builtin_tag>::value, int> = 0>
522 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR explicit complex(_Tag, _Complex double __v)
523 : __re_(__real__ __v), __im_(__imag__ __v) {}
525 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR complex(const complex<float>& __c);
526 _LIBCPP_HIDE_FROM_ABI explicit _LIBCPP_CONSTEXPR complex(const complex<long double>& __c);
528 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR double real() const { return __re_; }
529 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR double imag() const { return __im_; }
531 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 void real(value_type __re) { __re_ = __re; }
532 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 void imag(value_type __im) { __im_ = __im; }
534 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR _Complex double __builtin() const {
535 return std::__make_complex(__re_, __im_);
538 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX14 void __builtin(_Complex double __f) {
539 __re_ = __real__ __f;
540 __im_ = __imag__ __f;
543 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 complex& operator=(double __re) {
545 __im_ = value_type();
548 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 complex& operator+=(double __re) {
552 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 complex& operator-=(double __re) {
556 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 complex& operator*=(double __re) {
561 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 complex& operator/=(double __re) {
568 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 complex& operator=(const complex<_Xp>& __c) {
574 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 complex& operator+=(const complex<_Xp>& __c) {
580 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 complex& operator-=(const complex<_Xp>& __c) {
586 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 complex& operator*=(const complex<_Xp>& __c) {
587 *this = *this * complex(__c.real(), __c.imag());
591 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 complex& operator/=(const complex<_Xp>& __c) {
592 *this = *this / complex(__c.real(), __c.imag());
596 #if _LIBCPP_STD_VER >= 26
597 template <size_t _Ip, class _Xp>
598 friend _LIBCPP_HIDE_FROM_ABI constexpr _Xp& get(complex<_Xp>&) noexcept;
600 template <size_t _Ip, class _Xp>
601 friend _LIBCPP_HIDE_FROM_ABI constexpr _Xp&& get(complex<_Xp>&&) noexcept;
603 template <size_t _Ip, class _Xp>
604 friend _LIBCPP_HIDE_FROM_ABI constexpr const _Xp& get(const complex<_Xp>&) noexcept;
606 template <size_t _Ip, class _Xp>
607 friend _LIBCPP_HIDE_FROM_ABI constexpr const _Xp&& get(const complex<_Xp>&&) noexcept;
612 class _LIBCPP_TEMPLATE_VIS complex<long double> {
617 typedef long double value_type;
619 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR complex(long double __re = 0.0L, long double __im = 0.0L)
620 : __re_(__re), __im_(__im) {}
622 template <class _Tag, __enable_if_t<_IsSame<_Tag, __from_builtin_tag>::value, int> = 0>
623 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR explicit complex(_Tag, _Complex long double __v)
624 : __re_(__real__ __v), __im_(__imag__ __v) {}
626 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR complex(const complex<float>& __c);
627 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR complex(const complex<double>& __c);
629 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR long double real() const { return __re_; }
630 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR long double imag() const { return __im_; }
632 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 void real(value_type __re) { __re_ = __re; }
633 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 void imag(value_type __im) { __im_ = __im; }
635 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR _Complex long double __builtin() const {
636 return std::__make_complex(__re_, __im_);
639 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX14 void __builtin(_Complex long double __f) {
640 __re_ = __real__ __f;
641 __im_ = __imag__ __f;
644 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 complex& operator=(long double __re) {
646 __im_ = value_type();
649 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 complex& operator+=(long double __re) {
653 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 complex& operator-=(long double __re) {
657 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 complex& operator*=(long double __re) {
662 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 complex& operator/=(long double __re) {
669 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 complex& operator=(const complex<_Xp>& __c) {
675 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 complex& operator+=(const complex<_Xp>& __c) {
681 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 complex& operator-=(const complex<_Xp>& __c) {
687 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 complex& operator*=(const complex<_Xp>& __c) {
688 *this = *this * complex(__c.real(), __c.imag());
692 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 complex& operator/=(const complex<_Xp>& __c) {
693 *this = *this / complex(__c.real(), __c.imag());
697 #if _LIBCPP_STD_VER >= 26
698 template <size_t _Ip, class _Xp>
699 friend _LIBCPP_HIDE_FROM_ABI constexpr _Xp& get(complex<_Xp>&) noexcept;
701 template <size_t _Ip, class _Xp>
702 friend _LIBCPP_HIDE_FROM_ABI constexpr _Xp&& get(complex<_Xp>&&) noexcept;
704 template <size_t _Ip, class _Xp>
705 friend _LIBCPP_HIDE_FROM_ABI constexpr const _Xp& get(const complex<_Xp>&) noexcept;
707 template <size_t _Ip, class _Xp>
708 friend _LIBCPP_HIDE_FROM_ABI constexpr const _Xp&& get(const complex<_Xp>&&) noexcept;
712 inline _LIBCPP_CONSTEXPR complex<float>::complex(const complex<double>& __c) : __re_(__c.real()), __im_(__c.imag()) {}
714 inline _LIBCPP_CONSTEXPR complex<float>::complex(const complex<long double>& __c)
715 : __re_(__c.real()), __im_(__c.imag()) {}
717 inline _LIBCPP_CONSTEXPR complex<double>::complex(const complex<float>& __c) : __re_(__c.real()), __im_(__c.imag()) {}
719 inline _LIBCPP_CONSTEXPR complex<double>::complex(const complex<long double>& __c)
720 : __re_(__c.real()), __im_(__c.imag()) {}
722 inline _LIBCPP_CONSTEXPR complex<long double>::complex(const complex<float>& __c)
723 : __re_(__c.real()), __im_(__c.imag()) {}
725 inline _LIBCPP_CONSTEXPR complex<long double>::complex(const complex<double>& __c)
726 : __re_(__c.real()), __im_(__c.imag()) {}
731 inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 complex<_Tp>
732 operator+(const complex<_Tp>& __x, const complex<_Tp>& __y) {
733 complex<_Tp> __t(__x);
739 inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 complex<_Tp>
740 operator+(const complex<_Tp>& __x, const _Tp& __y) {
741 complex<_Tp> __t(__x);
747 inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 complex<_Tp>
748 operator+(const _Tp& __x, const complex<_Tp>& __y) {
749 complex<_Tp> __t(__y);
755 inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 complex<_Tp>
756 operator-(const complex<_Tp>& __x, const complex<_Tp>& __y) {
757 complex<_Tp> __t(__x);
763 inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 complex<_Tp>
764 operator-(const complex<_Tp>& __x, const _Tp& __y) {
765 complex<_Tp> __t(__x);
771 inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 complex<_Tp>
772 operator-(const _Tp& __x, const complex<_Tp>& __y) {
773 complex<_Tp> __t(-__y);
778 template <class _Tp, __enable_if_t<is_floating_point<_Tp>::value, int> >
779 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 complex<_Tp>
780 operator*(const complex<_Tp>& __lhs, const complex<_Tp>& __rhs) {
781 return complex<_Tp>(__from_builtin_tag(), __lhs.__builtin() * __rhs.__builtin());
784 template <class _Tp, __enable_if_t<!is_floating_point<_Tp>::value, int> >
785 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 complex<_Tp>
786 operator*(const complex<_Tp>& __z, const complex<_Tp>& __w) {
787 _Tp __a = __z.real();
788 _Tp __b = __z.imag();
789 _Tp __c = __w.real();
790 _Tp __d = __w.imag();
792 return complex<_Tp>((__a * __c) - (__b * __d), (__a * __d) + (__b * __c));
796 inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 complex<_Tp>
797 operator*(const complex<_Tp>& __x, const _Tp& __y) {
798 complex<_Tp> __t(__x);
804 inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 complex<_Tp>
805 operator*(const _Tp& __x, const complex<_Tp>& __y) {
806 complex<_Tp> __t(__y);
811 template <class _Tp, __enable_if_t<is_floating_point<_Tp>::value, int> >
812 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 complex<_Tp>
813 operator/(const complex<_Tp>& __lhs, const complex<_Tp>& __rhs) {
814 return complex<_Tp>(__from_builtin_tag(), __lhs.__builtin() / __rhs.__builtin());
817 template <class _Tp, __enable_if_t<!is_floating_point<_Tp>::value, int> >
818 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 complex<_Tp>
819 operator/(const complex<_Tp>& __z, const complex<_Tp>& __w) {
820 _Tp __a = __z.real();
821 _Tp __b = __z.imag();
822 _Tp __c = __w.real();
823 _Tp __d = __w.imag();
825 _Tp __denom = __c * __c + __d * __d;
826 return complex<_Tp>((__a * __c + __b * __d) / __denom, (__b * __c - __a * __d) / __denom);
830 inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 complex<_Tp>
831 operator/(const complex<_Tp>& __x, const _Tp& __y) {
832 return complex<_Tp>(__x.real() / __y, __x.imag() / __y);
836 inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 complex<_Tp>
837 operator/(const _Tp& __x, const complex<_Tp>& __y) {
838 complex<_Tp> __t(__x);
844 inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 complex<_Tp> operator+(const complex<_Tp>& __x) {
849 inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 complex<_Tp> operator-(const complex<_Tp>& __x) {
850 return complex<_Tp>(-__x.real(), -__x.imag());
854 inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX14 bool
855 operator==(const complex<_Tp>& __x, const complex<_Tp>& __y) {
856 return __x.real() == __y.real() && __x.imag() == __y.imag();
860 inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX14 bool operator==(const complex<_Tp>& __x, const _Tp& __y) {
861 return __x.real() == __y && __x.imag() == 0;
864 #if _LIBCPP_STD_VER <= 17
867 inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX14 bool operator==(const _Tp& __x, const complex<_Tp>& __y) {
868 return __x == __y.real() && 0 == __y.imag();
872 inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX14 bool
873 operator!=(const complex<_Tp>& __x, const complex<_Tp>& __y) {
874 return !(__x == __y);
878 inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX14 bool operator!=(const complex<_Tp>& __x, const _Tp& __y) {
879 return !(__x == __y);
883 inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX14 bool operator!=(const _Tp& __x, const complex<_Tp>& __y) {
884 return !(__x == __y);
891 template <class _Tp, bool = is_integral<_Tp>::value, bool = is_floating_point<_Tp>::value >
892 struct __libcpp_complex_overload_traits {};
896 struct __libcpp_complex_overload_traits<_Tp, true, false> {
897 typedef double _ValueType;
898 typedef complex<double> _ComplexType;
901 // Floating point types
903 struct __libcpp_complex_overload_traits<_Tp, false, true> {
904 typedef _Tp _ValueType;
905 typedef complex<_Tp> _ComplexType;
911 inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX14 _Tp real(const complex<_Tp>& __c) {
916 inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX14 typename __libcpp_complex_overload_traits<_Tp>::_ValueType
924 inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX14 _Tp imag(const complex<_Tp>& __c) {
929 inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX14 typename __libcpp_complex_overload_traits<_Tp>::_ValueType
937 inline _LIBCPP_HIDE_FROM_ABI _Tp abs(const complex<_Tp>& __c) {
938 return std::hypot(__c.real(), __c.imag());
944 inline _LIBCPP_HIDE_FROM_ABI _Tp arg(const complex<_Tp>& __c) {
945 return std::atan2(__c.imag(), __c.real());
948 template <class _Tp, __enable_if_t<is_same<_Tp, long double>::value, int> = 0>
949 inline _LIBCPP_HIDE_FROM_ABI long double arg(_Tp __re) {
950 return std::atan2l(0.L, __re);
953 template <class _Tp, __enable_if_t<is_integral<_Tp>::value || is_same<_Tp, double>::value, int> = 0>
954 inline _LIBCPP_HIDE_FROM_ABI double arg(_Tp __re) {
955 return std::atan2(0., __re);
958 template <class _Tp, __enable_if_t<is_same<_Tp, float>::value, int> = 0>
959 inline _LIBCPP_HIDE_FROM_ABI float arg(_Tp __re) {
960 return std::atan2f(0.F, __re);
966 inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 _Tp norm(const complex<_Tp>& __c) {
967 if (std::__constexpr_isinf(__c.real()))
968 return std::abs(__c.real());
969 if (std::__constexpr_isinf(__c.imag()))
970 return std::abs(__c.imag());
971 return __c.real() * __c.real() + __c.imag() * __c.imag();
975 inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 typename __libcpp_complex_overload_traits<_Tp>::_ValueType
977 typedef typename __libcpp_complex_overload_traits<_Tp>::_ValueType _ValueType;
978 return static_cast<_ValueType>(__re) * __re;
984 inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 complex<_Tp> conj(const complex<_Tp>& __c) {
985 return complex<_Tp>(__c.real(), -__c.imag());
989 inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 typename __libcpp_complex_overload_traits<_Tp>::_ComplexType
991 typedef typename __libcpp_complex_overload_traits<_Tp>::_ComplexType _ComplexType;
992 return _ComplexType(__re);
998 inline _LIBCPP_HIDE_FROM_ABI complex<_Tp> proj(const complex<_Tp>& __c) {
999 complex<_Tp> __r = __c;
1000 if (std::isinf(__c.real()) || std::isinf(__c.imag()))
1001 __r = complex<_Tp>(INFINITY, std::copysign(_Tp(0), __c.imag()));
1005 template <class _Tp, __enable_if_t<is_floating_point<_Tp>::value, int> = 0>
1006 inline _LIBCPP_HIDE_FROM_ABI typename __libcpp_complex_overload_traits<_Tp>::_ComplexType proj(_Tp __re) {
1007 if (std::isinf(__re))
1008 __re = std::abs(__re);
1009 return complex<_Tp>(__re);
1012 template <class _Tp, __enable_if_t<is_integral<_Tp>::value, int> = 0>
1013 inline _LIBCPP_HIDE_FROM_ABI typename __libcpp_complex_overload_traits<_Tp>::_ComplexType proj(_Tp __re) {
1014 typedef typename __libcpp_complex_overload_traits<_Tp>::_ComplexType _ComplexType;
1015 return _ComplexType(__re);
1020 template <class _Tp>
1021 _LIBCPP_HIDE_FROM_ABI complex<_Tp> polar(const _Tp& __rho, const _Tp& __theta = _Tp()) {
1022 if (std::isnan(__rho) || std::signbit(__rho))
1023 return complex<_Tp>(_Tp(NAN), _Tp(NAN));
1024 if (std::isnan(__theta)) {
1025 if (std::isinf(__rho))
1026 return complex<_Tp>(__rho, __theta);
1027 return complex<_Tp>(__theta, __theta);
1029 if (std::isinf(__theta)) {
1030 if (std::isinf(__rho))
1031 return complex<_Tp>(__rho, _Tp(NAN));
1032 return complex<_Tp>(_Tp(NAN), _Tp(NAN));
1034 _Tp __x = __rho * std::cos(__theta);
1035 if (std::isnan(__x))
1037 _Tp __y = __rho * std::sin(__theta);
1038 if (std::isnan(__y))
1040 return complex<_Tp>(__x, __y);
1045 template <class _Tp>
1046 inline _LIBCPP_HIDE_FROM_ABI complex<_Tp> log(const complex<_Tp>& __x) {
1047 return complex<_Tp>(std::log(std::abs(__x)), std::arg(__x));
1052 template <class _Tp>
1053 inline _LIBCPP_HIDE_FROM_ABI complex<_Tp> log10(const complex<_Tp>& __x) {
1054 return std::log(__x) / std::log(_Tp(10));
1059 template <class _Tp>
1060 _LIBCPP_HIDE_FROM_ABI complex<_Tp> sqrt(const complex<_Tp>& __x) {
1061 if (std::isinf(__x.imag()))
1062 return complex<_Tp>(_Tp(INFINITY), __x.imag());
1063 if (std::isinf(__x.real())) {
1064 if (__x.real() > _Tp(0))
1065 return complex<_Tp>(__x.real(), std::isnan(__x.imag()) ? __x.imag() : std::copysign(_Tp(0), __x.imag()));
1066 return complex<_Tp>(std::isnan(__x.imag()) ? __x.imag() : _Tp(0), std::copysign(__x.real(), __x.imag()));
1068 return std::polar(std::sqrt(std::abs(__x)), std::arg(__x) / _Tp(2));
1073 template <class _Tp>
1074 _LIBCPP_HIDE_FROM_ABI complex<_Tp> exp(const complex<_Tp>& __x) {
1075 _Tp __i = __x.imag();
1077 return complex<_Tp>(std::exp(__x.real()), std::copysign(_Tp(0), __x.imag()));
1079 if (std::isinf(__x.real())) {
1080 if (__x.real() < _Tp(0)) {
1081 if (!std::isfinite(__i))
1083 } else if (__i == 0 || !std::isfinite(__i)) {
1084 if (std::isinf(__i))
1086 return complex<_Tp>(__x.real(), __i);
1089 _Tp __e = std::exp(__x.real());
1090 return complex<_Tp>(__e * std::cos(__i), __e * std::sin(__i));
1095 template <class _Tp>
1096 inline _LIBCPP_HIDE_FROM_ABI complex<_Tp> pow(const complex<_Tp>& __x, const complex<_Tp>& __y) {
1097 return std::exp(__y * std::log(__x));
1100 template <class _Tp, class _Up, __enable_if_t<is_floating_point<_Tp>::value && is_floating_point<_Up>::value, int> = 0>
1101 inline _LIBCPP_HIDE_FROM_ABI complex<typename __promote<_Tp, _Up>::type>
1102 pow(const complex<_Tp>& __x, const complex<_Up>& __y) {
1103 typedef complex<typename __promote<_Tp, _Up>::type> result_type;
1104 return std::pow(result_type(__x), result_type(__y));
1107 template <class _Tp, class _Up, __enable_if_t<is_floating_point<_Tp>::value && is_arithmetic<_Up>::value, int> = 0>
1108 inline _LIBCPP_HIDE_FROM_ABI complex<typename __promote<_Tp, _Up>::type> pow(const complex<_Tp>& __x, const _Up& __y) {
1109 typedef complex<typename __promote<_Tp, _Up>::type> result_type;
1110 return std::pow(result_type(__x), result_type(__y));
1113 template <class _Tp, class _Up, __enable_if_t<is_arithmetic<_Tp>::value && is_floating_point<_Up>::value, int> = 0>
1114 inline _LIBCPP_HIDE_FROM_ABI complex<typename __promote<_Tp, _Up>::type> pow(const _Tp& __x, const complex<_Up>& __y) {
1115 typedef complex<typename __promote<_Tp, _Up>::type> result_type;
1116 return std::pow(result_type(__x), result_type(__y));
1119 // __sqr, computes pow(x, 2)
1121 template <class _Tp>
1122 inline _LIBCPP_HIDE_FROM_ABI complex<_Tp> __sqr(const complex<_Tp>& __x) {
1123 return complex<_Tp>((__x.real() - __x.imag()) * (__x.real() + __x.imag()), _Tp(2) * __x.real() * __x.imag());
1128 template <class _Tp>
1129 _LIBCPP_HIDE_FROM_ABI complex<_Tp> asinh(const complex<_Tp>& __x) {
1130 const _Tp __pi(atan2(+0., -0.));
1131 if (std::isinf(__x.real())) {
1132 if (std::isnan(__x.imag()))
1134 if (std::isinf(__x.imag()))
1135 return complex<_Tp>(__x.real(), std::copysign(__pi * _Tp(0.25), __x.imag()));
1136 return complex<_Tp>(__x.real(), std::copysign(_Tp(0), __x.imag()));
1138 if (std::isnan(__x.real())) {
1139 if (std::isinf(__x.imag()))
1140 return complex<_Tp>(__x.imag(), __x.real());
1141 if (__x.imag() == 0)
1143 return complex<_Tp>(__x.real(), __x.real());
1145 if (std::isinf(__x.imag()))
1146 return complex<_Tp>(std::copysign(__x.imag(), __x.real()), std::copysign(__pi / _Tp(2), __x.imag()));
1147 complex<_Tp> __z = std::log(__x + std::sqrt(std::__sqr(__x) + _Tp(1)));
1148 return complex<_Tp>(std::copysign(__z.real(), __x.real()), std::copysign(__z.imag(), __x.imag()));
1153 template <class _Tp>
1154 _LIBCPP_HIDE_FROM_ABI complex<_Tp> acosh(const complex<_Tp>& __x) {
1155 const _Tp __pi(atan2(+0., -0.));
1156 if (std::isinf(__x.real())) {
1157 if (std::isnan(__x.imag()))
1158 return complex<_Tp>(std::abs(__x.real()), __x.imag());
1159 if (std::isinf(__x.imag())) {
1161 return complex<_Tp>(__x.real(), std::copysign(__pi * _Tp(0.25), __x.imag()));
1163 return complex<_Tp>(-__x.real(), std::copysign(__pi * _Tp(0.75), __x.imag()));
1166 return complex<_Tp>(-__x.real(), std::copysign(__pi, __x.imag()));
1167 return complex<_Tp>(__x.real(), std::copysign(_Tp(0), __x.imag()));
1169 if (std::isnan(__x.real())) {
1170 if (std::isinf(__x.imag()))
1171 return complex<_Tp>(std::abs(__x.imag()), __x.real());
1172 return complex<_Tp>(__x.real(), __x.real());
1174 if (std::isinf(__x.imag()))
1175 return complex<_Tp>(std::abs(__x.imag()), std::copysign(__pi / _Tp(2), __x.imag()));
1176 complex<_Tp> __z = std::log(__x + std::sqrt(std::__sqr(__x) - _Tp(1)));
1177 return complex<_Tp>(std::copysign(__z.real(), _Tp(0)), std::copysign(__z.imag(), __x.imag()));
1182 template <class _Tp>
1183 _LIBCPP_HIDE_FROM_ABI complex<_Tp> atanh(const complex<_Tp>& __x) {
1184 const _Tp __pi(atan2(+0., -0.));
1185 if (std::isinf(__x.imag())) {
1186 return complex<_Tp>(std::copysign(_Tp(0), __x.real()), std::copysign(__pi / _Tp(2), __x.imag()));
1188 if (std::isnan(__x.imag())) {
1189 if (std::isinf(__x.real()) || __x.real() == 0)
1190 return complex<_Tp>(std::copysign(_Tp(0), __x.real()), __x.imag());
1191 return complex<_Tp>(__x.imag(), __x.imag());
1193 if (std::isnan(__x.real())) {
1194 return complex<_Tp>(__x.real(), __x.real());
1196 if (std::isinf(__x.real())) {
1197 return complex<_Tp>(std::copysign(_Tp(0), __x.real()), std::copysign(__pi / _Tp(2), __x.imag()));
1199 if (std::abs(__x.real()) == _Tp(1) && __x.imag() == _Tp(0)) {
1200 return complex<_Tp>(std::copysign(_Tp(INFINITY), __x.real()), std::copysign(_Tp(0), __x.imag()));
1202 complex<_Tp> __z = std::log((_Tp(1) + __x) / (_Tp(1) - __x)) / _Tp(2);
1203 return complex<_Tp>(std::copysign(__z.real(), __x.real()), std::copysign(__z.imag(), __x.imag()));
1208 template <class _Tp>
1209 _LIBCPP_HIDE_FROM_ABI complex<_Tp> sinh(const complex<_Tp>& __x) {
1210 if (std::isinf(__x.real()) && !std::isfinite(__x.imag()))
1211 return complex<_Tp>(__x.real(), _Tp(NAN));
1212 if (__x.real() == 0 && !std::isfinite(__x.imag()))
1213 return complex<_Tp>(__x.real(), _Tp(NAN));
1214 if (__x.imag() == 0 && !std::isfinite(__x.real()))
1216 return complex<_Tp>(std::sinh(__x.real()) * std::cos(__x.imag()), std::cosh(__x.real()) * std::sin(__x.imag()));
1221 template <class _Tp>
1222 _LIBCPP_HIDE_FROM_ABI complex<_Tp> cosh(const complex<_Tp>& __x) {
1223 if (std::isinf(__x.real()) && !std::isfinite(__x.imag()))
1224 return complex<_Tp>(std::abs(__x.real()), _Tp(NAN));
1225 if (__x.real() == 0 && !std::isfinite(__x.imag()))
1226 return complex<_Tp>(_Tp(NAN), __x.real());
1227 if (__x.real() == 0 && __x.imag() == 0)
1228 return complex<_Tp>(_Tp(1), __x.imag());
1229 if (__x.imag() == 0 && !std::isfinite(__x.real()))
1230 return complex<_Tp>(std::abs(__x.real()), __x.imag());
1231 return complex<_Tp>(std::cosh(__x.real()) * std::cos(__x.imag()), std::sinh(__x.real()) * std::sin(__x.imag()));
1236 template <class _Tp>
1237 _LIBCPP_HIDE_FROM_ABI complex<_Tp> tanh(const complex<_Tp>& __x) {
1238 if (std::isinf(__x.real())) {
1239 if (!std::isfinite(__x.imag()))
1240 return complex<_Tp>(std::copysign(_Tp(1), __x.real()), _Tp(0));
1241 return complex<_Tp>(std::copysign(_Tp(1), __x.real()), std::copysign(_Tp(0), std::sin(_Tp(2) * __x.imag())));
1243 if (std::isnan(__x.real()) && __x.imag() == 0)
1245 _Tp __2r(_Tp(2) * __x.real());
1246 _Tp __2i(_Tp(2) * __x.imag());
1247 _Tp __d(std::cosh(__2r) + std::cos(__2i));
1248 _Tp __2rsh(std::sinh(__2r));
1249 if (std::isinf(__2rsh) && std::isinf(__d))
1250 return complex<_Tp>(__2rsh > _Tp(0) ? _Tp(1) : _Tp(-1), __2i > _Tp(0) ? _Tp(0) : _Tp(-0.));
1251 return complex<_Tp>(__2rsh / __d, std::sin(__2i) / __d);
1256 template <class _Tp>
1257 _LIBCPP_HIDE_FROM_ABI complex<_Tp> asin(const complex<_Tp>& __x) {
1258 complex<_Tp> __z = std::asinh(complex<_Tp>(-__x.imag(), __x.real()));
1259 return complex<_Tp>(__z.imag(), -__z.real());
1264 template <class _Tp>
1265 _LIBCPP_HIDE_FROM_ABI complex<_Tp> acos(const complex<_Tp>& __x) {
1266 const _Tp __pi(atan2(+0., -0.));
1267 if (std::isinf(__x.real())) {
1268 if (std::isnan(__x.imag()))
1269 return complex<_Tp>(__x.imag(), __x.real());
1270 if (std::isinf(__x.imag())) {
1271 if (__x.real() < _Tp(0))
1272 return complex<_Tp>(_Tp(0.75) * __pi, -__x.imag());
1273 return complex<_Tp>(_Tp(0.25) * __pi, -__x.imag());
1275 if (__x.real() < _Tp(0))
1276 return complex<_Tp>(__pi, std::signbit(__x.imag()) ? -__x.real() : __x.real());
1277 return complex<_Tp>(_Tp(0), std::signbit(__x.imag()) ? __x.real() : -__x.real());
1279 if (std::isnan(__x.real())) {
1280 if (std::isinf(__x.imag()))
1281 return complex<_Tp>(__x.real(), -__x.imag());
1282 return complex<_Tp>(__x.real(), __x.real());
1284 if (std::isinf(__x.imag()))
1285 return complex<_Tp>(__pi / _Tp(2), -__x.imag());
1286 if (__x.real() == 0 && (__x.imag() == 0 || std::isnan(__x.imag())))
1287 return complex<_Tp>(__pi / _Tp(2), -__x.imag());
1288 complex<_Tp> __z = std::log(__x + std::sqrt(std::__sqr(__x) - _Tp(1)));
1289 if (std::signbit(__x.imag()))
1290 return complex<_Tp>(std::abs(__z.imag()), std::abs(__z.real()));
1291 return complex<_Tp>(std::abs(__z.imag()), -std::abs(__z.real()));
1296 template <class _Tp>
1297 _LIBCPP_HIDE_FROM_ABI complex<_Tp> atan(const complex<_Tp>& __x) {
1298 complex<_Tp> __z = std::atanh(complex<_Tp>(-__x.imag(), __x.real()));
1299 return complex<_Tp>(__z.imag(), -__z.real());
1304 template <class _Tp>
1305 _LIBCPP_HIDE_FROM_ABI complex<_Tp> sin(const complex<_Tp>& __x) {
1306 complex<_Tp> __z = std::sinh(complex<_Tp>(-__x.imag(), __x.real()));
1307 return complex<_Tp>(__z.imag(), -__z.real());
1312 template <class _Tp>
1313 inline _LIBCPP_HIDE_FROM_ABI complex<_Tp> cos(const complex<_Tp>& __x) {
1314 return std::cosh(complex<_Tp>(-__x.imag(), __x.real()));
1319 template <class _Tp>
1320 _LIBCPP_HIDE_FROM_ABI complex<_Tp> tan(const complex<_Tp>& __x) {
1321 complex<_Tp> __z = std::tanh(complex<_Tp>(-__x.imag(), __x.real()));
1322 return complex<_Tp>(__z.imag(), -__z.real());
1325 #if _LIBCPP_HAS_LOCALIZATION
1326 template <class _Tp, class _CharT, class _Traits>
1327 _LIBCPP_HIDE_FROM_ABI basic_istream<_CharT, _Traits>&
1328 operator>>(basic_istream<_CharT, _Traits>& __is, complex<_Tp>& __x) {
1331 if (__is.peek() == _CharT('(')) {
1337 _CharT __c = __is.peek();
1338 if (__c == _CharT(',')) {
1345 if (__c == _CharT(')')) {
1347 __x = complex<_Tp>(__r, __i);
1349 __is.setstate(__is.failbit);
1351 __is.setstate(__is.failbit);
1352 } else if (__c == _CharT(')')) {
1354 __x = complex<_Tp>(__r, _Tp(0));
1356 __is.setstate(__is.failbit);
1358 __is.setstate(__is.failbit);
1363 __x = complex<_Tp>(__r, _Tp(0));
1365 __is.setstate(__is.failbit);
1368 __is.setstate(__is.failbit);
1372 template <class _Tp, class _CharT, class _Traits>
1373 _LIBCPP_HIDE_FROM_ABI basic_ostream<_CharT, _Traits>&
1374 operator<<(basic_ostream<_CharT, _Traits>& __os, const complex<_Tp>& __x) {
1375 basic_ostringstream<_CharT, _Traits> __s;
1376 __s.flags(__os.flags());
1377 __s.imbue(__os.getloc());
1378 __s.precision(__os.precision());
1379 __s << '(' << __x.real() << ',' << __x.imag() << ')';
1380 return __os << __s.str();
1382 #endif // _LIBCPP_HAS_LOCALIZATION
1384 #if _LIBCPP_STD_VER >= 26
1386 // [complex.tuple], tuple interface
1388 template <class _Tp>
1389 struct tuple_size<complex<_Tp>> : integral_constant<size_t, 2> {};
1391 template <size_t _Ip, class _Tp>
1392 struct tuple_element<_Ip, complex<_Tp>> {
1393 static_assert(_Ip < 2, "Index value is out of range.");
1397 template <size_t _Ip, class _Xp>
1398 _LIBCPP_HIDE_FROM_ABI constexpr _Xp& get(complex<_Xp>& __z) noexcept {
1399 static_assert(_Ip < 2, "Index value is out of range.");
1400 if constexpr (_Ip == 0) {
1407 template <size_t _Ip, class _Xp>
1408 _LIBCPP_HIDE_FROM_ABI constexpr _Xp&& get(complex<_Xp>&& __z) noexcept {
1409 static_assert(_Ip < 2, "Index value is out of range.");
1410 if constexpr (_Ip == 0) {
1411 return std::move(__z.__re_);
1413 return std::move(__z.__im_);
1417 template <size_t _Ip, class _Xp>
1418 _LIBCPP_HIDE_FROM_ABI constexpr const _Xp& get(const complex<_Xp>& __z) noexcept {
1419 static_assert(_Ip < 2, "Index value is out of range.");
1420 if constexpr (_Ip == 0) {
1427 template <size_t _Ip, class _Xp>
1428 _LIBCPP_HIDE_FROM_ABI constexpr const _Xp&& get(const complex<_Xp>&& __z) noexcept {
1429 static_assert(_Ip < 2, "Index value is out of range.");
1430 if constexpr (_Ip == 0) {
1431 return std::move(__z.__re_);
1433 return std::move(__z.__im_);
1437 #endif // _LIBCPP_STD_VER >= 26
1439 #if _LIBCPP_STD_VER >= 14
1440 // Literal suffix for complex number literals [complex.literals]
1441 inline namespace literals {
1442 inline namespace complex_literals {
1443 _LIBCPP_HIDE_FROM_ABI inline constexpr complex<long double> operator""il(long double __im) { return {0.0l, __im}; }
1445 _LIBCPP_HIDE_FROM_ABI inline constexpr complex<long double> operator""il(unsigned long long __im) {
1446 return {0.0l, static_cast<long double>(__im)};
1449 _LIBCPP_HIDE_FROM_ABI inline constexpr complex<double> operator""i(long double __im) {
1450 return {0.0, static_cast<double>(__im)};
1453 _LIBCPP_HIDE_FROM_ABI inline constexpr complex<double> operator""i(unsigned long long __im) {
1454 return {0.0, static_cast<double>(__im)};
1457 _LIBCPP_HIDE_FROM_ABI inline constexpr complex<float> operator""if(long double __im) {
1458 return {0.0f, static_cast<float>(__im)};
1461 _LIBCPP_HIDE_FROM_ABI inline constexpr complex<float> operator""if(unsigned long long __im) {
1462 return {0.0f, static_cast<float>(__im)};
1464 } // namespace complex_literals
1465 } // namespace literals
1468 _LIBCPP_END_NAMESPACE_STD
1472 #if !defined(_LIBCPP_REMOVE_TRANSITIVE_INCLUDES) && _LIBCPP_STD_VER <= 20
1474 # include <stdexcept>
1475 # include <type_traits>
1478 #endif // _LIBCPP_COMPLEX