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
259 #if __cplusplus < 201103L && defined(_LIBCPP_USE_FROZEN_CXX03_HEADERS)
260 # include <__cxx03/complex>
263 # include <__fwd/complex.h>
264 # include <__fwd/tuple.h>
265 # include <__tuple/tuple_element.h>
266 # include <__tuple/tuple_size.h>
267 # include <__type_traits/conditional.h>
268 # include <__utility/move.h>
272 # if _LIBCPP_HAS_LOCALIZATION
273 # include <sstream> // for std::basic_ostringstream
276 # if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
277 # pragma GCC system_header
281 # include <__undef_macros>
283 _LIBCPP_BEGIN_NAMESPACE_STD
286 class _LIBCPP_TEMPLATE_VIS complex;
288 template <class _Tp, __enable_if_t<is_floating_point<_Tp>::value, int> = 0>
289 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 complex<_Tp>
290 operator*(const complex<_Tp>& __z, const complex<_Tp>& __w);
292 template <class _Tp, __enable_if_t<!is_floating_point<_Tp>::value, int> = 0>
293 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 complex<_Tp>
294 operator*(const complex<_Tp>& __z, const complex<_Tp>& __w);
296 template <class _Tp, __enable_if_t<is_floating_point<_Tp>::value, int> = 0>
297 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 complex<_Tp>
298 operator/(const complex<_Tp>& __x, const complex<_Tp>& __y);
300 template <class _Tp, __enable_if_t<!is_floating_point<_Tp>::value, int> = 0>
301 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 complex<_Tp>
302 operator/(const complex<_Tp>& __x, const complex<_Tp>& __y);
305 class _LIBCPP_TEMPLATE_VIS complex {
307 typedef _Tp value_type;
314 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX14
315 complex(const value_type& __re = value_type(), const value_type& __im = value_type())
316 : __re_(__re), __im_(__im) {}
318 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX14 complex(const complex<_Xp>& __c)
319 : __re_(__c.real()), __im_(__c.imag()) {}
321 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX14 value_type real() const { return __re_; }
322 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX14 value_type imag() const { return __im_; }
324 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 void real(value_type __re) { __re_ = __re; }
325 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 void imag(value_type __im) { __im_ = __im; }
327 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 complex& operator=(const value_type& __re) {
329 __im_ = value_type();
332 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 complex& operator+=(const value_type& __re) {
336 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 complex& operator-=(const value_type& __re) {
340 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 complex& operator*=(const value_type& __re) {
345 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 complex& operator/=(const value_type& __re) {
352 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 complex& operator=(const complex<_Xp>& __c) {
358 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 complex& operator+=(const complex<_Xp>& __c) {
364 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 complex& operator-=(const complex<_Xp>& __c) {
370 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 complex& operator*=(const complex<_Xp>& __c) {
371 *this = *this * complex(__c.real(), __c.imag());
375 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 complex& operator/=(const complex<_Xp>& __c) {
376 *this = *this / complex(__c.real(), __c.imag());
380 # if _LIBCPP_STD_VER >= 26
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 _Xp&& get(complex<_Xp>&&) noexcept;
387 template <size_t _Ip, class _Xp>
388 friend _LIBCPP_HIDE_FROM_ABI constexpr const _Xp& get(const complex<_Xp>&) noexcept;
390 template <size_t _Ip, class _Xp>
391 friend _LIBCPP_HIDE_FROM_ABI constexpr const _Xp&& get(const complex<_Xp>&&) noexcept;
396 class _LIBCPP_TEMPLATE_VIS complex<double>;
398 class _LIBCPP_TEMPLATE_VIS complex<long double>;
400 struct __from_builtin_tag {};
404 __conditional_t<is_same<_Tp, float>::value,
406 __conditional_t<is_same<_Tp, double>::value, _Complex double, _Complex long double> >;
409 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR __complex_t<_Tp> __make_complex(_Tp __re, _Tp __im) {
410 # if __has_builtin(__builtin_complex)
411 return __builtin_complex(__re, __im);
413 return __complex_t<_Tp>{__re, __im};
418 class _LIBCPP_TEMPLATE_VIS complex<float> {
423 typedef float value_type;
425 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR complex(float __re = 0.0f, float __im = 0.0f) : __re_(__re), __im_(__im) {}
427 template <class _Tag, __enable_if_t<_IsSame<_Tag, __from_builtin_tag>::value, int> = 0>
428 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR explicit complex(_Tag, _Complex float __v)
429 : __re_(__real__ __v), __im_(__imag__ __v) {}
431 _LIBCPP_HIDE_FROM_ABI explicit _LIBCPP_CONSTEXPR complex(const complex<double>& __c);
432 _LIBCPP_HIDE_FROM_ABI explicit _LIBCPP_CONSTEXPR complex(const complex<long double>& __c);
434 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR float real() const { return __re_; }
435 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR float imag() const { return __im_; }
437 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 void real(value_type __re) { __re_ = __re; }
438 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 void imag(value_type __im) { __im_ = __im; }
440 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR _Complex float __builtin() const { return std::__make_complex(__re_, __im_); }
441 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX14 void __builtin(_Complex float __f) {
442 __re_ = __real__ __f;
443 __im_ = __imag__ __f;
446 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 complex& operator=(float __re) {
448 __im_ = value_type();
451 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 complex& operator+=(float __re) {
455 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 complex& operator-=(float __re) {
459 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 complex& operator*=(float __re) {
464 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 complex& operator/=(float __re) {
471 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 complex& operator=(const complex<_Xp>& __c) {
477 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 complex& operator+=(const complex<_Xp>& __c) {
483 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 complex& operator-=(const complex<_Xp>& __c) {
489 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 complex& operator*=(const complex<_Xp>& __c) {
490 *this = *this * complex(__c.real(), __c.imag());
494 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 complex& operator/=(const complex<_Xp>& __c) {
495 *this = *this / complex(__c.real(), __c.imag());
499 # if _LIBCPP_STD_VER >= 26
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 _Xp&& get(complex<_Xp>&&) noexcept;
506 template <size_t _Ip, class _Xp>
507 friend _LIBCPP_HIDE_FROM_ABI constexpr const _Xp& get(const complex<_Xp>&) noexcept;
509 template <size_t _Ip, class _Xp>
510 friend _LIBCPP_HIDE_FROM_ABI constexpr const _Xp&& get(const complex<_Xp>&&) noexcept;
515 class _LIBCPP_TEMPLATE_VIS complex<double> {
520 typedef double value_type;
522 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR complex(double __re = 0.0, double __im = 0.0) : __re_(__re), __im_(__im) {}
524 template <class _Tag, __enable_if_t<_IsSame<_Tag, __from_builtin_tag>::value, int> = 0>
525 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR explicit complex(_Tag, _Complex double __v)
526 : __re_(__real__ __v), __im_(__imag__ __v) {}
528 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR complex(const complex<float>& __c);
529 _LIBCPP_HIDE_FROM_ABI explicit _LIBCPP_CONSTEXPR complex(const complex<long double>& __c);
531 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR double real() const { return __re_; }
532 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR double imag() const { return __im_; }
534 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 void real(value_type __re) { __re_ = __re; }
535 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 void imag(value_type __im) { __im_ = __im; }
537 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR _Complex double __builtin() const {
538 return std::__make_complex(__re_, __im_);
541 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX14 void __builtin(_Complex double __f) {
542 __re_ = __real__ __f;
543 __im_ = __imag__ __f;
546 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 complex& operator=(double __re) {
548 __im_ = value_type();
551 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 complex& operator+=(double __re) {
555 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 complex& operator-=(double __re) {
559 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 complex& operator*=(double __re) {
564 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 complex& operator/=(double __re) {
571 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 complex& operator=(const complex<_Xp>& __c) {
577 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 complex& operator+=(const complex<_Xp>& __c) {
583 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 complex& operator-=(const complex<_Xp>& __c) {
589 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 complex& operator*=(const complex<_Xp>& __c) {
590 *this = *this * complex(__c.real(), __c.imag());
594 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 complex& operator/=(const complex<_Xp>& __c) {
595 *this = *this / complex(__c.real(), __c.imag());
599 # if _LIBCPP_STD_VER >= 26
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 _Xp&& get(complex<_Xp>&&) noexcept;
606 template <size_t _Ip, class _Xp>
607 friend _LIBCPP_HIDE_FROM_ABI constexpr const _Xp& get(const complex<_Xp>&) noexcept;
609 template <size_t _Ip, class _Xp>
610 friend _LIBCPP_HIDE_FROM_ABI constexpr const _Xp&& get(const complex<_Xp>&&) noexcept;
615 class _LIBCPP_TEMPLATE_VIS complex<long double> {
620 typedef long double value_type;
622 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR complex(long double __re = 0.0L, long double __im = 0.0L)
623 : __re_(__re), __im_(__im) {}
625 template <class _Tag, __enable_if_t<_IsSame<_Tag, __from_builtin_tag>::value, int> = 0>
626 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR explicit complex(_Tag, _Complex long double __v)
627 : __re_(__real__ __v), __im_(__imag__ __v) {}
629 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR complex(const complex<float>& __c);
630 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR complex(const complex<double>& __c);
632 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR long double real() const { return __re_; }
633 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR long double imag() const { return __im_; }
635 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 void real(value_type __re) { __re_ = __re; }
636 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 void imag(value_type __im) { __im_ = __im; }
638 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR _Complex long double __builtin() const {
639 return std::__make_complex(__re_, __im_);
642 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX14 void __builtin(_Complex long double __f) {
643 __re_ = __real__ __f;
644 __im_ = __imag__ __f;
647 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 complex& operator=(long double __re) {
649 __im_ = value_type();
652 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 complex& operator+=(long double __re) {
656 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 complex& operator-=(long double __re) {
660 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 complex& operator*=(long double __re) {
665 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 complex& operator/=(long double __re) {
672 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 complex& operator=(const complex<_Xp>& __c) {
678 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 complex& operator+=(const complex<_Xp>& __c) {
684 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 complex& operator-=(const complex<_Xp>& __c) {
690 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 complex& operator*=(const complex<_Xp>& __c) {
691 *this = *this * complex(__c.real(), __c.imag());
695 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 complex& operator/=(const complex<_Xp>& __c) {
696 *this = *this / complex(__c.real(), __c.imag());
700 # if _LIBCPP_STD_VER >= 26
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 _Xp&& get(complex<_Xp>&&) noexcept;
707 template <size_t _Ip, class _Xp>
708 friend _LIBCPP_HIDE_FROM_ABI constexpr const _Xp& get(const complex<_Xp>&) noexcept;
710 template <size_t _Ip, class _Xp>
711 friend _LIBCPP_HIDE_FROM_ABI constexpr const _Xp&& get(const complex<_Xp>&&) noexcept;
715 inline _LIBCPP_CONSTEXPR complex<float>::complex(const complex<double>& __c) : __re_(__c.real()), __im_(__c.imag()) {}
717 inline _LIBCPP_CONSTEXPR complex<float>::complex(const complex<long double>& __c)
718 : __re_(__c.real()), __im_(__c.imag()) {}
720 inline _LIBCPP_CONSTEXPR complex<double>::complex(const complex<float>& __c) : __re_(__c.real()), __im_(__c.imag()) {}
722 inline _LIBCPP_CONSTEXPR complex<double>::complex(const complex<long double>& __c)
723 : __re_(__c.real()), __im_(__c.imag()) {}
725 inline _LIBCPP_CONSTEXPR complex<long double>::complex(const complex<float>& __c)
726 : __re_(__c.real()), __im_(__c.imag()) {}
728 inline _LIBCPP_CONSTEXPR complex<long double>::complex(const complex<double>& __c)
729 : __re_(__c.real()), __im_(__c.imag()) {}
734 inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 complex<_Tp>
735 operator+(const complex<_Tp>& __x, const complex<_Tp>& __y) {
736 complex<_Tp> __t(__x);
742 inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 complex<_Tp>
743 operator+(const complex<_Tp>& __x, const _Tp& __y) {
744 complex<_Tp> __t(__x);
750 inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 complex<_Tp>
751 operator+(const _Tp& __x, const complex<_Tp>& __y) {
752 complex<_Tp> __t(__y);
758 inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 complex<_Tp>
759 operator-(const complex<_Tp>& __x, const complex<_Tp>& __y) {
760 complex<_Tp> __t(__x);
766 inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 complex<_Tp>
767 operator-(const complex<_Tp>& __x, const _Tp& __y) {
768 complex<_Tp> __t(__x);
774 inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 complex<_Tp>
775 operator-(const _Tp& __x, const complex<_Tp>& __y) {
776 complex<_Tp> __t(-__y);
781 template <class _Tp, __enable_if_t<is_floating_point<_Tp>::value, int> >
782 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 complex<_Tp>
783 operator*(const complex<_Tp>& __lhs, const complex<_Tp>& __rhs) {
784 return complex<_Tp>(__from_builtin_tag(), __lhs.__builtin() * __rhs.__builtin());
787 template <class _Tp, __enable_if_t<!is_floating_point<_Tp>::value, int> >
788 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 complex<_Tp>
789 operator*(const complex<_Tp>& __z, const complex<_Tp>& __w) {
790 _Tp __a = __z.real();
791 _Tp __b = __z.imag();
792 _Tp __c = __w.real();
793 _Tp __d = __w.imag();
795 return complex<_Tp>((__a * __c) - (__b * __d), (__a * __d) + (__b * __c));
799 inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 complex<_Tp>
800 operator*(const complex<_Tp>& __x, const _Tp& __y) {
801 complex<_Tp> __t(__x);
807 inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 complex<_Tp>
808 operator*(const _Tp& __x, const complex<_Tp>& __y) {
809 complex<_Tp> __t(__y);
814 template <class _Tp, __enable_if_t<is_floating_point<_Tp>::value, int> >
815 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 complex<_Tp>
816 operator/(const complex<_Tp>& __lhs, const complex<_Tp>& __rhs) {
817 return complex<_Tp>(__from_builtin_tag(), __lhs.__builtin() / __rhs.__builtin());
820 template <class _Tp, __enable_if_t<!is_floating_point<_Tp>::value, int> >
821 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 complex<_Tp>
822 operator/(const complex<_Tp>& __z, const complex<_Tp>& __w) {
823 _Tp __a = __z.real();
824 _Tp __b = __z.imag();
825 _Tp __c = __w.real();
826 _Tp __d = __w.imag();
828 _Tp __denom = __c * __c + __d * __d;
829 return complex<_Tp>((__a * __c + __b * __d) / __denom, (__b * __c - __a * __d) / __denom);
833 inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 complex<_Tp>
834 operator/(const complex<_Tp>& __x, const _Tp& __y) {
835 return complex<_Tp>(__x.real() / __y, __x.imag() / __y);
839 inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 complex<_Tp>
840 operator/(const _Tp& __x, const complex<_Tp>& __y) {
841 complex<_Tp> __t(__x);
847 inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 complex<_Tp> operator+(const complex<_Tp>& __x) {
852 inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 complex<_Tp> operator-(const complex<_Tp>& __x) {
853 return complex<_Tp>(-__x.real(), -__x.imag());
857 inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX14 bool
858 operator==(const complex<_Tp>& __x, const complex<_Tp>& __y) {
859 return __x.real() == __y.real() && __x.imag() == __y.imag();
863 inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX14 bool operator==(const complex<_Tp>& __x, const _Tp& __y) {
864 return __x.real() == __y && __x.imag() == 0;
867 # if _LIBCPP_STD_VER <= 17
870 inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX14 bool operator==(const _Tp& __x, const complex<_Tp>& __y) {
871 return __x == __y.real() && 0 == __y.imag();
875 inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX14 bool
876 operator!=(const complex<_Tp>& __x, const complex<_Tp>& __y) {
877 return !(__x == __y);
881 inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX14 bool operator!=(const complex<_Tp>& __x, const _Tp& __y) {
882 return !(__x == __y);
886 inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX14 bool operator!=(const _Tp& __x, const complex<_Tp>& __y) {
887 return !(__x == __y);
894 template <class _Tp, bool = is_integral<_Tp>::value, bool = is_floating_point<_Tp>::value >
895 struct __libcpp_complex_overload_traits {};
899 struct __libcpp_complex_overload_traits<_Tp, true, false> {
900 typedef double _ValueType;
901 typedef complex<double> _ComplexType;
904 // Floating point types
906 struct __libcpp_complex_overload_traits<_Tp, false, true> {
907 typedef _Tp _ValueType;
908 typedef complex<_Tp> _ComplexType;
914 inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX14 _Tp real(const complex<_Tp>& __c) {
919 inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX14 typename __libcpp_complex_overload_traits<_Tp>::_ValueType
927 inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX14 _Tp imag(const complex<_Tp>& __c) {
932 inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX14 typename __libcpp_complex_overload_traits<_Tp>::_ValueType
940 inline _LIBCPP_HIDE_FROM_ABI _Tp abs(const complex<_Tp>& __c) {
941 return std::hypot(__c.real(), __c.imag());
947 inline _LIBCPP_HIDE_FROM_ABI _Tp arg(const complex<_Tp>& __c) {
948 return std::atan2(__c.imag(), __c.real());
951 template <class _Tp, __enable_if_t<is_same<_Tp, long double>::value, int> = 0>
952 inline _LIBCPP_HIDE_FROM_ABI long double arg(_Tp __re) {
953 return std::atan2l(0.L, __re);
956 template <class _Tp, __enable_if_t<is_integral<_Tp>::value || is_same<_Tp, double>::value, int> = 0>
957 inline _LIBCPP_HIDE_FROM_ABI double arg(_Tp __re) {
958 return std::atan2(0., __re);
961 template <class _Tp, __enable_if_t<is_same<_Tp, float>::value, int> = 0>
962 inline _LIBCPP_HIDE_FROM_ABI float arg(_Tp __re) {
963 return std::atan2f(0.F, __re);
969 inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 _Tp norm(const complex<_Tp>& __c) {
970 if (std::__constexpr_isinf(__c.real()))
971 return std::abs(__c.real());
972 if (std::__constexpr_isinf(__c.imag()))
973 return std::abs(__c.imag());
974 return __c.real() * __c.real() + __c.imag() * __c.imag();
978 inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 typename __libcpp_complex_overload_traits<_Tp>::_ValueType
980 typedef typename __libcpp_complex_overload_traits<_Tp>::_ValueType _ValueType;
981 return static_cast<_ValueType>(__re) * __re;
987 inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 complex<_Tp> conj(const complex<_Tp>& __c) {
988 return complex<_Tp>(__c.real(), -__c.imag());
992 inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 typename __libcpp_complex_overload_traits<_Tp>::_ComplexType
994 typedef typename __libcpp_complex_overload_traits<_Tp>::_ComplexType _ComplexType;
995 return _ComplexType(__re);
1000 template <class _Tp>
1001 inline _LIBCPP_HIDE_FROM_ABI complex<_Tp> proj(const complex<_Tp>& __c) {
1002 complex<_Tp> __r = __c;
1003 if (std::isinf(__c.real()) || std::isinf(__c.imag()))
1004 __r = complex<_Tp>(INFINITY, std::copysign(_Tp(0), __c.imag()));
1008 template <class _Tp, __enable_if_t<is_floating_point<_Tp>::value, int> = 0>
1009 inline _LIBCPP_HIDE_FROM_ABI typename __libcpp_complex_overload_traits<_Tp>::_ComplexType proj(_Tp __re) {
1010 if (std::isinf(__re))
1011 __re = std::abs(__re);
1012 return complex<_Tp>(__re);
1015 template <class _Tp, __enable_if_t<is_integral<_Tp>::value, int> = 0>
1016 inline _LIBCPP_HIDE_FROM_ABI typename __libcpp_complex_overload_traits<_Tp>::_ComplexType proj(_Tp __re) {
1017 typedef typename __libcpp_complex_overload_traits<_Tp>::_ComplexType _ComplexType;
1018 return _ComplexType(__re);
1023 template <class _Tp>
1024 _LIBCPP_HIDE_FROM_ABI complex<_Tp> polar(const _Tp& __rho, const _Tp& __theta = _Tp()) {
1025 if (std::isnan(__rho) || std::signbit(__rho))
1026 return complex<_Tp>(_Tp(NAN), _Tp(NAN));
1027 if (std::isnan(__theta)) {
1028 if (std::isinf(__rho))
1029 return complex<_Tp>(__rho, __theta);
1030 return complex<_Tp>(__theta, __theta);
1032 if (std::isinf(__theta)) {
1033 if (std::isinf(__rho))
1034 return complex<_Tp>(__rho, _Tp(NAN));
1035 return complex<_Tp>(_Tp(NAN), _Tp(NAN));
1037 _Tp __x = __rho * std::cos(__theta);
1038 if (std::isnan(__x))
1040 _Tp __y = __rho * std::sin(__theta);
1041 if (std::isnan(__y))
1043 return complex<_Tp>(__x, __y);
1048 template <class _Tp>
1049 inline _LIBCPP_HIDE_FROM_ABI complex<_Tp> log(const complex<_Tp>& __x) {
1050 return complex<_Tp>(std::log(std::abs(__x)), std::arg(__x));
1055 template <class _Tp>
1056 inline _LIBCPP_HIDE_FROM_ABI complex<_Tp> log10(const complex<_Tp>& __x) {
1057 return std::log(__x) / std::log(_Tp(10));
1062 template <class _Tp>
1063 _LIBCPP_HIDE_FROM_ABI complex<_Tp> sqrt(const complex<_Tp>& __x) {
1064 if (std::isinf(__x.imag()))
1065 return complex<_Tp>(_Tp(INFINITY), __x.imag());
1066 if (std::isinf(__x.real())) {
1067 if (__x.real() > _Tp(0))
1068 return complex<_Tp>(__x.real(), std::isnan(__x.imag()) ? __x.imag() : std::copysign(_Tp(0), __x.imag()));
1069 return complex<_Tp>(std::isnan(__x.imag()) ? __x.imag() : _Tp(0), std::copysign(__x.real(), __x.imag()));
1071 return std::polar(std::sqrt(std::abs(__x)), std::arg(__x) / _Tp(2));
1076 template <class _Tp>
1077 _LIBCPP_HIDE_FROM_ABI complex<_Tp> exp(const complex<_Tp>& __x) {
1078 _Tp __i = __x.imag();
1080 return complex<_Tp>(std::exp(__x.real()), std::copysign(_Tp(0), __x.imag()));
1082 if (std::isinf(__x.real())) {
1083 if (__x.real() < _Tp(0)) {
1084 if (!std::isfinite(__i))
1086 } else if (__i == 0 || !std::isfinite(__i)) {
1087 if (std::isinf(__i))
1089 return complex<_Tp>(__x.real(), __i);
1092 _Tp __e = std::exp(__x.real());
1093 return complex<_Tp>(__e * std::cos(__i), __e * std::sin(__i));
1098 template <class _Tp>
1099 inline _LIBCPP_HIDE_FROM_ABI complex<_Tp> pow(const complex<_Tp>& __x, const complex<_Tp>& __y) {
1100 return std::exp(__y * std::log(__x));
1103 template <class _Tp, class _Up, __enable_if_t<is_floating_point<_Tp>::value && is_floating_point<_Up>::value, int> = 0>
1104 inline _LIBCPP_HIDE_FROM_ABI complex<typename __promote<_Tp, _Up>::type>
1105 pow(const complex<_Tp>& __x, const complex<_Up>& __y) {
1106 typedef complex<typename __promote<_Tp, _Up>::type> result_type;
1107 return std::pow(result_type(__x), result_type(__y));
1110 template <class _Tp, class _Up, __enable_if_t<is_floating_point<_Tp>::value && is_arithmetic<_Up>::value, int> = 0>
1111 inline _LIBCPP_HIDE_FROM_ABI complex<typename __promote<_Tp, _Up>::type> pow(const complex<_Tp>& __x, const _Up& __y) {
1112 typedef complex<typename __promote<_Tp, _Up>::type> result_type;
1113 return std::pow(result_type(__x), result_type(__y));
1116 template <class _Tp, class _Up, __enable_if_t<is_arithmetic<_Tp>::value && is_floating_point<_Up>::value, int> = 0>
1117 inline _LIBCPP_HIDE_FROM_ABI complex<typename __promote<_Tp, _Up>::type> pow(const _Tp& __x, const complex<_Up>& __y) {
1118 typedef complex<typename __promote<_Tp, _Up>::type> result_type;
1119 return std::pow(result_type(__x), result_type(__y));
1122 // __sqr, computes pow(x, 2)
1124 template <class _Tp>
1125 inline _LIBCPP_HIDE_FROM_ABI complex<_Tp> __sqr(const complex<_Tp>& __x) {
1126 return complex<_Tp>((__x.real() - __x.imag()) * (__x.real() + __x.imag()), _Tp(2) * __x.real() * __x.imag());
1131 template <class _Tp>
1132 _LIBCPP_HIDE_FROM_ABI complex<_Tp> asinh(const complex<_Tp>& __x) {
1133 const _Tp __pi(atan2(+0., -0.));
1134 if (std::isinf(__x.real())) {
1135 if (std::isnan(__x.imag()))
1137 if (std::isinf(__x.imag()))
1138 return complex<_Tp>(__x.real(), std::copysign(__pi * _Tp(0.25), __x.imag()));
1139 return complex<_Tp>(__x.real(), std::copysign(_Tp(0), __x.imag()));
1141 if (std::isnan(__x.real())) {
1142 if (std::isinf(__x.imag()))
1143 return complex<_Tp>(__x.imag(), __x.real());
1144 if (__x.imag() == 0)
1146 return complex<_Tp>(__x.real(), __x.real());
1148 if (std::isinf(__x.imag()))
1149 return complex<_Tp>(std::copysign(__x.imag(), __x.real()), std::copysign(__pi / _Tp(2), __x.imag()));
1150 complex<_Tp> __z = std::log(__x + std::sqrt(std::__sqr(__x) + _Tp(1)));
1151 return complex<_Tp>(std::copysign(__z.real(), __x.real()), std::copysign(__z.imag(), __x.imag()));
1156 template <class _Tp>
1157 _LIBCPP_HIDE_FROM_ABI complex<_Tp> acosh(const complex<_Tp>& __x) {
1158 const _Tp __pi(atan2(+0., -0.));
1159 if (std::isinf(__x.real())) {
1160 if (std::isnan(__x.imag()))
1161 return complex<_Tp>(std::abs(__x.real()), __x.imag());
1162 if (std::isinf(__x.imag())) {
1164 return complex<_Tp>(__x.real(), std::copysign(__pi * _Tp(0.25), __x.imag()));
1166 return complex<_Tp>(-__x.real(), std::copysign(__pi * _Tp(0.75), __x.imag()));
1169 return complex<_Tp>(-__x.real(), std::copysign(__pi, __x.imag()));
1170 return complex<_Tp>(__x.real(), std::copysign(_Tp(0), __x.imag()));
1172 if (std::isnan(__x.real())) {
1173 if (std::isinf(__x.imag()))
1174 return complex<_Tp>(std::abs(__x.imag()), __x.real());
1175 return complex<_Tp>(__x.real(), __x.real());
1177 if (std::isinf(__x.imag()))
1178 return complex<_Tp>(std::abs(__x.imag()), std::copysign(__pi / _Tp(2), __x.imag()));
1179 complex<_Tp> __z = std::log(__x + std::sqrt(std::__sqr(__x) - _Tp(1)));
1180 return complex<_Tp>(std::copysign(__z.real(), _Tp(0)), std::copysign(__z.imag(), __x.imag()));
1185 template <class _Tp>
1186 _LIBCPP_HIDE_FROM_ABI complex<_Tp> atanh(const complex<_Tp>& __x) {
1187 const _Tp __pi(atan2(+0., -0.));
1188 if (std::isinf(__x.imag())) {
1189 return complex<_Tp>(std::copysign(_Tp(0), __x.real()), std::copysign(__pi / _Tp(2), __x.imag()));
1191 if (std::isnan(__x.imag())) {
1192 if (std::isinf(__x.real()) || __x.real() == 0)
1193 return complex<_Tp>(std::copysign(_Tp(0), __x.real()), __x.imag());
1194 return complex<_Tp>(__x.imag(), __x.imag());
1196 if (std::isnan(__x.real())) {
1197 return complex<_Tp>(__x.real(), __x.real());
1199 if (std::isinf(__x.real())) {
1200 return complex<_Tp>(std::copysign(_Tp(0), __x.real()), std::copysign(__pi / _Tp(2), __x.imag()));
1202 if (std::abs(__x.real()) == _Tp(1) && __x.imag() == _Tp(0)) {
1203 return complex<_Tp>(std::copysign(_Tp(INFINITY), __x.real()), std::copysign(_Tp(0), __x.imag()));
1205 complex<_Tp> __z = std::log((_Tp(1) + __x) / (_Tp(1) - __x)) / _Tp(2);
1206 return complex<_Tp>(std::copysign(__z.real(), __x.real()), std::copysign(__z.imag(), __x.imag()));
1211 template <class _Tp>
1212 _LIBCPP_HIDE_FROM_ABI complex<_Tp> sinh(const complex<_Tp>& __x) {
1213 if (std::isinf(__x.real()) && !std::isfinite(__x.imag()))
1214 return complex<_Tp>(__x.real(), _Tp(NAN));
1215 if (__x.real() == 0 && !std::isfinite(__x.imag()))
1216 return complex<_Tp>(__x.real(), _Tp(NAN));
1217 if (__x.imag() == 0 && !std::isfinite(__x.real()))
1219 return complex<_Tp>(std::sinh(__x.real()) * std::cos(__x.imag()), std::cosh(__x.real()) * std::sin(__x.imag()));
1224 template <class _Tp>
1225 _LIBCPP_HIDE_FROM_ABI complex<_Tp> cosh(const complex<_Tp>& __x) {
1226 if (std::isinf(__x.real()) && !std::isfinite(__x.imag()))
1227 return complex<_Tp>(std::abs(__x.real()), _Tp(NAN));
1228 if (__x.real() == 0 && !std::isfinite(__x.imag()))
1229 return complex<_Tp>(_Tp(NAN), __x.real());
1230 if (__x.real() == 0 && __x.imag() == 0)
1231 return complex<_Tp>(_Tp(1), __x.imag());
1232 if (__x.imag() == 0 && !std::isfinite(__x.real()))
1233 return complex<_Tp>(std::abs(__x.real()), __x.imag());
1234 return complex<_Tp>(std::cosh(__x.real()) * std::cos(__x.imag()), std::sinh(__x.real()) * std::sin(__x.imag()));
1239 template <class _Tp>
1240 _LIBCPP_HIDE_FROM_ABI complex<_Tp> tanh(const complex<_Tp>& __x) {
1241 if (std::isinf(__x.real())) {
1242 if (!std::isfinite(__x.imag()))
1243 return complex<_Tp>(std::copysign(_Tp(1), __x.real()), _Tp(0));
1244 return complex<_Tp>(std::copysign(_Tp(1), __x.real()), std::copysign(_Tp(0), std::sin(_Tp(2) * __x.imag())));
1246 if (std::isnan(__x.real()) && __x.imag() == 0)
1248 _Tp __2r(_Tp(2) * __x.real());
1249 _Tp __2i(_Tp(2) * __x.imag());
1250 _Tp __d(std::cosh(__2r) + std::cos(__2i));
1251 _Tp __2rsh(std::sinh(__2r));
1252 if (std::isinf(__2rsh) && std::isinf(__d))
1253 return complex<_Tp>(__2rsh > _Tp(0) ? _Tp(1) : _Tp(-1), __2i > _Tp(0) ? _Tp(0) : _Tp(-0.));
1254 return complex<_Tp>(__2rsh / __d, std::sin(__2i) / __d);
1259 template <class _Tp>
1260 _LIBCPP_HIDE_FROM_ABI complex<_Tp> asin(const complex<_Tp>& __x) {
1261 complex<_Tp> __z = std::asinh(complex<_Tp>(-__x.imag(), __x.real()));
1262 return complex<_Tp>(__z.imag(), -__z.real());
1267 template <class _Tp>
1268 _LIBCPP_HIDE_FROM_ABI complex<_Tp> acos(const complex<_Tp>& __x) {
1269 const _Tp __pi(atan2(+0., -0.));
1270 if (std::isinf(__x.real())) {
1271 if (std::isnan(__x.imag()))
1272 return complex<_Tp>(__x.imag(), __x.real());
1273 if (std::isinf(__x.imag())) {
1274 if (__x.real() < _Tp(0))
1275 return complex<_Tp>(_Tp(0.75) * __pi, -__x.imag());
1276 return complex<_Tp>(_Tp(0.25) * __pi, -__x.imag());
1278 if (__x.real() < _Tp(0))
1279 return complex<_Tp>(__pi, std::signbit(__x.imag()) ? -__x.real() : __x.real());
1280 return complex<_Tp>(_Tp(0), std::signbit(__x.imag()) ? __x.real() : -__x.real());
1282 if (std::isnan(__x.real())) {
1283 if (std::isinf(__x.imag()))
1284 return complex<_Tp>(__x.real(), -__x.imag());
1285 return complex<_Tp>(__x.real(), __x.real());
1287 if (std::isinf(__x.imag()))
1288 return complex<_Tp>(__pi / _Tp(2), -__x.imag());
1289 if (__x.real() == 0 && (__x.imag() == 0 || std::isnan(__x.imag())))
1290 return complex<_Tp>(__pi / _Tp(2), -__x.imag());
1291 complex<_Tp> __z = std::log(__x + std::sqrt(std::__sqr(__x) - _Tp(1)));
1292 if (std::signbit(__x.imag()))
1293 return complex<_Tp>(std::abs(__z.imag()), std::abs(__z.real()));
1294 return complex<_Tp>(std::abs(__z.imag()), -std::abs(__z.real()));
1299 template <class _Tp>
1300 _LIBCPP_HIDE_FROM_ABI complex<_Tp> atan(const complex<_Tp>& __x) {
1301 complex<_Tp> __z = std::atanh(complex<_Tp>(-__x.imag(), __x.real()));
1302 return complex<_Tp>(__z.imag(), -__z.real());
1307 template <class _Tp>
1308 _LIBCPP_HIDE_FROM_ABI complex<_Tp> sin(const complex<_Tp>& __x) {
1309 complex<_Tp> __z = std::sinh(complex<_Tp>(-__x.imag(), __x.real()));
1310 return complex<_Tp>(__z.imag(), -__z.real());
1315 template <class _Tp>
1316 inline _LIBCPP_HIDE_FROM_ABI complex<_Tp> cos(const complex<_Tp>& __x) {
1317 return std::cosh(complex<_Tp>(-__x.imag(), __x.real()));
1322 template <class _Tp>
1323 _LIBCPP_HIDE_FROM_ABI complex<_Tp> tan(const complex<_Tp>& __x) {
1324 complex<_Tp> __z = std::tanh(complex<_Tp>(-__x.imag(), __x.real()));
1325 return complex<_Tp>(__z.imag(), -__z.real());
1328 # if _LIBCPP_HAS_LOCALIZATION
1329 template <class _Tp, class _CharT, class _Traits>
1330 _LIBCPP_HIDE_FROM_ABI basic_istream<_CharT, _Traits>&
1331 operator>>(basic_istream<_CharT, _Traits>& __is, complex<_Tp>& __x) {
1334 if (__is.peek() == _CharT('(')) {
1340 _CharT __c = __is.peek();
1341 if (__c == _CharT(',')) {
1348 if (__c == _CharT(')')) {
1350 __x = complex<_Tp>(__r, __i);
1352 __is.setstate(__is.failbit);
1354 __is.setstate(__is.failbit);
1355 } else if (__c == _CharT(')')) {
1357 __x = complex<_Tp>(__r, _Tp(0));
1359 __is.setstate(__is.failbit);
1361 __is.setstate(__is.failbit);
1366 __x = complex<_Tp>(__r, _Tp(0));
1368 __is.setstate(__is.failbit);
1371 __is.setstate(__is.failbit);
1375 template <class _Tp, class _CharT, class _Traits>
1376 _LIBCPP_HIDE_FROM_ABI basic_ostream<_CharT, _Traits>&
1377 operator<<(basic_ostream<_CharT, _Traits>& __os, const complex<_Tp>& __x) {
1378 basic_ostringstream<_CharT, _Traits> __s;
1379 __s.flags(__os.flags());
1380 __s.imbue(__os.getloc());
1381 __s.precision(__os.precision());
1382 __s << '(' << __x.real() << ',' << __x.imag() << ')';
1383 return __os << __s.str();
1385 # endif // _LIBCPP_HAS_LOCALIZATION
1387 # if _LIBCPP_STD_VER >= 26
1389 // [complex.tuple], tuple interface
1391 template <class _Tp>
1392 struct tuple_size<complex<_Tp>> : integral_constant<size_t, 2> {};
1394 template <size_t _Ip, class _Tp>
1395 struct tuple_element<_Ip, complex<_Tp>> {
1396 static_assert(_Ip < 2, "Index value is out of range.");
1400 template <size_t _Ip, class _Xp>
1401 _LIBCPP_HIDE_FROM_ABI constexpr _Xp& get(complex<_Xp>& __z) noexcept {
1402 static_assert(_Ip < 2, "Index value is out of range.");
1403 if constexpr (_Ip == 0) {
1410 template <size_t _Ip, class _Xp>
1411 _LIBCPP_HIDE_FROM_ABI constexpr _Xp&& get(complex<_Xp>&& __z) noexcept {
1412 static_assert(_Ip < 2, "Index value is out of range.");
1413 if constexpr (_Ip == 0) {
1414 return std::move(__z.__re_);
1416 return std::move(__z.__im_);
1420 template <size_t _Ip, class _Xp>
1421 _LIBCPP_HIDE_FROM_ABI constexpr const _Xp& get(const complex<_Xp>& __z) noexcept {
1422 static_assert(_Ip < 2, "Index value is out of range.");
1423 if constexpr (_Ip == 0) {
1430 template <size_t _Ip, class _Xp>
1431 _LIBCPP_HIDE_FROM_ABI constexpr const _Xp&& get(const complex<_Xp>&& __z) noexcept {
1432 static_assert(_Ip < 2, "Index value is out of range.");
1433 if constexpr (_Ip == 0) {
1434 return std::move(__z.__re_);
1436 return std::move(__z.__im_);
1440 # endif // _LIBCPP_STD_VER >= 26
1442 # if _LIBCPP_STD_VER >= 14
1443 // Literal suffix for complex number literals [complex.literals]
1444 inline namespace literals {
1445 inline namespace complex_literals {
1446 _LIBCPP_HIDE_FROM_ABI inline constexpr complex<long double> operator""il(long double __im) { return {0.0l, __im}; }
1448 _LIBCPP_HIDE_FROM_ABI inline constexpr complex<long double> operator""il(unsigned long long __im) {
1449 return {0.0l, static_cast<long double>(__im)};
1452 _LIBCPP_HIDE_FROM_ABI inline constexpr complex<double> operator""i(long double __im) {
1453 return {0.0, static_cast<double>(__im)};
1456 _LIBCPP_HIDE_FROM_ABI inline constexpr complex<double> operator""i(unsigned long long __im) {
1457 return {0.0, static_cast<double>(__im)};
1460 _LIBCPP_HIDE_FROM_ABI inline constexpr complex<float> operator""if(long double __im) {
1461 return {0.0f, static_cast<float>(__im)};
1464 _LIBCPP_HIDE_FROM_ABI inline constexpr complex<float> operator""if(unsigned long long __im) {
1465 return {0.0f, static_cast<float>(__im)};
1467 } // namespace complex_literals
1468 } // namespace literals
1471 _LIBCPP_END_NAMESPACE_STD
1475 # if !defined(_LIBCPP_REMOVE_TRANSITIVE_INCLUDES) && _LIBCPP_STD_VER <= 20
1477 # include <stdexcept>
1478 # include <type_traits>
1480 #endif // __cplusplus < 201103L && defined(_LIBCPP_USE_FROZEN_CXX03_HEADERS)
1482 #endif // _LIBCPP_COMPLEX