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