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