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