xref: /netbsd-src/external/apache2/llvm/dist/libcxx/include/complex (revision 4d6fc14bc9b0c5bf3e30be318c143ee82cadd108)
1*4d6fc14bSjoerg// -*- C++ -*-
2*4d6fc14bSjoerg//===--------------------------- complex ----------------------------------===//
3*4d6fc14bSjoerg//
4*4d6fc14bSjoerg// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
5*4d6fc14bSjoerg// See https://llvm.org/LICENSE.txt for license information.
6*4d6fc14bSjoerg// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7*4d6fc14bSjoerg//
8*4d6fc14bSjoerg//===----------------------------------------------------------------------===//
9*4d6fc14bSjoerg
10*4d6fc14bSjoerg#ifndef _LIBCPP_COMPLEX
11*4d6fc14bSjoerg#define _LIBCPP_COMPLEX
12*4d6fc14bSjoerg
13*4d6fc14bSjoerg/*
14*4d6fc14bSjoerg    complex synopsis
15*4d6fc14bSjoerg
16*4d6fc14bSjoergnamespace std
17*4d6fc14bSjoerg{
18*4d6fc14bSjoerg
19*4d6fc14bSjoergtemplate<class T>
20*4d6fc14bSjoergclass complex
21*4d6fc14bSjoerg{
22*4d6fc14bSjoergpublic:
23*4d6fc14bSjoerg    typedef T value_type;
24*4d6fc14bSjoerg
25*4d6fc14bSjoerg    complex(const T& re = T(), const T& im = T()); // constexpr in C++14
26*4d6fc14bSjoerg    complex(const complex&);  // constexpr in C++14
27*4d6fc14bSjoerg    template<class X> complex(const complex<X>&);  // constexpr in C++14
28*4d6fc14bSjoerg
29*4d6fc14bSjoerg    T real() const; // constexpr in C++14
30*4d6fc14bSjoerg    T imag() const; // constexpr in C++14
31*4d6fc14bSjoerg
32*4d6fc14bSjoerg    void real(T);
33*4d6fc14bSjoerg    void imag(T);
34*4d6fc14bSjoerg
35*4d6fc14bSjoerg    complex<T>& operator= (const T&);
36*4d6fc14bSjoerg    complex<T>& operator+=(const T&);
37*4d6fc14bSjoerg    complex<T>& operator-=(const T&);
38*4d6fc14bSjoerg    complex<T>& operator*=(const T&);
39*4d6fc14bSjoerg    complex<T>& operator/=(const T&);
40*4d6fc14bSjoerg
41*4d6fc14bSjoerg    complex& operator=(const complex&);
42*4d6fc14bSjoerg    template<class X> complex<T>& operator= (const complex<X>&);
43*4d6fc14bSjoerg    template<class X> complex<T>& operator+=(const complex<X>&);
44*4d6fc14bSjoerg    template<class X> complex<T>& operator-=(const complex<X>&);
45*4d6fc14bSjoerg    template<class X> complex<T>& operator*=(const complex<X>&);
46*4d6fc14bSjoerg    template<class X> complex<T>& operator/=(const complex<X>&);
47*4d6fc14bSjoerg};
48*4d6fc14bSjoerg
49*4d6fc14bSjoergtemplate<>
50*4d6fc14bSjoergclass complex<float>
51*4d6fc14bSjoerg{
52*4d6fc14bSjoergpublic:
53*4d6fc14bSjoerg    typedef float value_type;
54*4d6fc14bSjoerg
55*4d6fc14bSjoerg    constexpr complex(float re = 0.0f, float im = 0.0f);
56*4d6fc14bSjoerg    explicit constexpr complex(const complex<double>&);
57*4d6fc14bSjoerg    explicit constexpr complex(const complex<long double>&);
58*4d6fc14bSjoerg
59*4d6fc14bSjoerg    constexpr float real() const;
60*4d6fc14bSjoerg    void real(float);
61*4d6fc14bSjoerg    constexpr float imag() const;
62*4d6fc14bSjoerg    void imag(float);
63*4d6fc14bSjoerg
64*4d6fc14bSjoerg    complex<float>& operator= (float);
65*4d6fc14bSjoerg    complex<float>& operator+=(float);
66*4d6fc14bSjoerg    complex<float>& operator-=(float);
67*4d6fc14bSjoerg    complex<float>& operator*=(float);
68*4d6fc14bSjoerg    complex<float>& operator/=(float);
69*4d6fc14bSjoerg
70*4d6fc14bSjoerg    complex<float>& operator=(const complex<float>&);
71*4d6fc14bSjoerg    template<class X> complex<float>& operator= (const complex<X>&);
72*4d6fc14bSjoerg    template<class X> complex<float>& operator+=(const complex<X>&);
73*4d6fc14bSjoerg    template<class X> complex<float>& operator-=(const complex<X>&);
74*4d6fc14bSjoerg    template<class X> complex<float>& operator*=(const complex<X>&);
75*4d6fc14bSjoerg    template<class X> complex<float>& operator/=(const complex<X>&);
76*4d6fc14bSjoerg};
77*4d6fc14bSjoerg
78*4d6fc14bSjoergtemplate<>
79*4d6fc14bSjoergclass complex<double>
80*4d6fc14bSjoerg{
81*4d6fc14bSjoergpublic:
82*4d6fc14bSjoerg    typedef double value_type;
83*4d6fc14bSjoerg
84*4d6fc14bSjoerg    constexpr complex(double re = 0.0, double im = 0.0);
85*4d6fc14bSjoerg    constexpr complex(const complex<float>&);
86*4d6fc14bSjoerg    explicit constexpr complex(const complex<long double>&);
87*4d6fc14bSjoerg
88*4d6fc14bSjoerg    constexpr double real() const;
89*4d6fc14bSjoerg    void real(double);
90*4d6fc14bSjoerg    constexpr double imag() const;
91*4d6fc14bSjoerg    void imag(double);
92*4d6fc14bSjoerg
93*4d6fc14bSjoerg    complex<double>& operator= (double);
94*4d6fc14bSjoerg    complex<double>& operator+=(double);
95*4d6fc14bSjoerg    complex<double>& operator-=(double);
96*4d6fc14bSjoerg    complex<double>& operator*=(double);
97*4d6fc14bSjoerg    complex<double>& operator/=(double);
98*4d6fc14bSjoerg    complex<double>& operator=(const complex<double>&);
99*4d6fc14bSjoerg
100*4d6fc14bSjoerg    template<class X> complex<double>& operator= (const complex<X>&);
101*4d6fc14bSjoerg    template<class X> complex<double>& operator+=(const complex<X>&);
102*4d6fc14bSjoerg    template<class X> complex<double>& operator-=(const complex<X>&);
103*4d6fc14bSjoerg    template<class X> complex<double>& operator*=(const complex<X>&);
104*4d6fc14bSjoerg    template<class X> complex<double>& operator/=(const complex<X>&);
105*4d6fc14bSjoerg};
106*4d6fc14bSjoerg
107*4d6fc14bSjoergtemplate<>
108*4d6fc14bSjoergclass complex<long double>
109*4d6fc14bSjoerg{
110*4d6fc14bSjoergpublic:
111*4d6fc14bSjoerg    typedef long double value_type;
112*4d6fc14bSjoerg
113*4d6fc14bSjoerg    constexpr complex(long double re = 0.0L, long double im = 0.0L);
114*4d6fc14bSjoerg    constexpr complex(const complex<float>&);
115*4d6fc14bSjoerg    constexpr complex(const complex<double>&);
116*4d6fc14bSjoerg
117*4d6fc14bSjoerg    constexpr long double real() const;
118*4d6fc14bSjoerg    void real(long double);
119*4d6fc14bSjoerg    constexpr long double imag() const;
120*4d6fc14bSjoerg    void imag(long double);
121*4d6fc14bSjoerg
122*4d6fc14bSjoerg    complex<long double>& operator=(const complex<long double>&);
123*4d6fc14bSjoerg    complex<long double>& operator= (long double);
124*4d6fc14bSjoerg    complex<long double>& operator+=(long double);
125*4d6fc14bSjoerg    complex<long double>& operator-=(long double);
126*4d6fc14bSjoerg    complex<long double>& operator*=(long double);
127*4d6fc14bSjoerg    complex<long double>& operator/=(long double);
128*4d6fc14bSjoerg
129*4d6fc14bSjoerg    template<class X> complex<long double>& operator= (const complex<X>&);
130*4d6fc14bSjoerg    template<class X> complex<long double>& operator+=(const complex<X>&);
131*4d6fc14bSjoerg    template<class X> complex<long double>& operator-=(const complex<X>&);
132*4d6fc14bSjoerg    template<class X> complex<long double>& operator*=(const complex<X>&);
133*4d6fc14bSjoerg    template<class X> complex<long double>& operator/=(const complex<X>&);
134*4d6fc14bSjoerg};
135*4d6fc14bSjoerg
136*4d6fc14bSjoerg// 26.3.6 operators:
137*4d6fc14bSjoergtemplate<class T> complex<T> operator+(const complex<T>&, const complex<T>&);
138*4d6fc14bSjoergtemplate<class T> complex<T> operator+(const complex<T>&, const T&);
139*4d6fc14bSjoergtemplate<class T> complex<T> operator+(const T&, const complex<T>&);
140*4d6fc14bSjoergtemplate<class T> complex<T> operator-(const complex<T>&, const complex<T>&);
141*4d6fc14bSjoergtemplate<class T> complex<T> operator-(const complex<T>&, const T&);
142*4d6fc14bSjoergtemplate<class T> complex<T> operator-(const T&, const complex<T>&);
143*4d6fc14bSjoergtemplate<class T> complex<T> operator*(const complex<T>&, const complex<T>&);
144*4d6fc14bSjoergtemplate<class T> complex<T> operator*(const complex<T>&, const T&);
145*4d6fc14bSjoergtemplate<class T> complex<T> operator*(const T&, const complex<T>&);
146*4d6fc14bSjoergtemplate<class T> complex<T> operator/(const complex<T>&, const complex<T>&);
147*4d6fc14bSjoergtemplate<class T> complex<T> operator/(const complex<T>&, const T&);
148*4d6fc14bSjoergtemplate<class T> complex<T> operator/(const T&, const complex<T>&);
149*4d6fc14bSjoergtemplate<class T> complex<T> operator+(const complex<T>&);
150*4d6fc14bSjoergtemplate<class T> complex<T> operator-(const complex<T>&);
151*4d6fc14bSjoergtemplate<class T> bool operator==(const complex<T>&, const complex<T>&); // constexpr in C++14
152*4d6fc14bSjoergtemplate<class T> bool operator==(const complex<T>&, const T&); // constexpr in C++14
153*4d6fc14bSjoergtemplate<class T> bool operator==(const T&, const complex<T>&); // constexpr in C++14
154*4d6fc14bSjoergtemplate<class T> bool operator!=(const complex<T>&, const complex<T>&); // constexpr in C++14
155*4d6fc14bSjoergtemplate<class T> bool operator!=(const complex<T>&, const T&); // constexpr in C++14
156*4d6fc14bSjoergtemplate<class T> bool operator!=(const T&, const complex<T>&); // constexpr in C++14
157*4d6fc14bSjoerg
158*4d6fc14bSjoergtemplate<class T, class charT, class traits>
159*4d6fc14bSjoerg  basic_istream<charT, traits>&
160*4d6fc14bSjoerg  operator>>(basic_istream<charT, traits>&, complex<T>&);
161*4d6fc14bSjoergtemplate<class T, class charT, class traits>
162*4d6fc14bSjoerg  basic_ostream<charT, traits>&
163*4d6fc14bSjoerg  operator<<(basic_ostream<charT, traits>&, const complex<T>&);
164*4d6fc14bSjoerg
165*4d6fc14bSjoerg// 26.3.7 values:
166*4d6fc14bSjoerg
167*4d6fc14bSjoergtemplate<class T>              T real(const complex<T>&); // constexpr in C++14
168*4d6fc14bSjoerg                     long double real(long double);       // constexpr in C++14
169*4d6fc14bSjoerg                          double real(double);            // constexpr in C++14
170*4d6fc14bSjoergtemplate<Integral T>      double real(T);                 // constexpr in C++14
171*4d6fc14bSjoerg                          float  real(float);             // constexpr in C++14
172*4d6fc14bSjoerg
173*4d6fc14bSjoergtemplate<class T>              T imag(const complex<T>&); // constexpr in C++14
174*4d6fc14bSjoerg                     long double imag(long double);       // constexpr in C++14
175*4d6fc14bSjoerg                          double imag(double);            // constexpr in C++14
176*4d6fc14bSjoergtemplate<Integral T>      double imag(T);                 // constexpr in C++14
177*4d6fc14bSjoerg                          float  imag(float);             // constexpr in C++14
178*4d6fc14bSjoerg
179*4d6fc14bSjoergtemplate<class T> T abs(const complex<T>&);
180*4d6fc14bSjoerg
181*4d6fc14bSjoergtemplate<class T>              T arg(const complex<T>&);
182*4d6fc14bSjoerg                     long double arg(long double);
183*4d6fc14bSjoerg                          double arg(double);
184*4d6fc14bSjoergtemplate<Integral T>      double arg(T);
185*4d6fc14bSjoerg                          float  arg(float);
186*4d6fc14bSjoerg
187*4d6fc14bSjoergtemplate<class T>              T norm(const complex<T>&);
188*4d6fc14bSjoerg                     long double norm(long double);
189*4d6fc14bSjoerg                          double norm(double);
190*4d6fc14bSjoergtemplate<Integral T>      double norm(T);
191*4d6fc14bSjoerg                          float  norm(float);
192*4d6fc14bSjoerg
193*4d6fc14bSjoergtemplate<class T>      complex<T>           conj(const complex<T>&);
194*4d6fc14bSjoerg                       complex<long double> conj(long double);
195*4d6fc14bSjoerg                       complex<double>      conj(double);
196*4d6fc14bSjoergtemplate<Integral T>   complex<double>      conj(T);
197*4d6fc14bSjoerg                       complex<float>       conj(float);
198*4d6fc14bSjoerg
199*4d6fc14bSjoergtemplate<class T>    complex<T>           proj(const complex<T>&);
200*4d6fc14bSjoerg                     complex<long double> proj(long double);
201*4d6fc14bSjoerg                     complex<double>      proj(double);
202*4d6fc14bSjoergtemplate<Integral T> complex<double>      proj(T);
203*4d6fc14bSjoerg                     complex<float>       proj(float);
204*4d6fc14bSjoerg
205*4d6fc14bSjoergtemplate<class T> complex<T> polar(const T&, const T& = T());
206*4d6fc14bSjoerg
207*4d6fc14bSjoerg// 26.3.8 transcendentals:
208*4d6fc14bSjoergtemplate<class T> complex<T> acos(const complex<T>&);
209*4d6fc14bSjoergtemplate<class T> complex<T> asin(const complex<T>&);
210*4d6fc14bSjoergtemplate<class T> complex<T> atan(const complex<T>&);
211*4d6fc14bSjoergtemplate<class T> complex<T> acosh(const complex<T>&);
212*4d6fc14bSjoergtemplate<class T> complex<T> asinh(const complex<T>&);
213*4d6fc14bSjoergtemplate<class T> complex<T> atanh(const complex<T>&);
214*4d6fc14bSjoergtemplate<class T> complex<T> cos (const complex<T>&);
215*4d6fc14bSjoergtemplate<class T> complex<T> cosh (const complex<T>&);
216*4d6fc14bSjoergtemplate<class T> complex<T> exp (const complex<T>&);
217*4d6fc14bSjoergtemplate<class T> complex<T> log (const complex<T>&);
218*4d6fc14bSjoergtemplate<class T> complex<T> log10(const complex<T>&);
219*4d6fc14bSjoerg
220*4d6fc14bSjoergtemplate<class T> complex<T> pow(const complex<T>&, const T&);
221*4d6fc14bSjoergtemplate<class T> complex<T> pow(const complex<T>&, const complex<T>&);
222*4d6fc14bSjoergtemplate<class T> complex<T> pow(const T&, const complex<T>&);
223*4d6fc14bSjoerg
224*4d6fc14bSjoergtemplate<class T> complex<T> sin (const complex<T>&);
225*4d6fc14bSjoergtemplate<class T> complex<T> sinh (const complex<T>&);
226*4d6fc14bSjoergtemplate<class T> complex<T> sqrt (const complex<T>&);
227*4d6fc14bSjoergtemplate<class T> complex<T> tan (const complex<T>&);
228*4d6fc14bSjoergtemplate<class T> complex<T> tanh (const complex<T>&);
229*4d6fc14bSjoerg
230*4d6fc14bSjoerg}  // std
231*4d6fc14bSjoerg
232*4d6fc14bSjoerg*/
233*4d6fc14bSjoerg
234*4d6fc14bSjoerg#include <__config>
235*4d6fc14bSjoerg#include <type_traits>
236*4d6fc14bSjoerg#include <stdexcept>
237*4d6fc14bSjoerg#include <cmath>
238*4d6fc14bSjoerg#include <iosfwd>
239*4d6fc14bSjoerg#include <version>
240*4d6fc14bSjoerg
241*4d6fc14bSjoerg#if !defined(_LIBCPP_HAS_NO_LOCALIZATION)
242*4d6fc14bSjoerg#   include <sstream> // for std::basic_ostringstream
243*4d6fc14bSjoerg#endif
244*4d6fc14bSjoerg
245*4d6fc14bSjoerg#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
246*4d6fc14bSjoerg#pragma GCC system_header
247*4d6fc14bSjoerg#endif
248*4d6fc14bSjoerg
249*4d6fc14bSjoerg_LIBCPP_BEGIN_NAMESPACE_STD
250*4d6fc14bSjoerg
251*4d6fc14bSjoergtemplate<class _Tp> class _LIBCPP_TEMPLATE_VIS complex;
252*4d6fc14bSjoerg
253*4d6fc14bSjoergtemplate<class _Tp> complex<_Tp> operator*(const complex<_Tp>& __z, const complex<_Tp>& __w);
254*4d6fc14bSjoergtemplate<class _Tp> complex<_Tp> operator/(const complex<_Tp>& __x, const complex<_Tp>& __y);
255*4d6fc14bSjoerg
256*4d6fc14bSjoergtemplate<class _Tp>
257*4d6fc14bSjoergclass _LIBCPP_TEMPLATE_VIS complex
258*4d6fc14bSjoerg{
259*4d6fc14bSjoergpublic:
260*4d6fc14bSjoerg    typedef _Tp value_type;
261*4d6fc14bSjoergprivate:
262*4d6fc14bSjoerg    value_type __re_;
263*4d6fc14bSjoerg    value_type __im_;
264*4d6fc14bSjoergpublic:
265*4d6fc14bSjoerg    _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
266*4d6fc14bSjoerg    complex(const value_type& __re = value_type(), const value_type& __im = value_type())
267*4d6fc14bSjoerg        : __re_(__re), __im_(__im) {}
268*4d6fc14bSjoerg    template<class _Xp> _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
269*4d6fc14bSjoerg    complex(const complex<_Xp>& __c)
270*4d6fc14bSjoerg        : __re_(__c.real()), __im_(__c.imag()) {}
271*4d6fc14bSjoerg
272*4d6fc14bSjoerg    _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11 value_type real() const {return __re_;}
273*4d6fc14bSjoerg    _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11 value_type imag() const {return __im_;}
274*4d6fc14bSjoerg
275*4d6fc14bSjoerg    _LIBCPP_INLINE_VISIBILITY void real(value_type __re) {__re_ = __re;}
276*4d6fc14bSjoerg    _LIBCPP_INLINE_VISIBILITY void imag(value_type __im) {__im_ = __im;}
277*4d6fc14bSjoerg
278*4d6fc14bSjoerg    _LIBCPP_INLINE_VISIBILITY complex& operator= (const value_type& __re)
279*4d6fc14bSjoerg        {__re_ = __re; __im_ = value_type(); return *this;}
280*4d6fc14bSjoerg    _LIBCPP_INLINE_VISIBILITY complex& operator+=(const value_type& __re) {__re_ += __re; return *this;}
281*4d6fc14bSjoerg    _LIBCPP_INLINE_VISIBILITY complex& operator-=(const value_type& __re) {__re_ -= __re; return *this;}
282*4d6fc14bSjoerg    _LIBCPP_INLINE_VISIBILITY complex& operator*=(const value_type& __re) {__re_ *= __re; __im_ *= __re; return *this;}
283*4d6fc14bSjoerg    _LIBCPP_INLINE_VISIBILITY complex& operator/=(const value_type& __re) {__re_ /= __re; __im_ /= __re; return *this;}
284*4d6fc14bSjoerg
285*4d6fc14bSjoerg    template<class _Xp> _LIBCPP_INLINE_VISIBILITY complex& operator= (const complex<_Xp>& __c)
286*4d6fc14bSjoerg        {
287*4d6fc14bSjoerg            __re_ = __c.real();
288*4d6fc14bSjoerg            __im_ = __c.imag();
289*4d6fc14bSjoerg            return *this;
290*4d6fc14bSjoerg        }
291*4d6fc14bSjoerg    template<class _Xp> _LIBCPP_INLINE_VISIBILITY complex& operator+=(const complex<_Xp>& __c)
292*4d6fc14bSjoerg        {
293*4d6fc14bSjoerg            __re_ += __c.real();
294*4d6fc14bSjoerg            __im_ += __c.imag();
295*4d6fc14bSjoerg            return *this;
296*4d6fc14bSjoerg        }
297*4d6fc14bSjoerg    template<class _Xp> _LIBCPP_INLINE_VISIBILITY complex& operator-=(const complex<_Xp>& __c)
298*4d6fc14bSjoerg        {
299*4d6fc14bSjoerg            __re_ -= __c.real();
300*4d6fc14bSjoerg            __im_ -= __c.imag();
301*4d6fc14bSjoerg            return *this;
302*4d6fc14bSjoerg        }
303*4d6fc14bSjoerg    template<class _Xp> _LIBCPP_INLINE_VISIBILITY complex& operator*=(const complex<_Xp>& __c)
304*4d6fc14bSjoerg        {
305*4d6fc14bSjoerg            *this = *this * complex(__c.real(), __c.imag());
306*4d6fc14bSjoerg            return *this;
307*4d6fc14bSjoerg        }
308*4d6fc14bSjoerg    template<class _Xp> _LIBCPP_INLINE_VISIBILITY complex& operator/=(const complex<_Xp>& __c)
309*4d6fc14bSjoerg        {
310*4d6fc14bSjoerg            *this = *this / complex(__c.real(), __c.imag());
311*4d6fc14bSjoerg            return *this;
312*4d6fc14bSjoerg        }
313*4d6fc14bSjoerg};
314*4d6fc14bSjoerg
315*4d6fc14bSjoergtemplate<> class _LIBCPP_TEMPLATE_VIS complex<double>;
316*4d6fc14bSjoergtemplate<> class _LIBCPP_TEMPLATE_VIS complex<long double>;
317*4d6fc14bSjoerg
318*4d6fc14bSjoergtemplate<>
319*4d6fc14bSjoergclass _LIBCPP_TEMPLATE_VIS complex<float>
320*4d6fc14bSjoerg{
321*4d6fc14bSjoerg    float __re_;
322*4d6fc14bSjoerg    float __im_;
323*4d6fc14bSjoergpublic:
324*4d6fc14bSjoerg    typedef float value_type;
325*4d6fc14bSjoerg
326*4d6fc14bSjoerg    _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR complex(float __re = 0.0f, float __im = 0.0f)
327*4d6fc14bSjoerg        : __re_(__re), __im_(__im) {}
328*4d6fc14bSjoerg    _LIBCPP_INLINE_VISIBILITY
329*4d6fc14bSjoerg    explicit _LIBCPP_CONSTEXPR complex(const complex<double>& __c);
330*4d6fc14bSjoerg    _LIBCPP_INLINE_VISIBILITY
331*4d6fc14bSjoerg    explicit _LIBCPP_CONSTEXPR complex(const complex<long double>& __c);
332*4d6fc14bSjoerg
333*4d6fc14bSjoerg    _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR float real() const {return __re_;}
334*4d6fc14bSjoerg    _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR float imag() const {return __im_;}
335*4d6fc14bSjoerg
336*4d6fc14bSjoerg    _LIBCPP_INLINE_VISIBILITY void real(value_type __re) {__re_ = __re;}
337*4d6fc14bSjoerg    _LIBCPP_INLINE_VISIBILITY void imag(value_type __im) {__im_ = __im;}
338*4d6fc14bSjoerg
339*4d6fc14bSjoerg    _LIBCPP_INLINE_VISIBILITY complex& operator= (float __re)
340*4d6fc14bSjoerg        {__re_ = __re; __im_ = value_type(); return *this;}
341*4d6fc14bSjoerg    _LIBCPP_INLINE_VISIBILITY complex& operator+=(float __re) {__re_ += __re; return *this;}
342*4d6fc14bSjoerg    _LIBCPP_INLINE_VISIBILITY complex& operator-=(float __re) {__re_ -= __re; return *this;}
343*4d6fc14bSjoerg    _LIBCPP_INLINE_VISIBILITY complex& operator*=(float __re) {__re_ *= __re; __im_ *= __re; return *this;}
344*4d6fc14bSjoerg    _LIBCPP_INLINE_VISIBILITY complex& operator/=(float __re) {__re_ /= __re; __im_ /= __re; return *this;}
345*4d6fc14bSjoerg
346*4d6fc14bSjoerg    template<class _Xp> _LIBCPP_INLINE_VISIBILITY complex& operator= (const complex<_Xp>& __c)
347*4d6fc14bSjoerg        {
348*4d6fc14bSjoerg            __re_ = __c.real();
349*4d6fc14bSjoerg            __im_ = __c.imag();
350*4d6fc14bSjoerg            return *this;
351*4d6fc14bSjoerg        }
352*4d6fc14bSjoerg    template<class _Xp> _LIBCPP_INLINE_VISIBILITY complex& operator+=(const complex<_Xp>& __c)
353*4d6fc14bSjoerg        {
354*4d6fc14bSjoerg            __re_ += __c.real();
355*4d6fc14bSjoerg            __im_ += __c.imag();
356*4d6fc14bSjoerg            return *this;
357*4d6fc14bSjoerg        }
358*4d6fc14bSjoerg    template<class _Xp> _LIBCPP_INLINE_VISIBILITY complex& operator-=(const complex<_Xp>& __c)
359*4d6fc14bSjoerg        {
360*4d6fc14bSjoerg            __re_ -= __c.real();
361*4d6fc14bSjoerg            __im_ -= __c.imag();
362*4d6fc14bSjoerg            return *this;
363*4d6fc14bSjoerg        }
364*4d6fc14bSjoerg    template<class _Xp> _LIBCPP_INLINE_VISIBILITY complex& operator*=(const complex<_Xp>& __c)
365*4d6fc14bSjoerg        {
366*4d6fc14bSjoerg            *this = *this * complex(__c.real(), __c.imag());
367*4d6fc14bSjoerg            return *this;
368*4d6fc14bSjoerg        }
369*4d6fc14bSjoerg    template<class _Xp> _LIBCPP_INLINE_VISIBILITY complex& operator/=(const complex<_Xp>& __c)
370*4d6fc14bSjoerg        {
371*4d6fc14bSjoerg            *this = *this / complex(__c.real(), __c.imag());
372*4d6fc14bSjoerg            return *this;
373*4d6fc14bSjoerg        }
374*4d6fc14bSjoerg};
375*4d6fc14bSjoerg
376*4d6fc14bSjoergtemplate<>
377*4d6fc14bSjoergclass _LIBCPP_TEMPLATE_VIS complex<double>
378*4d6fc14bSjoerg{
379*4d6fc14bSjoerg    double __re_;
380*4d6fc14bSjoerg    double __im_;
381*4d6fc14bSjoergpublic:
382*4d6fc14bSjoerg    typedef double value_type;
383*4d6fc14bSjoerg
384*4d6fc14bSjoerg    _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR complex(double __re = 0.0, double __im = 0.0)
385*4d6fc14bSjoerg        : __re_(__re), __im_(__im) {}
386*4d6fc14bSjoerg    _LIBCPP_INLINE_VISIBILITY
387*4d6fc14bSjoerg    _LIBCPP_CONSTEXPR complex(const complex<float>& __c);
388*4d6fc14bSjoerg    _LIBCPP_INLINE_VISIBILITY
389*4d6fc14bSjoerg    explicit _LIBCPP_CONSTEXPR complex(const complex<long double>& __c);
390*4d6fc14bSjoerg
391*4d6fc14bSjoerg    _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR double real() const {return __re_;}
392*4d6fc14bSjoerg    _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR double imag() const {return __im_;}
393*4d6fc14bSjoerg
394*4d6fc14bSjoerg    _LIBCPP_INLINE_VISIBILITY void real(value_type __re) {__re_ = __re;}
395*4d6fc14bSjoerg    _LIBCPP_INLINE_VISIBILITY void imag(value_type __im) {__im_ = __im;}
396*4d6fc14bSjoerg
397*4d6fc14bSjoerg    _LIBCPP_INLINE_VISIBILITY complex& operator= (double __re)
398*4d6fc14bSjoerg        {__re_ = __re; __im_ = value_type(); return *this;}
399*4d6fc14bSjoerg    _LIBCPP_INLINE_VISIBILITY complex& operator+=(double __re) {__re_ += __re; return *this;}
400*4d6fc14bSjoerg    _LIBCPP_INLINE_VISIBILITY complex& operator-=(double __re) {__re_ -= __re; return *this;}
401*4d6fc14bSjoerg    _LIBCPP_INLINE_VISIBILITY complex& operator*=(double __re) {__re_ *= __re; __im_ *= __re; return *this;}
402*4d6fc14bSjoerg    _LIBCPP_INLINE_VISIBILITY complex& operator/=(double __re) {__re_ /= __re; __im_ /= __re; return *this;}
403*4d6fc14bSjoerg
404*4d6fc14bSjoerg    template<class _Xp> _LIBCPP_INLINE_VISIBILITY complex& operator= (const complex<_Xp>& __c)
405*4d6fc14bSjoerg        {
406*4d6fc14bSjoerg            __re_ = __c.real();
407*4d6fc14bSjoerg            __im_ = __c.imag();
408*4d6fc14bSjoerg            return *this;
409*4d6fc14bSjoerg        }
410*4d6fc14bSjoerg    template<class _Xp> _LIBCPP_INLINE_VISIBILITY complex& operator+=(const complex<_Xp>& __c)
411*4d6fc14bSjoerg        {
412*4d6fc14bSjoerg            __re_ += __c.real();
413*4d6fc14bSjoerg            __im_ += __c.imag();
414*4d6fc14bSjoerg            return *this;
415*4d6fc14bSjoerg        }
416*4d6fc14bSjoerg    template<class _Xp> _LIBCPP_INLINE_VISIBILITY complex& operator-=(const complex<_Xp>& __c)
417*4d6fc14bSjoerg        {
418*4d6fc14bSjoerg            __re_ -= __c.real();
419*4d6fc14bSjoerg            __im_ -= __c.imag();
420*4d6fc14bSjoerg            return *this;
421*4d6fc14bSjoerg        }
422*4d6fc14bSjoerg    template<class _Xp> _LIBCPP_INLINE_VISIBILITY complex& operator*=(const complex<_Xp>& __c)
423*4d6fc14bSjoerg        {
424*4d6fc14bSjoerg            *this = *this * complex(__c.real(), __c.imag());
425*4d6fc14bSjoerg            return *this;
426*4d6fc14bSjoerg        }
427*4d6fc14bSjoerg    template<class _Xp> _LIBCPP_INLINE_VISIBILITY complex& operator/=(const complex<_Xp>& __c)
428*4d6fc14bSjoerg        {
429*4d6fc14bSjoerg            *this = *this / complex(__c.real(), __c.imag());
430*4d6fc14bSjoerg            return *this;
431*4d6fc14bSjoerg        }
432*4d6fc14bSjoerg};
433*4d6fc14bSjoerg
434*4d6fc14bSjoergtemplate<>
435*4d6fc14bSjoergclass _LIBCPP_TEMPLATE_VIS complex<long double>
436*4d6fc14bSjoerg{
437*4d6fc14bSjoerg    long double __re_;
438*4d6fc14bSjoerg    long double __im_;
439*4d6fc14bSjoergpublic:
440*4d6fc14bSjoerg    typedef long double value_type;
441*4d6fc14bSjoerg
442*4d6fc14bSjoerg    _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR complex(long double __re = 0.0L, long double __im = 0.0L)
443*4d6fc14bSjoerg        : __re_(__re), __im_(__im) {}
444*4d6fc14bSjoerg    _LIBCPP_INLINE_VISIBILITY
445*4d6fc14bSjoerg    _LIBCPP_CONSTEXPR complex(const complex<float>& __c);
446*4d6fc14bSjoerg    _LIBCPP_INLINE_VISIBILITY
447*4d6fc14bSjoerg    _LIBCPP_CONSTEXPR complex(const complex<double>& __c);
448*4d6fc14bSjoerg
449*4d6fc14bSjoerg    _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR long double real() const {return __re_;}
450*4d6fc14bSjoerg    _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR long double imag() const {return __im_;}
451*4d6fc14bSjoerg
452*4d6fc14bSjoerg    _LIBCPP_INLINE_VISIBILITY void real(value_type __re) {__re_ = __re;}
453*4d6fc14bSjoerg    _LIBCPP_INLINE_VISIBILITY void imag(value_type __im) {__im_ = __im;}
454*4d6fc14bSjoerg
455*4d6fc14bSjoerg    _LIBCPP_INLINE_VISIBILITY complex& operator= (long double __re)
456*4d6fc14bSjoerg        {__re_ = __re; __im_ = value_type(); return *this;}
457*4d6fc14bSjoerg    _LIBCPP_INLINE_VISIBILITY complex& operator+=(long double __re) {__re_ += __re; return *this;}
458*4d6fc14bSjoerg    _LIBCPP_INLINE_VISIBILITY complex& operator-=(long double __re) {__re_ -= __re; return *this;}
459*4d6fc14bSjoerg    _LIBCPP_INLINE_VISIBILITY complex& operator*=(long double __re) {__re_ *= __re; __im_ *= __re; return *this;}
460*4d6fc14bSjoerg    _LIBCPP_INLINE_VISIBILITY complex& operator/=(long double __re) {__re_ /= __re; __im_ /= __re; return *this;}
461*4d6fc14bSjoerg
462*4d6fc14bSjoerg    template<class _Xp> _LIBCPP_INLINE_VISIBILITY complex& operator= (const complex<_Xp>& __c)
463*4d6fc14bSjoerg        {
464*4d6fc14bSjoerg            __re_ = __c.real();
465*4d6fc14bSjoerg            __im_ = __c.imag();
466*4d6fc14bSjoerg            return *this;
467*4d6fc14bSjoerg        }
468*4d6fc14bSjoerg    template<class _Xp> _LIBCPP_INLINE_VISIBILITY complex& operator+=(const complex<_Xp>& __c)
469*4d6fc14bSjoerg        {
470*4d6fc14bSjoerg            __re_ += __c.real();
471*4d6fc14bSjoerg            __im_ += __c.imag();
472*4d6fc14bSjoerg            return *this;
473*4d6fc14bSjoerg        }
474*4d6fc14bSjoerg    template<class _Xp> _LIBCPP_INLINE_VISIBILITY complex& operator-=(const complex<_Xp>& __c)
475*4d6fc14bSjoerg        {
476*4d6fc14bSjoerg            __re_ -= __c.real();
477*4d6fc14bSjoerg            __im_ -= __c.imag();
478*4d6fc14bSjoerg            return *this;
479*4d6fc14bSjoerg        }
480*4d6fc14bSjoerg    template<class _Xp> _LIBCPP_INLINE_VISIBILITY complex& operator*=(const complex<_Xp>& __c)
481*4d6fc14bSjoerg        {
482*4d6fc14bSjoerg            *this = *this * complex(__c.real(), __c.imag());
483*4d6fc14bSjoerg            return *this;
484*4d6fc14bSjoerg        }
485*4d6fc14bSjoerg    template<class _Xp> _LIBCPP_INLINE_VISIBILITY complex& operator/=(const complex<_Xp>& __c)
486*4d6fc14bSjoerg        {
487*4d6fc14bSjoerg            *this = *this / complex(__c.real(), __c.imag());
488*4d6fc14bSjoerg            return *this;
489*4d6fc14bSjoerg        }
490*4d6fc14bSjoerg};
491*4d6fc14bSjoerg
492*4d6fc14bSjoerginline
493*4d6fc14bSjoerg_LIBCPP_CONSTEXPR
494*4d6fc14bSjoergcomplex<float>::complex(const complex<double>& __c)
495*4d6fc14bSjoerg    : __re_(__c.real()), __im_(__c.imag()) {}
496*4d6fc14bSjoerg
497*4d6fc14bSjoerginline
498*4d6fc14bSjoerg_LIBCPP_CONSTEXPR
499*4d6fc14bSjoergcomplex<float>::complex(const complex<long double>& __c)
500*4d6fc14bSjoerg    : __re_(__c.real()), __im_(__c.imag()) {}
501*4d6fc14bSjoerg
502*4d6fc14bSjoerginline
503*4d6fc14bSjoerg_LIBCPP_CONSTEXPR
504*4d6fc14bSjoergcomplex<double>::complex(const complex<float>& __c)
505*4d6fc14bSjoerg    : __re_(__c.real()), __im_(__c.imag()) {}
506*4d6fc14bSjoerg
507*4d6fc14bSjoerginline
508*4d6fc14bSjoerg_LIBCPP_CONSTEXPR
509*4d6fc14bSjoergcomplex<double>::complex(const complex<long double>& __c)
510*4d6fc14bSjoerg    : __re_(__c.real()), __im_(__c.imag()) {}
511*4d6fc14bSjoerg
512*4d6fc14bSjoerginline
513*4d6fc14bSjoerg_LIBCPP_CONSTEXPR
514*4d6fc14bSjoergcomplex<long double>::complex(const complex<float>& __c)
515*4d6fc14bSjoerg    : __re_(__c.real()), __im_(__c.imag()) {}
516*4d6fc14bSjoerg
517*4d6fc14bSjoerginline
518*4d6fc14bSjoerg_LIBCPP_CONSTEXPR
519*4d6fc14bSjoergcomplex<long double>::complex(const complex<double>& __c)
520*4d6fc14bSjoerg    : __re_(__c.real()), __im_(__c.imag()) {}
521*4d6fc14bSjoerg
522*4d6fc14bSjoerg// 26.3.6 operators:
523*4d6fc14bSjoerg
524*4d6fc14bSjoergtemplate<class _Tp>
525*4d6fc14bSjoerginline _LIBCPP_INLINE_VISIBILITY
526*4d6fc14bSjoergcomplex<_Tp>
527*4d6fc14bSjoergoperator+(const complex<_Tp>& __x, const complex<_Tp>& __y)
528*4d6fc14bSjoerg{
529*4d6fc14bSjoerg    complex<_Tp> __t(__x);
530*4d6fc14bSjoerg    __t += __y;
531*4d6fc14bSjoerg    return __t;
532*4d6fc14bSjoerg}
533*4d6fc14bSjoerg
534*4d6fc14bSjoergtemplate<class _Tp>
535*4d6fc14bSjoerginline _LIBCPP_INLINE_VISIBILITY
536*4d6fc14bSjoergcomplex<_Tp>
537*4d6fc14bSjoergoperator+(const complex<_Tp>& __x, const _Tp& __y)
538*4d6fc14bSjoerg{
539*4d6fc14bSjoerg    complex<_Tp> __t(__x);
540*4d6fc14bSjoerg    __t += __y;
541*4d6fc14bSjoerg    return __t;
542*4d6fc14bSjoerg}
543*4d6fc14bSjoerg
544*4d6fc14bSjoergtemplate<class _Tp>
545*4d6fc14bSjoerginline _LIBCPP_INLINE_VISIBILITY
546*4d6fc14bSjoergcomplex<_Tp>
547*4d6fc14bSjoergoperator+(const _Tp& __x, const complex<_Tp>& __y)
548*4d6fc14bSjoerg{
549*4d6fc14bSjoerg    complex<_Tp> __t(__y);
550*4d6fc14bSjoerg    __t += __x;
551*4d6fc14bSjoerg    return __t;
552*4d6fc14bSjoerg}
553*4d6fc14bSjoerg
554*4d6fc14bSjoergtemplate<class _Tp>
555*4d6fc14bSjoerginline _LIBCPP_INLINE_VISIBILITY
556*4d6fc14bSjoergcomplex<_Tp>
557*4d6fc14bSjoergoperator-(const complex<_Tp>& __x, const complex<_Tp>& __y)
558*4d6fc14bSjoerg{
559*4d6fc14bSjoerg    complex<_Tp> __t(__x);
560*4d6fc14bSjoerg    __t -= __y;
561*4d6fc14bSjoerg    return __t;
562*4d6fc14bSjoerg}
563*4d6fc14bSjoerg
564*4d6fc14bSjoergtemplate<class _Tp>
565*4d6fc14bSjoerginline _LIBCPP_INLINE_VISIBILITY
566*4d6fc14bSjoergcomplex<_Tp>
567*4d6fc14bSjoergoperator-(const complex<_Tp>& __x, const _Tp& __y)
568*4d6fc14bSjoerg{
569*4d6fc14bSjoerg    complex<_Tp> __t(__x);
570*4d6fc14bSjoerg    __t -= __y;
571*4d6fc14bSjoerg    return __t;
572*4d6fc14bSjoerg}
573*4d6fc14bSjoerg
574*4d6fc14bSjoergtemplate<class _Tp>
575*4d6fc14bSjoerginline _LIBCPP_INLINE_VISIBILITY
576*4d6fc14bSjoergcomplex<_Tp>
577*4d6fc14bSjoergoperator-(const _Tp& __x, const complex<_Tp>& __y)
578*4d6fc14bSjoerg{
579*4d6fc14bSjoerg    complex<_Tp> __t(-__y);
580*4d6fc14bSjoerg    __t += __x;
581*4d6fc14bSjoerg    return __t;
582*4d6fc14bSjoerg}
583*4d6fc14bSjoerg
584*4d6fc14bSjoergtemplate<class _Tp>
585*4d6fc14bSjoergcomplex<_Tp>
586*4d6fc14bSjoergoperator*(const complex<_Tp>& __z, const complex<_Tp>& __w)
587*4d6fc14bSjoerg{
588*4d6fc14bSjoerg    _Tp __a = __z.real();
589*4d6fc14bSjoerg    _Tp __b = __z.imag();
590*4d6fc14bSjoerg    _Tp __c = __w.real();
591*4d6fc14bSjoerg    _Tp __d = __w.imag();
592*4d6fc14bSjoerg    _Tp __ac = __a * __c;
593*4d6fc14bSjoerg    _Tp __bd = __b * __d;
594*4d6fc14bSjoerg    _Tp __ad = __a * __d;
595*4d6fc14bSjoerg    _Tp __bc = __b * __c;
596*4d6fc14bSjoerg    _Tp __x = __ac - __bd;
597*4d6fc14bSjoerg    _Tp __y = __ad + __bc;
598*4d6fc14bSjoerg    if (__libcpp_isnan_or_builtin(__x) && __libcpp_isnan_or_builtin(__y))
599*4d6fc14bSjoerg    {
600*4d6fc14bSjoerg        bool __recalc = false;
601*4d6fc14bSjoerg        if (__libcpp_isinf_or_builtin(__a) || __libcpp_isinf_or_builtin(__b))
602*4d6fc14bSjoerg        {
603*4d6fc14bSjoerg            __a = copysign(__libcpp_isinf_or_builtin(__a) ? _Tp(1) : _Tp(0), __a);
604*4d6fc14bSjoerg            __b = copysign(__libcpp_isinf_or_builtin(__b) ? _Tp(1) : _Tp(0), __b);
605*4d6fc14bSjoerg            if (__libcpp_isnan_or_builtin(__c))
606*4d6fc14bSjoerg                __c = copysign(_Tp(0), __c);
607*4d6fc14bSjoerg            if (__libcpp_isnan_or_builtin(__d))
608*4d6fc14bSjoerg                __d = copysign(_Tp(0), __d);
609*4d6fc14bSjoerg            __recalc = true;
610*4d6fc14bSjoerg        }
611*4d6fc14bSjoerg        if (__libcpp_isinf_or_builtin(__c) || __libcpp_isinf_or_builtin(__d))
612*4d6fc14bSjoerg        {
613*4d6fc14bSjoerg            __c = copysign(__libcpp_isinf_or_builtin(__c) ? _Tp(1) : _Tp(0), __c);
614*4d6fc14bSjoerg            __d = copysign(__libcpp_isinf_or_builtin(__d) ? _Tp(1) : _Tp(0), __d);
615*4d6fc14bSjoerg            if (__libcpp_isnan_or_builtin(__a))
616*4d6fc14bSjoerg                __a = copysign(_Tp(0), __a);
617*4d6fc14bSjoerg            if (__libcpp_isnan_or_builtin(__b))
618*4d6fc14bSjoerg                __b = copysign(_Tp(0), __b);
619*4d6fc14bSjoerg            __recalc = true;
620*4d6fc14bSjoerg        }
621*4d6fc14bSjoerg        if (!__recalc && (__libcpp_isinf_or_builtin(__ac) || __libcpp_isinf_or_builtin(__bd) ||
622*4d6fc14bSjoerg                          __libcpp_isinf_or_builtin(__ad) || __libcpp_isinf_or_builtin(__bc)))
623*4d6fc14bSjoerg        {
624*4d6fc14bSjoerg            if (__libcpp_isnan_or_builtin(__a))
625*4d6fc14bSjoerg                __a = copysign(_Tp(0), __a);
626*4d6fc14bSjoerg            if (__libcpp_isnan_or_builtin(__b))
627*4d6fc14bSjoerg                __b = copysign(_Tp(0), __b);
628*4d6fc14bSjoerg            if (__libcpp_isnan_or_builtin(__c))
629*4d6fc14bSjoerg                __c = copysign(_Tp(0), __c);
630*4d6fc14bSjoerg            if (__libcpp_isnan_or_builtin(__d))
631*4d6fc14bSjoerg                __d = copysign(_Tp(0), __d);
632*4d6fc14bSjoerg            __recalc = true;
633*4d6fc14bSjoerg        }
634*4d6fc14bSjoerg        if (__recalc)
635*4d6fc14bSjoerg        {
636*4d6fc14bSjoerg            __x = _Tp(INFINITY) * (__a * __c - __b * __d);
637*4d6fc14bSjoerg            __y = _Tp(INFINITY) * (__a * __d + __b * __c);
638*4d6fc14bSjoerg        }
639*4d6fc14bSjoerg    }
640*4d6fc14bSjoerg    return complex<_Tp>(__x, __y);
641*4d6fc14bSjoerg}
642*4d6fc14bSjoerg
643*4d6fc14bSjoergtemplate<class _Tp>
644*4d6fc14bSjoerginline _LIBCPP_INLINE_VISIBILITY
645*4d6fc14bSjoergcomplex<_Tp>
646*4d6fc14bSjoergoperator*(const complex<_Tp>& __x, const _Tp& __y)
647*4d6fc14bSjoerg{
648*4d6fc14bSjoerg    complex<_Tp> __t(__x);
649*4d6fc14bSjoerg    __t *= __y;
650*4d6fc14bSjoerg    return __t;
651*4d6fc14bSjoerg}
652*4d6fc14bSjoerg
653*4d6fc14bSjoergtemplate<class _Tp>
654*4d6fc14bSjoerginline _LIBCPP_INLINE_VISIBILITY
655*4d6fc14bSjoergcomplex<_Tp>
656*4d6fc14bSjoergoperator*(const _Tp& __x, const complex<_Tp>& __y)
657*4d6fc14bSjoerg{
658*4d6fc14bSjoerg    complex<_Tp> __t(__y);
659*4d6fc14bSjoerg    __t *= __x;
660*4d6fc14bSjoerg    return __t;
661*4d6fc14bSjoerg}
662*4d6fc14bSjoerg
663*4d6fc14bSjoergtemplate<class _Tp>
664*4d6fc14bSjoergcomplex<_Tp>
665*4d6fc14bSjoergoperator/(const complex<_Tp>& __z, const complex<_Tp>& __w)
666*4d6fc14bSjoerg{
667*4d6fc14bSjoerg    int __ilogbw = 0;
668*4d6fc14bSjoerg    _Tp __a = __z.real();
669*4d6fc14bSjoerg    _Tp __b = __z.imag();
670*4d6fc14bSjoerg    _Tp __c = __w.real();
671*4d6fc14bSjoerg    _Tp __d = __w.imag();
672*4d6fc14bSjoerg    _Tp __logbw = logb(fmax(fabs(__c), fabs(__d)));
673*4d6fc14bSjoerg    if (__libcpp_isfinite_or_builtin(__logbw))
674*4d6fc14bSjoerg    {
675*4d6fc14bSjoerg        __ilogbw = static_cast<int>(__logbw);
676*4d6fc14bSjoerg        __c = scalbn(__c, -__ilogbw);
677*4d6fc14bSjoerg        __d = scalbn(__d, -__ilogbw);
678*4d6fc14bSjoerg    }
679*4d6fc14bSjoerg    _Tp __denom = __c * __c + __d * __d;
680*4d6fc14bSjoerg    _Tp __x = scalbn((__a * __c + __b * __d) / __denom, -__ilogbw);
681*4d6fc14bSjoerg    _Tp __y = scalbn((__b * __c - __a * __d) / __denom, -__ilogbw);
682*4d6fc14bSjoerg    if (__libcpp_isnan_or_builtin(__x) && __libcpp_isnan_or_builtin(__y))
683*4d6fc14bSjoerg    {
684*4d6fc14bSjoerg        if ((__denom == _Tp(0)) && (!__libcpp_isnan_or_builtin(__a) || !__libcpp_isnan_or_builtin(__b)))
685*4d6fc14bSjoerg        {
686*4d6fc14bSjoerg            __x = copysign(_Tp(INFINITY), __c) * __a;
687*4d6fc14bSjoerg            __y = copysign(_Tp(INFINITY), __c) * __b;
688*4d6fc14bSjoerg        }
689*4d6fc14bSjoerg        else if ((__libcpp_isinf_or_builtin(__a) || __libcpp_isinf_or_builtin(__b)) && __libcpp_isfinite_or_builtin(__c) && __libcpp_isfinite_or_builtin(__d))
690*4d6fc14bSjoerg        {
691*4d6fc14bSjoerg            __a = copysign(__libcpp_isinf_or_builtin(__a) ? _Tp(1) : _Tp(0), __a);
692*4d6fc14bSjoerg            __b = copysign(__libcpp_isinf_or_builtin(__b) ? _Tp(1) : _Tp(0), __b);
693*4d6fc14bSjoerg            __x = _Tp(INFINITY) * (__a * __c + __b * __d);
694*4d6fc14bSjoerg            __y = _Tp(INFINITY) * (__b * __c - __a * __d);
695*4d6fc14bSjoerg        }
696*4d6fc14bSjoerg        else if (__libcpp_isinf_or_builtin(__logbw) && __logbw > _Tp(0) && __libcpp_isfinite_or_builtin(__a) && __libcpp_isfinite_or_builtin(__b))
697*4d6fc14bSjoerg        {
698*4d6fc14bSjoerg            __c = copysign(__libcpp_isinf_or_builtin(__c) ? _Tp(1) : _Tp(0), __c);
699*4d6fc14bSjoerg            __d = copysign(__libcpp_isinf_or_builtin(__d) ? _Tp(1) : _Tp(0), __d);
700*4d6fc14bSjoerg            __x = _Tp(0) * (__a * __c + __b * __d);
701*4d6fc14bSjoerg            __y = _Tp(0) * (__b * __c - __a * __d);
702*4d6fc14bSjoerg        }
703*4d6fc14bSjoerg    }
704*4d6fc14bSjoerg    return complex<_Tp>(__x, __y);
705*4d6fc14bSjoerg}
706*4d6fc14bSjoerg
707*4d6fc14bSjoergtemplate<class _Tp>
708*4d6fc14bSjoerginline _LIBCPP_INLINE_VISIBILITY
709*4d6fc14bSjoergcomplex<_Tp>
710*4d6fc14bSjoergoperator/(const complex<_Tp>& __x, const _Tp& __y)
711*4d6fc14bSjoerg{
712*4d6fc14bSjoerg    return complex<_Tp>(__x.real() / __y, __x.imag() / __y);
713*4d6fc14bSjoerg}
714*4d6fc14bSjoerg
715*4d6fc14bSjoergtemplate<class _Tp>
716*4d6fc14bSjoerginline _LIBCPP_INLINE_VISIBILITY
717*4d6fc14bSjoergcomplex<_Tp>
718*4d6fc14bSjoergoperator/(const _Tp& __x, const complex<_Tp>& __y)
719*4d6fc14bSjoerg{
720*4d6fc14bSjoerg    complex<_Tp> __t(__x);
721*4d6fc14bSjoerg    __t /= __y;
722*4d6fc14bSjoerg    return __t;
723*4d6fc14bSjoerg}
724*4d6fc14bSjoerg
725*4d6fc14bSjoergtemplate<class _Tp>
726*4d6fc14bSjoerginline _LIBCPP_INLINE_VISIBILITY
727*4d6fc14bSjoergcomplex<_Tp>
728*4d6fc14bSjoergoperator+(const complex<_Tp>& __x)
729*4d6fc14bSjoerg{
730*4d6fc14bSjoerg    return __x;
731*4d6fc14bSjoerg}
732*4d6fc14bSjoerg
733*4d6fc14bSjoergtemplate<class _Tp>
734*4d6fc14bSjoerginline _LIBCPP_INLINE_VISIBILITY
735*4d6fc14bSjoergcomplex<_Tp>
736*4d6fc14bSjoergoperator-(const complex<_Tp>& __x)
737*4d6fc14bSjoerg{
738*4d6fc14bSjoerg    return complex<_Tp>(-__x.real(), -__x.imag());
739*4d6fc14bSjoerg}
740*4d6fc14bSjoerg
741*4d6fc14bSjoergtemplate<class _Tp>
742*4d6fc14bSjoerginline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
743*4d6fc14bSjoergbool
744*4d6fc14bSjoergoperator==(const complex<_Tp>& __x, const complex<_Tp>& __y)
745*4d6fc14bSjoerg{
746*4d6fc14bSjoerg    return __x.real() == __y.real() && __x.imag() == __y.imag();
747*4d6fc14bSjoerg}
748*4d6fc14bSjoerg
749*4d6fc14bSjoergtemplate<class _Tp>
750*4d6fc14bSjoerginline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
751*4d6fc14bSjoergbool
752*4d6fc14bSjoergoperator==(const complex<_Tp>& __x, const _Tp& __y)
753*4d6fc14bSjoerg{
754*4d6fc14bSjoerg    return __x.real() == __y && __x.imag() == 0;
755*4d6fc14bSjoerg}
756*4d6fc14bSjoerg
757*4d6fc14bSjoergtemplate<class _Tp>
758*4d6fc14bSjoerginline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
759*4d6fc14bSjoergbool
760*4d6fc14bSjoergoperator==(const _Tp& __x, const complex<_Tp>& __y)
761*4d6fc14bSjoerg{
762*4d6fc14bSjoerg    return __x == __y.real() && 0 == __y.imag();
763*4d6fc14bSjoerg}
764*4d6fc14bSjoerg
765*4d6fc14bSjoergtemplate<class _Tp>
766*4d6fc14bSjoerginline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
767*4d6fc14bSjoergbool
768*4d6fc14bSjoergoperator!=(const complex<_Tp>& __x, const complex<_Tp>& __y)
769*4d6fc14bSjoerg{
770*4d6fc14bSjoerg    return !(__x == __y);
771*4d6fc14bSjoerg}
772*4d6fc14bSjoerg
773*4d6fc14bSjoergtemplate<class _Tp>
774*4d6fc14bSjoerginline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
775*4d6fc14bSjoergbool
776*4d6fc14bSjoergoperator!=(const complex<_Tp>& __x, const _Tp& __y)
777*4d6fc14bSjoerg{
778*4d6fc14bSjoerg    return !(__x == __y);
779*4d6fc14bSjoerg}
780*4d6fc14bSjoerg
781*4d6fc14bSjoergtemplate<class _Tp>
782*4d6fc14bSjoerginline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
783*4d6fc14bSjoergbool
784*4d6fc14bSjoergoperator!=(const _Tp& __x, const complex<_Tp>& __y)
785*4d6fc14bSjoerg{
786*4d6fc14bSjoerg    return !(__x == __y);
787*4d6fc14bSjoerg}
788*4d6fc14bSjoerg
789*4d6fc14bSjoerg// 26.3.7 values:
790*4d6fc14bSjoerg
791*4d6fc14bSjoergtemplate <class _Tp, bool = is_integral<_Tp>::value,
792*4d6fc14bSjoerg                     bool = is_floating_point<_Tp>::value
793*4d6fc14bSjoerg                     >
794*4d6fc14bSjoergstruct __libcpp_complex_overload_traits {};
795*4d6fc14bSjoerg
796*4d6fc14bSjoerg// Integral Types
797*4d6fc14bSjoergtemplate <class _Tp>
798*4d6fc14bSjoergstruct __libcpp_complex_overload_traits<_Tp, true, false>
799*4d6fc14bSjoerg{
800*4d6fc14bSjoerg    typedef double _ValueType;
801*4d6fc14bSjoerg    typedef complex<double> _ComplexType;
802*4d6fc14bSjoerg};
803*4d6fc14bSjoerg
804*4d6fc14bSjoerg// Floating point types
805*4d6fc14bSjoergtemplate <class _Tp>
806*4d6fc14bSjoergstruct __libcpp_complex_overload_traits<_Tp, false, true>
807*4d6fc14bSjoerg{
808*4d6fc14bSjoerg    typedef _Tp _ValueType;
809*4d6fc14bSjoerg    typedef complex<_Tp> _ComplexType;
810*4d6fc14bSjoerg};
811*4d6fc14bSjoerg
812*4d6fc14bSjoerg// real
813*4d6fc14bSjoerg
814*4d6fc14bSjoergtemplate<class _Tp>
815*4d6fc14bSjoerginline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
816*4d6fc14bSjoerg_Tp
817*4d6fc14bSjoergreal(const complex<_Tp>& __c)
818*4d6fc14bSjoerg{
819*4d6fc14bSjoerg    return __c.real();
820*4d6fc14bSjoerg}
821*4d6fc14bSjoerg
822*4d6fc14bSjoergtemplate <class _Tp>
823*4d6fc14bSjoerginline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
824*4d6fc14bSjoergtypename __libcpp_complex_overload_traits<_Tp>::_ValueType
825*4d6fc14bSjoergreal(_Tp __re)
826*4d6fc14bSjoerg{
827*4d6fc14bSjoerg    return __re;
828*4d6fc14bSjoerg}
829*4d6fc14bSjoerg
830*4d6fc14bSjoerg// imag
831*4d6fc14bSjoerg
832*4d6fc14bSjoergtemplate<class _Tp>
833*4d6fc14bSjoerginline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
834*4d6fc14bSjoerg_Tp
835*4d6fc14bSjoergimag(const complex<_Tp>& __c)
836*4d6fc14bSjoerg{
837*4d6fc14bSjoerg    return __c.imag();
838*4d6fc14bSjoerg}
839*4d6fc14bSjoerg
840*4d6fc14bSjoergtemplate <class _Tp>
841*4d6fc14bSjoerginline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
842*4d6fc14bSjoergtypename __libcpp_complex_overload_traits<_Tp>::_ValueType
843*4d6fc14bSjoergimag(_Tp)
844*4d6fc14bSjoerg{
845*4d6fc14bSjoerg    return 0;
846*4d6fc14bSjoerg}
847*4d6fc14bSjoerg
848*4d6fc14bSjoerg// abs
849*4d6fc14bSjoerg
850*4d6fc14bSjoergtemplate<class _Tp>
851*4d6fc14bSjoerginline _LIBCPP_INLINE_VISIBILITY
852*4d6fc14bSjoerg_Tp
853*4d6fc14bSjoergabs(const complex<_Tp>& __c)
854*4d6fc14bSjoerg{
855*4d6fc14bSjoerg    return hypot(__c.real(), __c.imag());
856*4d6fc14bSjoerg}
857*4d6fc14bSjoerg
858*4d6fc14bSjoerg// arg
859*4d6fc14bSjoerg
860*4d6fc14bSjoergtemplate<class _Tp>
861*4d6fc14bSjoerginline _LIBCPP_INLINE_VISIBILITY
862*4d6fc14bSjoerg_Tp
863*4d6fc14bSjoergarg(const complex<_Tp>& __c)
864*4d6fc14bSjoerg{
865*4d6fc14bSjoerg    return atan2(__c.imag(), __c.real());
866*4d6fc14bSjoerg}
867*4d6fc14bSjoerg
868*4d6fc14bSjoergtemplate <class _Tp>
869*4d6fc14bSjoerginline _LIBCPP_INLINE_VISIBILITY
870*4d6fc14bSjoergtypename enable_if<
871*4d6fc14bSjoerg    is_same<_Tp, long double>::value,
872*4d6fc14bSjoerg    long double
873*4d6fc14bSjoerg>::type
874*4d6fc14bSjoergarg(_Tp __re)
875*4d6fc14bSjoerg{
876*4d6fc14bSjoerg    return atan2l(0.L, __re);
877*4d6fc14bSjoerg}
878*4d6fc14bSjoerg
879*4d6fc14bSjoergtemplate<class _Tp>
880*4d6fc14bSjoerginline _LIBCPP_INLINE_VISIBILITY
881*4d6fc14bSjoergtypename enable_if
882*4d6fc14bSjoerg<
883*4d6fc14bSjoerg    is_integral<_Tp>::value || is_same<_Tp, double>::value,
884*4d6fc14bSjoerg    double
885*4d6fc14bSjoerg>::type
886*4d6fc14bSjoergarg(_Tp __re)
887*4d6fc14bSjoerg{
888*4d6fc14bSjoerg    return atan2(0., __re);
889*4d6fc14bSjoerg}
890*4d6fc14bSjoerg
891*4d6fc14bSjoergtemplate <class _Tp>
892*4d6fc14bSjoerginline _LIBCPP_INLINE_VISIBILITY
893*4d6fc14bSjoergtypename enable_if<
894*4d6fc14bSjoerg    is_same<_Tp, float>::value,
895*4d6fc14bSjoerg    float
896*4d6fc14bSjoerg>::type
897*4d6fc14bSjoergarg(_Tp __re)
898*4d6fc14bSjoerg{
899*4d6fc14bSjoerg    return atan2f(0.F, __re);
900*4d6fc14bSjoerg}
901*4d6fc14bSjoerg
902*4d6fc14bSjoerg// norm
903*4d6fc14bSjoerg
904*4d6fc14bSjoergtemplate<class _Tp>
905*4d6fc14bSjoerginline _LIBCPP_INLINE_VISIBILITY
906*4d6fc14bSjoerg_Tp
907*4d6fc14bSjoergnorm(const complex<_Tp>& __c)
908*4d6fc14bSjoerg{
909*4d6fc14bSjoerg    if (__libcpp_isinf_or_builtin(__c.real()))
910*4d6fc14bSjoerg        return abs(__c.real());
911*4d6fc14bSjoerg    if (__libcpp_isinf_or_builtin(__c.imag()))
912*4d6fc14bSjoerg        return abs(__c.imag());
913*4d6fc14bSjoerg    return __c.real() * __c.real() + __c.imag() * __c.imag();
914*4d6fc14bSjoerg}
915*4d6fc14bSjoerg
916*4d6fc14bSjoergtemplate <class _Tp>
917*4d6fc14bSjoerginline _LIBCPP_INLINE_VISIBILITY
918*4d6fc14bSjoergtypename __libcpp_complex_overload_traits<_Tp>::_ValueType
919*4d6fc14bSjoergnorm(_Tp __re)
920*4d6fc14bSjoerg{
921*4d6fc14bSjoerg    typedef typename __libcpp_complex_overload_traits<_Tp>::_ValueType _ValueType;
922*4d6fc14bSjoerg    return static_cast<_ValueType>(__re) * __re;
923*4d6fc14bSjoerg}
924*4d6fc14bSjoerg
925*4d6fc14bSjoerg// conj
926*4d6fc14bSjoerg
927*4d6fc14bSjoergtemplate<class _Tp>
928*4d6fc14bSjoerginline _LIBCPP_INLINE_VISIBILITY
929*4d6fc14bSjoergcomplex<_Tp>
930*4d6fc14bSjoergconj(const complex<_Tp>& __c)
931*4d6fc14bSjoerg{
932*4d6fc14bSjoerg    return complex<_Tp>(__c.real(), -__c.imag());
933*4d6fc14bSjoerg}
934*4d6fc14bSjoerg
935*4d6fc14bSjoergtemplate <class _Tp>
936*4d6fc14bSjoerginline _LIBCPP_INLINE_VISIBILITY
937*4d6fc14bSjoergtypename __libcpp_complex_overload_traits<_Tp>::_ComplexType
938*4d6fc14bSjoergconj(_Tp __re)
939*4d6fc14bSjoerg{
940*4d6fc14bSjoerg    typedef typename __libcpp_complex_overload_traits<_Tp>::_ComplexType _ComplexType;
941*4d6fc14bSjoerg    return _ComplexType(__re);
942*4d6fc14bSjoerg}
943*4d6fc14bSjoerg
944*4d6fc14bSjoerg
945*4d6fc14bSjoerg
946*4d6fc14bSjoerg// proj
947*4d6fc14bSjoerg
948*4d6fc14bSjoergtemplate<class _Tp>
949*4d6fc14bSjoerginline _LIBCPP_INLINE_VISIBILITY
950*4d6fc14bSjoergcomplex<_Tp>
951*4d6fc14bSjoergproj(const complex<_Tp>& __c)
952*4d6fc14bSjoerg{
953*4d6fc14bSjoerg    complex<_Tp> __r = __c;
954*4d6fc14bSjoerg    if (__libcpp_isinf_or_builtin(__c.real()) || __libcpp_isinf_or_builtin(__c.imag()))
955*4d6fc14bSjoerg        __r = complex<_Tp>(INFINITY, copysign(_Tp(0), __c.imag()));
956*4d6fc14bSjoerg    return __r;
957*4d6fc14bSjoerg}
958*4d6fc14bSjoerg
959*4d6fc14bSjoergtemplate <class _Tp>
960*4d6fc14bSjoerginline _LIBCPP_INLINE_VISIBILITY
961*4d6fc14bSjoergtypename enable_if
962*4d6fc14bSjoerg<
963*4d6fc14bSjoerg    is_floating_point<_Tp>::value,
964*4d6fc14bSjoerg    typename __libcpp_complex_overload_traits<_Tp>::_ComplexType
965*4d6fc14bSjoerg>::type
966*4d6fc14bSjoergproj(_Tp __re)
967*4d6fc14bSjoerg{
968*4d6fc14bSjoerg    if (__libcpp_isinf_or_builtin(__re))
969*4d6fc14bSjoerg        __re = abs(__re);
970*4d6fc14bSjoerg    return complex<_Tp>(__re);
971*4d6fc14bSjoerg}
972*4d6fc14bSjoerg
973*4d6fc14bSjoergtemplate <class _Tp>
974*4d6fc14bSjoerginline _LIBCPP_INLINE_VISIBILITY
975*4d6fc14bSjoergtypename enable_if
976*4d6fc14bSjoerg<
977*4d6fc14bSjoerg    is_integral<_Tp>::value,
978*4d6fc14bSjoerg    typename __libcpp_complex_overload_traits<_Tp>::_ComplexType
979*4d6fc14bSjoerg>::type
980*4d6fc14bSjoergproj(_Tp __re)
981*4d6fc14bSjoerg{
982*4d6fc14bSjoerg    typedef typename __libcpp_complex_overload_traits<_Tp>::_ComplexType _ComplexType;
983*4d6fc14bSjoerg    return _ComplexType(__re);
984*4d6fc14bSjoerg}
985*4d6fc14bSjoerg
986*4d6fc14bSjoerg// polar
987*4d6fc14bSjoerg
988*4d6fc14bSjoergtemplate<class _Tp>
989*4d6fc14bSjoergcomplex<_Tp>
990*4d6fc14bSjoergpolar(const _Tp& __rho, const _Tp& __theta = _Tp())
991*4d6fc14bSjoerg{
992*4d6fc14bSjoerg    if (__libcpp_isnan_or_builtin(__rho) || signbit(__rho))
993*4d6fc14bSjoerg        return complex<_Tp>(_Tp(NAN), _Tp(NAN));
994*4d6fc14bSjoerg    if (__libcpp_isnan_or_builtin(__theta))
995*4d6fc14bSjoerg    {
996*4d6fc14bSjoerg        if (__libcpp_isinf_or_builtin(__rho))
997*4d6fc14bSjoerg            return complex<_Tp>(__rho, __theta);
998*4d6fc14bSjoerg        return complex<_Tp>(__theta, __theta);
999*4d6fc14bSjoerg    }
1000*4d6fc14bSjoerg    if (__libcpp_isinf_or_builtin(__theta))
1001*4d6fc14bSjoerg    {
1002*4d6fc14bSjoerg        if (__libcpp_isinf_or_builtin(__rho))
1003*4d6fc14bSjoerg            return complex<_Tp>(__rho, _Tp(NAN));
1004*4d6fc14bSjoerg        return complex<_Tp>(_Tp(NAN), _Tp(NAN));
1005*4d6fc14bSjoerg    }
1006*4d6fc14bSjoerg    _Tp __x = __rho * cos(__theta);
1007*4d6fc14bSjoerg    if (__libcpp_isnan_or_builtin(__x))
1008*4d6fc14bSjoerg        __x = 0;
1009*4d6fc14bSjoerg    _Tp __y = __rho * sin(__theta);
1010*4d6fc14bSjoerg    if (__libcpp_isnan_or_builtin(__y))
1011*4d6fc14bSjoerg        __y = 0;
1012*4d6fc14bSjoerg    return complex<_Tp>(__x, __y);
1013*4d6fc14bSjoerg}
1014*4d6fc14bSjoerg
1015*4d6fc14bSjoerg// log
1016*4d6fc14bSjoerg
1017*4d6fc14bSjoergtemplate<class _Tp>
1018*4d6fc14bSjoerginline _LIBCPP_INLINE_VISIBILITY
1019*4d6fc14bSjoergcomplex<_Tp>
1020*4d6fc14bSjoerglog(const complex<_Tp>& __x)
1021*4d6fc14bSjoerg{
1022*4d6fc14bSjoerg    return complex<_Tp>(log(abs(__x)), arg(__x));
1023*4d6fc14bSjoerg}
1024*4d6fc14bSjoerg
1025*4d6fc14bSjoerg// log10
1026*4d6fc14bSjoerg
1027*4d6fc14bSjoergtemplate<class _Tp>
1028*4d6fc14bSjoerginline _LIBCPP_INLINE_VISIBILITY
1029*4d6fc14bSjoergcomplex<_Tp>
1030*4d6fc14bSjoerglog10(const complex<_Tp>& __x)
1031*4d6fc14bSjoerg{
1032*4d6fc14bSjoerg    return log(__x) / log(_Tp(10));
1033*4d6fc14bSjoerg}
1034*4d6fc14bSjoerg
1035*4d6fc14bSjoerg// sqrt
1036*4d6fc14bSjoerg
1037*4d6fc14bSjoergtemplate<class _Tp>
1038*4d6fc14bSjoergcomplex<_Tp>
1039*4d6fc14bSjoergsqrt(const complex<_Tp>& __x)
1040*4d6fc14bSjoerg{
1041*4d6fc14bSjoerg    if (__libcpp_isinf_or_builtin(__x.imag()))
1042*4d6fc14bSjoerg        return complex<_Tp>(_Tp(INFINITY), __x.imag());
1043*4d6fc14bSjoerg    if (__libcpp_isinf_or_builtin(__x.real()))
1044*4d6fc14bSjoerg    {
1045*4d6fc14bSjoerg        if (__x.real() > _Tp(0))
1046*4d6fc14bSjoerg            return complex<_Tp>(__x.real(), __libcpp_isnan_or_builtin(__x.imag()) ? __x.imag() : copysign(_Tp(0), __x.imag()));
1047*4d6fc14bSjoerg        return complex<_Tp>(__libcpp_isnan_or_builtin(__x.imag()) ? __x.imag() : _Tp(0), copysign(__x.real(), __x.imag()));
1048*4d6fc14bSjoerg    }
1049*4d6fc14bSjoerg    return polar(sqrt(abs(__x)), arg(__x) / _Tp(2));
1050*4d6fc14bSjoerg}
1051*4d6fc14bSjoerg
1052*4d6fc14bSjoerg// exp
1053*4d6fc14bSjoerg
1054*4d6fc14bSjoergtemplate<class _Tp>
1055*4d6fc14bSjoergcomplex<_Tp>
1056*4d6fc14bSjoergexp(const complex<_Tp>& __x)
1057*4d6fc14bSjoerg{
1058*4d6fc14bSjoerg    _Tp __i = __x.imag();
1059*4d6fc14bSjoerg    if (__libcpp_isinf_or_builtin(__x.real()))
1060*4d6fc14bSjoerg    {
1061*4d6fc14bSjoerg        if (__x.real() < _Tp(0))
1062*4d6fc14bSjoerg        {
1063*4d6fc14bSjoerg            if (!__libcpp_isfinite_or_builtin(__i))
1064*4d6fc14bSjoerg                __i = _Tp(1);
1065*4d6fc14bSjoerg        }
1066*4d6fc14bSjoerg        else if (__i == 0 || !__libcpp_isfinite_or_builtin(__i))
1067*4d6fc14bSjoerg        {
1068*4d6fc14bSjoerg            if (__libcpp_isinf_or_builtin(__i))
1069*4d6fc14bSjoerg                __i = _Tp(NAN);
1070*4d6fc14bSjoerg            return complex<_Tp>(__x.real(), __i);
1071*4d6fc14bSjoerg        }
1072*4d6fc14bSjoerg    }
1073*4d6fc14bSjoerg    else if (__libcpp_isnan_or_builtin(__x.real()) && __x.imag() == 0)
1074*4d6fc14bSjoerg        return __x;
1075*4d6fc14bSjoerg    _Tp __e = exp(__x.real());
1076*4d6fc14bSjoerg    return complex<_Tp>(__e * cos(__i), __e * sin(__i));
1077*4d6fc14bSjoerg}
1078*4d6fc14bSjoerg
1079*4d6fc14bSjoerg// pow
1080*4d6fc14bSjoerg
1081*4d6fc14bSjoergtemplate<class _Tp>
1082*4d6fc14bSjoerginline _LIBCPP_INLINE_VISIBILITY
1083*4d6fc14bSjoergcomplex<_Tp>
1084*4d6fc14bSjoergpow(const complex<_Tp>& __x, const complex<_Tp>& __y)
1085*4d6fc14bSjoerg{
1086*4d6fc14bSjoerg    return exp(__y * log(__x));
1087*4d6fc14bSjoerg}
1088*4d6fc14bSjoerg
1089*4d6fc14bSjoergtemplate<class _Tp, class _Up>
1090*4d6fc14bSjoerginline _LIBCPP_INLINE_VISIBILITY
1091*4d6fc14bSjoergcomplex<typename __promote<_Tp, _Up>::type>
1092*4d6fc14bSjoergpow(const complex<_Tp>& __x, const complex<_Up>& __y)
1093*4d6fc14bSjoerg{
1094*4d6fc14bSjoerg    typedef complex<typename __promote<_Tp, _Up>::type> result_type;
1095*4d6fc14bSjoerg    return _VSTD::pow(result_type(__x), result_type(__y));
1096*4d6fc14bSjoerg}
1097*4d6fc14bSjoerg
1098*4d6fc14bSjoergtemplate<class _Tp, class _Up>
1099*4d6fc14bSjoerginline _LIBCPP_INLINE_VISIBILITY
1100*4d6fc14bSjoergtypename enable_if
1101*4d6fc14bSjoerg<
1102*4d6fc14bSjoerg    is_arithmetic<_Up>::value,
1103*4d6fc14bSjoerg    complex<typename __promote<_Tp, _Up>::type>
1104*4d6fc14bSjoerg>::type
1105*4d6fc14bSjoergpow(const complex<_Tp>& __x, const _Up& __y)
1106*4d6fc14bSjoerg{
1107*4d6fc14bSjoerg    typedef complex<typename __promote<_Tp, _Up>::type> result_type;
1108*4d6fc14bSjoerg    return _VSTD::pow(result_type(__x), result_type(__y));
1109*4d6fc14bSjoerg}
1110*4d6fc14bSjoerg
1111*4d6fc14bSjoergtemplate<class _Tp, class _Up>
1112*4d6fc14bSjoerginline _LIBCPP_INLINE_VISIBILITY
1113*4d6fc14bSjoergtypename enable_if
1114*4d6fc14bSjoerg<
1115*4d6fc14bSjoerg    is_arithmetic<_Tp>::value,
1116*4d6fc14bSjoerg    complex<typename __promote<_Tp, _Up>::type>
1117*4d6fc14bSjoerg>::type
1118*4d6fc14bSjoergpow(const _Tp& __x, const complex<_Up>& __y)
1119*4d6fc14bSjoerg{
1120*4d6fc14bSjoerg    typedef complex<typename __promote<_Tp, _Up>::type> result_type;
1121*4d6fc14bSjoerg    return _VSTD::pow(result_type(__x), result_type(__y));
1122*4d6fc14bSjoerg}
1123*4d6fc14bSjoerg
1124*4d6fc14bSjoerg// __sqr, computes pow(x, 2)
1125*4d6fc14bSjoerg
1126*4d6fc14bSjoergtemplate<class _Tp>
1127*4d6fc14bSjoerginline _LIBCPP_INLINE_VISIBILITY
1128*4d6fc14bSjoergcomplex<_Tp>
1129*4d6fc14bSjoerg__sqr(const complex<_Tp>& __x)
1130*4d6fc14bSjoerg{
1131*4d6fc14bSjoerg    return complex<_Tp>((__x.real() - __x.imag()) * (__x.real() + __x.imag()),
1132*4d6fc14bSjoerg                        _Tp(2) * __x.real() * __x.imag());
1133*4d6fc14bSjoerg}
1134*4d6fc14bSjoerg
1135*4d6fc14bSjoerg// asinh
1136*4d6fc14bSjoerg
1137*4d6fc14bSjoergtemplate<class _Tp>
1138*4d6fc14bSjoergcomplex<_Tp>
1139*4d6fc14bSjoergasinh(const complex<_Tp>& __x)
1140*4d6fc14bSjoerg{
1141*4d6fc14bSjoerg    const _Tp __pi(atan2(+0., -0.));
1142*4d6fc14bSjoerg    if (__libcpp_isinf_or_builtin(__x.real()))
1143*4d6fc14bSjoerg    {
1144*4d6fc14bSjoerg        if (__libcpp_isnan_or_builtin(__x.imag()))
1145*4d6fc14bSjoerg            return __x;
1146*4d6fc14bSjoerg        if (__libcpp_isinf_or_builtin(__x.imag()))
1147*4d6fc14bSjoerg            return complex<_Tp>(__x.real(), copysign(__pi * _Tp(0.25), __x.imag()));
1148*4d6fc14bSjoerg        return complex<_Tp>(__x.real(), copysign(_Tp(0), __x.imag()));
1149*4d6fc14bSjoerg    }
1150*4d6fc14bSjoerg    if (__libcpp_isnan_or_builtin(__x.real()))
1151*4d6fc14bSjoerg    {
1152*4d6fc14bSjoerg        if (__libcpp_isinf_or_builtin(__x.imag()))
1153*4d6fc14bSjoerg            return complex<_Tp>(__x.imag(), __x.real());
1154*4d6fc14bSjoerg        if (__x.imag() == 0)
1155*4d6fc14bSjoerg            return __x;
1156*4d6fc14bSjoerg        return complex<_Tp>(__x.real(), __x.real());
1157*4d6fc14bSjoerg    }
1158*4d6fc14bSjoerg    if (__libcpp_isinf_or_builtin(__x.imag()))
1159*4d6fc14bSjoerg        return complex<_Tp>(copysign(__x.imag(), __x.real()), copysign(__pi/_Tp(2), __x.imag()));
1160*4d6fc14bSjoerg    complex<_Tp> __z = log(__x + sqrt(__sqr(__x) + _Tp(1)));
1161*4d6fc14bSjoerg    return complex<_Tp>(copysign(__z.real(), __x.real()), copysign(__z.imag(), __x.imag()));
1162*4d6fc14bSjoerg}
1163*4d6fc14bSjoerg
1164*4d6fc14bSjoerg// acosh
1165*4d6fc14bSjoerg
1166*4d6fc14bSjoergtemplate<class _Tp>
1167*4d6fc14bSjoergcomplex<_Tp>
1168*4d6fc14bSjoergacosh(const complex<_Tp>& __x)
1169*4d6fc14bSjoerg{
1170*4d6fc14bSjoerg    const _Tp __pi(atan2(+0., -0.));
1171*4d6fc14bSjoerg    if (__libcpp_isinf_or_builtin(__x.real()))
1172*4d6fc14bSjoerg    {
1173*4d6fc14bSjoerg        if (__libcpp_isnan_or_builtin(__x.imag()))
1174*4d6fc14bSjoerg            return complex<_Tp>(abs(__x.real()), __x.imag());
1175*4d6fc14bSjoerg        if (__libcpp_isinf_or_builtin(__x.imag()))
1176*4d6fc14bSjoerg        {
1177*4d6fc14bSjoerg            if (__x.real() > 0)
1178*4d6fc14bSjoerg                return complex<_Tp>(__x.real(), copysign(__pi * _Tp(0.25), __x.imag()));
1179*4d6fc14bSjoerg            else
1180*4d6fc14bSjoerg                return complex<_Tp>(-__x.real(), copysign(__pi * _Tp(0.75), __x.imag()));
1181*4d6fc14bSjoerg        }
1182*4d6fc14bSjoerg        if (__x.real() < 0)
1183*4d6fc14bSjoerg            return complex<_Tp>(-__x.real(), copysign(__pi, __x.imag()));
1184*4d6fc14bSjoerg        return complex<_Tp>(__x.real(), copysign(_Tp(0), __x.imag()));
1185*4d6fc14bSjoerg    }
1186*4d6fc14bSjoerg    if (__libcpp_isnan_or_builtin(__x.real()))
1187*4d6fc14bSjoerg    {
1188*4d6fc14bSjoerg        if (__libcpp_isinf_or_builtin(__x.imag()))
1189*4d6fc14bSjoerg            return complex<_Tp>(abs(__x.imag()), __x.real());
1190*4d6fc14bSjoerg        return complex<_Tp>(__x.real(), __x.real());
1191*4d6fc14bSjoerg    }
1192*4d6fc14bSjoerg    if (__libcpp_isinf_or_builtin(__x.imag()))
1193*4d6fc14bSjoerg        return complex<_Tp>(abs(__x.imag()), copysign(__pi/_Tp(2), __x.imag()));
1194*4d6fc14bSjoerg    complex<_Tp> __z = log(__x + sqrt(__sqr(__x) - _Tp(1)));
1195*4d6fc14bSjoerg    return complex<_Tp>(copysign(__z.real(), _Tp(0)), copysign(__z.imag(), __x.imag()));
1196*4d6fc14bSjoerg}
1197*4d6fc14bSjoerg
1198*4d6fc14bSjoerg// atanh
1199*4d6fc14bSjoerg
1200*4d6fc14bSjoergtemplate<class _Tp>
1201*4d6fc14bSjoergcomplex<_Tp>
1202*4d6fc14bSjoergatanh(const complex<_Tp>& __x)
1203*4d6fc14bSjoerg{
1204*4d6fc14bSjoerg    const _Tp __pi(atan2(+0., -0.));
1205*4d6fc14bSjoerg    if (__libcpp_isinf_or_builtin(__x.imag()))
1206*4d6fc14bSjoerg    {
1207*4d6fc14bSjoerg        return complex<_Tp>(copysign(_Tp(0), __x.real()), copysign(__pi/_Tp(2), __x.imag()));
1208*4d6fc14bSjoerg    }
1209*4d6fc14bSjoerg    if (__libcpp_isnan_or_builtin(__x.imag()))
1210*4d6fc14bSjoerg    {
1211*4d6fc14bSjoerg        if (__libcpp_isinf_or_builtin(__x.real()) || __x.real() == 0)
1212*4d6fc14bSjoerg            return complex<_Tp>(copysign(_Tp(0), __x.real()), __x.imag());
1213*4d6fc14bSjoerg        return complex<_Tp>(__x.imag(), __x.imag());
1214*4d6fc14bSjoerg    }
1215*4d6fc14bSjoerg    if (__libcpp_isnan_or_builtin(__x.real()))
1216*4d6fc14bSjoerg    {
1217*4d6fc14bSjoerg        return complex<_Tp>(__x.real(), __x.real());
1218*4d6fc14bSjoerg    }
1219*4d6fc14bSjoerg    if (__libcpp_isinf_or_builtin(__x.real()))
1220*4d6fc14bSjoerg    {
1221*4d6fc14bSjoerg        return complex<_Tp>(copysign(_Tp(0), __x.real()), copysign(__pi/_Tp(2), __x.imag()));
1222*4d6fc14bSjoerg    }
1223*4d6fc14bSjoerg    if (abs(__x.real()) == _Tp(1) && __x.imag() == _Tp(0))
1224*4d6fc14bSjoerg    {
1225*4d6fc14bSjoerg        return complex<_Tp>(copysign(_Tp(INFINITY), __x.real()), copysign(_Tp(0), __x.imag()));
1226*4d6fc14bSjoerg    }
1227*4d6fc14bSjoerg    complex<_Tp> __z = log((_Tp(1) + __x) / (_Tp(1) - __x)) / _Tp(2);
1228*4d6fc14bSjoerg    return complex<_Tp>(copysign(__z.real(), __x.real()), copysign(__z.imag(), __x.imag()));
1229*4d6fc14bSjoerg}
1230*4d6fc14bSjoerg
1231*4d6fc14bSjoerg// sinh
1232*4d6fc14bSjoerg
1233*4d6fc14bSjoergtemplate<class _Tp>
1234*4d6fc14bSjoergcomplex<_Tp>
1235*4d6fc14bSjoergsinh(const complex<_Tp>& __x)
1236*4d6fc14bSjoerg{
1237*4d6fc14bSjoerg    if (__libcpp_isinf_or_builtin(__x.real()) && !__libcpp_isfinite_or_builtin(__x.imag()))
1238*4d6fc14bSjoerg        return complex<_Tp>(__x.real(), _Tp(NAN));
1239*4d6fc14bSjoerg    if (__x.real() == 0 && !__libcpp_isfinite_or_builtin(__x.imag()))
1240*4d6fc14bSjoerg        return complex<_Tp>(__x.real(), _Tp(NAN));
1241*4d6fc14bSjoerg    if (__x.imag() == 0 && !__libcpp_isfinite_or_builtin(__x.real()))
1242*4d6fc14bSjoerg        return __x;
1243*4d6fc14bSjoerg    return complex<_Tp>(sinh(__x.real()) * cos(__x.imag()), cosh(__x.real()) * sin(__x.imag()));
1244*4d6fc14bSjoerg}
1245*4d6fc14bSjoerg
1246*4d6fc14bSjoerg// cosh
1247*4d6fc14bSjoerg
1248*4d6fc14bSjoergtemplate<class _Tp>
1249*4d6fc14bSjoergcomplex<_Tp>
1250*4d6fc14bSjoergcosh(const complex<_Tp>& __x)
1251*4d6fc14bSjoerg{
1252*4d6fc14bSjoerg    if (__libcpp_isinf_or_builtin(__x.real()) && !__libcpp_isfinite_or_builtin(__x.imag()))
1253*4d6fc14bSjoerg        return complex<_Tp>(abs(__x.real()), _Tp(NAN));
1254*4d6fc14bSjoerg    if (__x.real() == 0 && !__libcpp_isfinite_or_builtin(__x.imag()))
1255*4d6fc14bSjoerg        return complex<_Tp>(_Tp(NAN), __x.real());
1256*4d6fc14bSjoerg    if (__x.real() == 0 && __x.imag() == 0)
1257*4d6fc14bSjoerg        return complex<_Tp>(_Tp(1), __x.imag());
1258*4d6fc14bSjoerg    if (__x.imag() == 0 && !__libcpp_isfinite_or_builtin(__x.real()))
1259*4d6fc14bSjoerg        return complex<_Tp>(abs(__x.real()), __x.imag());
1260*4d6fc14bSjoerg    return complex<_Tp>(cosh(__x.real()) * cos(__x.imag()), sinh(__x.real()) * sin(__x.imag()));
1261*4d6fc14bSjoerg}
1262*4d6fc14bSjoerg
1263*4d6fc14bSjoerg// tanh
1264*4d6fc14bSjoerg
1265*4d6fc14bSjoergtemplate<class _Tp>
1266*4d6fc14bSjoergcomplex<_Tp>
1267*4d6fc14bSjoergtanh(const complex<_Tp>& __x)
1268*4d6fc14bSjoerg{
1269*4d6fc14bSjoerg    if (__libcpp_isinf_or_builtin(__x.real()))
1270*4d6fc14bSjoerg    {
1271*4d6fc14bSjoerg        if (!__libcpp_isfinite_or_builtin(__x.imag()))
1272*4d6fc14bSjoerg            return complex<_Tp>(_Tp(1), _Tp(0));
1273*4d6fc14bSjoerg        return complex<_Tp>(_Tp(1), copysign(_Tp(0), sin(_Tp(2) * __x.imag())));
1274*4d6fc14bSjoerg    }
1275*4d6fc14bSjoerg    if (__libcpp_isnan_or_builtin(__x.real()) && __x.imag() == 0)
1276*4d6fc14bSjoerg        return __x;
1277*4d6fc14bSjoerg    _Tp __2r(_Tp(2) * __x.real());
1278*4d6fc14bSjoerg    _Tp __2i(_Tp(2) * __x.imag());
1279*4d6fc14bSjoerg    _Tp __d(cosh(__2r) + cos(__2i));
1280*4d6fc14bSjoerg    _Tp __2rsh(sinh(__2r));
1281*4d6fc14bSjoerg    if (__libcpp_isinf_or_builtin(__2rsh) && __libcpp_isinf_or_builtin(__d))
1282*4d6fc14bSjoerg        return complex<_Tp>(__2rsh > _Tp(0) ? _Tp(1) : _Tp(-1),
1283*4d6fc14bSjoerg                            __2i > _Tp(0) ? _Tp(0) : _Tp(-0.));
1284*4d6fc14bSjoerg    return  complex<_Tp>(__2rsh/__d, sin(__2i)/__d);
1285*4d6fc14bSjoerg}
1286*4d6fc14bSjoerg
1287*4d6fc14bSjoerg// asin
1288*4d6fc14bSjoerg
1289*4d6fc14bSjoergtemplate<class _Tp>
1290*4d6fc14bSjoergcomplex<_Tp>
1291*4d6fc14bSjoergasin(const complex<_Tp>& __x)
1292*4d6fc14bSjoerg{
1293*4d6fc14bSjoerg    complex<_Tp> __z = asinh(complex<_Tp>(-__x.imag(), __x.real()));
1294*4d6fc14bSjoerg    return complex<_Tp>(__z.imag(), -__z.real());
1295*4d6fc14bSjoerg}
1296*4d6fc14bSjoerg
1297*4d6fc14bSjoerg// acos
1298*4d6fc14bSjoerg
1299*4d6fc14bSjoergtemplate<class _Tp>
1300*4d6fc14bSjoergcomplex<_Tp>
1301*4d6fc14bSjoergacos(const complex<_Tp>& __x)
1302*4d6fc14bSjoerg{
1303*4d6fc14bSjoerg    const _Tp __pi(atan2(+0., -0.));
1304*4d6fc14bSjoerg    if (__libcpp_isinf_or_builtin(__x.real()))
1305*4d6fc14bSjoerg    {
1306*4d6fc14bSjoerg        if (__libcpp_isnan_or_builtin(__x.imag()))
1307*4d6fc14bSjoerg            return complex<_Tp>(__x.imag(), __x.real());
1308*4d6fc14bSjoerg        if (__libcpp_isinf_or_builtin(__x.imag()))
1309*4d6fc14bSjoerg        {
1310*4d6fc14bSjoerg            if (__x.real() < _Tp(0))
1311*4d6fc14bSjoerg                return complex<_Tp>(_Tp(0.75) * __pi, -__x.imag());
1312*4d6fc14bSjoerg            return complex<_Tp>(_Tp(0.25) * __pi, -__x.imag());
1313*4d6fc14bSjoerg        }
1314*4d6fc14bSjoerg        if (__x.real() < _Tp(0))
1315*4d6fc14bSjoerg            return complex<_Tp>(__pi, signbit(__x.imag()) ? -__x.real() : __x.real());
1316*4d6fc14bSjoerg        return complex<_Tp>(_Tp(0), signbit(__x.imag()) ? __x.real() : -__x.real());
1317*4d6fc14bSjoerg    }
1318*4d6fc14bSjoerg    if (__libcpp_isnan_or_builtin(__x.real()))
1319*4d6fc14bSjoerg    {
1320*4d6fc14bSjoerg        if (__libcpp_isinf_or_builtin(__x.imag()))
1321*4d6fc14bSjoerg            return complex<_Tp>(__x.real(), -__x.imag());
1322*4d6fc14bSjoerg        return complex<_Tp>(__x.real(), __x.real());
1323*4d6fc14bSjoerg    }
1324*4d6fc14bSjoerg    if (__libcpp_isinf_or_builtin(__x.imag()))
1325*4d6fc14bSjoerg        return complex<_Tp>(__pi/_Tp(2), -__x.imag());
1326*4d6fc14bSjoerg    if (__x.real() == 0 && (__x.imag() == 0 || isnan(__x.imag())))
1327*4d6fc14bSjoerg        return complex<_Tp>(__pi/_Tp(2), -__x.imag());
1328*4d6fc14bSjoerg    complex<_Tp> __z = log(__x + sqrt(__sqr(__x) - _Tp(1)));
1329*4d6fc14bSjoerg    if (signbit(__x.imag()))
1330*4d6fc14bSjoerg        return complex<_Tp>(abs(__z.imag()), abs(__z.real()));
1331*4d6fc14bSjoerg    return complex<_Tp>(abs(__z.imag()), -abs(__z.real()));
1332*4d6fc14bSjoerg}
1333*4d6fc14bSjoerg
1334*4d6fc14bSjoerg// atan
1335*4d6fc14bSjoerg
1336*4d6fc14bSjoergtemplate<class _Tp>
1337*4d6fc14bSjoergcomplex<_Tp>
1338*4d6fc14bSjoergatan(const complex<_Tp>& __x)
1339*4d6fc14bSjoerg{
1340*4d6fc14bSjoerg    complex<_Tp> __z = atanh(complex<_Tp>(-__x.imag(), __x.real()));
1341*4d6fc14bSjoerg    return complex<_Tp>(__z.imag(), -__z.real());
1342*4d6fc14bSjoerg}
1343*4d6fc14bSjoerg
1344*4d6fc14bSjoerg// sin
1345*4d6fc14bSjoerg
1346*4d6fc14bSjoergtemplate<class _Tp>
1347*4d6fc14bSjoergcomplex<_Tp>
1348*4d6fc14bSjoergsin(const complex<_Tp>& __x)
1349*4d6fc14bSjoerg{
1350*4d6fc14bSjoerg    complex<_Tp> __z = sinh(complex<_Tp>(-__x.imag(), __x.real()));
1351*4d6fc14bSjoerg    return complex<_Tp>(__z.imag(), -__z.real());
1352*4d6fc14bSjoerg}
1353*4d6fc14bSjoerg
1354*4d6fc14bSjoerg// cos
1355*4d6fc14bSjoerg
1356*4d6fc14bSjoergtemplate<class _Tp>
1357*4d6fc14bSjoerginline _LIBCPP_INLINE_VISIBILITY
1358*4d6fc14bSjoergcomplex<_Tp>
1359*4d6fc14bSjoergcos(const complex<_Tp>& __x)
1360*4d6fc14bSjoerg{
1361*4d6fc14bSjoerg    return cosh(complex<_Tp>(-__x.imag(), __x.real()));
1362*4d6fc14bSjoerg}
1363*4d6fc14bSjoerg
1364*4d6fc14bSjoerg// tan
1365*4d6fc14bSjoerg
1366*4d6fc14bSjoergtemplate<class _Tp>
1367*4d6fc14bSjoergcomplex<_Tp>
1368*4d6fc14bSjoergtan(const complex<_Tp>& __x)
1369*4d6fc14bSjoerg{
1370*4d6fc14bSjoerg    complex<_Tp> __z = tanh(complex<_Tp>(-__x.imag(), __x.real()));
1371*4d6fc14bSjoerg    return complex<_Tp>(__z.imag(), -__z.real());
1372*4d6fc14bSjoerg}
1373*4d6fc14bSjoerg
1374*4d6fc14bSjoergtemplate<class _Tp, class _CharT, class _Traits>
1375*4d6fc14bSjoergbasic_istream<_CharT, _Traits>&
1376*4d6fc14bSjoergoperator>>(basic_istream<_CharT, _Traits>& __is, complex<_Tp>& __x)
1377*4d6fc14bSjoerg{
1378*4d6fc14bSjoerg    if (__is.good())
1379*4d6fc14bSjoerg    {
1380*4d6fc14bSjoerg        ws(__is);
1381*4d6fc14bSjoerg        if (__is.peek() == _CharT('('))
1382*4d6fc14bSjoerg        {
1383*4d6fc14bSjoerg            __is.get();
1384*4d6fc14bSjoerg            _Tp __r;
1385*4d6fc14bSjoerg            __is >> __r;
1386*4d6fc14bSjoerg            if (!__is.fail())
1387*4d6fc14bSjoerg            {
1388*4d6fc14bSjoerg                ws(__is);
1389*4d6fc14bSjoerg                _CharT __c = __is.peek();
1390*4d6fc14bSjoerg                if (__c == _CharT(','))
1391*4d6fc14bSjoerg                {
1392*4d6fc14bSjoerg                    __is.get();
1393*4d6fc14bSjoerg                    _Tp __i;
1394*4d6fc14bSjoerg                    __is >> __i;
1395*4d6fc14bSjoerg                    if (!__is.fail())
1396*4d6fc14bSjoerg                    {
1397*4d6fc14bSjoerg                        ws(__is);
1398*4d6fc14bSjoerg                        __c = __is.peek();
1399*4d6fc14bSjoerg                        if (__c == _CharT(')'))
1400*4d6fc14bSjoerg                        {
1401*4d6fc14bSjoerg                            __is.get();
1402*4d6fc14bSjoerg                            __x = complex<_Tp>(__r, __i);
1403*4d6fc14bSjoerg                        }
1404*4d6fc14bSjoerg                        else
1405*4d6fc14bSjoerg                            __is.setstate(__is.failbit);
1406*4d6fc14bSjoerg                    }
1407*4d6fc14bSjoerg                    else
1408*4d6fc14bSjoerg                        __is.setstate(__is.failbit);
1409*4d6fc14bSjoerg                }
1410*4d6fc14bSjoerg                else if (__c == _CharT(')'))
1411*4d6fc14bSjoerg                {
1412*4d6fc14bSjoerg                    __is.get();
1413*4d6fc14bSjoerg                    __x = complex<_Tp>(__r, _Tp(0));
1414*4d6fc14bSjoerg                }
1415*4d6fc14bSjoerg                else
1416*4d6fc14bSjoerg                    __is.setstate(__is.failbit);
1417*4d6fc14bSjoerg            }
1418*4d6fc14bSjoerg            else
1419*4d6fc14bSjoerg                __is.setstate(__is.failbit);
1420*4d6fc14bSjoerg        }
1421*4d6fc14bSjoerg        else
1422*4d6fc14bSjoerg        {
1423*4d6fc14bSjoerg            _Tp __r;
1424*4d6fc14bSjoerg            __is >> __r;
1425*4d6fc14bSjoerg            if (!__is.fail())
1426*4d6fc14bSjoerg                __x = complex<_Tp>(__r, _Tp(0));
1427*4d6fc14bSjoerg            else
1428*4d6fc14bSjoerg                __is.setstate(__is.failbit);
1429*4d6fc14bSjoerg        }
1430*4d6fc14bSjoerg    }
1431*4d6fc14bSjoerg    else
1432*4d6fc14bSjoerg        __is.setstate(__is.failbit);
1433*4d6fc14bSjoerg    return __is;
1434*4d6fc14bSjoerg}
1435*4d6fc14bSjoerg
1436*4d6fc14bSjoerg#if !defined(_LIBCPP_HAS_NO_LOCALIZATION)
1437*4d6fc14bSjoergtemplate<class _Tp, class _CharT, class _Traits>
1438*4d6fc14bSjoergbasic_ostream<_CharT, _Traits>&
1439*4d6fc14bSjoergoperator<<(basic_ostream<_CharT, _Traits>& __os, const complex<_Tp>& __x)
1440*4d6fc14bSjoerg{
1441*4d6fc14bSjoerg    basic_ostringstream<_CharT, _Traits> __s;
1442*4d6fc14bSjoerg    __s.flags(__os.flags());
1443*4d6fc14bSjoerg    __s.imbue(__os.getloc());
1444*4d6fc14bSjoerg    __s.precision(__os.precision());
1445*4d6fc14bSjoerg    __s << '(' << __x.real() << ',' << __x.imag() << ')';
1446*4d6fc14bSjoerg    return __os << __s.str();
1447*4d6fc14bSjoerg}
1448*4d6fc14bSjoerg#endif // !_LIBCPP_HAS_NO_LOCALIZATION
1449*4d6fc14bSjoerg
1450*4d6fc14bSjoerg#if _LIBCPP_STD_VER > 11
1451*4d6fc14bSjoerg// Literal suffix for complex number literals [complex.literals]
1452*4d6fc14bSjoerginline namespace literals
1453*4d6fc14bSjoerg{
1454*4d6fc14bSjoerg  inline namespace complex_literals
1455*4d6fc14bSjoerg  {
1456*4d6fc14bSjoerg    constexpr complex<long double> operator""il(long double __im)
1457*4d6fc14bSjoerg    {
1458*4d6fc14bSjoerg        return { 0.0l, __im };
1459*4d6fc14bSjoerg    }
1460*4d6fc14bSjoerg
1461*4d6fc14bSjoerg    constexpr complex<long double> operator""il(unsigned long long __im)
1462*4d6fc14bSjoerg    {
1463*4d6fc14bSjoerg        return { 0.0l, static_cast<long double>(__im) };
1464*4d6fc14bSjoerg    }
1465*4d6fc14bSjoerg
1466*4d6fc14bSjoerg
1467*4d6fc14bSjoerg    constexpr complex<double> operator""i(long double __im)
1468*4d6fc14bSjoerg    {
1469*4d6fc14bSjoerg        return { 0.0, static_cast<double>(__im) };
1470*4d6fc14bSjoerg    }
1471*4d6fc14bSjoerg
1472*4d6fc14bSjoerg    constexpr complex<double> operator""i(unsigned long long __im)
1473*4d6fc14bSjoerg    {
1474*4d6fc14bSjoerg        return { 0.0, static_cast<double>(__im) };
1475*4d6fc14bSjoerg    }
1476*4d6fc14bSjoerg
1477*4d6fc14bSjoerg
1478*4d6fc14bSjoerg    constexpr complex<float> operator""if(long double __im)
1479*4d6fc14bSjoerg    {
1480*4d6fc14bSjoerg        return { 0.0f, static_cast<float>(__im) };
1481*4d6fc14bSjoerg    }
1482*4d6fc14bSjoerg
1483*4d6fc14bSjoerg    constexpr complex<float> operator""if(unsigned long long __im)
1484*4d6fc14bSjoerg    {
1485*4d6fc14bSjoerg        return { 0.0f, static_cast<float>(__im) };
1486*4d6fc14bSjoerg    }
1487*4d6fc14bSjoerg  }
1488*4d6fc14bSjoerg}
1489*4d6fc14bSjoerg#endif
1490*4d6fc14bSjoerg
1491*4d6fc14bSjoerg_LIBCPP_END_NAMESPACE_STD
1492*4d6fc14bSjoerg
1493*4d6fc14bSjoerg#endif // _LIBCPP_COMPLEX
1494