libstdc++
complex
Go to the documentation of this file.
1 // The template and inlines for the -*- C++ -*- complex number classes.
2 
3 // Copyright (C) 1997-2022 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 
54 namespace 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  class complex
128  {
129  public:
130  /// Value typedef.
131  typedef _Tp value_type;
132 
133  /// Default constructor. First parameter is x, second parameter is y.
134  /// Unspecified parameters default to 0.
135  _GLIBCXX_CONSTEXPR complex(const _Tp& __r = _Tp(), const _Tp& __i = _Tp())
136  : _M_real(__r), _M_imag(__i) { }
137 
138  // Let the compiler synthesize the copy constructor
139 #if __cplusplus >= 201103L
140  constexpr complex(const complex&) = default;
141 #endif
142 
143  /// Converting constructor.
144  template<typename _Up>
145  _GLIBCXX_CONSTEXPR complex(const complex<_Up>& __z)
146  : _M_real(__z.real()), _M_imag(__z.imag()) { }
147 
148 #if __cplusplus >= 201103L
149  // _GLIBCXX_RESOLVE_LIB_DEFECTS
150  // DR 387. std::complex over-encapsulated.
151  _GLIBCXX_ABI_TAG_CXX11
152  constexpr _Tp
153  real() const { return _M_real; }
154 
155  _GLIBCXX_ABI_TAG_CXX11
156  constexpr _Tp
157  imag() const { return _M_imag; }
158 #else
159  /// Return real part of complex number.
160  _Tp&
161  real() { return _M_real; }
162 
163  /// Return real part of complex number.
164  const _Tp&
165  real() const { return _M_real; }
166 
167  /// Return imaginary part of complex number.
168  _Tp&
169  imag() { return _M_imag; }
170 
171  /// Return imaginary part of complex number.
172  const _Tp&
173  imag() const { return _M_imag; }
174 #endif
175 
176  // _GLIBCXX_RESOLVE_LIB_DEFECTS
177  // DR 387. std::complex over-encapsulated.
178  _GLIBCXX20_CONSTEXPR void
179  real(_Tp __val) { _M_real = __val; }
180 
181  _GLIBCXX20_CONSTEXPR void
182  imag(_Tp __val) { _M_imag = __val; }
183 
184  /// Assign a scalar to this complex number.
185  _GLIBCXX20_CONSTEXPR complex<_Tp>& operator=(const _Tp&);
186 
187  /// Add a scalar to this complex number.
188  // 26.2.5/1
189  _GLIBCXX20_CONSTEXPR complex<_Tp>&
190  operator+=(const _Tp& __t)
191  {
192  _M_real += __t;
193  return *this;
194  }
195 
196  /// Subtract a scalar from this complex number.
197  // 26.2.5/3
198  _GLIBCXX20_CONSTEXPR complex<_Tp>&
199  operator-=(const _Tp& __t)
200  {
201  _M_real -= __t;
202  return *this;
203  }
204 
205  /// Multiply this complex number by a scalar.
206  _GLIBCXX20_CONSTEXPR complex<_Tp>& operator*=(const _Tp&);
207  /// Divide this complex number by a scalar.
208  _GLIBCXX20_CONSTEXPR complex<_Tp>& operator/=(const _Tp&);
209 
210  // Let the compiler synthesize the copy assignment operator
211 #if __cplusplus >= 201103L
212  _GLIBCXX20_CONSTEXPR complex& operator=(const complex&) = default;
213 #endif
214 
215  /// Assign another complex number to this one.
216  template<typename _Up>
217  _GLIBCXX20_CONSTEXPR complex<_Tp>& operator=(const complex<_Up>&);
218  /// Add another complex number to this one.
219  template<typename _Up>
220  _GLIBCXX20_CONSTEXPR complex<_Tp>& operator+=(const complex<_Up>&);
221  /// Subtract another complex number from this one.
222  template<typename _Up>
223  _GLIBCXX20_CONSTEXPR complex<_Tp>& operator-=(const complex<_Up>&);
224  /// Multiply this complex number by another.
225  template<typename _Up>
226  _GLIBCXX20_CONSTEXPR complex<_Tp>& operator*=(const complex<_Up>&);
227  /// Divide this complex number by another.
228  template<typename _Up>
229  _GLIBCXX20_CONSTEXPR complex<_Tp>& operator/=(const complex<_Up>&);
230 
231  _GLIBCXX_CONSTEXPR complex __rep() const
232  { return *this; }
233 
234  private:
235  _Tp _M_real;
236  _Tp _M_imag;
237  };
238 
239  template<typename _Tp>
240  _GLIBCXX20_CONSTEXPR complex<_Tp>&
241  complex<_Tp>::operator=(const _Tp& __t)
242  {
243  _M_real = __t;
244  _M_imag = _Tp();
245  return *this;
246  }
247 
248  // 26.2.5/5
249  template<typename _Tp>
250  _GLIBCXX20_CONSTEXPR complex<_Tp>&
251  complex<_Tp>::operator*=(const _Tp& __t)
252  {
253  _M_real *= __t;
254  _M_imag *= __t;
255  return *this;
256  }
257 
258  // 26.2.5/7
259  template<typename _Tp>
260  _GLIBCXX20_CONSTEXPR complex<_Tp>&
261  complex<_Tp>::operator/=(const _Tp& __t)
262  {
263  _M_real /= __t;
264  _M_imag /= __t;
265  return *this;
266  }
267 
268  template<typename _Tp>
269  template<typename _Up>
270  _GLIBCXX20_CONSTEXPR complex<_Tp>&
271  complex<_Tp>::operator=(const complex<_Up>& __z)
272  {
273  _M_real = __z.real();
274  _M_imag = __z.imag();
275  return *this;
276  }
277 
278  // 26.2.5/9
279  template<typename _Tp>
280  template<typename _Up>
281  _GLIBCXX20_CONSTEXPR complex<_Tp>&
282  complex<_Tp>::operator+=(const complex<_Up>& __z)
283  {
284  _M_real += __z.real();
285  _M_imag += __z.imag();
286  return *this;
287  }
288 
289  // 26.2.5/11
290  template<typename _Tp>
291  template<typename _Up>
292  _GLIBCXX20_CONSTEXPR complex<_Tp>&
293  complex<_Tp>::operator-=(const complex<_Up>& __z)
294  {
295  _M_real -= __z.real();
296  _M_imag -= __z.imag();
297  return *this;
298  }
299 
300  // 26.2.5/13
301  // XXX: This is a grammar school implementation.
302  template<typename _Tp>
303  template<typename _Up>
304  _GLIBCXX20_CONSTEXPR complex<_Tp>&
305  complex<_Tp>::operator*=(const complex<_Up>& __z)
306  {
307  const _Tp __r = _M_real * __z.real() - _M_imag * __z.imag();
308  _M_imag = _M_real * __z.imag() + _M_imag * __z.real();
309  _M_real = __r;
310  return *this;
311  }
312 
313  // 26.2.5/15
314  // XXX: This is a grammar school implementation.
315  template<typename _Tp>
316  template<typename _Up>
317  _GLIBCXX20_CONSTEXPR complex<_Tp>&
318  complex<_Tp>::operator/=(const complex<_Up>& __z)
319  {
320  const _Tp __r = _M_real * __z.real() + _M_imag * __z.imag();
321  const _Tp __n = std::norm(__z);
322  _M_imag = (_M_imag * __z.real() - _M_real * __z.imag()) / __n;
323  _M_real = __r / __n;
324  return *this;
325  }
326 
327  // Operators:
328  ///@{
329  /// Return new complex value @a x plus @a y.
330  template<typename _Tp>
331  inline _GLIBCXX20_CONSTEXPR complex<_Tp>
332  operator+(const complex<_Tp>& __x, const complex<_Tp>& __y)
333  {
334  complex<_Tp> __r = __x;
335  __r += __y;
336  return __r;
337  }
338 
339  template<typename _Tp>
340  inline _GLIBCXX20_CONSTEXPR complex<_Tp>
341  operator+(const complex<_Tp>& __x, const _Tp& __y)
342  {
343  complex<_Tp> __r = __x;
344  __r += __y;
345  return __r;
346  }
347 
348  template<typename _Tp>
349  inline _GLIBCXX20_CONSTEXPR complex<_Tp>
350  operator+(const _Tp& __x, const complex<_Tp>& __y)
351  {
352  complex<_Tp> __r = __y;
353  __r += __x;
354  return __r;
355  }
356  ///@}
357 
358  ///@{
359  /// Return new complex value @a x minus @a y.
360  template<typename _Tp>
361  inline _GLIBCXX20_CONSTEXPR complex<_Tp>
362  operator-(const complex<_Tp>& __x, const complex<_Tp>& __y)
363  {
364  complex<_Tp> __r = __x;
365  __r -= __y;
366  return __r;
367  }
368 
369  template<typename _Tp>
370  inline _GLIBCXX20_CONSTEXPR complex<_Tp>
371  operator-(const complex<_Tp>& __x, const _Tp& __y)
372  {
373  complex<_Tp> __r = __x;
374  __r -= __y;
375  return __r;
376  }
377 
378  template<typename _Tp>
379  inline _GLIBCXX20_CONSTEXPR complex<_Tp>
380  operator-(const _Tp& __x, const complex<_Tp>& __y)
381  {
382  complex<_Tp> __r = -__y;
383  __r += __x;
384  return __r;
385  }
386  ///@}
387 
388  ///@{
389  /// Return new complex value @a x times @a y.
390  template<typename _Tp>
391  inline _GLIBCXX20_CONSTEXPR complex<_Tp>
392  operator*(const complex<_Tp>& __x, const complex<_Tp>& __y)
393  {
394  complex<_Tp> __r = __x;
395  __r *= __y;
396  return __r;
397  }
398 
399  template<typename _Tp>
400  inline _GLIBCXX20_CONSTEXPR complex<_Tp>
401  operator*(const complex<_Tp>& __x, const _Tp& __y)
402  {
403  complex<_Tp> __r = __x;
404  __r *= __y;
405  return __r;
406  }
407 
408  template<typename _Tp>
409  inline _GLIBCXX20_CONSTEXPR complex<_Tp>
410  operator*(const _Tp& __x, const complex<_Tp>& __y)
411  {
412  complex<_Tp> __r = __y;
413  __r *= __x;
414  return __r;
415  }
416  ///@}
417 
418  ///@{
419  /// Return new complex value @a x divided by @a y.
420  template<typename _Tp>
421  inline _GLIBCXX20_CONSTEXPR complex<_Tp>
422  operator/(const complex<_Tp>& __x, const complex<_Tp>& __y)
423  {
424  complex<_Tp> __r = __x;
425  __r /= __y;
426  return __r;
427  }
428 
429  template<typename _Tp>
430  inline _GLIBCXX20_CONSTEXPR complex<_Tp>
431  operator/(const complex<_Tp>& __x, const _Tp& __y)
432  {
433  complex<_Tp> __r = __x;
434  __r /= __y;
435  return __r;
436  }
437 
438  template<typename _Tp>
439  inline _GLIBCXX20_CONSTEXPR complex<_Tp>
440  operator/(const _Tp& __x, const complex<_Tp>& __y)
441  {
442  complex<_Tp> __r = __x;
443  __r /= __y;
444  return __r;
445  }
446  ///@}
447 
448  /// Return @a x.
449  template<typename _Tp>
450  inline _GLIBCXX20_CONSTEXPR complex<_Tp>
451  operator+(const complex<_Tp>& __x)
452  { return __x; }
453 
454  /// Return complex negation of @a x.
455  template<typename _Tp>
456  inline _GLIBCXX20_CONSTEXPR complex<_Tp>
457  operator-(const complex<_Tp>& __x)
458  { return complex<_Tp>(-__x.real(), -__x.imag()); }
459 
460  ///@{
461  /// Return true if @a x is equal to @a y.
462  template<typename _Tp>
463  inline _GLIBCXX_CONSTEXPR bool
464  operator==(const complex<_Tp>& __x, const complex<_Tp>& __y)
465  { return __x.real() == __y.real() && __x.imag() == __y.imag(); }
466 
467  template<typename _Tp>
468  inline _GLIBCXX_CONSTEXPR bool
469  operator==(const complex<_Tp>& __x, const _Tp& __y)
470  { return __x.real() == __y && __x.imag() == _Tp(); }
471 
472 #if !(__cpp_impl_three_way_comparison >= 201907L)
473  template<typename _Tp>
474  inline _GLIBCXX_CONSTEXPR bool
475  operator==(const _Tp& __x, const complex<_Tp>& __y)
476  { return __x == __y.real() && _Tp() == __y.imag(); }
477  ///@}
478 
479  ///@{
480  /// Return false if @a x is equal to @a y.
481  template<typename _Tp>
482  inline _GLIBCXX_CONSTEXPR bool
483  operator!=(const complex<_Tp>& __x, const complex<_Tp>& __y)
484  { return __x.real() != __y.real() || __x.imag() != __y.imag(); }
485 
486  template<typename _Tp>
487  inline _GLIBCXX_CONSTEXPR bool
488  operator!=(const complex<_Tp>& __x, const _Tp& __y)
489  { return __x.real() != __y || __x.imag() != _Tp(); }
490 
491  template<typename _Tp>
492  inline _GLIBCXX_CONSTEXPR bool
493  operator!=(const _Tp& __x, const complex<_Tp>& __y)
494  { return __x != __y.real() || _Tp() != __y.imag(); }
495 #endif
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  bool __fail = true;
504  _CharT __ch;
505  if (__is >> __ch)
506  {
507  if (_Traits::eq(__ch, __is.widen('(')))
508  {
509  _Tp __u;
510  if (__is >> __u >> __ch)
511  {
512  const _CharT __rparen = __is.widen(')');
513  if (_Traits::eq(__ch, __rparen))
514  {
515  __x = __u;
516  __fail = false;
517  }
518  else if (_Traits::eq(__ch, __is.widen(',')))
519  {
520  _Tp __v;
521  if (__is >> __v >> __ch)
522  {
523  if (_Traits::eq(__ch, __rparen))
524  {
525  __x = complex<_Tp>(__u, __v);
526  __fail = false;
527  }
528  else
529  __is.putback(__ch);
530  }
531  }
532  else
533  __is.putback(__ch);
534  }
535  }
536  else
537  {
538  __is.putback(__ch);
539  _Tp __u;
540  if (__is >> __u)
541  {
542  __x = __u;
543  __fail = false;
544  }
545  }
546  }
547  if (__fail)
548  __is.setstate(ios_base::failbit);
549  return __is;
550  }
551 
552  /// Insertion operator for complex values.
553  template<typename _Tp, typename _CharT, class _Traits>
554  basic_ostream<_CharT, _Traits>&
555  operator<<(basic_ostream<_CharT, _Traits>& __os, const complex<_Tp>& __x)
556  {
557  basic_ostringstream<_CharT, _Traits> __s;
558  __s.flags(__os.flags());
559  __s.imbue(__os.getloc());
560  __s.precision(__os.precision());
561  __s << '(' << __x.real() << ',' << __x.imag() << ')';
562  return __os << __s.str();
563  }
564 
565  // Values
566 #if __cplusplus >= 201103L
567  template<typename _Tp>
568  constexpr _Tp
569  real(const complex<_Tp>& __z)
570  { return __z.real(); }
571 
572  template<typename _Tp>
573  constexpr _Tp
574  imag(const complex<_Tp>& __z)
575  { return __z.imag(); }
576 #else
577  template<typename _Tp>
578  inline _Tp&
579  real(complex<_Tp>& __z)
580  { return __z.real(); }
581 
582  template<typename _Tp>
583  inline const _Tp&
584  real(const complex<_Tp>& __z)
585  { return __z.real(); }
586 
587  template<typename _Tp>
588  inline _Tp&
589  imag(complex<_Tp>& __z)
590  { return __z.imag(); }
591 
592  template<typename _Tp>
593  inline const _Tp&
594  imag(const complex<_Tp>& __z)
595  { return __z.imag(); }
596 #endif
597 
598  // 26.2.7/3 abs(__z): Returns the magnitude of __z.
599  template<typename _Tp>
600  inline _Tp
601  __complex_abs(const complex<_Tp>& __z)
602  {
603  _Tp __x = __z.real();
604  _Tp __y = __z.imag();
605  const _Tp __s = std::max(abs(__x), abs(__y));
606  if (__s == _Tp()) // well ...
607  return __s;
608  __x /= __s;
609  __y /= __s;
610  return __s * sqrt(__x * __x + __y * __y);
611  }
612 
613 #if _GLIBCXX_USE_C99_COMPLEX
614  inline float
615  __complex_abs(__complex__ float __z) { return __builtin_cabsf(__z); }
616 
617  inline double
618  __complex_abs(__complex__ double __z) { return __builtin_cabs(__z); }
619 
620  inline long double
621  __complex_abs(const __complex__ long double& __z)
622  { return __builtin_cabsl(__z); }
623 
624  template<typename _Tp>
625  inline _Tp
626  abs(const complex<_Tp>& __z) { return __complex_abs(__z.__rep()); }
627 #else
628  template<typename _Tp>
629  inline _Tp
630  abs(const complex<_Tp>& __z) { return __complex_abs(__z); }
631 #endif
632 
633 
634  // 26.2.7/4: arg(__z): Returns the phase angle of __z.
635  template<typename _Tp>
636  inline _Tp
637  __complex_arg(const complex<_Tp>& __z)
638  { return atan2(__z.imag(), __z.real()); }
639 
640 #if _GLIBCXX_USE_C99_COMPLEX
641  inline float
642  __complex_arg(__complex__ float __z) { return __builtin_cargf(__z); }
643 
644  inline double
645  __complex_arg(__complex__ double __z) { return __builtin_carg(__z); }
646 
647  inline long double
648  __complex_arg(const __complex__ long double& __z)
649  { return __builtin_cargl(__z); }
650 
651  template<typename _Tp>
652  inline _Tp
653  arg(const complex<_Tp>& __z) { return __complex_arg(__z.__rep()); }
654 #else
655  template<typename _Tp>
656  inline _Tp
657  arg(const complex<_Tp>& __z) { return __complex_arg(__z); }
658 #endif
659 
660  // 26.2.7/5: norm(__z) returns the squared magnitude of __z.
661  // As defined, norm() is -not- a norm is the common mathematical
662  // sense used in numerics. The helper class _Norm_helper<> tries to
663  // distinguish between builtin floating point and the rest, so as
664  // to deliver an answer as close as possible to the real value.
665  template<bool>
666  struct _Norm_helper
667  {
668  template<typename _Tp>
669  static inline _GLIBCXX20_CONSTEXPR _Tp _S_do_it(const complex<_Tp>& __z)
670  {
671  const _Tp __x = __z.real();
672  const _Tp __y = __z.imag();
673  return __x * __x + __y * __y;
674  }
675  };
676 
677  template<>
678  struct _Norm_helper<true>
679  {
680  template<typename _Tp>
681  static inline _GLIBCXX20_CONSTEXPR _Tp _S_do_it(const complex<_Tp>& __z)
682  {
683  //_Tp __res = std::abs(__z);
684  //return __res * __res;
685  const _Tp __x = __z.real();
686  const _Tp __y = __z.imag();
687  return __x * __x + __y * __y;
688  }
689  };
690 
691  template<typename _Tp>
692  inline _GLIBCXX20_CONSTEXPR _Tp
693  norm(const complex<_Tp>& __z)
694  {
695  return _Norm_helper<__is_floating<_Tp>::__value
696  && !_GLIBCXX_FAST_MATH>::_S_do_it(__z);
697  }
698 
699  template<typename _Tp>
700  inline complex<_Tp>
701  polar(const _Tp& __rho, const _Tp& __theta)
702  {
703  __glibcxx_assert( __rho >= 0 );
704  return complex<_Tp>(__rho * cos(__theta), __rho * sin(__theta));
705  }
706 
707  template<typename _Tp>
708  inline _GLIBCXX20_CONSTEXPR complex<_Tp>
709  conj(const complex<_Tp>& __z)
710  { return complex<_Tp>(__z.real(), -__z.imag()); }
711 
712  // Transcendentals
713 
714  // 26.2.8/1 cos(__z): Returns the cosine of __z.
715  template<typename _Tp>
716  inline complex<_Tp>
717  __complex_cos(const complex<_Tp>& __z)
718  {
719  const _Tp __x = __z.real();
720  const _Tp __y = __z.imag();
721  return complex<_Tp>(cos(__x) * cosh(__y), -sin(__x) * sinh(__y));
722  }
723 
724 #if _GLIBCXX_USE_C99_COMPLEX
725  inline __complex__ float
726  __complex_cos(__complex__ float __z) { return __builtin_ccosf(__z); }
727 
728  inline __complex__ double
729  __complex_cos(__complex__ double __z) { return __builtin_ccos(__z); }
730 
731  inline __complex__ long double
732  __complex_cos(const __complex__ long double& __z)
733  { return __builtin_ccosl(__z); }
734 
735  template<typename _Tp>
736  inline complex<_Tp>
737  cos(const complex<_Tp>& __z) { return __complex_cos(__z.__rep()); }
738 #else
739  template<typename _Tp>
740  inline complex<_Tp>
741  cos(const complex<_Tp>& __z) { return __complex_cos(__z); }
742 #endif
743 
744  // 26.2.8/2 cosh(__z): Returns the hyperbolic cosine of __z.
745  template<typename _Tp>
746  inline complex<_Tp>
747  __complex_cosh(const complex<_Tp>& __z)
748  {
749  const _Tp __x = __z.real();
750  const _Tp __y = __z.imag();
751  return complex<_Tp>(cosh(__x) * cos(__y), sinh(__x) * sin(__y));
752  }
753 
754 #if _GLIBCXX_USE_C99_COMPLEX
755  inline __complex__ float
756  __complex_cosh(__complex__ float __z) { return __builtin_ccoshf(__z); }
757 
758  inline __complex__ double
759  __complex_cosh(__complex__ double __z) { return __builtin_ccosh(__z); }
760 
761  inline __complex__ long double
762  __complex_cosh(const __complex__ long double& __z)
763  { return __builtin_ccoshl(__z); }
764 
765  template<typename _Tp>
766  inline complex<_Tp>
767  cosh(const complex<_Tp>& __z) { return __complex_cosh(__z.__rep()); }
768 #else
769  template<typename _Tp>
770  inline complex<_Tp>
771  cosh(const complex<_Tp>& __z) { return __complex_cosh(__z); }
772 #endif
773 
774  // 26.2.8/3 exp(__z): Returns the complex base e exponential of x
775  template<typename _Tp>
776  inline complex<_Tp>
777  __complex_exp(const complex<_Tp>& __z)
778  { return std::polar<_Tp>(exp(__z.real()), __z.imag()); }
779 
780 #if _GLIBCXX_USE_C99_COMPLEX
781  inline __complex__ float
782  __complex_exp(__complex__ float __z) { return __builtin_cexpf(__z); }
783 
784  inline __complex__ double
785  __complex_exp(__complex__ double __z) { return __builtin_cexp(__z); }
786 
787  inline __complex__ long double
788  __complex_exp(const __complex__ long double& __z)
789  { return __builtin_cexpl(__z); }
790 
791  template<typename _Tp>
792  inline complex<_Tp>
793  exp(const complex<_Tp>& __z) { return __complex_exp(__z.__rep()); }
794 #else
795  template<typename _Tp>
796  inline complex<_Tp>
797  exp(const complex<_Tp>& __z) { return __complex_exp(__z); }
798 #endif
799 
800  // 26.2.8/5 log(__z): Returns the natural complex logarithm of __z.
801  // The branch cut is along the negative axis.
802  template<typename _Tp>
803  inline complex<_Tp>
804  __complex_log(const complex<_Tp>& __z)
805  { return complex<_Tp>(log(std::abs(__z)), std::arg(__z)); }
806 
807 #if _GLIBCXX_USE_C99_COMPLEX
808  inline __complex__ float
809  __complex_log(__complex__ float __z) { return __builtin_clogf(__z); }
810 
811  inline __complex__ double
812  __complex_log(__complex__ double __z) { return __builtin_clog(__z); }
813 
814  inline __complex__ long double
815  __complex_log(const __complex__ long double& __z)
816  { return __builtin_clogl(__z); }
817 
818  template<typename _Tp>
819  inline complex<_Tp>
820  log(const complex<_Tp>& __z) { return __complex_log(__z.__rep()); }
821 #else
822  template<typename _Tp>
823  inline complex<_Tp>
824  log(const complex<_Tp>& __z) { return __complex_log(__z); }
825 #endif
826 
827  template<typename _Tp>
828  inline complex<_Tp>
829  log10(const complex<_Tp>& __z)
830  { return std::log(__z) / log(_Tp(10.0)); }
831 
832  // 26.2.8/10 sin(__z): Returns the sine of __z.
833  template<typename _Tp>
834  inline complex<_Tp>
835  __complex_sin(const complex<_Tp>& __z)
836  {
837  const _Tp __x = __z.real();
838  const _Tp __y = __z.imag();
839  return complex<_Tp>(sin(__x) * cosh(__y), cos(__x) * sinh(__y));
840  }
841 
842 #if _GLIBCXX_USE_C99_COMPLEX
843  inline __complex__ float
844  __complex_sin(__complex__ float __z) { return __builtin_csinf(__z); }
845 
846  inline __complex__ double
847  __complex_sin(__complex__ double __z) { return __builtin_csin(__z); }
848 
849  inline __complex__ long double
850  __complex_sin(const __complex__ long double& __z)
851  { return __builtin_csinl(__z); }
852 
853  template<typename _Tp>
854  inline complex<_Tp>
855  sin(const complex<_Tp>& __z) { return __complex_sin(__z.__rep()); }
856 #else
857  template<typename _Tp>
858  inline complex<_Tp>
859  sin(const complex<_Tp>& __z) { return __complex_sin(__z); }
860 #endif
861 
862  // 26.2.8/11 sinh(__z): Returns the hyperbolic sine of __z.
863  template<typename _Tp>
864  inline complex<_Tp>
865  __complex_sinh(const complex<_Tp>& __z)
866  {
867  const _Tp __x = __z.real();
868  const _Tp __y = __z.imag();
869  return complex<_Tp>(sinh(__x) * cos(__y), cosh(__x) * sin(__y));
870  }
871 
872 #if _GLIBCXX_USE_C99_COMPLEX
873  inline __complex__ float
874  __complex_sinh(__complex__ float __z) { return __builtin_csinhf(__z); }
875 
876  inline __complex__ double
877  __complex_sinh(__complex__ double __z) { return __builtin_csinh(__z); }
878 
879  inline __complex__ long double
880  __complex_sinh(const __complex__ long double& __z)
881  { return __builtin_csinhl(__z); }
882 
883  template<typename _Tp>
884  inline complex<_Tp>
885  sinh(const complex<_Tp>& __z) { return __complex_sinh(__z.__rep()); }
886 #else
887  template<typename _Tp>
888  inline complex<_Tp>
889  sinh(const complex<_Tp>& __z) { return __complex_sinh(__z); }
890 #endif
891 
892  // 26.2.8/13 sqrt(__z): Returns the complex square root of __z.
893  // The branch cut is on the negative axis.
894  template<typename _Tp>
895  complex<_Tp>
896  __complex_sqrt(const complex<_Tp>& __z)
897  {
898  _Tp __x = __z.real();
899  _Tp __y = __z.imag();
900 
901  if (__x == _Tp())
902  {
903  _Tp __t = sqrt(abs(__y) / 2);
904  return complex<_Tp>(__t, __y < _Tp() ? -__t : __t);
905  }
906  else
907  {
908  _Tp __t = sqrt(2 * (std::abs(__z) + abs(__x)));
909  _Tp __u = __t / 2;
910  return __x > _Tp()
911  ? complex<_Tp>(__u, __y / __t)
912  : complex<_Tp>(abs(__y) / __t, __y < _Tp() ? -__u : __u);
913  }
914  }
915 
916 #if _GLIBCXX_USE_C99_COMPLEX
917  inline __complex__ float
918  __complex_sqrt(__complex__ float __z) { return __builtin_csqrtf(__z); }
919 
920  inline __complex__ double
921  __complex_sqrt(__complex__ double __z) { return __builtin_csqrt(__z); }
922 
923  inline __complex__ long double
924  __complex_sqrt(const __complex__ long double& __z)
925  { return __builtin_csqrtl(__z); }
926 
927  template<typename _Tp>
928  inline complex<_Tp>
929  sqrt(const complex<_Tp>& __z) { return __complex_sqrt(__z.__rep()); }
930 #else
931  template<typename _Tp>
932  inline complex<_Tp>
933  sqrt(const complex<_Tp>& __z) { return __complex_sqrt(__z); }
934 #endif
935 
936  // 26.2.8/14 tan(__z): Return the complex tangent of __z.
937 
938  template<typename _Tp>
939  inline complex<_Tp>
940  __complex_tan(const complex<_Tp>& __z)
941  { return std::sin(__z) / std::cos(__z); }
942 
943 #if _GLIBCXX_USE_C99_COMPLEX
944  inline __complex__ float
945  __complex_tan(__complex__ float __z) { return __builtin_ctanf(__z); }
946 
947  inline __complex__ double
948  __complex_tan(__complex__ double __z) { return __builtin_ctan(__z); }
949 
950  inline __complex__ long double
951  __complex_tan(const __complex__ long double& __z)
952  { return __builtin_ctanl(__z); }
953 
954  template<typename _Tp>
955  inline complex<_Tp>
956  tan(const complex<_Tp>& __z) { return __complex_tan(__z.__rep()); }
957 #else
958  template<typename _Tp>
959  inline complex<_Tp>
960  tan(const complex<_Tp>& __z) { return __complex_tan(__z); }
961 #endif
962 
963 
964  // 26.2.8/15 tanh(__z): Returns the hyperbolic tangent of __z.
965 
966  template<typename _Tp>
967  inline complex<_Tp>
968  __complex_tanh(const complex<_Tp>& __z)
969  { return std::sinh(__z) / std::cosh(__z); }
970 
971 #if _GLIBCXX_USE_C99_COMPLEX
972  inline __complex__ float
973  __complex_tanh(__complex__ float __z) { return __builtin_ctanhf(__z); }
974 
975  inline __complex__ double
976  __complex_tanh(__complex__ double __z) { return __builtin_ctanh(__z); }
977 
978  inline __complex__ long double
979  __complex_tanh(const __complex__ long double& __z)
980  { return __builtin_ctanhl(__z); }
981 
982  template<typename _Tp>
983  inline complex<_Tp>
984  tanh(const complex<_Tp>& __z) { return __complex_tanh(__z.__rep()); }
985 #else
986  template<typename _Tp>
987  inline complex<_Tp>
988  tanh(const complex<_Tp>& __z) { return __complex_tanh(__z); }
989 #endif
990 
991 
992  // 26.2.8/9 pow(__x, __y): Returns the complex power base of __x
993  // raised to the __y-th power. The branch
994  // cut is on the negative axis.
995  template<typename _Tp>
996  complex<_Tp>
997  __complex_pow_unsigned(complex<_Tp> __x, unsigned __n)
998  {
999  complex<_Tp> __y = __n % 2 ? __x : complex<_Tp>(1);
1000 
1001  while (__n >>= 1)
1002  {
1003  __x *= __x;
1004  if (__n % 2)
1005  __y *= __x;
1006  }
1007 
1008  return __y;
1009  }
1010 
1011  // In C++11 mode we used to implement the resolution of
1012  // DR 844. complex pow return type is ambiguous.
1013  // thus the following overload was disabled in that mode. However, doing
1014  // that causes all sorts of issues, see, for example:
1015  // http://gcc.gnu.org/ml/libstdc++/2013-01/msg00058.html
1016  // and also PR57974.
1017  template<typename _Tp>
1018  inline complex<_Tp>
1019  pow(const complex<_Tp>& __z, int __n)
1020  {
1021  return __n < 0
1022  ? complex<_Tp>(1) / std::__complex_pow_unsigned(__z, -(unsigned)__n)
1023  : std::__complex_pow_unsigned(__z, __n);
1024  }
1025 
1026  template<typename _Tp>
1027  complex<_Tp>
1028  pow(const complex<_Tp>& __x, const _Tp& __y)
1029  {
1030 #if ! _GLIBCXX_USE_C99_COMPLEX
1031  if (__x == _Tp())
1032  return _Tp();
1033 #endif
1034  if (__x.imag() == _Tp() && __x.real() > _Tp())
1035  return pow(__x.real(), __y);
1036 
1037  complex<_Tp> __t = std::log(__x);
1038  return std::polar<_Tp>(exp(__y * __t.real()), __y * __t.imag());
1039  }
1040 
1041  template<typename _Tp>
1042  inline complex<_Tp>
1043  __complex_pow(const complex<_Tp>& __x, const complex<_Tp>& __y)
1044  { return __x == _Tp() ? _Tp() : std::exp(__y * std::log(__x)); }
1045 
1046 #if _GLIBCXX_USE_C99_COMPLEX
1047  inline __complex__ float
1048  __complex_pow(__complex__ float __x, __complex__ float __y)
1049  { return __builtin_cpowf(__x, __y); }
1050 
1051  inline __complex__ double
1052  __complex_pow(__complex__ double __x, __complex__ double __y)
1053  { return __builtin_cpow(__x, __y); }
1054 
1055  inline __complex__ long double
1056  __complex_pow(const __complex__ long double& __x,
1057  const __complex__ long double& __y)
1058  { return __builtin_cpowl(__x, __y); }
1059 
1060  template<typename _Tp>
1061  inline complex<_Tp>
1062  pow(const complex<_Tp>& __x, const complex<_Tp>& __y)
1063  { return __complex_pow(__x.__rep(), __y.__rep()); }
1064 #else
1065  template<typename _Tp>
1066  inline complex<_Tp>
1067  pow(const complex<_Tp>& __x, const complex<_Tp>& __y)
1068  { return __complex_pow(__x, __y); }
1069 #endif
1070 
1071  template<typename _Tp>
1072  inline complex<_Tp>
1073  pow(const _Tp& __x, const complex<_Tp>& __y)
1074  {
1075  return __x > _Tp() ? std::polar<_Tp>(pow(__x, __y.real()),
1076  __y.imag() * log(__x))
1077  : std::pow(complex<_Tp>(__x), __y);
1078  }
1079 
1080  /// 26.2.3 complex specializations
1081  /// complex<float> specialization
1082  template<>
1083  class complex<float>
1084  {
1085  public:
1086  typedef float value_type;
1087  typedef __complex__ float _ComplexT;
1088 
1089  _GLIBCXX_CONSTEXPR complex(_ComplexT __z) : _M_value(__z) { }
1090 
1091  _GLIBCXX_CONSTEXPR complex(float __r = 0.0f, float __i = 0.0f)
1092 #if __cplusplus >= 201103L
1093  : _M_value{ __r, __i } { }
1094 #else
1095  {
1096  __real__ _M_value = __r;
1097  __imag__ _M_value = __i;
1098  }
1099 #endif
1100 
1101 #if __cplusplus >= 201103L
1102  _GLIBCXX14_CONSTEXPR complex(const complex&) = default;
1103 #endif
1104 
1105  explicit _GLIBCXX_CONSTEXPR complex(const complex<double>&);
1106  explicit _GLIBCXX_CONSTEXPR complex(const complex<long double>&);
1107 
1108 #if __cplusplus >= 201103L
1109  // _GLIBCXX_RESOLVE_LIB_DEFECTS
1110  // DR 387. std::complex over-encapsulated.
1111  __attribute ((__abi_tag__ ("cxx11")))
1112  constexpr float
1113  real() const { return __real__ _M_value; }
1114 
1115  __attribute ((__abi_tag__ ("cxx11")))
1116  constexpr float
1117  imag() const { return __imag__ _M_value; }
1118 #else
1119  float&
1120  real() { return __real__ _M_value; }
1121 
1122  const float&
1123  real() const { return __real__ _M_value; }
1124 
1125  float&
1126  imag() { return __imag__ _M_value; }
1127 
1128  const float&
1129  imag() const { return __imag__ _M_value; }
1130 #endif
1131 
1132  // _GLIBCXX_RESOLVE_LIB_DEFECTS
1133  // DR 387. std::complex over-encapsulated.
1134  _GLIBCXX20_CONSTEXPR void
1135  real(float __val) { __real__ _M_value = __val; }
1136 
1137  _GLIBCXX20_CONSTEXPR void
1138  imag(float __val) { __imag__ _M_value = __val; }
1139 
1140  _GLIBCXX20_CONSTEXPR complex&
1141  operator=(float __f)
1142  {
1143  _M_value = __f;
1144  return *this;
1145  }
1146 
1147  _GLIBCXX20_CONSTEXPR complex&
1148  operator+=(float __f)
1149  {
1150  _M_value += __f;
1151  return *this;
1152  }
1153 
1154  _GLIBCXX20_CONSTEXPR complex&
1155  operator-=(float __f)
1156  {
1157  _M_value -= __f;
1158  return *this;
1159  }
1160 
1161  _GLIBCXX20_CONSTEXPR complex&
1162  operator*=(float __f)
1163  {
1164  _M_value *= __f;
1165  return *this;
1166  }
1167 
1168  _GLIBCXX20_CONSTEXPR complex&
1169  operator/=(float __f)
1170  {
1171  _M_value /= __f;
1172  return *this;
1173  }
1174 
1175  // Let the compiler synthesize the copy and assignment
1176  // operator. It always does a pretty good job.
1177 #if __cplusplus >= 201103L
1178  _GLIBCXX14_CONSTEXPR complex& operator=(const complex&) = default;
1179 #endif
1180 
1181  template<typename _Tp>
1182  _GLIBCXX20_CONSTEXPR complex&
1183  operator=(const complex<_Tp>& __z)
1184  {
1185  __real__ _M_value = __z.real();
1186  __imag__ _M_value = __z.imag();
1187  return *this;
1188  }
1189 
1190  template<typename _Tp>
1191  _GLIBCXX20_CONSTEXPR complex&
1192  operator+=(const complex<_Tp>& __z)
1193  {
1194  _M_value += __z.__rep();
1195  return *this;
1196  }
1197 
1198  template<class _Tp>
1199  _GLIBCXX20_CONSTEXPR complex&
1200  operator-=(const complex<_Tp>& __z)
1201  {
1202  _M_value -= __z.__rep();
1203  return *this;
1204  }
1205 
1206  template<class _Tp>
1207  _GLIBCXX20_CONSTEXPR complex&
1208  operator*=(const complex<_Tp>& __z)
1209  {
1210  const _ComplexT __t = __z.__rep();
1211  _M_value *= __t;
1212  return *this;
1213  }
1214 
1215  template<class _Tp>
1216  _GLIBCXX20_CONSTEXPR complex&
1217  operator/=(const complex<_Tp>& __z)
1218  {
1219  const _ComplexT __t = __z.__rep();
1220  _M_value /= __t;
1221  return *this;
1222  }
1223 
1224  _GLIBCXX_CONSTEXPR _ComplexT __rep() const { return _M_value; }
1225 
1226  private:
1227  _ComplexT _M_value;
1228  };
1229 
1230  /// 26.2.3 complex specializations
1231  /// complex<double> specialization
1232  template<>
1233  class complex<double>
1234  {
1235  public:
1236  typedef double value_type;
1237  typedef __complex__ double _ComplexT;
1238 
1239  _GLIBCXX_CONSTEXPR complex(_ComplexT __z) : _M_value(__z) { }
1240 
1241  _GLIBCXX_CONSTEXPR complex(double __r = 0.0, double __i = 0.0)
1242 #if __cplusplus >= 201103L
1243  : _M_value{ __r, __i } { }
1244 #else
1245  {
1246  __real__ _M_value = __r;
1247  __imag__ _M_value = __i;
1248  }
1249 #endif
1250 
1251 #if __cplusplus >= 201103L
1252  _GLIBCXX14_CONSTEXPR complex(const complex&) = default;
1253 #endif
1254 
1255  _GLIBCXX_CONSTEXPR complex(const complex<float>& __z)
1256  : _M_value(__z.__rep()) { }
1257 
1258  explicit _GLIBCXX_CONSTEXPR complex(const complex<long double>&);
1259 
1260 #if __cplusplus >= 201103L
1261  // _GLIBCXX_RESOLVE_LIB_DEFECTS
1262  // DR 387. std::complex over-encapsulated.
1263  __attribute ((__abi_tag__ ("cxx11")))
1264  constexpr double
1265  real() const { return __real__ _M_value; }
1266 
1267  __attribute ((__abi_tag__ ("cxx11")))
1268  constexpr double
1269  imag() const { return __imag__ _M_value; }
1270 #else
1271  double&
1272  real() { return __real__ _M_value; }
1273 
1274  const double&
1275  real() const { return __real__ _M_value; }
1276 
1277  double&
1278  imag() { return __imag__ _M_value; }
1279 
1280  const double&
1281  imag() const { return __imag__ _M_value; }
1282 #endif
1283 
1284  // _GLIBCXX_RESOLVE_LIB_DEFECTS
1285  // DR 387. std::complex over-encapsulated.
1286  _GLIBCXX20_CONSTEXPR void
1287  real(double __val) { __real__ _M_value = __val; }
1288 
1289  _GLIBCXX20_CONSTEXPR void
1290  imag(double __val) { __imag__ _M_value = __val; }
1291 
1292  _GLIBCXX20_CONSTEXPR complex&
1293  operator=(double __d)
1294  {
1295  _M_value = __d;
1296  return *this;
1297  }
1298 
1299  _GLIBCXX20_CONSTEXPR complex&
1300  operator+=(double __d)
1301  {
1302  _M_value += __d;
1303  return *this;
1304  }
1305 
1306  _GLIBCXX20_CONSTEXPR complex&
1307  operator-=(double __d)
1308  {
1309  _M_value -= __d;
1310  return *this;
1311  }
1312 
1313  _GLIBCXX20_CONSTEXPR complex&
1314  operator*=(double __d)
1315  {
1316  _M_value *= __d;
1317  return *this;
1318  }
1319 
1320  _GLIBCXX20_CONSTEXPR complex&
1321  operator/=(double __d)
1322  {
1323  _M_value /= __d;
1324  return *this;
1325  }
1326 
1327  // The compiler will synthesize this, efficiently.
1328 #if __cplusplus >= 201103L
1329  _GLIBCXX14_CONSTEXPR complex& operator=(const complex&) = default;
1330 #endif
1331 
1332  template<typename _Tp>
1333  _GLIBCXX20_CONSTEXPR complex&
1334  operator=(const complex<_Tp>& __z)
1335  {
1336  _M_value = __z.__rep();
1337  return *this;
1338  }
1339 
1340  template<typename _Tp>
1341  _GLIBCXX20_CONSTEXPR complex&
1342  operator+=(const complex<_Tp>& __z)
1343  {
1344  _M_value += __z.__rep();
1345  return *this;
1346  }
1347 
1348  template<typename _Tp>
1349  _GLIBCXX20_CONSTEXPR complex&
1350  operator-=(const complex<_Tp>& __z)
1351  {
1352  _M_value -= __z.__rep();
1353  return *this;
1354  }
1355 
1356  template<typename _Tp>
1357  _GLIBCXX20_CONSTEXPR complex&
1358  operator*=(const complex<_Tp>& __z)
1359  {
1360  const _ComplexT __t = __z.__rep();
1361  _M_value *= __t;
1362  return *this;
1363  }
1364 
1365  template<typename _Tp>
1366  _GLIBCXX20_CONSTEXPR complex&
1367  operator/=(const complex<_Tp>& __z)
1368  {
1369  const _ComplexT __t = __z.__rep();
1370  _M_value /= __t;
1371  return *this;
1372  }
1373 
1374  _GLIBCXX_CONSTEXPR _ComplexT __rep() const { return _M_value; }
1375 
1376  private:
1377  _ComplexT _M_value;
1378  };
1379 
1380  /// 26.2.3 complex specializations
1381  /// complex<long double> specialization
1382  template<>
1383  class complex<long double>
1384  {
1385  public:
1386  typedef long double value_type;
1387  typedef __complex__ long double _ComplexT;
1388 
1389  _GLIBCXX_CONSTEXPR complex(_ComplexT __z) : _M_value(__z) { }
1390 
1391  _GLIBCXX_CONSTEXPR complex(long double __r = 0.0L,
1392  long double __i = 0.0L)
1393 #if __cplusplus >= 201103L
1394  : _M_value{ __r, __i } { }
1395 #else
1396  {
1397  __real__ _M_value = __r;
1398  __imag__ _M_value = __i;
1399  }
1400 #endif
1401 
1402 #if __cplusplus >= 201103L
1403  _GLIBCXX14_CONSTEXPR complex(const complex&) = default;
1404 #endif
1405 
1406  _GLIBCXX_CONSTEXPR complex(const complex<float>& __z)
1407  : _M_value(__z.__rep()) { }
1408 
1409  _GLIBCXX_CONSTEXPR complex(const complex<double>& __z)
1410  : _M_value(__z.__rep()) { }
1411 
1412 #if __cplusplus >= 201103L
1413  // _GLIBCXX_RESOLVE_LIB_DEFECTS
1414  // DR 387. std::complex over-encapsulated.
1415  __attribute ((__abi_tag__ ("cxx11")))
1416  constexpr long double
1417  real() const { return __real__ _M_value; }
1418 
1419  __attribute ((__abi_tag__ ("cxx11")))
1420  constexpr long double
1421  imag() const { return __imag__ _M_value; }
1422 #else
1423  long double&
1424  real() { return __real__ _M_value; }
1425 
1426  const long double&
1427  real() const { return __real__ _M_value; }
1428 
1429  long double&
1430  imag() { return __imag__ _M_value; }
1431 
1432  const long double&
1433  imag() const { return __imag__ _M_value; }
1434 #endif
1435 
1436  // _GLIBCXX_RESOLVE_LIB_DEFECTS
1437  // DR 387. std::complex over-encapsulated.
1438  _GLIBCXX20_CONSTEXPR void
1439  real(long double __val) { __real__ _M_value = __val; }
1440 
1441  _GLIBCXX20_CONSTEXPR void
1442  imag(long double __val) { __imag__ _M_value = __val; }
1443 
1444  _GLIBCXX20_CONSTEXPR complex&
1445  operator=(long double __r)
1446  {
1447  _M_value = __r;
1448  return *this;
1449  }
1450 
1451  _GLIBCXX20_CONSTEXPR complex&
1452  operator+=(long double __r)
1453  {
1454  _M_value += __r;
1455  return *this;
1456  }
1457 
1458  _GLIBCXX20_CONSTEXPR complex&
1459  operator-=(long double __r)
1460  {
1461  _M_value -= __r;
1462  return *this;
1463  }
1464 
1465  _GLIBCXX20_CONSTEXPR complex&
1466  operator*=(long double __r)
1467  {
1468  _M_value *= __r;
1469  return *this;
1470  }
1471 
1472  _GLIBCXX20_CONSTEXPR complex&
1473  operator/=(long double __r)
1474  {
1475  _M_value /= __r;
1476  return *this;
1477  }
1478 
1479  // The compiler knows how to do this efficiently
1480 #if __cplusplus >= 201103L
1481  _GLIBCXX14_CONSTEXPR complex& operator=(const complex&) = default;
1482 #endif
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  _M_value += __z.__rep();
1497  return *this;
1498  }
1499 
1500  template<typename _Tp>
1501  _GLIBCXX20_CONSTEXPR complex&
1502  operator-=(const complex<_Tp>& __z)
1503  {
1504  _M_value -= __z.__rep();
1505  return *this;
1506  }
1507 
1508  template<typename _Tp>
1509  _GLIBCXX20_CONSTEXPR complex&
1510  operator*=(const complex<_Tp>& __z)
1511  {
1512  const _ComplexT __t = __z.__rep();
1513  _M_value *= __t;
1514  return *this;
1515  }
1516 
1517  template<typename _Tp>
1518  _GLIBCXX20_CONSTEXPR complex&
1519  operator/=(const complex<_Tp>& __z)
1520  {
1521  const _ComplexT __t = __z.__rep();
1522  _M_value /= __t;
1523  return *this;
1524  }
1525 
1526  _GLIBCXX_CONSTEXPR _ComplexT __rep() const { return _M_value; }
1527 
1528  private:
1529  _ComplexT _M_value;
1530  };
1531 
1532  // These bits have to be at the end of this file, so that the
1533  // specializations have all been defined.
1534  inline _GLIBCXX_CONSTEXPR
1535  complex<float>::complex(const complex<double>& __z)
1536  : _M_value(__z.__rep()) { }
1537 
1538  inline _GLIBCXX_CONSTEXPR
1539  complex<float>::complex(const complex<long double>& __z)
1540  : _M_value(__z.__rep()) { }
1541 
1542  inline _GLIBCXX_CONSTEXPR
1543  complex<double>::complex(const complex<long double>& __z)
1544  : _M_value(__z.__rep()) { }
1545 
1546  // Inhibit implicit instantiations for required instantiations,
1547  // which are defined via explicit instantiations elsewhere.
1548  // NB: This syntax is a GNU extension.
1549 #if _GLIBCXX_EXTERN_TEMPLATE
1550  extern template istream& operator>>(istream&, complex<float>&);
1551  extern template ostream& operator<<(ostream&, const complex<float>&);
1552  extern template istream& operator>>(istream&, complex<double>&);
1553  extern template ostream& operator<<(ostream&, const complex<double>&);
1554  extern template istream& operator>>(istream&, complex<long double>&);
1555  extern template ostream& operator<<(ostream&, const complex<long double>&);
1556 
1557 #ifdef _GLIBCXX_USE_WCHAR_T
1558  extern template wistream& operator>>(wistream&, complex<float>&);
1559  extern template wostream& operator<<(wostream&, const complex<float>&);
1560  extern template wistream& operator>>(wistream&, complex<double>&);
1561  extern template wostream& operator<<(wostream&, const complex<double>&);
1562  extern template wistream& operator>>(wistream&, complex<long double>&);
1563  extern template wostream& operator<<(wostream&, const complex<long double>&);
1564 #endif
1565 #endif
1566 
1567  /// @} group complex_numbers
1568 
1569 _GLIBCXX_END_NAMESPACE_VERSION
1570 } // namespace
1571 
1572 #if __cplusplus >= 201103L
1573 
1574 namespace 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 (_GLIBCXX11_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  _GLIBCXX_CONSTEXPR inline typename __gnu_cxx::__promote<_Tp>::__type
1848  imag(_Tp)
1849  { return _Tp(); }
1850 
1851  template<typename _Tp>
1852  _GLIBCXX20_CONSTEXPR 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  _GLIBCXX_CONSTEXPR 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>
1892  std::complex<_Tp> proj(const std::complex<_Tp>&);
1893 
1894  // Generic implementation of std::proj, does not work for infinities.
1895  template<typename _Tp>
1896  inline std::complex<_Tp>
1897  __complex_proj(const std::complex<_Tp>& __z)
1898  { return __z; }
1899 
1900 #if _GLIBCXX_USE_C99_COMPLEX
1901  inline complex<float>
1902  __complex_proj(const complex<float>& __z)
1903  { return __builtin_cprojf(__z.__rep()); }
1904 
1905  inline complex<double>
1906  __complex_proj(const complex<double>& __z)
1907  { return __builtin_cproj(__z.__rep()); }
1908 
1909  inline complex<long double>
1910  __complex_proj(const complex<long double>& __z)
1911  { return __builtin_cprojl(__z.__rep()); }
1912 #elif defined _GLIBCXX_USE_C99_MATH_TR1
1913  inline complex<float>
1914  __complex_proj(const complex<float>& __z)
1915  {
1916  if (__builtin_isinf(__z.real()) || __builtin_isinf(__z.imag()))
1917  return complex<float>(__builtin_inff(),
1918  __builtin_copysignf(0.0f, __z.imag()));
1919  return __z;
1920  }
1921 
1922  inline complex<double>
1923  __complex_proj(const complex<double>& __z)
1924  {
1925  if (__builtin_isinf(__z.real()) || __builtin_isinf(__z.imag()))
1926  return complex<double>(__builtin_inf(),
1927  __builtin_copysign(0.0, __z.imag()));
1928  return __z;
1929  }
1930 
1931  inline complex<long double>
1932  __complex_proj(const complex<long double>& __z)
1933  {
1934  if (__builtin_isinf(__z.real()) || __builtin_isinf(__z.imag()))
1935  return complex<long double>(__builtin_infl(),
1936  __builtin_copysignl(0.0l, __z.imag()));
1937  return __z;
1938  }
1939 #endif
1940 
1941  template<typename _Tp>
1942  inline std::complex<_Tp>
1943  proj(const std::complex<_Tp>& __z)
1944  { return __complex_proj(__z); }
1945 
1946  // Overload for scalars
1947  template<typename _Tp>
1948  inline std::complex<typename __gnu_cxx::__promote<_Tp>::__type>
1949  proj(_Tp __x)
1950  {
1951  typedef typename __gnu_cxx::__promote<_Tp>::__type __type;
1952  return std::proj(std::complex<__type>(__x));
1953  }
1954 
1955  template<typename _Tp>
1956  inline _GLIBCXX20_CONSTEXPR
1957  std::complex<typename __gnu_cxx::__promote<_Tp>::__type>
1958  conj(_Tp __x)
1959  {
1960  typedef typename __gnu_cxx::__promote<_Tp>::__type __type;
1961  return std::complex<__type>(__x, -__type());
1962  }
1963 
1964 #if __cplusplus > 201103L
1965 
1966 inline namespace literals {
1967 inline namespace complex_literals {
1968 #pragma GCC diagnostic push
1969 #pragma GCC diagnostic ignored "-Wliteral-suffix"
1970 #define __cpp_lib_complex_udls 201309L
1971 
1972  constexpr std::complex<float>
1973  operator""if(long double __num)
1974  { return std::complex<float>{0.0F, static_cast<float>(__num)}; }
1975 
1976  constexpr std::complex<float>
1977  operator""if(unsigned long long __num)
1978  { return std::complex<float>{0.0F, static_cast<float>(__num)}; }
1979 
1980  constexpr std::complex<double>
1981  operator""i(long double __num)
1982  { return std::complex<double>{0.0, static_cast<double>(__num)}; }
1983 
1984  constexpr std::complex<double>
1985  operator""i(unsigned long long __num)
1986  { return std::complex<double>{0.0, static_cast<double>(__num)}; }
1987 
1988  constexpr std::complex<long double>
1989  operator""il(long double __num)
1990  { return std::complex<long double>{0.0L, __num}; }
1991 
1992  constexpr std::complex<long double>
1993  operator""il(unsigned long long __num)
1994  { return std::complex<long double>{0.0L, static_cast<long double>(__num)}; }
1995 
1996 #pragma GCC diagnostic pop
1997 } // inline namespace complex_literals
1998 } // inline namespace literals
1999 
2000 #endif // C++14
2001 
2002 _GLIBCXX_END_NAMESPACE_VERSION
2003 } // namespace
2004 
2005 #endif // C++11
2006 
2007 #endif /* _GLIBCXX_COMPLEX */