complex revision 227825
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
17namespace std
18{
19
20template<class T>
21class complex
22{
23public:
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
50template<>
51class complex<float>
52{
53public:
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
79template<>
80class complex<double>
81{
82public:
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
108template<>
109class complex<long double>
110{
111public:
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:
138template<class T> complex<T> operator+(const complex<T>&, const complex<T>&);
139template<class T> complex<T> operator+(const complex<T>&, const T&);
140template<class T> complex<T> operator+(const T&, const complex<T>&);
141template<class T> complex<T> operator-(const complex<T>&, const complex<T>&);
142template<class T> complex<T> operator-(const complex<T>&, const T&);
143template<class T> complex<T> operator-(const T&, const complex<T>&);
144template<class T> complex<T> operator*(const complex<T>&, const complex<T>&);
145template<class T> complex<T> operator*(const complex<T>&, const T&);
146template<class T> complex<T> operator*(const T&, const complex<T>&);
147template<class T> complex<T> operator/(const complex<T>&, const complex<T>&);
148template<class T> complex<T> operator/(const complex<T>&, const T&);
149template<class T> complex<T> operator/(const T&, const complex<T>&);
150template<class T> complex<T> operator+(const complex<T>&);
151template<class T> complex<T> operator-(const complex<T>&);
152template<class T> bool operator==(const complex<T>&, const complex<T>&);
153template<class T> bool operator==(const complex<T>&, const T&);
154template<class T> bool operator==(const T&, const complex<T>&);
155template<class T> bool operator!=(const complex<T>&, const complex<T>&);
156template<class T> bool operator!=(const complex<T>&, const T&);
157template<class T> bool operator!=(const T&, const complex<T>&);
158
159template<class T, class charT, class traits>
160  basic_istream<charT, traits>&
161  operator>>(basic_istream<charT, traits>&, complex<T>&);
162template<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
168template<class T>              T real(const complex<T>&);
169                     long double real(long double);
170                          double real(double);
171template<Integral T>      double real(T);
172                          float  real(float);
173
174template<class T>              T imag(const complex<T>&);
175                     long double imag(long double);
176                          double imag(double);
177template<Integral T>      double imag(T);
178                          float  imag(float);
179
180template<class T> T abs(const complex<T>&);
181
182template<class T>              T arg(const complex<T>&);
183                     long double arg(long double);
184                          double arg(double);
185template<Integral T>      double arg(T);
186                          float  arg(float);
187
188template<class T>              T norm(const complex<T>&);
189                     long double norm(long double);
190                          double norm(double);
191template<Integral T>      double norm(T);
192                          float  norm(float);
193
194template<class T>      complex<T>           conj(const complex<T>&);
195                       complex<long double> conj(long double);
196                       complex<double>      conj(double);
197template<Integral T>   complex<double>      conj(T);
198                       complex<float>       conj(float);
199
200template<class T>    complex<T>           proj(const complex<T>&);
201                     complex<long double> proj(long double);
202                     complex<double>      proj(double);
203template<Integral T> complex<double>      proj(T);
204                     complex<float>       proj(float);
205
206template<class T> complex<T> polar(const T&, const T& = 0);
207
208// 26.3.8 transcendentals:
209template<class T> complex<T> acos(const complex<T>&);
210template<class T> complex<T> asin(const complex<T>&);
211template<class T> complex<T> atan(const complex<T>&);
212template<class T> complex<T> acosh(const complex<T>&);
213template<class T> complex<T> asinh(const complex<T>&);
214template<class T> complex<T> atanh(const complex<T>&);
215template<class T> complex<T> cos (const complex<T>&);
216template<class T> complex<T> cosh (const complex<T>&);
217template<class T> complex<T> exp (const complex<T>&);
218template<class T> complex<T> log (const complex<T>&);
219template<class T> complex<T> log10(const complex<T>&);
220
221template<class T> complex<T> pow(const complex<T>&, const T&);
222template<class T> complex<T> pow(const complex<T>&, const complex<T>&);
223template<class T> complex<T> pow(const T&, const complex<T>&);
224
225template<class T> complex<T> sin (const complex<T>&);
226template<class T> complex<T> sinh (const complex<T>&);
227template<class T> complex<T> sqrt (const complex<T>&);
228template<class T> complex<T> tan (const complex<T>&);
229template<class T> complex<T> tanh (const complex<T>&);
230
231template<class T, class charT, class traits>
232  basic_istream<charT, traits>&
233  operator>>(basic_istream<charT, traits>& is, complex<T>& x);
234
235template<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
258template<class _Tp> class _LIBCPP_VISIBLE complex;
259
260template<class _Tp> complex<_Tp> operator*(const complex<_Tp>& __z, const complex<_Tp>& __w);
261template<class _Tp> complex<_Tp> operator/(const complex<_Tp>& __x, const complex<_Tp>& __y);
262
263template<class _Tp>
264class _LIBCPP_VISIBLE complex
265{
266public:
267    typedef _Tp value_type;
268private:
269    value_type __re_;
270    value_type __im_;
271public:
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
321template<> class _LIBCPP_VISIBLE complex<double>;
322template<> class _LIBCPP_VISIBLE complex<long double>;
323
324template<>
325class _LIBCPP_VISIBLE complex<float>
326{
327    float __re_;
328    float __im_;
329public:
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
379template<>
380class _LIBCPP_VISIBLE complex<double>
381{
382    double __re_;
383    double __im_;
384public:
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
434template<>
435class _LIBCPP_VISIBLE complex<long double>
436{
437    long double __re_;
438    long double __im_;
439public:
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
490inline _LIBCPP_INLINE_VISIBILITY
491complex<float>::complex(const complex<double>& __c)
492    : __re_(__c.real()), __im_(__c.imag()) {}
493
494//constexpr
495inline _LIBCPP_INLINE_VISIBILITY
496complex<float>::complex(const complex<long double>& __c)
497    : __re_(__c.real()), __im_(__c.imag()) {}
498
499//constexpr
500inline _LIBCPP_INLINE_VISIBILITY
501complex<double>::complex(const complex<float>& __c)
502    : __re_(__c.real()), __im_(__c.imag()) {}
503
504//constexpr
505inline _LIBCPP_INLINE_VISIBILITY
506complex<double>::complex(const complex<long double>& __c)
507    : __re_(__c.real()), __im_(__c.imag()) {}
508
509//constexpr
510inline _LIBCPP_INLINE_VISIBILITY
511complex<long double>::complex(const complex<float>& __c)
512    : __re_(__c.real()), __im_(__c.imag()) {}
513
514//constexpr
515inline _LIBCPP_INLINE_VISIBILITY
516complex<long double>::complex(const complex<double>& __c)
517    : __re_(__c.real()), __im_(__c.imag()) {}
518
519// 26.3.6 operators:
520
521template<class _Tp>
522inline _LIBCPP_INLINE_VISIBILITY
523complex<_Tp>
524operator+(const complex<_Tp>& __x, const complex<_Tp>& __y)
525{
526    complex<_Tp> __t(__x);
527    __t += __y;
528    return __t;
529}
530
531template<class _Tp>
532inline _LIBCPP_INLINE_VISIBILITY
533complex<_Tp>
534operator+(const complex<_Tp>& __x, const _Tp& __y)
535{
536    complex<_Tp> __t(__x);
537    __t += __y;
538    return __t;
539}
540
541template<class _Tp>
542inline _LIBCPP_INLINE_VISIBILITY
543complex<_Tp>
544operator+(const _Tp& __x, const complex<_Tp>& __y)
545{
546    complex<_Tp> __t(__y);
547    __t += __x;
548    return __t;
549}
550
551template<class _Tp>
552inline _LIBCPP_INLINE_VISIBILITY
553complex<_Tp>
554operator-(const complex<_Tp>& __x, const complex<_Tp>& __y)
555{
556    complex<_Tp> __t(__x);
557    __t -= __y;
558    return __t;
559}
560
561template<class _Tp>
562inline _LIBCPP_INLINE_VISIBILITY
563complex<_Tp>
564operator-(const complex<_Tp>& __x, const _Tp& __y)
565{
566    complex<_Tp> __t(__x);
567    __t -= __y;
568    return __t;
569}
570
571template<class _Tp>
572inline _LIBCPP_INLINE_VISIBILITY
573complex<_Tp>
574operator-(const _Tp& __x, const complex<_Tp>& __y)
575{
576    complex<_Tp> __t(-__y);
577    __t += __x;
578    return __t;
579}
580
581template<class _Tp>
582complex<_Tp>
583operator*(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
640template<class _Tp>
641inline _LIBCPP_INLINE_VISIBILITY
642complex<_Tp>
643operator*(const complex<_Tp>& __x, const _Tp& __y)
644{
645    complex<_Tp> __t(__x);
646    __t *= __y;
647    return __t;
648}
649
650template<class _Tp>
651inline _LIBCPP_INLINE_VISIBILITY
652complex<_Tp>
653operator*(const _Tp& __x, const complex<_Tp>& __y)
654{
655    complex<_Tp> __t(__y);
656    __t *= __x;
657    return __t;
658}
659
660template<class _Tp>
661complex<_Tp>
662operator/(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
704template<class _Tp>
705inline _LIBCPP_INLINE_VISIBILITY
706complex<_Tp>
707operator/(const complex<_Tp>& __x, const _Tp& __y)
708{
709    return complex<_Tp>(__x.real() / __y, __x.imag() / __y);
710}
711
712template<class _Tp>
713inline _LIBCPP_INLINE_VISIBILITY
714complex<_Tp>
715operator/(const _Tp& __x, const complex<_Tp>& __y)
716{
717    complex<_Tp> __t(__x);
718    __t /= __y;
719    return __t;
720}
721
722template<class _Tp>
723inline _LIBCPP_INLINE_VISIBILITY
724complex<_Tp>
725operator+(const complex<_Tp>& __x)
726{
727    return __x;
728}
729
730template<class _Tp>
731inline _LIBCPP_INLINE_VISIBILITY
732complex<_Tp>
733operator-(const complex<_Tp>& __x)
734{
735    return complex<_Tp>(-__x.real(), -__x.imag());
736}
737
738template<class _Tp>
739inline _LIBCPP_INLINE_VISIBILITY
740bool
741operator==(const complex<_Tp>& __x, const complex<_Tp>& __y)
742{
743    return __x.real() == __y.real() && __x.imag() == __y.imag();
744}
745
746template<class _Tp>
747inline _LIBCPP_INLINE_VISIBILITY
748bool
749operator==(const complex<_Tp>& __x, const _Tp& __y)
750{
751    return __x.real() == __y && __x.imag() == 0;
752}
753
754template<class _Tp>
755inline _LIBCPP_INLINE_VISIBILITY
756bool
757operator==(const _Tp& __x, const complex<_Tp>& __y)
758{
759    return __x == __y.real() && 0 == __y.imag();
760}
761
762template<class _Tp>
763inline _LIBCPP_INLINE_VISIBILITY
764bool
765operator!=(const complex<_Tp>& __x, const complex<_Tp>& __y)
766{
767    return !(__x == __y);
768}
769
770template<class _Tp>
771inline _LIBCPP_INLINE_VISIBILITY
772bool
773operator!=(const complex<_Tp>& __x, const _Tp& __y)
774{
775    return !(__x == __y);
776}
777
778template<class _Tp>
779inline _LIBCPP_INLINE_VISIBILITY
780bool
781operator!=(const _Tp& __x, const complex<_Tp>& __y)
782{
783    return !(__x == __y);
784}
785
786// 26.3.7 values:
787
788// real
789
790template<class _Tp>
791inline _LIBCPP_INLINE_VISIBILITY
792_Tp
793real(const complex<_Tp>& __c)
794{
795    return __c.real();
796}
797
798inline _LIBCPP_INLINE_VISIBILITY
799long double
800real(long double __re)
801{
802    return __re;
803}
804
805inline _LIBCPP_INLINE_VISIBILITY
806double
807real(double __re)
808{
809    return __re;
810}
811
812template<class _Tp>
813inline _LIBCPP_INLINE_VISIBILITY
814typename enable_if
815<
816    is_integral<_Tp>::value,
817    double
818>::type
819real(_Tp  __re)
820{
821    return __re;
822}
823
824inline _LIBCPP_INLINE_VISIBILITY
825float
826real(float  __re)
827{
828    return __re;
829}
830
831// imag
832
833template<class _Tp>
834inline _LIBCPP_INLINE_VISIBILITY
835_Tp
836imag(const complex<_Tp>& __c)
837{
838    return __c.imag();
839}
840
841inline _LIBCPP_INLINE_VISIBILITY
842long double
843imag(long double __re)
844{
845    return 0;
846}
847
848inline _LIBCPP_INLINE_VISIBILITY
849double
850imag(double __re)
851{
852    return 0;
853}
854
855template<class _Tp>
856inline _LIBCPP_INLINE_VISIBILITY
857typename enable_if
858<
859    is_integral<_Tp>::value,
860    double
861>::type
862imag(_Tp  __re)
863{
864    return 0;
865}
866
867inline _LIBCPP_INLINE_VISIBILITY
868float
869imag(float  __re)
870{
871    return 0;
872}
873
874// abs
875
876template<class _Tp>
877inline _LIBCPP_INLINE_VISIBILITY
878_Tp
879abs(const complex<_Tp>& __c)
880{
881    return hypot(__c.real(), __c.imag());
882}
883
884// arg
885
886template<class _Tp>
887inline _LIBCPP_INLINE_VISIBILITY
888_Tp
889arg(const complex<_Tp>& __c)
890{
891    return atan2(__c.imag(), __c.real());
892}
893
894inline _LIBCPP_INLINE_VISIBILITY
895long double
896arg(long double __re)
897{
898    return atan2l(0.L, __re);
899}
900
901inline _LIBCPP_INLINE_VISIBILITY
902double
903arg(double __re)
904{
905    return atan2(0., __re);
906}
907
908template<class _Tp>
909inline _LIBCPP_INLINE_VISIBILITY
910typename enable_if
911<
912    is_integral<_Tp>::value,
913    double
914>::type
915arg(_Tp __re)
916{
917    return atan2(0., __re);
918}
919
920inline _LIBCPP_INLINE_VISIBILITY
921float
922arg(float __re)
923{
924    return atan2f(0.F, __re);
925}
926
927// norm
928
929template<class _Tp>
930inline _LIBCPP_INLINE_VISIBILITY
931_Tp
932norm(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
941inline _LIBCPP_INLINE_VISIBILITY
942long double
943norm(long double __re)
944{
945    return __re * __re;
946}
947
948inline _LIBCPP_INLINE_VISIBILITY
949double
950norm(double __re)
951{
952    return __re * __re;
953}
954
955template<class _Tp>
956inline _LIBCPP_INLINE_VISIBILITY
957typename enable_if
958<
959    is_integral<_Tp>::value,
960    double
961>::type
962norm(_Tp __re)
963{
964    return (double)__re * __re;
965}
966
967inline _LIBCPP_INLINE_VISIBILITY
968float
969norm(float __re)
970{
971    return __re * __re;
972}
973
974// conj
975
976template<class _Tp>
977inline _LIBCPP_INLINE_VISIBILITY
978complex<_Tp>
979conj(const complex<_Tp>& __c)
980{
981    return complex<_Tp>(__c.real(), -__c.imag());
982}
983
984inline _LIBCPP_INLINE_VISIBILITY
985complex<long double>
986conj(long double __re)
987{
988    return complex<long double>(__re);
989}
990
991inline _LIBCPP_INLINE_VISIBILITY
992complex<double>
993conj(double __re)
994{
995    return complex<double>(__re);
996}
997
998template<class _Tp>
999inline _LIBCPP_INLINE_VISIBILITY
1000typename enable_if
1001<
1002    is_integral<_Tp>::value,
1003    complex<double>
1004>::type
1005conj(_Tp __re)
1006{
1007    return complex<double>(__re);
1008}
1009
1010inline _LIBCPP_INLINE_VISIBILITY
1011complex<float>
1012conj(float __re)
1013{
1014    return complex<float>(__re);
1015}
1016
1017// proj
1018
1019template<class _Tp>
1020inline _LIBCPP_INLINE_VISIBILITY
1021complex<_Tp>
1022proj(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
1030inline _LIBCPP_INLINE_VISIBILITY
1031complex<long double>
1032proj(long double __re)
1033{
1034    if (isinf(__re))
1035        __re = abs(__re);
1036    return complex<long double>(__re);
1037}
1038
1039inline _LIBCPP_INLINE_VISIBILITY
1040complex<double>
1041proj(double __re)
1042{
1043    if (isinf(__re))
1044        __re = abs(__re);
1045    return complex<double>(__re);
1046}
1047
1048template<class _Tp>
1049inline _LIBCPP_INLINE_VISIBILITY
1050typename enable_if
1051<
1052    is_integral<_Tp>::value,
1053    complex<double>
1054>::type
1055proj(_Tp __re)
1056{
1057    return complex<double>(__re);
1058}
1059
1060inline _LIBCPP_INLINE_VISIBILITY
1061complex<float>
1062proj(float __re)
1063{
1064    if (isinf(__re))
1065        __re = abs(__re);
1066    return complex<float>(__re);
1067}
1068
1069// polar
1070
1071template<class _Tp>
1072complex<_Tp>
1073polar(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
1100template<class _Tp>
1101inline _LIBCPP_INLINE_VISIBILITY
1102complex<_Tp>
1103log(const complex<_Tp>& __x)
1104{
1105    return complex<_Tp>(log(abs(__x)), arg(__x));
1106}
1107
1108// log10
1109
1110template<class _Tp>
1111inline _LIBCPP_INLINE_VISIBILITY
1112complex<_Tp>
1113log10(const complex<_Tp>& __x)
1114{
1115    return log(__x) / log(_Tp(10));
1116}
1117
1118// sqrt
1119
1120template<class _Tp>
1121complex<_Tp>
1122sqrt(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
1137template<class _Tp>
1138complex<_Tp>
1139exp(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
1164template<class _Tp>
1165inline _LIBCPP_INLINE_VISIBILITY
1166complex<_Tp>
1167pow(const complex<_Tp>& __x, const complex<_Tp>& __y)
1168{
1169    return exp(__y * log(__x));
1170}
1171
1172template<class _Tp, class _Up>
1173inline _LIBCPP_INLINE_VISIBILITY
1174complex<typename __promote<_Tp, _Up>::type>
1175pow(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
1181template<class _Tp, class _Up>
1182inline _LIBCPP_INLINE_VISIBILITY
1183typename enable_if
1184<
1185    is_arithmetic<_Up>::value,
1186    complex<typename __promote<_Tp, _Up>::type>
1187>::type
1188pow(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
1194template<class _Tp, class _Up>
1195inline _LIBCPP_INLINE_VISIBILITY
1196typename enable_if
1197<
1198    is_arithmetic<_Tp>::value,
1199    complex<typename __promote<_Tp, _Up>::type>
1200>::type
1201pow(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
1209template<class _Tp>
1210complex<_Tp>
1211asinh(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
1238template<class _Tp>
1239complex<_Tp>
1240acosh(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
1270template<class _Tp>
1271complex<_Tp>
1272atanh(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
1303template<class _Tp>
1304complex<_Tp>
1305sinh(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
1318template<class _Tp>
1319complex<_Tp>
1320cosh(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
1335template<class _Tp>
1336complex<_Tp>
1337tanh(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
1355template<class _Tp>
1356complex<_Tp>
1357asin(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
1365template<class _Tp>
1366complex<_Tp>
1367acos(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
1402template<class _Tp>
1403complex<_Tp>
1404atan(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
1412template<class _Tp>
1413complex<_Tp>
1414sin(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
1422template<class _Tp>
1423inline _LIBCPP_INLINE_VISIBILITY
1424complex<_Tp>
1425cos(const complex<_Tp>& __x)
1426{
1427    return cosh(complex<_Tp>(-__x.imag(), __x.real()));
1428}
1429
1430// tan
1431
1432template<class _Tp>
1433complex<_Tp>
1434tan(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
1440template<class _Tp, class _CharT, class _Traits>
1441basic_istream<_CharT, _Traits>&
1442operator>>(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
1502template<class _Tp, class _CharT, class _Traits>
1503basic_ostream<_CharT, _Traits>&
1504operator<<(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
1517