libstdc++
complex
Go to the documentation of this file.
00001 // The template and inlines for the -*- C++ -*- complex number classes.
00002 
00003 // Copyright (C) 1997-2017 Free Software Foundation, Inc.
00004 //
00005 // This file is part of the GNU ISO C++ Library.  This library is free
00006 // software; you can redistribute it and/or modify it under the
00007 // terms of the GNU General Public License as published by the
00008 // Free Software Foundation; either version 3, or (at your option)
00009 // any later version.
00010 
00011 // This library is distributed in the hope that it will be useful,
00012 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00013 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00014 // GNU General Public License for more details.
00015 
00016 // Under Section 7 of GPL version 3, you are granted additional
00017 // permissions described in the GCC Runtime Library Exception, version
00018 // 3.1, as published by the Free Software Foundation.
00019 
00020 // You should have received a copy of the GNU General Public License and
00021 // a copy of the GCC Runtime Library Exception along with this program;
00022 // see the files COPYING3 and COPYING.RUNTIME respectively.  If not, see
00023 // <http://www.gnu.org/licenses/>.
00024 
00025 /** @file include/complex
00026  *  This is a Standard C++ Library header.
00027  */
00028 
00029 //
00030 // ISO C++ 14882: 26.2  Complex Numbers
00031 // Note: this is not a conforming implementation.
00032 // Initially implemented by Ulrich Drepper <drepper@cygnus.com>
00033 // Improved by Gabriel Dos Reis <dosreis@cmla.ens-cachan.fr>
00034 //
00035 
00036 #ifndef _GLIBCXX_COMPLEX
00037 #define _GLIBCXX_COMPLEX 1
00038 
00039 #pragma GCC system_header
00040 
00041 #include <bits/c++config.h>
00042 #include <bits/cpp_type_traits.h>
00043 #include <ext/type_traits.h>
00044 #include <cmath>
00045 #include <sstream>
00046 
00047 // Get rid of a macro possibly defined in <complex.h>
00048 #undef complex
00049 
00050 namespace std _GLIBCXX_VISIBILITY(default)
00051 {
00052 _GLIBCXX_BEGIN_NAMESPACE_VERSION
00053 
00054   /**
00055    * @defgroup complex_numbers Complex Numbers
00056    * @ingroup numerics
00057    *
00058    * Classes and functions for complex numbers.
00059    * @{
00060    */
00061 
00062   // Forward declarations.
00063   template<typename _Tp> class complex;
00064   template<> class complex<float>;
00065   template<> class complex<double>;
00066   template<> class complex<long double>;
00067 
00068   ///  Return magnitude of @a z.
00069   template<typename _Tp> _Tp abs(const complex<_Tp>&);
00070   ///  Return phase angle of @a z.
00071   template<typename _Tp> _Tp arg(const complex<_Tp>&);
00072   ///  Return @a z magnitude squared.
00073   template<typename _Tp> _Tp norm(const complex<_Tp>&);
00074 
00075   ///  Return complex conjugate of @a z.
00076   template<typename _Tp> complex<_Tp> conj(const complex<_Tp>&);
00077   ///  Return complex with magnitude @a rho and angle @a theta.
00078   template<typename _Tp> complex<_Tp> polar(const _Tp&, const _Tp& = 0);
00079 
00080   // Transcendentals:
00081   /// Return complex cosine of @a z.
00082   template<typename _Tp> complex<_Tp> cos(const complex<_Tp>&);
00083   /// Return complex hyperbolic cosine of @a z.
00084   template<typename _Tp> complex<_Tp> cosh(const complex<_Tp>&);
00085   /// Return complex base e exponential of @a z.
00086   template<typename _Tp> complex<_Tp> exp(const complex<_Tp>&);
00087   /// Return complex natural logarithm of @a z.
00088   template<typename _Tp> complex<_Tp> log(const complex<_Tp>&);
00089   /// Return complex base 10 logarithm of @a z.
00090   template<typename _Tp> complex<_Tp> log10(const complex<_Tp>&);
00091   /// Return @a x to the @a y'th power.
00092   template<typename _Tp> complex<_Tp> pow(const complex<_Tp>&, int);
00093   /// Return @a x to the @a y'th power.
00094   template<typename _Tp> complex<_Tp> pow(const complex<_Tp>&, const _Tp&);
00095   /// Return @a x to the @a y'th power.
00096   template<typename _Tp> complex<_Tp> pow(const complex<_Tp>&,
00097                                           const complex<_Tp>&);
00098   /// Return @a x to the @a y'th power.
00099   template<typename _Tp> complex<_Tp> pow(const _Tp&, const complex<_Tp>&);
00100   /// Return complex sine of @a z.
00101   template<typename _Tp> complex<_Tp> sin(const complex<_Tp>&);
00102   /// Return complex hyperbolic sine of @a z.
00103   template<typename _Tp> complex<_Tp> sinh(const complex<_Tp>&);
00104   /// Return complex square root of @a z.
00105   template<typename _Tp> complex<_Tp> sqrt(const complex<_Tp>&);
00106   /// Return complex tangent of @a z.
00107   template<typename _Tp> complex<_Tp> tan(const complex<_Tp>&);
00108   /// Return complex hyperbolic tangent of @a z.
00109   template<typename _Tp> complex<_Tp> tanh(const complex<_Tp>&);
00110 
00111 
00112   // 26.2.2  Primary template class complex
00113   /**
00114    *  Template to represent complex numbers.
00115    *
00116    *  Specializations for float, double, and long double are part of the
00117    *  library.  Results with any other type are not guaranteed.
00118    *
00119    *  @param  Tp  Type of real and imaginary values.
00120   */
00121   template<typename _Tp>
00122     struct complex
00123     {
00124       /// Value typedef.
00125       typedef _Tp value_type;
00126 
00127       ///  Default constructor.  First parameter is x, second parameter is y.
00128       ///  Unspecified parameters default to 0.
00129       _GLIBCXX_CONSTEXPR complex(const _Tp& __r = _Tp(), const _Tp& __i = _Tp())
00130       : _M_real(__r), _M_imag(__i) { }
00131 
00132       // Let the compiler synthesize the copy constructor
00133 #if __cplusplus >= 201103L
00134       constexpr complex(const complex&) = default;
00135 #endif
00136 
00137       ///  Converting constructor.
00138       template<typename _Up>
00139         _GLIBCXX_CONSTEXPR complex(const complex<_Up>& __z)
00140         : _M_real(__z.real()), _M_imag(__z.imag()) { }
00141 
00142 #if __cplusplus >= 201103L
00143       // _GLIBCXX_RESOLVE_LIB_DEFECTS
00144       // DR 387. std::complex over-encapsulated.
00145       _GLIBCXX_ABI_TAG_CXX11
00146       constexpr _Tp
00147       real() const { return _M_real; }
00148 
00149       _GLIBCXX_ABI_TAG_CXX11
00150       constexpr _Tp
00151       imag() const { return _M_imag; }
00152 #else
00153       ///  Return real part of complex number.
00154       _Tp&
00155       real() { return _M_real; }
00156 
00157       ///  Return real part of complex number.
00158       const _Tp&
00159       real() const { return _M_real; }
00160 
00161       ///  Return imaginary part of complex number.
00162       _Tp&
00163       imag() { return _M_imag; }
00164 
00165       ///  Return imaginary part of complex number.
00166       const _Tp&
00167       imag() const { return _M_imag; }
00168 #endif
00169 
00170       // _GLIBCXX_RESOLVE_LIB_DEFECTS
00171       // DR 387. std::complex over-encapsulated.
00172       void
00173       real(_Tp __val) { _M_real = __val; }
00174 
00175       void
00176       imag(_Tp __val) { _M_imag = __val; }
00177 
00178       /// Assign a scalar to this complex number.
00179       complex<_Tp>& operator=(const _Tp&);
00180 
00181       /// Add a scalar to this complex number.
00182       // 26.2.5/1
00183       complex<_Tp>&
00184       operator+=(const _Tp& __t)
00185       {
00186         _M_real += __t;
00187         return *this;
00188       }
00189 
00190       /// Subtract a scalar from this complex number.
00191       // 26.2.5/3
00192       complex<_Tp>&
00193       operator-=(const _Tp& __t)
00194       {
00195         _M_real -= __t;
00196         return *this;
00197       }
00198 
00199       /// Multiply this complex number by a scalar.
00200       complex<_Tp>& operator*=(const _Tp&);
00201       /// Divide this complex number by a scalar.
00202       complex<_Tp>& operator/=(const _Tp&);
00203 
00204       // Let the compiler synthesize the copy assignment operator
00205 #if __cplusplus >= 201103L
00206       complex& operator=(const complex&) = default;
00207 #endif
00208 
00209       /// Assign another complex number to this one.
00210       template<typename _Up>
00211         complex<_Tp>& operator=(const complex<_Up>&);
00212       /// Add another complex number to this one.
00213       template<typename _Up>
00214         complex<_Tp>& operator+=(const complex<_Up>&);
00215       /// Subtract another complex number from this one.
00216       template<typename _Up>
00217         complex<_Tp>& operator-=(const complex<_Up>&);
00218       /// Multiply this complex number by another.
00219       template<typename _Up>
00220         complex<_Tp>& operator*=(const complex<_Up>&);
00221       /// Divide this complex number by another.
00222       template<typename _Up>
00223         complex<_Tp>& operator/=(const complex<_Up>&);
00224 
00225       _GLIBCXX_CONSTEXPR complex __rep() const
00226       { return *this; }
00227 
00228     private:
00229       _Tp _M_real;
00230       _Tp _M_imag;
00231     };
00232 
00233   template<typename _Tp>
00234     complex<_Tp>&
00235     complex<_Tp>::operator=(const _Tp& __t)
00236     {
00237      _M_real = __t;
00238      _M_imag = _Tp();
00239      return *this;
00240     }
00241 
00242   // 26.2.5/5
00243   template<typename _Tp>
00244     complex<_Tp>&
00245     complex<_Tp>::operator*=(const _Tp& __t)
00246     {
00247       _M_real *= __t;
00248       _M_imag *= __t;
00249       return *this;
00250     }
00251 
00252   // 26.2.5/7
00253   template<typename _Tp>
00254     complex<_Tp>&
00255     complex<_Tp>::operator/=(const _Tp& __t)
00256     {
00257       _M_real /= __t;
00258       _M_imag /= __t;
00259       return *this;
00260     }
00261 
00262   template<typename _Tp>
00263     template<typename _Up>
00264     complex<_Tp>&
00265     complex<_Tp>::operator=(const complex<_Up>& __z)
00266     {
00267       _M_real = __z.real();
00268       _M_imag = __z.imag();
00269       return *this;
00270     }
00271 
00272   // 26.2.5/9
00273   template<typename _Tp>
00274     template<typename _Up>
00275     complex<_Tp>&
00276     complex<_Tp>::operator+=(const complex<_Up>& __z)
00277     {
00278       _M_real += __z.real();
00279       _M_imag += __z.imag();
00280       return *this;
00281     }
00282 
00283   // 26.2.5/11
00284   template<typename _Tp>
00285     template<typename _Up>
00286     complex<_Tp>&
00287     complex<_Tp>::operator-=(const complex<_Up>& __z)
00288     {
00289       _M_real -= __z.real();
00290       _M_imag -= __z.imag();
00291       return *this;
00292     }
00293 
00294   // 26.2.5/13
00295   // XXX: This is a grammar school implementation.
00296   template<typename _Tp>
00297     template<typename _Up>
00298     complex<_Tp>&
00299     complex<_Tp>::operator*=(const complex<_Up>& __z)
00300     {
00301       const _Tp __r = _M_real * __z.real() - _M_imag * __z.imag();
00302       _M_imag = _M_real * __z.imag() + _M_imag * __z.real();
00303       _M_real = __r;
00304       return *this;
00305     }
00306 
00307   // 26.2.5/15
00308   // XXX: This is a grammar school implementation.
00309   template<typename _Tp>
00310     template<typename _Up>
00311     complex<_Tp>&
00312     complex<_Tp>::operator/=(const complex<_Up>& __z)
00313     {
00314       const _Tp __r =  _M_real * __z.real() + _M_imag * __z.imag();
00315       const _Tp __n = std::norm(__z);
00316       _M_imag = (_M_imag * __z.real() - _M_real * __z.imag()) / __n;
00317       _M_real = __r / __n;
00318       return *this;
00319     }
00320 
00321   // Operators:
00322   //@{
00323   ///  Return new complex value @a x plus @a y.
00324   template<typename _Tp>
00325     inline complex<_Tp>
00326     operator+(const complex<_Tp>& __x, const complex<_Tp>& __y)
00327     {
00328       complex<_Tp> __r = __x;
00329       __r += __y;
00330       return __r;
00331     }
00332 
00333   template<typename _Tp>
00334     inline complex<_Tp>
00335     operator+(const complex<_Tp>& __x, const _Tp& __y)
00336     {
00337       complex<_Tp> __r = __x;
00338       __r += __y;
00339       return __r;
00340     }
00341 
00342   template<typename _Tp>
00343     inline complex<_Tp>
00344     operator+(const _Tp& __x, const complex<_Tp>& __y)
00345     {
00346       complex<_Tp> __r = __y;
00347       __r += __x;
00348       return __r;
00349     }
00350   //@}
00351 
00352   //@{
00353   ///  Return new complex value @a x minus @a y.
00354   template<typename _Tp>
00355     inline complex<_Tp>
00356     operator-(const complex<_Tp>& __x, const complex<_Tp>& __y)
00357     {
00358       complex<_Tp> __r = __x;
00359       __r -= __y;
00360       return __r;
00361     }
00362 
00363   template<typename _Tp>
00364     inline complex<_Tp>
00365     operator-(const complex<_Tp>& __x, const _Tp& __y)
00366     {
00367       complex<_Tp> __r = __x;
00368       __r -= __y;
00369       return __r;
00370     }
00371 
00372   template<typename _Tp>
00373     inline complex<_Tp>
00374     operator-(const _Tp& __x, const complex<_Tp>& __y)
00375     {
00376       complex<_Tp> __r(__x, -__y.imag());
00377       __r -= __y.real();
00378       return __r;
00379     }
00380   //@}
00381 
00382   //@{
00383   ///  Return new complex value @a x times @a y.
00384   template<typename _Tp>
00385     inline complex<_Tp>
00386     operator*(const complex<_Tp>& __x, const complex<_Tp>& __y)
00387     {
00388       complex<_Tp> __r = __x;
00389       __r *= __y;
00390       return __r;
00391     }
00392 
00393   template<typename _Tp>
00394     inline complex<_Tp>
00395     operator*(const complex<_Tp>& __x, const _Tp& __y)
00396     {
00397       complex<_Tp> __r = __x;
00398       __r *= __y;
00399       return __r;
00400     }
00401 
00402   template<typename _Tp>
00403     inline complex<_Tp>
00404     operator*(const _Tp& __x, const complex<_Tp>& __y)
00405     {
00406       complex<_Tp> __r = __y;
00407       __r *= __x;
00408       return __r;
00409     }
00410   //@}
00411 
00412   //@{
00413   ///  Return new complex value @a x divided by @a y.
00414   template<typename _Tp>
00415     inline complex<_Tp>
00416     operator/(const complex<_Tp>& __x, const complex<_Tp>& __y)
00417     {
00418       complex<_Tp> __r = __x;
00419       __r /= __y;
00420       return __r;
00421     }
00422 
00423   template<typename _Tp>
00424     inline complex<_Tp>
00425     operator/(const complex<_Tp>& __x, const _Tp& __y)
00426     {
00427       complex<_Tp> __r = __x;
00428       __r /= __y;
00429       return __r;
00430     }
00431 
00432   template<typename _Tp>
00433     inline complex<_Tp>
00434     operator/(const _Tp& __x, const complex<_Tp>& __y)
00435     {
00436       complex<_Tp> __r = __x;
00437       __r /= __y;
00438       return __r;
00439     }
00440   //@}
00441 
00442   ///  Return @a x.
00443   template<typename _Tp>
00444     inline complex<_Tp>
00445     operator+(const complex<_Tp>& __x)
00446     { return __x; }
00447 
00448   ///  Return complex negation of @a x.
00449   template<typename _Tp>
00450     inline complex<_Tp>
00451     operator-(const complex<_Tp>& __x)
00452     {  return complex<_Tp>(-__x.real(), -__x.imag()); }
00453 
00454   //@{
00455   ///  Return true if @a x is equal to @a y.
00456   template<typename _Tp>
00457     inline _GLIBCXX_CONSTEXPR bool
00458     operator==(const complex<_Tp>& __x, const complex<_Tp>& __y)
00459     { return __x.real() == __y.real() && __x.imag() == __y.imag(); }
00460 
00461   template<typename _Tp>
00462     inline _GLIBCXX_CONSTEXPR bool
00463     operator==(const complex<_Tp>& __x, const _Tp& __y)
00464     { return __x.real() == __y && __x.imag() == _Tp(); }
00465 
00466   template<typename _Tp>
00467     inline _GLIBCXX_CONSTEXPR bool
00468     operator==(const _Tp& __x, const complex<_Tp>& __y)
00469     { return __x == __y.real() && _Tp() == __y.imag(); }
00470   //@}
00471 
00472   //@{
00473   ///  Return false if @a x is equal to @a y.
00474   template<typename _Tp>
00475     inline _GLIBCXX_CONSTEXPR bool
00476     operator!=(const complex<_Tp>& __x, const complex<_Tp>& __y)
00477     { return __x.real() != __y.real() || __x.imag() != __y.imag(); }
00478 
00479   template<typename _Tp>
00480     inline _GLIBCXX_CONSTEXPR bool
00481     operator!=(const complex<_Tp>& __x, const _Tp& __y)
00482     { return __x.real() != __y || __x.imag() != _Tp(); }
00483 
00484   template<typename _Tp>
00485     inline _GLIBCXX_CONSTEXPR bool
00486     operator!=(const _Tp& __x, const complex<_Tp>& __y)
00487     { return __x != __y.real() || _Tp() != __y.imag(); }
00488   //@}
00489 
00490   ///  Extraction operator for complex values.
00491   template<typename _Tp, typename _CharT, class _Traits>
00492     basic_istream<_CharT, _Traits>&
00493     operator>>(basic_istream<_CharT, _Traits>& __is, complex<_Tp>& __x)
00494     {
00495       _Tp __re_x, __im_x;
00496       _CharT __ch;
00497       __is >> __ch;
00498       if (__ch == '(')
00499         {
00500           __is >> __re_x >> __ch;
00501           if (__ch == ',')
00502             {
00503               __is >> __im_x >> __ch;
00504               if (__ch == ')')
00505                 __x = complex<_Tp>(__re_x, __im_x);
00506               else
00507                 __is.setstate(ios_base::failbit);
00508             }
00509           else if (__ch == ')')
00510             __x = __re_x;
00511           else
00512             __is.setstate(ios_base::failbit);
00513         }
00514       else
00515         {
00516           __is.putback(__ch);
00517           __is >> __re_x;
00518           __x = __re_x;
00519         }
00520       return __is;
00521     }
00522 
00523   ///  Insertion operator for complex values.
00524   template<typename _Tp, typename _CharT, class _Traits>
00525     basic_ostream<_CharT, _Traits>&
00526     operator<<(basic_ostream<_CharT, _Traits>& __os, const complex<_Tp>& __x)
00527     {
00528       basic_ostringstream<_CharT, _Traits> __s;
00529       __s.flags(__os.flags());
00530       __s.imbue(__os.getloc());
00531       __s.precision(__os.precision());
00532       __s << '(' << __x.real() << ',' << __x.imag() << ')';
00533       return __os << __s.str();
00534     }
00535 
00536   // Values
00537 #if __cplusplus >= 201103L
00538   template<typename _Tp>
00539     constexpr _Tp
00540     real(const complex<_Tp>& __z)
00541     { return __z.real(); }
00542 
00543   template<typename _Tp>
00544     constexpr _Tp
00545     imag(const complex<_Tp>& __z)
00546     { return __z.imag(); }
00547 #else
00548   template<typename _Tp>
00549     inline _Tp&
00550     real(complex<_Tp>& __z)
00551     { return __z.real(); }
00552 
00553   template<typename _Tp>
00554     inline const _Tp&
00555     real(const complex<_Tp>& __z)
00556     { return __z.real(); }
00557 
00558   template<typename _Tp>
00559     inline _Tp&
00560     imag(complex<_Tp>& __z)
00561     { return __z.imag(); }
00562 
00563   template<typename _Tp>
00564     inline const _Tp&
00565     imag(const complex<_Tp>& __z)
00566     { return __z.imag(); }
00567 #endif
00568 
00569   // 26.2.7/3 abs(__z):  Returns the magnitude of __z.
00570   template<typename _Tp>
00571     inline _Tp
00572     __complex_abs(const complex<_Tp>& __z)
00573     {
00574       _Tp __x = __z.real();
00575       _Tp __y = __z.imag();
00576       const _Tp __s = std::max(abs(__x), abs(__y));
00577       if (__s == _Tp())  // well ...
00578         return __s;
00579       __x /= __s;
00580       __y /= __s;
00581       return __s * sqrt(__x * __x + __y * __y);
00582     }
00583 
00584 #if _GLIBCXX_USE_C99_COMPLEX
00585   inline float
00586   __complex_abs(__complex__ float __z) { return __builtin_cabsf(__z); }
00587 
00588   inline double
00589   __complex_abs(__complex__ double __z) { return __builtin_cabs(__z); }
00590 
00591   inline long double
00592   __complex_abs(const __complex__ long double& __z)
00593   { return __builtin_cabsl(__z); }
00594 
00595   template<typename _Tp>
00596     inline _Tp
00597     abs(const complex<_Tp>& __z) { return __complex_abs(__z.__rep()); }
00598 #else
00599   template<typename _Tp>
00600     inline _Tp
00601     abs(const complex<_Tp>& __z) { return __complex_abs(__z); }
00602 #endif
00603 
00604 
00605   // 26.2.7/4: arg(__z): Returns the phase angle of __z.
00606   template<typename _Tp>
00607     inline _Tp
00608     __complex_arg(const complex<_Tp>& __z)
00609     { return  atan2(__z.imag(), __z.real()); }
00610 
00611 #if _GLIBCXX_USE_C99_COMPLEX
00612   inline float
00613   __complex_arg(__complex__ float __z) { return __builtin_cargf(__z); }
00614 
00615   inline double
00616   __complex_arg(__complex__ double __z) { return __builtin_carg(__z); }
00617 
00618   inline long double
00619   __complex_arg(const __complex__ long double& __z)
00620   { return __builtin_cargl(__z); }
00621 
00622   template<typename _Tp>
00623     inline _Tp
00624     arg(const complex<_Tp>& __z) { return __complex_arg(__z.__rep()); }
00625 #else
00626   template<typename _Tp>
00627     inline _Tp
00628     arg(const complex<_Tp>& __z) { return __complex_arg(__z); }
00629 #endif
00630 
00631   // 26.2.7/5: norm(__z) returns the squared magnitude of __z.
00632   //     As defined, norm() is -not- a norm is the common mathematical
00633   //     sense used in numerics.  The helper class _Norm_helper<> tries to
00634   //     distinguish between builtin floating point and the rest, so as
00635   //     to deliver an answer as close as possible to the real value.
00636   template<bool>
00637     struct _Norm_helper
00638     {
00639       template<typename _Tp>
00640         static inline _Tp _S_do_it(const complex<_Tp>& __z)
00641         {
00642           const _Tp __x = __z.real();
00643           const _Tp __y = __z.imag();
00644           return __x * __x + __y * __y;
00645         }
00646     };
00647 
00648   template<>
00649     struct _Norm_helper<true>
00650     {
00651       template<typename _Tp>
00652         static inline _Tp _S_do_it(const complex<_Tp>& __z)
00653         {
00654           _Tp __res = std::abs(__z);
00655           return __res * __res;
00656         }
00657     };
00658 
00659   template<typename _Tp>
00660     inline _Tp
00661     norm(const complex<_Tp>& __z)
00662     {
00663       return _Norm_helper<__is_floating<_Tp>::__value
00664         && !_GLIBCXX_FAST_MATH>::_S_do_it(__z);
00665     }
00666 
00667   template<typename _Tp>
00668     inline complex<_Tp>
00669     polar(const _Tp& __rho, const _Tp& __theta)
00670     {
00671       __glibcxx_assert( __rho >= 0 );
00672       return complex<_Tp>(__rho * cos(__theta), __rho * sin(__theta));
00673     }
00674 
00675   template<typename _Tp>
00676     inline complex<_Tp>
00677     conj(const complex<_Tp>& __z)
00678     { return complex<_Tp>(__z.real(), -__z.imag()); }
00679 
00680   // Transcendentals
00681 
00682   // 26.2.8/1 cos(__z):  Returns the cosine of __z.
00683   template<typename _Tp>
00684     inline complex<_Tp>
00685     __complex_cos(const complex<_Tp>& __z)
00686     {
00687       const _Tp __x = __z.real();
00688       const _Tp __y = __z.imag();
00689       return complex<_Tp>(cos(__x) * cosh(__y), -sin(__x) * sinh(__y));
00690     }
00691 
00692 #if _GLIBCXX_USE_C99_COMPLEX
00693   inline __complex__ float
00694   __complex_cos(__complex__ float __z) { return __builtin_ccosf(__z); }
00695 
00696   inline __complex__ double
00697   __complex_cos(__complex__ double __z) { return __builtin_ccos(__z); }
00698 
00699   inline __complex__ long double
00700   __complex_cos(const __complex__ long double& __z)
00701   { return __builtin_ccosl(__z); }
00702 
00703   template<typename _Tp>
00704     inline complex<_Tp>
00705     cos(const complex<_Tp>& __z) { return __complex_cos(__z.__rep()); }
00706 #else
00707   template<typename _Tp>
00708     inline complex<_Tp>
00709     cos(const complex<_Tp>& __z) { return __complex_cos(__z); }
00710 #endif
00711 
00712   // 26.2.8/2 cosh(__z): Returns the hyperbolic cosine of __z.
00713   template<typename _Tp>
00714     inline complex<_Tp>
00715     __complex_cosh(const complex<_Tp>& __z)
00716     {
00717       const _Tp __x = __z.real();
00718       const _Tp __y = __z.imag();
00719       return complex<_Tp>(cosh(__x) * cos(__y), sinh(__x) * sin(__y));
00720     }
00721 
00722 #if _GLIBCXX_USE_C99_COMPLEX
00723   inline __complex__ float
00724   __complex_cosh(__complex__ float __z) { return __builtin_ccoshf(__z); }
00725 
00726   inline __complex__ double
00727   __complex_cosh(__complex__ double __z) { return __builtin_ccosh(__z); }
00728 
00729   inline __complex__ long double
00730   __complex_cosh(const __complex__ long double& __z)
00731   { return __builtin_ccoshl(__z); }
00732 
00733   template<typename _Tp>
00734     inline complex<_Tp>
00735     cosh(const complex<_Tp>& __z) { return __complex_cosh(__z.__rep()); }
00736 #else
00737   template<typename _Tp>
00738     inline complex<_Tp>
00739     cosh(const complex<_Tp>& __z) { return __complex_cosh(__z); }
00740 #endif
00741 
00742   // 26.2.8/3 exp(__z): Returns the complex base e exponential of x
00743   template<typename _Tp>
00744     inline complex<_Tp>
00745     __complex_exp(const complex<_Tp>& __z)
00746     { return std::polar<_Tp>(exp(__z.real()), __z.imag()); }
00747 
00748 #if _GLIBCXX_USE_C99_COMPLEX
00749   inline __complex__ float
00750   __complex_exp(__complex__ float __z) { return __builtin_cexpf(__z); }
00751 
00752   inline __complex__ double
00753   __complex_exp(__complex__ double __z) { return __builtin_cexp(__z); }
00754 
00755   inline __complex__ long double
00756   __complex_exp(const __complex__ long double& __z)
00757   { return __builtin_cexpl(__z); }
00758 
00759   template<typename _Tp>
00760     inline complex<_Tp>
00761     exp(const complex<_Tp>& __z) { return __complex_exp(__z.__rep()); }
00762 #else
00763   template<typename _Tp>
00764     inline complex<_Tp>
00765     exp(const complex<_Tp>& __z) { return __complex_exp(__z); }
00766 #endif
00767 
00768   // 26.2.8/5 log(__z): Returns the natural complex logarithm of __z.
00769   //                    The branch cut is along the negative axis.
00770   template<typename _Tp>
00771     inline complex<_Tp>
00772     __complex_log(const complex<_Tp>& __z)
00773     { return complex<_Tp>(log(std::abs(__z)), std::arg(__z)); }
00774 
00775 #if _GLIBCXX_USE_C99_COMPLEX
00776   inline __complex__ float
00777   __complex_log(__complex__ float __z) { return __builtin_clogf(__z); }
00778 
00779   inline __complex__ double
00780   __complex_log(__complex__ double __z) { return __builtin_clog(__z); }
00781 
00782   inline __complex__ long double
00783   __complex_log(const __complex__ long double& __z)
00784   { return __builtin_clogl(__z); }
00785 
00786   template<typename _Tp>
00787     inline complex<_Tp>
00788     log(const complex<_Tp>& __z) { return __complex_log(__z.__rep()); }
00789 #else
00790   template<typename _Tp>
00791     inline complex<_Tp>
00792     log(const complex<_Tp>& __z) { return __complex_log(__z); }
00793 #endif
00794 
00795   template<typename _Tp>
00796     inline complex<_Tp>
00797     log10(const complex<_Tp>& __z)
00798     { return std::log(__z) / log(_Tp(10.0)); }
00799 
00800   // 26.2.8/10 sin(__z): Returns the sine of __z.
00801   template<typename _Tp>
00802     inline complex<_Tp>
00803     __complex_sin(const complex<_Tp>& __z)
00804     {
00805       const _Tp __x = __z.real();
00806       const _Tp __y = __z.imag();
00807       return complex<_Tp>(sin(__x) * cosh(__y), cos(__x) * sinh(__y));
00808     }
00809 
00810 #if _GLIBCXX_USE_C99_COMPLEX
00811   inline __complex__ float
00812   __complex_sin(__complex__ float __z) { return __builtin_csinf(__z); }
00813 
00814   inline __complex__ double
00815   __complex_sin(__complex__ double __z) { return __builtin_csin(__z); }
00816 
00817   inline __complex__ long double
00818   __complex_sin(const __complex__ long double& __z)
00819   { return __builtin_csinl(__z); }
00820 
00821   template<typename _Tp>
00822     inline complex<_Tp>
00823     sin(const complex<_Tp>& __z) { return __complex_sin(__z.__rep()); }
00824 #else
00825   template<typename _Tp>
00826     inline complex<_Tp>
00827     sin(const complex<_Tp>& __z) { return __complex_sin(__z); }
00828 #endif
00829 
00830   // 26.2.8/11 sinh(__z): Returns the hyperbolic sine of __z.
00831   template<typename _Tp>
00832     inline complex<_Tp>
00833     __complex_sinh(const complex<_Tp>& __z)
00834     {
00835       const _Tp __x = __z.real();
00836       const _Tp  __y = __z.imag();
00837       return complex<_Tp>(sinh(__x) * cos(__y), cosh(__x) * sin(__y));
00838     }
00839 
00840 #if _GLIBCXX_USE_C99_COMPLEX
00841   inline __complex__ float
00842   __complex_sinh(__complex__ float __z) { return __builtin_csinhf(__z); }
00843 
00844   inline __complex__ double
00845   __complex_sinh(__complex__ double __z) { return __builtin_csinh(__z); }
00846 
00847   inline __complex__ long double
00848   __complex_sinh(const __complex__ long double& __z)
00849   { return __builtin_csinhl(__z); }
00850 
00851   template<typename _Tp>
00852     inline complex<_Tp>
00853     sinh(const complex<_Tp>& __z) { return __complex_sinh(__z.__rep()); }
00854 #else
00855   template<typename _Tp>
00856     inline complex<_Tp>
00857     sinh(const complex<_Tp>& __z) { return __complex_sinh(__z); }
00858 #endif
00859 
00860   // 26.2.8/13 sqrt(__z): Returns the complex square root of __z.
00861   //                     The branch cut is on the negative axis.
00862   template<typename _Tp>
00863     complex<_Tp>
00864     __complex_sqrt(const complex<_Tp>& __z)
00865     {
00866       _Tp __x = __z.real();
00867       _Tp __y = __z.imag();
00868 
00869       if (__x == _Tp())
00870         {
00871           _Tp __t = sqrt(abs(__y) / 2);
00872           return complex<_Tp>(__t, __y < _Tp() ? -__t : __t);
00873         }
00874       else
00875         {
00876           _Tp __t = sqrt(2 * (std::abs(__z) + abs(__x)));
00877           _Tp __u = __t / 2;
00878           return __x > _Tp()
00879             ? complex<_Tp>(__u, __y / __t)
00880             : complex<_Tp>(abs(__y) / __t, __y < _Tp() ? -__u : __u);
00881         }
00882     }
00883 
00884 #if _GLIBCXX_USE_C99_COMPLEX
00885   inline __complex__ float
00886   __complex_sqrt(__complex__ float __z) { return __builtin_csqrtf(__z); }
00887 
00888   inline __complex__ double
00889   __complex_sqrt(__complex__ double __z) { return __builtin_csqrt(__z); }
00890 
00891   inline __complex__ long double
00892   __complex_sqrt(const __complex__ long double& __z)
00893   { return __builtin_csqrtl(__z); }
00894 
00895   template<typename _Tp>
00896     inline complex<_Tp>
00897     sqrt(const complex<_Tp>& __z) { return __complex_sqrt(__z.__rep()); }
00898 #else
00899   template<typename _Tp>
00900     inline complex<_Tp>
00901     sqrt(const complex<_Tp>& __z) { return __complex_sqrt(__z); }
00902 #endif
00903 
00904   // 26.2.8/14 tan(__z):  Return the complex tangent of __z.
00905 
00906   template<typename _Tp>
00907     inline complex<_Tp>
00908     __complex_tan(const complex<_Tp>& __z)
00909     { return std::sin(__z) / std::cos(__z); }
00910 
00911 #if _GLIBCXX_USE_C99_COMPLEX
00912   inline __complex__ float
00913   __complex_tan(__complex__ float __z) { return __builtin_ctanf(__z); }
00914 
00915   inline __complex__ double
00916   __complex_tan(__complex__ double __z) { return __builtin_ctan(__z); }
00917 
00918   inline __complex__ long double
00919   __complex_tan(const __complex__ long double& __z)
00920   { return __builtin_ctanl(__z); }
00921 
00922   template<typename _Tp>
00923     inline complex<_Tp>
00924     tan(const complex<_Tp>& __z) { return __complex_tan(__z.__rep()); }
00925 #else
00926   template<typename _Tp>
00927     inline complex<_Tp>
00928     tan(const complex<_Tp>& __z) { return __complex_tan(__z); }
00929 #endif
00930 
00931 
00932   // 26.2.8/15 tanh(__z):  Returns the hyperbolic tangent of __z.
00933 
00934   template<typename _Tp>
00935     inline complex<_Tp>
00936     __complex_tanh(const complex<_Tp>& __z)
00937     { return std::sinh(__z) / std::cosh(__z); }
00938 
00939 #if _GLIBCXX_USE_C99_COMPLEX
00940   inline __complex__ float
00941   __complex_tanh(__complex__ float __z) { return __builtin_ctanhf(__z); }
00942 
00943   inline __complex__ double
00944   __complex_tanh(__complex__ double __z) { return __builtin_ctanh(__z); }
00945 
00946   inline __complex__ long double
00947   __complex_tanh(const __complex__ long double& __z)
00948   { return __builtin_ctanhl(__z); }
00949 
00950   template<typename _Tp>
00951     inline complex<_Tp>
00952     tanh(const complex<_Tp>& __z) { return __complex_tanh(__z.__rep()); }
00953 #else
00954   template<typename _Tp>
00955     inline complex<_Tp>
00956     tanh(const complex<_Tp>& __z) { return __complex_tanh(__z); }
00957 #endif
00958 
00959 
00960   // 26.2.8/9  pow(__x, __y): Returns the complex power base of __x
00961   //                          raised to the __y-th power.  The branch
00962   //                          cut is on the negative axis.
00963   template<typename _Tp>
00964     complex<_Tp>
00965     __complex_pow_unsigned(complex<_Tp> __x, unsigned __n)
00966     {
00967       complex<_Tp> __y = __n % 2 ? __x : complex<_Tp>(1);
00968 
00969       while (__n >>= 1)
00970         {
00971           __x *= __x;
00972           if (__n % 2)
00973             __y *= __x;
00974         }
00975 
00976       return __y;
00977     }
00978 
00979   // In C++11 mode we used to implement the resolution of
00980   // DR 844. complex pow return type is ambiguous.
00981   // thus the following overload was disabled in that mode.  However, doing
00982   // that causes all sorts of issues, see, for example:
00983   //   http://gcc.gnu.org/ml/libstdc++/2013-01/msg00058.html
00984   // and also PR57974.
00985   template<typename _Tp>
00986     inline complex<_Tp>
00987     pow(const complex<_Tp>& __z, int __n)
00988     {
00989       return __n < 0
00990         ? complex<_Tp>(1) / std::__complex_pow_unsigned(__z, -(unsigned)__n)
00991         : std::__complex_pow_unsigned(__z, __n);
00992     }
00993 
00994   template<typename _Tp>
00995     complex<_Tp>
00996     pow(const complex<_Tp>& __x, const _Tp& __y)
00997     {
00998 #if ! _GLIBCXX_USE_C99_COMPLEX
00999       if (__x == _Tp())
01000         return _Tp();
01001 #endif
01002       if (__x.imag() == _Tp() && __x.real() > _Tp())
01003         return pow(__x.real(), __y);
01004 
01005       complex<_Tp> __t = std::log(__x);
01006       return std::polar<_Tp>(exp(__y * __t.real()), __y * __t.imag());
01007     }
01008 
01009   template<typename _Tp>
01010     inline complex<_Tp>
01011     __complex_pow(const complex<_Tp>& __x, const complex<_Tp>& __y)
01012     { return __x == _Tp() ? _Tp() : std::exp(__y * std::log(__x)); }
01013 
01014 #if _GLIBCXX_USE_C99_COMPLEX
01015   inline __complex__ float
01016   __complex_pow(__complex__ float __x, __complex__ float __y)
01017   { return __builtin_cpowf(__x, __y); }
01018 
01019   inline __complex__ double
01020   __complex_pow(__complex__ double __x, __complex__ double __y)
01021   { return __builtin_cpow(__x, __y); }
01022 
01023   inline __complex__ long double
01024   __complex_pow(const __complex__ long double& __x,
01025                 const __complex__ long double& __y)
01026   { return __builtin_cpowl(__x, __y); }
01027 
01028   template<typename _Tp>
01029     inline complex<_Tp>
01030     pow(const complex<_Tp>& __x, const complex<_Tp>& __y)
01031     { return __complex_pow(__x.__rep(), __y.__rep()); }
01032 #else
01033   template<typename _Tp>
01034     inline complex<_Tp>
01035     pow(const complex<_Tp>& __x, const complex<_Tp>& __y)
01036     { return __complex_pow(__x, __y); }
01037 #endif
01038 
01039   template<typename _Tp>
01040     inline complex<_Tp>
01041     pow(const _Tp& __x, const complex<_Tp>& __y)
01042     {
01043       return __x > _Tp() ? std::polar<_Tp>(pow(__x, __y.real()),
01044                                            __y.imag() * log(__x))
01045                          : std::pow(complex<_Tp>(__x), __y);
01046     }
01047 
01048   /// 26.2.3  complex specializations
01049   /// complex<float> specialization
01050   template<>
01051     struct complex<float>
01052     {
01053       typedef float value_type;
01054       typedef __complex__ float _ComplexT;
01055 
01056       _GLIBCXX_CONSTEXPR complex(_ComplexT __z) : _M_value(__z) { }
01057 
01058       _GLIBCXX_CONSTEXPR complex(float __r = 0.0f, float __i = 0.0f)
01059 #if __cplusplus >= 201103L
01060       : _M_value{ __r, __i } { }
01061 #else
01062       {
01063         __real__ _M_value = __r;
01064         __imag__ _M_value = __i;
01065       }
01066 #endif
01067 
01068       explicit _GLIBCXX_CONSTEXPR complex(const complex<double>&);
01069       explicit _GLIBCXX_CONSTEXPR complex(const complex<long double>&);
01070 
01071 #if __cplusplus >= 201103L
01072       // _GLIBCXX_RESOLVE_LIB_DEFECTS
01073       // DR 387. std::complex over-encapsulated.
01074       __attribute ((__abi_tag__ ("cxx11")))
01075       constexpr float
01076       real() const { return __real__ _M_value; }
01077 
01078       __attribute ((__abi_tag__ ("cxx11")))
01079       constexpr float
01080       imag() const { return __imag__ _M_value; }
01081 #else
01082       float&
01083       real() { return __real__ _M_value; }
01084 
01085       const float&
01086       real() const { return __real__ _M_value; }
01087 
01088       float&
01089       imag() { return __imag__ _M_value; }
01090 
01091       const float&
01092       imag() const { return __imag__ _M_value; }
01093 #endif
01094 
01095       // _GLIBCXX_RESOLVE_LIB_DEFECTS
01096       // DR 387. std::complex over-encapsulated.
01097       void
01098       real(float __val) { __real__ _M_value = __val; }
01099 
01100       void
01101       imag(float __val) { __imag__ _M_value = __val; }
01102 
01103       complex&
01104       operator=(float __f)
01105       {
01106         _M_value = __f;
01107         return *this;
01108       }
01109 
01110       complex&
01111       operator+=(float __f)
01112       {
01113         _M_value += __f;
01114         return *this;
01115       }
01116 
01117       complex&
01118       operator-=(float __f)
01119       {
01120         _M_value -= __f;
01121         return *this;
01122       }
01123 
01124       complex&
01125       operator*=(float __f)
01126       {
01127         _M_value *= __f;
01128         return *this;
01129       }
01130 
01131       complex&
01132       operator/=(float __f)
01133       {
01134         _M_value /= __f;
01135         return *this;
01136       }
01137 
01138       // Let the compiler synthesize the copy and assignment
01139       // operator.  It always does a pretty good job.
01140       // complex& operator=(const complex&);
01141 
01142       template<typename _Tp>
01143         complex&
01144         operator=(const complex<_Tp>&  __z)
01145         {
01146           __real__ _M_value = __z.real();
01147           __imag__ _M_value = __z.imag();
01148           return *this;
01149         }
01150 
01151       template<typename _Tp>
01152         complex&
01153         operator+=(const complex<_Tp>& __z)
01154         {
01155           __real__ _M_value += __z.real();
01156           __imag__ _M_value += __z.imag();
01157           return *this;
01158         }
01159 
01160       template<class _Tp>
01161         complex&
01162         operator-=(const complex<_Tp>& __z)
01163         {
01164           __real__ _M_value -= __z.real();
01165           __imag__ _M_value -= __z.imag();
01166           return *this;
01167         }
01168 
01169       template<class _Tp>
01170         complex&
01171         operator*=(const complex<_Tp>& __z)
01172         {
01173           _ComplexT __t;
01174           __real__ __t = __z.real();
01175           __imag__ __t = __z.imag();
01176           _M_value *= __t;
01177           return *this;
01178         }
01179 
01180       template<class _Tp>
01181         complex&
01182         operator/=(const complex<_Tp>& __z)
01183         {
01184           _ComplexT __t;
01185           __real__ __t = __z.real();
01186           __imag__ __t = __z.imag();
01187           _M_value /= __t;
01188           return *this;
01189         }
01190 
01191       _GLIBCXX_CONSTEXPR _ComplexT __rep() const { return _M_value; }
01192 
01193     private:
01194       _ComplexT _M_value;
01195     };
01196 
01197   /// 26.2.3  complex specializations
01198   /// complex<double> specialization
01199   template<>
01200     struct complex<double>
01201     {
01202       typedef double value_type;
01203       typedef __complex__ double _ComplexT;
01204 
01205       _GLIBCXX_CONSTEXPR complex(_ComplexT __z) : _M_value(__z) { }
01206 
01207       _GLIBCXX_CONSTEXPR complex(double __r = 0.0, double __i = 0.0)
01208 #if __cplusplus >= 201103L
01209       : _M_value{ __r, __i } { }
01210 #else
01211       {
01212         __real__ _M_value = __r;
01213         __imag__ _M_value = __i;
01214       }
01215 #endif
01216 
01217       _GLIBCXX_CONSTEXPR complex(const complex<float>& __z)
01218       : _M_value(__z.__rep()) { }
01219 
01220       explicit _GLIBCXX_CONSTEXPR complex(const complex<long double>&);
01221 
01222 #if __cplusplus >= 201103L
01223       // _GLIBCXX_RESOLVE_LIB_DEFECTS
01224       // DR 387. std::complex over-encapsulated.
01225       __attribute ((__abi_tag__ ("cxx11")))
01226       constexpr double
01227       real() const { return __real__ _M_value; }
01228 
01229       __attribute ((__abi_tag__ ("cxx11")))
01230       constexpr double
01231       imag() const { return __imag__ _M_value; }
01232 #else
01233       double&
01234       real() { return __real__ _M_value; }
01235 
01236       const double&
01237       real() const { return __real__ _M_value; }
01238 
01239       double&
01240       imag() { return __imag__ _M_value; }
01241 
01242       const double&
01243       imag() const { return __imag__ _M_value; }
01244 #endif
01245 
01246       // _GLIBCXX_RESOLVE_LIB_DEFECTS
01247       // DR 387. std::complex over-encapsulated.
01248       void
01249       real(double __val) { __real__ _M_value = __val; }
01250 
01251       void
01252       imag(double __val) { __imag__ _M_value = __val; }
01253 
01254       complex&
01255       operator=(double __d)
01256       {
01257         _M_value = __d;
01258         return *this;
01259       }
01260 
01261       complex&
01262       operator+=(double __d)
01263       {
01264         _M_value += __d;
01265         return *this;
01266       }
01267 
01268       complex&
01269       operator-=(double __d)
01270       {
01271         _M_value -= __d;
01272         return *this;
01273       }
01274 
01275       complex&
01276       operator*=(double __d)
01277       {
01278         _M_value *= __d;
01279         return *this;
01280       }
01281 
01282       complex&
01283       operator/=(double __d)
01284       {
01285         _M_value /= __d;
01286         return *this;
01287       }
01288 
01289       // The compiler will synthesize this, efficiently.
01290       // complex& operator=(const complex&);
01291 
01292       template<typename _Tp>
01293         complex&
01294         operator=(const complex<_Tp>& __z)
01295         {
01296           __real__ _M_value = __z.real();
01297           __imag__ _M_value = __z.imag();
01298           return *this;
01299         }
01300 
01301       template<typename _Tp>
01302         complex&
01303         operator+=(const complex<_Tp>& __z)
01304         {
01305           __real__ _M_value += __z.real();
01306           __imag__ _M_value += __z.imag();
01307           return *this;
01308         }
01309 
01310       template<typename _Tp>
01311         complex&
01312         operator-=(const complex<_Tp>& __z)
01313         {
01314           __real__ _M_value -= __z.real();
01315           __imag__ _M_value -= __z.imag();
01316           return *this;
01317         }
01318 
01319       template<typename _Tp>
01320         complex&
01321         operator*=(const complex<_Tp>& __z)
01322         {
01323           _ComplexT __t;
01324           __real__ __t = __z.real();
01325           __imag__ __t = __z.imag();
01326           _M_value *= __t;
01327           return *this;
01328         }
01329 
01330       template<typename _Tp>
01331         complex&
01332         operator/=(const complex<_Tp>& __z)
01333         {
01334           _ComplexT __t;
01335           __real__ __t = __z.real();
01336           __imag__ __t = __z.imag();
01337           _M_value /= __t;
01338           return *this;
01339         }
01340 
01341       _GLIBCXX_CONSTEXPR _ComplexT __rep() const { return _M_value; }
01342 
01343     private:
01344       _ComplexT _M_value;
01345     };
01346 
01347   /// 26.2.3  complex specializations
01348   /// complex<long double> specialization
01349   template<>
01350     struct complex<long double>
01351     {
01352       typedef long double value_type;
01353       typedef __complex__ long double _ComplexT;
01354 
01355       _GLIBCXX_CONSTEXPR complex(_ComplexT __z) : _M_value(__z) { }
01356 
01357       _GLIBCXX_CONSTEXPR complex(long double __r = 0.0L,
01358                                  long double __i = 0.0L)
01359 #if __cplusplus >= 201103L
01360       : _M_value{ __r, __i } { }
01361 #else
01362       {
01363         __real__ _M_value = __r;
01364         __imag__ _M_value = __i;
01365       }
01366 #endif
01367 
01368       _GLIBCXX_CONSTEXPR complex(const complex<float>& __z)
01369       : _M_value(__z.__rep()) { }
01370 
01371       _GLIBCXX_CONSTEXPR complex(const complex<double>& __z)
01372       : _M_value(__z.__rep()) { }
01373 
01374 #if __cplusplus >= 201103L
01375       // _GLIBCXX_RESOLVE_LIB_DEFECTS
01376       // DR 387. std::complex over-encapsulated.
01377       __attribute ((__abi_tag__ ("cxx11")))
01378       constexpr long double
01379       real() const { return __real__ _M_value; }
01380 
01381       __attribute ((__abi_tag__ ("cxx11")))
01382       constexpr long double
01383       imag() const { return __imag__ _M_value; }
01384 #else
01385       long double&
01386       real() { return __real__ _M_value; }
01387 
01388       const long double&
01389       real() const { return __real__ _M_value; }
01390 
01391       long double&
01392       imag() { return __imag__ _M_value; }
01393 
01394       const long double&
01395       imag() const { return __imag__ _M_value; }
01396 #endif
01397 
01398       // _GLIBCXX_RESOLVE_LIB_DEFECTS
01399       // DR 387. std::complex over-encapsulated.
01400       void
01401       real(long double __val) { __real__ _M_value = __val; }
01402 
01403       void
01404       imag(long double __val) { __imag__ _M_value = __val; }
01405 
01406       complex&
01407       operator=(long double __r)
01408       {
01409         _M_value = __r;
01410         return *this;
01411       }
01412 
01413       complex&
01414       operator+=(long double __r)
01415       {
01416         _M_value += __r;
01417         return *this;
01418       }
01419 
01420       complex&
01421       operator-=(long double __r)
01422       {
01423         _M_value -= __r;
01424         return *this;
01425       }
01426 
01427       complex&
01428       operator*=(long double __r)
01429       {
01430         _M_value *= __r;
01431         return *this;
01432       }
01433 
01434       complex&
01435       operator/=(long double __r)
01436       {
01437         _M_value /= __r;
01438         return *this;
01439       }
01440 
01441       // The compiler knows how to do this efficiently
01442       // complex& operator=(const complex&);
01443 
01444       template<typename _Tp>
01445         complex&
01446         operator=(const complex<_Tp>& __z)
01447         {
01448           __real__ _M_value = __z.real();
01449           __imag__ _M_value = __z.imag();
01450           return *this;
01451         }
01452 
01453       template<typename _Tp>
01454         complex&
01455         operator+=(const complex<_Tp>& __z)
01456         {
01457           __real__ _M_value += __z.real();
01458           __imag__ _M_value += __z.imag();
01459           return *this;
01460         }
01461 
01462       template<typename _Tp>
01463         complex&
01464         operator-=(const complex<_Tp>& __z)
01465         {
01466           __real__ _M_value -= __z.real();
01467           __imag__ _M_value -= __z.imag();
01468           return *this;
01469         }
01470 
01471       template<typename _Tp>
01472         complex&
01473         operator*=(const complex<_Tp>& __z)
01474         {
01475           _ComplexT __t;
01476           __real__ __t = __z.real();
01477           __imag__ __t = __z.imag();
01478           _M_value *= __t;
01479           return *this;
01480         }
01481 
01482       template<typename _Tp>
01483         complex&
01484         operator/=(const complex<_Tp>& __z)
01485         {
01486           _ComplexT __t;
01487           __real__ __t = __z.real();
01488           __imag__ __t = __z.imag();
01489           _M_value /= __t;
01490           return *this;
01491         }
01492 
01493       _GLIBCXX_CONSTEXPR _ComplexT __rep() const { return _M_value; }
01494 
01495     private:
01496       _ComplexT _M_value;
01497     };
01498 
01499   // These bits have to be at the end of this file, so that the
01500   // specializations have all been defined.
01501   inline _GLIBCXX_CONSTEXPR
01502   complex<float>::complex(const complex<double>& __z)
01503   : _M_value(__z.__rep()) { }
01504 
01505   inline _GLIBCXX_CONSTEXPR
01506   complex<float>::complex(const complex<long double>& __z)
01507   : _M_value(__z.__rep()) { }
01508 
01509   inline _GLIBCXX_CONSTEXPR
01510   complex<double>::complex(const complex<long double>& __z)
01511   : _M_value(__z.__rep()) { }
01512 
01513   // Inhibit implicit instantiations for required instantiations,
01514   // which are defined via explicit instantiations elsewhere.
01515   // NB:  This syntax is a GNU extension.
01516 #if _GLIBCXX_EXTERN_TEMPLATE
01517   extern template istream& operator>>(istream&, complex<float>&);
01518   extern template ostream& operator<<(ostream&, const complex<float>&);
01519   extern template istream& operator>>(istream&, complex<double>&);
01520   extern template ostream& operator<<(ostream&, const complex<double>&);
01521   extern template istream& operator>>(istream&, complex<long double>&);
01522   extern template ostream& operator<<(ostream&, const complex<long double>&);
01523 
01524 #ifdef _GLIBCXX_USE_WCHAR_T
01525   extern template wistream& operator>>(wistream&, complex<float>&);
01526   extern template wostream& operator<<(wostream&, const complex<float>&);
01527   extern template wistream& operator>>(wistream&, complex<double>&);
01528   extern template wostream& operator<<(wostream&, const complex<double>&);
01529   extern template wistream& operator>>(wistream&, complex<long double>&);
01530   extern template wostream& operator<<(wostream&, const complex<long double>&);
01531 #endif
01532 #endif
01533 
01534   // @} group complex_numbers
01535 
01536 _GLIBCXX_END_NAMESPACE_VERSION
01537 } // namespace
01538 
01539 namespace __gnu_cxx _GLIBCXX_VISIBILITY(default)
01540 {
01541 _GLIBCXX_BEGIN_NAMESPACE_VERSION
01542 
01543   // See ext/type_traits.h for the primary template.
01544   template<typename _Tp, typename _Up>
01545     struct __promote_2<std::complex<_Tp>, _Up>
01546     {
01547     public:
01548       typedef std::complex<typename __promote_2<_Tp, _Up>::__type> __type;
01549     };
01550 
01551   template<typename _Tp, typename _Up>
01552     struct __promote_2<_Tp, std::complex<_Up> >
01553     {
01554     public:
01555       typedef std::complex<typename __promote_2<_Tp, _Up>::__type> __type;
01556     };
01557 
01558   template<typename _Tp, typename _Up>
01559     struct __promote_2<std::complex<_Tp>, std::complex<_Up> >
01560     {
01561     public:
01562       typedef std::complex<typename __promote_2<_Tp, _Up>::__type> __type;
01563     };
01564 
01565 _GLIBCXX_END_NAMESPACE_VERSION
01566 } // namespace
01567 
01568 #if __cplusplus >= 201103L
01569 
01570 namespace std _GLIBCXX_VISIBILITY(default)
01571 {
01572 _GLIBCXX_BEGIN_NAMESPACE_VERSION
01573 
01574   // Forward declarations.
01575   template<typename _Tp> std::complex<_Tp> acos(const std::complex<_Tp>&);
01576   template<typename _Tp> std::complex<_Tp> asin(const std::complex<_Tp>&);
01577   template<typename _Tp> std::complex<_Tp> atan(const std::complex<_Tp>&);
01578 
01579   template<typename _Tp> std::complex<_Tp> acosh(const std::complex<_Tp>&);
01580   template<typename _Tp> std::complex<_Tp> asinh(const std::complex<_Tp>&);
01581   template<typename _Tp> std::complex<_Tp> atanh(const std::complex<_Tp>&);
01582   // DR 595.
01583   template<typename _Tp> _Tp               fabs(const std::complex<_Tp>&);
01584 
01585   template<typename _Tp>
01586     inline std::complex<_Tp>
01587     __complex_acos(const std::complex<_Tp>& __z)
01588     {
01589       const std::complex<_Tp> __t = std::asin(__z);
01590       const _Tp __pi_2 = 1.5707963267948966192313216916397514L;
01591       return std::complex<_Tp>(__pi_2 - __t.real(), -__t.imag());
01592     }
01593 
01594 #if _GLIBCXX_USE_C99_COMPLEX_TR1
01595   inline __complex__ float
01596   __complex_acos(__complex__ float __z)
01597   { return __builtin_cacosf(__z); }
01598 
01599   inline __complex__ double
01600   __complex_acos(__complex__ double __z)
01601   { return __builtin_cacos(__z); }
01602 
01603   inline __complex__ long double
01604   __complex_acos(const __complex__ long double& __z)
01605   { return __builtin_cacosl(__z); }
01606 
01607   template<typename _Tp>
01608     inline std::complex<_Tp>
01609     acos(const std::complex<_Tp>& __z)
01610     { return __complex_acos(__z.__rep()); }
01611 #else
01612   /// acos(__z) [8.1.2].
01613   //  Effects:  Behaves the same as C99 function cacos, defined
01614   //            in subclause 7.3.5.1.
01615   template<typename _Tp>
01616     inline std::complex<_Tp>
01617     acos(const std::complex<_Tp>& __z)
01618     { return __complex_acos(__z); }
01619 #endif
01620 
01621   template<typename _Tp>
01622     inline std::complex<_Tp>
01623     __complex_asin(const std::complex<_Tp>& __z)
01624     {
01625       std::complex<_Tp> __t(-__z.imag(), __z.real());
01626       __t = std::asinh(__t);
01627       return std::complex<_Tp>(__t.imag(), -__t.real());
01628     }
01629 
01630 #if _GLIBCXX_USE_C99_COMPLEX_TR1
01631   inline __complex__ float
01632   __complex_asin(__complex__ float __z)
01633   { return __builtin_casinf(__z); }
01634 
01635   inline __complex__ double
01636   __complex_asin(__complex__ double __z)
01637   { return __builtin_casin(__z); }
01638 
01639   inline __complex__ long double
01640   __complex_asin(const __complex__ long double& __z)
01641   { return __builtin_casinl(__z); }
01642 
01643   template<typename _Tp>
01644     inline std::complex<_Tp>
01645     asin(const std::complex<_Tp>& __z)
01646     { return __complex_asin(__z.__rep()); }
01647 #else
01648   /// asin(__z) [8.1.3].
01649   //  Effects:  Behaves the same as C99 function casin, defined
01650   //            in subclause 7.3.5.2.
01651   template<typename _Tp>
01652     inline std::complex<_Tp>
01653     asin(const std::complex<_Tp>& __z)
01654     { return __complex_asin(__z); }
01655 #endif
01656 
01657   template<typename _Tp>
01658     std::complex<_Tp>
01659     __complex_atan(const std::complex<_Tp>& __z)
01660     {
01661       const _Tp __r2 = __z.real() * __z.real();
01662       const _Tp __x = _Tp(1.0) - __r2 - __z.imag() * __z.imag();
01663 
01664       _Tp __num = __z.imag() + _Tp(1.0);
01665       _Tp __den = __z.imag() - _Tp(1.0);
01666 
01667       __num = __r2 + __num * __num;
01668       __den = __r2 + __den * __den;
01669 
01670       return std::complex<_Tp>(_Tp(0.5) * atan2(_Tp(2.0) * __z.real(), __x),
01671                                _Tp(0.25) * log(__num / __den));
01672     }
01673 
01674 #if _GLIBCXX_USE_C99_COMPLEX_TR1
01675   inline __complex__ float
01676   __complex_atan(__complex__ float __z)
01677   { return __builtin_catanf(__z); }
01678 
01679   inline __complex__ double
01680   __complex_atan(__complex__ double __z)
01681   { return __builtin_catan(__z); }
01682 
01683   inline __complex__ long double
01684   __complex_atan(const __complex__ long double& __z)
01685   { return __builtin_catanl(__z); }
01686 
01687   template<typename _Tp>
01688     inline std::complex<_Tp>
01689     atan(const std::complex<_Tp>& __z)
01690     { return __complex_atan(__z.__rep()); }
01691 #else
01692   /// atan(__z) [8.1.4].
01693   //  Effects:  Behaves the same as C99 function catan, defined
01694   //            in subclause 7.3.5.3.
01695   template<typename _Tp>
01696     inline std::complex<_Tp>
01697     atan(const std::complex<_Tp>& __z)
01698     { return __complex_atan(__z); }
01699 #endif
01700 
01701   template<typename _Tp>
01702     std::complex<_Tp>
01703     __complex_acosh(const std::complex<_Tp>& __z)
01704     {
01705       // Kahan's formula.
01706       return _Tp(2.0) * std::log(std::sqrt(_Tp(0.5) * (__z + _Tp(1.0)))
01707                                  + std::sqrt(_Tp(0.5) * (__z - _Tp(1.0))));
01708     }
01709 
01710 #if _GLIBCXX_USE_C99_COMPLEX_TR1
01711   inline __complex__ float
01712   __complex_acosh(__complex__ float __z)
01713   { return __builtin_cacoshf(__z); }
01714 
01715   inline __complex__ double
01716   __complex_acosh(__complex__ double __z)
01717   { return __builtin_cacosh(__z); }
01718 
01719   inline __complex__ long double
01720   __complex_acosh(const __complex__ long double& __z)
01721   { return __builtin_cacoshl(__z); }
01722 
01723   template<typename _Tp>
01724     inline std::complex<_Tp>
01725     acosh(const std::complex<_Tp>& __z)
01726     { return __complex_acosh(__z.__rep()); }
01727 #else
01728   /// acosh(__z) [8.1.5].
01729   //  Effects:  Behaves the same as C99 function cacosh, defined
01730   //            in subclause 7.3.6.1.
01731   template<typename _Tp>
01732     inline std::complex<_Tp>
01733     acosh(const std::complex<_Tp>& __z)
01734     { return __complex_acosh(__z); }
01735 #endif
01736 
01737   template<typename _Tp>
01738     std::complex<_Tp>
01739     __complex_asinh(const std::complex<_Tp>& __z)
01740     {
01741       std::complex<_Tp> __t((__z.real() - __z.imag())
01742                             * (__z.real() + __z.imag()) + _Tp(1.0),
01743                             _Tp(2.0) * __z.real() * __z.imag());
01744       __t = std::sqrt(__t);
01745 
01746       return std::log(__t + __z);
01747     }
01748 
01749 #if _GLIBCXX_USE_C99_COMPLEX_TR1
01750   inline __complex__ float
01751   __complex_asinh(__complex__ float __z)
01752   { return __builtin_casinhf(__z); }
01753 
01754   inline __complex__ double
01755   __complex_asinh(__complex__ double __z)
01756   { return __builtin_casinh(__z); }
01757 
01758   inline __complex__ long double
01759   __complex_asinh(const __complex__ long double& __z)
01760   { return __builtin_casinhl(__z); }
01761 
01762   template<typename _Tp>
01763     inline std::complex<_Tp>
01764     asinh(const std::complex<_Tp>& __z)
01765     { return __complex_asinh(__z.__rep()); }
01766 #else
01767   /// asinh(__z) [8.1.6].
01768   //  Effects:  Behaves the same as C99 function casin, defined
01769   //            in subclause 7.3.6.2.
01770   template<typename _Tp>
01771     inline std::complex<_Tp>
01772     asinh(const std::complex<_Tp>& __z)
01773     { return __complex_asinh(__z); }
01774 #endif
01775 
01776   template<typename _Tp>
01777     std::complex<_Tp>
01778     __complex_atanh(const std::complex<_Tp>& __z)
01779     {
01780       const _Tp __i2 = __z.imag() * __z.imag();
01781       const _Tp __x = _Tp(1.0) - __i2 - __z.real() * __z.real();
01782 
01783       _Tp __num = _Tp(1.0) + __z.real();
01784       _Tp __den = _Tp(1.0) - __z.real();
01785 
01786       __num = __i2 + __num * __num;
01787       __den = __i2 + __den * __den;
01788 
01789       return std::complex<_Tp>(_Tp(0.25) * (log(__num) - log(__den)),
01790                                _Tp(0.5) * atan2(_Tp(2.0) * __z.imag(), __x));
01791     }
01792 
01793 #if _GLIBCXX_USE_C99_COMPLEX_TR1
01794   inline __complex__ float
01795   __complex_atanh(__complex__ float __z)
01796   { return __builtin_catanhf(__z); }
01797 
01798   inline __complex__ double
01799   __complex_atanh(__complex__ double __z)
01800   { return __builtin_catanh(__z); }
01801 
01802   inline __complex__ long double
01803   __complex_atanh(const __complex__ long double& __z)
01804   { return __builtin_catanhl(__z); }
01805 
01806   template<typename _Tp>
01807     inline std::complex<_Tp>
01808     atanh(const std::complex<_Tp>& __z)
01809     { return __complex_atanh(__z.__rep()); }
01810 #else
01811   /// atanh(__z) [8.1.7].
01812   //  Effects:  Behaves the same as C99 function catanh, defined
01813   //            in subclause 7.3.6.3.
01814   template<typename _Tp>
01815     inline std::complex<_Tp>
01816     atanh(const std::complex<_Tp>& __z)
01817     { return __complex_atanh(__z); }
01818 #endif
01819 
01820   template<typename _Tp>
01821     inline _Tp
01822     /// fabs(__z) [8.1.8].
01823     //  Effects:  Behaves the same as C99 function cabs, defined
01824     //            in subclause 7.3.8.1.
01825     fabs(const std::complex<_Tp>& __z)
01826     { return std::abs(__z); }
01827 
01828   /// Additional overloads [8.1.9].
01829   template<typename _Tp>
01830     inline typename __gnu_cxx::__promote<_Tp>::__type
01831     arg(_Tp __x)
01832     {
01833       typedef typename __gnu_cxx::__promote<_Tp>::__type __type;
01834 #if (_GLIBCXX11_USE_C99_MATH && !_GLIBCXX_USE_C99_FP_MACROS_DYNAMIC)
01835       return std::signbit(__x) ? __type(3.1415926535897932384626433832795029L)
01836                                : __type();
01837 #else
01838       return std::arg(std::complex<__type>(__x));
01839 #endif
01840     }
01841 
01842   template<typename _Tp>
01843     _GLIBCXX_CONSTEXPR inline typename __gnu_cxx::__promote<_Tp>::__type
01844     imag(_Tp)
01845     { return _Tp(); }
01846 
01847   template<typename _Tp>
01848     inline typename __gnu_cxx::__promote<_Tp>::__type
01849     norm(_Tp __x)
01850     {
01851       typedef typename __gnu_cxx::__promote<_Tp>::__type __type;
01852       return __type(__x) * __type(__x);
01853     }
01854 
01855   template<typename _Tp>
01856     _GLIBCXX_CONSTEXPR inline typename __gnu_cxx::__promote<_Tp>::__type
01857     real(_Tp __x)
01858     { return __x; }
01859 
01860   template<typename _Tp, typename _Up>
01861     inline std::complex<typename __gnu_cxx::__promote_2<_Tp, _Up>::__type>
01862     pow(const std::complex<_Tp>& __x, const _Up& __y)
01863     {
01864       typedef typename __gnu_cxx::__promote_2<_Tp, _Up>::__type __type;
01865       return std::pow(std::complex<__type>(__x), __type(__y));
01866     }
01867 
01868   template<typename _Tp, typename _Up>
01869     inline std::complex<typename __gnu_cxx::__promote_2<_Tp, _Up>::__type>
01870     pow(const _Tp& __x, const std::complex<_Up>& __y)
01871     {
01872       typedef typename __gnu_cxx::__promote_2<_Tp, _Up>::__type __type;
01873       return std::pow(__type(__x), std::complex<__type>(__y));
01874     }
01875 
01876   template<typename _Tp, typename _Up>
01877     inline std::complex<typename __gnu_cxx::__promote_2<_Tp, _Up>::__type>
01878     pow(const std::complex<_Tp>& __x, const std::complex<_Up>& __y)
01879     {
01880       typedef typename __gnu_cxx::__promote_2<_Tp, _Up>::__type __type;
01881       return std::pow(std::complex<__type>(__x),
01882                       std::complex<__type>(__y));
01883     }
01884 
01885   // Forward declarations.
01886   // DR 781.
01887   template<typename _Tp> std::complex<_Tp> proj(const std::complex<_Tp>&);
01888 
01889   template<typename _Tp>
01890     std::complex<_Tp>
01891     __complex_proj(const std::complex<_Tp>& __z)
01892     {
01893       const _Tp __den = (__z.real() * __z.real()
01894                          + __z.imag() * __z.imag() + _Tp(1.0));
01895 
01896       return std::complex<_Tp>((_Tp(2.0) * __z.real()) / __den,
01897                                (_Tp(2.0) * __z.imag()) / __den);
01898     }
01899 
01900 #if _GLIBCXX_USE_C99_COMPLEX
01901   inline __complex__ float
01902   __complex_proj(__complex__ float __z)
01903   { return __builtin_cprojf(__z); }
01904 
01905   inline __complex__ double
01906   __complex_proj(__complex__ double __z)
01907   { return __builtin_cproj(__z); }
01908 
01909   inline __complex__ long double
01910   __complex_proj(const __complex__ long double& __z)
01911   { return __builtin_cprojl(__z); }
01912 
01913   template<typename _Tp>
01914     inline std::complex<_Tp>
01915     proj(const std::complex<_Tp>& __z)
01916     { return __complex_proj(__z.__rep()); }
01917 #else
01918   template<typename _Tp>
01919     inline std::complex<_Tp>
01920     proj(const std::complex<_Tp>& __z)
01921     { return __complex_proj(__z); }
01922 #endif
01923 
01924   template<typename _Tp>
01925     inline std::complex<typename __gnu_cxx::__promote<_Tp>::__type>
01926     proj(_Tp __x)
01927     {
01928       typedef typename __gnu_cxx::__promote<_Tp>::__type __type;
01929       return std::proj(std::complex<__type>(__x));
01930     }
01931 
01932   template<typename _Tp>
01933     inline std::complex<typename __gnu_cxx::__promote<_Tp>::__type>
01934     conj(_Tp __x)
01935     {
01936       typedef typename __gnu_cxx::__promote<_Tp>::__type __type;
01937       return std::complex<__type>(__x, -__type());
01938     }
01939 
01940 _GLIBCXX_END_NAMESPACE_VERSION
01941 
01942 #if __cplusplus > 201103L
01943 
01944 inline namespace literals {
01945 inline namespace complex_literals {
01946 _GLIBCXX_BEGIN_NAMESPACE_VERSION
01947 
01948 #define __cpp_lib_complex_udls 201309
01949 
01950   constexpr std::complex<float>
01951   operator""if(long double __num)
01952   { return std::complex<float>{0.0F, static_cast<float>(__num)}; }
01953 
01954   constexpr std::complex<float>
01955   operator""if(unsigned long long __num)
01956   { return std::complex<float>{0.0F, static_cast<float>(__num)}; }
01957 
01958   constexpr std::complex<double>
01959   operator""i(long double __num)
01960   { return std::complex<double>{0.0, static_cast<double>(__num)}; }
01961 
01962   constexpr std::complex<double>
01963   operator""i(unsigned long long __num)
01964   { return std::complex<double>{0.0, static_cast<double>(__num)}; }
01965 
01966   constexpr std::complex<long double>
01967   operator""il(long double __num)
01968   { return std::complex<long double>{0.0L, __num}; }
01969 
01970   constexpr std::complex<long double>
01971   operator""il(unsigned long long __num)
01972   { return std::complex<long double>{0.0L, static_cast<long double>(__num)}; }
01973 
01974 _GLIBCXX_END_NAMESPACE_VERSION
01975 } // inline namespace complex_literals
01976 } // inline namespace literals
01977 
01978 #endif // C++14
01979 
01980 } // namespace
01981 
01982 #endif  // C++11
01983 
01984 #endif  /* _GLIBCXX_COMPLEX */