# complex

Go to the documentation of this file.

    00001 // The template and inlines for the -*- C++ -*- complex number classes.
    00002
    00003 // Copyright (C) 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005,
    00004 // 2006, 2007, 2008, 2009, 2010
    00005 // Free Software Foundation, Inc.
    00006 //
    00007 // This file is part of the GNU ISO C++ Library.  This library is free
    00008 // software; you can redistribute it and/or modify it under the
    00009 // terms of the GNU General Public License as published by the
    00010 // Free Software Foundation; either version 3, or (at your option)
    00011 // any later version.
    00012
    00013 // This library is distributed in the hope that it will be useful,
    00014 // but WITHOUT ANY WARRANTY; without even the implied warranty of
    00015 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    00016 // GNU General Public License for more details.
    00017
    00018 // Under Section 7 of GPL version 3, you are granted additional
    00019 // permissions described in the GCC Runtime Library Exception, version
    00020 // 3.1, as published by the Free Software Foundation.
    00021
    00022 // You should have received a copy of the GNU General Public License and
    00023 // a copy of the GCC Runtime Library Exception along with this program;
    00024 // see the files COPYING3 and COPYING.RUNTIME respectively.  If not, see
    00025 // <http://www.gnu.org/licenses/>.
    00026
    00027 /** @file include/complex
    00028  *  This is a Standard C++ Library header.
    00029  */
    00030
    00031 //
    00032 // ISO C++ 14882: 26.2  Complex Numbers
    00033 // Note: this is not a conforming implementation.
    00034 // Initially implemented by Ulrich Drepper <drepper@cygnus.com>
    00035 // Improved by Gabriel Dos Reis <dosreis@cmla.ens-cachan.fr>
    00036 //
    00037
    00038 #ifndef _GLIBCXX_COMPLEX
    00039 #define _GLIBCXX_COMPLEX 1
    00040
    00041 #pragma GCC system_header
    00042
    00043 #include <bits/c++config.h>
    00044 #include <bits/cpp_type_traits.h>
    00045 #include <ext/type_traits.h>
    00046 #include <cmath>
    00047 #include <sstream>
    00048
    00049 _GLIBCXX_BEGIN_NAMESPACE(std)
    00050
    00051   /**
    00052    * @defgroup complex_numbers Complex Numbers
    00053    * @ingroup numerics
    00054    *
    00055    * Classes and functions for complex numbers.
    00056    * @{
    00057    */
    00058
    00059   // Forward declarations.
    00060   template<typename _Tp> class complex;
    00061   template<> class complex<float>;
    00062   template<> class complex<double>;
    00063   template<> class complex<long double>;
    00064
    00065   ///  Return magnitude of @a z.
    00066   template<typename _Tp> _Tp abs(const complex<_Tp>&);
    00067   ///  Return phase angle of @a z.
    00068   template<typename _Tp> _Tp arg(const complex<_Tp>&);
    00069   ///  Return @a z magnitude squared.
    00070   template<typename _Tp> _Tp norm(const complex<_Tp>&);
    00071
    00072   ///  Return complex conjugate of @a z.
    00073   template<typename _Tp> complex<_Tp> conj(const complex<_Tp>&);
    00074   ///  Return complex with magnitude @a rho and angle @a theta.
    00075   template<typename _Tp> complex<_Tp> polar(const _Tp&, const _Tp& = 0);
    00076
    00077   // Transcendentals:
    00078   /// Return complex cosine of @a z.
    00079   template<typename _Tp> complex<_Tp> cos(const complex<_Tp>&);
    00080   /// Return complex hyperbolic cosine of @a z.
    00081   template<typename _Tp> complex<_Tp> cosh(const complex<_Tp>&);
    00082   /// Return complex base e exponential of @a z.
    00083   template<typename _Tp> complex<_Tp> exp(const complex<_Tp>&);
    00084   /// Return complex natural logarithm of @a z.
    00085   template<typename _Tp> complex<_Tp> log(const complex<_Tp>&);
    00086   /// Return complex base 10 logarithm of @a z.
    00087   template<typename _Tp> complex<_Tp> log10(const complex<_Tp>&);
    00088 #ifndef __GXX_EXPERIMENTAL_CXX0X__
    00089   // DR 844.
    00090   /// Return @a x to the @a y'th power.
    00091   template<typename _Tp> complex<_Tp> pow(const complex<_Tp>&, int);
    00092 #endif
    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       complex(const _Tp& __r = _Tp(), const _Tp& __i = _Tp())
    00130       : _M_real(__r), _M_imag(__i) { }
    00131
    00132       // Lets the compiler synthesize the copy constructor
    00133       // complex (const complex<_Tp>&);
    00134       ///  Copy constructor.
    00135       template<typename _Up>
    00136         complex(const complex<_Up>& __z)
    00137     : _M_real(__z.real()), _M_imag(__z.imag()) { }
    00138
    00139 #ifdef __GXX_EXPERIMENTAL_CXX0X__
    00140       // _GLIBCXX_RESOLVE_LIB_DEFECTS
    00141       // DR 387. std::complex over-encapsulated.
    00142       _Tp real() const
    00143       { return _M_real; }
    00144
    00145       _Tp imag() const
    00146       { return _M_imag; }
    00147 #else
    00148       ///  Return real part of complex number.
    00149       _Tp& real()
    00150       { return _M_real; }
    00151
    00152       ///  Return real part of complex number.
    00153       const _Tp& real() const
    00154       { return _M_real; }
    00155
    00156       ///  Return imaginary part of complex number.
    00157       _Tp& imag()
    00158       { return _M_imag; }
    00159
    00160       ///  Return imaginary part of complex number.
    00161       const _Tp& imag() const
    00162       { return _M_imag; }
    00163 #endif
    00164
    00165       // _GLIBCXX_RESOLVE_LIB_DEFECTS
    00166       // DR 387. std::complex over-encapsulated.
    00167       void real(_Tp __val)
    00168       { _M_real = __val; }
    00169
    00170       void imag(_Tp __val)
    00171       { _M_imag = __val; }
    00172
    00173       /// Assign this complex number to scalar @a t.
    00174       complex<_Tp>& operator=(const _Tp&);
    00175
    00176       /// Add @a t to this complex number.
    00177       // 26.2.5/1
    00178       complex<_Tp>&
    00179       operator+=(const _Tp& __t)
    00180       {
    00181     _M_real += __t;
    00182     return *this;
    00183       }
    00184
    00185       /// Subtract @a t from this complex number.
    00186       // 26.2.5/3
    00187       complex<_Tp>&
    00188       operator-=(const _Tp& __t)
    00189       {
    00190     _M_real -= __t;
    00191     return *this;
    00192       }
    00193
    00194       /// Multiply this complex number by @a t.
    00195       complex<_Tp>& operator*=(const _Tp&);
    00196       /// Divide this complex number by @a t.
    00197       complex<_Tp>& operator/=(const _Tp&);
    00198
    00199       // Lets the compiler synthesize the
    00200       // copy and assignment operator
    00201       // complex<_Tp>& operator= (const complex<_Tp>&);
    00202       /// Assign this complex number to complex @a z.
    00203       template<typename _Up>
    00204         complex<_Tp>& operator=(const complex<_Up>&);
    00205       /// Add @a z to this complex number.
    00206       template<typename _Up>
    00207         complex<_Tp>& operator+=(const complex<_Up>&);
    00208       /// Subtract @a z from this complex number.
    00209       template<typename _Up>
    00210         complex<_Tp>& operator-=(const complex<_Up>&);
    00211       /// Multiply this complex number by @a z.
    00212       template<typename _Up>
    00213         complex<_Tp>& operator*=(const complex<_Up>&);
    00214       /// Divide this complex number by @a z.
    00215       template<typename _Up>
    00216         complex<_Tp>& operator/=(const complex<_Up>&);
    00217
    00218       const complex& __rep() const
    00219       { return *this; }
    00220
    00221     private:
    00222       _Tp _M_real;
    00223       _Tp _M_imag;
    00224     };
    00225
    00226   template<typename _Tp>
    00227     complex<_Tp>&
    00228     complex<_Tp>::operator=(const _Tp& __t)
    00229     {
    00230      _M_real = __t;
    00231      _M_imag = _Tp();
    00232      return *this;
    00233     }
    00234
    00235   // 26.2.5/5
    00236   template<typename _Tp>
    00237     complex<_Tp>&
    00238     complex<_Tp>::operator*=(const _Tp& __t)
    00239     {
    00240       _M_real *= __t;
    00241       _M_imag *= __t;
    00242       return *this;
    00243     }
    00244
    00245   // 26.2.5/7
    00246   template<typename _Tp>
    00247     complex<_Tp>&
    00248     complex<_Tp>::operator/=(const _Tp& __t)
    00249     {
    00250       _M_real /= __t;
    00251       _M_imag /= __t;
    00252       return *this;
    00253     }
    00254
    00255   template<typename _Tp>
    00256     template<typename _Up>
    00257     complex<_Tp>&
    00258     complex<_Tp>::operator=(const complex<_Up>& __z)
    00259     {
    00260       _M_real = __z.real();
    00261       _M_imag = __z.imag();
    00262       return *this;
    00263     }
    00264
    00265   // 26.2.5/9
    00266   template<typename _Tp>
    00267     template<typename _Up>
    00268     complex<_Tp>&
    00269     complex<_Tp>::operator+=(const complex<_Up>& __z)
    00270     {
    00271       _M_real += __z.real();
    00272       _M_imag += __z.imag();
    00273       return *this;
    00274     }
    00275
    00276   // 26.2.5/11
    00277   template<typename _Tp>
    00278     template<typename _Up>
    00279     complex<_Tp>&
    00280     complex<_Tp>::operator-=(const complex<_Up>& __z)
    00281     {
    00282       _M_real -= __z.real();
    00283       _M_imag -= __z.imag();
    00284       return *this;
    00285     }
    00286
    00287   // 26.2.5/13
    00288   // XXX: This is a grammar school implementation.
    00289   template<typename _Tp>
    00290     template<typename _Up>
    00291     complex<_Tp>&
    00292     complex<_Tp>::operator*=(const complex<_Up>& __z)
    00293     {
    00294       const _Tp __r = _M_real * __z.real() - _M_imag * __z.imag();
    00295       _M_imag = _M_real * __z.imag() + _M_imag * __z.real();
    00296       _M_real = __r;
    00297       return *this;
    00298     }
    00299
    00300   // 26.2.5/15
    00301   // XXX: This is a grammar school implementation.
    00302   template<typename _Tp>
    00303     template<typename _Up>
    00304     complex<_Tp>&
    00305     complex<_Tp>::operator/=(const complex<_Up>& __z)
    00306     {
    00307       const _Tp __r =  _M_real * __z.real() + _M_imag * __z.imag();
    00308       const _Tp __n = std::norm(__z);
    00309       _M_imag = (_M_imag * __z.real() - _M_real * __z.imag()) / __n;
    00310       _M_real = __r / __n;
    00311       return *this;
    00312     }
    00313
    00314   // Operators:
    00315   //@{
    00316   ///  Return new complex value @a x plus @a y.
    00317   template<typename _Tp>
    00318     inline complex<_Tp>
    00319     operator+(const complex<_Tp>& __x, const complex<_Tp>& __y)
    00320     {
    00321       complex<_Tp> __r = __x;
    00322       __r += __y;
    00323       return __r;
    00324     }
    00325
    00326   template<typename _Tp>
    00327     inline complex<_Tp>
    00328     operator+(const complex<_Tp>& __x, const _Tp& __y)
    00329     {
    00330       complex<_Tp> __r = __x;
    00331       __r += __y;
    00332       return __r;
    00333     }
    00334
    00335   template<typename _Tp>
    00336     inline complex<_Tp>
    00337     operator+(const _Tp& __x, const complex<_Tp>& __y)
    00338     {
    00339       complex<_Tp> __r = __y;
    00340       __r += __x;
    00341       return __r;
    00342     }
    00343   //@}
    00344
    00345   //@{
    00346   ///  Return new complex value @a x minus @a y.
    00347   template<typename _Tp>
    00348     inline complex<_Tp>
    00349     operator-(const complex<_Tp>& __x, const complex<_Tp>& __y)
    00350     {
    00351       complex<_Tp> __r = __x;
    00352       __r -= __y;
    00353       return __r;
    00354     }
    00355
    00356   template<typename _Tp>
    00357     inline complex<_Tp>
    00358     operator-(const complex<_Tp>& __x, const _Tp& __y)
    00359     {
    00360       complex<_Tp> __r = __x;
    00361       __r -= __y;
    00362       return __r;
    00363     }
    00364
    00365   template<typename _Tp>
    00366     inline complex<_Tp>
    00367     operator-(const _Tp& __x, const complex<_Tp>& __y)
    00368     {
    00369       complex<_Tp> __r(__x, -__y.imag());
    00370       __r -= __y.real();
    00371       return __r;
    00372     }
    00373   //@}
    00374
    00375   //@{
    00376   ///  Return new complex value @a x times @a y.
    00377   template<typename _Tp>
    00378     inline complex<_Tp>
    00379     operator*(const complex<_Tp>& __x, const complex<_Tp>& __y)
    00380     {
    00381       complex<_Tp> __r = __x;
    00382       __r *= __y;
    00383       return __r;
    00384     }
    00385
    00386   template<typename _Tp>
    00387     inline complex<_Tp>
    00388     operator*(const complex<_Tp>& __x, const _Tp& __y)
    00389     {
    00390       complex<_Tp> __r = __x;
    00391       __r *= __y;
    00392       return __r;
    00393     }
    00394
    00395   template<typename _Tp>
    00396     inline complex<_Tp>
    00397     operator*(const _Tp& __x, const complex<_Tp>& __y)
    00398     {
    00399       complex<_Tp> __r = __y;
    00400       __r *= __x;
    00401       return __r;
    00402     }
    00403   //@}
    00404
    00405   //@{
    00406   ///  Return new complex value @a x divided by @a y.
    00407   template<typename _Tp>
    00408     inline complex<_Tp>
    00409     operator/(const complex<_Tp>& __x, const complex<_Tp>& __y)
    00410     {
    00411       complex<_Tp> __r = __x;
    00412       __r /= __y;
    00413       return __r;
    00414     }
    00415
    00416   template<typename _Tp>
    00417     inline complex<_Tp>
    00418     operator/(const complex<_Tp>& __x, const _Tp& __y)
    00419     {
    00420       complex<_Tp> __r = __x;
    00421       __r /= __y;
    00422       return __r;
    00423     }
    00424
    00425   template<typename _Tp>
    00426     inline complex<_Tp>
    00427     operator/(const _Tp& __x, const complex<_Tp>& __y)
    00428     {
    00429       complex<_Tp> __r = __x;
    00430       __r /= __y;
    00431       return __r;
    00432     }
    00433   //@}
    00434
    00435   ///  Return @a x.
    00436   template<typename _Tp>
    00437     inline complex<_Tp>
    00438     operator+(const complex<_Tp>& __x)
    00439     { return __x; }
    00440
    00441   ///  Return complex negation of @a x.
    00442   template<typename _Tp>
    00443     inline complex<_Tp>
    00444     operator-(const complex<_Tp>& __x)
    00445     {  return complex<_Tp>(-__x.real(), -__x.imag()); }
    00446
    00447   //@{
    00448   ///  Return true if @a x is equal to @a y.
    00449   template<typename _Tp>
    00450     inline bool
    00451     operator==(const complex<_Tp>& __x, const complex<_Tp>& __y)
    00452     { return __x.real() == __y.real() && __x.imag() == __y.imag(); }
    00453
    00454   template<typename _Tp>
    00455     inline bool
    00456     operator==(const complex<_Tp>& __x, const _Tp& __y)
    00457     { return __x.real() == __y && __x.imag() == _Tp(); }
    00458
    00459   template<typename _Tp>
    00460     inline bool
    00461     operator==(const _Tp& __x, const complex<_Tp>& __y)
    00462     { return __x == __y.real() && _Tp() == __y.imag(); }
    00463   //@}
    00464
    00465   //@{
    00466   ///  Return false if @a x is equal to @a y.
    00467   template<typename _Tp>
    00468     inline bool
    00469     operator!=(const complex<_Tp>& __x, const complex<_Tp>& __y)
    00470     { return __x.real() != __y.real() || __x.imag() != __y.imag(); }
    00471
    00472   template<typename _Tp>
    00473     inline bool
    00474     operator!=(const complex<_Tp>& __x, const _Tp& __y)
    00475     { return __x.real() != __y || __x.imag() != _Tp(); }
    00476
    00477   template<typename _Tp>
    00478     inline bool
    00479     operator!=(const _Tp& __x, const complex<_Tp>& __y)
    00480     { return __x != __y.real() || _Tp() != __y.imag(); }
    00481   //@}
    00482
    00483   ///  Extraction operator for complex values.
    00484   template<typename _Tp, typename _CharT, class _Traits>
    00485     basic_istream<_CharT, _Traits>&
    00486     operator>>(basic_istream<_CharT, _Traits>& __is, complex<_Tp>& __x)
    00487     {
    00488       _Tp __re_x, __im_x;
    00489       _CharT __ch;
    00490       __is >> __ch;
    00491       if (__ch == '(')
    00492     {
    00493       __is >> __re_x >> __ch;
    00494       if (__ch == ',')
    00495         {
    00496           __is >> __im_x >> __ch;
    00497           if (__ch == ')')
    00498         __x = complex<_Tp>(__re_x, __im_x);
    00499           else
    00500         __is.setstate(ios_base::failbit);
    00501         }
    00502       else if (__ch == ')')
    00503         __x = __re_x;
    00504       else
    00505         __is.setstate(ios_base::failbit);
    00506     }
    00507       else
    00508     {
    00509       __is.putback(__ch);
    00510       __is >> __re_x;
    00511       __x = __re_x;
    00512     }
    00513       return __is;
    00514     }
    00515
    00516   ///  Insertion operator for complex values.
    00517   template<typename _Tp, typename _CharT, class _Traits>
    00518     basic_ostream<_CharT, _Traits>&
    00519     operator<<(basic_ostream<_CharT, _Traits>& __os, const complex<_Tp>& __x)
    00520     {
    00521       basic_ostringstream<_CharT, _Traits> __s;
    00522       __s.flags(__os.flags());
    00523       __s.imbue(__os.getloc());
    00524       __s.precision(__os.precision());
    00525       __s << '(' << __x.real() << ',' << __x.imag() << ')';
    00526       return __os << __s.str();
    00527     }
    00528
    00529   // Values
    00530 #ifdef __GXX_EXPERIMENTAL_CXX0X__
    00531   template<typename _Tp>
    00532     inline _Tp
    00533     real(const complex<_Tp>& __z)
    00534     { return __z.real(); }
    00535
    00536   template<typename _Tp>
    00537     inline _Tp
    00538     imag(const complex<_Tp>& __z)
    00539     { return __z.imag(); }
    00540 #else
    00541   template<typename _Tp>
    00542     inline _Tp&
    00543     real(complex<_Tp>& __z)
    00544     { return __z.real(); }
    00545
    00546   template<typename _Tp>
    00547     inline const _Tp&
    00548     real(const complex<_Tp>& __z)
    00549     { return __z.real(); }
    00550
    00551   template<typename _Tp>
    00552     inline _Tp&
    00553     imag(complex<_Tp>& __z)
    00554     { return __z.imag(); }
    00555
    00556   template<typename _Tp>
    00557     inline const _Tp&
    00558     imag(const complex<_Tp>& __z)
    00559     { return __z.imag(); }
    00560 #endif
    00561
    00562   // 26.2.7/3 abs(__z):  Returns the magnitude of __z.
    00563   template<typename _Tp>
    00564     inline _Tp
    00565     __complex_abs(const complex<_Tp>& __z)
    00566     {
    00567       _Tp __x = __z.real();
    00568       _Tp __y = __z.imag();
    00569       const _Tp __s = std::max(abs(__x), abs(__y));
    00570       if (__s == _Tp())  // well ...
    00571         return __s;
    00572       __x /= __s;
    00573       __y /= __s;
    00574       return __s * sqrt(__x * __x + __y * __y);
    00575     }
    00576
    00577 #if _GLIBCXX_USE_C99_COMPLEX
    00578   inline float
    00579   __complex_abs(__complex__ float __z) { return __builtin_cabsf(__z); }
    00580
    00581   inline double
    00582   __complex_abs(__complex__ double __z) { return __builtin_cabs(__z); }
    00583
    00584   inline long double
    00585   __complex_abs(const __complex__ long double& __z)
    00586   { return __builtin_cabsl(__z); }
    00587
    00588   template<typename _Tp>
    00589     inline _Tp
    00590     abs(const complex<_Tp>& __z) { return __complex_abs(__z.__rep()); }
    00591 #else
    00592   template<typename _Tp>
    00593     inline _Tp
    00594     abs(const complex<_Tp>& __z) { return __complex_abs(__z); }
    00595 #endif
    00596
    00597
    00598   // 26.2.7/4: arg(__z): Returns the phase angle of __z.
    00599   template<typename _Tp>
    00600     inline _Tp
    00601     __complex_arg(const complex<_Tp>& __z)
    00602     { return  atan2(__z.imag(), __z.real()); }
    00603
    00604 #if _GLIBCXX_USE_C99_COMPLEX
    00605   inline float
    00606   __complex_arg(__complex__ float __z) { return __builtin_cargf(__z); }
    00607
    00608   inline double
    00609   __complex_arg(__complex__ double __z) { return __builtin_carg(__z); }
    00610
    00611   inline long double
    00612   __complex_arg(const __complex__ long double& __z)
    00613   { return __builtin_cargl(__z); }
    00614
    00615   template<typename _Tp>
    00616     inline _Tp
    00617     arg(const complex<_Tp>& __z) { return __complex_arg(__z.__rep()); }
    00618 #else
    00619   template<typename _Tp>
    00620     inline _Tp
    00621     arg(const complex<_Tp>& __z) { return __complex_arg(__z); }
    00622 #endif
    00623
    00624   // 26.2.7/5: norm(__z) returns the squared magnitude of __z.
    00625   //     As defined, norm() is -not- a norm is the common mathematical
    00626   //     sens used in numerics.  The helper class _Norm_helper<> tries to
    00627   //     distinguish between builtin floating point and the rest, so as
    00628   //     to deliver an answer as close as possible to the real value.
    00629   template<bool>
    00630     struct _Norm_helper
    00631     {
    00632       template<typename _Tp>
    00633         static inline _Tp _S_do_it(const complex<_Tp>& __z)
    00634         {
    00635           const _Tp __x = __z.real();
    00636           const _Tp __y = __z.imag();
    00637           return __x * __x + __y * __y;
    00638         }
    00639     };
    00640
    00641   template<>
    00642     struct _Norm_helper<true>
    00643     {
    00644       template<typename _Tp>
    00645         static inline _Tp _S_do_it(const complex<_Tp>& __z)
    00646         {
    00647           _Tp __res = std::abs(__z);
    00648           return __res * __res;
    00649         }
    00650     };
    00651
    00652   template<typename _Tp>
    00653     inline _Tp
    00654     norm(const complex<_Tp>& __z)
    00655     {
    00656       return _Norm_helper<__is_floating<_Tp>::__value
    00657     && !_GLIBCXX_FAST_MATH>::_S_do_it(__z);
    00658     }
    00659
    00660   template<typename _Tp>
    00661     inline complex<_Tp>
    00662     polar(const _Tp& __rho, const _Tp& __theta)
    00663     { return complex<_Tp>(__rho * cos(__theta), __rho * sin(__theta)); }
    00664
    00665   template<typename _Tp>
    00666     inline complex<_Tp>
    00667     conj(const complex<_Tp>& __z)
    00668     { return complex<_Tp>(__z.real(), -__z.imag()); }
    00669
    00670   // Transcendentals
    00671
    00672   // 26.2.8/1 cos(__z):  Returns the cosine of __z.
    00673   template<typename _Tp>
    00674     inline complex<_Tp>
    00675     __complex_cos(const complex<_Tp>& __z)
    00676     {
    00677       const _Tp __x = __z.real();
    00678       const _Tp __y = __z.imag();
    00679       return complex<_Tp>(cos(__x) * cosh(__y), -sin(__x) * sinh(__y));
    00680     }
    00681
    00682 #if _GLIBCXX_USE_C99_COMPLEX
    00683   inline __complex__ float
    00684   __complex_cos(__complex__ float __z) { return __builtin_ccosf(__z); }
    00685
    00686   inline __complex__ double
    00687   __complex_cos(__complex__ double __z) { return __builtin_ccos(__z); }
    00688
    00689   inline __complex__ long double
    00690   __complex_cos(const __complex__ long double& __z)
    00691   { return __builtin_ccosl(__z); }
    00692
    00693   template<typename _Tp>
    00694     inline complex<_Tp>
    00695     cos(const complex<_Tp>& __z) { return __complex_cos(__z.__rep()); }
    00696 #else
    00697   template<typename _Tp>
    00698     inline complex<_Tp>
    00699     cos(const complex<_Tp>& __z) { return __complex_cos(__z); }
    00700 #endif
    00701
    00702   // 26.2.8/2 cosh(__z): Returns the hyperbolic cosine of __z.
    00703   template<typename _Tp>
    00704     inline complex<_Tp>
    00705     __complex_cosh(const complex<_Tp>& __z)
    00706     {
    00707       const _Tp __x = __z.real();
    00708       const _Tp __y = __z.imag();
    00709       return complex<_Tp>(cosh(__x) * cos(__y), sinh(__x) * sin(__y));
    00710     }
    00711
    00712 #if _GLIBCXX_USE_C99_COMPLEX
    00713   inline __complex__ float
    00714   __complex_cosh(__complex__ float __z) { return __builtin_ccoshf(__z); }
    00715
    00716   inline __complex__ double
    00717   __complex_cosh(__complex__ double __z) { return __builtin_ccosh(__z); }
    00718
    00719   inline __complex__ long double
    00720   __complex_cosh(const __complex__ long double& __z)
    00721   { return __builtin_ccoshl(__z); }
    00722
    00723   template<typename _Tp>
    00724     inline complex<_Tp>
    00725     cosh(const complex<_Tp>& __z) { return __complex_cosh(__z.__rep()); }
    00726 #else
    00727   template<typename _Tp>
    00728     inline complex<_Tp>
    00729     cosh(const complex<_Tp>& __z) { return __complex_cosh(__z); }
    00730 #endif
    00731
    00732   // 26.2.8/3 exp(__z): Returns the complex base e exponential of x
    00733   template<typename _Tp>
    00734     inline complex<_Tp>
    00735     __complex_exp(const complex<_Tp>& __z)
    00736     { return std::polar(exp(__z.real()), __z.imag()); }
    00737
    00738 #if _GLIBCXX_USE_C99_COMPLEX
    00739   inline __complex__ float
    00740   __complex_exp(__complex__ float __z) { return __builtin_cexpf(__z); }
    00741
    00742   inline __complex__ double
    00743   __complex_exp(__complex__ double __z) { return __builtin_cexp(__z); }
    00744
    00745   inline __complex__ long double
    00746   __complex_exp(const __complex__ long double& __z)
    00747   { return __builtin_cexpl(__z); }
    00748
    00749   template<typename _Tp>
    00750     inline complex<_Tp>
    00751     exp(const complex<_Tp>& __z) { return __complex_exp(__z.__rep()); }
    00752 #else
    00753   template<typename _Tp>
    00754     inline complex<_Tp>
    00755     exp(const complex<_Tp>& __z) { return __complex_exp(__z); }
    00756 #endif
    00757
    00758   // 26.2.8/5 log(__z): Returns the natural complex logarithm of __z.
    00759   //                    The branch cut is along the negative axis.
    00760   template<typename _Tp>
    00761     inline complex<_Tp>
    00762     __complex_log(const complex<_Tp>& __z)
    00763     { return complex<_Tp>(log(std::abs(__z)), std::arg(__z)); }
    00764
    00765 #if _GLIBCXX_USE_C99_COMPLEX
    00766   inline __complex__ float
    00767   __complex_log(__complex__ float __z) { return __builtin_clogf(__z); }
    00768
    00769   inline __complex__ double
    00770   __complex_log(__complex__ double __z) { return __builtin_clog(__z); }
    00771
    00772   inline __complex__ long double
    00773   __complex_log(const __complex__ long double& __z)
    00774   { return __builtin_clogl(__z); }
    00775
    00776   template<typename _Tp>
    00777     inline complex<_Tp>
    00778     log(const complex<_Tp>& __z) { return __complex_log(__z.__rep()); }
    00779 #else
    00780   template<typename _Tp>
    00781     inline complex<_Tp>
    00782     log(const complex<_Tp>& __z) { return __complex_log(__z); }
    00783 #endif
    00784
    00785   template<typename _Tp>
    00786     inline complex<_Tp>
    00787     log10(const complex<_Tp>& __z)
    00788     { return std::log(__z) / log(_Tp(10.0)); }
    00789
    00790   // 26.2.8/10 sin(__z): Returns the sine of __z.
    00791   template<typename _Tp>
    00792     inline complex<_Tp>
    00793     __complex_sin(const complex<_Tp>& __z)
    00794     {
    00795       const _Tp __x = __z.real();
    00796       const _Tp __y = __z.imag();
    00797       return complex<_Tp>(sin(__x) * cosh(__y), cos(__x) * sinh(__y));
    00798     }
    00799
    00800 #if _GLIBCXX_USE_C99_COMPLEX
    00801   inline __complex__ float
    00802   __complex_sin(__complex__ float __z) { return __builtin_csinf(__z); }
    00803
    00804   inline __complex__ double
    00805   __complex_sin(__complex__ double __z) { return __builtin_csin(__z); }
    00806
    00807   inline __complex__ long double
    00808   __complex_sin(const __complex__ long double& __z)
    00809   { return __builtin_csinl(__z); }
    00810
    00811   template<typename _Tp>
    00812     inline complex<_Tp>
    00813     sin(const complex<_Tp>& __z) { return __complex_sin(__z.__rep()); }
    00814 #else
    00815   template<typename _Tp>
    00816     inline complex<_Tp>
    00817     sin(const complex<_Tp>& __z) { return __complex_sin(__z); }
    00818 #endif
    00819
    00820   // 26.2.8/11 sinh(__z): Returns the hyperbolic sine of __z.
    00821   template<typename _Tp>
    00822     inline complex<_Tp>
    00823     __complex_sinh(const complex<_Tp>& __z)
    00824     {
    00825       const _Tp __x = __z.real();
    00826       const _Tp  __y = __z.imag();
    00827       return complex<_Tp>(sinh(__x) * cos(__y), cosh(__x) * sin(__y));
    00828     }
    00829
    00830 #if _GLIBCXX_USE_C99_COMPLEX
    00831   inline __complex__ float
    00832   __complex_sinh(__complex__ float __z) { return __builtin_csinhf(__z); }
    00833
    00834   inline __complex__ double
    00835   __complex_sinh(__complex__ double __z) { return __builtin_csinh(__z); }
    00836
    00837   inline __complex__ long double
    00838   __complex_sinh(const __complex__ long double& __z)
    00839   { return __builtin_csinhl(__z); }
    00840
    00841   template<typename _Tp>
    00842     inline complex<_Tp>
    00843     sinh(const complex<_Tp>& __z) { return __complex_sinh(__z.__rep()); }
    00844 #else
    00845   template<typename _Tp>
    00846     inline complex<_Tp>
    00847     sinh(const complex<_Tp>& __z) { return __complex_sinh(__z); }
    00848 #endif
    00849
    00850   // 26.2.8/13 sqrt(__z): Returns the complex square root of __z.
    00851   //                     The branch cut is on the negative axis.
    00852   template<typename _Tp>
    00853     complex<_Tp>
    00854     __complex_sqrt(const complex<_Tp>& __z)
    00855     {
    00856       _Tp __x = __z.real();
    00857       _Tp __y = __z.imag();
    00858
    00859       if (__x == _Tp())
    00860         {
    00861           _Tp __t = sqrt(abs(__y) / 2);
    00862           return complex<_Tp>(__t, __y < _Tp() ? -__t : __t);
    00863         }
    00864       else
    00865         {
    00866           _Tp __t = sqrt(2 * (std::abs(__z) + abs(__x)));
    00867           _Tp __u = __t / 2;
    00868           return __x > _Tp()
    00869             ? complex<_Tp>(__u, __y / __t)
    00870             : complex<_Tp>(abs(__y) / __t, __y < _Tp() ? -__u : __u);
    00871         }
    00872     }
    00873
    00874 #if _GLIBCXX_USE_C99_COMPLEX
    00875   inline __complex__ float
    00876   __complex_sqrt(__complex__ float __z) { return __builtin_csqrtf(__z); }
    00877
    00878   inline __complex__ double
    00879   __complex_sqrt(__complex__ double __z) { return __builtin_csqrt(__z); }
    00880
    00881   inline __complex__ long double
    00882   __complex_sqrt(const __complex__ long double& __z)
    00883   { return __builtin_csqrtl(__z); }
    00884
    00885   template<typename _Tp>
    00886     inline complex<_Tp>
    00887     sqrt(const complex<_Tp>& __z) { return __complex_sqrt(__z.__rep()); }
    00888 #else
    00889   template<typename _Tp>
    00890     inline complex<_Tp>
    00891     sqrt(const complex<_Tp>& __z) { return __complex_sqrt(__z); }
    00892 #endif
    00893
    00894   // 26.2.8/14 tan(__z):  Return the complex tangent of __z.
    00895
    00896   template<typename _Tp>
    00897     inline complex<_Tp>
    00898     __complex_tan(const complex<_Tp>& __z)
    00899     { return std::sin(__z) / std::cos(__z); }
    00900
    00901 #if _GLIBCXX_USE_C99_COMPLEX
    00902   inline __complex__ float
    00903   __complex_tan(__complex__ float __z) { return __builtin_ctanf(__z); }
    00904
    00905   inline __complex__ double
    00906   __complex_tan(__complex__ double __z) { return __builtin_ctan(__z); }
    00907
    00908   inline __complex__ long double
    00909   __complex_tan(const __complex__ long double& __z)
    00910   { return __builtin_ctanl(__z); }
    00911
    00912   template<typename _Tp>
    00913     inline complex<_Tp>
    00914     tan(const complex<_Tp>& __z) { return __complex_tan(__z.__rep()); }
    00915 #else
    00916   template<typename _Tp>
    00917     inline complex<_Tp>
    00918     tan(const complex<_Tp>& __z) { return __complex_tan(__z); }
    00919 #endif
    00920
    00921
    00922   // 26.2.8/15 tanh(__z):  Returns the hyperbolic tangent of __z.
    00923
    00924   template<typename _Tp>
    00925     inline complex<_Tp>
    00926     __complex_tanh(const complex<_Tp>& __z)
    00927     { return std::sinh(__z) / std::cosh(__z); }
    00928
    00929 #if _GLIBCXX_USE_C99_COMPLEX
    00930   inline __complex__ float
    00931   __complex_tanh(__complex__ float __z) { return __builtin_ctanhf(__z); }
    00932
    00933   inline __complex__ double
    00934   __complex_tanh(__complex__ double __z) { return __builtin_ctanh(__z); }
    00935
    00936   inline __complex__ long double
    00937   __complex_tanh(const __complex__ long double& __z)
    00938   { return __builtin_ctanhl(__z); }
    00939
    00940   template<typename _Tp>
    00941     inline complex<_Tp>
    00942     tanh(const complex<_Tp>& __z) { return __complex_tanh(__z.__rep()); }
    00943 #else
    00944   template<typename _Tp>
    00945     inline complex<_Tp>
    00946     tanh(const complex<_Tp>& __z) { return __complex_tanh(__z); }
    00947 #endif
    00948
    00949
    00950   // 26.2.8/9  pow(__x, __y): Returns the complex power base of __x
    00951   //                          raised to the __y-th power.  The branch
    00952   //                          cut is on the negative axis.
    00953 #ifndef __GXX_EXPERIMENTAL_CXX0X__
    00954   // _GLIBCXX_RESOLVE_LIB_DEFECTS
    00955   // DR 844. complex pow return type is ambiguous.
    00956   template<typename _Tp>
    00957     inline complex<_Tp>
    00958     pow(const complex<_Tp>& __z, int __n)
    00959     { return std::__pow_helper(__z, __n); }
    00960 #endif
    00961
    00962   template<typename _Tp>
    00963     complex<_Tp>
    00964     pow(const complex<_Tp>& __x, const _Tp& __y)
    00965     {
    00966 #ifndef _GLIBCXX_USE_C99_COMPLEX
    00967       if (__x == _Tp())
    00968     return _Tp();
    00969 #endif
    00970       if (__x.imag() == _Tp() && __x.real() > _Tp())
    00971         return pow(__x.real(), __y);
    00972
    00973       complex<_Tp> __t = std::log(__x);
    00974       return std::polar(exp(__y * __t.real()), __y * __t.imag());
    00975     }
    00976
    00977   template<typename _Tp>
    00978     inline complex<_Tp>
    00979     __complex_pow(const complex<_Tp>& __x, const complex<_Tp>& __y)
    00980     { return __x == _Tp() ? _Tp() : std::exp(__y * std::log(__x)); }
    00981
    00982 #if _GLIBCXX_USE_C99_COMPLEX
    00983   inline __complex__ float
    00984   __complex_pow(__complex__ float __x, __complex__ float __y)
    00985   { return __builtin_cpowf(__x, __y); }
    00986
    00987   inline __complex__ double
    00988   __complex_pow(__complex__ double __x, __complex__ double __y)
    00989   { return __builtin_cpow(__x, __y); }
    00990
    00991   inline __complex__ long double
    00992   __complex_pow(const __complex__ long double& __x,
    00993         const __complex__ long double& __y)
    00994   { return __builtin_cpowl(__x, __y); }
    00995
    00996   template<typename _Tp>
    00997     inline complex<_Tp>
    00998     pow(const complex<_Tp>& __x, const complex<_Tp>& __y)
    00999     { return __complex_pow(__x.__rep(), __y.__rep()); }
    01000 #else
    01001   template<typename _Tp>
    01002     inline complex<_Tp>
    01003     pow(const complex<_Tp>& __x, const complex<_Tp>& __y)
    01004     { return __complex_pow(__x, __y); }
    01005 #endif
    01006
    01007   template<typename _Tp>
    01008     inline complex<_Tp>
    01009     pow(const _Tp& __x, const complex<_Tp>& __y)
    01010     {
    01011       return __x > _Tp() ? std::polar(pow(__x, __y.real()),
    01012                       __y.imag() * log(__x))
    01013                      : std::pow(complex<_Tp>(__x), __y);
    01014     }
    01015
    01016   // 26.2.3  complex specializations
    01017   // complex<float> specialization
    01018   template<>
    01019     struct complex<float>
    01020     {
    01021       typedef float value_type;
    01022       typedef __complex__ float _ComplexT;
    01023
    01024       complex(_ComplexT __z) : _M_value(__z) { }
    01025
    01026       complex(float __r = 0.0f, float __i = 0.0f)
    01027       {
    01028     __real__ _M_value = __r;
    01029     __imag__ _M_value = __i;
    01030       }
    01031
    01032       explicit complex(const complex<double>&);
    01033       explicit complex(const complex<long double>&);
    01034
    01035 #ifdef __GXX_EXPERIMENTAL_CXX0X__
    01036       // _GLIBCXX_RESOLVE_LIB_DEFECTS
    01037       // DR 387. std::complex over-encapsulated.
    01038       float real() const
    01039       { return __real__ _M_value; }
    01040
    01041       float imag() const
    01042       { return __imag__ _M_value; }
    01043 #else
    01044       float& real()
    01045       { return __real__ _M_value; }
    01046
    01047       const float& real() const
    01048       { return __real__ _M_value; }
    01049
    01050       float& imag()
    01051       { return __imag__ _M_value; }
    01052
    01053       const float& imag() const
    01054       { return __imag__ _M_value; }
    01055 #endif
    01056
    01057       // _GLIBCXX_RESOLVE_LIB_DEFECTS
    01058       // DR 387. std::complex over-encapsulated.
    01059       void real(float __val)
    01060       { __real__ _M_value = __val; }
    01061
    01062       void imag(float __val)
    01063       { __imag__ _M_value = __val; }
    01064
    01065       complex<float>&
    01066       operator=(float __f)
    01067       {
    01068     __real__ _M_value = __f;
    01069     __imag__ _M_value = 0.0f;
    01070     return *this;
    01071       }
    01072
    01073       complex<float>&
    01074       operator+=(float __f)
    01075       {
    01076     __real__ _M_value += __f;
    01077     return *this;
    01078       }
    01079
    01080       complex<float>&
    01081       operator-=(float __f)
    01082       {
    01083     __real__ _M_value -= __f;
    01084     return *this;
    01085       }
    01086
    01087       complex<float>&
    01088       operator*=(float __f)
    01089       {
    01090     _M_value *= __f;
    01091     return *this;
    01092       }
    01093
    01094       complex<float>&
    01095       operator/=(float __f)
    01096       {
    01097     _M_value /= __f;
    01098     return *this;
    01099       }
    01100
    01101       // Let the compiler synthesize the copy and assignment
    01102       // operator.  It always does a pretty good job.
    01103       // complex& operator=(const complex&);
    01104
    01105       template<typename _Tp>
    01106         complex<float>&
    01107         operator=(const complex<_Tp>&  __z)
    01108     {
    01109       __real__ _M_value = __z.real();
    01110       __imag__ _M_value = __z.imag();
    01111       return *this;
    01112     }
    01113
    01114       template<typename _Tp>
    01115         complex<float>&
    01116         operator+=(const complex<_Tp>& __z)
    01117     {
    01118       __real__ _M_value += __z.real();
    01119       __imag__ _M_value += __z.imag();
    01120       return *this;
    01121     }
    01122
    01123       template<class _Tp>
    01124         complex<float>&
    01125         operator-=(const complex<_Tp>& __z)
    01126     {
    01127       __real__ _M_value -= __z.real();
    01128       __imag__ _M_value -= __z.imag();
    01129       return *this;
    01130     }
    01131
    01132       template<class _Tp>
    01133         complex<float>&
    01134         operator*=(const complex<_Tp>& __z)
    01135     {
    01136       _ComplexT __t;
    01137       __real__ __t = __z.real();
    01138       __imag__ __t = __z.imag();
    01139       _M_value *= __t;
    01140       return *this;
    01141     }
    01142
    01143       template<class _Tp>
    01144         complex<float>&
    01145         operator/=(const complex<_Tp>& __z)
    01146     {
    01147       _ComplexT __t;
    01148       __real__ __t = __z.real();
    01149       __imag__ __t = __z.imag();
    01150       _M_value /= __t;
    01151       return *this;
    01152     }
    01153
    01154       const _ComplexT& __rep() const { return _M_value; }
    01155
    01156     private:
    01157       _ComplexT _M_value;
    01158     };
    01159
    01160   // 26.2.3  complex specializations
    01161   // complex<double> specialization
    01162   template<>
    01163     struct complex<double>
    01164     {
    01165       typedef double value_type;
    01166       typedef __complex__ double _ComplexT;
    01167
    01168       complex(_ComplexT __z) : _M_value(__z) { }
    01169
    01170       complex(double __r = 0.0, double __i = 0.0)
    01171       {
    01172     __real__ _M_value = __r;
    01173     __imag__ _M_value = __i;
    01174       }
    01175
    01176       complex(const complex<float>& __z)
    01177       : _M_value(__z.__rep()) { }
    01178
    01179       explicit complex(const complex<long double>&);
    01180
    01181 #ifdef __GXX_EXPERIMENTAL_CXX0X__
    01182       // _GLIBCXX_RESOLVE_LIB_DEFECTS
    01183       // DR 387. std::complex over-encapsulated.
    01184       double real() const
    01185       { return __real__ _M_value; }
    01186
    01187       double imag() const
    01188       { return __imag__ _M_value; }
    01189 #else
    01190       double& real()
    01191       { return __real__ _M_value; }
    01192
    01193       const double& real() const
    01194       { return __real__ _M_value; }
    01195
    01196       double& imag()
    01197       { return __imag__ _M_value; }
    01198
    01199       const double& imag() const
    01200       { return __imag__ _M_value; }
    01201 #endif
    01202
    01203       // _GLIBCXX_RESOLVE_LIB_DEFECTS
    01204       // DR 387. std::complex over-encapsulated.
    01205       void real(double __val)
    01206       { __real__ _M_value = __val; }
    01207
    01208       void imag(double __val)
    01209       { __imag__ _M_value = __val; }
    01210
    01211       complex<double>&
    01212       operator=(double __d)
    01213       {
    01214     __real__ _M_value = __d;
    01215     __imag__ _M_value = 0.0;
    01216     return *this;
    01217       }
    01218
    01219       complex<double>&
    01220       operator+=(double __d)
    01221       {
    01222     __real__ _M_value += __d;
    01223     return *this;
    01224       }
    01225
    01226       complex<double>&
    01227       operator-=(double __d)
    01228       {
    01229     __real__ _M_value -= __d;
    01230     return *this;
    01231       }
    01232
    01233       complex<double>&
    01234       operator*=(double __d)
    01235       {
    01236     _M_value *= __d;
    01237     return *this;
    01238       }
    01239
    01240       complex<double>&
    01241       operator/=(double __d)
    01242       {
    01243     _M_value /= __d;
    01244     return *this;
    01245       }
    01246
    01247       // The compiler will synthesize this, efficiently.
    01248       // complex& operator=(const complex&);
    01249
    01250       template<typename _Tp>
    01251         complex<double>&
    01252         operator=(const complex<_Tp>& __z)
    01253     {
    01254       __real__ _M_value = __z.real();
    01255       __imag__ _M_value = __z.imag();
    01256       return *this;
    01257     }
    01258
    01259       template<typename _Tp>
    01260         complex<double>&
    01261         operator+=(const complex<_Tp>& __z)
    01262     {
    01263       __real__ _M_value += __z.real();
    01264       __imag__ _M_value += __z.imag();
    01265       return *this;
    01266     }
    01267
    01268       template<typename _Tp>
    01269         complex<double>&
    01270         operator-=(const complex<_Tp>& __z)
    01271     {
    01272       __real__ _M_value -= __z.real();
    01273       __imag__ _M_value -= __z.imag();
    01274       return *this;
    01275     }
    01276
    01277       template<typename _Tp>
    01278         complex<double>&
    01279         operator*=(const complex<_Tp>& __z)
    01280     {
    01281       _ComplexT __t;
    01282       __real__ __t = __z.real();
    01283       __imag__ __t = __z.imag();
    01284       _M_value *= __t;
    01285       return *this;
    01286     }
    01287
    01288       template<typename _Tp>
    01289         complex<double>&
    01290         operator/=(const complex<_Tp>& __z)
    01291     {
    01292       _ComplexT __t;
    01293       __real__ __t = __z.real();
    01294       __imag__ __t = __z.imag();
    01295       _M_value /= __t;
    01296       return *this;
    01297     }
    01298
    01299       const _ComplexT& __rep() const { return _M_value; }
    01300
    01301     private:
    01302       _ComplexT _M_value;
    01303     };
    01304
    01305   // 26.2.3  complex specializations
    01306   // complex<long double> specialization
    01307   template<>
    01308     struct complex<long double>
    01309     {
    01310       typedef long double value_type;
    01311       typedef __complex__ long double _ComplexT;
    01312
    01313       complex(_ComplexT __z) : _M_value(__z) { }
    01314
    01315       complex(long double __r = 0.0L, long double __i = 0.0L)
    01316       {
    01317     __real__ _M_value = __r;
    01318     __imag__ _M_value = __i;
    01319       }
    01320
    01321       complex(const complex<float>& __z)
    01322       : _M_value(__z.__rep()) { }
    01323
    01324       complex(const complex<double>& __z)
    01325       : _M_value(__z.__rep()) { }
    01326
    01327 #ifdef __GXX_EXPERIMENTAL_CXX0X__
    01328       // _GLIBCXX_RESOLVE_LIB_DEFECTS
    01329       // DR 387. std::complex over-encapsulated.
    01330       long double real() const
    01331       { return __real__ _M_value; }
    01332
    01333       long double imag() const
    01334       { return __imag__ _M_value; }
    01335 #else
    01336       long double& real()
    01337       { return __real__ _M_value; }
    01338
    01339       const long double& real() const
    01340       { return __real__ _M_value; }
    01341
    01342       long double& imag()
    01343       { return __imag__ _M_value; }
    01344
    01345       const long double& imag() const
    01346       { return __imag__ _M_value; }
    01347 #endif
    01348
    01349       // _GLIBCXX_RESOLVE_LIB_DEFECTS
    01350       // DR 387. std::complex over-encapsulated.
    01351       void real(long double __val)
    01352       { __real__ _M_value = __val; }
    01353
    01354       void imag(long double __val)
    01355       { __imag__ _M_value = __val; }
    01356
    01357       complex<long double>&
    01358       operator=(long double __r)
    01359       {
    01360     __real__ _M_value = __r;
    01361     __imag__ _M_value = 0.0L;
    01362     return *this;
    01363       }
    01364
    01365       complex<long double>&
    01366       operator+=(long double __r)
    01367       {
    01368     __real__ _M_value += __r;
    01369     return *this;
    01370       }
    01371
    01372       complex<long double>&
    01373       operator-=(long double __r)
    01374       {
    01375     __real__ _M_value -= __r;
    01376     return *this;
    01377       }
    01378
    01379       complex<long double>&
    01380       operator*=(long double __r)
    01381       {
    01382     _M_value *= __r;
    01383     return *this;
    01384       }
    01385
    01386       complex<long double>&
    01387       operator/=(long double __r)
    01388       {
    01389     _M_value /= __r;
    01390     return *this;
    01391       }
    01392
    01393       // The compiler knows how to do this efficiently
    01394       // complex& operator=(const complex&);
    01395
    01396       template<typename _Tp>
    01397         complex<long double>&
    01398         operator=(const complex<_Tp>& __z)
    01399     {
    01400       __real__ _M_value = __z.real();
    01401       __imag__ _M_value = __z.imag();
    01402       return *this;
    01403     }
    01404
    01405       template<typename _Tp>
    01406         complex<long double>&
    01407     operator+=(const complex<_Tp>& __z)
    01408     {
    01409       __real__ _M_value += __z.real();
    01410       __imag__ _M_value += __z.imag();
    01411       return *this;
    01412     }
    01413
    01414       template<typename _Tp>
    01415         complex<long double>&
    01416     operator-=(const complex<_Tp>& __z)
    01417     {
    01418       __real__ _M_value -= __z.real();
    01419       __imag__ _M_value -= __z.imag();
    01420       return *this;
    01421     }
    01422
    01423       template<typename _Tp>
    01424         complex<long double>&
    01425     operator*=(const complex<_Tp>& __z)
    01426     {
    01427       _ComplexT __t;
    01428       __real__ __t = __z.real();
    01429       __imag__ __t = __z.imag();
    01430       _M_value *= __t;
    01431       return *this;
    01432     }
    01433
    01434       template<typename _Tp>
    01435         complex<long double>&
    01436     operator/=(const complex<_Tp>& __z)
    01437     {
    01438       _ComplexT __t;
    01439       __real__ __t = __z.real();
    01440       __imag__ __t = __z.imag();
    01441       _M_value /= __t;
    01442       return *this;
    01443     }
    01444
    01445       const _ComplexT& __rep() const { return _M_value; }
    01446
    01447     private:
    01448       _ComplexT _M_value;
    01449     };
    01450
    01451   // These bits have to be at the end of this file, so that the
    01452   // specializations have all been defined.
    01453   inline
    01454   complex<float>::complex(const complex<double>& __z)
    01455   : _M_value(__z.__rep()) { }
    01456
    01457   inline
    01458   complex<float>::complex(const complex<long double>& __z)
    01459   : _M_value(__z.__rep()) { }
    01460
    01461   inline
    01462   complex<double>::complex(const complex<long double>& __z)
    01463   : _M_value(__z.__rep()) { }
    01464
    01465   // Inhibit implicit instantiations for required instantiations,
    01466   // which are defined via explicit instantiations elsewhere.
    01467   // NB:  This syntax is a GNU extension.
    01468 #if _GLIBCXX_EXTERN_TEMPLATE
    01469   extern template istream& operator>>(istream&, complex<float>&);
    01470   extern template ostream& operator<<(ostream&, const complex<float>&);
    01471   extern template istream& operator>>(istream&, complex<double>&);
    01472   extern template ostream& operator<<(ostream&, const complex<double>&);
    01473   extern template istream& operator>>(istream&, complex<long double>&);
    01474   extern template ostream& operator<<(ostream&, const complex<long double>&);
    01475
    01476 #ifdef _GLIBCXX_USE_WCHAR_T
    01477   extern template wistream& operator>>(wistream&, complex<float>&);
    01478   extern template wostream& operator<<(wostream&, const complex<float>&);
    01479   extern template wistream& operator>>(wistream&, complex<double>&);
    01480   extern template wostream& operator<<(wostream&, const complex<double>&);
    01481   extern template wistream& operator>>(wistream&, complex<long double>&);
    01482   extern template wostream& operator<<(wostream&, const complex<long double>&);
    01483 #endif
    01484 #endif
    01485
    01486   // @} group complex_numbers
    01487
    01488 _GLIBCXX_END_NAMESPACE
    01489
    01490 _GLIBCXX_BEGIN_NAMESPACE(__gnu_cxx)
    01491
    01492   // See ext/type_traits.h for the primary template.
    01493   template<typename _Tp, typename _Up>
    01494     struct __promote_2<std::complex<_Tp>, _Up>
    01495     {
    01496     public:
    01497       typedef std::complex<typename __promote_2<_Tp, _Up>::__type> __type;
    01498     };
    01499
    01500   template<typename _Tp, typename _Up>
    01501     struct __promote_2<_Tp, std::complex<_Up> >
    01502     {
    01503     public:
    01504       typedef std::complex<typename __promote_2<_Tp, _Up>::__type> __type;
    01505     };
    01506
    01507   template<typename _Tp, typename _Up>
    01508     struct __promote_2<std::complex<_Tp>, std::complex<_Up> >
    01509     {
    01510     public:
    01511       typedef std::complex<typename __promote_2<_Tp, _Up>::__type> __type;
    01512     };
    01513
    01514 _GLIBCXX_END_NAMESPACE
    01515
    01516 #ifdef __GXX_EXPERIMENTAL_CXX0X__
    01517 #  if defined(_GLIBCXX_INCLUDE_AS_TR1)
    01518 #    error C++0x header cannot be included from TR1 header
    01519 #  endif
    01520 #  if defined(_GLIBCXX_INCLUDE_AS_CXX0X)
    01521 #    include <tr1_impl/complex>
    01522 #  else
    01523 #    define _GLIBCXX_INCLUDE_AS_CXX0X
    01524 #    define _GLIBCXX_BEGIN_NAMESPACE_TR1
    01525 #    define _GLIBCXX_END_NAMESPACE_TR1
    01526 #    define _GLIBCXX_TR1
    01527 #    include <tr1_impl/complex>
    01528 #    undef _GLIBCXX_TR1
    01529 #    undef _GLIBCXX_END_NAMESPACE_TR1
    01530 #    undef _GLIBCXX_BEGIN_NAMESPACE_TR1
    01531 #    undef _GLIBCXX_INCLUDE_AS_CXX0X
    01532 #  endif
    01533
    01534 _GLIBCXX_BEGIN_NAMESPACE(std)
    01535
    01536   // Forward declarations.
    01537   // DR 781.
    01538   template<typename _Tp> std::complex<_Tp> proj(const std::complex<_Tp>&);
    01539
    01540   template<typename _Tp>
    01541     std::complex<_Tp>
    01542     __complex_proj(const std::complex<_Tp>& __z)
    01543     {
    01544       const _Tp __den = (__z.real() * __z.real()
    01545              + __z.imag() * __z.imag() + _Tp(1.0));
    01546
    01547       return std::complex<_Tp>((_Tp(2.0) * __z.real()) / __den,
    01548                    (_Tp(2.0) * __z.imag()) / __den);
    01549     }
    01550
    01551 #if _GLIBCXX_USE_C99_COMPLEX
    01552   inline __complex__ float
    01553   __complex_proj(__complex__ float __z)
    01554   { return __builtin_cprojf(__z); }
    01555
    01556   inline __complex__ double
    01557   __complex_proj(__complex__ double __z)
    01558   { return __builtin_cproj(__z); }
    01559
    01560   inline __complex__ long double
    01561   __complex_proj(const __complex__ long double& __z)
    01562   { return __builtin_cprojl(__z); }
    01563
    01564   template<typename _Tp>
    01565     inline std::complex<_Tp>
    01566     proj(const std::complex<_Tp>& __z)
    01567     { return __complex_proj(__z.__rep()); }
    01568 #else
    01569   template<typename _Tp>
    01570     inline std::complex<_Tp>
    01571     proj(const std::complex<_Tp>& __z)
    01572     { return __complex_proj(__z); }
    01573 #endif
    01574
    01575   // DR 1137.
    01576   template<typename _Tp>
    01577     inline typename __gnu_cxx::__promote<_Tp>::__type
    01578     proj(_Tp __x)
    01579     { return __x; }
    01580
    01581   template<typename _Tp>
    01582     inline typename __gnu_cxx::__promote<_Tp>::__type
    01583     conj(_Tp __x)
    01584     { return __x; }
    01585
    01586 _GLIBCXX_END_NAMESPACE
    01587
    01588 #endif
    01589
    01590 #endif  /* _GLIBCXX_COMPLEX */