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