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