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