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