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