complex revision 309124
1227825Stheraven// -*- C++ -*-
2227825Stheraven//===--------------------------- complex ----------------------------------===//
3227825Stheraven//
4227825Stheraven//                     The LLVM Compiler Infrastructure
5227825Stheraven//
6227825Stheraven// This file is dual licensed under the MIT and the University of Illinois Open
7227825Stheraven// Source Licenses. See LICENSE.TXT for details.
8227825Stheraven//
9227825Stheraven//===----------------------------------------------------------------------===//
10227825Stheraven
11227825Stheraven#ifndef _LIBCPP_COMPLEX
12227825Stheraven#define _LIBCPP_COMPLEX
13227825Stheraven
14227825Stheraven/*
15227825Stheraven    complex synopsis
16227825Stheraven
17227825Stheravennamespace std
18227825Stheraven{
19227825Stheraven
20227825Stheraventemplate<class T>
21227825Stheravenclass complex
22227825Stheraven{
23227825Stheravenpublic:
24227825Stheraven    typedef T value_type;
25227825Stheraven
26261272Sdim    complex(const T& re = T(), const T& im = T()); // constexpr in C++14
27261272Sdim    complex(const complex&);  // constexpr in C++14
28261272Sdim    template<class X> complex(const complex<X>&);  // constexpr in C++14
29227825Stheraven
30261272Sdim    T real() const; // constexpr in C++14
31261272Sdim    T imag() const; // constexpr in C++14
32227825Stheraven
33227825Stheraven    void real(T);
34227825Stheraven    void imag(T);
35227825Stheraven
36227825Stheraven    complex<T>& operator= (const T&);
37227825Stheraven    complex<T>& operator+=(const T&);
38227825Stheraven    complex<T>& operator-=(const T&);
39227825Stheraven    complex<T>& operator*=(const T&);
40227825Stheraven    complex<T>& operator/=(const T&);
41227825Stheraven
42227825Stheraven    complex& operator=(const complex&);
43227825Stheraven    template<class X> complex<T>& operator= (const complex<X>&);
44227825Stheraven    template<class X> complex<T>& operator+=(const complex<X>&);
45227825Stheraven    template<class X> complex<T>& operator-=(const complex<X>&);
46227825Stheraven    template<class X> complex<T>& operator*=(const complex<X>&);
47227825Stheraven    template<class X> complex<T>& operator/=(const complex<X>&);
48227825Stheraven};
49227825Stheraven
50227825Stheraventemplate<>
51227825Stheravenclass complex<float>
52227825Stheraven{
53227825Stheravenpublic:
54227825Stheraven    typedef float value_type;
55227825Stheraven
56227825Stheraven    constexpr complex(float re = 0.0f, float im = 0.0f);
57227825Stheraven    explicit constexpr complex(const complex<double>&);
58227825Stheraven    explicit constexpr complex(const complex<long double>&);
59227825Stheraven
60227825Stheraven    constexpr float real() const;
61227825Stheraven    void real(float);
62227825Stheraven    constexpr float imag() const;
63227825Stheraven    void imag(float);
64227825Stheraven
65227825Stheraven    complex<float>& operator= (float);
66227825Stheraven    complex<float>& operator+=(float);
67227825Stheraven    complex<float>& operator-=(float);
68227825Stheraven    complex<float>& operator*=(float);
69227825Stheraven    complex<float>& operator/=(float);
70227825Stheraven
71227825Stheraven    complex<float>& operator=(const complex<float>&);
72227825Stheraven    template<class X> complex<float>& operator= (const complex<X>&);
73227825Stheraven    template<class X> complex<float>& operator+=(const complex<X>&);
74227825Stheraven    template<class X> complex<float>& operator-=(const complex<X>&);
75227825Stheraven    template<class X> complex<float>& operator*=(const complex<X>&);
76227825Stheraven    template<class X> complex<float>& operator/=(const complex<X>&);
77227825Stheraven};
78227825Stheraven
79227825Stheraventemplate<>
80227825Stheravenclass complex<double>
81227825Stheraven{
82227825Stheravenpublic:
83227825Stheraven    typedef double value_type;
84227825Stheraven
85227825Stheraven    constexpr complex(double re = 0.0, double im = 0.0);
86227825Stheraven    constexpr complex(const complex<float>&);
87227825Stheraven    explicit constexpr complex(const complex<long double>&);
88227825Stheraven
89227825Stheraven    constexpr double real() const;
90227825Stheraven    void real(double);
91227825Stheraven    constexpr double imag() const;
92227825Stheraven    void imag(double);
93227825Stheraven
94227825Stheraven    complex<double>& operator= (double);
95227825Stheraven    complex<double>& operator+=(double);
96227825Stheraven    complex<double>& operator-=(double);
97227825Stheraven    complex<double>& operator*=(double);
98227825Stheraven    complex<double>& operator/=(double);
99227825Stheraven    complex<double>& operator=(const complex<double>&);
100227825Stheraven
101227825Stheraven    template<class X> complex<double>& operator= (const complex<X>&);
102227825Stheraven    template<class X> complex<double>& operator+=(const complex<X>&);
103227825Stheraven    template<class X> complex<double>& operator-=(const complex<X>&);
104227825Stheraven    template<class X> complex<double>& operator*=(const complex<X>&);
105227825Stheraven    template<class X> complex<double>& operator/=(const complex<X>&);
106227825Stheraven};
107227825Stheraven
108227825Stheraventemplate<>
109227825Stheravenclass complex<long double>
110227825Stheraven{
111227825Stheravenpublic:
112227825Stheraven    typedef long double value_type;
113227825Stheraven
114227825Stheraven    constexpr complex(long double re = 0.0L, long double im = 0.0L);
115227825Stheraven    constexpr complex(const complex<float>&);
116227825Stheraven    constexpr complex(const complex<double>&);
117227825Stheraven
118227825Stheraven    constexpr long double real() const;
119227825Stheraven    void real(long double);
120227825Stheraven    constexpr long double imag() const;
121227825Stheraven    void imag(long double);
122227825Stheraven
123227825Stheraven    complex<long double>& operator=(const complex<long double>&);
124227825Stheraven    complex<long double>& operator= (long double);
125227825Stheraven    complex<long double>& operator+=(long double);
126227825Stheraven    complex<long double>& operator-=(long double);
127227825Stheraven    complex<long double>& operator*=(long double);
128227825Stheraven    complex<long double>& operator/=(long double);
129227825Stheraven
130227825Stheraven    template<class X> complex<long double>& operator= (const complex<X>&);
131227825Stheraven    template<class X> complex<long double>& operator+=(const complex<X>&);
132227825Stheraven    template<class X> complex<long double>& operator-=(const complex<X>&);
133227825Stheraven    template<class X> complex<long double>& operator*=(const complex<X>&);
134227825Stheraven    template<class X> complex<long double>& operator/=(const complex<X>&);
135227825Stheraven};
136227825Stheraven
137227825Stheraven// 26.3.6 operators:
138227825Stheraventemplate<class T> complex<T> operator+(const complex<T>&, const complex<T>&);
139227825Stheraventemplate<class T> complex<T> operator+(const complex<T>&, const T&);
140227825Stheraventemplate<class T> complex<T> operator+(const T&, const complex<T>&);
141227825Stheraventemplate<class T> complex<T> operator-(const complex<T>&, const complex<T>&);
142227825Stheraventemplate<class T> complex<T> operator-(const complex<T>&, const T&);
143227825Stheraventemplate<class T> complex<T> operator-(const T&, const complex<T>&);
144227825Stheraventemplate<class T> complex<T> operator*(const complex<T>&, const complex<T>&);
145227825Stheraventemplate<class T> complex<T> operator*(const complex<T>&, const T&);
146227825Stheraventemplate<class T> complex<T> operator*(const T&, const complex<T>&);
147227825Stheraventemplate<class T> complex<T> operator/(const complex<T>&, const complex<T>&);
148227825Stheraventemplate<class T> complex<T> operator/(const complex<T>&, const T&);
149227825Stheraventemplate<class T> complex<T> operator/(const T&, const complex<T>&);
150227825Stheraventemplate<class T> complex<T> operator+(const complex<T>&);
151227825Stheraventemplate<class T> complex<T> operator-(const complex<T>&);
152261272Sdimtemplate<class T> bool operator==(const complex<T>&, const complex<T>&); // constexpr in C++14
153261272Sdimtemplate<class T> bool operator==(const complex<T>&, const T&); // constexpr in C++14
154261272Sdimtemplate<class T> bool operator==(const T&, const complex<T>&); // constexpr in C++14
155261272Sdimtemplate<class T> bool operator!=(const complex<T>&, const complex<T>&); // constexpr in C++14
156261272Sdimtemplate<class T> bool operator!=(const complex<T>&, const T&); // constexpr in C++14
157261272Sdimtemplate<class T> bool operator!=(const T&, const complex<T>&); // constexpr in C++14
158227825Stheraven
159227825Stheraventemplate<class T, class charT, class traits>
160227825Stheraven  basic_istream<charT, traits>&
161227825Stheraven  operator>>(basic_istream<charT, traits>&, complex<T>&);
162227825Stheraventemplate<class T, class charT, class traits>
163227825Stheraven  basic_ostream<charT, traits>&
164227825Stheraven  operator<<(basic_ostream<charT, traits>&, const complex<T>&);
165227825Stheraven
166227825Stheraven// 26.3.7 values:
167227825Stheraven
168261272Sdimtemplate<class T>              T real(const complex<T>&); // constexpr in C++14
169261272Sdim                     long double real(long double);       // constexpr in C++14
170261272Sdim                          double real(double);            // constexpr in C++14
171261272Sdimtemplate<Integral T>      double real(T);                 // constexpr in C++14
172261272Sdim                          float  real(float);             // constexpr in C++14
173227825Stheraven
174261272Sdimtemplate<class T>              T imag(const complex<T>&); // constexpr in C++14
175261272Sdim                     long double imag(long double);       // constexpr in C++14
176261272Sdim                          double imag(double);            // constexpr in C++14
177261272Sdimtemplate<Integral T>      double imag(T);                 // constexpr in C++14
178261272Sdim                          float  imag(float);             // constexpr in C++14
179227825Stheraven
180227825Stheraventemplate<class T> T abs(const complex<T>&);
181227825Stheraven
182227825Stheraventemplate<class T>              T arg(const complex<T>&);
183227825Stheraven                     long double arg(long double);
184227825Stheraven                          double arg(double);
185227825Stheraventemplate<Integral T>      double arg(T);
186227825Stheraven                          float  arg(float);
187227825Stheraven
188227825Stheraventemplate<class T>              T norm(const complex<T>&);
189227825Stheraven                     long double norm(long double);
190227825Stheraven                          double norm(double);
191227825Stheraventemplate<Integral T>      double norm(T);
192227825Stheraven                          float  norm(float);
193227825Stheraven
194227825Stheraventemplate<class T>      complex<T>           conj(const complex<T>&);
195227825Stheraven                       complex<long double> conj(long double);
196227825Stheraven                       complex<double>      conj(double);
197227825Stheraventemplate<Integral T>   complex<double>      conj(T);
198227825Stheraven                       complex<float>       conj(float);
199227825Stheraven
200227825Stheraventemplate<class T>    complex<T>           proj(const complex<T>&);
201227825Stheraven                     complex<long double> proj(long double);
202227825Stheraven                     complex<double>      proj(double);
203227825Stheraventemplate<Integral T> complex<double>      proj(T);
204227825Stheraven                     complex<float>       proj(float);
205227825Stheraven
206227825Stheraventemplate<class T> complex<T> polar(const T&, const T& = 0);
207227825Stheraven
208227825Stheraven// 26.3.8 transcendentals:
209227825Stheraventemplate<class T> complex<T> acos(const complex<T>&);
210227825Stheraventemplate<class T> complex<T> asin(const complex<T>&);
211227825Stheraventemplate<class T> complex<T> atan(const complex<T>&);
212227825Stheraventemplate<class T> complex<T> acosh(const complex<T>&);
213227825Stheraventemplate<class T> complex<T> asinh(const complex<T>&);
214227825Stheraventemplate<class T> complex<T> atanh(const complex<T>&);
215227825Stheraventemplate<class T> complex<T> cos (const complex<T>&);
216227825Stheraventemplate<class T> complex<T> cosh (const complex<T>&);
217227825Stheraventemplate<class T> complex<T> exp (const complex<T>&);
218227825Stheraventemplate<class T> complex<T> log (const complex<T>&);
219227825Stheraventemplate<class T> complex<T> log10(const complex<T>&);
220227825Stheraven
221227825Stheraventemplate<class T> complex<T> pow(const complex<T>&, const T&);
222227825Stheraventemplate<class T> complex<T> pow(const complex<T>&, const complex<T>&);
223227825Stheraventemplate<class T> complex<T> pow(const T&, const complex<T>&);
224227825Stheraven
225227825Stheraventemplate<class T> complex<T> sin (const complex<T>&);
226227825Stheraventemplate<class T> complex<T> sinh (const complex<T>&);
227227825Stheraventemplate<class T> complex<T> sqrt (const complex<T>&);
228227825Stheraventemplate<class T> complex<T> tan (const complex<T>&);
229227825Stheraventemplate<class T> complex<T> tanh (const complex<T>&);
230227825Stheraven
231227825Stheraventemplate<class T, class charT, class traits>
232227825Stheraven  basic_istream<charT, traits>&
233227825Stheraven  operator>>(basic_istream<charT, traits>& is, complex<T>& x);
234227825Stheraven
235227825Stheraventemplate<class T, class charT, class traits>
236227825Stheraven  basic_ostream<charT, traits>&
237227825Stheraven  operator<<(basic_ostream<charT, traits>& o, const complex<T>& x);
238227825Stheraven
239227825Stheraven}  // std
240227825Stheraven
241227825Stheraven*/
242227825Stheraven
243227825Stheraven#include <__config>
244227825Stheraven#include <type_traits>
245227825Stheraven#include <stdexcept>
246227825Stheraven#include <cmath>
247227825Stheraven#include <sstream>
248227825Stheraven#if defined(_LIBCPP_NO_EXCEPTIONS)
249227825Stheraven    #include <cassert>
250227825Stheraven#endif
251227825Stheraven
252227825Stheraven#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
253227825Stheraven#pragma GCC system_header
254227825Stheraven#endif
255227825Stheraven
256227825Stheraven_LIBCPP_BEGIN_NAMESPACE_STD
257227825Stheraven
258261272Sdimtemplate<class _Tp> class _LIBCPP_TYPE_VIS_ONLY complex;
259227825Stheraven
260227825Stheraventemplate<class _Tp> complex<_Tp> operator*(const complex<_Tp>& __z, const complex<_Tp>& __w);
261227825Stheraventemplate<class _Tp> complex<_Tp> operator/(const complex<_Tp>& __x, const complex<_Tp>& __y);
262227825Stheraven
263227825Stheraventemplate<class _Tp>
264261272Sdimclass _LIBCPP_TYPE_VIS_ONLY complex
265227825Stheraven{
266227825Stheravenpublic:
267227825Stheraven    typedef _Tp value_type;
268227825Stheravenprivate:
269227825Stheraven    value_type __re_;
270227825Stheraven    value_type __im_;
271227825Stheravenpublic:
272261272Sdim    _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
273227825Stheraven    complex(const value_type& __re = value_type(), const value_type& __im = value_type())
274227825Stheraven        : __re_(__re), __im_(__im) {}
275261272Sdim    template<class _Xp> _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
276227825Stheraven    complex(const complex<_Xp>& __c)
277227825Stheraven        : __re_(__c.real()), __im_(__c.imag()) {}
278227825Stheraven
279261272Sdim    _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11 value_type real() const {return __re_;}
280261272Sdim    _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11 value_type imag() const {return __im_;}
281227825Stheraven
282227825Stheraven    _LIBCPP_INLINE_VISIBILITY void real(value_type __re) {__re_ = __re;}
283227825Stheraven    _LIBCPP_INLINE_VISIBILITY void imag(value_type __im) {__im_ = __im;}
284227825Stheraven
285232924Stheraven    _LIBCPP_INLINE_VISIBILITY complex& operator= (const value_type& __re)
286232924Stheraven        {__re_ = __re; __im_ = value_type(); return *this;}
287227825Stheraven    _LIBCPP_INLINE_VISIBILITY complex& operator+=(const value_type& __re) {__re_ += __re; return *this;}
288227825Stheraven    _LIBCPP_INLINE_VISIBILITY complex& operator-=(const value_type& __re) {__re_ -= __re; return *this;}
289227825Stheraven    _LIBCPP_INLINE_VISIBILITY complex& operator*=(const value_type& __re) {__re_ *= __re; __im_ *= __re; return *this;}
290227825Stheraven    _LIBCPP_INLINE_VISIBILITY complex& operator/=(const value_type& __re) {__re_ /= __re; __im_ /= __re; return *this;}
291227825Stheraven
292227825Stheraven    template<class _Xp> _LIBCPP_INLINE_VISIBILITY complex& operator= (const complex<_Xp>& __c)
293227825Stheraven        {
294227825Stheraven            __re_ = __c.real();
295227825Stheraven            __im_ = __c.imag();
296227825Stheraven            return *this;
297227825Stheraven        }
298227825Stheraven    template<class _Xp> _LIBCPP_INLINE_VISIBILITY complex& operator+=(const complex<_Xp>& __c)
299227825Stheraven        {
300227825Stheraven            __re_ += __c.real();
301227825Stheraven            __im_ += __c.imag();
302227825Stheraven            return *this;
303227825Stheraven        }
304227825Stheraven    template<class _Xp> _LIBCPP_INLINE_VISIBILITY complex& operator-=(const complex<_Xp>& __c)
305227825Stheraven        {
306227825Stheraven            __re_ -= __c.real();
307227825Stheraven            __im_ -= __c.imag();
308227825Stheraven            return *this;
309227825Stheraven        }
310227825Stheraven    template<class _Xp> _LIBCPP_INLINE_VISIBILITY complex& operator*=(const complex<_Xp>& __c)
311227825Stheraven        {
312261272Sdim            *this = *this * complex(__c.real(), __c.imag());
313227825Stheraven            return *this;
314227825Stheraven        }
315227825Stheraven    template<class _Xp> _LIBCPP_INLINE_VISIBILITY complex& operator/=(const complex<_Xp>& __c)
316227825Stheraven        {
317261272Sdim            *this = *this / complex(__c.real(), __c.imag());
318227825Stheraven            return *this;
319227825Stheraven        }
320227825Stheraven};
321227825Stheraven
322261272Sdimtemplate<> class _LIBCPP_TYPE_VIS_ONLY complex<double>;
323261272Sdimtemplate<> class _LIBCPP_TYPE_VIS_ONLY complex<long double>;
324227825Stheraven
325227825Stheraventemplate<>
326261272Sdimclass _LIBCPP_TYPE_VIS_ONLY complex<float>
327227825Stheraven{
328227825Stheraven    float __re_;
329227825Stheraven    float __im_;
330227825Stheravenpublic:
331227825Stheraven    typedef float value_type;
332227825Stheraven
333241900Sdim    _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR complex(float __re = 0.0f, float __im = 0.0f)
334227825Stheraven        : __re_(__re), __im_(__im) {}
335309124Sdim    _LIBCPP_INLINE_VISIBILITY
336241900Sdim    explicit _LIBCPP_CONSTEXPR complex(const complex<double>& __c);
337309124Sdim    _LIBCPP_INLINE_VISIBILITY
338241900Sdim    explicit _LIBCPP_CONSTEXPR complex(const complex<long double>& __c);
339227825Stheraven
340241900Sdim    _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR float real() const {return __re_;}
341241900Sdim    _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR float imag() const {return __im_;}
342227825Stheraven
343227825Stheraven    _LIBCPP_INLINE_VISIBILITY void real(value_type __re) {__re_ = __re;}
344227825Stheraven    _LIBCPP_INLINE_VISIBILITY void imag(value_type __im) {__im_ = __im;}
345227825Stheraven
346232924Stheraven    _LIBCPP_INLINE_VISIBILITY complex& operator= (float __re)
347232924Stheraven        {__re_ = __re; __im_ = value_type(); return *this;}
348227825Stheraven    _LIBCPP_INLINE_VISIBILITY complex& operator+=(float __re) {__re_ += __re; return *this;}
349227825Stheraven    _LIBCPP_INLINE_VISIBILITY complex& operator-=(float __re) {__re_ -= __re; return *this;}
350227825Stheraven    _LIBCPP_INLINE_VISIBILITY complex& operator*=(float __re) {__re_ *= __re; __im_ *= __re; return *this;}
351227825Stheraven    _LIBCPP_INLINE_VISIBILITY complex& operator/=(float __re) {__re_ /= __re; __im_ /= __re; return *this;}
352227825Stheraven
353227825Stheraven    template<class _Xp> _LIBCPP_INLINE_VISIBILITY complex& operator= (const complex<_Xp>& __c)
354227825Stheraven        {
355227825Stheraven            __re_ = __c.real();
356227825Stheraven            __im_ = __c.imag();
357227825Stheraven            return *this;
358227825Stheraven        }
359227825Stheraven    template<class _Xp> _LIBCPP_INLINE_VISIBILITY complex& operator+=(const complex<_Xp>& __c)
360227825Stheraven        {
361227825Stheraven            __re_ += __c.real();
362227825Stheraven            __im_ += __c.imag();
363227825Stheraven            return *this;
364227825Stheraven        }
365227825Stheraven    template<class _Xp> _LIBCPP_INLINE_VISIBILITY complex& operator-=(const complex<_Xp>& __c)
366227825Stheraven        {
367227825Stheraven            __re_ -= __c.real();
368227825Stheraven            __im_ -= __c.imag();
369227825Stheraven            return *this;
370227825Stheraven        }
371227825Stheraven    template<class _Xp> _LIBCPP_INLINE_VISIBILITY complex& operator*=(const complex<_Xp>& __c)
372227825Stheraven        {
373261272Sdim            *this = *this * complex(__c.real(), __c.imag());
374227825Stheraven            return *this;
375227825Stheraven        }
376227825Stheraven    template<class _Xp> _LIBCPP_INLINE_VISIBILITY complex& operator/=(const complex<_Xp>& __c)
377227825Stheraven        {
378261272Sdim            *this = *this / complex(__c.real(), __c.imag());
379227825Stheraven            return *this;
380227825Stheraven        }
381227825Stheraven};
382227825Stheraven
383227825Stheraventemplate<>
384261272Sdimclass _LIBCPP_TYPE_VIS_ONLY complex<double>
385227825Stheraven{
386227825Stheraven    double __re_;
387227825Stheraven    double __im_;
388227825Stheravenpublic:
389227825Stheraven    typedef double value_type;
390227825Stheraven
391241900Sdim    _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR complex(double __re = 0.0, double __im = 0.0)
392227825Stheraven        : __re_(__re), __im_(__im) {}
393309124Sdim    _LIBCPP_INLINE_VISIBILITY
394241900Sdim    _LIBCPP_CONSTEXPR complex(const complex<float>& __c);
395309124Sdim    _LIBCPP_INLINE_VISIBILITY
396241900Sdim    explicit _LIBCPP_CONSTEXPR complex(const complex<long double>& __c);
397227825Stheraven
398241900Sdim    _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR double real() const {return __re_;}
399241900Sdim    _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR double imag() const {return __im_;}
400227825Stheraven
401227825Stheraven    _LIBCPP_INLINE_VISIBILITY void real(value_type __re) {__re_ = __re;}
402227825Stheraven    _LIBCPP_INLINE_VISIBILITY void imag(value_type __im) {__im_ = __im;}
403227825Stheraven
404232924Stheraven    _LIBCPP_INLINE_VISIBILITY complex& operator= (double __re)
405232924Stheraven        {__re_ = __re; __im_ = value_type(); return *this;}
406227825Stheraven    _LIBCPP_INLINE_VISIBILITY complex& operator+=(double __re) {__re_ += __re; return *this;}
407227825Stheraven    _LIBCPP_INLINE_VISIBILITY complex& operator-=(double __re) {__re_ -= __re; return *this;}
408227825Stheraven    _LIBCPP_INLINE_VISIBILITY complex& operator*=(double __re) {__re_ *= __re; __im_ *= __re; return *this;}
409227825Stheraven    _LIBCPP_INLINE_VISIBILITY complex& operator/=(double __re) {__re_ /= __re; __im_ /= __re; return *this;}
410227825Stheraven
411227825Stheraven    template<class _Xp> _LIBCPP_INLINE_VISIBILITY complex& operator= (const complex<_Xp>& __c)
412227825Stheraven        {
413227825Stheraven            __re_ = __c.real();
414227825Stheraven            __im_ = __c.imag();
415227825Stheraven            return *this;
416227825Stheraven        }
417227825Stheraven    template<class _Xp> _LIBCPP_INLINE_VISIBILITY complex& operator+=(const complex<_Xp>& __c)
418227825Stheraven        {
419227825Stheraven            __re_ += __c.real();
420227825Stheraven            __im_ += __c.imag();
421227825Stheraven            return *this;
422227825Stheraven        }
423227825Stheraven    template<class _Xp> _LIBCPP_INLINE_VISIBILITY complex& operator-=(const complex<_Xp>& __c)
424227825Stheraven        {
425227825Stheraven            __re_ -= __c.real();
426227825Stheraven            __im_ -= __c.imag();
427227825Stheraven            return *this;
428227825Stheraven        }
429227825Stheraven    template<class _Xp> _LIBCPP_INLINE_VISIBILITY complex& operator*=(const complex<_Xp>& __c)
430227825Stheraven        {
431261272Sdim            *this = *this * complex(__c.real(), __c.imag());
432227825Stheraven            return *this;
433227825Stheraven        }
434227825Stheraven    template<class _Xp> _LIBCPP_INLINE_VISIBILITY complex& operator/=(const complex<_Xp>& __c)
435227825Stheraven        {
436261272Sdim            *this = *this / complex(__c.real(), __c.imag());
437227825Stheraven            return *this;
438227825Stheraven        }
439227825Stheraven};
440227825Stheraven
441227825Stheraventemplate<>
442261272Sdimclass _LIBCPP_TYPE_VIS_ONLY complex<long double>
443227825Stheraven{
444227825Stheraven    long double __re_;
445227825Stheraven    long double __im_;
446227825Stheravenpublic:
447227825Stheraven    typedef long double value_type;
448227825Stheraven
449241900Sdim    _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR complex(long double __re = 0.0L, long double __im = 0.0L)
450227825Stheraven        : __re_(__re), __im_(__im) {}
451309124Sdim    _LIBCPP_INLINE_VISIBILITY
452241900Sdim    _LIBCPP_CONSTEXPR complex(const complex<float>& __c);
453309124Sdim    _LIBCPP_INLINE_VISIBILITY
454241900Sdim    _LIBCPP_CONSTEXPR complex(const complex<double>& __c);
455227825Stheraven
456241900Sdim    _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR long double real() const {return __re_;}
457241900Sdim    _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR long double imag() const {return __im_;}
458227825Stheraven
459227825Stheraven    _LIBCPP_INLINE_VISIBILITY void real(value_type __re) {__re_ = __re;}
460227825Stheraven    _LIBCPP_INLINE_VISIBILITY void imag(value_type __im) {__im_ = __im;}
461227825Stheraven
462232924Stheraven    _LIBCPP_INLINE_VISIBILITY complex& operator= (long double __re)
463232924Stheraven        {__re_ = __re; __im_ = value_type(); return *this;}
464227825Stheraven    _LIBCPP_INLINE_VISIBILITY complex& operator+=(long double __re) {__re_ += __re; return *this;}
465227825Stheraven    _LIBCPP_INLINE_VISIBILITY complex& operator-=(long double __re) {__re_ -= __re; return *this;}
466227825Stheraven    _LIBCPP_INLINE_VISIBILITY complex& operator*=(long double __re) {__re_ *= __re; __im_ *= __re; return *this;}
467227825Stheraven    _LIBCPP_INLINE_VISIBILITY complex& operator/=(long double __re) {__re_ /= __re; __im_ /= __re; return *this;}
468227825Stheraven
469227825Stheraven    template<class _Xp> _LIBCPP_INLINE_VISIBILITY complex& operator= (const complex<_Xp>& __c)
470227825Stheraven        {
471227825Stheraven            __re_ = __c.real();
472227825Stheraven            __im_ = __c.imag();
473227825Stheraven            return *this;
474227825Stheraven        }
475227825Stheraven    template<class _Xp> _LIBCPP_INLINE_VISIBILITY complex& operator+=(const complex<_Xp>& __c)
476227825Stheraven        {
477227825Stheraven            __re_ += __c.real();
478227825Stheraven            __im_ += __c.imag();
479227825Stheraven            return *this;
480227825Stheraven        }
481227825Stheraven    template<class _Xp> _LIBCPP_INLINE_VISIBILITY complex& operator-=(const complex<_Xp>& __c)
482227825Stheraven        {
483227825Stheraven            __re_ -= __c.real();
484227825Stheraven            __im_ -= __c.imag();
485227825Stheraven            return *this;
486227825Stheraven        }
487227825Stheraven    template<class _Xp> _LIBCPP_INLINE_VISIBILITY complex& operator*=(const complex<_Xp>& __c)
488227825Stheraven        {
489261272Sdim            *this = *this * complex(__c.real(), __c.imag());
490227825Stheraven            return *this;
491227825Stheraven        }
492227825Stheraven    template<class _Xp> _LIBCPP_INLINE_VISIBILITY complex& operator/=(const complex<_Xp>& __c)
493227825Stheraven        {
494261272Sdim            *this = *this / complex(__c.real(), __c.imag());
495227825Stheraven            return *this;
496227825Stheraven        }
497227825Stheraven};
498227825Stheraven
499309124Sdiminline
500241900Sdim_LIBCPP_CONSTEXPR
501227825Stheravencomplex<float>::complex(const complex<double>& __c)
502227825Stheraven    : __re_(__c.real()), __im_(__c.imag()) {}
503227825Stheraven
504309124Sdiminline
505241900Sdim_LIBCPP_CONSTEXPR
506227825Stheravencomplex<float>::complex(const complex<long double>& __c)
507227825Stheraven    : __re_(__c.real()), __im_(__c.imag()) {}
508227825Stheraven
509309124Sdiminline
510241900Sdim_LIBCPP_CONSTEXPR
511227825Stheravencomplex<double>::complex(const complex<float>& __c)
512227825Stheraven    : __re_(__c.real()), __im_(__c.imag()) {}
513227825Stheraven
514309124Sdiminline
515241900Sdim_LIBCPP_CONSTEXPR
516227825Stheravencomplex<double>::complex(const complex<long double>& __c)
517227825Stheraven    : __re_(__c.real()), __im_(__c.imag()) {}
518227825Stheraven
519309124Sdiminline
520241900Sdim_LIBCPP_CONSTEXPR
521227825Stheravencomplex<long double>::complex(const complex<float>& __c)
522227825Stheraven    : __re_(__c.real()), __im_(__c.imag()) {}
523227825Stheraven
524309124Sdiminline
525241900Sdim_LIBCPP_CONSTEXPR
526227825Stheravencomplex<long double>::complex(const complex<double>& __c)
527227825Stheraven    : __re_(__c.real()), __im_(__c.imag()) {}
528227825Stheraven
529227825Stheraven// 26.3.6 operators:
530227825Stheraven
531227825Stheraventemplate<class _Tp>
532227825Stheraveninline _LIBCPP_INLINE_VISIBILITY
533227825Stheravencomplex<_Tp>
534227825Stheravenoperator+(const complex<_Tp>& __x, const complex<_Tp>& __y)
535227825Stheraven{
536227825Stheraven    complex<_Tp> __t(__x);
537227825Stheraven    __t += __y;
538227825Stheraven    return __t;
539227825Stheraven}
540227825Stheraven
541227825Stheraventemplate<class _Tp>
542227825Stheraveninline _LIBCPP_INLINE_VISIBILITY
543227825Stheravencomplex<_Tp>
544227825Stheravenoperator+(const complex<_Tp>& __x, const _Tp& __y)
545227825Stheraven{
546227825Stheraven    complex<_Tp> __t(__x);
547227825Stheraven    __t += __y;
548227825Stheraven    return __t;
549227825Stheraven}
550227825Stheraven
551227825Stheraventemplate<class _Tp>
552227825Stheraveninline _LIBCPP_INLINE_VISIBILITY
553227825Stheravencomplex<_Tp>
554227825Stheravenoperator+(const _Tp& __x, const complex<_Tp>& __y)
555227825Stheraven{
556227825Stheraven    complex<_Tp> __t(__y);
557227825Stheraven    __t += __x;
558227825Stheraven    return __t;
559227825Stheraven}
560227825Stheraven
561227825Stheraventemplate<class _Tp>
562227825Stheraveninline _LIBCPP_INLINE_VISIBILITY
563227825Stheravencomplex<_Tp>
564227825Stheravenoperator-(const complex<_Tp>& __x, const complex<_Tp>& __y)
565227825Stheraven{
566227825Stheraven    complex<_Tp> __t(__x);
567227825Stheraven    __t -= __y;
568227825Stheraven    return __t;
569227825Stheraven}
570227825Stheraven
571227825Stheraventemplate<class _Tp>
572227825Stheraveninline _LIBCPP_INLINE_VISIBILITY
573227825Stheravencomplex<_Tp>
574227825Stheravenoperator-(const complex<_Tp>& __x, const _Tp& __y)
575227825Stheraven{
576227825Stheraven    complex<_Tp> __t(__x);
577227825Stheraven    __t -= __y;
578227825Stheraven    return __t;
579227825Stheraven}
580227825Stheraven
581227825Stheraventemplate<class _Tp>
582227825Stheraveninline _LIBCPP_INLINE_VISIBILITY
583227825Stheravencomplex<_Tp>
584227825Stheravenoperator-(const _Tp& __x, const complex<_Tp>& __y)
585227825Stheraven{
586227825Stheraven    complex<_Tp> __t(-__y);
587227825Stheraven    __t += __x;
588227825Stheraven    return __t;
589227825Stheraven}
590227825Stheraven
591227825Stheraventemplate<class _Tp>
592227825Stheravencomplex<_Tp>
593227825Stheravenoperator*(const complex<_Tp>& __z, const complex<_Tp>& __w)
594227825Stheraven{
595227825Stheraven    _Tp __a = __z.real();
596227825Stheraven    _Tp __b = __z.imag();
597227825Stheraven    _Tp __c = __w.real();
598227825Stheraven    _Tp __d = __w.imag();
599227825Stheraven    _Tp __ac = __a * __c;
600227825Stheraven    _Tp __bd = __b * __d;
601227825Stheraven    _Tp __ad = __a * __d;
602227825Stheraven    _Tp __bc = __b * __c;
603227825Stheraven    _Tp __x = __ac - __bd;
604227825Stheraven    _Tp __y = __ad + __bc;
605227825Stheraven    if (isnan(__x) && isnan(__y))
606227825Stheraven    {
607227825Stheraven        bool __recalc = false;
608227825Stheraven        if (isinf(__a) || isinf(__b))
609227825Stheraven        {
610227825Stheraven            __a = copysign(isinf(__a) ? _Tp(1) : _Tp(0), __a);
611227825Stheraven            __b = copysign(isinf(__b) ? _Tp(1) : _Tp(0), __b);
612227825Stheraven            if (isnan(__c))
613227825Stheraven                __c = copysign(_Tp(0), __c);
614227825Stheraven            if (isnan(__d))
615227825Stheraven                __d = copysign(_Tp(0), __d);
616227825Stheraven            __recalc = true;
617227825Stheraven        }
618227825Stheraven        if (isinf(__c) || isinf(__d))
619227825Stheraven        {
620227825Stheraven            __c = copysign(isinf(__c) ? _Tp(1) : _Tp(0), __c);
621227825Stheraven            __d = copysign(isinf(__d) ? _Tp(1) : _Tp(0), __d);
622227825Stheraven            if (isnan(__a))
623227825Stheraven                __a = copysign(_Tp(0), __a);
624227825Stheraven            if (isnan(__b))
625227825Stheraven                __b = copysign(_Tp(0), __b);
626227825Stheraven            __recalc = true;
627227825Stheraven        }
628227825Stheraven        if (!__recalc && (isinf(__ac) || isinf(__bd) ||
629227825Stheraven                          isinf(__ad) || isinf(__bc)))
630227825Stheraven        {
631227825Stheraven            if (isnan(__a))
632227825Stheraven                __a = copysign(_Tp(0), __a);
633227825Stheraven            if (isnan(__b))
634227825Stheraven                __b = copysign(_Tp(0), __b);
635227825Stheraven            if (isnan(__c))
636227825Stheraven                __c = copysign(_Tp(0), __c);
637227825Stheraven            if (isnan(__d))
638227825Stheraven                __d = copysign(_Tp(0), __d);
639227825Stheraven            __recalc = true;
640227825Stheraven        }
641227825Stheraven        if (__recalc)
642227825Stheraven        {
643227825Stheraven            __x = _Tp(INFINITY) * (__a * __c - __b * __d);
644227825Stheraven            __y = _Tp(INFINITY) * (__a * __d + __b * __c);
645227825Stheraven        }
646227825Stheraven    }
647227825Stheraven    return complex<_Tp>(__x, __y);
648227825Stheraven}
649227825Stheraven
650227825Stheraventemplate<class _Tp>
651227825Stheraveninline _LIBCPP_INLINE_VISIBILITY
652227825Stheravencomplex<_Tp>
653227825Stheravenoperator*(const complex<_Tp>& __x, const _Tp& __y)
654227825Stheraven{
655227825Stheraven    complex<_Tp> __t(__x);
656227825Stheraven    __t *= __y;
657227825Stheraven    return __t;
658227825Stheraven}
659227825Stheraven
660227825Stheraventemplate<class _Tp>
661227825Stheraveninline _LIBCPP_INLINE_VISIBILITY
662227825Stheravencomplex<_Tp>
663227825Stheravenoperator*(const _Tp& __x, const complex<_Tp>& __y)
664227825Stheraven{
665227825Stheraven    complex<_Tp> __t(__y);
666227825Stheraven    __t *= __x;
667227825Stheraven    return __t;
668227825Stheraven}
669227825Stheraven
670227825Stheraventemplate<class _Tp>
671227825Stheravencomplex<_Tp>
672227825Stheravenoperator/(const complex<_Tp>& __z, const complex<_Tp>& __w)
673227825Stheraven{
674227825Stheraven    int __ilogbw = 0;
675227825Stheraven    _Tp __a = __z.real();
676227825Stheraven    _Tp __b = __z.imag();
677227825Stheraven    _Tp __c = __w.real();
678227825Stheraven    _Tp __d = __w.imag();
679227825Stheraven    _Tp __logbw = logb(fmax(fabs(__c), fabs(__d)));
680227825Stheraven    if (isfinite(__logbw))
681227825Stheraven    {
682227825Stheraven        __ilogbw = static_cast<int>(__logbw);
683227825Stheraven        __c = scalbn(__c, -__ilogbw);
684227825Stheraven        __d = scalbn(__d, -__ilogbw);
685227825Stheraven    }
686227825Stheraven    _Tp __denom = __c * __c + __d * __d;
687227825Stheraven    _Tp __x = scalbn((__a * __c + __b * __d) / __denom, -__ilogbw);
688227825Stheraven    _Tp __y = scalbn((__b * __c - __a * __d) / __denom, -__ilogbw);
689227825Stheraven    if (isnan(__x) && isnan(__y))
690227825Stheraven    {
691227825Stheraven        if ((__denom == _Tp(0)) && (!isnan(__a) || !isnan(__b)))
692227825Stheraven        {
693227825Stheraven            __x = copysign(_Tp(INFINITY), __c) * __a;
694227825Stheraven            __y = copysign(_Tp(INFINITY), __c) * __b;
695227825Stheraven        }
696227825Stheraven        else if ((isinf(__a) || isinf(__b)) && isfinite(__c) && isfinite(__d))
697227825Stheraven        {
698227825Stheraven            __a = copysign(isinf(__a) ? _Tp(1) : _Tp(0), __a);
699227825Stheraven            __b = copysign(isinf(__b) ? _Tp(1) : _Tp(0), __b);
700227825Stheraven            __x = _Tp(INFINITY) * (__a * __c + __b * __d);
701227825Stheraven            __y = _Tp(INFINITY) * (__b * __c - __a * __d);
702227825Stheraven        }
703227825Stheraven        else if (isinf(__logbw) && __logbw > _Tp(0) && isfinite(__a) && isfinite(__b))
704227825Stheraven        {
705227825Stheraven            __c = copysign(isinf(__c) ? _Tp(1) : _Tp(0), __c);
706227825Stheraven            __d = copysign(isinf(__d) ? _Tp(1) : _Tp(0), __d);
707227825Stheraven            __x = _Tp(0) * (__a * __c + __b * __d);
708227825Stheraven            __y = _Tp(0) * (__b * __c - __a * __d);
709227825Stheraven        }
710227825Stheraven    }
711227825Stheraven    return complex<_Tp>(__x, __y);
712227825Stheraven}
713227825Stheraven
714227825Stheraventemplate<class _Tp>
715227825Stheraveninline _LIBCPP_INLINE_VISIBILITY
716227825Stheravencomplex<_Tp>
717227825Stheravenoperator/(const complex<_Tp>& __x, const _Tp& __y)
718227825Stheraven{
719227825Stheraven    return complex<_Tp>(__x.real() / __y, __x.imag() / __y);
720227825Stheraven}
721227825Stheraven
722227825Stheraventemplate<class _Tp>
723227825Stheraveninline _LIBCPP_INLINE_VISIBILITY
724227825Stheravencomplex<_Tp>
725227825Stheravenoperator/(const _Tp& __x, const complex<_Tp>& __y)
726227825Stheraven{
727227825Stheraven    complex<_Tp> __t(__x);
728227825Stheraven    __t /= __y;
729227825Stheraven    return __t;
730227825Stheraven}
731227825Stheraven
732227825Stheraventemplate<class _Tp>
733227825Stheraveninline _LIBCPP_INLINE_VISIBILITY
734227825Stheravencomplex<_Tp>
735227825Stheravenoperator+(const complex<_Tp>& __x)
736227825Stheraven{
737227825Stheraven    return __x;
738227825Stheraven}
739227825Stheraven
740227825Stheraventemplate<class _Tp>
741227825Stheraveninline _LIBCPP_INLINE_VISIBILITY
742227825Stheravencomplex<_Tp>
743227825Stheravenoperator-(const complex<_Tp>& __x)
744227825Stheraven{
745227825Stheraven    return complex<_Tp>(-__x.real(), -__x.imag());
746227825Stheraven}
747227825Stheraven
748227825Stheraventemplate<class _Tp>
749261272Sdiminline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
750227825Stheravenbool
751227825Stheravenoperator==(const complex<_Tp>& __x, const complex<_Tp>& __y)
752227825Stheraven{
753227825Stheraven    return __x.real() == __y.real() && __x.imag() == __y.imag();
754227825Stheraven}
755227825Stheraven
756227825Stheraventemplate<class _Tp>
757261272Sdiminline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
758227825Stheravenbool
759227825Stheravenoperator==(const complex<_Tp>& __x, const _Tp& __y)
760227825Stheraven{
761227825Stheraven    return __x.real() == __y && __x.imag() == 0;
762227825Stheraven}
763227825Stheraven
764227825Stheraventemplate<class _Tp>
765261272Sdiminline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
766227825Stheravenbool
767227825Stheravenoperator==(const _Tp& __x, const complex<_Tp>& __y)
768227825Stheraven{
769227825Stheraven    return __x == __y.real() && 0 == __y.imag();
770227825Stheraven}
771227825Stheraven
772227825Stheraventemplate<class _Tp>
773261272Sdiminline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
774227825Stheravenbool
775227825Stheravenoperator!=(const complex<_Tp>& __x, const complex<_Tp>& __y)
776227825Stheraven{
777227825Stheraven    return !(__x == __y);
778227825Stheraven}
779227825Stheraven
780227825Stheraventemplate<class _Tp>
781261272Sdiminline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
782227825Stheravenbool
783227825Stheravenoperator!=(const complex<_Tp>& __x, const _Tp& __y)
784227825Stheraven{
785227825Stheraven    return !(__x == __y);
786227825Stheraven}
787227825Stheraven
788227825Stheraventemplate<class _Tp>
789261272Sdiminline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
790227825Stheravenbool
791227825Stheravenoperator!=(const _Tp& __x, const complex<_Tp>& __y)
792227825Stheraven{
793227825Stheraven    return !(__x == __y);
794227825Stheraven}
795227825Stheraven
796227825Stheraven// 26.3.7 values:
797227825Stheraven
798227825Stheraven// real
799227825Stheraven
800227825Stheraventemplate<class _Tp>
801261272Sdiminline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
802227825Stheraven_Tp
803227825Stheravenreal(const complex<_Tp>& __c)
804227825Stheraven{
805227825Stheraven    return __c.real();
806227825Stheraven}
807227825Stheraven
808261272Sdiminline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
809227825Stheravenlong double
810227825Stheravenreal(long double __re)
811227825Stheraven{
812227825Stheraven    return __re;
813227825Stheraven}
814227825Stheraven
815261272Sdiminline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
816227825Stheravendouble
817227825Stheravenreal(double __re)
818227825Stheraven{
819227825Stheraven    return __re;
820227825Stheraven}
821227825Stheraven
822227825Stheraventemplate<class _Tp>
823261272Sdiminline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
824227825Stheraventypename enable_if
825227825Stheraven<
826227825Stheraven    is_integral<_Tp>::value,
827227825Stheraven    double
828227825Stheraven>::type
829227825Stheravenreal(_Tp  __re)
830227825Stheraven{
831227825Stheraven    return __re;
832227825Stheraven}
833227825Stheraven
834261272Sdiminline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
835227825Stheravenfloat
836227825Stheravenreal(float  __re)
837227825Stheraven{
838227825Stheraven    return __re;
839227825Stheraven}
840227825Stheraven
841227825Stheraven// imag
842227825Stheraven
843227825Stheraventemplate<class _Tp>
844261272Sdiminline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
845227825Stheraven_Tp
846227825Stheravenimag(const complex<_Tp>& __c)
847227825Stheraven{
848227825Stheraven    return __c.imag();
849227825Stheraven}
850227825Stheraven
851261272Sdiminline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
852227825Stheravenlong double
853227825Stheravenimag(long double __re)
854227825Stheraven{
855227825Stheraven    return 0;
856227825Stheraven}
857227825Stheraven
858261272Sdiminline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
859227825Stheravendouble
860227825Stheravenimag(double __re)
861227825Stheraven{
862227825Stheraven    return 0;
863227825Stheraven}
864227825Stheraven
865227825Stheraventemplate<class _Tp>
866261272Sdiminline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
867227825Stheraventypename enable_if
868227825Stheraven<
869227825Stheraven    is_integral<_Tp>::value,
870227825Stheraven    double
871227825Stheraven>::type
872227825Stheravenimag(_Tp  __re)
873227825Stheraven{
874227825Stheraven    return 0;
875227825Stheraven}
876227825Stheraven
877261272Sdiminline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
878227825Stheravenfloat
879227825Stheravenimag(float  __re)
880227825Stheraven{
881227825Stheraven    return 0;
882227825Stheraven}
883227825Stheraven
884227825Stheraven// abs
885227825Stheraven
886227825Stheraventemplate<class _Tp>
887227825Stheraveninline _LIBCPP_INLINE_VISIBILITY
888227825Stheraven_Tp
889227825Stheravenabs(const complex<_Tp>& __c)
890227825Stheraven{
891227825Stheraven    return hypot(__c.real(), __c.imag());
892227825Stheraven}
893227825Stheraven
894227825Stheraven// arg
895227825Stheraven
896227825Stheraventemplate<class _Tp>
897227825Stheraveninline _LIBCPP_INLINE_VISIBILITY
898227825Stheraven_Tp
899227825Stheravenarg(const complex<_Tp>& __c)
900227825Stheraven{
901227825Stheraven    return atan2(__c.imag(), __c.real());
902227825Stheraven}
903227825Stheraven
904227825Stheraveninline _LIBCPP_INLINE_VISIBILITY
905227825Stheravenlong double
906227825Stheravenarg(long double __re)
907227825Stheraven{
908227825Stheraven    return atan2l(0.L, __re);
909227825Stheraven}
910227825Stheraven
911227825Stheraveninline _LIBCPP_INLINE_VISIBILITY
912227825Stheravendouble
913227825Stheravenarg(double __re)
914227825Stheraven{
915227825Stheraven    return atan2(0., __re);
916227825Stheraven}
917227825Stheraven
918227825Stheraventemplate<class _Tp>
919227825Stheraveninline _LIBCPP_INLINE_VISIBILITY
920227825Stheraventypename enable_if
921227825Stheraven<
922227825Stheraven    is_integral<_Tp>::value,
923227825Stheraven    double
924227825Stheraven>::type
925227825Stheravenarg(_Tp __re)
926227825Stheraven{
927227825Stheraven    return atan2(0., __re);
928227825Stheraven}
929227825Stheraven
930227825Stheraveninline _LIBCPP_INLINE_VISIBILITY
931227825Stheravenfloat
932227825Stheravenarg(float __re)
933227825Stheraven{
934227825Stheraven    return atan2f(0.F, __re);
935227825Stheraven}
936227825Stheraven
937227825Stheraven// norm
938227825Stheraven
939227825Stheraventemplate<class _Tp>
940227825Stheraveninline _LIBCPP_INLINE_VISIBILITY
941227825Stheraven_Tp
942227825Stheravennorm(const complex<_Tp>& __c)
943227825Stheraven{
944227825Stheraven    if (isinf(__c.real()))
945227825Stheraven        return abs(__c.real());
946227825Stheraven    if (isinf(__c.imag()))
947227825Stheraven        return abs(__c.imag());
948227825Stheraven    return __c.real() * __c.real() + __c.imag() * __c.imag();
949227825Stheraven}
950227825Stheraven
951227825Stheraveninline _LIBCPP_INLINE_VISIBILITY
952227825Stheravenlong double
953227825Stheravennorm(long double __re)
954227825Stheraven{
955227825Stheraven    return __re * __re;
956227825Stheraven}
957227825Stheraven
958227825Stheraveninline _LIBCPP_INLINE_VISIBILITY
959227825Stheravendouble
960227825Stheravennorm(double __re)
961227825Stheraven{
962227825Stheraven    return __re * __re;
963227825Stheraven}
964227825Stheraven
965227825Stheraventemplate<class _Tp>
966227825Stheraveninline _LIBCPP_INLINE_VISIBILITY
967227825Stheraventypename enable_if
968227825Stheraven<
969227825Stheraven    is_integral<_Tp>::value,
970227825Stheraven    double
971227825Stheraven>::type
972227825Stheravennorm(_Tp __re)
973227825Stheraven{
974227825Stheraven    return (double)__re * __re;
975227825Stheraven}
976227825Stheraven
977227825Stheraveninline _LIBCPP_INLINE_VISIBILITY
978227825Stheravenfloat
979227825Stheravennorm(float __re)
980227825Stheraven{
981227825Stheraven    return __re * __re;
982227825Stheraven}
983227825Stheraven
984227825Stheraven// conj
985227825Stheraven
986227825Stheraventemplate<class _Tp>
987227825Stheraveninline _LIBCPP_INLINE_VISIBILITY
988227825Stheravencomplex<_Tp>
989227825Stheravenconj(const complex<_Tp>& __c)
990227825Stheraven{
991227825Stheraven    return complex<_Tp>(__c.real(), -__c.imag());
992227825Stheraven}
993227825Stheraven
994227825Stheraveninline _LIBCPP_INLINE_VISIBILITY
995227825Stheravencomplex<long double>
996227825Stheravenconj(long double __re)
997227825Stheraven{
998227825Stheraven    return complex<long double>(__re);
999227825Stheraven}
1000227825Stheraven
1001227825Stheraveninline _LIBCPP_INLINE_VISIBILITY
1002227825Stheravencomplex<double>
1003227825Stheravenconj(double __re)
1004227825Stheraven{
1005227825Stheraven    return complex<double>(__re);
1006227825Stheraven}
1007227825Stheraven
1008227825Stheraventemplate<class _Tp>
1009227825Stheraveninline _LIBCPP_INLINE_VISIBILITY
1010227825Stheraventypename enable_if
1011227825Stheraven<
1012227825Stheraven    is_integral<_Tp>::value,
1013227825Stheraven    complex<double>
1014227825Stheraven>::type
1015227825Stheravenconj(_Tp __re)
1016227825Stheraven{
1017227825Stheraven    return complex<double>(__re);
1018227825Stheraven}
1019227825Stheraven
1020227825Stheraveninline _LIBCPP_INLINE_VISIBILITY
1021227825Stheravencomplex<float>
1022227825Stheravenconj(float __re)
1023227825Stheraven{
1024227825Stheraven    return complex<float>(__re);
1025227825Stheraven}
1026227825Stheraven
1027227825Stheraven// proj
1028227825Stheraven
1029227825Stheraventemplate<class _Tp>
1030227825Stheraveninline _LIBCPP_INLINE_VISIBILITY
1031227825Stheravencomplex<_Tp>
1032227825Stheravenproj(const complex<_Tp>& __c)
1033227825Stheraven{
1034227825Stheraven    std::complex<_Tp> __r = __c;
1035227825Stheraven    if (isinf(__c.real()) || isinf(__c.imag()))
1036227825Stheraven        __r = complex<_Tp>(INFINITY, copysign(_Tp(0), __c.imag()));
1037227825Stheraven    return __r;
1038227825Stheraven}
1039227825Stheraven
1040227825Stheraveninline _LIBCPP_INLINE_VISIBILITY
1041227825Stheravencomplex<long double>
1042227825Stheravenproj(long double __re)
1043227825Stheraven{
1044227825Stheraven    if (isinf(__re))
1045227825Stheraven        __re = abs(__re);
1046227825Stheraven    return complex<long double>(__re);
1047227825Stheraven}
1048227825Stheraven
1049227825Stheraveninline _LIBCPP_INLINE_VISIBILITY
1050227825Stheravencomplex<double>
1051227825Stheravenproj(double __re)
1052227825Stheraven{
1053227825Stheraven    if (isinf(__re))
1054227825Stheraven        __re = abs(__re);
1055227825Stheraven    return complex<double>(__re);
1056227825Stheraven}
1057227825Stheraven
1058227825Stheraventemplate<class _Tp>
1059227825Stheraveninline _LIBCPP_INLINE_VISIBILITY
1060227825Stheraventypename enable_if
1061227825Stheraven<
1062227825Stheraven    is_integral<_Tp>::value,
1063227825Stheraven    complex<double>
1064227825Stheraven>::type
1065227825Stheravenproj(_Tp __re)
1066227825Stheraven{
1067227825Stheraven    return complex<double>(__re);
1068227825Stheraven}
1069227825Stheraven
1070227825Stheraveninline _LIBCPP_INLINE_VISIBILITY
1071227825Stheravencomplex<float>
1072227825Stheravenproj(float __re)
1073227825Stheraven{
1074227825Stheraven    if (isinf(__re))
1075227825Stheraven        __re = abs(__re);
1076227825Stheraven    return complex<float>(__re);
1077227825Stheraven}
1078227825Stheraven
1079227825Stheraven// polar
1080227825Stheraven
1081227825Stheraventemplate<class _Tp>
1082227825Stheravencomplex<_Tp>
1083227825Stheravenpolar(const _Tp& __rho, const _Tp& __theta = _Tp(0))
1084227825Stheraven{
1085227825Stheraven    if (isnan(__rho) || signbit(__rho))
1086227825Stheraven        return complex<_Tp>(_Tp(NAN), _Tp(NAN));
1087227825Stheraven    if (isnan(__theta))
1088227825Stheraven    {
1089227825Stheraven        if (isinf(__rho))
1090227825Stheraven            return complex<_Tp>(__rho, __theta);
1091227825Stheraven        return complex<_Tp>(__theta, __theta);
1092227825Stheraven    }
1093227825Stheraven    if (isinf(__theta))
1094227825Stheraven    {
1095227825Stheraven        if (isinf(__rho))
1096227825Stheraven            return complex<_Tp>(__rho, _Tp(NAN));
1097227825Stheraven        return complex<_Tp>(_Tp(NAN), _Tp(NAN));
1098227825Stheraven    }
1099227825Stheraven    _Tp __x = __rho * cos(__theta);
1100227825Stheraven    if (isnan(__x))
1101227825Stheraven        __x = 0;
1102227825Stheraven    _Tp __y = __rho * sin(__theta);
1103227825Stheraven    if (isnan(__y))
1104227825Stheraven        __y = 0;
1105227825Stheraven    return complex<_Tp>(__x, __y);
1106227825Stheraven}
1107227825Stheraven
1108227825Stheraven// log
1109227825Stheraven
1110227825Stheraventemplate<class _Tp>
1111227825Stheraveninline _LIBCPP_INLINE_VISIBILITY
1112227825Stheravencomplex<_Tp>
1113227825Stheravenlog(const complex<_Tp>& __x)
1114227825Stheraven{
1115227825Stheraven    return complex<_Tp>(log(abs(__x)), arg(__x));
1116227825Stheraven}
1117227825Stheraven
1118227825Stheraven// log10
1119227825Stheraven
1120227825Stheraventemplate<class _Tp>
1121227825Stheraveninline _LIBCPP_INLINE_VISIBILITY
1122227825Stheravencomplex<_Tp>
1123227825Stheravenlog10(const complex<_Tp>& __x)
1124227825Stheraven{
1125227825Stheraven    return log(__x) / log(_Tp(10));
1126227825Stheraven}
1127227825Stheraven
1128227825Stheraven// sqrt
1129227825Stheraven
1130227825Stheraventemplate<class _Tp>
1131227825Stheravencomplex<_Tp>
1132227825Stheravensqrt(const complex<_Tp>& __x)
1133227825Stheraven{
1134227825Stheraven    if (isinf(__x.imag()))
1135227825Stheraven        return complex<_Tp>(_Tp(INFINITY), __x.imag());
1136227825Stheraven    if (isinf(__x.real()))
1137227825Stheraven    {
1138227825Stheraven        if (__x.real() > _Tp(0))
1139227825Stheraven            return complex<_Tp>(__x.real(), isnan(__x.imag()) ? __x.imag() : copysign(_Tp(0), __x.imag()));
1140227825Stheraven        return complex<_Tp>(isnan(__x.imag()) ? __x.imag() : _Tp(0), copysign(__x.real(), __x.imag()));
1141227825Stheraven    }
1142227825Stheraven    return polar(sqrt(abs(__x)), arg(__x) / _Tp(2));
1143227825Stheraven}
1144227825Stheraven
1145227825Stheraven// exp
1146227825Stheraven
1147227825Stheraventemplate<class _Tp>
1148227825Stheravencomplex<_Tp>
1149227825Stheravenexp(const complex<_Tp>& __x)
1150227825Stheraven{
1151227825Stheraven    _Tp __i = __x.imag();
1152227825Stheraven    if (isinf(__x.real()))
1153227825Stheraven    {
1154227825Stheraven        if (__x.real() < _Tp(0))
1155227825Stheraven        {
1156227825Stheraven            if (!isfinite(__i))
1157227825Stheraven                __i = _Tp(1);
1158227825Stheraven        }
1159227825Stheraven        else if (__i == 0 || !isfinite(__i))
1160227825Stheraven        {
1161227825Stheraven            if (isinf(__i))
1162227825Stheraven                __i = _Tp(NAN);
1163227825Stheraven            return complex<_Tp>(__x.real(), __i);
1164227825Stheraven        }
1165227825Stheraven    }
1166227825Stheraven    else if (isnan(__x.real()) && __x.imag() == 0)
1167227825Stheraven        return __x;
1168227825Stheraven    _Tp __e = exp(__x.real());
1169227825Stheraven    return complex<_Tp>(__e * cos(__i), __e * sin(__i));
1170227825Stheraven}
1171227825Stheraven
1172227825Stheraven// pow
1173227825Stheraven
1174227825Stheraventemplate<class _Tp>
1175227825Stheraveninline _LIBCPP_INLINE_VISIBILITY
1176227825Stheravencomplex<_Tp>
1177227825Stheravenpow(const complex<_Tp>& __x, const complex<_Tp>& __y)
1178227825Stheraven{
1179227825Stheraven    return exp(__y * log(__x));
1180227825Stheraven}
1181227825Stheraven
1182227825Stheraventemplate<class _Tp, class _Up>
1183227825Stheraveninline _LIBCPP_INLINE_VISIBILITY
1184227825Stheravencomplex<typename __promote<_Tp, _Up>::type>
1185227825Stheravenpow(const complex<_Tp>& __x, const complex<_Up>& __y)
1186227825Stheraven{
1187227825Stheraven    typedef complex<typename __promote<_Tp, _Up>::type> result_type;
1188227825Stheraven    return _VSTD::pow(result_type(__x), result_type(__y));
1189227825Stheraven}
1190227825Stheraven
1191227825Stheraventemplate<class _Tp, class _Up>
1192227825Stheraveninline _LIBCPP_INLINE_VISIBILITY
1193227825Stheraventypename enable_if
1194227825Stheraven<
1195227825Stheraven    is_arithmetic<_Up>::value,
1196227825Stheraven    complex<typename __promote<_Tp, _Up>::type>
1197227825Stheraven>::type
1198227825Stheravenpow(const complex<_Tp>& __x, const _Up& __y)
1199227825Stheraven{
1200227825Stheraven    typedef complex<typename __promote<_Tp, _Up>::type> result_type;
1201227825Stheraven    return _VSTD::pow(result_type(__x), result_type(__y));
1202227825Stheraven}
1203227825Stheraven
1204227825Stheraventemplate<class _Tp, class _Up>
1205227825Stheraveninline _LIBCPP_INLINE_VISIBILITY
1206227825Stheraventypename enable_if
1207227825Stheraven<
1208227825Stheraven    is_arithmetic<_Tp>::value,
1209227825Stheraven    complex<typename __promote<_Tp, _Up>::type>
1210227825Stheraven>::type
1211227825Stheravenpow(const _Tp& __x, const complex<_Up>& __y)
1212227825Stheraven{
1213227825Stheraven    typedef complex<typename __promote<_Tp, _Up>::type> result_type;
1214227825Stheraven    return _VSTD::pow(result_type(__x), result_type(__y));
1215227825Stheraven}
1216227825Stheraven
1217227825Stheraven// asinh
1218227825Stheraven
1219227825Stheraventemplate<class _Tp>
1220227825Stheravencomplex<_Tp>
1221227825Stheravenasinh(const complex<_Tp>& __x)
1222227825Stheraven{
1223227825Stheraven    const _Tp __pi(atan2(+0., -0.));
1224227825Stheraven    if (isinf(__x.real()))
1225227825Stheraven    {
1226227825Stheraven        if (isnan(__x.imag()))
1227227825Stheraven            return __x;
1228227825Stheraven        if (isinf(__x.imag()))
1229227825Stheraven            return complex<_Tp>(__x.real(), copysign(__pi * _Tp(0.25), __x.imag()));
1230227825Stheraven        return complex<_Tp>(__x.real(), copysign(_Tp(0), __x.imag()));
1231227825Stheraven    }
1232227825Stheraven    if (isnan(__x.real()))
1233227825Stheraven    {
1234227825Stheraven        if (isinf(__x.imag()))
1235227825Stheraven            return complex<_Tp>(__x.imag(), __x.real());
1236227825Stheraven        if (__x.imag() == 0)
1237227825Stheraven            return __x;
1238227825Stheraven        return complex<_Tp>(__x.real(), __x.real());
1239227825Stheraven    }
1240227825Stheraven    if (isinf(__x.imag()))
1241227825Stheraven        return complex<_Tp>(copysign(__x.imag(), __x.real()), copysign(__pi/_Tp(2), __x.imag()));
1242227825Stheraven    complex<_Tp> __z = log(__x + sqrt(pow(__x, _Tp(2)) + _Tp(1)));
1243227825Stheraven    return complex<_Tp>(copysign(__z.real(), __x.real()), copysign(__z.imag(), __x.imag()));
1244227825Stheraven}
1245227825Stheraven
1246227825Stheraven// acosh
1247227825Stheraven
1248227825Stheraventemplate<class _Tp>
1249227825Stheravencomplex<_Tp>
1250227825Stheravenacosh(const complex<_Tp>& __x)
1251227825Stheraven{
1252227825Stheraven    const _Tp __pi(atan2(+0., -0.));
1253227825Stheraven    if (isinf(__x.real()))
1254227825Stheraven    {
1255227825Stheraven        if (isnan(__x.imag()))
1256227825Stheraven            return complex<_Tp>(abs(__x.real()), __x.imag());
1257227825Stheraven        if (isinf(__x.imag()))
1258242939Stheraven        {
1259227825Stheraven            if (__x.real() > 0)
1260227825Stheraven                return complex<_Tp>(__x.real(), copysign(__pi * _Tp(0.25), __x.imag()));
1261227825Stheraven            else
1262227825Stheraven                return complex<_Tp>(-__x.real(), copysign(__pi * _Tp(0.75), __x.imag()));
1263242939Stheraven        }
1264227825Stheraven        if (__x.real() < 0)
1265227825Stheraven            return complex<_Tp>(-__x.real(), copysign(__pi, __x.imag()));
1266227825Stheraven        return complex<_Tp>(__x.real(), copysign(_Tp(0), __x.imag()));
1267227825Stheraven    }
1268227825Stheraven    if (isnan(__x.real()))
1269227825Stheraven    {
1270227825Stheraven        if (isinf(__x.imag()))
1271227825Stheraven            return complex<_Tp>(abs(__x.imag()), __x.real());
1272227825Stheraven        return complex<_Tp>(__x.real(), __x.real());
1273227825Stheraven    }
1274227825Stheraven    if (isinf(__x.imag()))
1275227825Stheraven        return complex<_Tp>(abs(__x.imag()), copysign(__pi/_Tp(2), __x.imag()));
1276227825Stheraven    complex<_Tp> __z = log(__x + sqrt(pow(__x, _Tp(2)) - _Tp(1)));
1277227825Stheraven    return complex<_Tp>(copysign(__z.real(), _Tp(0)), copysign(__z.imag(), __x.imag()));
1278227825Stheraven}
1279227825Stheraven
1280227825Stheraven// atanh
1281227825Stheraven
1282227825Stheraventemplate<class _Tp>
1283227825Stheravencomplex<_Tp>
1284227825Stheravenatanh(const complex<_Tp>& __x)
1285227825Stheraven{
1286227825Stheraven    const _Tp __pi(atan2(+0., -0.));
1287227825Stheraven    if (isinf(__x.imag()))
1288227825Stheraven    {
1289227825Stheraven        return complex<_Tp>(copysign(_Tp(0), __x.real()), copysign(__pi/_Tp(2), __x.imag()));
1290227825Stheraven    }
1291227825Stheraven    if (isnan(__x.imag()))
1292227825Stheraven    {
1293227825Stheraven        if (isinf(__x.real()) || __x.real() == 0)
1294227825Stheraven            return complex<_Tp>(copysign(_Tp(0), __x.real()), __x.imag());
1295227825Stheraven        return complex<_Tp>(__x.imag(), __x.imag());
1296227825Stheraven    }
1297227825Stheraven    if (isnan(__x.real()))
1298227825Stheraven    {
1299227825Stheraven        return complex<_Tp>(__x.real(), __x.real());
1300227825Stheraven    }
1301227825Stheraven    if (isinf(__x.real()))
1302227825Stheraven    {
1303227825Stheraven        return complex<_Tp>(copysign(_Tp(0), __x.real()), copysign(__pi/_Tp(2), __x.imag()));
1304227825Stheraven    }
1305227825Stheraven    if (abs(__x.real()) == _Tp(1) && __x.imag() == _Tp(0))
1306227825Stheraven    {
1307227825Stheraven        return complex<_Tp>(copysign(_Tp(INFINITY), __x.real()), copysign(_Tp(0), __x.imag()));
1308227825Stheraven    }
1309227825Stheraven    complex<_Tp> __z = log((_Tp(1) + __x) / (_Tp(1) - __x)) / _Tp(2);
1310227825Stheraven    return complex<_Tp>(copysign(__z.real(), __x.real()), copysign(__z.imag(), __x.imag()));
1311227825Stheraven}
1312227825Stheraven
1313227825Stheraven// sinh
1314227825Stheraven
1315227825Stheraventemplate<class _Tp>
1316227825Stheravencomplex<_Tp>
1317227825Stheravensinh(const complex<_Tp>& __x)
1318227825Stheraven{
1319227825Stheraven    if (isinf(__x.real()) && !isfinite(__x.imag()))
1320227825Stheraven        return complex<_Tp>(__x.real(), _Tp(NAN));
1321227825Stheraven    if (__x.real() == 0 && !isfinite(__x.imag()))
1322227825Stheraven        return complex<_Tp>(__x.real(), _Tp(NAN));
1323227825Stheraven    if (__x.imag() == 0 && !isfinite(__x.real()))
1324227825Stheraven        return __x;
1325227825Stheraven    return complex<_Tp>(sinh(__x.real()) * cos(__x.imag()), cosh(__x.real()) * sin(__x.imag()));
1326227825Stheraven}
1327227825Stheraven
1328227825Stheraven// cosh
1329227825Stheraven
1330227825Stheraventemplate<class _Tp>
1331227825Stheravencomplex<_Tp>
1332227825Stheravencosh(const complex<_Tp>& __x)
1333227825Stheraven{
1334227825Stheraven    if (isinf(__x.real()) && !isfinite(__x.imag()))
1335227825Stheraven        return complex<_Tp>(abs(__x.real()), _Tp(NAN));
1336227825Stheraven    if (__x.real() == 0 && !isfinite(__x.imag()))
1337227825Stheraven        return complex<_Tp>(_Tp(NAN), __x.real());
1338227825Stheraven    if (__x.real() == 0 && __x.imag() == 0)
1339227825Stheraven        return complex<_Tp>(_Tp(1), __x.imag());
1340227825Stheraven    if (__x.imag() == 0 && !isfinite(__x.real()))
1341227825Stheraven        return complex<_Tp>(abs(__x.real()), __x.imag());
1342227825Stheraven    return complex<_Tp>(cosh(__x.real()) * cos(__x.imag()), sinh(__x.real()) * sin(__x.imag()));
1343227825Stheraven}
1344227825Stheraven
1345227825Stheraven// tanh
1346227825Stheraven
1347227825Stheraventemplate<class _Tp>
1348227825Stheravencomplex<_Tp>
1349227825Stheraventanh(const complex<_Tp>& __x)
1350227825Stheraven{
1351227825Stheraven    if (isinf(__x.real()))
1352227825Stheraven    {
1353227825Stheraven        if (!isfinite(__x.imag()))
1354227825Stheraven            return complex<_Tp>(_Tp(1), _Tp(0));
1355227825Stheraven        return complex<_Tp>(_Tp(1), copysign(_Tp(0), sin(_Tp(2) * __x.imag())));
1356227825Stheraven    }
1357227825Stheraven    if (isnan(__x.real()) && __x.imag() == 0)
1358227825Stheraven        return __x;
1359227825Stheraven    _Tp __2r(_Tp(2) * __x.real());
1360227825Stheraven    _Tp __2i(_Tp(2) * __x.imag());
1361227825Stheraven    _Tp __d(cosh(__2r) + cos(__2i));
1362241900Sdim    _Tp __2rsh(sinh(__2r));
1363241900Sdim    if (isinf(__2rsh) && isinf(__d))
1364241900Sdim        return complex<_Tp>(__2rsh > _Tp(0) ? _Tp(1) : _Tp(-1),
1365241900Sdim                            __2i > _Tp(0) ? _Tp(0) : _Tp(-0.));
1366241900Sdim    return  complex<_Tp>(__2rsh/__d, sin(__2i)/__d);
1367227825Stheraven}
1368227825Stheraven
1369227825Stheraven// asin
1370227825Stheraven
1371227825Stheraventemplate<class _Tp>
1372227825Stheravencomplex<_Tp>
1373227825Stheravenasin(const complex<_Tp>& __x)
1374227825Stheraven{
1375227825Stheraven    complex<_Tp> __z = asinh(complex<_Tp>(-__x.imag(), __x.real()));
1376227825Stheraven    return complex<_Tp>(__z.imag(), -__z.real());
1377227825Stheraven}
1378227825Stheraven
1379227825Stheraven// acos
1380227825Stheraven
1381227825Stheraventemplate<class _Tp>
1382227825Stheravencomplex<_Tp>
1383227825Stheravenacos(const complex<_Tp>& __x)
1384227825Stheraven{
1385227825Stheraven    const _Tp __pi(atan2(+0., -0.));
1386227825Stheraven    if (isinf(__x.real()))
1387227825Stheraven    {
1388227825Stheraven        if (isnan(__x.imag()))
1389227825Stheraven            return complex<_Tp>(__x.imag(), __x.real());
1390227825Stheraven        if (isinf(__x.imag()))
1391227825Stheraven        {
1392227825Stheraven            if (__x.real() < _Tp(0))
1393227825Stheraven                return complex<_Tp>(_Tp(0.75) * __pi, -__x.imag());
1394227825Stheraven            return complex<_Tp>(_Tp(0.25) * __pi, -__x.imag());
1395227825Stheraven        }
1396227825Stheraven        if (__x.real() < _Tp(0))
1397227825Stheraven            return complex<_Tp>(__pi, signbit(__x.imag()) ? -__x.real() : __x.real());
1398227825Stheraven        return complex<_Tp>(_Tp(0), signbit(__x.imag()) ? __x.real() : -__x.real());
1399227825Stheraven    }
1400227825Stheraven    if (isnan(__x.real()))
1401227825Stheraven    {
1402227825Stheraven        if (isinf(__x.imag()))
1403227825Stheraven            return complex<_Tp>(__x.real(), -__x.imag());
1404227825Stheraven        return complex<_Tp>(__x.real(), __x.real());
1405227825Stheraven    }
1406227825Stheraven    if (isinf(__x.imag()))
1407227825Stheraven        return complex<_Tp>(__pi/_Tp(2), -__x.imag());
1408309124Sdim    if (__x.real() == 0 && (__x.imag() == 0 || isnan(__x.imag())))
1409227825Stheraven        return complex<_Tp>(__pi/_Tp(2), -__x.imag());
1410227825Stheraven    complex<_Tp> __z = log(__x + sqrt(pow(__x, _Tp(2)) - _Tp(1)));
1411227825Stheraven    if (signbit(__x.imag()))
1412227825Stheraven        return complex<_Tp>(abs(__z.imag()), abs(__z.real()));
1413227825Stheraven    return complex<_Tp>(abs(__z.imag()), -abs(__z.real()));
1414227825Stheraven}
1415227825Stheraven
1416227825Stheraven// atan
1417227825Stheraven
1418227825Stheraventemplate<class _Tp>
1419227825Stheravencomplex<_Tp>
1420227825Stheravenatan(const complex<_Tp>& __x)
1421227825Stheraven{
1422227825Stheraven    complex<_Tp> __z = atanh(complex<_Tp>(-__x.imag(), __x.real()));
1423227825Stheraven    return complex<_Tp>(__z.imag(), -__z.real());
1424227825Stheraven}
1425227825Stheraven
1426227825Stheraven// sin
1427227825Stheraven
1428227825Stheraventemplate<class _Tp>
1429227825Stheravencomplex<_Tp>
1430227825Stheravensin(const complex<_Tp>& __x)
1431227825Stheraven{
1432227825Stheraven    complex<_Tp> __z = sinh(complex<_Tp>(-__x.imag(), __x.real()));
1433227825Stheraven    return complex<_Tp>(__z.imag(), -__z.real());
1434227825Stheraven}
1435227825Stheraven
1436227825Stheraven// cos
1437227825Stheraven
1438227825Stheraventemplate<class _Tp>
1439227825Stheraveninline _LIBCPP_INLINE_VISIBILITY
1440227825Stheravencomplex<_Tp>
1441227825Stheravencos(const complex<_Tp>& __x)
1442227825Stheraven{
1443227825Stheraven    return cosh(complex<_Tp>(-__x.imag(), __x.real()));
1444227825Stheraven}
1445227825Stheraven
1446227825Stheraven// tan
1447227825Stheraven
1448227825Stheraventemplate<class _Tp>
1449227825Stheravencomplex<_Tp>
1450227825Stheraventan(const complex<_Tp>& __x)
1451227825Stheraven{
1452227825Stheraven    complex<_Tp> __z = tanh(complex<_Tp>(-__x.imag(), __x.real()));
1453227825Stheraven    return complex<_Tp>(__z.imag(), -__z.real());
1454227825Stheraven}
1455227825Stheraven
1456227825Stheraventemplate<class _Tp, class _CharT, class _Traits>
1457227825Stheravenbasic_istream<_CharT, _Traits>&
1458227825Stheravenoperator>>(basic_istream<_CharT, _Traits>& __is, complex<_Tp>& __x)
1459227825Stheraven{
1460227825Stheraven    if (__is.good())
1461227825Stheraven    {
1462227825Stheraven        ws(__is);
1463227825Stheraven        if (__is.peek() == _CharT('('))
1464227825Stheraven        {
1465227825Stheraven            __is.get();
1466227825Stheraven            _Tp __r;
1467227825Stheraven            __is >> __r;
1468227825Stheraven            if (!__is.fail())
1469227825Stheraven            {
1470227825Stheraven                ws(__is);
1471227825Stheraven                _CharT __c = __is.peek();
1472227825Stheraven                if (__c == _CharT(','))
1473227825Stheraven                {
1474227825Stheraven                    __is.get();
1475227825Stheraven                    _Tp __i;
1476227825Stheraven                    __is >> __i;
1477227825Stheraven                    if (!__is.fail())
1478227825Stheraven                    {
1479227825Stheraven                        ws(__is);
1480227825Stheraven                        __c = __is.peek();
1481227825Stheraven                        if (__c == _CharT(')'))
1482227825Stheraven                        {
1483227825Stheraven                            __is.get();
1484227825Stheraven                            __x = complex<_Tp>(__r, __i);
1485227825Stheraven                        }
1486227825Stheraven                        else
1487227825Stheraven                            __is.setstate(ios_base::failbit);
1488227825Stheraven                    }
1489227825Stheraven                    else
1490227825Stheraven                        __is.setstate(ios_base::failbit);
1491227825Stheraven                }
1492227825Stheraven                else if (__c == _CharT(')'))
1493227825Stheraven                {
1494227825Stheraven                    __is.get();
1495227825Stheraven                    __x = complex<_Tp>(__r, _Tp(0));
1496227825Stheraven                }
1497227825Stheraven                else
1498227825Stheraven                    __is.setstate(ios_base::failbit);
1499227825Stheraven            }
1500227825Stheraven            else
1501227825Stheraven                __is.setstate(ios_base::failbit);
1502227825Stheraven        }
1503227825Stheraven        else
1504227825Stheraven        {
1505227825Stheraven            _Tp __r;
1506227825Stheraven            __is >> __r;
1507227825Stheraven            if (!__is.fail())
1508227825Stheraven                __x = complex<_Tp>(__r, _Tp(0));
1509227825Stheraven            else
1510227825Stheraven                __is.setstate(ios_base::failbit);
1511227825Stheraven        }
1512227825Stheraven    }
1513227825Stheraven    else
1514227825Stheraven        __is.setstate(ios_base::failbit);
1515227825Stheraven    return __is;
1516227825Stheraven}
1517227825Stheraven
1518227825Stheraventemplate<class _Tp, class _CharT, class _Traits>
1519227825Stheravenbasic_ostream<_CharT, _Traits>&
1520227825Stheravenoperator<<(basic_ostream<_CharT, _Traits>& __os, const complex<_Tp>& __x)
1521227825Stheraven{
1522227825Stheraven    basic_ostringstream<_CharT, _Traits> __s;
1523227825Stheraven    __s.flags(__os.flags());
1524227825Stheraven    __s.imbue(__os.getloc());
1525227825Stheraven    __s.precision(__os.precision());
1526227825Stheraven    __s << '(' << __x.real() << ',' << __x.imag() << ')';
1527227825Stheraven    return __os << __s.str();
1528227825Stheraven}
1529227825Stheraven
1530261272Sdim#if _LIBCPP_STD_VER > 11 
1531261272Sdim// Literal suffix for complex number literals [complex.literals]
1532261272Sdiminline namespace literals
1533261272Sdim{ 
1534261272Sdim  inline namespace complex_literals
1535261272Sdim  {
1536261272Sdim    constexpr complex<long double> operator""il(long double __im)
1537261272Sdim    {
1538261272Sdim        return { 0.0l, __im };
1539261272Sdim    }
1540261272Sdim
1541261272Sdim    constexpr complex<long double> operator""il(unsigned long long __im)
1542261272Sdim    {
1543261272Sdim        return { 0.0l, static_cast<long double>(__im) };
1544261272Sdim    }
1545261272Sdim
1546261272Sdim
1547261272Sdim    constexpr complex<double> operator""i(long double __im)
1548261272Sdim    {
1549261272Sdim        return { 0.0, static_cast<double>(__im) };
1550261272Sdim    }
1551261272Sdim
1552261272Sdim    constexpr complex<double> operator""i(unsigned long long __im)
1553261272Sdim    {
1554261272Sdim        return { 0.0, static_cast<double>(__im) };
1555261272Sdim    }
1556261272Sdim
1557261272Sdim
1558261272Sdim    constexpr complex<float> operator""if(long double __im)
1559261272Sdim    {
1560261272Sdim        return { 0.0f, static_cast<float>(__im) };
1561261272Sdim    }
1562261272Sdim
1563261272Sdim    constexpr complex<float> operator""if(unsigned long long __im)
1564261272Sdim    {
1565261272Sdim        return { 0.0f, static_cast<float>(__im) };
1566261272Sdim    }
1567261272Sdim  }
1568261272Sdim}
1569261272Sdim#endif
1570261272Sdim
1571227825Stheraven_LIBCPP_END_NAMESPACE_STD
1572227825Stheraven
1573227825Stheraven#endif  // _LIBCPP_COMPLEX
1574