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