xref: /llvm-project/libcxx/include/complex (revision f69585235ec85d54e0f3fc41b2d5700430907f99)
1// -*- C++ -*-
2//===----------------------------------------------------------------------===//
3//
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
7//
8//===----------------------------------------------------------------------===//
9
10#ifndef _LIBCPP_COMPLEX
11#define _LIBCPP_COMPLEX
12
13/*
14    complex synopsis
15
16namespace std
17{
18
19template<class T>
20class complex
21{
22public:
23    typedef T value_type;
24
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
28
29    T real() const; // constexpr in C++14
30    T imag() const; // constexpr in C++14
31
32    void real(T); // constexpr in C++20
33    void imag(T); // constexpr in C++20
34
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
40
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
47};
48
49template<>
50class complex<float>
51{
52public:
53    typedef float value_type;
54
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>&);
58
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
63
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
69
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
76};
77
78template<>
79class complex<double>
80{
81public:
82    typedef double value_type;
83
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>&);
87
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
92
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
99
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
105};
106
107template<>
108class complex<long double>
109{
110public:
111    typedef long double value_type;
112
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>&);
116
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
121
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
128
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
134};
135
136// 26.3.6 operators:
137template<class T> complex<T> operator+(const complex<T>&, const complex<T>&); // constexpr in C++20
138template<class T> complex<T> operator+(const complex<T>&, const T&);          // constexpr in C++20
139template<class T> complex<T> operator+(const T&, const complex<T>&);          // constexpr in C++20
140template<class T> complex<T> operator-(const complex<T>&, const complex<T>&); // constexpr in C++20
141template<class T> complex<T> operator-(const complex<T>&, const T&);          // constexpr in C++20
142template<class T> complex<T> operator-(const T&, const complex<T>&);          // constexpr in C++20
143template<class T> complex<T> operator*(const complex<T>&, const complex<T>&); // constexpr in C++20
144template<class T> complex<T> operator*(const complex<T>&, const T&);          // constexpr in C++20
145template<class T> complex<T> operator*(const T&, const complex<T>&);          // constexpr in C++20
146template<class T> complex<T> operator/(const complex<T>&, const complex<T>&); // constexpr in C++20
147template<class T> complex<T> operator/(const complex<T>&, const T&);          // constexpr in C++20
148template<class T> complex<T> operator/(const T&, const complex<T>&);          // constexpr in C++20
149template<class T> complex<T> operator+(const complex<T>&);                    // constexpr in C++20
150template<class T> complex<T> operator-(const complex<T>&);                    // constexpr in C++20
151template<class T> bool operator==(const complex<T>&, const complex<T>&);      // constexpr in C++14
152template<class T> bool operator==(const complex<T>&, const T&);               // constexpr in C++14
153template<class T> bool operator==(const T&, const complex<T>&);               // constexpr in C++14, removed in C++20
154template<class T> bool operator!=(const complex<T>&, const complex<T>&);      // constexpr in C++14, removed in C++20
155template<class T> bool operator!=(const complex<T>&, const T&);               // constexpr in C++14, removed in C++20
156template<class T> bool operator!=(const T&, const complex<T>&);               // constexpr in C++14, removed in C++20
157
158template<class T, class charT, class traits>
159  basic_istream<charT, traits>&
160  operator>>(basic_istream<charT, traits>&, complex<T>&);
161template<class T, class charT, class traits>
162  basic_ostream<charT, traits>&
163  operator<<(basic_ostream<charT, traits>&, const complex<T>&);
164
165// 26.3.7 values:
166
167template<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
170template<Integral T>      double real(T);                 // constexpr in C++14
171                          float  real(float);             // constexpr in C++14
172
173template<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
176template<Integral T>      double imag(T);                 // constexpr in C++14
177                          float  imag(float);             // constexpr in C++14
178
179template<class T> T abs(const complex<T>&);
180
181template<class T>              T arg(const complex<T>&);
182                     long double arg(long double);
183                          double arg(double);
184template<Integral T>      double arg(T);
185                          float  arg(float);
186
187template<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
190template<Integral T>      double norm(T);                 // constexpr in C++20
191                          float  norm(float);             // constexpr in C++20
192
193template<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
196template<Integral T>   complex<double>      conj(T);                 // constexpr in C++20
197                       complex<float>       conj(float);             // constexpr in C++20
198
199template<class T>    complex<T>           proj(const complex<T>&);
200                     complex<long double> proj(long double);
201                     complex<double>      proj(double);
202template<Integral T> complex<double>      proj(T);
203                     complex<float>       proj(float);
204
205template<class T> complex<T> polar(const T&, const T& = T());
206
207// 26.3.8 transcendentals:
208template<class T> complex<T> acos(const complex<T>&);
209template<class T> complex<T> asin(const complex<T>&);
210template<class T> complex<T> atan(const complex<T>&);
211template<class T> complex<T> acosh(const complex<T>&);
212template<class T> complex<T> asinh(const complex<T>&);
213template<class T> complex<T> atanh(const complex<T>&);
214template<class T> complex<T> cos (const complex<T>&);
215template<class T> complex<T> cosh (const complex<T>&);
216template<class T> complex<T> exp (const complex<T>&);
217template<class T> complex<T> log (const complex<T>&);
218template<class T> complex<T> log10(const complex<T>&);
219
220template<class T> complex<T> pow(const complex<T>&, const T&);
221template<class T> complex<T> pow(const complex<T>&, const complex<T>&);
222template<class T> complex<T> pow(const T&, const complex<T>&);
223
224template<class T> complex<T> sin (const complex<T>&);
225template<class T> complex<T> sinh (const complex<T>&);
226template<class T> complex<T> sqrt (const complex<T>&);
227template<class T> complex<T> tan (const complex<T>&);
228template<class T> complex<T> tanh (const complex<T>&);
229
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
243
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
253  }
254  }
255}  // std
256
257*/
258
259#if __cplusplus < 201103L && defined(_LIBCPP_USE_FROZEN_CXX03_HEADERS)
260#  include <__cxx03/complex>
261#else
262#  include <__config>
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>
269#  include <cmath>
270#  include <version>
271
272#  if _LIBCPP_HAS_LOCALIZATION
273#    include <sstream> // for std::basic_ostringstream
274#  endif
275
276#  if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
277#    pragma GCC system_header
278#  endif
279
280_LIBCPP_PUSH_MACROS
281#  include <__undef_macros>
282
283_LIBCPP_BEGIN_NAMESPACE_STD
284
285template <class _Tp>
286class _LIBCPP_TEMPLATE_VIS complex;
287
288template <class _Tp, __enable_if_t<is_floating_point<_Tp>::value, int> = 0>
289_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 complex<_Tp>
290operator*(const complex<_Tp>& __z, const complex<_Tp>& __w);
291
292template <class _Tp, __enable_if_t<!is_floating_point<_Tp>::value, int> = 0>
293_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 complex<_Tp>
294operator*(const complex<_Tp>& __z, const complex<_Tp>& __w);
295
296template <class _Tp, __enable_if_t<is_floating_point<_Tp>::value, int> = 0>
297_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 complex<_Tp>
298operator/(const complex<_Tp>& __x, const complex<_Tp>& __y);
299
300template <class _Tp, __enable_if_t<!is_floating_point<_Tp>::value, int> = 0>
301_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 complex<_Tp>
302operator/(const complex<_Tp>& __x, const complex<_Tp>& __y);
303
304template <class _Tp>
305class _LIBCPP_TEMPLATE_VIS complex {
306public:
307  typedef _Tp value_type;
308
309private:
310  value_type __re_;
311  value_type __im_;
312
313public:
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) {}
317  template <class _Xp>
318  _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX14 complex(const complex<_Xp>& __c)
319      : __re_(__c.real()), __im_(__c.imag()) {}
320
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_; }
323
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; }
326
327  _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 complex& operator=(const value_type& __re) {
328    __re_ = __re;
329    __im_ = value_type();
330    return *this;
331  }
332  _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 complex& operator+=(const value_type& __re) {
333    __re_ += __re;
334    return *this;
335  }
336  _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 complex& operator-=(const value_type& __re) {
337    __re_ -= __re;
338    return *this;
339  }
340  _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 complex& operator*=(const value_type& __re) {
341    __re_ *= __re;
342    __im_ *= __re;
343    return *this;
344  }
345  _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 complex& operator/=(const value_type& __re) {
346    __re_ /= __re;
347    __im_ /= __re;
348    return *this;
349  }
350
351  template <class _Xp>
352  _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 complex& operator=(const complex<_Xp>& __c) {
353    __re_ = __c.real();
354    __im_ = __c.imag();
355    return *this;
356  }
357  template <class _Xp>
358  _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 complex& operator+=(const complex<_Xp>& __c) {
359    __re_ += __c.real();
360    __im_ += __c.imag();
361    return *this;
362  }
363  template <class _Xp>
364  _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 complex& operator-=(const complex<_Xp>& __c) {
365    __re_ -= __c.real();
366    __im_ -= __c.imag();
367    return *this;
368  }
369  template <class _Xp>
370  _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 complex& operator*=(const complex<_Xp>& __c) {
371    *this = *this * complex(__c.real(), __c.imag());
372    return *this;
373  }
374  template <class _Xp>
375  _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 complex& operator/=(const complex<_Xp>& __c) {
376    *this = *this / complex(__c.real(), __c.imag());
377    return *this;
378  }
379
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;
383
384  template <size_t _Ip, class _Xp>
385  friend _LIBCPP_HIDE_FROM_ABI constexpr _Xp&& get(complex<_Xp>&&) noexcept;
386
387  template <size_t _Ip, class _Xp>
388  friend _LIBCPP_HIDE_FROM_ABI constexpr const _Xp& get(const complex<_Xp>&) noexcept;
389
390  template <size_t _Ip, class _Xp>
391  friend _LIBCPP_HIDE_FROM_ABI constexpr const _Xp&& get(const complex<_Xp>&&) noexcept;
392#  endif
393};
394
395template <>
396class _LIBCPP_TEMPLATE_VIS complex<double>;
397template <>
398class _LIBCPP_TEMPLATE_VIS complex<long double>;
399
400struct __from_builtin_tag {};
401
402template <class _Tp>
403using __complex_t _LIBCPP_NODEBUG =
404    __conditional_t<is_same<_Tp, float>::value,
405                    _Complex float,
406                    __conditional_t<is_same<_Tp, double>::value, _Complex double, _Complex long double> >;
407
408template <class _Tp>
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);
412#  else
413  return __complex_t<_Tp>{__re, __im};
414#  endif
415}
416
417template <>
418class _LIBCPP_TEMPLATE_VIS complex<float> {
419  float __re_;
420  float __im_;
421
422public:
423  typedef float value_type;
424
425  _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR complex(float __re = 0.0f, float __im = 0.0f) : __re_(__re), __im_(__im) {}
426
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) {}
430
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);
433
434  _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR float real() const { return __re_; }
435  _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR float imag() const { return __im_; }
436
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; }
439
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;
444  }
445
446  _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 complex& operator=(float __re) {
447    __re_ = __re;
448    __im_ = value_type();
449    return *this;
450  }
451  _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 complex& operator+=(float __re) {
452    __re_ += __re;
453    return *this;
454  }
455  _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 complex& operator-=(float __re) {
456    __re_ -= __re;
457    return *this;
458  }
459  _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 complex& operator*=(float __re) {
460    __re_ *= __re;
461    __im_ *= __re;
462    return *this;
463  }
464  _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 complex& operator/=(float __re) {
465    __re_ /= __re;
466    __im_ /= __re;
467    return *this;
468  }
469
470  template <class _Xp>
471  _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 complex& operator=(const complex<_Xp>& __c) {
472    __re_ = __c.real();
473    __im_ = __c.imag();
474    return *this;
475  }
476  template <class _Xp>
477  _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 complex& operator+=(const complex<_Xp>& __c) {
478    __re_ += __c.real();
479    __im_ += __c.imag();
480    return *this;
481  }
482  template <class _Xp>
483  _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 complex& operator-=(const complex<_Xp>& __c) {
484    __re_ -= __c.real();
485    __im_ -= __c.imag();
486    return *this;
487  }
488  template <class _Xp>
489  _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 complex& operator*=(const complex<_Xp>& __c) {
490    *this = *this * complex(__c.real(), __c.imag());
491    return *this;
492  }
493  template <class _Xp>
494  _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 complex& operator/=(const complex<_Xp>& __c) {
495    *this = *this / complex(__c.real(), __c.imag());
496    return *this;
497  }
498
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;
502
503  template <size_t _Ip, class _Xp>
504  friend _LIBCPP_HIDE_FROM_ABI constexpr _Xp&& get(complex<_Xp>&&) noexcept;
505
506  template <size_t _Ip, class _Xp>
507  friend _LIBCPP_HIDE_FROM_ABI constexpr const _Xp& get(const complex<_Xp>&) noexcept;
508
509  template <size_t _Ip, class _Xp>
510  friend _LIBCPP_HIDE_FROM_ABI constexpr const _Xp&& get(const complex<_Xp>&&) noexcept;
511#  endif
512};
513
514template <>
515class _LIBCPP_TEMPLATE_VIS complex<double> {
516  double __re_;
517  double __im_;
518
519public:
520  typedef double value_type;
521
522  _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR complex(double __re = 0.0, double __im = 0.0) : __re_(__re), __im_(__im) {}
523
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) {}
527
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);
530
531  _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR double real() const { return __re_; }
532  _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR double imag() const { return __im_; }
533
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; }
536
537  _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR _Complex double __builtin() const {
538    return std::__make_complex(__re_, __im_);
539  }
540
541  _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX14 void __builtin(_Complex double __f) {
542    __re_ = __real__ __f;
543    __im_ = __imag__ __f;
544  }
545
546  _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 complex& operator=(double __re) {
547    __re_ = __re;
548    __im_ = value_type();
549    return *this;
550  }
551  _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 complex& operator+=(double __re) {
552    __re_ += __re;
553    return *this;
554  }
555  _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 complex& operator-=(double __re) {
556    __re_ -= __re;
557    return *this;
558  }
559  _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 complex& operator*=(double __re) {
560    __re_ *= __re;
561    __im_ *= __re;
562    return *this;
563  }
564  _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 complex& operator/=(double __re) {
565    __re_ /= __re;
566    __im_ /= __re;
567    return *this;
568  }
569
570  template <class _Xp>
571  _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 complex& operator=(const complex<_Xp>& __c) {
572    __re_ = __c.real();
573    __im_ = __c.imag();
574    return *this;
575  }
576  template <class _Xp>
577  _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 complex& operator+=(const complex<_Xp>& __c) {
578    __re_ += __c.real();
579    __im_ += __c.imag();
580    return *this;
581  }
582  template <class _Xp>
583  _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 complex& operator-=(const complex<_Xp>& __c) {
584    __re_ -= __c.real();
585    __im_ -= __c.imag();
586    return *this;
587  }
588  template <class _Xp>
589  _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 complex& operator*=(const complex<_Xp>& __c) {
590    *this = *this * complex(__c.real(), __c.imag());
591    return *this;
592  }
593  template <class _Xp>
594  _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 complex& operator/=(const complex<_Xp>& __c) {
595    *this = *this / complex(__c.real(), __c.imag());
596    return *this;
597  }
598
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;
602
603  template <size_t _Ip, class _Xp>
604  friend _LIBCPP_HIDE_FROM_ABI constexpr _Xp&& get(complex<_Xp>&&) noexcept;
605
606  template <size_t _Ip, class _Xp>
607  friend _LIBCPP_HIDE_FROM_ABI constexpr const _Xp& get(const complex<_Xp>&) noexcept;
608
609  template <size_t _Ip, class _Xp>
610  friend _LIBCPP_HIDE_FROM_ABI constexpr const _Xp&& get(const complex<_Xp>&&) noexcept;
611#  endif
612};
613
614template <>
615class _LIBCPP_TEMPLATE_VIS complex<long double> {
616  long double __re_;
617  long double __im_;
618
619public:
620  typedef long double value_type;
621
622  _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR complex(long double __re = 0.0L, long double __im = 0.0L)
623      : __re_(__re), __im_(__im) {}
624
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) {}
628
629  _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR complex(const complex<float>& __c);
630  _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR complex(const complex<double>& __c);
631
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_; }
634
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; }
637
638  _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR _Complex long double __builtin() const {
639    return std::__make_complex(__re_, __im_);
640  }
641
642  _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX14 void __builtin(_Complex long double __f) {
643    __re_ = __real__ __f;
644    __im_ = __imag__ __f;
645  }
646
647  _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 complex& operator=(long double __re) {
648    __re_ = __re;
649    __im_ = value_type();
650    return *this;
651  }
652  _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 complex& operator+=(long double __re) {
653    __re_ += __re;
654    return *this;
655  }
656  _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 complex& operator-=(long double __re) {
657    __re_ -= __re;
658    return *this;
659  }
660  _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 complex& operator*=(long double __re) {
661    __re_ *= __re;
662    __im_ *= __re;
663    return *this;
664  }
665  _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 complex& operator/=(long double __re) {
666    __re_ /= __re;
667    __im_ /= __re;
668    return *this;
669  }
670
671  template <class _Xp>
672  _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 complex& operator=(const complex<_Xp>& __c) {
673    __re_ = __c.real();
674    __im_ = __c.imag();
675    return *this;
676  }
677  template <class _Xp>
678  _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 complex& operator+=(const complex<_Xp>& __c) {
679    __re_ += __c.real();
680    __im_ += __c.imag();
681    return *this;
682  }
683  template <class _Xp>
684  _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 complex& operator-=(const complex<_Xp>& __c) {
685    __re_ -= __c.real();
686    __im_ -= __c.imag();
687    return *this;
688  }
689  template <class _Xp>
690  _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 complex& operator*=(const complex<_Xp>& __c) {
691    *this = *this * complex(__c.real(), __c.imag());
692    return *this;
693  }
694  template <class _Xp>
695  _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 complex& operator/=(const complex<_Xp>& __c) {
696    *this = *this / complex(__c.real(), __c.imag());
697    return *this;
698  }
699
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;
703
704  template <size_t _Ip, class _Xp>
705  friend _LIBCPP_HIDE_FROM_ABI constexpr _Xp&& get(complex<_Xp>&&) noexcept;
706
707  template <size_t _Ip, class _Xp>
708  friend _LIBCPP_HIDE_FROM_ABI constexpr const _Xp& get(const complex<_Xp>&) noexcept;
709
710  template <size_t _Ip, class _Xp>
711  friend _LIBCPP_HIDE_FROM_ABI constexpr const _Xp&& get(const complex<_Xp>&&) noexcept;
712#  endif
713};
714
715inline _LIBCPP_CONSTEXPR complex<float>::complex(const complex<double>& __c) : __re_(__c.real()), __im_(__c.imag()) {}
716
717inline _LIBCPP_CONSTEXPR complex<float>::complex(const complex<long double>& __c)
718    : __re_(__c.real()), __im_(__c.imag()) {}
719
720inline _LIBCPP_CONSTEXPR complex<double>::complex(const complex<float>& __c) : __re_(__c.real()), __im_(__c.imag()) {}
721
722inline _LIBCPP_CONSTEXPR complex<double>::complex(const complex<long double>& __c)
723    : __re_(__c.real()), __im_(__c.imag()) {}
724
725inline _LIBCPP_CONSTEXPR complex<long double>::complex(const complex<float>& __c)
726    : __re_(__c.real()), __im_(__c.imag()) {}
727
728inline _LIBCPP_CONSTEXPR complex<long double>::complex(const complex<double>& __c)
729    : __re_(__c.real()), __im_(__c.imag()) {}
730
731// 26.3.6 operators:
732
733template <class _Tp>
734inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 complex<_Tp>
735operator+(const complex<_Tp>& __x, const complex<_Tp>& __y) {
736  complex<_Tp> __t(__x);
737  __t += __y;
738  return __t;
739}
740
741template <class _Tp>
742inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 complex<_Tp>
743operator+(const complex<_Tp>& __x, const _Tp& __y) {
744  complex<_Tp> __t(__x);
745  __t += __y;
746  return __t;
747}
748
749template <class _Tp>
750inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 complex<_Tp>
751operator+(const _Tp& __x, const complex<_Tp>& __y) {
752  complex<_Tp> __t(__y);
753  __t += __x;
754  return __t;
755}
756
757template <class _Tp>
758inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 complex<_Tp>
759operator-(const complex<_Tp>& __x, const complex<_Tp>& __y) {
760  complex<_Tp> __t(__x);
761  __t -= __y;
762  return __t;
763}
764
765template <class _Tp>
766inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 complex<_Tp>
767operator-(const complex<_Tp>& __x, const _Tp& __y) {
768  complex<_Tp> __t(__x);
769  __t -= __y;
770  return __t;
771}
772
773template <class _Tp>
774inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 complex<_Tp>
775operator-(const _Tp& __x, const complex<_Tp>& __y) {
776  complex<_Tp> __t(-__y);
777  __t += __x;
778  return __t;
779}
780
781template <class _Tp, __enable_if_t<is_floating_point<_Tp>::value, int> >
782_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 complex<_Tp>
783operator*(const complex<_Tp>& __lhs, const complex<_Tp>& __rhs) {
784  return complex<_Tp>(__from_builtin_tag(), __lhs.__builtin() * __rhs.__builtin());
785}
786
787template <class _Tp, __enable_if_t<!is_floating_point<_Tp>::value, int> >
788_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 complex<_Tp>
789operator*(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();
794
795  return complex<_Tp>((__a * __c) - (__b * __d), (__a * __d) + (__b * __c));
796}
797
798template <class _Tp>
799inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 complex<_Tp>
800operator*(const complex<_Tp>& __x, const _Tp& __y) {
801  complex<_Tp> __t(__x);
802  __t *= __y;
803  return __t;
804}
805
806template <class _Tp>
807inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 complex<_Tp>
808operator*(const _Tp& __x, const complex<_Tp>& __y) {
809  complex<_Tp> __t(__y);
810  __t *= __x;
811  return __t;
812}
813
814template <class _Tp, __enable_if_t<is_floating_point<_Tp>::value, int> >
815_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 complex<_Tp>
816operator/(const complex<_Tp>& __lhs, const complex<_Tp>& __rhs) {
817  return complex<_Tp>(__from_builtin_tag(), __lhs.__builtin() / __rhs.__builtin());
818}
819
820template <class _Tp, __enable_if_t<!is_floating_point<_Tp>::value, int> >
821_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 complex<_Tp>
822operator/(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();
827
828  _Tp __denom = __c * __c + __d * __d;
829  return complex<_Tp>((__a * __c + __b * __d) / __denom, (__b * __c - __a * __d) / __denom);
830}
831
832template <class _Tp>
833inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 complex<_Tp>
834operator/(const complex<_Tp>& __x, const _Tp& __y) {
835  return complex<_Tp>(__x.real() / __y, __x.imag() / __y);
836}
837
838template <class _Tp>
839inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 complex<_Tp>
840operator/(const _Tp& __x, const complex<_Tp>& __y) {
841  complex<_Tp> __t(__x);
842  __t /= __y;
843  return __t;
844}
845
846template <class _Tp>
847inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 complex<_Tp> operator+(const complex<_Tp>& __x) {
848  return __x;
849}
850
851template <class _Tp>
852inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 complex<_Tp> operator-(const complex<_Tp>& __x) {
853  return complex<_Tp>(-__x.real(), -__x.imag());
854}
855
856template <class _Tp>
857inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX14 bool
858operator==(const complex<_Tp>& __x, const complex<_Tp>& __y) {
859  return __x.real() == __y.real() && __x.imag() == __y.imag();
860}
861
862template <class _Tp>
863inline _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;
865}
866
867#  if _LIBCPP_STD_VER <= 17
868
869template <class _Tp>
870inline _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();
872}
873
874template <class _Tp>
875inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX14 bool
876operator!=(const complex<_Tp>& __x, const complex<_Tp>& __y) {
877  return !(__x == __y);
878}
879
880template <class _Tp>
881inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX14 bool operator!=(const complex<_Tp>& __x, const _Tp& __y) {
882  return !(__x == __y);
883}
884
885template <class _Tp>
886inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX14 bool operator!=(const _Tp& __x, const complex<_Tp>& __y) {
887  return !(__x == __y);
888}
889
890#  endif
891
892// 26.3.7 values:
893
894template <class _Tp, bool = is_integral<_Tp>::value, bool = is_floating_point<_Tp>::value >
895struct __libcpp_complex_overload_traits {};
896
897// Integral Types
898template <class _Tp>
899struct __libcpp_complex_overload_traits<_Tp, true, false> {
900  typedef double _ValueType;
901  typedef complex<double> _ComplexType;
902};
903
904// Floating point types
905template <class _Tp>
906struct __libcpp_complex_overload_traits<_Tp, false, true> {
907  typedef _Tp _ValueType;
908  typedef complex<_Tp> _ComplexType;
909};
910
911// real
912
913template <class _Tp>
914inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX14 _Tp real(const complex<_Tp>& __c) {
915  return __c.real();
916}
917
918template <class _Tp>
919inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX14 typename __libcpp_complex_overload_traits<_Tp>::_ValueType
920real(_Tp __re) {
921  return __re;
922}
923
924// imag
925
926template <class _Tp>
927inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX14 _Tp imag(const complex<_Tp>& __c) {
928  return __c.imag();
929}
930
931template <class _Tp>
932inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX14 typename __libcpp_complex_overload_traits<_Tp>::_ValueType
933imag(_Tp) {
934  return 0;
935}
936
937// abs
938
939template <class _Tp>
940inline _LIBCPP_HIDE_FROM_ABI _Tp abs(const complex<_Tp>& __c) {
941  return std::hypot(__c.real(), __c.imag());
942}
943
944// arg
945
946template <class _Tp>
947inline _LIBCPP_HIDE_FROM_ABI _Tp arg(const complex<_Tp>& __c) {
948  return std::atan2(__c.imag(), __c.real());
949}
950
951template <class _Tp, __enable_if_t<is_same<_Tp, long double>::value, int> = 0>
952inline _LIBCPP_HIDE_FROM_ABI long double arg(_Tp __re) {
953  return std::atan2l(0.L, __re);
954}
955
956template <class _Tp, __enable_if_t<is_integral<_Tp>::value || is_same<_Tp, double>::value, int> = 0>
957inline _LIBCPP_HIDE_FROM_ABI double arg(_Tp __re) {
958  return std::atan2(0., __re);
959}
960
961template <class _Tp, __enable_if_t<is_same<_Tp, float>::value, int> = 0>
962inline _LIBCPP_HIDE_FROM_ABI float arg(_Tp __re) {
963  return std::atan2f(0.F, __re);
964}
965
966// norm
967
968template <class _Tp>
969inline _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();
975}
976
977template <class _Tp>
978inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 typename __libcpp_complex_overload_traits<_Tp>::_ValueType
979norm(_Tp __re) {
980  typedef typename __libcpp_complex_overload_traits<_Tp>::_ValueType _ValueType;
981  return static_cast<_ValueType>(__re) * __re;
982}
983
984// conj
985
986template <class _Tp>
987inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 complex<_Tp> conj(const complex<_Tp>& __c) {
988  return complex<_Tp>(__c.real(), -__c.imag());
989}
990
991template <class _Tp>
992inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 typename __libcpp_complex_overload_traits<_Tp>::_ComplexType
993conj(_Tp __re) {
994  typedef typename __libcpp_complex_overload_traits<_Tp>::_ComplexType _ComplexType;
995  return _ComplexType(__re);
996}
997
998// proj
999
1000template <class _Tp>
1001inline _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()));
1005  return __r;
1006}
1007
1008template <class _Tp, __enable_if_t<is_floating_point<_Tp>::value, int> = 0>
1009inline _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);
1013}
1014
1015template <class _Tp, __enable_if_t<is_integral<_Tp>::value, int> = 0>
1016inline _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);
1019}
1020
1021// polar
1022
1023template <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);
1031  }
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));
1036  }
1037  _Tp __x = __rho * std::cos(__theta);
1038  if (std::isnan(__x))
1039    __x = 0;
1040  _Tp __y = __rho * std::sin(__theta);
1041  if (std::isnan(__y))
1042    __y = 0;
1043  return complex<_Tp>(__x, __y);
1044}
1045
1046// log
1047
1048template <class _Tp>
1049inline _LIBCPP_HIDE_FROM_ABI complex<_Tp> log(const complex<_Tp>& __x) {
1050  return complex<_Tp>(std::log(std::abs(__x)), std::arg(__x));
1051}
1052
1053// log10
1054
1055template <class _Tp>
1056inline _LIBCPP_HIDE_FROM_ABI complex<_Tp> log10(const complex<_Tp>& __x) {
1057  return std::log(__x) / std::log(_Tp(10));
1058}
1059
1060// sqrt
1061
1062template <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()));
1070  }
1071  return std::polar(std::sqrt(std::abs(__x)), std::arg(__x) / _Tp(2));
1072}
1073
1074// exp
1075
1076template <class _Tp>
1077_LIBCPP_HIDE_FROM_ABI complex<_Tp> exp(const complex<_Tp>& __x) {
1078  _Tp __i = __x.imag();
1079  if (__i == 0) {
1080    return complex<_Tp>(std::exp(__x.real()), std::copysign(_Tp(0), __x.imag()));
1081  }
1082  if (std::isinf(__x.real())) {
1083    if (__x.real() < _Tp(0)) {
1084      if (!std::isfinite(__i))
1085        __i = _Tp(1);
1086    } else if (__i == 0 || !std::isfinite(__i)) {
1087      if (std::isinf(__i))
1088        __i = _Tp(NAN);
1089      return complex<_Tp>(__x.real(), __i);
1090    }
1091  }
1092  _Tp __e = std::exp(__x.real());
1093  return complex<_Tp>(__e * std::cos(__i), __e * std::sin(__i));
1094}
1095
1096// pow
1097
1098template <class _Tp>
1099inline _LIBCPP_HIDE_FROM_ABI complex<_Tp> pow(const complex<_Tp>& __x, const complex<_Tp>& __y) {
1100  return std::exp(__y * std::log(__x));
1101}
1102
1103template <class _Tp, class _Up, __enable_if_t<is_floating_point<_Tp>::value && is_floating_point<_Up>::value, int> = 0>
1104inline _LIBCPP_HIDE_FROM_ABI complex<typename __promote<_Tp, _Up>::type>
1105pow(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));
1108}
1109
1110template <class _Tp, class _Up, __enable_if_t<is_floating_point<_Tp>::value && is_arithmetic<_Up>::value, int> = 0>
1111inline _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));
1114}
1115
1116template <class _Tp, class _Up, __enable_if_t<is_arithmetic<_Tp>::value && is_floating_point<_Up>::value, int> = 0>
1117inline _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));
1120}
1121
1122// __sqr, computes pow(x, 2)
1123
1124template <class _Tp>
1125inline _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());
1127}
1128
1129// asinh
1130
1131template <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()))
1136      return __x;
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()));
1140  }
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)
1145      return __x;
1146    return complex<_Tp>(__x.real(), __x.real());
1147  }
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()));
1152}
1153
1154// acosh
1155
1156template <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())) {
1163      if (__x.real() > 0)
1164        return complex<_Tp>(__x.real(), std::copysign(__pi * _Tp(0.25), __x.imag()));
1165      else
1166        return complex<_Tp>(-__x.real(), std::copysign(__pi * _Tp(0.75), __x.imag()));
1167    }
1168    if (__x.real() < 0)
1169      return complex<_Tp>(-__x.real(), std::copysign(__pi, __x.imag()));
1170    return complex<_Tp>(__x.real(), std::copysign(_Tp(0), __x.imag()));
1171  }
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());
1176  }
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()));
1181}
1182
1183// atanh
1184
1185template <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()));
1190  }
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());
1195  }
1196  if (std::isnan(__x.real())) {
1197    return complex<_Tp>(__x.real(), __x.real());
1198  }
1199  if (std::isinf(__x.real())) {
1200    return complex<_Tp>(std::copysign(_Tp(0), __x.real()), std::copysign(__pi / _Tp(2), __x.imag()));
1201  }
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()));
1204  }
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()));
1207}
1208
1209// sinh
1210
1211template <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()))
1218    return __x;
1219  return complex<_Tp>(std::sinh(__x.real()) * std::cos(__x.imag()), std::cosh(__x.real()) * std::sin(__x.imag()));
1220}
1221
1222// cosh
1223
1224template <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()));
1235}
1236
1237// tanh
1238
1239template <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())));
1245  }
1246  if (std::isnan(__x.real()) && __x.imag() == 0)
1247    return __x;
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);
1255}
1256
1257// asin
1258
1259template <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());
1263}
1264
1265// acos
1266
1267template <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());
1277    }
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());
1281  }
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());
1286  }
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()));
1295}
1296
1297// atan
1298
1299template <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());
1303}
1304
1305// sin
1306
1307template <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());
1311}
1312
1313// cos
1314
1315template <class _Tp>
1316inline _LIBCPP_HIDE_FROM_ABI complex<_Tp> cos(const complex<_Tp>& __x) {
1317  return std::cosh(complex<_Tp>(-__x.imag(), __x.real()));
1318}
1319
1320// tan
1321
1322template <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());
1326}
1327
1328#  if _LIBCPP_HAS_LOCALIZATION
1329template <class _Tp, class _CharT, class _Traits>
1330_LIBCPP_HIDE_FROM_ABI basic_istream<_CharT, _Traits>&
1331operator>>(basic_istream<_CharT, _Traits>& __is, complex<_Tp>& __x) {
1332  if (__is.good()) {
1333    std::ws(__is);
1334    if (__is.peek() == _CharT('(')) {
1335      __is.get();
1336      _Tp __r;
1337      __is >> __r;
1338      if (!__is.fail()) {
1339        std::ws(__is);
1340        _CharT __c = __is.peek();
1341        if (__c == _CharT(',')) {
1342          __is.get();
1343          _Tp __i;
1344          __is >> __i;
1345          if (!__is.fail()) {
1346            std::ws(__is);
1347            __c = __is.peek();
1348            if (__c == _CharT(')')) {
1349              __is.get();
1350              __x = complex<_Tp>(__r, __i);
1351            } else
1352              __is.setstate(__is.failbit);
1353          } else
1354            __is.setstate(__is.failbit);
1355        } else if (__c == _CharT(')')) {
1356          __is.get();
1357          __x = complex<_Tp>(__r, _Tp(0));
1358        } else
1359          __is.setstate(__is.failbit);
1360      } else
1361        __is.setstate(__is.failbit);
1362    } else {
1363      _Tp __r;
1364      __is >> __r;
1365      if (!__is.fail())
1366        __x = complex<_Tp>(__r, _Tp(0));
1367      else
1368        __is.setstate(__is.failbit);
1369    }
1370  } else
1371    __is.setstate(__is.failbit);
1372  return __is;
1373}
1374
1375template <class _Tp, class _CharT, class _Traits>
1376_LIBCPP_HIDE_FROM_ABI basic_ostream<_CharT, _Traits>&
1377operator<<(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();
1384}
1385#  endif // _LIBCPP_HAS_LOCALIZATION
1386
1387#  if _LIBCPP_STD_VER >= 26
1388
1389// [complex.tuple], tuple interface
1390
1391template <class _Tp>
1392struct tuple_size<complex<_Tp>> : integral_constant<size_t, 2> {};
1393
1394template <size_t _Ip, class _Tp>
1395struct tuple_element<_Ip, complex<_Tp>> {
1396  static_assert(_Ip < 2, "Index value is out of range.");
1397  using type = _Tp;
1398};
1399
1400template <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) {
1404    return __z.__re_;
1405  } else {
1406    return __z.__im_;
1407  }
1408}
1409
1410template <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_);
1415  } else {
1416    return std::move(__z.__im_);
1417  }
1418}
1419
1420template <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) {
1424    return __z.__re_;
1425  } else {
1426    return __z.__im_;
1427  }
1428}
1429
1430template <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_);
1435  } else {
1436    return std::move(__z.__im_);
1437  }
1438}
1439
1440#  endif // _LIBCPP_STD_VER >= 26
1441
1442#  if _LIBCPP_STD_VER >= 14
1443// Literal suffix for complex number literals [complex.literals]
1444inline namespace literals {
1445inline namespace complex_literals {
1446_LIBCPP_HIDE_FROM_ABI inline constexpr complex<long double> operator""il(long double __im) { return {0.0l, __im}; }
1447
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)};
1450}
1451
1452_LIBCPP_HIDE_FROM_ABI inline constexpr complex<double> operator""i(long double __im) {
1453  return {0.0, static_cast<double>(__im)};
1454}
1455
1456_LIBCPP_HIDE_FROM_ABI inline constexpr complex<double> operator""i(unsigned long long __im) {
1457  return {0.0, static_cast<double>(__im)};
1458}
1459
1460_LIBCPP_HIDE_FROM_ABI inline constexpr complex<float> operator""if(long double __im) {
1461  return {0.0f, static_cast<float>(__im)};
1462}
1463
1464_LIBCPP_HIDE_FROM_ABI inline constexpr complex<float> operator""if(unsigned long long __im) {
1465  return {0.0f, static_cast<float>(__im)};
1466}
1467} // namespace complex_literals
1468} // namespace literals
1469#  endif
1470
1471_LIBCPP_END_NAMESPACE_STD
1472
1473_LIBCPP_POP_MACROS
1474
1475#  if !defined(_LIBCPP_REMOVE_TRANSITIVE_INCLUDES) && _LIBCPP_STD_VER <= 20
1476#    include <iosfwd>
1477#    include <stdexcept>
1478#    include <type_traits>
1479#  endif
1480#endif // __cplusplus < 201103L && defined(_LIBCPP_USE_FROZEN_CXX03_HEADERS)
1481
1482#endif // _LIBCPP_COMPLEX
1483