* include/std/complex (real, imag): Add ABI tag in C++11 mode.
[official-gcc.git] / libstdc++-v3 / include / std / complex
blobb13b11becc5c0bd1929fadfaf822570ed5f6c194
1 // The template and inlines for the -*- C++ -*- complex number classes.
3 // Copyright (C) 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005,
4 // 2006, 2007, 2008, 2009, 2010, 2011
5 // Free Software Foundation, Inc.
6 //
7 // This file is part of the GNU ISO C++ Library.  This library is free
8 // software; you can redistribute it and/or modify it under the
9 // terms of the GNU General Public License as published by the
10 // Free Software Foundation; either version 3, or (at your option)
11 // any later version.
13 // This library is distributed in the hope that it will be useful,
14 // but WITHOUT ANY WARRANTY; without even the implied warranty of
15 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16 // GNU General Public License for more details.
18 // Under Section 7 of GPL version 3, you are granted additional
19 // permissions described in the GCC Runtime Library Exception, version
20 // 3.1, as published by the Free Software Foundation.
22 // You should have received a copy of the GNU General Public License and
23 // a copy of the GCC Runtime Library Exception along with this program;
24 // see the files COPYING3 and COPYING.RUNTIME respectively.  If not, see
25 // <http://www.gnu.org/licenses/>.
27 /** @file include/complex
28  *  This is a Standard C++ Library header.
29  */
32 // ISO C++ 14882: 26.2  Complex Numbers
33 // Note: this is not a conforming implementation.
34 // Initially implemented by Ulrich Drepper <drepper@cygnus.com>
35 // Improved by Gabriel Dos Reis <dosreis@cmla.ens-cachan.fr>
38 #ifndef _GLIBCXX_COMPLEX
39 #define _GLIBCXX_COMPLEX 1
41 #pragma GCC system_header
43 #include <bits/c++config.h>
44 #include <bits/cpp_type_traits.h>
45 #include <ext/type_traits.h>
46 #include <cmath>
47 #include <sstream>
49 namespace std _GLIBCXX_VISIBILITY(default)
51 _GLIBCXX_BEGIN_NAMESPACE_VERSION
53   /**
54    * @defgroup complex_numbers Complex Numbers
55    * @ingroup numerics
56    *
57    * Classes and functions for complex numbers.
58    * @{
59    */
61   // Forward declarations.
62   template<typename _Tp> class complex;
63   template<> class complex<float>;
64   template<> class complex<double>;
65   template<> class complex<long double>;
67   ///  Return magnitude of @a z.
68   template<typename _Tp> _Tp abs(const complex<_Tp>&);
69   ///  Return phase angle of @a z.
70   template<typename _Tp> _Tp arg(const complex<_Tp>&);
71   ///  Return @a z magnitude squared.
72   template<typename _Tp> _Tp norm(const complex<_Tp>&);
74   ///  Return complex conjugate of @a z.
75   template<typename _Tp> complex<_Tp> conj(const complex<_Tp>&);
76   ///  Return complex with magnitude @a rho and angle @a theta.
77   template<typename _Tp> complex<_Tp> polar(const _Tp&, const _Tp& = 0);
79   // Transcendentals:
80   /// Return complex cosine of @a z.
81   template<typename _Tp> complex<_Tp> cos(const complex<_Tp>&);
82   /// Return complex hyperbolic cosine of @a z.
83   template<typename _Tp> complex<_Tp> cosh(const complex<_Tp>&);
84   /// Return complex base e exponential of @a z.
85   template<typename _Tp> complex<_Tp> exp(const complex<_Tp>&);
86   /// Return complex natural logarithm of @a z.
87   template<typename _Tp> complex<_Tp> log(const complex<_Tp>&);
88   /// Return complex base 10 logarithm of @a z.
89   template<typename _Tp> complex<_Tp> log10(const complex<_Tp>&);
90 #if __cplusplus < 201103L
91   // DR 844.
92   /// Return @a x to the @a y'th power.
93   template<typename _Tp> complex<_Tp> pow(const complex<_Tp>&, int);
94 #endif
95   /// Return @a x to the @a y'th power.
96   template<typename _Tp> complex<_Tp> pow(const complex<_Tp>&, const _Tp&);
97   /// Return @a x to the @a y'th power.
98   template<typename _Tp> complex<_Tp> pow(const complex<_Tp>&, 
99                                           const complex<_Tp>&);
100   /// Return @a x to the @a y'th power.
101   template<typename _Tp> complex<_Tp> pow(const _Tp&, const complex<_Tp>&);
102   /// Return complex sine of @a z.
103   template<typename _Tp> complex<_Tp> sin(const complex<_Tp>&);
104   /// Return complex hyperbolic sine of @a z.
105   template<typename _Tp> complex<_Tp> sinh(const complex<_Tp>&);
106   /// Return complex square root of @a z.
107   template<typename _Tp> complex<_Tp> sqrt(const complex<_Tp>&);
108   /// Return complex tangent of @a z.
109   template<typename _Tp> complex<_Tp> tan(const complex<_Tp>&);
110   /// Return complex hyperbolic tangent of @a z.
111   template<typename _Tp> complex<_Tp> tanh(const complex<_Tp>&);
112     
113     
114   // 26.2.2  Primary template class complex
115   /**
116    *  Template to represent complex numbers.
117    *
118    *  Specializations for float, double, and long double are part of the
119    *  library.  Results with any other type are not guaranteed.
120    *
121    *  @param  Tp  Type of real and imaginary values.
122   */
123   template<typename _Tp>
124     struct complex
125     {
126       /// Value typedef.
127       typedef _Tp value_type;
128       
129       ///  Default constructor.  First parameter is x, second parameter is y.
130       ///  Unspecified parameters default to 0.
131       _GLIBCXX_CONSTEXPR complex(const _Tp& __r = _Tp(), const _Tp& __i = _Tp())
132       : _M_real(__r), _M_imag(__i) { }
134       // Lets the compiler synthesize the copy constructor   
135       // complex (const complex<_Tp>&);
136       ///  Copy constructor.
137       template<typename _Up>
138         _GLIBCXX_CONSTEXPR complex(const complex<_Up>& __z)
139         : _M_real(__z.real()), _M_imag(__z.imag()) { }
141 #if __cplusplus >= 201103L
142       // _GLIBCXX_RESOLVE_LIB_DEFECTS
143       // DR 387. std::complex over-encapsulated.
144       __attribute ((__abi_tag__ ("cxx11")))
145       constexpr _Tp 
146       real() { return _M_real; }
148       __attribute ((__abi_tag__ ("cxx11")))
149       constexpr _Tp 
150       imag() { return _M_imag; }
151 #else
152       ///  Return real part of complex number.
153       _Tp& 
154       real() { return _M_real; }
156       ///  Return real part of complex number.
157       const _Tp& 
158       real() const { return _M_real; }
160       ///  Return imaginary part of complex number.
161       _Tp& 
162       imag() { return _M_imag; }
164       ///  Return imaginary part of complex number.
165       const _Tp& 
166       imag() const { return _M_imag; }
167 #endif
169       // _GLIBCXX_RESOLVE_LIB_DEFECTS
170       // DR 387. std::complex over-encapsulated.
171       void 
172       real(_Tp __val) { _M_real = __val; }
174       void 
175       imag(_Tp __val) { _M_imag = __val; }
177       /// Assign this complex number to scalar @a t.
178       complex<_Tp>& operator=(const _Tp&);
179       
180       /// Add @a t to this complex number.
181       // 26.2.5/1
182       complex<_Tp>&
183       operator+=(const _Tp& __t)
184       {
185         _M_real += __t;
186         return *this;
187       }
189       /// Subtract @a t from this complex number.
190       // 26.2.5/3
191       complex<_Tp>&
192       operator-=(const _Tp& __t)
193       {
194         _M_real -= __t;
195         return *this;
196       }
198       /// Multiply this complex number by @a t.
199       complex<_Tp>& operator*=(const _Tp&);
200       /// Divide this complex number by @a t.
201       complex<_Tp>& operator/=(const _Tp&);
203       // Lets the compiler synthesize the
204       // copy and assignment operator
205       // complex<_Tp>& operator= (const complex<_Tp>&);
206       /// Assign this complex number to complex @a z.
207       template<typename _Up>
208         complex<_Tp>& operator=(const complex<_Up>&);
209       /// Add @a z to this complex number.
210       template<typename _Up>
211         complex<_Tp>& operator+=(const complex<_Up>&);
212       /// Subtract @a z from this complex number.
213       template<typename _Up>
214         complex<_Tp>& operator-=(const complex<_Up>&);
215       /// Multiply this complex number by @a z.
216       template<typename _Up>
217         complex<_Tp>& operator*=(const complex<_Up>&);
218       /// Divide this complex number by @a z.
219       template<typename _Up>
220         complex<_Tp>& operator/=(const complex<_Up>&);
222       _GLIBCXX_USE_CONSTEXPR complex __rep() const
223       { return *this; }
225     private:
226       _Tp _M_real;
227       _Tp _M_imag;
228     };
230   template<typename _Tp>
231     complex<_Tp>&
232     complex<_Tp>::operator=(const _Tp& __t)
233     {
234      _M_real = __t;
235      _M_imag = _Tp();
236      return *this;
237     } 
239   // 26.2.5/5
240   template<typename _Tp>
241     complex<_Tp>&
242     complex<_Tp>::operator*=(const _Tp& __t)
243     {
244       _M_real *= __t;
245       _M_imag *= __t;
246       return *this;
247     }
249   // 26.2.5/7
250   template<typename _Tp>
251     complex<_Tp>&
252     complex<_Tp>::operator/=(const _Tp& __t)
253     {
254       _M_real /= __t;
255       _M_imag /= __t;
256       return *this;
257     }
259   template<typename _Tp>
260     template<typename _Up>
261     complex<_Tp>&
262     complex<_Tp>::operator=(const complex<_Up>& __z)
263     {
264       _M_real = __z.real();
265       _M_imag = __z.imag();
266       return *this;
267     }
269   // 26.2.5/9
270   template<typename _Tp>
271     template<typename _Up>
272     complex<_Tp>&
273     complex<_Tp>::operator+=(const complex<_Up>& __z)
274     {
275       _M_real += __z.real();
276       _M_imag += __z.imag();
277       return *this;
278     }
280   // 26.2.5/11
281   template<typename _Tp>
282     template<typename _Up>
283     complex<_Tp>&
284     complex<_Tp>::operator-=(const complex<_Up>& __z)
285     {
286       _M_real -= __z.real();
287       _M_imag -= __z.imag();
288       return *this;
289     }
291   // 26.2.5/13
292   // XXX: This is a grammar school implementation.
293   template<typename _Tp>
294     template<typename _Up>
295     complex<_Tp>&
296     complex<_Tp>::operator*=(const complex<_Up>& __z)
297     {
298       const _Tp __r = _M_real * __z.real() - _M_imag * __z.imag();
299       _M_imag = _M_real * __z.imag() + _M_imag * __z.real();
300       _M_real = __r;
301       return *this;
302     }
304   // 26.2.5/15
305   // XXX: This is a grammar school implementation.
306   template<typename _Tp>
307     template<typename _Up>
308     complex<_Tp>&
309     complex<_Tp>::operator/=(const complex<_Up>& __z)
310     {
311       const _Tp __r =  _M_real * __z.real() + _M_imag * __z.imag();
312       const _Tp __n = std::norm(__z);
313       _M_imag = (_M_imag * __z.real() - _M_real * __z.imag()) / __n;
314       _M_real = __r / __n;
315       return *this;
316     }
317     
318   // Operators:
319   //@{
320   ///  Return new complex value @a x plus @a y.
321   template<typename _Tp>
322     inline complex<_Tp>
323     operator+(const complex<_Tp>& __x, const complex<_Tp>& __y)
324     {
325       complex<_Tp> __r = __x;
326       __r += __y;
327       return __r;
328     }
330   template<typename _Tp>
331     inline complex<_Tp>
332     operator+(const complex<_Tp>& __x, const _Tp& __y)
333     {
334       complex<_Tp> __r = __x;
335       __r += __y;
336       return __r;
337     }
339   template<typename _Tp>
340     inline complex<_Tp>
341     operator+(const _Tp& __x, const complex<_Tp>& __y)
342     {
343       complex<_Tp> __r = __y;
344       __r += __x;
345       return __r;
346     }
347   //@}
349   //@{
350   ///  Return new complex value @a x minus @a y.
351   template<typename _Tp>
352     inline complex<_Tp>
353     operator-(const complex<_Tp>& __x, const complex<_Tp>& __y)
354     {
355       complex<_Tp> __r = __x;
356       __r -= __y;
357       return __r;
358     }
359     
360   template<typename _Tp>
361     inline complex<_Tp>
362     operator-(const complex<_Tp>& __x, const _Tp& __y)
363     {
364       complex<_Tp> __r = __x;
365       __r -= __y;
366       return __r;
367     }
369   template<typename _Tp>
370     inline complex<_Tp>
371     operator-(const _Tp& __x, const complex<_Tp>& __y)
372     {
373       complex<_Tp> __r(__x, -__y.imag());
374       __r -= __y.real();
375       return __r;
376     }
377   //@}
379   //@{
380   ///  Return new complex value @a x times @a y.
381   template<typename _Tp>
382     inline complex<_Tp>
383     operator*(const complex<_Tp>& __x, const complex<_Tp>& __y)
384     {
385       complex<_Tp> __r = __x;
386       __r *= __y;
387       return __r;
388     }
390   template<typename _Tp>
391     inline complex<_Tp>
392     operator*(const complex<_Tp>& __x, const _Tp& __y)
393     {
394       complex<_Tp> __r = __x;
395       __r *= __y;
396       return __r;
397     }
399   template<typename _Tp>
400     inline complex<_Tp>
401     operator*(const _Tp& __x, const complex<_Tp>& __y)
402     {
403       complex<_Tp> __r = __y;
404       __r *= __x;
405       return __r;
406     }
407   //@}
409   //@{
410   ///  Return new complex value @a x divided by @a y.
411   template<typename _Tp>
412     inline complex<_Tp>
413     operator/(const complex<_Tp>& __x, const complex<_Tp>& __y)
414     {
415       complex<_Tp> __r = __x;
416       __r /= __y;
417       return __r;
418     }
419     
420   template<typename _Tp>
421     inline complex<_Tp>
422     operator/(const complex<_Tp>& __x, const _Tp& __y)
423     {
424       complex<_Tp> __r = __x;
425       __r /= __y;
426       return __r;
427     }
429   template<typename _Tp>
430     inline complex<_Tp>
431     operator/(const _Tp& __x, const complex<_Tp>& __y)
432     {
433       complex<_Tp> __r = __x;
434       __r /= __y;
435       return __r;
436     }
437   //@}
439   ///  Return @a x.
440   template<typename _Tp>
441     inline complex<_Tp>
442     operator+(const complex<_Tp>& __x)
443     { return __x; }
445   ///  Return complex negation of @a x.
446   template<typename _Tp>
447     inline complex<_Tp>
448     operator-(const complex<_Tp>& __x)
449     {  return complex<_Tp>(-__x.real(), -__x.imag()); }
451   //@{
452   ///  Return true if @a x is equal to @a y.
453   template<typename _Tp>
454     inline _GLIBCXX_CONSTEXPR bool
455     operator==(const complex<_Tp>& __x, const complex<_Tp>& __y)
456     { return __x.real() == __y.real() && __x.imag() == __y.imag(); }
458   template<typename _Tp>
459     inline _GLIBCXX_CONSTEXPR bool
460     operator==(const complex<_Tp>& __x, const _Tp& __y)
461     { return __x.real() == __y && __x.imag() == _Tp(); }
463   template<typename _Tp>
464     inline _GLIBCXX_CONSTEXPR bool
465     operator==(const _Tp& __x, const complex<_Tp>& __y)
466     { return __x == __y.real() && _Tp() == __y.imag(); }
467   //@}
469   //@{
470   ///  Return false if @a x is equal to @a y.
471   template<typename _Tp>
472     inline _GLIBCXX_CONSTEXPR bool
473     operator!=(const complex<_Tp>& __x, const complex<_Tp>& __y)
474     { return __x.real() != __y.real() || __x.imag() != __y.imag(); }
476   template<typename _Tp>
477     inline _GLIBCXX_CONSTEXPR bool
478     operator!=(const complex<_Tp>& __x, const _Tp& __y)
479     { return __x.real() != __y || __x.imag() != _Tp(); }
481   template<typename _Tp>
482     inline _GLIBCXX_CONSTEXPR bool
483     operator!=(const _Tp& __x, const complex<_Tp>& __y)
484     { return __x != __y.real() || _Tp() != __y.imag(); }
485   //@}
487   ///  Extraction operator for complex values.
488   template<typename _Tp, typename _CharT, class _Traits>
489     basic_istream<_CharT, _Traits>&
490     operator>>(basic_istream<_CharT, _Traits>& __is, complex<_Tp>& __x)
491     {
492       _Tp __re_x, __im_x;
493       _CharT __ch;
494       __is >> __ch;
495       if (__ch == '(') 
496         {
497           __is >> __re_x >> __ch;
498           if (__ch == ',') 
499             {
500               __is >> __im_x >> __ch;
501               if (__ch == ')') 
502                 __x = complex<_Tp>(__re_x, __im_x);
503               else
504                 __is.setstate(ios_base::failbit);
505             }
506           else if (__ch == ')') 
507             __x = __re_x;
508           else
509             __is.setstate(ios_base::failbit);
510         }
511       else 
512         {
513           __is.putback(__ch);
514           __is >> __re_x;
515           __x = __re_x;
516         }
517       return __is;
518     }
520   ///  Insertion operator for complex values.
521   template<typename _Tp, typename _CharT, class _Traits>
522     basic_ostream<_CharT, _Traits>&
523     operator<<(basic_ostream<_CharT, _Traits>& __os, const complex<_Tp>& __x)
524     {
525       basic_ostringstream<_CharT, _Traits> __s;
526       __s.flags(__os.flags());
527       __s.imbue(__os.getloc());
528       __s.precision(__os.precision());
529       __s << '(' << __x.real() << ',' << __x.imag() << ')';
530       return __os << __s.str();
531     }
533   // Values
534 #if __cplusplus >= 201103L
535   template<typename _Tp>
536     constexpr _Tp
537     real(const complex<_Tp>& __z)
538     { return __z.real(); }
540   template<typename _Tp>
541     constexpr _Tp
542     imag(const complex<_Tp>& __z)
543     { return __z.imag(); }
544 #else
545   template<typename _Tp>
546     inline _Tp&
547     real(complex<_Tp>& __z)
548     { return __z.real(); }
549     
550   template<typename _Tp>
551     inline const _Tp&
552     real(const complex<_Tp>& __z)
553     { return __z.real(); }
554     
555   template<typename _Tp>
556     inline _Tp&
557     imag(complex<_Tp>& __z)
558     { return __z.imag(); }
559     
560   template<typename _Tp>
561     inline const _Tp&
562     imag(const complex<_Tp>& __z)
563     { return __z.imag(); }
564 #endif
566   // 26.2.7/3 abs(__z):  Returns the magnitude of __z.
567   template<typename _Tp>
568     inline _Tp
569     __complex_abs(const complex<_Tp>& __z)
570     {
571       _Tp __x = __z.real();
572       _Tp __y = __z.imag();
573       const _Tp __s = std::max(abs(__x), abs(__y));
574       if (__s == _Tp())  // well ...
575         return __s;
576       __x /= __s; 
577       __y /= __s;
578       return __s * sqrt(__x * __x + __y * __y);
579     }
581 #if _GLIBCXX_USE_C99_COMPLEX
582   inline float
583   __complex_abs(__complex__ float __z) { return __builtin_cabsf(__z); }
585   inline double
586   __complex_abs(__complex__ double __z) { return __builtin_cabs(__z); }
588   inline long double
589   __complex_abs(const __complex__ long double& __z)
590   { return __builtin_cabsl(__z); }
592   template<typename _Tp>
593     inline _Tp
594     abs(const complex<_Tp>& __z) { return __complex_abs(__z.__rep()); }
595 #else
596   template<typename _Tp>
597     inline _Tp
598     abs(const complex<_Tp>& __z) { return __complex_abs(__z); }
599 #endif  
602   // 26.2.7/4: arg(__z): Returns the phase angle of __z.
603   template<typename _Tp>
604     inline _Tp
605     __complex_arg(const complex<_Tp>& __z)
606     { return  atan2(__z.imag(), __z.real()); }
608 #if _GLIBCXX_USE_C99_COMPLEX
609   inline float
610   __complex_arg(__complex__ float __z) { return __builtin_cargf(__z); }
612   inline double
613   __complex_arg(__complex__ double __z) { return __builtin_carg(__z); }
615   inline long double
616   __complex_arg(const __complex__ long double& __z)
617   { return __builtin_cargl(__z); }
619   template<typename _Tp>
620     inline _Tp
621     arg(const complex<_Tp>& __z) { return __complex_arg(__z.__rep()); }
622 #else
623   template<typename _Tp>
624     inline _Tp
625     arg(const complex<_Tp>& __z) { return __complex_arg(__z); }
626 #endif
628   // 26.2.7/5: norm(__z) returns the squared magnitude of __z.
629   //     As defined, norm() is -not- a norm is the common mathematical
630   //     sens used in numerics.  The helper class _Norm_helper<> tries to
631   //     distinguish between builtin floating point and the rest, so as
632   //     to deliver an answer as close as possible to the real value.
633   template<bool>
634     struct _Norm_helper
635     {
636       template<typename _Tp>
637         static inline _Tp _S_do_it(const complex<_Tp>& __z)
638         {
639           const _Tp __x = __z.real();
640           const _Tp __y = __z.imag();
641           return __x * __x + __y * __y;
642         }
643     };
645   template<>
646     struct _Norm_helper<true>
647     {
648       template<typename _Tp>
649         static inline _Tp _S_do_it(const complex<_Tp>& __z)
650         {
651           _Tp __res = std::abs(__z);
652           return __res * __res;
653         }
654     };
655   
656   template<typename _Tp>
657     inline _Tp
658     norm(const complex<_Tp>& __z)
659     {
660       return _Norm_helper<__is_floating<_Tp>::__value 
661         && !_GLIBCXX_FAST_MATH>::_S_do_it(__z);
662     }
664   template<typename _Tp>
665     inline complex<_Tp>
666     polar(const _Tp& __rho, const _Tp& __theta)
667     { return complex<_Tp>(__rho * cos(__theta), __rho * sin(__theta)); }
669   template<typename _Tp>
670     inline complex<_Tp>
671     conj(const complex<_Tp>& __z)
672     { return complex<_Tp>(__z.real(), -__z.imag()); }
673   
674   // Transcendentals
676   // 26.2.8/1 cos(__z):  Returns the cosine of __z.
677   template<typename _Tp>
678     inline complex<_Tp>
679     __complex_cos(const complex<_Tp>& __z)
680     {
681       const _Tp __x = __z.real();
682       const _Tp __y = __z.imag();
683       return complex<_Tp>(cos(__x) * cosh(__y), -sin(__x) * sinh(__y));
684     }
686 #if _GLIBCXX_USE_C99_COMPLEX
687   inline __complex__ float
688   __complex_cos(__complex__ float __z) { return __builtin_ccosf(__z); }
690   inline __complex__ double
691   __complex_cos(__complex__ double __z) { return __builtin_ccos(__z); }
693   inline __complex__ long double
694   __complex_cos(const __complex__ long double& __z)
695   { return __builtin_ccosl(__z); }
697   template<typename _Tp>
698     inline complex<_Tp>
699     cos(const complex<_Tp>& __z) { return __complex_cos(__z.__rep()); }
700 #else
701   template<typename _Tp>
702     inline complex<_Tp>
703     cos(const complex<_Tp>& __z) { return __complex_cos(__z); }
704 #endif
706   // 26.2.8/2 cosh(__z): Returns the hyperbolic cosine of __z.
707   template<typename _Tp>
708     inline complex<_Tp>
709     __complex_cosh(const complex<_Tp>& __z)
710     {
711       const _Tp __x = __z.real();
712       const _Tp __y = __z.imag();
713       return complex<_Tp>(cosh(__x) * cos(__y), sinh(__x) * sin(__y));
714     }
716 #if _GLIBCXX_USE_C99_COMPLEX
717   inline __complex__ float
718   __complex_cosh(__complex__ float __z) { return __builtin_ccoshf(__z); }
720   inline __complex__ double
721   __complex_cosh(__complex__ double __z) { return __builtin_ccosh(__z); }
723   inline __complex__ long double
724   __complex_cosh(const __complex__ long double& __z)
725   { return __builtin_ccoshl(__z); }
727   template<typename _Tp>
728     inline complex<_Tp>
729     cosh(const complex<_Tp>& __z) { return __complex_cosh(__z.__rep()); }
730 #else
731   template<typename _Tp>
732     inline complex<_Tp>
733     cosh(const complex<_Tp>& __z) { return __complex_cosh(__z); }
734 #endif
736   // 26.2.8/3 exp(__z): Returns the complex base e exponential of x
737   template<typename _Tp>
738     inline complex<_Tp>
739     __complex_exp(const complex<_Tp>& __z)
740     { return std::polar(exp(__z.real()), __z.imag()); }
742 #if _GLIBCXX_USE_C99_COMPLEX
743   inline __complex__ float
744   __complex_exp(__complex__ float __z) { return __builtin_cexpf(__z); }
746   inline __complex__ double
747   __complex_exp(__complex__ double __z) { return __builtin_cexp(__z); }
749   inline __complex__ long double
750   __complex_exp(const __complex__ long double& __z)
751   { return __builtin_cexpl(__z); }
753   template<typename _Tp>
754     inline complex<_Tp>
755     exp(const complex<_Tp>& __z) { return __complex_exp(__z.__rep()); }
756 #else
757   template<typename _Tp>
758     inline complex<_Tp>
759     exp(const complex<_Tp>& __z) { return __complex_exp(__z); }
760 #endif
762   // 26.2.8/5 log(__z): Returns the natural complex logarithm of __z.
763   //                    The branch cut is along the negative axis.
764   template<typename _Tp>
765     inline complex<_Tp>
766     __complex_log(const complex<_Tp>& __z)
767     { return complex<_Tp>(log(std::abs(__z)), std::arg(__z)); }
769 #if _GLIBCXX_USE_C99_COMPLEX
770   inline __complex__ float
771   __complex_log(__complex__ float __z) { return __builtin_clogf(__z); }
773   inline __complex__ double
774   __complex_log(__complex__ double __z) { return __builtin_clog(__z); }
776   inline __complex__ long double
777   __complex_log(const __complex__ long double& __z)
778   { return __builtin_clogl(__z); }
780   template<typename _Tp>
781     inline complex<_Tp>
782     log(const complex<_Tp>& __z) { return __complex_log(__z.__rep()); }
783 #else
784   template<typename _Tp>
785     inline complex<_Tp>
786     log(const complex<_Tp>& __z) { return __complex_log(__z); }
787 #endif
789   template<typename _Tp>
790     inline complex<_Tp>
791     log10(const complex<_Tp>& __z)
792     { return std::log(__z) / log(_Tp(10.0)); }
794   // 26.2.8/10 sin(__z): Returns the sine of __z.
795   template<typename _Tp>
796     inline complex<_Tp>
797     __complex_sin(const complex<_Tp>& __z)
798     {
799       const _Tp __x = __z.real();
800       const _Tp __y = __z.imag();
801       return complex<_Tp>(sin(__x) * cosh(__y), cos(__x) * sinh(__y)); 
802     }
804 #if _GLIBCXX_USE_C99_COMPLEX
805   inline __complex__ float
806   __complex_sin(__complex__ float __z) { return __builtin_csinf(__z); }
808   inline __complex__ double
809   __complex_sin(__complex__ double __z) { return __builtin_csin(__z); }
811   inline __complex__ long double
812   __complex_sin(const __complex__ long double& __z)
813   { return __builtin_csinl(__z); }
815   template<typename _Tp>
816     inline complex<_Tp>
817     sin(const complex<_Tp>& __z) { return __complex_sin(__z.__rep()); }
818 #else
819   template<typename _Tp>
820     inline complex<_Tp>
821     sin(const complex<_Tp>& __z) { return __complex_sin(__z); }
822 #endif
824   // 26.2.8/11 sinh(__z): Returns the hyperbolic sine of __z.
825   template<typename _Tp>
826     inline complex<_Tp>
827     __complex_sinh(const complex<_Tp>& __z)
828     {
829       const _Tp __x = __z.real();
830       const _Tp  __y = __z.imag();
831       return complex<_Tp>(sinh(__x) * cos(__y), cosh(__x) * sin(__y));
832     }
834 #if _GLIBCXX_USE_C99_COMPLEX
835   inline __complex__ float
836   __complex_sinh(__complex__ float __z) { return __builtin_csinhf(__z); }      
838   inline __complex__ double
839   __complex_sinh(__complex__ double __z) { return __builtin_csinh(__z); }      
841   inline __complex__ long double
842   __complex_sinh(const __complex__ long double& __z)
843   { return __builtin_csinhl(__z); }      
845   template<typename _Tp>
846     inline complex<_Tp>
847     sinh(const complex<_Tp>& __z) { return __complex_sinh(__z.__rep()); }
848 #else
849   template<typename _Tp>
850     inline complex<_Tp>
851     sinh(const complex<_Tp>& __z) { return __complex_sinh(__z); }
852 #endif
854   // 26.2.8/13 sqrt(__z): Returns the complex square root of __z.
855   //                     The branch cut is on the negative axis.
856   template<typename _Tp>
857     complex<_Tp>
858     __complex_sqrt(const complex<_Tp>& __z)
859     {
860       _Tp __x = __z.real();
861       _Tp __y = __z.imag();
863       if (__x == _Tp())
864         {
865           _Tp __t = sqrt(abs(__y) / 2);
866           return complex<_Tp>(__t, __y < _Tp() ? -__t : __t);
867         }
868       else
869         {
870           _Tp __t = sqrt(2 * (std::abs(__z) + abs(__x)));
871           _Tp __u = __t / 2;
872           return __x > _Tp()
873             ? complex<_Tp>(__u, __y / __t)
874             : complex<_Tp>(abs(__y) / __t, __y < _Tp() ? -__u : __u);
875         }
876     }
878 #if _GLIBCXX_USE_C99_COMPLEX
879   inline __complex__ float
880   __complex_sqrt(__complex__ float __z) { return __builtin_csqrtf(__z); }
882   inline __complex__ double
883   __complex_sqrt(__complex__ double __z) { return __builtin_csqrt(__z); }
885   inline __complex__ long double
886   __complex_sqrt(const __complex__ long double& __z)
887   { return __builtin_csqrtl(__z); }
889   template<typename _Tp>
890     inline complex<_Tp>
891     sqrt(const complex<_Tp>& __z) { return __complex_sqrt(__z.__rep()); }
892 #else
893   template<typename _Tp>
894     inline complex<_Tp>
895     sqrt(const complex<_Tp>& __z) { return __complex_sqrt(__z); }
896 #endif
898   // 26.2.8/14 tan(__z):  Return the complex tangent of __z.
899   
900   template<typename _Tp>
901     inline complex<_Tp>
902     __complex_tan(const complex<_Tp>& __z)
903     { return std::sin(__z) / std::cos(__z); }
905 #if _GLIBCXX_USE_C99_COMPLEX
906   inline __complex__ float
907   __complex_tan(__complex__ float __z) { return __builtin_ctanf(__z); }
909   inline __complex__ double
910   __complex_tan(__complex__ double __z) { return __builtin_ctan(__z); }
912   inline __complex__ long double
913   __complex_tan(const __complex__ long double& __z)
914   { return __builtin_ctanl(__z); }
916   template<typename _Tp>
917     inline complex<_Tp>
918     tan(const complex<_Tp>& __z) { return __complex_tan(__z.__rep()); }
919 #else
920   template<typename _Tp>
921     inline complex<_Tp>
922     tan(const complex<_Tp>& __z) { return __complex_tan(__z); }
923 #endif
926   // 26.2.8/15 tanh(__z):  Returns the hyperbolic tangent of __z.
927   
928   template<typename _Tp>
929     inline complex<_Tp>
930     __complex_tanh(const complex<_Tp>& __z)
931     { return std::sinh(__z) / std::cosh(__z); }
933 #if _GLIBCXX_USE_C99_COMPLEX
934   inline __complex__ float
935   __complex_tanh(__complex__ float __z) { return __builtin_ctanhf(__z); }
937   inline __complex__ double
938   __complex_tanh(__complex__ double __z) { return __builtin_ctanh(__z); }
940   inline __complex__ long double
941   __complex_tanh(const __complex__ long double& __z)
942   { return __builtin_ctanhl(__z); }
944   template<typename _Tp>
945     inline complex<_Tp>
946     tanh(const complex<_Tp>& __z) { return __complex_tanh(__z.__rep()); }
947 #else
948   template<typename _Tp>
949     inline complex<_Tp>
950     tanh(const complex<_Tp>& __z) { return __complex_tanh(__z); }
951 #endif
954   // 26.2.8/9  pow(__x, __y): Returns the complex power base of __x
955   //                          raised to the __y-th power.  The branch
956   //                          cut is on the negative axis.
957 #if __cplusplus < 201103L
958   template<typename _Tp>
959     complex<_Tp>
960     __complex_pow_unsigned(complex<_Tp> __x, unsigned __n)
961     {
962       complex<_Tp> __y = __n % 2 ? __x : complex<_Tp>(1);
964       while (__n >>= 1)
965         {
966           __x *= __x;
967           if (__n % 2)
968             __y *= __x;
969         }
971       return __y;
972     }
974   // _GLIBCXX_RESOLVE_LIB_DEFECTS
975   // DR 844. complex pow return type is ambiguous.
976   template<typename _Tp>
977     inline complex<_Tp>
978     pow(const complex<_Tp>& __z, int __n)
979     {
980       return __n < 0
981         ? complex<_Tp>(1) / std::__complex_pow_unsigned(__z, -__n)
982         : std::__complex_pow_unsigned(__z, __n);
983     }
984 #endif
986   template<typename _Tp>
987     complex<_Tp>
988     pow(const complex<_Tp>& __x, const _Tp& __y)
989     {
990 #ifndef _GLIBCXX_USE_C99_COMPLEX
991       if (__x == _Tp())
992         return _Tp();
993 #endif
994       if (__x.imag() == _Tp() && __x.real() > _Tp())
995         return pow(__x.real(), __y);
997       complex<_Tp> __t = std::log(__x);
998       return std::polar(exp(__y * __t.real()), __y * __t.imag());
999     }
1001   template<typename _Tp>
1002     inline complex<_Tp>
1003     __complex_pow(const complex<_Tp>& __x, const complex<_Tp>& __y)
1004     { return __x == _Tp() ? _Tp() : std::exp(__y * std::log(__x)); }
1006 #if _GLIBCXX_USE_C99_COMPLEX
1007   inline __complex__ float
1008   __complex_pow(__complex__ float __x, __complex__ float __y)
1009   { return __builtin_cpowf(__x, __y); }
1011   inline __complex__ double
1012   __complex_pow(__complex__ double __x, __complex__ double __y)
1013   { return __builtin_cpow(__x, __y); }
1015   inline __complex__ long double
1016   __complex_pow(const __complex__ long double& __x,
1017                 const __complex__ long double& __y)
1018   { return __builtin_cpowl(__x, __y); }
1020   template<typename _Tp>
1021     inline complex<_Tp>
1022     pow(const complex<_Tp>& __x, const complex<_Tp>& __y)
1023     { return __complex_pow(__x.__rep(), __y.__rep()); }
1024 #else
1025   template<typename _Tp>
1026     inline complex<_Tp>
1027     pow(const complex<_Tp>& __x, const complex<_Tp>& __y)
1028     { return __complex_pow(__x, __y); }
1029 #endif
1031   template<typename _Tp>
1032     inline complex<_Tp>
1033     pow(const _Tp& __x, const complex<_Tp>& __y)
1034     {
1035       return __x > _Tp() ? std::polar(pow(__x, __y.real()),
1036                                       __y.imag() * log(__x))
1037                          : std::pow(complex<_Tp>(__x), __y);
1038     }
1040   /// 26.2.3  complex specializations
1041   /// complex<float> specialization
1042   template<>
1043     struct complex<float>
1044     {
1045       typedef float value_type;
1046       typedef __complex__ float _ComplexT;
1048       _GLIBCXX_CONSTEXPR complex(_ComplexT __z) : _M_value(__z) { }
1050       _GLIBCXX_CONSTEXPR complex(float __r = 0.0f, float __i = 0.0f)
1051 #if __cplusplus >= 201103L
1052       : _M_value{ __r, __i } { }
1053 #else
1054       {
1055         __real__ _M_value = __r;
1056         __imag__ _M_value = __i;
1057       }
1058 #endif
1060       explicit _GLIBCXX_CONSTEXPR complex(const complex<double>&);
1061       explicit _GLIBCXX_CONSTEXPR complex(const complex<long double>&); 
1063 #if __cplusplus >= 201103L
1064       // _GLIBCXX_RESOLVE_LIB_DEFECTS
1065       // DR 387. std::complex over-encapsulated.
1066       __attribute ((__abi_tag__ ("cxx11")))
1067       constexpr float 
1068       real() { return __real__ _M_value; }
1070       __attribute ((__abi_tag__ ("cxx11")))
1071       constexpr float 
1072       imag() { return __imag__ _M_value; }
1073 #else
1074       float& 
1075       real() { return __real__ _M_value; }
1077       const float& 
1078       real() const { return __real__ _M_value; }      
1080       float& 
1081       imag() { return __imag__ _M_value; }
1083       const float& 
1084       imag() const { return __imag__ _M_value; }
1085 #endif
1087       // _GLIBCXX_RESOLVE_LIB_DEFECTS
1088       // DR 387. std::complex over-encapsulated.
1089       void 
1090       real(float __val) { __real__ _M_value = __val; }
1092       void 
1093       imag(float __val) { __imag__ _M_value = __val; }
1095       complex&
1096       operator=(float __f)
1097       {
1098         _M_value = __f;
1099         return *this;
1100       }
1102       complex&
1103       operator+=(float __f)
1104       {
1105         _M_value += __f;
1106         return *this;
1107       }
1109       complex&
1110       operator-=(float __f)
1111       {
1112         _M_value -= __f;
1113         return *this;
1114       }
1116       complex&
1117       operator*=(float __f)
1118       {
1119         _M_value *= __f;
1120         return *this;
1121       }
1123       complex&
1124       operator/=(float __f)
1125       {
1126         _M_value /= __f;
1127         return *this;
1128       }
1130       // Let the compiler synthesize the copy and assignment
1131       // operator.  It always does a pretty good job.
1132       // complex& operator=(const complex&);
1134       template<typename _Tp>
1135         complex&
1136         operator=(const complex<_Tp>&  __z)
1137         {
1138           __real__ _M_value = __z.real();
1139           __imag__ _M_value = __z.imag();
1140           return *this;
1141         }
1143       template<typename _Tp>
1144         complex&
1145         operator+=(const complex<_Tp>& __z)
1146         {
1147           __real__ _M_value += __z.real();
1148           __imag__ _M_value += __z.imag();
1149           return *this;
1150         }
1152       template<class _Tp>
1153         complex&
1154         operator-=(const complex<_Tp>& __z)
1155         {
1156           __real__ _M_value -= __z.real();
1157           __imag__ _M_value -= __z.imag();
1158           return *this;
1159         }
1161       template<class _Tp>
1162         complex&
1163         operator*=(const complex<_Tp>& __z)
1164         {
1165           _ComplexT __t;
1166           __real__ __t = __z.real();
1167           __imag__ __t = __z.imag();
1168           _M_value *= __t;
1169           return *this;
1170         }
1172       template<class _Tp>
1173         complex&
1174         operator/=(const complex<_Tp>& __z)
1175         {
1176           _ComplexT __t;
1177           __real__ __t = __z.real();
1178           __imag__ __t = __z.imag();
1179           _M_value /= __t;
1180           return *this;
1181         }
1183       _GLIBCXX_USE_CONSTEXPR _ComplexT __rep() const { return _M_value; }
1185     private:
1186       _ComplexT _M_value;
1187     };
1189   /// 26.2.3  complex specializations
1190   /// complex<double> specialization
1191   template<>
1192     struct complex<double>
1193     {
1194       typedef double value_type;
1195       typedef __complex__ double _ComplexT;
1197       _GLIBCXX_CONSTEXPR complex(_ComplexT __z) : _M_value(__z) { }
1199       _GLIBCXX_CONSTEXPR complex(double __r = 0.0, double __i = 0.0)
1200 #if __cplusplus >= 201103L
1201       : _M_value{ __r, __i } { }
1202 #else
1203       {
1204         __real__ _M_value = __r;
1205         __imag__ _M_value = __i;
1206       }
1207 #endif
1209       _GLIBCXX_CONSTEXPR complex(const complex<float>& __z)
1210       : _M_value(__z.__rep()) { }
1212       explicit _GLIBCXX_CONSTEXPR complex(const complex<long double>&); 
1214 #if __cplusplus >= 201103L
1215       // _GLIBCXX_RESOLVE_LIB_DEFECTS
1216       // DR 387. std::complex over-encapsulated.
1217       __attribute ((__abi_tag__ ("cxx11")))
1218       constexpr double 
1219       real() { return __real__ _M_value; }
1221       __attribute ((__abi_tag__ ("cxx11")))
1222       constexpr double 
1223       imag() { return __imag__ _M_value; }
1224 #else
1225       double& 
1226       real() { return __real__ _M_value; }
1228       const double& 
1229       real() const { return __real__ _M_value; }
1231       double& 
1232       imag() { return __imag__ _M_value; }
1234       const double& 
1235       imag() const { return __imag__ _M_value; }
1236 #endif
1238       // _GLIBCXX_RESOLVE_LIB_DEFECTS
1239       // DR 387. std::complex over-encapsulated.
1240       void 
1241       real(double __val) { __real__ _M_value = __val; }
1243       void 
1244       imag(double __val) { __imag__ _M_value = __val; }
1246       complex&
1247       operator=(double __d)
1248       {
1249         _M_value = __d;
1250         return *this;
1251       }
1253       complex&
1254       operator+=(double __d)
1255       {
1256         _M_value += __d;
1257         return *this;
1258       }
1259         
1260       complex&
1261       operator-=(double __d)
1262       {
1263         _M_value -= __d;
1264         return *this;
1265       }
1267       complex&
1268       operator*=(double __d)
1269       {
1270         _M_value *= __d;
1271         return *this;
1272       }
1274       complex&
1275       operator/=(double __d)
1276       {
1277         _M_value /= __d;
1278         return *this;
1279       }
1281       // The compiler will synthesize this, efficiently.
1282       // complex& operator=(const complex&);
1284       template<typename _Tp>
1285         complex&
1286         operator=(const complex<_Tp>& __z)
1287         {
1288           __real__ _M_value = __z.real();
1289           __imag__ _M_value = __z.imag();
1290           return *this;
1291         }
1293       template<typename _Tp>
1294         complex&
1295         operator+=(const complex<_Tp>& __z)
1296         {
1297           __real__ _M_value += __z.real();
1298           __imag__ _M_value += __z.imag();
1299           return *this;
1300         }
1302       template<typename _Tp>
1303         complex&
1304         operator-=(const complex<_Tp>& __z)
1305         {
1306           __real__ _M_value -= __z.real();
1307           __imag__ _M_value -= __z.imag();
1308           return *this;
1309         }
1311       template<typename _Tp>
1312         complex&
1313         operator*=(const complex<_Tp>& __z)
1314         {
1315           _ComplexT __t;
1316           __real__ __t = __z.real();
1317           __imag__ __t = __z.imag();
1318           _M_value *= __t;
1319           return *this;
1320         }
1322       template<typename _Tp>
1323         complex&
1324         operator/=(const complex<_Tp>& __z)
1325         {
1326           _ComplexT __t;
1327           __real__ __t = __z.real();
1328           __imag__ __t = __z.imag();
1329           _M_value /= __t;
1330           return *this;
1331         }
1333       _GLIBCXX_USE_CONSTEXPR _ComplexT __rep() const { return _M_value; }
1335     private:
1336       _ComplexT _M_value;
1337     };
1339   /// 26.2.3  complex specializations
1340   /// complex<long double> specialization
1341   template<>
1342     struct complex<long double>
1343     {
1344       typedef long double value_type;
1345       typedef __complex__ long double _ComplexT;
1347       _GLIBCXX_CONSTEXPR complex(_ComplexT __z) : _M_value(__z) { }
1349       _GLIBCXX_CONSTEXPR complex(long double __r = 0.0L, 
1350                                  long double __i = 0.0L)
1351 #if __cplusplus >= 201103L
1352       : _M_value{ __r, __i } { }
1353 #else
1354       {
1355         __real__ _M_value = __r;
1356         __imag__ _M_value = __i;
1357       }
1358 #endif
1360       _GLIBCXX_CONSTEXPR complex(const complex<float>& __z)
1361       : _M_value(__z.__rep()) { }
1363       _GLIBCXX_CONSTEXPR complex(const complex<double>& __z)
1364       : _M_value(__z.__rep()) { }
1366 #if __cplusplus >= 201103L
1367       // _GLIBCXX_RESOLVE_LIB_DEFECTS
1368       // DR 387. std::complex over-encapsulated.
1369       __attribute ((__abi_tag__ ("cxx11")))
1370       constexpr long double 
1371       real() { return __real__ _M_value; }
1373       __attribute ((__abi_tag__ ("cxx11")))
1374       constexpr long double 
1375       imag() { return __imag__ _M_value; }
1376 #else
1377       long double& 
1378       real() { return __real__ _M_value; }
1380       const long double& 
1381       real() const { return __real__ _M_value; }
1383       long double& 
1384       imag() { return __imag__ _M_value; }
1386       const long double& 
1387       imag() const { return __imag__ _M_value; }
1388 #endif
1390       // _GLIBCXX_RESOLVE_LIB_DEFECTS
1391       // DR 387. std::complex over-encapsulated.
1392       void 
1393       real(long double __val) { __real__ _M_value = __val; }
1395       void 
1396       imag(long double __val) { __imag__ _M_value = __val; }
1398       complex&
1399       operator=(long double __r)
1400       {
1401         _M_value = __r;
1402         return *this;
1403       }
1405       complex&
1406       operator+=(long double __r)
1407       {
1408         _M_value += __r;
1409         return *this;
1410       }
1412       complex&
1413       operator-=(long double __r)
1414       {
1415         _M_value -= __r;
1416         return *this;
1417       }
1419       complex&
1420       operator*=(long double __r)
1421       {
1422         _M_value *= __r;
1423         return *this;
1424       }
1426       complex&
1427       operator/=(long double __r)
1428       {
1429         _M_value /= __r;
1430         return *this;
1431       }
1433       // The compiler knows how to do this efficiently
1434       // complex& operator=(const complex&);
1436       template<typename _Tp>
1437         complex&
1438         operator=(const complex<_Tp>& __z)
1439         {
1440           __real__ _M_value = __z.real();
1441           __imag__ _M_value = __z.imag();
1442           return *this;
1443         }
1445       template<typename _Tp>
1446         complex&
1447         operator+=(const complex<_Tp>& __z)
1448         {
1449           __real__ _M_value += __z.real();
1450           __imag__ _M_value += __z.imag();
1451           return *this;
1452         }
1454       template<typename _Tp>
1455         complex&
1456         operator-=(const complex<_Tp>& __z)
1457         {
1458           __real__ _M_value -= __z.real();
1459           __imag__ _M_value -= __z.imag();
1460           return *this;
1461         }
1463       template<typename _Tp>
1464         complex&
1465         operator*=(const complex<_Tp>& __z)
1466         {
1467           _ComplexT __t;
1468           __real__ __t = __z.real();
1469           __imag__ __t = __z.imag();
1470           _M_value *= __t;
1471           return *this;
1472         }
1474       template<typename _Tp>
1475         complex&
1476         operator/=(const complex<_Tp>& __z)
1477         {
1478           _ComplexT __t;
1479           __real__ __t = __z.real();
1480           __imag__ __t = __z.imag();
1481           _M_value /= __t;
1482           return *this;
1483         }
1485       _GLIBCXX_USE_CONSTEXPR _ComplexT __rep() const { return _M_value; }
1487     private:
1488       _ComplexT _M_value;
1489     };
1491   // These bits have to be at the end of this file, so that the
1492   // specializations have all been defined.
1493   inline _GLIBCXX_CONSTEXPR
1494   complex<float>::complex(const complex<double>& __z)
1495   : _M_value(__z.__rep()) { }
1497   inline _GLIBCXX_CONSTEXPR
1498   complex<float>::complex(const complex<long double>& __z)
1499   : _M_value(__z.__rep()) { }
1501   inline _GLIBCXX_CONSTEXPR
1502   complex<double>::complex(const complex<long double>& __z)
1503   : _M_value(__z.__rep()) { }
1505   // Inhibit implicit instantiations for required instantiations,
1506   // which are defined via explicit instantiations elsewhere.
1507   // NB:  This syntax is a GNU extension.
1508 #if _GLIBCXX_EXTERN_TEMPLATE
1509   extern template istream& operator>>(istream&, complex<float>&);
1510   extern template ostream& operator<<(ostream&, const complex<float>&);
1511   extern template istream& operator>>(istream&, complex<double>&);
1512   extern template ostream& operator<<(ostream&, const complex<double>&);
1513   extern template istream& operator>>(istream&, complex<long double>&);
1514   extern template ostream& operator<<(ostream&, const complex<long double>&);
1516 #ifdef _GLIBCXX_USE_WCHAR_T
1517   extern template wistream& operator>>(wistream&, complex<float>&);
1518   extern template wostream& operator<<(wostream&, const complex<float>&);
1519   extern template wistream& operator>>(wistream&, complex<double>&);
1520   extern template wostream& operator<<(wostream&, const complex<double>&);
1521   extern template wistream& operator>>(wistream&, complex<long double>&);
1522   extern template wostream& operator<<(wostream&, const complex<long double>&);
1523 #endif
1524 #endif
1526   // @} group complex_numbers
1528 _GLIBCXX_END_NAMESPACE_VERSION
1529 } // namespace
1531 namespace __gnu_cxx _GLIBCXX_VISIBILITY(default)
1533 _GLIBCXX_BEGIN_NAMESPACE_VERSION
1535   // See ext/type_traits.h for the primary template.
1536   template<typename _Tp, typename _Up>
1537     struct __promote_2<std::complex<_Tp>, _Up>
1538     {
1539     public:
1540       typedef std::complex<typename __promote_2<_Tp, _Up>::__type> __type;
1541     };
1543   template<typename _Tp, typename _Up>
1544     struct __promote_2<_Tp, std::complex<_Up> >
1545     {
1546     public:
1547       typedef std::complex<typename __promote_2<_Tp, _Up>::__type> __type;
1548     };
1549   
1550   template<typename _Tp, typename _Up>
1551     struct __promote_2<std::complex<_Tp>, std::complex<_Up> >
1552     {
1553     public:
1554       typedef std::complex<typename __promote_2<_Tp, _Up>::__type> __type;
1555     };
1557 _GLIBCXX_END_NAMESPACE_VERSION
1558 } // namespace
1560 #if __cplusplus >= 201103L
1562 namespace std _GLIBCXX_VISIBILITY(default)
1564 _GLIBCXX_BEGIN_NAMESPACE_VERSION
1566   // Forward declarations.
1567   template<typename _Tp> std::complex<_Tp> acos(const std::complex<_Tp>&);
1568   template<typename _Tp> std::complex<_Tp> asin(const std::complex<_Tp>&);
1569   template<typename _Tp> std::complex<_Tp> atan(const std::complex<_Tp>&);
1571   template<typename _Tp> std::complex<_Tp> acosh(const std::complex<_Tp>&);
1572   template<typename _Tp> std::complex<_Tp> asinh(const std::complex<_Tp>&);
1573   template<typename _Tp> std::complex<_Tp> atanh(const std::complex<_Tp>&);
1574   // DR 595.
1575   template<typename _Tp> _Tp               fabs(const std::complex<_Tp>&);
1577   template<typename _Tp>
1578     inline std::complex<_Tp>
1579     __complex_acos(const std::complex<_Tp>& __z)
1580     {
1581       const std::complex<_Tp> __t = std::asin(__z);
1582       const _Tp __pi_2 = 1.5707963267948966192313216916397514L;
1583       return std::complex<_Tp>(__pi_2 - __t.real(), -__t.imag());
1584     }
1586 #if _GLIBCXX_USE_C99_COMPLEX_TR1
1587   inline __complex__ float
1588   __complex_acos(__complex__ float __z)
1589   { return __builtin_cacosf(__z); }
1591   inline __complex__ double
1592   __complex_acos(__complex__ double __z)
1593   { return __builtin_cacos(__z); }
1595   inline __complex__ long double
1596   __complex_acos(const __complex__ long double& __z)
1597   { return __builtin_cacosl(__z); }
1599   template<typename _Tp>
1600     inline std::complex<_Tp>
1601     acos(const std::complex<_Tp>& __z)
1602     { return __complex_acos(__z.__rep()); }
1603 #else
1604   /// acos(__z) [8.1.2].
1605   //  Effects:  Behaves the same as C99 function cacos, defined
1606   //            in subclause 7.3.5.1.
1607   template<typename _Tp>
1608     inline std::complex<_Tp>
1609     acos(const std::complex<_Tp>& __z)
1610     { return __complex_acos(__z); }
1611 #endif
1613   template<typename _Tp>
1614     inline std::complex<_Tp>
1615     __complex_asin(const std::complex<_Tp>& __z)
1616     {
1617       std::complex<_Tp> __t(-__z.imag(), __z.real());
1618       __t = std::asinh(__t);
1619       return std::complex<_Tp>(__t.imag(), -__t.real());
1620     }
1622 #if _GLIBCXX_USE_C99_COMPLEX_TR1
1623   inline __complex__ float
1624   __complex_asin(__complex__ float __z)
1625   { return __builtin_casinf(__z); }
1627   inline __complex__ double
1628   __complex_asin(__complex__ double __z)
1629   { return __builtin_casin(__z); }
1631   inline __complex__ long double
1632   __complex_asin(const __complex__ long double& __z)
1633   { return __builtin_casinl(__z); }
1635   template<typename _Tp>
1636     inline std::complex<_Tp>
1637     asin(const std::complex<_Tp>& __z)
1638     { return __complex_asin(__z.__rep()); }
1639 #else
1640   /// asin(__z) [8.1.3].
1641   //  Effects:  Behaves the same as C99 function casin, defined
1642   //            in subclause 7.3.5.2.
1643   template<typename _Tp>
1644     inline std::complex<_Tp>
1645     asin(const std::complex<_Tp>& __z)
1646     { return __complex_asin(__z); }
1647 #endif
1648   
1649   template<typename _Tp>
1650     std::complex<_Tp>
1651     __complex_atan(const std::complex<_Tp>& __z)
1652     {
1653       const _Tp __r2 = __z.real() * __z.real();
1654       const _Tp __x = _Tp(1.0) - __r2 - __z.imag() * __z.imag();
1656       _Tp __num = __z.imag() + _Tp(1.0);
1657       _Tp __den = __z.imag() - _Tp(1.0);
1659       __num = __r2 + __num * __num;
1660       __den = __r2 + __den * __den;
1662       return std::complex<_Tp>(_Tp(0.5) * atan2(_Tp(2.0) * __z.real(), __x),
1663                                _Tp(0.25) * log(__num / __den));
1664     }
1666 #if _GLIBCXX_USE_C99_COMPLEX_TR1
1667   inline __complex__ float
1668   __complex_atan(__complex__ float __z)
1669   { return __builtin_catanf(__z); }
1671   inline __complex__ double
1672   __complex_atan(__complex__ double __z)
1673   { return __builtin_catan(__z); }
1675   inline __complex__ long double
1676   __complex_atan(const __complex__ long double& __z)
1677   { return __builtin_catanl(__z); }
1679   template<typename _Tp>
1680     inline std::complex<_Tp>
1681     atan(const std::complex<_Tp>& __z)
1682     { return __complex_atan(__z.__rep()); }
1683 #else
1684   /// atan(__z) [8.1.4].
1685   //  Effects:  Behaves the same as C99 function catan, defined
1686   //            in subclause 7.3.5.3.
1687   template<typename _Tp>
1688     inline std::complex<_Tp>
1689     atan(const std::complex<_Tp>& __z)
1690     { return __complex_atan(__z); }
1691 #endif
1693   template<typename _Tp>
1694     std::complex<_Tp>
1695     __complex_acosh(const std::complex<_Tp>& __z)
1696     {
1697       // Kahan's formula.
1698       return _Tp(2.0) * std::log(std::sqrt(_Tp(0.5) * (__z + _Tp(1.0)))
1699                                  + std::sqrt(_Tp(0.5) * (__z - _Tp(1.0))));
1700     }
1702 #if _GLIBCXX_USE_C99_COMPLEX_TR1
1703   inline __complex__ float
1704   __complex_acosh(__complex__ float __z)
1705   { return __builtin_cacoshf(__z); }
1707   inline __complex__ double
1708   __complex_acosh(__complex__ double __z)
1709   { return __builtin_cacosh(__z); }
1711   inline __complex__ long double
1712   __complex_acosh(const __complex__ long double& __z)
1713   { return __builtin_cacoshl(__z); }
1715   template<typename _Tp>
1716     inline std::complex<_Tp>
1717     acosh(const std::complex<_Tp>& __z)
1718     { return __complex_acosh(__z.__rep()); }
1719 #else
1720   /// acosh(__z) [8.1.5].
1721   //  Effects:  Behaves the same as C99 function cacosh, defined
1722   //            in subclause 7.3.6.1.
1723   template<typename _Tp>
1724     inline std::complex<_Tp>
1725     acosh(const std::complex<_Tp>& __z)
1726     { return __complex_acosh(__z); }
1727 #endif
1729   template<typename _Tp>
1730     std::complex<_Tp>
1731     __complex_asinh(const std::complex<_Tp>& __z)
1732     {
1733       std::complex<_Tp> __t((__z.real() - __z.imag())
1734                             * (__z.real() + __z.imag()) + _Tp(1.0),
1735                             _Tp(2.0) * __z.real() * __z.imag());
1736       __t = std::sqrt(__t);
1738       return std::log(__t + __z);
1739     }
1741 #if _GLIBCXX_USE_C99_COMPLEX_TR1
1742   inline __complex__ float
1743   __complex_asinh(__complex__ float __z)
1744   { return __builtin_casinhf(__z); }
1746   inline __complex__ double
1747   __complex_asinh(__complex__ double __z)
1748   { return __builtin_casinh(__z); }
1750   inline __complex__ long double
1751   __complex_asinh(const __complex__ long double& __z)
1752   { return __builtin_casinhl(__z); }
1754   template<typename _Tp>
1755     inline std::complex<_Tp>
1756     asinh(const std::complex<_Tp>& __z)
1757     { return __complex_asinh(__z.__rep()); }
1758 #else
1759   /// asinh(__z) [8.1.6].
1760   //  Effects:  Behaves the same as C99 function casin, defined
1761   //            in subclause 7.3.6.2.
1762   template<typename _Tp>
1763     inline std::complex<_Tp>
1764     asinh(const std::complex<_Tp>& __z)
1765     { return __complex_asinh(__z); }
1766 #endif
1768   template<typename _Tp>
1769     std::complex<_Tp>
1770     __complex_atanh(const std::complex<_Tp>& __z)
1771     {
1772       const _Tp __i2 = __z.imag() * __z.imag();
1773       const _Tp __x = _Tp(1.0) - __i2 - __z.real() * __z.real();
1775       _Tp __num = _Tp(1.0) + __z.real();
1776       _Tp __den = _Tp(1.0) - __z.real();
1778       __num = __i2 + __num * __num;
1779       __den = __i2 + __den * __den;
1781       return std::complex<_Tp>(_Tp(0.25) * (log(__num) - log(__den)),
1782                                _Tp(0.5) * atan2(_Tp(2.0) * __z.imag(), __x));
1783     }
1785 #if _GLIBCXX_USE_C99_COMPLEX_TR1
1786   inline __complex__ float
1787   __complex_atanh(__complex__ float __z)
1788   { return __builtin_catanhf(__z); }
1790   inline __complex__ double
1791   __complex_atanh(__complex__ double __z)
1792   { return __builtin_catanh(__z); }
1794   inline __complex__ long double
1795   __complex_atanh(const __complex__ long double& __z)
1796   { return __builtin_catanhl(__z); }
1798   template<typename _Tp>
1799     inline std::complex<_Tp>
1800     atanh(const std::complex<_Tp>& __z)
1801     { return __complex_atanh(__z.__rep()); }
1802 #else
1803   /// atanh(__z) [8.1.7].
1804   //  Effects:  Behaves the same as C99 function catanh, defined
1805   //            in subclause 7.3.6.3.
1806   template<typename _Tp>
1807     inline std::complex<_Tp>
1808     atanh(const std::complex<_Tp>& __z)
1809     { return __complex_atanh(__z); }
1810 #endif
1812   template<typename _Tp>
1813     inline _Tp
1814     /// fabs(__z) [8.1.8].
1815     //  Effects:  Behaves the same as C99 function cabs, defined
1816     //            in subclause 7.3.8.1.
1817     fabs(const std::complex<_Tp>& __z)
1818     { return std::abs(__z); }
1820   /// Additional overloads [8.1.9].
1821   template<typename _Tp>
1822     inline typename __gnu_cxx::__promote<_Tp>::__type
1823     arg(_Tp __x)
1824     {
1825       typedef typename __gnu_cxx::__promote<_Tp>::__type __type;
1826 #if (_GLIBCXX_USE_C99_MATH && !_GLIBCXX_USE_C99_FP_MACROS_DYNAMIC)
1827       return std::signbit(__x) ? __type(3.1415926535897932384626433832795029L)
1828                                : __type();
1829 #else
1830       return std::arg(std::complex<__type>(__x));
1831 #endif
1832     }
1834   template<typename _Tp>
1835     inline typename __gnu_cxx::__promote<_Tp>::__type
1836     imag(_Tp)
1837     { return _Tp(); }
1839   template<typename _Tp>
1840     inline typename __gnu_cxx::__promote<_Tp>::__type
1841     norm(_Tp __x)
1842     {
1843       typedef typename __gnu_cxx::__promote<_Tp>::__type __type;
1844       return __type(__x) * __type(__x);
1845     }
1847   template<typename _Tp>
1848     inline typename __gnu_cxx::__promote<_Tp>::__type
1849     real(_Tp __x)
1850     { return __x; }
1852   template<typename _Tp, typename _Up>
1853     inline std::complex<typename __gnu_cxx::__promote_2<_Tp, _Up>::__type>
1854     pow(const std::complex<_Tp>& __x, const _Up& __y)
1855     {
1856       typedef typename __gnu_cxx::__promote_2<_Tp, _Up>::__type __type;
1857       return std::pow(std::complex<__type>(__x), __type(__y));
1858     }
1860   template<typename _Tp, typename _Up>
1861     inline std::complex<typename __gnu_cxx::__promote_2<_Tp, _Up>::__type>
1862     pow(const _Tp& __x, const std::complex<_Up>& __y)
1863     {
1864       typedef typename __gnu_cxx::__promote_2<_Tp, _Up>::__type __type;
1865       return std::pow(__type(__x), std::complex<__type>(__y));
1866     }
1868   template<typename _Tp, typename _Up>
1869     inline std::complex<typename __gnu_cxx::__promote_2<_Tp, _Up>::__type>
1870     pow(const std::complex<_Tp>& __x, const std::complex<_Up>& __y)
1871     {
1872       typedef typename __gnu_cxx::__promote_2<_Tp, _Up>::__type __type;
1873       return std::pow(std::complex<__type>(__x),
1874                       std::complex<__type>(__y));
1875     }
1877   // Forward declarations.
1878   // DR 781.
1879   template<typename _Tp> std::complex<_Tp> proj(const std::complex<_Tp>&);
1881   template<typename _Tp>
1882     std::complex<_Tp>
1883     __complex_proj(const std::complex<_Tp>& __z)
1884     {
1885       const _Tp __den = (__z.real() * __z.real()
1886                          + __z.imag() * __z.imag() + _Tp(1.0));
1888       return std::complex<_Tp>((_Tp(2.0) * __z.real()) / __den,
1889                                (_Tp(2.0) * __z.imag()) / __den);
1890     }
1892 #if _GLIBCXX_USE_C99_COMPLEX
1893   inline __complex__ float
1894   __complex_proj(__complex__ float __z)
1895   { return __builtin_cprojf(__z); }
1897   inline __complex__ double
1898   __complex_proj(__complex__ double __z)
1899   { return __builtin_cproj(__z); }
1901   inline __complex__ long double
1902   __complex_proj(const __complex__ long double& __z)
1903   { return __builtin_cprojl(__z); }
1905   template<typename _Tp>
1906     inline std::complex<_Tp>
1907     proj(const std::complex<_Tp>& __z)
1908     { return __complex_proj(__z.__rep()); }
1909 #else
1910   template<typename _Tp>
1911     inline std::complex<_Tp>
1912     proj(const std::complex<_Tp>& __z)
1913     { return __complex_proj(__z); }
1914 #endif
1916   // DR 1137.
1917   template<typename _Tp>
1918     inline typename __gnu_cxx::__promote<_Tp>::__type
1919     proj(_Tp __x)
1920     { return __x; }
1922   template<typename _Tp>
1923     inline typename __gnu_cxx::__promote<_Tp>::__type
1924     conj(_Tp __x)
1925     { return __x; }
1927 _GLIBCXX_END_NAMESPACE_VERSION
1928 } // namespace
1930 #endif  // C++11
1932 #endif  /* _GLIBCXX_COMPLEX */