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