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