• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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#include <__config>
260#include <__fwd/complex.h>
261#include <__fwd/tuple.h>
262#include <__tuple/tuple_element.h>
263#include <__tuple/tuple_size.h>
264#include <__type_traits/conditional.h>
265#include <__utility/move.h>
266#include <cmath>
267#include <version>
268
269#if _LIBCPP_HAS_LOCALIZATION
270#  include <sstream> // for std::basic_ostringstream
271#endif
272
273#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
274#  pragma GCC system_header
275#endif
276
277_LIBCPP_PUSH_MACROS
278#include <__undef_macros>
279
280_LIBCPP_BEGIN_NAMESPACE_STD
281
282template <class _Tp>
283class _LIBCPP_TEMPLATE_VIS complex;
284
285template <class _Tp, __enable_if_t<is_floating_point<_Tp>::value, int> = 0>
286_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 complex<_Tp>
287operator*(const complex<_Tp>& __z, const complex<_Tp>& __w);
288
289template <class _Tp, __enable_if_t<!is_floating_point<_Tp>::value, int> = 0>
290_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 complex<_Tp>
291operator*(const complex<_Tp>& __z, const complex<_Tp>& __w);
292
293template <class _Tp, __enable_if_t<is_floating_point<_Tp>::value, int> = 0>
294_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 complex<_Tp>
295operator/(const complex<_Tp>& __x, const complex<_Tp>& __y);
296
297template <class _Tp, __enable_if_t<!is_floating_point<_Tp>::value, int> = 0>
298_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 complex<_Tp>
299operator/(const complex<_Tp>& __x, const complex<_Tp>& __y);
300
301template <class _Tp>
302class _LIBCPP_TEMPLATE_VIS complex {
303public:
304  typedef _Tp value_type;
305
306private:
307  value_type __re_;
308  value_type __im_;
309
310public:
311  _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX14
312  complex(const value_type& __re = value_type(), const value_type& __im = value_type())
313      : __re_(__re), __im_(__im) {}
314  template <class _Xp>
315  _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX14 complex(const complex<_Xp>& __c)
316      : __re_(__c.real()), __im_(__c.imag()) {}
317
318  _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX14 value_type real() const { return __re_; }
319  _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX14 value_type imag() const { return __im_; }
320
321  _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 void real(value_type __re) { __re_ = __re; }
322  _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 void imag(value_type __im) { __im_ = __im; }
323
324  _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 complex& operator=(const value_type& __re) {
325    __re_ = __re;
326    __im_ = value_type();
327    return *this;
328  }
329  _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 complex& operator+=(const value_type& __re) {
330    __re_ += __re;
331    return *this;
332  }
333  _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 complex& operator-=(const value_type& __re) {
334    __re_ -= __re;
335    return *this;
336  }
337  _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 complex& operator*=(const value_type& __re) {
338    __re_ *= __re;
339    __im_ *= __re;
340    return *this;
341  }
342  _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 complex& operator/=(const value_type& __re) {
343    __re_ /= __re;
344    __im_ /= __re;
345    return *this;
346  }
347
348  template <class _Xp>
349  _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 complex& operator=(const complex<_Xp>& __c) {
350    __re_ = __c.real();
351    __im_ = __c.imag();
352    return *this;
353  }
354  template <class _Xp>
355  _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 complex& operator+=(const complex<_Xp>& __c) {
356    __re_ += __c.real();
357    __im_ += __c.imag();
358    return *this;
359  }
360  template <class _Xp>
361  _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 complex& operator-=(const complex<_Xp>& __c) {
362    __re_ -= __c.real();
363    __im_ -= __c.imag();
364    return *this;
365  }
366  template <class _Xp>
367  _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 complex& operator*=(const complex<_Xp>& __c) {
368    *this = *this * complex(__c.real(), __c.imag());
369    return *this;
370  }
371  template <class _Xp>
372  _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 complex& operator/=(const complex<_Xp>& __c) {
373    *this = *this / complex(__c.real(), __c.imag());
374    return *this;
375  }
376
377#if _LIBCPP_STD_VER >= 26
378  template <size_t _Ip, class _Xp>
379  friend _LIBCPP_HIDE_FROM_ABI constexpr _Xp& get(complex<_Xp>&) noexcept;
380
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 const _Xp& get(const 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#endif
390};
391
392template <>
393class _LIBCPP_TEMPLATE_VIS complex<double>;
394template <>
395class _LIBCPP_TEMPLATE_VIS complex<long double>;
396
397struct __from_builtin_tag {};
398
399template <class _Tp>
400using __complex_t =
401    __conditional_t<is_same<_Tp, float>::value,
402                    _Complex float,
403                    __conditional_t<is_same<_Tp, double>::value, _Complex double, _Complex long double> >;
404
405template <class _Tp>
406_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR __complex_t<_Tp> __make_complex(_Tp __re, _Tp __im) {
407#if __has_builtin(__builtin_complex)
408  return __builtin_complex(__re, __im);
409#else
410  return __complex_t<_Tp>{__re, __im};
411#endif
412}
413
414template <>
415class _LIBCPP_TEMPLATE_VIS complex<float> {
416  float __re_;
417  float __im_;
418
419public:
420  typedef float value_type;
421
422  _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR complex(float __re = 0.0f, float __im = 0.0f) : __re_(__re), __im_(__im) {}
423
424  template <class _Tag, __enable_if_t<_IsSame<_Tag, __from_builtin_tag>::value, int> = 0>
425  _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR explicit complex(_Tag, _Complex float __v)
426      : __re_(__real__ __v), __im_(__imag__ __v) {}
427
428  _LIBCPP_HIDE_FROM_ABI explicit _LIBCPP_CONSTEXPR complex(const complex<double>& __c);
429  _LIBCPP_HIDE_FROM_ABI explicit _LIBCPP_CONSTEXPR complex(const complex<long double>& __c);
430
431  _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR float real() const { return __re_; }
432  _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR float imag() const { return __im_; }
433
434  _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 void real(value_type __re) { __re_ = __re; }
435  _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 void imag(value_type __im) { __im_ = __im; }
436
437  _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR _Complex float __builtin() const { return std::__make_complex(__re_, __im_); }
438  _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX14 void __builtin(_Complex float __f) {
439    __re_ = __real__ __f;
440    __im_ = __imag__ __f;
441  }
442
443  _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 complex& operator=(float __re) {
444    __re_ = __re;
445    __im_ = value_type();
446    return *this;
447  }
448  _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 complex& operator+=(float __re) {
449    __re_ += __re;
450    return *this;
451  }
452  _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 complex& operator-=(float __re) {
453    __re_ -= __re;
454    return *this;
455  }
456  _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 complex& operator*=(float __re) {
457    __re_ *= __re;
458    __im_ *= __re;
459    return *this;
460  }
461  _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 complex& operator/=(float __re) {
462    __re_ /= __re;
463    __im_ /= __re;
464    return *this;
465  }
466
467  template <class _Xp>
468  _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 complex& operator=(const complex<_Xp>& __c) {
469    __re_ = __c.real();
470    __im_ = __c.imag();
471    return *this;
472  }
473  template <class _Xp>
474  _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 complex& operator+=(const complex<_Xp>& __c) {
475    __re_ += __c.real();
476    __im_ += __c.imag();
477    return *this;
478  }
479  template <class _Xp>
480  _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 complex& operator-=(const complex<_Xp>& __c) {
481    __re_ -= __c.real();
482    __im_ -= __c.imag();
483    return *this;
484  }
485  template <class _Xp>
486  _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 complex& operator*=(const complex<_Xp>& __c) {
487    *this = *this * complex(__c.real(), __c.imag());
488    return *this;
489  }
490  template <class _Xp>
491  _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 complex& operator/=(const complex<_Xp>& __c) {
492    *this = *this / complex(__c.real(), __c.imag());
493    return *this;
494  }
495
496#if _LIBCPP_STD_VER >= 26
497  template <size_t _Ip, class _Xp>
498  friend _LIBCPP_HIDE_FROM_ABI constexpr _Xp& get(complex<_Xp>&) noexcept;
499
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 const _Xp& get(const 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#endif
509};
510
511template <>
512class _LIBCPP_TEMPLATE_VIS complex<double> {
513  double __re_;
514  double __im_;
515
516public:
517  typedef double value_type;
518
519  _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR complex(double __re = 0.0, double __im = 0.0) : __re_(__re), __im_(__im) {}
520
521  template <class _Tag, __enable_if_t<_IsSame<_Tag, __from_builtin_tag>::value, int> = 0>
522  _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR explicit complex(_Tag, _Complex double __v)
523      : __re_(__real__ __v), __im_(__imag__ __v) {}
524
525  _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR complex(const complex<float>& __c);
526  _LIBCPP_HIDE_FROM_ABI explicit _LIBCPP_CONSTEXPR complex(const complex<long double>& __c);
527
528  _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR double real() const { return __re_; }
529  _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR double imag() const { return __im_; }
530
531  _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 void real(value_type __re) { __re_ = __re; }
532  _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 void imag(value_type __im) { __im_ = __im; }
533
534  _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR _Complex double __builtin() const {
535    return std::__make_complex(__re_, __im_);
536  }
537
538  _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX14 void __builtin(_Complex double __f) {
539    __re_ = __real__ __f;
540    __im_ = __imag__ __f;
541  }
542
543  _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 complex& operator=(double __re) {
544    __re_ = __re;
545    __im_ = value_type();
546    return *this;
547  }
548  _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 complex& operator+=(double __re) {
549    __re_ += __re;
550    return *this;
551  }
552  _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 complex& operator-=(double __re) {
553    __re_ -= __re;
554    return *this;
555  }
556  _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 complex& operator*=(double __re) {
557    __re_ *= __re;
558    __im_ *= __re;
559    return *this;
560  }
561  _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 complex& operator/=(double __re) {
562    __re_ /= __re;
563    __im_ /= __re;
564    return *this;
565  }
566
567  template <class _Xp>
568  _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 complex& operator=(const complex<_Xp>& __c) {
569    __re_ = __c.real();
570    __im_ = __c.imag();
571    return *this;
572  }
573  template <class _Xp>
574  _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 complex& operator+=(const complex<_Xp>& __c) {
575    __re_ += __c.real();
576    __im_ += __c.imag();
577    return *this;
578  }
579  template <class _Xp>
580  _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 complex& operator-=(const complex<_Xp>& __c) {
581    __re_ -= __c.real();
582    __im_ -= __c.imag();
583    return *this;
584  }
585  template <class _Xp>
586  _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 complex& operator*=(const complex<_Xp>& __c) {
587    *this = *this * complex(__c.real(), __c.imag());
588    return *this;
589  }
590  template <class _Xp>
591  _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 complex& operator/=(const complex<_Xp>& __c) {
592    *this = *this / complex(__c.real(), __c.imag());
593    return *this;
594  }
595
596#if _LIBCPP_STD_VER >= 26
597  template <size_t _Ip, class _Xp>
598  friend _LIBCPP_HIDE_FROM_ABI constexpr _Xp& get(complex<_Xp>&) noexcept;
599
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 const _Xp& get(const 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#endif
609};
610
611template <>
612class _LIBCPP_TEMPLATE_VIS complex<long double> {
613  long double __re_;
614  long double __im_;
615
616public:
617  typedef long double value_type;
618
619  _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR complex(long double __re = 0.0L, long double __im = 0.0L)
620      : __re_(__re), __im_(__im) {}
621
622  template <class _Tag, __enable_if_t<_IsSame<_Tag, __from_builtin_tag>::value, int> = 0>
623  _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR explicit complex(_Tag, _Complex long double __v)
624      : __re_(__real__ __v), __im_(__imag__ __v) {}
625
626  _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR complex(const complex<float>& __c);
627  _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR complex(const complex<double>& __c);
628
629  _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR long double real() const { return __re_; }
630  _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR long double imag() const { return __im_; }
631
632  _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 void real(value_type __re) { __re_ = __re; }
633  _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 void imag(value_type __im) { __im_ = __im; }
634
635  _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR _Complex long double __builtin() const {
636    return std::__make_complex(__re_, __im_);
637  }
638
639  _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX14 void __builtin(_Complex long double __f) {
640    __re_ = __real__ __f;
641    __im_ = __imag__ __f;
642  }
643
644  _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 complex& operator=(long double __re) {
645    __re_ = __re;
646    __im_ = value_type();
647    return *this;
648  }
649  _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 complex& operator+=(long double __re) {
650    __re_ += __re;
651    return *this;
652  }
653  _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 complex& operator-=(long double __re) {
654    __re_ -= __re;
655    return *this;
656  }
657  _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 complex& operator*=(long double __re) {
658    __re_ *= __re;
659    __im_ *= __re;
660    return *this;
661  }
662  _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 complex& operator/=(long double __re) {
663    __re_ /= __re;
664    __im_ /= __re;
665    return *this;
666  }
667
668  template <class _Xp>
669  _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 complex& operator=(const complex<_Xp>& __c) {
670    __re_ = __c.real();
671    __im_ = __c.imag();
672    return *this;
673  }
674  template <class _Xp>
675  _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 complex& operator+=(const complex<_Xp>& __c) {
676    __re_ += __c.real();
677    __im_ += __c.imag();
678    return *this;
679  }
680  template <class _Xp>
681  _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 complex& operator-=(const complex<_Xp>& __c) {
682    __re_ -= __c.real();
683    __im_ -= __c.imag();
684    return *this;
685  }
686  template <class _Xp>
687  _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 complex& operator*=(const complex<_Xp>& __c) {
688    *this = *this * complex(__c.real(), __c.imag());
689    return *this;
690  }
691  template <class _Xp>
692  _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 complex& operator/=(const complex<_Xp>& __c) {
693    *this = *this / complex(__c.real(), __c.imag());
694    return *this;
695  }
696
697#if _LIBCPP_STD_VER >= 26
698  template <size_t _Ip, class _Xp>
699  friend _LIBCPP_HIDE_FROM_ABI constexpr _Xp& get(complex<_Xp>&) noexcept;
700
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 const _Xp& get(const 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#endif
710};
711
712inline _LIBCPP_CONSTEXPR complex<float>::complex(const complex<double>& __c) : __re_(__c.real()), __im_(__c.imag()) {}
713
714inline _LIBCPP_CONSTEXPR complex<float>::complex(const complex<long double>& __c)
715    : __re_(__c.real()), __im_(__c.imag()) {}
716
717inline _LIBCPP_CONSTEXPR complex<double>::complex(const complex<float>& __c) : __re_(__c.real()), __im_(__c.imag()) {}
718
719inline _LIBCPP_CONSTEXPR complex<double>::complex(const complex<long double>& __c)
720    : __re_(__c.real()), __im_(__c.imag()) {}
721
722inline _LIBCPP_CONSTEXPR complex<long double>::complex(const complex<float>& __c)
723    : __re_(__c.real()), __im_(__c.imag()) {}
724
725inline _LIBCPP_CONSTEXPR complex<long double>::complex(const complex<double>& __c)
726    : __re_(__c.real()), __im_(__c.imag()) {}
727
728// 26.3.6 operators:
729
730template <class _Tp>
731inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 complex<_Tp>
732operator+(const complex<_Tp>& __x, const complex<_Tp>& __y) {
733  complex<_Tp> __t(__x);
734  __t += __y;
735  return __t;
736}
737
738template <class _Tp>
739inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 complex<_Tp>
740operator+(const complex<_Tp>& __x, const _Tp& __y) {
741  complex<_Tp> __t(__x);
742  __t += __y;
743  return __t;
744}
745
746template <class _Tp>
747inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 complex<_Tp>
748operator+(const _Tp& __x, const complex<_Tp>& __y) {
749  complex<_Tp> __t(__y);
750  __t += __x;
751  return __t;
752}
753
754template <class _Tp>
755inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 complex<_Tp>
756operator-(const complex<_Tp>& __x, const complex<_Tp>& __y) {
757  complex<_Tp> __t(__x);
758  __t -= __y;
759  return __t;
760}
761
762template <class _Tp>
763inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 complex<_Tp>
764operator-(const complex<_Tp>& __x, const _Tp& __y) {
765  complex<_Tp> __t(__x);
766  __t -= __y;
767  return __t;
768}
769
770template <class _Tp>
771inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 complex<_Tp>
772operator-(const _Tp& __x, const complex<_Tp>& __y) {
773  complex<_Tp> __t(-__y);
774  __t += __x;
775  return __t;
776}
777
778template <class _Tp, __enable_if_t<is_floating_point<_Tp>::value, int> >
779_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 complex<_Tp>
780operator*(const complex<_Tp>& __lhs, const complex<_Tp>& __rhs) {
781  return complex<_Tp>(__from_builtin_tag(), __lhs.__builtin() * __rhs.__builtin());
782}
783
784template <class _Tp, __enable_if_t<!is_floating_point<_Tp>::value, int> >
785_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 complex<_Tp>
786operator*(const complex<_Tp>& __z, const complex<_Tp>& __w) {
787  _Tp __a = __z.real();
788  _Tp __b = __z.imag();
789  _Tp __c = __w.real();
790  _Tp __d = __w.imag();
791
792  return complex<_Tp>((__a * __c) - (__b * __d), (__a * __d) + (__b * __c));
793}
794
795template <class _Tp>
796inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 complex<_Tp>
797operator*(const complex<_Tp>& __x, const _Tp& __y) {
798  complex<_Tp> __t(__x);
799  __t *= __y;
800  return __t;
801}
802
803template <class _Tp>
804inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 complex<_Tp>
805operator*(const _Tp& __x, const complex<_Tp>& __y) {
806  complex<_Tp> __t(__y);
807  __t *= __x;
808  return __t;
809}
810
811template <class _Tp, __enable_if_t<is_floating_point<_Tp>::value, int> >
812_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 complex<_Tp>
813operator/(const complex<_Tp>& __lhs, const complex<_Tp>& __rhs) {
814  return complex<_Tp>(__from_builtin_tag(), __lhs.__builtin() / __rhs.__builtin());
815}
816
817template <class _Tp, __enable_if_t<!is_floating_point<_Tp>::value, int> >
818_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 complex<_Tp>
819operator/(const complex<_Tp>& __z, const complex<_Tp>& __w) {
820  _Tp __a = __z.real();
821  _Tp __b = __z.imag();
822  _Tp __c = __w.real();
823  _Tp __d = __w.imag();
824
825  _Tp __denom = __c * __c + __d * __d;
826  return complex<_Tp>((__a * __c + __b * __d) / __denom, (__b * __c - __a * __d) / __denom);
827}
828
829template <class _Tp>
830inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 complex<_Tp>
831operator/(const complex<_Tp>& __x, const _Tp& __y) {
832  return complex<_Tp>(__x.real() / __y, __x.imag() / __y);
833}
834
835template <class _Tp>
836inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 complex<_Tp>
837operator/(const _Tp& __x, const complex<_Tp>& __y) {
838  complex<_Tp> __t(__x);
839  __t /= __y;
840  return __t;
841}
842
843template <class _Tp>
844inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 complex<_Tp> operator+(const complex<_Tp>& __x) {
845  return __x;
846}
847
848template <class _Tp>
849inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 complex<_Tp> operator-(const complex<_Tp>& __x) {
850  return complex<_Tp>(-__x.real(), -__x.imag());
851}
852
853template <class _Tp>
854inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX14 bool
855operator==(const complex<_Tp>& __x, const complex<_Tp>& __y) {
856  return __x.real() == __y.real() && __x.imag() == __y.imag();
857}
858
859template <class _Tp>
860inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX14 bool operator==(const complex<_Tp>& __x, const _Tp& __y) {
861  return __x.real() == __y && __x.imag() == 0;
862}
863
864#if _LIBCPP_STD_VER <= 17
865
866template <class _Tp>
867inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX14 bool operator==(const _Tp& __x, const complex<_Tp>& __y) {
868  return __x == __y.real() && 0 == __y.imag();
869}
870
871template <class _Tp>
872inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX14 bool
873operator!=(const complex<_Tp>& __x, const complex<_Tp>& __y) {
874  return !(__x == __y);
875}
876
877template <class _Tp>
878inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX14 bool operator!=(const complex<_Tp>& __x, const _Tp& __y) {
879  return !(__x == __y);
880}
881
882template <class _Tp>
883inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX14 bool operator!=(const _Tp& __x, const complex<_Tp>& __y) {
884  return !(__x == __y);
885}
886
887#endif
888
889// 26.3.7 values:
890
891template <class _Tp, bool = is_integral<_Tp>::value, bool = is_floating_point<_Tp>::value >
892struct __libcpp_complex_overload_traits {};
893
894// Integral Types
895template <class _Tp>
896struct __libcpp_complex_overload_traits<_Tp, true, false> {
897  typedef double _ValueType;
898  typedef complex<double> _ComplexType;
899};
900
901// Floating point types
902template <class _Tp>
903struct __libcpp_complex_overload_traits<_Tp, false, true> {
904  typedef _Tp _ValueType;
905  typedef complex<_Tp> _ComplexType;
906};
907
908// real
909
910template <class _Tp>
911inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX14 _Tp real(const complex<_Tp>& __c) {
912  return __c.real();
913}
914
915template <class _Tp>
916inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX14 typename __libcpp_complex_overload_traits<_Tp>::_ValueType
917real(_Tp __re) {
918  return __re;
919}
920
921// imag
922
923template <class _Tp>
924inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX14 _Tp imag(const complex<_Tp>& __c) {
925  return __c.imag();
926}
927
928template <class _Tp>
929inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX14 typename __libcpp_complex_overload_traits<_Tp>::_ValueType
930imag(_Tp) {
931  return 0;
932}
933
934// abs
935
936template <class _Tp>
937inline _LIBCPP_HIDE_FROM_ABI _Tp abs(const complex<_Tp>& __c) {
938  return std::hypot(__c.real(), __c.imag());
939}
940
941// arg
942
943template <class _Tp>
944inline _LIBCPP_HIDE_FROM_ABI _Tp arg(const complex<_Tp>& __c) {
945  return std::atan2(__c.imag(), __c.real());
946}
947
948template <class _Tp, __enable_if_t<is_same<_Tp, long double>::value, int> = 0>
949inline _LIBCPP_HIDE_FROM_ABI long double arg(_Tp __re) {
950  return std::atan2l(0.L, __re);
951}
952
953template <class _Tp, __enable_if_t<is_integral<_Tp>::value || is_same<_Tp, double>::value, int> = 0>
954inline _LIBCPP_HIDE_FROM_ABI double arg(_Tp __re) {
955  return std::atan2(0., __re);
956}
957
958template <class _Tp, __enable_if_t<is_same<_Tp, float>::value, int> = 0>
959inline _LIBCPP_HIDE_FROM_ABI float arg(_Tp __re) {
960  return std::atan2f(0.F, __re);
961}
962
963// norm
964
965template <class _Tp>
966inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 _Tp norm(const complex<_Tp>& __c) {
967  if (std::__constexpr_isinf(__c.real()))
968    return std::abs(__c.real());
969  if (std::__constexpr_isinf(__c.imag()))
970    return std::abs(__c.imag());
971  return __c.real() * __c.real() + __c.imag() * __c.imag();
972}
973
974template <class _Tp>
975inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 typename __libcpp_complex_overload_traits<_Tp>::_ValueType
976norm(_Tp __re) {
977  typedef typename __libcpp_complex_overload_traits<_Tp>::_ValueType _ValueType;
978  return static_cast<_ValueType>(__re) * __re;
979}
980
981// conj
982
983template <class _Tp>
984inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 complex<_Tp> conj(const complex<_Tp>& __c) {
985  return complex<_Tp>(__c.real(), -__c.imag());
986}
987
988template <class _Tp>
989inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 typename __libcpp_complex_overload_traits<_Tp>::_ComplexType
990conj(_Tp __re) {
991  typedef typename __libcpp_complex_overload_traits<_Tp>::_ComplexType _ComplexType;
992  return _ComplexType(__re);
993}
994
995// proj
996
997template <class _Tp>
998inline _LIBCPP_HIDE_FROM_ABI complex<_Tp> proj(const complex<_Tp>& __c) {
999  complex<_Tp> __r = __c;
1000  if (std::isinf(__c.real()) || std::isinf(__c.imag()))
1001    __r = complex<_Tp>(INFINITY, std::copysign(_Tp(0), __c.imag()));
1002  return __r;
1003}
1004
1005template <class _Tp, __enable_if_t<is_floating_point<_Tp>::value, int> = 0>
1006inline _LIBCPP_HIDE_FROM_ABI typename __libcpp_complex_overload_traits<_Tp>::_ComplexType proj(_Tp __re) {
1007  if (std::isinf(__re))
1008    __re = std::abs(__re);
1009  return complex<_Tp>(__re);
1010}
1011
1012template <class _Tp, __enable_if_t<is_integral<_Tp>::value, int> = 0>
1013inline _LIBCPP_HIDE_FROM_ABI typename __libcpp_complex_overload_traits<_Tp>::_ComplexType proj(_Tp __re) {
1014  typedef typename __libcpp_complex_overload_traits<_Tp>::_ComplexType _ComplexType;
1015  return _ComplexType(__re);
1016}
1017
1018// polar
1019
1020template <class _Tp>
1021_LIBCPP_HIDE_FROM_ABI complex<_Tp> polar(const _Tp& __rho, const _Tp& __theta = _Tp()) {
1022  if (std::isnan(__rho) || std::signbit(__rho))
1023    return complex<_Tp>(_Tp(NAN), _Tp(NAN));
1024  if (std::isnan(__theta)) {
1025    if (std::isinf(__rho))
1026      return complex<_Tp>(__rho, __theta);
1027    return complex<_Tp>(__theta, __theta);
1028  }
1029  if (std::isinf(__theta)) {
1030    if (std::isinf(__rho))
1031      return complex<_Tp>(__rho, _Tp(NAN));
1032    return complex<_Tp>(_Tp(NAN), _Tp(NAN));
1033  }
1034  _Tp __x = __rho * std::cos(__theta);
1035  if (std::isnan(__x))
1036    __x = 0;
1037  _Tp __y = __rho * std::sin(__theta);
1038  if (std::isnan(__y))
1039    __y = 0;
1040  return complex<_Tp>(__x, __y);
1041}
1042
1043// log
1044
1045template <class _Tp>
1046inline _LIBCPP_HIDE_FROM_ABI complex<_Tp> log(const complex<_Tp>& __x) {
1047  return complex<_Tp>(std::log(std::abs(__x)), std::arg(__x));
1048}
1049
1050// log10
1051
1052template <class _Tp>
1053inline _LIBCPP_HIDE_FROM_ABI complex<_Tp> log10(const complex<_Tp>& __x) {
1054  return std::log(__x) / std::log(_Tp(10));
1055}
1056
1057// sqrt
1058
1059template <class _Tp>
1060_LIBCPP_HIDE_FROM_ABI complex<_Tp> sqrt(const complex<_Tp>& __x) {
1061  if (std::isinf(__x.imag()))
1062    return complex<_Tp>(_Tp(INFINITY), __x.imag());
1063  if (std::isinf(__x.real())) {
1064    if (__x.real() > _Tp(0))
1065      return complex<_Tp>(__x.real(), std::isnan(__x.imag()) ? __x.imag() : std::copysign(_Tp(0), __x.imag()));
1066    return complex<_Tp>(std::isnan(__x.imag()) ? __x.imag() : _Tp(0), std::copysign(__x.real(), __x.imag()));
1067  }
1068  return std::polar(std::sqrt(std::abs(__x)), std::arg(__x) / _Tp(2));
1069}
1070
1071// exp
1072
1073template <class _Tp>
1074_LIBCPP_HIDE_FROM_ABI complex<_Tp> exp(const complex<_Tp>& __x) {
1075  _Tp __i = __x.imag();
1076  if (__i == 0) {
1077    return complex<_Tp>(std::exp(__x.real()), std::copysign(_Tp(0), __x.imag()));
1078  }
1079  if (std::isinf(__x.real())) {
1080    if (__x.real() < _Tp(0)) {
1081      if (!std::isfinite(__i))
1082        __i = _Tp(1);
1083    } else if (__i == 0 || !std::isfinite(__i)) {
1084      if (std::isinf(__i))
1085        __i = _Tp(NAN);
1086      return complex<_Tp>(__x.real(), __i);
1087    }
1088  }
1089  _Tp __e = std::exp(__x.real());
1090  return complex<_Tp>(__e * std::cos(__i), __e * std::sin(__i));
1091}
1092
1093// pow
1094
1095template <class _Tp>
1096inline _LIBCPP_HIDE_FROM_ABI complex<_Tp> pow(const complex<_Tp>& __x, const complex<_Tp>& __y) {
1097  return std::exp(__y * std::log(__x));
1098}
1099
1100template <class _Tp, class _Up, __enable_if_t<is_floating_point<_Tp>::value && is_floating_point<_Up>::value, int> = 0>
1101inline _LIBCPP_HIDE_FROM_ABI complex<typename __promote<_Tp, _Up>::type>
1102pow(const complex<_Tp>& __x, const complex<_Up>& __y) {
1103  typedef complex<typename __promote<_Tp, _Up>::type> result_type;
1104  return std::pow(result_type(__x), result_type(__y));
1105}
1106
1107template <class _Tp, class _Up, __enable_if_t<is_floating_point<_Tp>::value && is_arithmetic<_Up>::value, int> = 0>
1108inline _LIBCPP_HIDE_FROM_ABI complex<typename __promote<_Tp, _Up>::type> pow(const complex<_Tp>& __x, const _Up& __y) {
1109  typedef complex<typename __promote<_Tp, _Up>::type> result_type;
1110  return std::pow(result_type(__x), result_type(__y));
1111}
1112
1113template <class _Tp, class _Up, __enable_if_t<is_arithmetic<_Tp>::value && is_floating_point<_Up>::value, int> = 0>
1114inline _LIBCPP_HIDE_FROM_ABI complex<typename __promote<_Tp, _Up>::type> pow(const _Tp& __x, const complex<_Up>& __y) {
1115  typedef complex<typename __promote<_Tp, _Up>::type> result_type;
1116  return std::pow(result_type(__x), result_type(__y));
1117}
1118
1119// __sqr, computes pow(x, 2)
1120
1121template <class _Tp>
1122inline _LIBCPP_HIDE_FROM_ABI complex<_Tp> __sqr(const complex<_Tp>& __x) {
1123  return complex<_Tp>((__x.real() - __x.imag()) * (__x.real() + __x.imag()), _Tp(2) * __x.real() * __x.imag());
1124}
1125
1126// asinh
1127
1128template <class _Tp>
1129_LIBCPP_HIDE_FROM_ABI complex<_Tp> asinh(const complex<_Tp>& __x) {
1130  const _Tp __pi(atan2(+0., -0.));
1131  if (std::isinf(__x.real())) {
1132    if (std::isnan(__x.imag()))
1133      return __x;
1134    if (std::isinf(__x.imag()))
1135      return complex<_Tp>(__x.real(), std::copysign(__pi * _Tp(0.25), __x.imag()));
1136    return complex<_Tp>(__x.real(), std::copysign(_Tp(0), __x.imag()));
1137  }
1138  if (std::isnan(__x.real())) {
1139    if (std::isinf(__x.imag()))
1140      return complex<_Tp>(__x.imag(), __x.real());
1141    if (__x.imag() == 0)
1142      return __x;
1143    return complex<_Tp>(__x.real(), __x.real());
1144  }
1145  if (std::isinf(__x.imag()))
1146    return complex<_Tp>(std::copysign(__x.imag(), __x.real()), std::copysign(__pi / _Tp(2), __x.imag()));
1147  complex<_Tp> __z = std::log(__x + std::sqrt(std::__sqr(__x) + _Tp(1)));
1148  return complex<_Tp>(std::copysign(__z.real(), __x.real()), std::copysign(__z.imag(), __x.imag()));
1149}
1150
1151// acosh
1152
1153template <class _Tp>
1154_LIBCPP_HIDE_FROM_ABI complex<_Tp> acosh(const complex<_Tp>& __x) {
1155  const _Tp __pi(atan2(+0., -0.));
1156  if (std::isinf(__x.real())) {
1157    if (std::isnan(__x.imag()))
1158      return complex<_Tp>(std::abs(__x.real()), __x.imag());
1159    if (std::isinf(__x.imag())) {
1160      if (__x.real() > 0)
1161        return complex<_Tp>(__x.real(), std::copysign(__pi * _Tp(0.25), __x.imag()));
1162      else
1163        return complex<_Tp>(-__x.real(), std::copysign(__pi * _Tp(0.75), __x.imag()));
1164    }
1165    if (__x.real() < 0)
1166      return complex<_Tp>(-__x.real(), std::copysign(__pi, __x.imag()));
1167    return complex<_Tp>(__x.real(), std::copysign(_Tp(0), __x.imag()));
1168  }
1169  if (std::isnan(__x.real())) {
1170    if (std::isinf(__x.imag()))
1171      return complex<_Tp>(std::abs(__x.imag()), __x.real());
1172    return complex<_Tp>(__x.real(), __x.real());
1173  }
1174  if (std::isinf(__x.imag()))
1175    return complex<_Tp>(std::abs(__x.imag()), std::copysign(__pi / _Tp(2), __x.imag()));
1176  complex<_Tp> __z = std::log(__x + std::sqrt(std::__sqr(__x) - _Tp(1)));
1177  return complex<_Tp>(std::copysign(__z.real(), _Tp(0)), std::copysign(__z.imag(), __x.imag()));
1178}
1179
1180// atanh
1181
1182template <class _Tp>
1183_LIBCPP_HIDE_FROM_ABI complex<_Tp> atanh(const complex<_Tp>& __x) {
1184  const _Tp __pi(atan2(+0., -0.));
1185  if (std::isinf(__x.imag())) {
1186    return complex<_Tp>(std::copysign(_Tp(0), __x.real()), std::copysign(__pi / _Tp(2), __x.imag()));
1187  }
1188  if (std::isnan(__x.imag())) {
1189    if (std::isinf(__x.real()) || __x.real() == 0)
1190      return complex<_Tp>(std::copysign(_Tp(0), __x.real()), __x.imag());
1191    return complex<_Tp>(__x.imag(), __x.imag());
1192  }
1193  if (std::isnan(__x.real())) {
1194    return complex<_Tp>(__x.real(), __x.real());
1195  }
1196  if (std::isinf(__x.real())) {
1197    return complex<_Tp>(std::copysign(_Tp(0), __x.real()), std::copysign(__pi / _Tp(2), __x.imag()));
1198  }
1199  if (std::abs(__x.real()) == _Tp(1) && __x.imag() == _Tp(0)) {
1200    return complex<_Tp>(std::copysign(_Tp(INFINITY), __x.real()), std::copysign(_Tp(0), __x.imag()));
1201  }
1202  complex<_Tp> __z = std::log((_Tp(1) + __x) / (_Tp(1) - __x)) / _Tp(2);
1203  return complex<_Tp>(std::copysign(__z.real(), __x.real()), std::copysign(__z.imag(), __x.imag()));
1204}
1205
1206// sinh
1207
1208template <class _Tp>
1209_LIBCPP_HIDE_FROM_ABI complex<_Tp> sinh(const complex<_Tp>& __x) {
1210  if (std::isinf(__x.real()) && !std::isfinite(__x.imag()))
1211    return complex<_Tp>(__x.real(), _Tp(NAN));
1212  if (__x.real() == 0 && !std::isfinite(__x.imag()))
1213    return complex<_Tp>(__x.real(), _Tp(NAN));
1214  if (__x.imag() == 0 && !std::isfinite(__x.real()))
1215    return __x;
1216  return complex<_Tp>(std::sinh(__x.real()) * std::cos(__x.imag()), std::cosh(__x.real()) * std::sin(__x.imag()));
1217}
1218
1219// cosh
1220
1221template <class _Tp>
1222_LIBCPP_HIDE_FROM_ABI complex<_Tp> cosh(const complex<_Tp>& __x) {
1223  if (std::isinf(__x.real()) && !std::isfinite(__x.imag()))
1224    return complex<_Tp>(std::abs(__x.real()), _Tp(NAN));
1225  if (__x.real() == 0 && !std::isfinite(__x.imag()))
1226    return complex<_Tp>(_Tp(NAN), __x.real());
1227  if (__x.real() == 0 && __x.imag() == 0)
1228    return complex<_Tp>(_Tp(1), __x.imag());
1229  if (__x.imag() == 0 && !std::isfinite(__x.real()))
1230    return complex<_Tp>(std::abs(__x.real()), __x.imag());
1231  return complex<_Tp>(std::cosh(__x.real()) * std::cos(__x.imag()), std::sinh(__x.real()) * std::sin(__x.imag()));
1232}
1233
1234// tanh
1235
1236template <class _Tp>
1237_LIBCPP_HIDE_FROM_ABI complex<_Tp> tanh(const complex<_Tp>& __x) {
1238  if (std::isinf(__x.real())) {
1239    if (!std::isfinite(__x.imag()))
1240      return complex<_Tp>(std::copysign(_Tp(1), __x.real()), _Tp(0));
1241    return complex<_Tp>(std::copysign(_Tp(1), __x.real()), std::copysign(_Tp(0), std::sin(_Tp(2) * __x.imag())));
1242  }
1243  if (std::isnan(__x.real()) && __x.imag() == 0)
1244    return __x;
1245  _Tp __2r(_Tp(2) * __x.real());
1246  _Tp __2i(_Tp(2) * __x.imag());
1247  _Tp __d(std::cosh(__2r) + std::cos(__2i));
1248  _Tp __2rsh(std::sinh(__2r));
1249  if (std::isinf(__2rsh) && std::isinf(__d))
1250    return complex<_Tp>(__2rsh > _Tp(0) ? _Tp(1) : _Tp(-1), __2i > _Tp(0) ? _Tp(0) : _Tp(-0.));
1251  return complex<_Tp>(__2rsh / __d, std::sin(__2i) / __d);
1252}
1253
1254// asin
1255
1256template <class _Tp>
1257_LIBCPP_HIDE_FROM_ABI complex<_Tp> asin(const complex<_Tp>& __x) {
1258  complex<_Tp> __z = std::asinh(complex<_Tp>(-__x.imag(), __x.real()));
1259  return complex<_Tp>(__z.imag(), -__z.real());
1260}
1261
1262// acos
1263
1264template <class _Tp>
1265_LIBCPP_HIDE_FROM_ABI complex<_Tp> acos(const complex<_Tp>& __x) {
1266  const _Tp __pi(atan2(+0., -0.));
1267  if (std::isinf(__x.real())) {
1268    if (std::isnan(__x.imag()))
1269      return complex<_Tp>(__x.imag(), __x.real());
1270    if (std::isinf(__x.imag())) {
1271      if (__x.real() < _Tp(0))
1272        return complex<_Tp>(_Tp(0.75) * __pi, -__x.imag());
1273      return complex<_Tp>(_Tp(0.25) * __pi, -__x.imag());
1274    }
1275    if (__x.real() < _Tp(0))
1276      return complex<_Tp>(__pi, std::signbit(__x.imag()) ? -__x.real() : __x.real());
1277    return complex<_Tp>(_Tp(0), std::signbit(__x.imag()) ? __x.real() : -__x.real());
1278  }
1279  if (std::isnan(__x.real())) {
1280    if (std::isinf(__x.imag()))
1281      return complex<_Tp>(__x.real(), -__x.imag());
1282    return complex<_Tp>(__x.real(), __x.real());
1283  }
1284  if (std::isinf(__x.imag()))
1285    return complex<_Tp>(__pi / _Tp(2), -__x.imag());
1286  if (__x.real() == 0 && (__x.imag() == 0 || std::isnan(__x.imag())))
1287    return complex<_Tp>(__pi / _Tp(2), -__x.imag());
1288  complex<_Tp> __z = std::log(__x + std::sqrt(std::__sqr(__x) - _Tp(1)));
1289  if (std::signbit(__x.imag()))
1290    return complex<_Tp>(std::abs(__z.imag()), std::abs(__z.real()));
1291  return complex<_Tp>(std::abs(__z.imag()), -std::abs(__z.real()));
1292}
1293
1294// atan
1295
1296template <class _Tp>
1297_LIBCPP_HIDE_FROM_ABI complex<_Tp> atan(const complex<_Tp>& __x) {
1298  complex<_Tp> __z = std::atanh(complex<_Tp>(-__x.imag(), __x.real()));
1299  return complex<_Tp>(__z.imag(), -__z.real());
1300}
1301
1302// sin
1303
1304template <class _Tp>
1305_LIBCPP_HIDE_FROM_ABI complex<_Tp> sin(const complex<_Tp>& __x) {
1306  complex<_Tp> __z = std::sinh(complex<_Tp>(-__x.imag(), __x.real()));
1307  return complex<_Tp>(__z.imag(), -__z.real());
1308}
1309
1310// cos
1311
1312template <class _Tp>
1313inline _LIBCPP_HIDE_FROM_ABI complex<_Tp> cos(const complex<_Tp>& __x) {
1314  return std::cosh(complex<_Tp>(-__x.imag(), __x.real()));
1315}
1316
1317// tan
1318
1319template <class _Tp>
1320_LIBCPP_HIDE_FROM_ABI complex<_Tp> tan(const complex<_Tp>& __x) {
1321  complex<_Tp> __z = std::tanh(complex<_Tp>(-__x.imag(), __x.real()));
1322  return complex<_Tp>(__z.imag(), -__z.real());
1323}
1324
1325#if _LIBCPP_HAS_LOCALIZATION
1326template <class _Tp, class _CharT, class _Traits>
1327_LIBCPP_HIDE_FROM_ABI basic_istream<_CharT, _Traits>&
1328operator>>(basic_istream<_CharT, _Traits>& __is, complex<_Tp>& __x) {
1329  if (__is.good()) {
1330    std::ws(__is);
1331    if (__is.peek() == _CharT('(')) {
1332      __is.get();
1333      _Tp __r;
1334      __is >> __r;
1335      if (!__is.fail()) {
1336        std::ws(__is);
1337        _CharT __c = __is.peek();
1338        if (__c == _CharT(',')) {
1339          __is.get();
1340          _Tp __i;
1341          __is >> __i;
1342          if (!__is.fail()) {
1343            std::ws(__is);
1344            __c = __is.peek();
1345            if (__c == _CharT(')')) {
1346              __is.get();
1347              __x = complex<_Tp>(__r, __i);
1348            } else
1349              __is.setstate(__is.failbit);
1350          } else
1351            __is.setstate(__is.failbit);
1352        } else if (__c == _CharT(')')) {
1353          __is.get();
1354          __x = complex<_Tp>(__r, _Tp(0));
1355        } else
1356          __is.setstate(__is.failbit);
1357      } else
1358        __is.setstate(__is.failbit);
1359    } else {
1360      _Tp __r;
1361      __is >> __r;
1362      if (!__is.fail())
1363        __x = complex<_Tp>(__r, _Tp(0));
1364      else
1365        __is.setstate(__is.failbit);
1366    }
1367  } else
1368    __is.setstate(__is.failbit);
1369  return __is;
1370}
1371
1372template <class _Tp, class _CharT, class _Traits>
1373_LIBCPP_HIDE_FROM_ABI basic_ostream<_CharT, _Traits>&
1374operator<<(basic_ostream<_CharT, _Traits>& __os, const complex<_Tp>& __x) {
1375  basic_ostringstream<_CharT, _Traits> __s;
1376  __s.flags(__os.flags());
1377  __s.imbue(__os.getloc());
1378  __s.precision(__os.precision());
1379  __s << '(' << __x.real() << ',' << __x.imag() << ')';
1380  return __os << __s.str();
1381}
1382#endif // _LIBCPP_HAS_LOCALIZATION
1383
1384#if _LIBCPP_STD_VER >= 26
1385
1386// [complex.tuple], tuple interface
1387
1388template <class _Tp>
1389struct tuple_size<complex<_Tp>> : integral_constant<size_t, 2> {};
1390
1391template <size_t _Ip, class _Tp>
1392struct tuple_element<_Ip, complex<_Tp>> {
1393  static_assert(_Ip < 2, "Index value is out of range.");
1394  using type = _Tp;
1395};
1396
1397template <size_t _Ip, class _Xp>
1398_LIBCPP_HIDE_FROM_ABI constexpr _Xp& get(complex<_Xp>& __z) noexcept {
1399  static_assert(_Ip < 2, "Index value is out of range.");
1400  if constexpr (_Ip == 0) {
1401    return __z.__re_;
1402  } else {
1403    return __z.__im_;
1404  }
1405}
1406
1407template <size_t _Ip, class _Xp>
1408_LIBCPP_HIDE_FROM_ABI constexpr _Xp&& get(complex<_Xp>&& __z) noexcept {
1409  static_assert(_Ip < 2, "Index value is out of range.");
1410  if constexpr (_Ip == 0) {
1411    return std::move(__z.__re_);
1412  } else {
1413    return std::move(__z.__im_);
1414  }
1415}
1416
1417template <size_t _Ip, class _Xp>
1418_LIBCPP_HIDE_FROM_ABI constexpr const _Xp& get(const complex<_Xp>& __z) noexcept {
1419  static_assert(_Ip < 2, "Index value is out of range.");
1420  if constexpr (_Ip == 0) {
1421    return __z.__re_;
1422  } else {
1423    return __z.__im_;
1424  }
1425}
1426
1427template <size_t _Ip, class _Xp>
1428_LIBCPP_HIDE_FROM_ABI constexpr const _Xp&& get(const complex<_Xp>&& __z) noexcept {
1429  static_assert(_Ip < 2, "Index value is out of range.");
1430  if constexpr (_Ip == 0) {
1431    return std::move(__z.__re_);
1432  } else {
1433    return std::move(__z.__im_);
1434  }
1435}
1436
1437#endif // _LIBCPP_STD_VER >= 26
1438
1439#if _LIBCPP_STD_VER >= 14
1440// Literal suffix for complex number literals [complex.literals]
1441inline namespace literals {
1442inline namespace complex_literals {
1443_LIBCPP_HIDE_FROM_ABI inline constexpr complex<long double> operator""il(long double __im) { return {0.0l, __im}; }
1444
1445_LIBCPP_HIDE_FROM_ABI inline constexpr complex<long double> operator""il(unsigned long long __im) {
1446  return {0.0l, static_cast<long double>(__im)};
1447}
1448
1449_LIBCPP_HIDE_FROM_ABI inline constexpr complex<double> operator""i(long double __im) {
1450  return {0.0, static_cast<double>(__im)};
1451}
1452
1453_LIBCPP_HIDE_FROM_ABI inline constexpr complex<double> operator""i(unsigned long long __im) {
1454  return {0.0, static_cast<double>(__im)};
1455}
1456
1457_LIBCPP_HIDE_FROM_ABI inline constexpr complex<float> operator""if(long double __im) {
1458  return {0.0f, static_cast<float>(__im)};
1459}
1460
1461_LIBCPP_HIDE_FROM_ABI inline constexpr complex<float> operator""if(unsigned long long __im) {
1462  return {0.0f, static_cast<float>(__im)};
1463}
1464} // namespace complex_literals
1465} // namespace literals
1466#endif
1467
1468_LIBCPP_END_NAMESPACE_STD
1469
1470_LIBCPP_POP_MACROS
1471
1472#if !defined(_LIBCPP_REMOVE_TRANSITIVE_INCLUDES) && _LIBCPP_STD_VER <= 20
1473#  include <iosfwd>
1474#  include <stdexcept>
1475#  include <type_traits>
1476#endif
1477
1478#endif // _LIBCPP_COMPLEX
1479