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-2023 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 #if __cplusplus > 202002L
146  explicit(!requires(_Up __u) { _Tp{__u}; })
147 #endif
148  _GLIBCXX_CONSTEXPR complex(const complex<_Up>& __z)
149  : _M_real(_Tp(__z.real())), _M_imag(_Tp(__z.imag())) { }
150 
151 #if __cplusplus >= 201103L
152  // _GLIBCXX_RESOLVE_LIB_DEFECTS
153  // DR 387. std::complex over-encapsulated.
154  _GLIBCXX_ABI_TAG_CXX11
155  constexpr _Tp
156  real() const { return _M_real; }
157 
158  _GLIBCXX_ABI_TAG_CXX11
159  constexpr _Tp
160  imag() const { return _M_imag; }
161 #else
162  /// Return real part of complex number.
163  _Tp&
164  real() { return _M_real; }
165 
166  /// Return real part of complex number.
167  const _Tp&
168  real() const { return _M_real; }
169 
170  /// Return imaginary part of complex number.
171  _Tp&
172  imag() { return _M_imag; }
173 
174  /// Return imaginary part of complex number.
175  const _Tp&
176  imag() const { return _M_imag; }
177 #endif
178 
179  // _GLIBCXX_RESOLVE_LIB_DEFECTS
180  // DR 387. std::complex over-encapsulated.
181  _GLIBCXX20_CONSTEXPR void
182  real(_Tp __val) { _M_real = __val; }
183 
184  _GLIBCXX20_CONSTEXPR void
185  imag(_Tp __val) { _M_imag = __val; }
186 
187  /// Assign a scalar to this complex number.
188  _GLIBCXX20_CONSTEXPR complex<_Tp>& operator=(const _Tp&);
189 
190  /// Add a scalar to this complex number.
191  // 26.2.5/1
192  _GLIBCXX20_CONSTEXPR complex<_Tp>&
193  operator+=(const _Tp& __t)
194  {
195  _M_real += __t;
196  return *this;
197  }
198 
199  /// Subtract a scalar from this complex number.
200  // 26.2.5/3
201  _GLIBCXX20_CONSTEXPR complex<_Tp>&
202  operator-=(const _Tp& __t)
203  {
204  _M_real -= __t;
205  return *this;
206  }
207 
208  /// Multiply this complex number by a scalar.
209  _GLIBCXX20_CONSTEXPR complex<_Tp>& operator*=(const _Tp&);
210  /// Divide this complex number by a scalar.
211  _GLIBCXX20_CONSTEXPR complex<_Tp>& operator/=(const _Tp&);
212 
213  // Let the compiler synthesize the copy assignment operator
214 #if __cplusplus >= 201103L
215  _GLIBCXX20_CONSTEXPR complex& operator=(const complex&) = default;
216 #endif
217 
218  /// Assign another complex number to this one.
219  template<typename _Up>
220  _GLIBCXX20_CONSTEXPR complex<_Tp>& operator=(const complex<_Up>&);
221  /// Add another complex number to this one.
222  template<typename _Up>
223  _GLIBCXX20_CONSTEXPR complex<_Tp>& operator+=(const complex<_Up>&);
224  /// Subtract another complex number from this one.
225  template<typename _Up>
226  _GLIBCXX20_CONSTEXPR complex<_Tp>& operator-=(const complex<_Up>&);
227  /// Multiply this complex number by another.
228  template<typename _Up>
229  _GLIBCXX20_CONSTEXPR complex<_Tp>& operator*=(const complex<_Up>&);
230  /// Divide this complex number by another.
231  template<typename _Up>
232  _GLIBCXX20_CONSTEXPR complex<_Tp>& operator/=(const complex<_Up>&);
233 
234  _GLIBCXX_CONSTEXPR complex __rep() const
235  { return *this; }
236 
237  private:
238  _Tp _M_real;
239  _Tp _M_imag;
240  };
241 
242  template<typename _Tp>
243  _GLIBCXX20_CONSTEXPR complex<_Tp>&
244  complex<_Tp>::operator=(const _Tp& __t)
245  {
246  _M_real = __t;
247  _M_imag = _Tp();
248  return *this;
249  }
250 
251  // 26.2.5/5
252  template<typename _Tp>
253  _GLIBCXX20_CONSTEXPR complex<_Tp>&
254  complex<_Tp>::operator*=(const _Tp& __t)
255  {
256  _M_real *= __t;
257  _M_imag *= __t;
258  return *this;
259  }
260 
261  // 26.2.5/7
262  template<typename _Tp>
263  _GLIBCXX20_CONSTEXPR complex<_Tp>&
264  complex<_Tp>::operator/=(const _Tp& __t)
265  {
266  _M_real /= __t;
267  _M_imag /= __t;
268  return *this;
269  }
270 
271  template<typename _Tp>
272  template<typename _Up>
273  _GLIBCXX20_CONSTEXPR complex<_Tp>&
274  complex<_Tp>::operator=(const complex<_Up>& __z)
275  {
276  _M_real = __z.real();
277  _M_imag = __z.imag();
278  return *this;
279  }
280 
281  // 26.2.5/9
282  template<typename _Tp>
283  template<typename _Up>
284  _GLIBCXX20_CONSTEXPR complex<_Tp>&
285  complex<_Tp>::operator+=(const complex<_Up>& __z)
286  {
287  _M_real += __z.real();
288  _M_imag += __z.imag();
289  return *this;
290  }
291 
292  // 26.2.5/11
293  template<typename _Tp>
294  template<typename _Up>
295  _GLIBCXX20_CONSTEXPR complex<_Tp>&
296  complex<_Tp>::operator-=(const complex<_Up>& __z)
297  {
298  _M_real -= __z.real();
299  _M_imag -= __z.imag();
300  return *this;
301  }
302 
303  // 26.2.5/13
304  // XXX: This is a grammar school implementation.
305  template<typename _Tp>
306  template<typename _Up>
307  _GLIBCXX20_CONSTEXPR complex<_Tp>&
308  complex<_Tp>::operator*=(const complex<_Up>& __z)
309  {
310  const _Tp __r = _M_real * __z.real() - _M_imag * __z.imag();
311  _M_imag = _M_real * __z.imag() + _M_imag * __z.real();
312  _M_real = __r;
313  return *this;
314  }
315 
316  // 26.2.5/15
317  // XXX: This is a grammar school implementation.
318  template<typename _Tp>
319  template<typename _Up>
320  _GLIBCXX20_CONSTEXPR complex<_Tp>&
321  complex<_Tp>::operator/=(const complex<_Up>& __z)
322  {
323  const _Tp __r = _M_real * __z.real() + _M_imag * __z.imag();
324  const _Tp __n = std::norm(__z);
325  _M_imag = (_M_imag * __z.real() - _M_real * __z.imag()) / __n;
326  _M_real = __r / __n;
327  return *this;
328  }
329 
330  // Operators:
331  ///@{
332  /// Return new complex value @a x plus @a y.
333  template<typename _Tp>
334  inline _GLIBCXX20_CONSTEXPR complex<_Tp>
335  operator+(const complex<_Tp>& __x, const complex<_Tp>& __y)
336  {
337  complex<_Tp> __r = __x;
338  __r += __y;
339  return __r;
340  }
341 
342  template<typename _Tp>
343  inline _GLIBCXX20_CONSTEXPR complex<_Tp>
344  operator+(const complex<_Tp>& __x, const _Tp& __y)
345  {
346  complex<_Tp> __r = __x;
347  __r += __y;
348  return __r;
349  }
350 
351  template<typename _Tp>
352  inline _GLIBCXX20_CONSTEXPR complex<_Tp>
353  operator+(const _Tp& __x, const complex<_Tp>& __y)
354  {
355  complex<_Tp> __r = __y;
356  __r += __x;
357  return __r;
358  }
359  ///@}
360 
361  ///@{
362  /// Return new complex value @a x minus @a y.
363  template<typename _Tp>
364  inline _GLIBCXX20_CONSTEXPR complex<_Tp>
365  operator-(const complex<_Tp>& __x, const complex<_Tp>& __y)
366  {
367  complex<_Tp> __r = __x;
368  __r -= __y;
369  return __r;
370  }
371 
372  template<typename _Tp>
373  inline _GLIBCXX20_CONSTEXPR complex<_Tp>
374  operator-(const complex<_Tp>& __x, const _Tp& __y)
375  {
376  complex<_Tp> __r = __x;
377  __r -= __y;
378  return __r;
379  }
380 
381  template<typename _Tp>
382  inline _GLIBCXX20_CONSTEXPR complex<_Tp>
383  operator-(const _Tp& __x, const complex<_Tp>& __y)
384  {
385  complex<_Tp> __r = -__y;
386  __r += __x;
387  return __r;
388  }
389  ///@}
390 
391  ///@{
392  /// Return new complex value @a x times @a y.
393  template<typename _Tp>
394  inline _GLIBCXX20_CONSTEXPR complex<_Tp>
395  operator*(const complex<_Tp>& __x, const complex<_Tp>& __y)
396  {
397  complex<_Tp> __r = __x;
398  __r *= __y;
399  return __r;
400  }
401 
402  template<typename _Tp>
403  inline _GLIBCXX20_CONSTEXPR complex<_Tp>
404  operator*(const complex<_Tp>& __x, const _Tp& __y)
405  {
406  complex<_Tp> __r = __x;
407  __r *= __y;
408  return __r;
409  }
410 
411  template<typename _Tp>
412  inline _GLIBCXX20_CONSTEXPR complex<_Tp>
413  operator*(const _Tp& __x, const complex<_Tp>& __y)
414  {
415  complex<_Tp> __r = __y;
416  __r *= __x;
417  return __r;
418  }
419  ///@}
420 
421  ///@{
422  /// Return new complex value @a x divided by @a y.
423  template<typename _Tp>
424  inline _GLIBCXX20_CONSTEXPR complex<_Tp>
425  operator/(const complex<_Tp>& __x, const complex<_Tp>& __y)
426  {
427  complex<_Tp> __r = __x;
428  __r /= __y;
429  return __r;
430  }
431 
432  template<typename _Tp>
433  inline _GLIBCXX20_CONSTEXPR complex<_Tp>
434  operator/(const complex<_Tp>& __x, const _Tp& __y)
435  {
436  complex<_Tp> __r = __x;
437  __r /= __y;
438  return __r;
439  }
440 
441  template<typename _Tp>
442  inline _GLIBCXX20_CONSTEXPR complex<_Tp>
443  operator/(const _Tp& __x, const complex<_Tp>& __y)
444  {
445  complex<_Tp> __r = __x;
446  __r /= __y;
447  return __r;
448  }
449  ///@}
450 
451  /// Return @a x.
452  template<typename _Tp>
453  inline _GLIBCXX20_CONSTEXPR complex<_Tp>
454  operator+(const complex<_Tp>& __x)
455  { return __x; }
456 
457  /// Return complex negation of @a x.
458  template<typename _Tp>
459  inline _GLIBCXX20_CONSTEXPR complex<_Tp>
460  operator-(const complex<_Tp>& __x)
461  { return complex<_Tp>(-__x.real(), -__x.imag()); }
462 
463  ///@{
464  /// Return true if @a x is equal to @a y.
465  template<typename _Tp>
466  inline _GLIBCXX_CONSTEXPR bool
467  operator==(const complex<_Tp>& __x, const complex<_Tp>& __y)
468  { return __x.real() == __y.real() && __x.imag() == __y.imag(); }
469 
470  template<typename _Tp>
471  inline _GLIBCXX_CONSTEXPR bool
472  operator==(const complex<_Tp>& __x, const _Tp& __y)
473  { return __x.real() == __y && __x.imag() == _Tp(); }
474 
475 #if !(__cpp_impl_three_way_comparison >= 201907L)
476  template<typename _Tp>
477  inline _GLIBCXX_CONSTEXPR bool
478  operator==(const _Tp& __x, const complex<_Tp>& __y)
479  { return __x == __y.real() && _Tp() == __y.imag(); }
480  ///@}
481 
482  ///@{
483  /// Return false if @a x is equal to @a y.
484  template<typename _Tp>
485  inline _GLIBCXX_CONSTEXPR bool
486  operator!=(const complex<_Tp>& __x, const complex<_Tp>& __y)
487  { return __x.real() != __y.real() || __x.imag() != __y.imag(); }
488 
489  template<typename _Tp>
490  inline _GLIBCXX_CONSTEXPR bool
491  operator!=(const complex<_Tp>& __x, const _Tp& __y)
492  { return __x.real() != __y || __x.imag() != _Tp(); }
493 
494  template<typename _Tp>
495  inline _GLIBCXX_CONSTEXPR bool
496  operator!=(const _Tp& __x, const complex<_Tp>& __y)
497  { return __x != __y.real() || _Tp() != __y.imag(); }
498 #endif
499  ///@}
500 
501  /// Extraction operator for complex values.
502  template<typename _Tp, typename _CharT, class _Traits>
503  basic_istream<_CharT, _Traits>&
504  operator>>(basic_istream<_CharT, _Traits>& __is, complex<_Tp>& __x)
505  {
506  bool __fail = true;
507  _CharT __ch;
508  if (__is >> __ch)
509  {
510  if (_Traits::eq(__ch, __is.widen('(')))
511  {
512  _Tp __u;
513  if (__is >> __u >> __ch)
514  {
515  const _CharT __rparen = __is.widen(')');
516  if (_Traits::eq(__ch, __rparen))
517  {
518  __x = __u;
519  __fail = false;
520  }
521  else if (_Traits::eq(__ch, __is.widen(',')))
522  {
523  _Tp __v;
524  if (__is >> __v >> __ch)
525  {
526  if (_Traits::eq(__ch, __rparen))
527  {
528  __x = complex<_Tp>(__u, __v);
529  __fail = false;
530  }
531  else
532  __is.putback(__ch);
533  }
534  }
535  else
536  __is.putback(__ch);
537  }
538  }
539  else
540  {
541  __is.putback(__ch);
542  _Tp __u;
543  if (__is >> __u)
544  {
545  __x = __u;
546  __fail = false;
547  }
548  }
549  }
550  if (__fail)
551  __is.setstate(ios_base::failbit);
552  return __is;
553  }
554 
555  /// Insertion operator for complex values.
556  template<typename _Tp, typename _CharT, class _Traits>
557  basic_ostream<_CharT, _Traits>&
558  operator<<(basic_ostream<_CharT, _Traits>& __os, const complex<_Tp>& __x)
559  {
560  basic_ostringstream<_CharT, _Traits> __s;
561  __s.flags(__os.flags());
562  __s.imbue(__os.getloc());
563  __s.precision(__os.precision());
564  __s << '(' << __x.real() << ',' << __x.imag() << ')';
565  return __os << __s.str();
566  }
567 
568  // Values
569 #if __cplusplus >= 201103L
570  template<typename _Tp>
571  constexpr _Tp
572  real(const complex<_Tp>& __z)
573  { return __z.real(); }
574 
575  template<typename _Tp>
576  constexpr _Tp
577  imag(const complex<_Tp>& __z)
578  { return __z.imag(); }
579 #else
580  template<typename _Tp>
581  inline _Tp&
582  real(complex<_Tp>& __z)
583  { return __z.real(); }
584 
585  template<typename _Tp>
586  inline const _Tp&
587  real(const complex<_Tp>& __z)
588  { return __z.real(); }
589 
590  template<typename _Tp>
591  inline _Tp&
592  imag(complex<_Tp>& __z)
593  { return __z.imag(); }
594 
595  template<typename _Tp>
596  inline const _Tp&
597  imag(const complex<_Tp>& __z)
598  { return __z.imag(); }
599 #endif
600 
601 #if _GLIBCXX_USE_C99_COMPLEX
602 #if defined(__STDCPP_FLOAT16_T__) && defined(_GLIBCXX_FLOAT_IS_IEEE_BINARY32)
603  inline _Float16
604  __complex_abs(__complex__ _Float16 __z)
605  { return _Float16(__builtin_cabsf(__z)); }
606 
607  inline _Float16
608  __complex_arg(__complex__ _Float16 __z)
609  { return _Float16(__builtin_cargf(__z)); }
610 
611  inline __complex__ _Float16
612  __complex_cos(__complex__ _Float16 __z)
613  { return static_cast<__complex__ _Float16>(__builtin_ccosf(__z)); }
614 
615  inline __complex__ _Float16
616  __complex_cosh(__complex__ _Float16 __z)
617  { return static_cast<__complex__ _Float16>(__builtin_ccoshf(__z)); }
618 
619  inline __complex__ _Float16
620  __complex_exp(__complex__ _Float16 __z)
621  { return static_cast<__complex__ _Float16>(__builtin_cexpf(__z)); }
622 
623  inline __complex__ _Float16
624  __complex_log(__complex__ _Float16 __z)
625  { return static_cast<__complex__ _Float16>(__builtin_clogf(__z)); }
626 
627  inline __complex__ _Float16
628  __complex_sin(__complex__ _Float16 __z)
629  { return static_cast<__complex__ _Float16>(__builtin_csinf(__z)); }
630 
631  inline __complex__ _Float16
632  __complex_sinh(__complex__ _Float16 __z)
633  { return static_cast<__complex__ _Float16>(__builtin_csinhf(__z)); }
634 
635  inline __complex__ _Float16
636  __complex_sqrt(__complex__ _Float16 __z)
637  { return static_cast<__complex__ _Float16>(__builtin_csqrtf(__z)); }
638 
639  inline __complex__ _Float16
640  __complex_tan(__complex__ _Float16 __z)
641  { return static_cast<__complex__ _Float16>(__builtin_ctanf(__z)); }
642 
643  inline __complex__ _Float16
644  __complex_tanh(__complex__ _Float16 __z)
645  { return static_cast<__complex__ _Float16>(__builtin_ctanhf(__z)); }
646 
647  inline __complex__ _Float16
648  __complex_pow(__complex__ _Float16 __x, __complex__ _Float16 __y)
649  { return static_cast<__complex__ _Float16>(__builtin_cpowf(__x, __y)); }
650 #endif
651 
652 #if defined(__STDCPP_FLOAT32_T__) && defined(_GLIBCXX_FLOAT_IS_IEEE_BINARY32)
653  inline _Float32
654  __complex_abs(__complex__ _Float32 __z) { return __builtin_cabsf(__z); }
655 
656  inline _Float32
657  __complex_arg(__complex__ _Float32 __z) { return __builtin_cargf(__z); }
658 
659  inline __complex__ _Float32
660  __complex_cos(__complex__ _Float32 __z) { return __builtin_ccosf(__z); }
661 
662  inline __complex__ _Float32
663  __complex_cosh(__complex__ _Float32 __z) { return __builtin_ccoshf(__z); }
664 
665  inline __complex__ _Float32
666  __complex_exp(__complex__ _Float32 __z) { return __builtin_cexpf(__z); }
667 
668  inline __complex__ _Float32
669  __complex_log(__complex__ _Float32 __z) { return __builtin_clogf(__z); }
670 
671  inline __complex__ _Float32
672  __complex_sin(__complex__ _Float32 __z) { return __builtin_csinf(__z); }
673 
674  inline __complex__ _Float32
675  __complex_sinh(__complex__ _Float32 __z) { return __builtin_csinhf(__z); }
676 
677  inline __complex__ _Float32
678  __complex_sqrt(__complex__ _Float32 __z) { return __builtin_csqrtf(__z); }
679 
680  inline __complex__ _Float32
681  __complex_tan(__complex__ _Float32 __z) { return __builtin_ctanf(__z); }
682 
683  inline __complex__ _Float32
684  __complex_tanh(__complex__ _Float32 __z) { return __builtin_ctanhf(__z); }
685 
686  inline __complex__ _Float32
687  __complex_pow(__complex__ _Float32 __x, __complex__ _Float32 __y)
688  { return __builtin_cpowf(__x, __y); }
689 #endif
690 
691 #if defined(__STDCPP_FLOAT64_T__) && defined(_GLIBCXX_DOUBLE_IS_IEEE_BINARY64)
692  inline _Float64
693  __complex_abs(__complex__ _Float64 __z) { return __builtin_cabs(__z); }
694 
695  inline _Float64
696  __complex_arg(__complex__ _Float64 __z) { return __builtin_carg(__z); }
697 
698  inline __complex__ _Float64
699  __complex_cos(__complex__ _Float64 __z) { return __builtin_ccos(__z); }
700 
701  inline __complex__ _Float64
702  __complex_cosh(__complex__ _Float64 __z) { return __builtin_ccosh(__z); }
703 
704  inline __complex__ _Float64
705  __complex_exp(__complex__ _Float64 __z) { return __builtin_cexp(__z); }
706 
707  inline __complex__ _Float64
708  __complex_log(__complex__ _Float64 __z) { return __builtin_clog(__z); }
709 
710  inline __complex__ _Float64
711  __complex_sin(__complex__ _Float64 __z) { return __builtin_csin(__z); }
712 
713  inline __complex__ _Float64
714  __complex_sinh(__complex__ _Float64 __z) { return __builtin_csinh(__z); }
715 
716  inline __complex__ _Float64
717  __complex_sqrt(__complex__ _Float64 __z) { return __builtin_csqrt(__z); }
718 
719  inline __complex__ _Float64
720  __complex_tan(__complex__ _Float64 __z) { return __builtin_ctan(__z); }
721 
722  inline __complex__ _Float64
723  __complex_tanh(__complex__ _Float64 __z) { return __builtin_ctanh(__z); }
724 
725  inline __complex__ _Float64
726  __complex_pow(__complex__ _Float64 __x, __complex__ _Float64 __y)
727  { return __builtin_cpow(__x, __y); }
728 #endif
729 
730 #if defined(__STDCPP_FLOAT128_T__) && defined(_GLIBCXX_LDOUBLE_IS_IEEE_BINARY128)
731  inline _Float128
732  __complex_abs(__complex__ _Float128 __z) { return __builtin_cabsl(__z); }
733 
734  inline _Float128
735  __complex_arg(__complex__ _Float128 __z) { return __builtin_cargl(__z); }
736 
737  inline __complex__ _Float128
738  __complex_cos(__complex__ _Float128 __z) { return __builtin_ccosl(__z); }
739 
740  inline __complex__ _Float128
741  __complex_cosh(__complex__ _Float128 __z) { return __builtin_ccoshl(__z); }
742 
743  inline __complex__ _Float128
744  __complex_exp(__complex__ _Float128 __z) { return __builtin_cexpl(__z); }
745 
746  inline __complex__ _Float128
747  __complex_log(__complex__ _Float128 __z) { return __builtin_clogl(__z); }
748 
749  inline __complex__ _Float128
750  __complex_sin(__complex__ _Float128 __z) { return __builtin_csinl(__z); }
751 
752  inline __complex__ _Float128
753  __complex_sinh(__complex__ _Float128 __z) { return __builtin_csinhl(__z); }
754 
755  inline __complex__ _Float128
756  __complex_sqrt(__complex__ _Float128 __z) { return __builtin_csqrtl(__z); }
757 
758  inline __complex__ _Float128
759  __complex_tan(__complex__ _Float128 __z) { return __builtin_ctanl(__z); }
760 
761  inline __complex__ _Float128
762  __complex_tanh(__complex__ _Float128 __z) { return __builtin_ctanhl(__z); }
763 
764  inline __complex__ _Float128
765  __complex_pow(__complex__ _Float128 __x, __complex__ _Float128 __y)
766  { return __builtin_cpowl(__x, __y); }
767 #elif defined(__STDCPP_FLOAT128_T__) && defined(_GLIBCXX_HAVE_FLOAT128_MATH)
768  inline _Float128
769  __complex_abs(__complex__ _Float128 __z) { return __builtin_cabsf128(__z); }
770 
771  inline _Float128
772  __complex_arg(__complex__ _Float128 __z) { return __builtin_cargf128(__z); }
773 
774  inline __complex__ _Float128
775  __complex_cos(__complex__ _Float128 __z) { return __builtin_ccosf128(__z); }
776 
777  inline __complex__ _Float128
778  __complex_cosh(__complex__ _Float128 __z) { return __builtin_ccoshf128(__z); }
779 
780  inline __complex__ _Float128
781  __complex_exp(__complex__ _Float128 __z) { return __builtin_cexpf128(__z); }
782 
783  inline __complex__ _Float128
784  __complex_log(__complex__ _Float128 __z) { return __builtin_clogf128(__z); }
785 
786  inline __complex__ _Float128
787  __complex_sin(__complex__ _Float128 __z) { return __builtin_csinf128(__z); }
788 
789  inline __complex__ _Float128
790  __complex_sinh(__complex__ _Float128 __z) { return __builtin_csinhf128(__z); }
791 
792  inline __complex__ _Float128
793  __complex_sqrt(__complex__ _Float128 __z) { return __builtin_csqrtf128(__z); }
794 
795  inline __complex__ _Float128
796  __complex_tan(__complex__ _Float128 __z) { return __builtin_ctanf128(__z); }
797 
798  inline __complex__ _Float128
799  __complex_tanh(__complex__ _Float128 __z) { return __builtin_ctanhf128(__z); }
800 
801  inline __complex__ _Float128
802  __complex_pow(__complex__ _Float128 __x, __complex__ _Float128 __y)
803  { return __builtin_cpowf128(__x, __y); }
804 #endif
805 
806 #if defined(__STDCPP_BFLOAT16_T__) && defined(_GLIBCXX_FLOAT_IS_IEEE_BINARY32)
807  inline __gnu_cxx::__bfloat16_t
808  __complex_abs(__complex__ decltype(0.0bf16) __z)
809  { return __gnu_cxx::__bfloat16_t(__builtin_cabsf(__z)); }
810 
811  inline __gnu_cxx::__bfloat16_t
812  __complex_arg(__complex__ decltype(0.0bf16) __z)
813  { return __gnu_cxx::__bfloat16_t(__builtin_cargf(__z)); }
814 
815  inline __complex__ decltype(0.0bf16)
816  __complex_cos(__complex__ decltype(0.0bf16) __z)
817  { return static_cast<__complex__ decltype(0.0bf16)>(__builtin_ccosf(__z)); }
818 
819  inline __complex__ decltype(0.0bf16)
820  __complex_cosh(__complex__ decltype(0.0bf16) __z)
821  { return static_cast<__complex__ decltype(0.0bf16)>(__builtin_ccoshf(__z)); }
822 
823  inline __complex__ decltype(0.0bf16)
824  __complex_exp(__complex__ decltype(0.0bf16) __z)
825  { return static_cast<__complex__ decltype(0.0bf16)>(__builtin_cexpf(__z)); }
826 
827  inline __complex__ decltype(0.0bf16)
828  __complex_log(__complex__ decltype(0.0bf16) __z)
829  { return static_cast<__complex__ decltype(0.0bf16)>(__builtin_clogf(__z)); }
830 
831  inline __complex__ decltype(0.0bf16)
832  __complex_sin(__complex__ decltype(0.0bf16) __z)
833  { return static_cast<__complex__ decltype(0.0bf16)>(__builtin_csinf(__z)); }
834 
835  inline __complex__ decltype(0.0bf16)
836  __complex_sinh(__complex__ decltype(0.0bf16) __z)
837  { return static_cast<__complex__ decltype(0.0bf16)>(__builtin_csinhf(__z)); }
838 
839  inline __complex__ decltype(0.0bf16)
840  __complex_sqrt(__complex__ decltype(0.0bf16) __z)
841  { return static_cast<__complex__ decltype(0.0bf16)>(__builtin_csqrtf(__z)); }
842 
843  inline __complex__ decltype(0.0bf16)
844  __complex_tan(__complex__ decltype(0.0bf16) __z)
845  { return static_cast<__complex__ decltype(0.0bf16)>(__builtin_ctanf(__z)); }
846 
847  inline __complex__ decltype(0.0bf16)
848  __complex_tanh(__complex__ decltype(0.0bf16) __z)
849  { return static_cast<__complex__ decltype(0.0bf16)>(__builtin_ctanhf(__z)); }
850 
851  inline __complex__ decltype(0.0bf16)
852  __complex_pow(__complex__ decltype(0.0bf16) __x,
853  __complex__ decltype(0.0bf16) __y)
854  { return static_cast<__complex__ decltype(0.0bf16)>(__builtin_cpowf(__x,
855  __y)); }
856 #endif
857 #endif
858 
859  // 26.2.7/3 abs(__z): Returns the magnitude of __z.
860  template<typename _Tp>
861  inline _Tp
862  __complex_abs(const complex<_Tp>& __z)
863  {
864  _Tp __x = __z.real();
865  _Tp __y = __z.imag();
866  const _Tp __s = std::max(abs(__x), abs(__y));
867  if (__s == _Tp()) // well ...
868  return __s;
869  __x /= __s;
870  __y /= __s;
871  return __s * sqrt(__x * __x + __y * __y);
872  }
873 
874 #if _GLIBCXX_USE_C99_COMPLEX
875  inline float
876  __complex_abs(__complex__ float __z) { return __builtin_cabsf(__z); }
877 
878  inline double
879  __complex_abs(__complex__ double __z) { return __builtin_cabs(__z); }
880 
881  inline long double
882  __complex_abs(const __complex__ long double& __z)
883  { return __builtin_cabsl(__z); }
884 
885  template<typename _Tp>
886  inline _Tp
887  abs(const complex<_Tp>& __z) { return __complex_abs(__z.__rep()); }
888 #else
889  template<typename _Tp>
890  inline _Tp
891  abs(const complex<_Tp>& __z) { return __complex_abs(__z); }
892 #endif
893 
894 
895  // 26.2.7/4: arg(__z): Returns the phase angle of __z.
896  template<typename _Tp>
897  inline _Tp
898  __complex_arg(const complex<_Tp>& __z)
899  { return atan2(__z.imag(), __z.real()); }
900 
901 #if _GLIBCXX_USE_C99_COMPLEX
902  inline float
903  __complex_arg(__complex__ float __z) { return __builtin_cargf(__z); }
904 
905  inline double
906  __complex_arg(__complex__ double __z) { return __builtin_carg(__z); }
907 
908  inline long double
909  __complex_arg(const __complex__ long double& __z)
910  { return __builtin_cargl(__z); }
911 
912  template<typename _Tp>
913  inline _Tp
914  arg(const complex<_Tp>& __z) { return __complex_arg(__z.__rep()); }
915 #else
916  template<typename _Tp>
917  inline _Tp
918  arg(const complex<_Tp>& __z) { return __complex_arg(__z); }
919 #endif
920 
921  // 26.2.7/5: norm(__z) returns the squared magnitude of __z.
922  // As defined, norm() is -not- a norm is the common mathematical
923  // sense used in numerics. The helper class _Norm_helper<> tries to
924  // distinguish between builtin floating point and the rest, so as
925  // to deliver an answer as close as possible to the real value.
926  template<bool>
927  struct _Norm_helper
928  {
929  template<typename _Tp>
930  static inline _GLIBCXX20_CONSTEXPR _Tp _S_do_it(const complex<_Tp>& __z)
931  {
932  const _Tp __x = __z.real();
933  const _Tp __y = __z.imag();
934  return __x * __x + __y * __y;
935  }
936  };
937 
938  template<>
939  struct _Norm_helper<true>
940  {
941  template<typename _Tp>
942  static inline _GLIBCXX20_CONSTEXPR _Tp _S_do_it(const complex<_Tp>& __z)
943  {
944  //_Tp __res = std::abs(__z);
945  //return __res * __res;
946  const _Tp __x = __z.real();
947  const _Tp __y = __z.imag();
948  return __x * __x + __y * __y;
949  }
950  };
951 
952  template<typename _Tp>
953  inline _GLIBCXX20_CONSTEXPR _Tp
954  norm(const complex<_Tp>& __z)
955  {
956  return _Norm_helper<__is_floating<_Tp>::__value
957  && !_GLIBCXX_FAST_MATH>::_S_do_it(__z);
958  }
959 
960  template<typename _Tp>
961  inline complex<_Tp>
962  polar(const _Tp& __rho, const _Tp& __theta)
963  {
964  __glibcxx_assert( __rho >= 0 );
965  return complex<_Tp>(__rho * cos(__theta), __rho * sin(__theta));
966  }
967 
968  template<typename _Tp>
969  inline _GLIBCXX20_CONSTEXPR complex<_Tp>
970  conj(const complex<_Tp>& __z)
971  { return complex<_Tp>(__z.real(), -__z.imag()); }
972 
973  // Transcendentals
974 
975  // 26.2.8/1 cos(__z): Returns the cosine of __z.
976  template<typename _Tp>
977  inline complex<_Tp>
978  __complex_cos(const complex<_Tp>& __z)
979  {
980  const _Tp __x = __z.real();
981  const _Tp __y = __z.imag();
982  return complex<_Tp>(cos(__x) * cosh(__y), -sin(__x) * sinh(__y));
983  }
984 
985 #if _GLIBCXX_USE_C99_COMPLEX
986  inline __complex__ float
987  __complex_cos(__complex__ float __z) { return __builtin_ccosf(__z); }
988 
989  inline __complex__ double
990  __complex_cos(__complex__ double __z) { return __builtin_ccos(__z); }
991 
992  inline __complex__ long double
993  __complex_cos(const __complex__ long double& __z)
994  { return __builtin_ccosl(__z); }
995 
996  template<typename _Tp>
997  inline complex<_Tp>
998  cos(const complex<_Tp>& __z) { return __complex_cos(__z.__rep()); }
999 #else
1000  template<typename _Tp>
1001  inline complex<_Tp>
1002  cos(const complex<_Tp>& __z) { return __complex_cos(__z); }
1003 #endif
1004 
1005  // 26.2.8/2 cosh(__z): Returns the hyperbolic cosine of __z.
1006  template<typename _Tp>
1007  inline complex<_Tp>
1008  __complex_cosh(const complex<_Tp>& __z)
1009  {
1010  const _Tp __x = __z.real();
1011  const _Tp __y = __z.imag();
1012  return complex<_Tp>(cosh(__x) * cos(__y), sinh(__x) * sin(__y));
1013  }
1014 
1015 #if _GLIBCXX_USE_C99_COMPLEX
1016  inline __complex__ float
1017  __complex_cosh(__complex__ float __z) { return __builtin_ccoshf(__z); }
1018 
1019  inline __complex__ double
1020  __complex_cosh(__complex__ double __z) { return __builtin_ccosh(__z); }
1021 
1022  inline __complex__ long double
1023  __complex_cosh(const __complex__ long double& __z)
1024  { return __builtin_ccoshl(__z); }
1025 
1026  template<typename _Tp>
1027  inline complex<_Tp>
1028  cosh(const complex<_Tp>& __z) { return __complex_cosh(__z.__rep()); }
1029 #else
1030  template<typename _Tp>
1031  inline complex<_Tp>
1032  cosh(const complex<_Tp>& __z) { return __complex_cosh(__z); }
1033 #endif
1034 
1035  // 26.2.8/3 exp(__z): Returns the complex base e exponential of x
1036  template<typename _Tp>
1037  inline complex<_Tp>
1038  __complex_exp(const complex<_Tp>& __z)
1039  { return std::polar<_Tp>(exp(__z.real()), __z.imag()); }
1040 
1041 #if _GLIBCXX_USE_C99_COMPLEX
1042  inline __complex__ float
1043  __complex_exp(__complex__ float __z) { return __builtin_cexpf(__z); }
1044 
1045  inline __complex__ double
1046  __complex_exp(__complex__ double __z) { return __builtin_cexp(__z); }
1047 
1048  inline __complex__ long double
1049  __complex_exp(const __complex__ long double& __z)
1050  { return __builtin_cexpl(__z); }
1051 
1052  template<typename _Tp>
1053  inline complex<_Tp>
1054  exp(const complex<_Tp>& __z) { return __complex_exp(__z.__rep()); }
1055 #else
1056  template<typename _Tp>
1057  inline complex<_Tp>
1058  exp(const complex<_Tp>& __z) { return __complex_exp(__z); }
1059 #endif
1060 
1061  // 26.2.8/5 log(__z): Returns the natural complex logarithm of __z.
1062  // The branch cut is along the negative axis.
1063  template<typename _Tp>
1064  inline complex<_Tp>
1065  __complex_log(const complex<_Tp>& __z)
1066  { return complex<_Tp>(log(std::abs(__z)), std::arg(__z)); }
1067 
1068 #if _GLIBCXX_USE_C99_COMPLEX
1069  inline __complex__ float
1070  __complex_log(__complex__ float __z) { return __builtin_clogf(__z); }
1071 
1072  inline __complex__ double
1073  __complex_log(__complex__ double __z) { return __builtin_clog(__z); }
1074 
1075  inline __complex__ long double
1076  __complex_log(const __complex__ long double& __z)
1077  { return __builtin_clogl(__z); }
1078 
1079  template<typename _Tp>
1080  inline complex<_Tp>
1081  log(const complex<_Tp>& __z) { return __complex_log(__z.__rep()); }
1082 #else
1083  template<typename _Tp>
1084  inline complex<_Tp>
1085  log(const complex<_Tp>& __z) { return __complex_log(__z); }
1086 #endif
1087 
1088  template<typename _Tp>
1089  inline complex<_Tp>
1090  log10(const complex<_Tp>& __z)
1091  { return std::log(__z) / log(_Tp(10.0)); }
1092 
1093  // 26.2.8/10 sin(__z): Returns the sine of __z.
1094  template<typename _Tp>
1095  inline complex<_Tp>
1096  __complex_sin(const complex<_Tp>& __z)
1097  {
1098  const _Tp __x = __z.real();
1099  const _Tp __y = __z.imag();
1100  return complex<_Tp>(sin(__x) * cosh(__y), cos(__x) * sinh(__y));
1101  }
1102 
1103 #if _GLIBCXX_USE_C99_COMPLEX
1104  inline __complex__ float
1105  __complex_sin(__complex__ float __z) { return __builtin_csinf(__z); }
1106 
1107  inline __complex__ double
1108  __complex_sin(__complex__ double __z) { return __builtin_csin(__z); }
1109 
1110  inline __complex__ long double
1111  __complex_sin(const __complex__ long double& __z)
1112  { return __builtin_csinl(__z); }
1113 
1114  template<typename _Tp>
1115  inline complex<_Tp>
1116  sin(const complex<_Tp>& __z) { return __complex_sin(__z.__rep()); }
1117 #else
1118  template<typename _Tp>
1119  inline complex<_Tp>
1120  sin(const complex<_Tp>& __z) { return __complex_sin(__z); }
1121 #endif
1122 
1123  // 26.2.8/11 sinh(__z): Returns the hyperbolic sine of __z.
1124  template<typename _Tp>
1125  inline complex<_Tp>
1126  __complex_sinh(const complex<_Tp>& __z)
1127  {
1128  const _Tp __x = __z.real();
1129  const _Tp __y = __z.imag();
1130  return complex<_Tp>(sinh(__x) * cos(__y), cosh(__x) * sin(__y));
1131  }
1132 
1133 #if _GLIBCXX_USE_C99_COMPLEX
1134  inline __complex__ float
1135  __complex_sinh(__complex__ float __z) { return __builtin_csinhf(__z); }
1136 
1137  inline __complex__ double
1138  __complex_sinh(__complex__ double __z) { return __builtin_csinh(__z); }
1139 
1140  inline __complex__ long double
1141  __complex_sinh(const __complex__ long double& __z)
1142  { return __builtin_csinhl(__z); }
1143 
1144  template<typename _Tp>
1145  inline complex<_Tp>
1146  sinh(const complex<_Tp>& __z) { return __complex_sinh(__z.__rep()); }
1147 #else
1148  template<typename _Tp>
1149  inline complex<_Tp>
1150  sinh(const complex<_Tp>& __z) { return __complex_sinh(__z); }
1151 #endif
1152 
1153  // 26.2.8/13 sqrt(__z): Returns the complex square root of __z.
1154  // The branch cut is on the negative axis.
1155  template<typename _Tp>
1156  complex<_Tp>
1157  __complex_sqrt(const complex<_Tp>& __z)
1158  {
1159  _Tp __x = __z.real();
1160  _Tp __y = __z.imag();
1161 
1162  if (__x == _Tp())
1163  {
1164  _Tp __t = sqrt(abs(__y) / 2);
1165  return complex<_Tp>(__t, __y < _Tp() ? -__t : __t);
1166  }
1167  else
1168  {
1169  _Tp __t = sqrt(2 * (std::abs(__z) + abs(__x)));
1170  _Tp __u = __t / 2;
1171  return __x > _Tp()
1172  ? complex<_Tp>(__u, __y / __t)
1173  : complex<_Tp>(abs(__y) / __t, __y < _Tp() ? -__u : __u);
1174  }
1175  }
1176 
1177 #if _GLIBCXX_USE_C99_COMPLEX
1178  inline __complex__ float
1179  __complex_sqrt(__complex__ float __z) { return __builtin_csqrtf(__z); }
1180 
1181  inline __complex__ double
1182  __complex_sqrt(__complex__ double __z) { return __builtin_csqrt(__z); }
1183 
1184  inline __complex__ long double
1185  __complex_sqrt(const __complex__ long double& __z)
1186  { return __builtin_csqrtl(__z); }
1187 
1188  template<typename _Tp>
1189  inline complex<_Tp>
1190  sqrt(const complex<_Tp>& __z) { return __complex_sqrt(__z.__rep()); }
1191 #else
1192  template<typename _Tp>
1193  inline complex<_Tp>
1194  sqrt(const complex<_Tp>& __z) { return __complex_sqrt(__z); }
1195 #endif
1196 
1197  // 26.2.8/14 tan(__z): Return the complex tangent of __z.
1198 
1199  template<typename _Tp>
1200  inline complex<_Tp>
1201  __complex_tan(const complex<_Tp>& __z)
1202  { return std::sin(__z) / std::cos(__z); }
1203 
1204 #if _GLIBCXX_USE_C99_COMPLEX
1205  inline __complex__ float
1206  __complex_tan(__complex__ float __z) { return __builtin_ctanf(__z); }
1207 
1208  inline __complex__ double
1209  __complex_tan(__complex__ double __z) { return __builtin_ctan(__z); }
1210 
1211  inline __complex__ long double
1212  __complex_tan(const __complex__ long double& __z)
1213  { return __builtin_ctanl(__z); }
1214 
1215  template<typename _Tp>
1216  inline complex<_Tp>
1217  tan(const complex<_Tp>& __z) { return __complex_tan(__z.__rep()); }
1218 #else
1219  template<typename _Tp>
1220  inline complex<_Tp>
1221  tan(const complex<_Tp>& __z) { return __complex_tan(__z); }
1222 #endif
1223 
1224 
1225  // 26.2.8/15 tanh(__z): Returns the hyperbolic tangent of __z.
1226 
1227  template<typename _Tp>
1228  inline complex<_Tp>
1229  __complex_tanh(const complex<_Tp>& __z)
1230  { return std::sinh(__z) / std::cosh(__z); }
1231 
1232 #if _GLIBCXX_USE_C99_COMPLEX
1233  inline __complex__ float
1234  __complex_tanh(__complex__ float __z) { return __builtin_ctanhf(__z); }
1235 
1236  inline __complex__ double
1237  __complex_tanh(__complex__ double __z) { return __builtin_ctanh(__z); }
1238 
1239  inline __complex__ long double
1240  __complex_tanh(const __complex__ long double& __z)
1241  { return __builtin_ctanhl(__z); }
1242 
1243  template<typename _Tp>
1244  inline complex<_Tp>
1245  tanh(const complex<_Tp>& __z) { return __complex_tanh(__z.__rep()); }
1246 #else
1247  template<typename _Tp>
1248  inline complex<_Tp>
1249  tanh(const complex<_Tp>& __z) { return __complex_tanh(__z); }
1250 #endif
1251 
1252 
1253  // 26.2.8/9 pow(__x, __y): Returns the complex power base of __x
1254  // raised to the __y-th power. The branch
1255  // cut is on the negative axis.
1256  template<typename _Tp>
1257  complex<_Tp>
1258  __complex_pow_unsigned(complex<_Tp> __x, unsigned __n)
1259  {
1260  complex<_Tp> __y = __n % 2 ? __x : complex<_Tp>(1);
1261 
1262  while (__n >>= 1)
1263  {
1264  __x *= __x;
1265  if (__n % 2)
1266  __y *= __x;
1267  }
1268 
1269  return __y;
1270  }
1271 
1272  // In C++11 mode we used to implement the resolution of
1273  // DR 844. complex pow return type is ambiguous.
1274  // thus the following overload was disabled in that mode. However, doing
1275  // that causes all sorts of issues, see, for example:
1276  // http://gcc.gnu.org/ml/libstdc++/2013-01/msg00058.html
1277  // and also PR57974.
1278  template<typename _Tp>
1279  inline complex<_Tp>
1280  pow(const complex<_Tp>& __z, int __n)
1281  {
1282  return __n < 0
1283  ? complex<_Tp>(1) / std::__complex_pow_unsigned(__z, -(unsigned)__n)
1284  : std::__complex_pow_unsigned(__z, __n);
1285  }
1286 
1287  template<typename _Tp>
1288  complex<_Tp>
1289  pow(const complex<_Tp>& __x, const _Tp& __y)
1290  {
1291 #if ! _GLIBCXX_USE_C99_COMPLEX
1292  if (__x == _Tp())
1293  return _Tp();
1294 #endif
1295  if (__x.imag() == _Tp() && __x.real() > _Tp())
1296  return pow(__x.real(), __y);
1297 
1298  complex<_Tp> __t = std::log(__x);
1299  return std::polar<_Tp>(exp(__y * __t.real()), __y * __t.imag());
1300  }
1301 
1302  template<typename _Tp>
1303  inline complex<_Tp>
1304  __complex_pow(const complex<_Tp>& __x, const complex<_Tp>& __y)
1305  { return __x == _Tp() ? _Tp() : std::exp(__y * std::log(__x)); }
1306 
1307 #if _GLIBCXX_USE_C99_COMPLEX
1308  inline __complex__ float
1309  __complex_pow(__complex__ float __x, __complex__ float __y)
1310  { return __builtin_cpowf(__x, __y); }
1311 
1312  inline __complex__ double
1313  __complex_pow(__complex__ double __x, __complex__ double __y)
1314  { return __builtin_cpow(__x, __y); }
1315 
1316  inline __complex__ long double
1317  __complex_pow(const __complex__ long double& __x,
1318  const __complex__ long double& __y)
1319  { return __builtin_cpowl(__x, __y); }
1320 
1321  template<typename _Tp>
1322  inline complex<_Tp>
1323  pow(const complex<_Tp>& __x, const complex<_Tp>& __y)
1324  { return __complex_pow(__x.__rep(), __y.__rep()); }
1325 #else
1326  template<typename _Tp>
1327  inline complex<_Tp>
1328  pow(const complex<_Tp>& __x, const complex<_Tp>& __y)
1329  { return __complex_pow(__x, __y); }
1330 #endif
1331 
1332  template<typename _Tp>
1333  inline complex<_Tp>
1334  pow(const _Tp& __x, const complex<_Tp>& __y)
1335  {
1336  return __x > _Tp() ? std::polar<_Tp>(pow(__x, __y.real()),
1337  __y.imag() * log(__x))
1338  : std::pow(complex<_Tp>(__x), __y);
1339  }
1340 
1341  /// 26.2.3 complex specializations
1342  /// complex<float> specialization
1343  template<>
1344  class complex<float>
1345  {
1346  public:
1347  typedef float value_type;
1348  typedef __complex__ float _ComplexT;
1349 
1350  _GLIBCXX_CONSTEXPR complex(_ComplexT __z) : _M_value(__z) { }
1351 
1352  _GLIBCXX_CONSTEXPR complex(float __r = 0.0f, float __i = 0.0f)
1353 #if __cplusplus >= 201103L
1354  : _M_value{ __r, __i } { }
1355 #else
1356  {
1357  __real__ _M_value = __r;
1358  __imag__ _M_value = __i;
1359  }
1360 #endif
1361 
1362 #if __cplusplus > 202002L
1363  template<typename _Up>
1364  explicit(!requires(_Up __u) { value_type{__u}; })
1365  constexpr complex(const complex<_Up>& __z)
1366  : _M_value{ value_type(__z.real()), value_type(__z.imag()) } { }
1367 #else
1368  explicit _GLIBCXX_CONSTEXPR complex(const complex<double>&);
1369  explicit _GLIBCXX_CONSTEXPR complex(const complex<long double>&);
1370 #endif
1371 
1372 #if __cplusplus >= 201103L
1373  // _GLIBCXX_RESOLVE_LIB_DEFECTS
1374  // DR 387. std::complex over-encapsulated.
1375  __attribute ((__abi_tag__ ("cxx11")))
1376  constexpr float
1377  real() const { return __real__ _M_value; }
1378 
1379  __attribute ((__abi_tag__ ("cxx11")))
1380  constexpr float
1381  imag() const { return __imag__ _M_value; }
1382 #else
1383  float&
1384  real() { return __real__ _M_value; }
1385 
1386  const float&
1387  real() const { return __real__ _M_value; }
1388 
1389  float&
1390  imag() { return __imag__ _M_value; }
1391 
1392  const float&
1393  imag() const { return __imag__ _M_value; }
1394 #endif
1395 
1396  // _GLIBCXX_RESOLVE_LIB_DEFECTS
1397  // DR 387. std::complex over-encapsulated.
1398  _GLIBCXX20_CONSTEXPR void
1399  real(float __val) { __real__ _M_value = __val; }
1400 
1401  _GLIBCXX20_CONSTEXPR void
1402  imag(float __val) { __imag__ _M_value = __val; }
1403 
1404  _GLIBCXX20_CONSTEXPR complex&
1405  operator=(float __f)
1406  {
1407  _M_value = __f;
1408  return *this;
1409  }
1410 
1411  _GLIBCXX20_CONSTEXPR complex&
1412  operator+=(float __f)
1413  {
1414  _M_value += __f;
1415  return *this;
1416  }
1417 
1418  _GLIBCXX20_CONSTEXPR complex&
1419  operator-=(float __f)
1420  {
1421  _M_value -= __f;
1422  return *this;
1423  }
1424 
1425  _GLIBCXX20_CONSTEXPR complex&
1426  operator*=(float __f)
1427  {
1428  _M_value *= __f;
1429  return *this;
1430  }
1431 
1432  _GLIBCXX20_CONSTEXPR complex&
1433  operator/=(float __f)
1434  {
1435  _M_value /= __f;
1436  return *this;
1437  }
1438 
1439  // Let the compiler synthesize the copy and assignment
1440  // operator. It always does a pretty good job.
1441 #if __cplusplus >= 201103L
1442  _GLIBCXX14_CONSTEXPR complex& operator=(const complex&) = default;
1443 #endif
1444 
1445  template<typename _Tp>
1446  _GLIBCXX20_CONSTEXPR complex&
1447  operator=(const complex<_Tp>& __z)
1448  {
1449  __real__ _M_value = __z.real();
1450  __imag__ _M_value = __z.imag();
1451  return *this;
1452  }
1453 
1454  template<typename _Tp>
1455  _GLIBCXX20_CONSTEXPR complex&
1456  operator+=(const complex<_Tp>& __z)
1457  {
1458  _M_value += __z.__rep();
1459  return *this;
1460  }
1461 
1462  template<class _Tp>
1463  _GLIBCXX20_CONSTEXPR complex&
1464  operator-=(const complex<_Tp>& __z)
1465  {
1466  _M_value -= __z.__rep();
1467  return *this;
1468  }
1469 
1470  template<class _Tp>
1471  _GLIBCXX20_CONSTEXPR complex&
1472  operator*=(const complex<_Tp>& __z)
1473  {
1474  const _ComplexT __t = __z.__rep();
1475  _M_value *= __t;
1476  return *this;
1477  }
1478 
1479  template<class _Tp>
1480  _GLIBCXX20_CONSTEXPR complex&
1481  operator/=(const complex<_Tp>& __z)
1482  {
1483  const _ComplexT __t = __z.__rep();
1484  _M_value /= __t;
1485  return *this;
1486  }
1487 
1488  _GLIBCXX_CONSTEXPR _ComplexT __rep() const { return _M_value; }
1489 
1490  private:
1491  _ComplexT _M_value;
1492  };
1493 
1494  /// 26.2.3 complex specializations
1495  /// complex<double> specialization
1496  template<>
1497  class complex<double>
1498  {
1499  public:
1500  typedef double value_type;
1501  typedef __complex__ double _ComplexT;
1502 
1503  _GLIBCXX_CONSTEXPR complex(_ComplexT __z) : _M_value(__z) { }
1504 
1505  _GLIBCXX_CONSTEXPR complex(double __r = 0.0, double __i = 0.0)
1506 #if __cplusplus >= 201103L
1507  : _M_value{ __r, __i } { }
1508 #else
1509  {
1510  __real__ _M_value = __r;
1511  __imag__ _M_value = __i;
1512  }
1513 #endif
1514 
1515 #if __cplusplus > 202002L
1516  template<typename _Up>
1517  explicit(!requires(_Up __u) { value_type{__u}; })
1518  constexpr complex(const complex<_Up>& __z)
1519  : _M_value{ value_type(__z.real()), value_type(__z.imag()) } { }
1520 #else
1521  _GLIBCXX_CONSTEXPR complex(const complex<float>& __z)
1522  : _M_value(__z.__rep()) { }
1523 
1524  explicit _GLIBCXX_CONSTEXPR complex(const complex<long double>&);
1525 #endif
1526 
1527 #if __cplusplus >= 201103L
1528  // _GLIBCXX_RESOLVE_LIB_DEFECTS
1529  // DR 387. std::complex over-encapsulated.
1530  __attribute ((__abi_tag__ ("cxx11")))
1531  constexpr double
1532  real() const { return __real__ _M_value; }
1533 
1534  __attribute ((__abi_tag__ ("cxx11")))
1535  constexpr double
1536  imag() const { return __imag__ _M_value; }
1537 #else
1538  double&
1539  real() { return __real__ _M_value; }
1540 
1541  const double&
1542  real() const { return __real__ _M_value; }
1543 
1544  double&
1545  imag() { return __imag__ _M_value; }
1546 
1547  const double&
1548  imag() const { return __imag__ _M_value; }
1549 #endif
1550 
1551  // _GLIBCXX_RESOLVE_LIB_DEFECTS
1552  // DR 387. std::complex over-encapsulated.
1553  _GLIBCXX20_CONSTEXPR void
1554  real(double __val) { __real__ _M_value = __val; }
1555 
1556  _GLIBCXX20_CONSTEXPR void
1557  imag(double __val) { __imag__ _M_value = __val; }
1558 
1559  _GLIBCXX20_CONSTEXPR complex&
1560  operator=(double __d)
1561  {
1562  _M_value = __d;
1563  return *this;
1564  }
1565 
1566  _GLIBCXX20_CONSTEXPR complex&
1567  operator+=(double __d)
1568  {
1569  _M_value += __d;
1570  return *this;
1571  }
1572 
1573  _GLIBCXX20_CONSTEXPR complex&
1574  operator-=(double __d)
1575  {
1576  _M_value -= __d;
1577  return *this;
1578  }
1579 
1580  _GLIBCXX20_CONSTEXPR complex&
1581  operator*=(double __d)
1582  {
1583  _M_value *= __d;
1584  return *this;
1585  }
1586 
1587  _GLIBCXX20_CONSTEXPR complex&
1588  operator/=(double __d)
1589  {
1590  _M_value /= __d;
1591  return *this;
1592  }
1593 
1594  // The compiler will synthesize this, efficiently.
1595 #if __cplusplus >= 201103L
1596  _GLIBCXX14_CONSTEXPR complex& operator=(const complex&) = default;
1597 #endif
1598 
1599  template<typename _Tp>
1600  _GLIBCXX20_CONSTEXPR complex&
1601  operator=(const complex<_Tp>& __z)
1602  {
1603  _M_value = __z.__rep();
1604  return *this;
1605  }
1606 
1607  template<typename _Tp>
1608  _GLIBCXX20_CONSTEXPR complex&
1609  operator+=(const complex<_Tp>& __z)
1610  {
1611  _M_value += __z.__rep();
1612  return *this;
1613  }
1614 
1615  template<typename _Tp>
1616  _GLIBCXX20_CONSTEXPR complex&
1617  operator-=(const complex<_Tp>& __z)
1618  {
1619  _M_value -= __z.__rep();
1620  return *this;
1621  }
1622 
1623  template<typename _Tp>
1624  _GLIBCXX20_CONSTEXPR complex&
1625  operator*=(const complex<_Tp>& __z)
1626  {
1627  const _ComplexT __t = __z.__rep();
1628  _M_value *= __t;
1629  return *this;
1630  }
1631 
1632  template<typename _Tp>
1633  _GLIBCXX20_CONSTEXPR complex&
1634  operator/=(const complex<_Tp>& __z)
1635  {
1636  const _ComplexT __t = __z.__rep();
1637  _M_value /= __t;
1638  return *this;
1639  }
1640 
1641  _GLIBCXX_CONSTEXPR _ComplexT __rep() const { return _M_value; }
1642 
1643  private:
1644  _ComplexT _M_value;
1645  };
1646 
1647  /// 26.2.3 complex specializations
1648  /// complex<long double> specialization
1649  template<>
1650  class complex<long double>
1651  {
1652  public:
1653  typedef long double value_type;
1654  typedef __complex__ long double _ComplexT;
1655 
1656  _GLIBCXX_CONSTEXPR complex(_ComplexT __z) : _M_value(__z) { }
1657 
1658  _GLIBCXX_CONSTEXPR complex(long double __r = 0.0L,
1659  long double __i = 0.0L)
1660 #if __cplusplus >= 201103L
1661  : _M_value{ __r, __i } { }
1662 #else
1663  {
1664  __real__ _M_value = __r;
1665  __imag__ _M_value = __i;
1666  }
1667 #endif
1668 
1669 #if __cplusplus > 202002L
1670  template<typename _Up>
1671  explicit(!requires(_Up __u) { value_type{__u}; })
1672  constexpr complex(const complex<_Up>& __z)
1673  : _M_value{ value_type(__z.real()), value_type(__z.imag()) } { }
1674 #else
1675  _GLIBCXX_CONSTEXPR complex(const complex<float>& __z)
1676  : _M_value(__z.__rep()) { }
1677 
1678  _GLIBCXX_CONSTEXPR complex(const complex<double>& __z)
1679  : _M_value(__z.__rep()) { }
1680 #endif
1681 
1682 #if __cplusplus >= 201103L
1683  // _GLIBCXX_RESOLVE_LIB_DEFECTS
1684  // DR 387. std::complex over-encapsulated.
1685  __attribute ((__abi_tag__ ("cxx11")))
1686  constexpr long double
1687  real() const { return __real__ _M_value; }
1688 
1689  __attribute ((__abi_tag__ ("cxx11")))
1690  constexpr long double
1691  imag() const { return __imag__ _M_value; }
1692 #else
1693  long double&
1694  real() { return __real__ _M_value; }
1695 
1696  const long double&
1697  real() const { return __real__ _M_value; }
1698 
1699  long double&
1700  imag() { return __imag__ _M_value; }
1701 
1702  const long double&
1703  imag() const { return __imag__ _M_value; }
1704 #endif
1705 
1706  // _GLIBCXX_RESOLVE_LIB_DEFECTS
1707  // DR 387. std::complex over-encapsulated.
1708  _GLIBCXX20_CONSTEXPR void
1709  real(long double __val) { __real__ _M_value = __val; }
1710 
1711  _GLIBCXX20_CONSTEXPR void
1712  imag(long double __val) { __imag__ _M_value = __val; }
1713 
1714  _GLIBCXX20_CONSTEXPR complex&
1715  operator=(long double __r)
1716  {
1717  _M_value = __r;
1718  return *this;
1719  }
1720 
1721  _GLIBCXX20_CONSTEXPR complex&
1722  operator+=(long double __r)
1723  {
1724  _M_value += __r;
1725  return *this;
1726  }
1727 
1728  _GLIBCXX20_CONSTEXPR complex&
1729  operator-=(long double __r)
1730  {
1731  _M_value -= __r;
1732  return *this;
1733  }
1734 
1735  _GLIBCXX20_CONSTEXPR complex&
1736  operator*=(long double __r)
1737  {
1738  _M_value *= __r;
1739  return *this;
1740  }
1741 
1742  _GLIBCXX20_CONSTEXPR complex&
1743  operator/=(long double __r)
1744  {
1745  _M_value /= __r;
1746  return *this;
1747  }
1748 
1749  // The compiler knows how to do this efficiently
1750 #if __cplusplus >= 201103L
1751  _GLIBCXX14_CONSTEXPR complex& operator=(const complex&) = default;
1752 #endif
1753 
1754  template<typename _Tp>
1755  _GLIBCXX20_CONSTEXPR complex&
1756  operator=(const complex<_Tp>& __z)
1757  {
1758  _M_value = __z.__rep();
1759  return *this;
1760  }
1761 
1762  template<typename _Tp>
1763  _GLIBCXX20_CONSTEXPR complex&
1764  operator+=(const complex<_Tp>& __z)
1765  {
1766  _M_value += __z.__rep();
1767  return *this;
1768  }
1769 
1770  template<typename _Tp>
1771  _GLIBCXX20_CONSTEXPR complex&
1772  operator-=(const complex<_Tp>& __z)
1773  {
1774  _M_value -= __z.__rep();
1775  return *this;
1776  }
1777 
1778  template<typename _Tp>
1779  _GLIBCXX20_CONSTEXPR complex&
1780  operator*=(const complex<_Tp>& __z)
1781  {
1782  const _ComplexT __t = __z.__rep();
1783  _M_value *= __t;
1784  return *this;
1785  }
1786 
1787  template<typename _Tp>
1788  _GLIBCXX20_CONSTEXPR complex&
1789  operator/=(const complex<_Tp>& __z)
1790  {
1791  const _ComplexT __t = __z.__rep();
1792  _M_value /= __t;
1793  return *this;
1794  }
1795 
1796  _GLIBCXX_CONSTEXPR _ComplexT __rep() const { return _M_value; }
1797 
1798  private:
1799  _ComplexT _M_value;
1800  };
1801 
1802 #if __cplusplus > 202002L
1803  template<typename _Tp>
1804  struct __complex_type
1805  { };
1806 
1807 #ifdef __STDCPP_FLOAT16_T__
1808  template<>
1809  struct __complex_type<_Float16>
1810  { typedef __complex__ _Float16 type; };
1811 #endif
1812 
1813 #ifdef __STDCPP_FLOAT32_T__
1814  template<>
1815  struct __complex_type<_Float32>
1816  { typedef __complex__ _Float32 type; };
1817 #endif
1818 
1819 #ifdef __STDCPP_FLOAT64_T__
1820  template<>
1821  struct __complex_type<_Float64>
1822  { typedef __complex__ _Float64 type; };
1823 #endif
1824 
1825 #ifdef __STDCPP_FLOAT128_T__
1826  template<>
1827  struct __complex_type<_Float128>
1828  { typedef __complex__ _Float128 type; };
1829 #endif
1830 
1831 #ifdef __STDCPP_BFLOAT16_T__
1832  template<>
1833  struct __complex_type<__gnu_cxx::__bfloat16_t>
1834  { typedef __complex__ decltype(0.0bf16) type; };
1835 #endif
1836 
1837  template<typename _Tp>
1838  requires requires { typename __complex_type<_Tp>::type; }
1839  class complex<_Tp>
1840  {
1841  public:
1842  typedef _Tp value_type;
1843  typedef typename std::__complex_type<_Tp>::type _ComplexT;
1844 
1845  constexpr complex(_ComplexT __z) : _M_value(__z) { }
1846 
1847  constexpr complex(_Tp __r = _Tp(), _Tp __i = _Tp())
1848  : _M_value{ __r, __i } { }
1849 
1850  template<typename _Up>
1851  explicit(!requires(_Up __u) { value_type{__u}; })
1852  constexpr complex(const complex<_Up>& __z)
1853  : _M_value{ value_type(__z.real()), value_type(__z.imag()) } { }
1854 
1855  constexpr _Tp
1856  real() const { return __real__ _M_value; }
1857 
1858  constexpr _Tp
1859  imag() const { return __imag__ _M_value; }
1860 
1861  constexpr void
1862  real(_Tp __val) { __real__ _M_value = __val; }
1863 
1864  constexpr void
1865  imag(_Tp __val) { __imag__ _M_value = __val; }
1866 
1867  constexpr complex&
1868  operator=(_Tp __f)
1869  {
1870  _M_value = __f;
1871  return *this;
1872  }
1873 
1874  constexpr complex&
1875  operator+=(_Tp __f)
1876  {
1877  _M_value += __f;
1878  return *this;
1879  }
1880 
1881  constexpr complex&
1882  operator-=(_Tp __f)
1883  {
1884  _M_value -= __f;
1885  return *this;
1886  }
1887 
1888  constexpr complex&
1889  operator*=(_Tp __f)
1890  {
1891  _M_value *= __f;
1892  return *this;
1893  }
1894 
1895  constexpr complex&
1896  operator/=(_Tp __f)
1897  {
1898  _M_value /= __f;
1899  return *this;
1900  }
1901 
1902  // Let the compiler synthesize the copy and assignment
1903  // operator. It always does a pretty good job.
1904  constexpr complex& operator=(const complex&) = default;
1905 
1906  template<typename _Up>
1907  constexpr complex&
1908  operator=(const complex<_Up>& __z)
1909  {
1910  __real__ _M_value = __z.real();
1911  __imag__ _M_value = __z.imag();
1912  return *this;
1913  }
1914 
1915  template<typename _Up>
1916  constexpr complex&
1917  operator+=(const complex<_Up>& __z)
1918  {
1919  _M_value += __z.__rep();
1920  return *this;
1921  }
1922 
1923  template<class _Up>
1924  constexpr complex&
1925  operator-=(const complex<_Up>& __z)
1926  {
1927  _M_value -= __z.__rep();
1928  return *this;
1929  }
1930 
1931  template<class _Up>
1932  constexpr complex&
1933  operator*=(const complex<_Up>& __z)
1934  {
1935  const _ComplexT __t = __z.__rep();
1936  _M_value *= __t;
1937  return *this;
1938  }
1939 
1940  template<class _Up>
1941  constexpr complex&
1942  operator/=(const complex<_Up>& __z)
1943  {
1944  const _ComplexT __t = __z.__rep();
1945  _M_value /= __t;
1946  return *this;
1947  }
1948 
1949  constexpr _ComplexT __rep() const { return _M_value; }
1950 
1951  private:
1952  _ComplexT _M_value;
1953  };
1954 #endif
1955 
1956 #if __cplusplus <= 202002L
1957  // These bits have to be at the end of this file, so that the
1958  // specializations have all been defined.
1959  inline _GLIBCXX_CONSTEXPR
1960  complex<float>::complex(const complex<double>& __z)
1961  : _M_value(__z.__rep()) { }
1962 
1963  inline _GLIBCXX_CONSTEXPR
1964  complex<float>::complex(const complex<long double>& __z)
1965  : _M_value(__z.__rep()) { }
1966 
1967  inline _GLIBCXX_CONSTEXPR
1968  complex<double>::complex(const complex<long double>& __z)
1969  : _M_value(__z.__rep()) { }
1970 #endif
1971 
1972  // Inhibit implicit instantiations for required instantiations,
1973  // which are defined via explicit instantiations elsewhere.
1974  // NB: This syntax is a GNU extension.
1975 #if _GLIBCXX_EXTERN_TEMPLATE
1976  extern template istream& operator>>(istream&, complex<float>&);
1977  extern template ostream& operator<<(ostream&, const complex<float>&);
1978  extern template istream& operator>>(istream&, complex<double>&);
1979  extern template ostream& operator<<(ostream&, const complex<double>&);
1980  extern template istream& operator>>(istream&, complex<long double>&);
1981  extern template ostream& operator<<(ostream&, const complex<long double>&);
1982 
1983 #ifdef _GLIBCXX_USE_WCHAR_T
1984  extern template wistream& operator>>(wistream&, complex<float>&);
1985  extern template wostream& operator<<(wostream&, const complex<float>&);
1986  extern template wistream& operator>>(wistream&, complex<double>&);
1987  extern template wostream& operator<<(wostream&, const complex<double>&);
1988  extern template wistream& operator>>(wistream&, complex<long double>&);
1989  extern template wostream& operator<<(wostream&, const complex<long double>&);
1990 #endif
1991 #endif
1992 
1993  /// @} group complex_numbers
1994 
1995 _GLIBCXX_END_NAMESPACE_VERSION
1996 } // namespace
1997 
1998 #if __cplusplus >= 201103L
1999 
2000 namespace std _GLIBCXX_VISIBILITY(default)
2001 {
2002 _GLIBCXX_BEGIN_NAMESPACE_VERSION
2003 
2004  // Forward declarations.
2005  template<typename _Tp> std::complex<_Tp> acos(const std::complex<_Tp>&);
2006  template<typename _Tp> std::complex<_Tp> asin(const std::complex<_Tp>&);
2007  template<typename _Tp> std::complex<_Tp> atan(const std::complex<_Tp>&);
2008 
2009  template<typename _Tp> std::complex<_Tp> acosh(const std::complex<_Tp>&);
2010  template<typename _Tp> std::complex<_Tp> asinh(const std::complex<_Tp>&);
2011  template<typename _Tp> std::complex<_Tp> atanh(const std::complex<_Tp>&);
2012  // DR 595.
2013  template<typename _Tp> _Tp fabs(const std::complex<_Tp>&);
2014 
2015  template<typename _Tp>
2016  inline std::complex<_Tp>
2017  __complex_acos(const std::complex<_Tp>& __z)
2018  {
2019  const std::complex<_Tp> __t = std::asin(__z);
2020  const _Tp __pi_2 = 1.5707963267948966192313216916397514L;
2021  return std::complex<_Tp>(__pi_2 - __t.real(), -__t.imag());
2022  }
2023 
2024 #if _GLIBCXX_USE_C99_COMPLEX_TR1
2025 #if defined(__STDCPP_FLOAT16_T__) && defined(_GLIBCXX_FLOAT_IS_IEEE_BINARY32)
2026  inline __complex__ _Float16
2027  __complex_acos(__complex__ _Float16 __z)
2028  { return static_cast<__complex__ _Float16>(__builtin_cacosf(__z)); }
2029 
2030  inline __complex__ _Float16
2031  __complex_asin(__complex__ _Float16 __z)
2032  { return static_cast<__complex__ _Float16>(__builtin_casinf(__z)); }
2033 
2034  inline __complex__ _Float16
2035  __complex_atan(__complex__ _Float16 __z)
2036  { return static_cast<__complex__ _Float16>(__builtin_catanf(__z)); }
2037 
2038  inline __complex__ _Float16
2039  __complex_acosh(__complex__ _Float16 __z)
2040  { return static_cast<__complex__ _Float16>(__builtin_cacoshf(__z)); }
2041 
2042  inline __complex__ _Float16
2043  __complex_asinh(__complex__ _Float16 __z)
2044  { return static_cast<__complex__ _Float16>(__builtin_casinhf(__z)); }
2045 
2046  inline __complex__ _Float16
2047  __complex_atanh(__complex__ _Float16 __z)
2048  { return static_cast<__complex__ _Float16>(__builtin_catanhf(__z)); }
2049 #endif
2050 
2051 #if defined(__STDCPP_FLOAT32_T__) && defined(_GLIBCXX_FLOAT_IS_IEEE_BINARY32)
2052  inline __complex__ _Float32
2053  __complex_acos(__complex__ _Float32 __z)
2054  { return __builtin_cacosf(__z); }
2055 
2056  inline __complex__ _Float32
2057  __complex_asin(__complex__ _Float32 __z)
2058  { return __builtin_casinf(__z); }
2059 
2060  inline __complex__ _Float32
2061  __complex_atan(__complex__ _Float32 __z)
2062  { return __builtin_catanf(__z); }
2063 
2064  inline __complex__ _Float32
2065  __complex_acosh(__complex__ _Float32 __z)
2066  { return __builtin_cacoshf(__z); }
2067 
2068  inline __complex__ _Float32
2069  __complex_asinh(__complex__ _Float32 __z)
2070  { return __builtin_casinhf(__z); }
2071 
2072  inline __complex__ _Float32
2073  __complex_atanh(__complex__ _Float32 __z)
2074  { return __builtin_catanhf(__z); }
2075 #endif
2076 
2077 #if defined(__STDCPP_FLOAT64_T__) && defined(_GLIBCXX_DOUBLE_IS_IEEE_BINARY64)
2078  inline __complex__ _Float64
2079  __complex_acos(__complex__ _Float64 __z)
2080  { return __builtin_cacos(__z); }
2081 
2082  inline __complex__ _Float64
2083  __complex_asin(__complex__ _Float64 __z)
2084  { return __builtin_casin(__z); }
2085 
2086  inline __complex__ _Float64
2087  __complex_atan(__complex__ _Float64 __z)
2088  { return __builtin_catan(__z); }
2089 
2090  inline __complex__ _Float64
2091  __complex_acosh(__complex__ _Float64 __z)
2092  { return __builtin_cacosh(__z); }
2093 
2094  inline __complex__ _Float64
2095  __complex_asinh(__complex__ _Float64 __z)
2096  { return __builtin_casinh(__z); }
2097 
2098  inline __complex__ _Float64
2099  __complex_atanh(__complex__ _Float64 __z)
2100  { return __builtin_catanh(__z); }
2101 #endif
2102 
2103 #if defined(__STDCPP_FLOAT128_T__) && defined(_GLIBCXX_LDOUBLE_IS_IEEE_BINARY128)
2104  inline __complex__ _Float128
2105  __complex_acos(__complex__ _Float128 __z)
2106  { return __builtin_cacosl(__z); }
2107 
2108  inline __complex__ _Float128
2109  __complex_asin(__complex__ _Float128 __z)
2110  { return __builtin_casinl(__z); }
2111 
2112  inline __complex__ _Float128
2113  __complex_atan(__complex__ _Float128 __z)
2114  { return __builtin_catanl(__z); }
2115 
2116  inline __complex__ _Float128
2117  __complex_acosh(__complex__ _Float128 __z)
2118  { return __builtin_cacoshl(__z); }
2119 
2120  inline __complex__ _Float128
2121  __complex_asinh(__complex__ _Float128 __z)
2122  { return __builtin_casinhl(__z); }
2123 
2124  inline __complex__ _Float128
2125  __complex_atanh(__complex__ _Float128 __z)
2126  { return __builtin_catanhl(__z); }
2127 #elif defined(__STDCPP_FLOAT128_T__) && defined(_GLIBCXX_HAVE_FLOAT128_MATH)
2128  inline __complex__ _Float128
2129  __complex_acos(__complex__ _Float128 __z)
2130  { return __builtin_cacosf128(__z); }
2131 
2132  inline __complex__ _Float128
2133  __complex_asin(__complex__ _Float128 __z)
2134  { return __builtin_casinf128(__z); }
2135 
2136  inline __complex__ _Float128
2137  __complex_atan(__complex__ _Float128 __z)
2138  { return __builtin_catanf128(__z); }
2139 
2140  inline __complex__ _Float128
2141  __complex_acosh(__complex__ _Float128 __z)
2142  { return __builtin_cacoshf128(__z); }
2143 
2144  inline __complex__ _Float128
2145  __complex_asinh(__complex__ _Float128 __z)
2146  { return __builtin_casinhf128(__z); }
2147 
2148  inline __complex__ _Float128
2149  __complex_atanh(__complex__ _Float128 __z)
2150  { return __builtin_catanhf128(__z); }
2151 #endif
2152 
2153 #if defined(__STDCPP_BFLOAT16_T__) && defined(_GLIBCXX_FLOAT_IS_IEEE_BINARY32)
2154  inline __complex__ decltype(0.0bf16)
2155  __complex_acos(__complex__ decltype(0.0bf16) __z)
2156  { return static_cast<__complex__ decltype(0.0bf16)>(__builtin_cacosf(__z)); }
2157 
2158  inline __complex__ decltype(0.0bf16)
2159  __complex_asin(__complex__ decltype(0.0bf16) __z)
2160  { return static_cast<__complex__ decltype(0.0bf16)>(__builtin_casinf(__z)); }
2161 
2162  inline __complex__ decltype(0.0bf16)
2163  __complex_atan(__complex__ decltype(0.0bf16) __z)
2164  { return static_cast<__complex__ decltype(0.0bf16)>(__builtin_catanf(__z)); }
2165 
2166  inline __complex__ decltype(0.0bf16)
2167  __complex_acosh(__complex__ decltype(0.0bf16) __z)
2168  { return static_cast<__complex__ decltype(0.0bf16)>(__builtin_cacoshf(__z)); }
2169 
2170  inline __complex__ decltype(0.0bf16)
2171  __complex_asinh(__complex__ decltype(0.0bf16) __z)
2172  { return static_cast<__complex__ decltype(0.0bf16)>(__builtin_casinhf(__z)); }
2173 
2174  inline __complex__ decltype(0.0bf16)
2175  __complex_atanh(__complex__ decltype(0.0bf16) __z)
2176  { return static_cast<__complex__ decltype(0.0bf16)>(__builtin_catanhf(__z)); }
2177 #endif
2178 #endif
2179 
2180 #if _GLIBCXX_USE_C99_COMPLEX_TR1
2181  inline __complex__ float
2182  __complex_acos(__complex__ float __z)
2183  { return __builtin_cacosf(__z); }
2184 
2185  inline __complex__ double
2186  __complex_acos(__complex__ double __z)
2187  { return __builtin_cacos(__z); }
2188 
2189  inline __complex__ long double
2190  __complex_acos(const __complex__ long double& __z)
2191  { return __builtin_cacosl(__z); }
2192 
2193  template<typename _Tp>
2194  inline std::complex<_Tp>
2195  acos(const std::complex<_Tp>& __z)
2196  { return __complex_acos(__z.__rep()); }
2197 #else
2198  /// acos(__z) [8.1.2].
2199  // Effects: Behaves the same as C99 function cacos, defined
2200  // in subclause 7.3.5.1.
2201  template<typename _Tp>
2202  inline std::complex<_Tp>
2203  acos(const std::complex<_Tp>& __z)
2204  { return __complex_acos(__z); }
2205 #endif
2206 
2207  template<typename _Tp>
2208  inline std::complex<_Tp>
2209  __complex_asin(const std::complex<_Tp>& __z)
2210  {
2211  std::complex<_Tp> __t(-__z.imag(), __z.real());
2212  __t = std::asinh(__t);
2213  return std::complex<_Tp>(__t.imag(), -__t.real());
2214  }
2215 
2216 #if _GLIBCXX_USE_C99_COMPLEX_TR1
2217  inline __complex__ float
2218  __complex_asin(__complex__ float __z)
2219  { return __builtin_casinf(__z); }
2220 
2221  inline __complex__ double
2222  __complex_asin(__complex__ double __z)
2223  { return __builtin_casin(__z); }
2224 
2225  inline __complex__ long double
2226  __complex_asin(const __complex__ long double& __z)
2227  { return __builtin_casinl(__z); }
2228 
2229  template<typename _Tp>
2230  inline std::complex<_Tp>
2231  asin(const std::complex<_Tp>& __z)
2232  { return __complex_asin(__z.__rep()); }
2233 #else
2234  /// asin(__z) [8.1.3].
2235  // Effects: Behaves the same as C99 function casin, defined
2236  // in subclause 7.3.5.2.
2237  template<typename _Tp>
2238  inline std::complex<_Tp>
2239  asin(const std::complex<_Tp>& __z)
2240  { return __complex_asin(__z); }
2241 #endif
2242 
2243  template<typename _Tp>
2244  std::complex<_Tp>
2245  __complex_atan(const std::complex<_Tp>& __z)
2246  {
2247  const _Tp __r2 = __z.real() * __z.real();
2248  const _Tp __x = _Tp(1.0) - __r2 - __z.imag() * __z.imag();
2249 
2250  _Tp __num = __z.imag() + _Tp(1.0);
2251  _Tp __den = __z.imag() - _Tp(1.0);
2252 
2253  __num = __r2 + __num * __num;
2254  __den = __r2 + __den * __den;
2255 
2256  return std::complex<_Tp>(_Tp(0.5) * atan2(_Tp(2.0) * __z.real(), __x),
2257  _Tp(0.25) * log(__num / __den));
2258  }
2259 
2260 #if _GLIBCXX_USE_C99_COMPLEX_TR1
2261  inline __complex__ float
2262  __complex_atan(__complex__ float __z)
2263  { return __builtin_catanf(__z); }
2264 
2265  inline __complex__ double
2266  __complex_atan(__complex__ double __z)
2267  { return __builtin_catan(__z); }
2268 
2269  inline __complex__ long double
2270  __complex_atan(const __complex__ long double& __z)
2271  { return __builtin_catanl(__z); }
2272 
2273  template<typename _Tp>
2274  inline std::complex<_Tp>
2275  atan(const std::complex<_Tp>& __z)
2276  { return __complex_atan(__z.__rep()); }
2277 #else
2278  /// atan(__z) [8.1.4].
2279  // Effects: Behaves the same as C99 function catan, defined
2280  // in subclause 7.3.5.3.
2281  template<typename _Tp>
2282  inline std::complex<_Tp>
2283  atan(const std::complex<_Tp>& __z)
2284  { return __complex_atan(__z); }
2285 #endif
2286 
2287  template<typename _Tp>
2288  std::complex<_Tp>
2289  __complex_acosh(const std::complex<_Tp>& __z)
2290  {
2291  // Kahan's formula.
2292  return _Tp(2.0) * std::log(std::sqrt(_Tp(0.5) * (__z + _Tp(1.0)))
2293  + std::sqrt(_Tp(0.5) * (__z - _Tp(1.0))));
2294  }
2295 
2296 #if _GLIBCXX_USE_C99_COMPLEX_TR1
2297  inline __complex__ float
2298  __complex_acosh(__complex__ float __z)
2299  { return __builtin_cacoshf(__z); }
2300 
2301  inline __complex__ double
2302  __complex_acosh(__complex__ double __z)
2303  { return __builtin_cacosh(__z); }
2304 
2305  inline __complex__ long double
2306  __complex_acosh(const __complex__ long double& __z)
2307  { return __builtin_cacoshl(__z); }
2308 
2309  template<typename _Tp>
2310  inline std::complex<_Tp>
2311  acosh(const std::complex<_Tp>& __z)
2312  { return __complex_acosh(__z.__rep()); }
2313 #else
2314  /// acosh(__z) [8.1.5].
2315  // Effects: Behaves the same as C99 function cacosh, defined
2316  // in subclause 7.3.6.1.
2317  template<typename _Tp>
2318  inline std::complex<_Tp>
2319  acosh(const std::complex<_Tp>& __z)
2320  { return __complex_acosh(__z); }
2321 #endif
2322 
2323  template<typename _Tp>
2324  std::complex<_Tp>
2325  __complex_asinh(const std::complex<_Tp>& __z)
2326  {
2327  std::complex<_Tp> __t((__z.real() - __z.imag())
2328  * (__z.real() + __z.imag()) + _Tp(1.0),
2329  _Tp(2.0) * __z.real() * __z.imag());
2330  __t = std::sqrt(__t);
2331 
2332  return std::log(__t + __z);
2333  }
2334 
2335 #if _GLIBCXX_USE_C99_COMPLEX_TR1
2336  inline __complex__ float
2337  __complex_asinh(__complex__ float __z)
2338  { return __builtin_casinhf(__z); }
2339 
2340  inline __complex__ double
2341  __complex_asinh(__complex__ double __z)
2342  { return __builtin_casinh(__z); }
2343 
2344  inline __complex__ long double
2345  __complex_asinh(const __complex__ long double& __z)
2346  { return __builtin_casinhl(__z); }
2347 
2348  template<typename _Tp>
2349  inline std::complex<_Tp>
2350  asinh(const std::complex<_Tp>& __z)
2351  { return __complex_asinh(__z.__rep()); }
2352 #else
2353  /// asinh(__z) [8.1.6].
2354  // Effects: Behaves the same as C99 function casin, defined
2355  // in subclause 7.3.6.2.
2356  template<typename _Tp>
2357  inline std::complex<_Tp>
2358  asinh(const std::complex<_Tp>& __z)
2359  { return __complex_asinh(__z); }
2360 #endif
2361 
2362  template<typename _Tp>
2363  std::complex<_Tp>
2364  __complex_atanh(const std::complex<_Tp>& __z)
2365  {
2366  const _Tp __i2 = __z.imag() * __z.imag();
2367  const _Tp __x = _Tp(1.0) - __i2 - __z.real() * __z.real();
2368 
2369  _Tp __num = _Tp(1.0) + __z.real();
2370  _Tp __den = _Tp(1.0) - __z.real();
2371 
2372  __num = __i2 + __num * __num;
2373  __den = __i2 + __den * __den;
2374 
2375  return std::complex<_Tp>(_Tp(0.25) * (log(__num) - log(__den)),
2376  _Tp(0.5) * atan2(_Tp(2.0) * __z.imag(), __x));
2377  }
2378 
2379 #if _GLIBCXX_USE_C99_COMPLEX_TR1
2380  inline __complex__ float
2381  __complex_atanh(__complex__ float __z)
2382  { return __builtin_catanhf(__z); }
2383 
2384  inline __complex__ double
2385  __complex_atanh(__complex__ double __z)
2386  { return __builtin_catanh(__z); }
2387 
2388  inline __complex__ long double
2389  __complex_atanh(const __complex__ long double& __z)
2390  { return __builtin_catanhl(__z); }
2391 
2392  template<typename _Tp>
2393  inline std::complex<_Tp>
2394  atanh(const std::complex<_Tp>& __z)
2395  { return __complex_atanh(__z.__rep()); }
2396 #else
2397  /// atanh(__z) [8.1.7].
2398  // Effects: Behaves the same as C99 function catanh, defined
2399  // in subclause 7.3.6.3.
2400  template<typename _Tp>
2401  inline std::complex<_Tp>
2402  atanh(const std::complex<_Tp>& __z)
2403  { return __complex_atanh(__z); }
2404 #endif
2405 
2406  template<typename _Tp>
2407  inline _Tp
2408  /// fabs(__z) [8.1.8].
2409  // Effects: Behaves the same as C99 function cabs, defined
2410  // in subclause 7.3.8.1.
2411  fabs(const std::complex<_Tp>& __z)
2412  { return std::abs(__z); }
2413 
2414  /// Additional overloads [8.1.9].
2415  template<typename _Tp>
2416  inline typename __gnu_cxx::__promote<_Tp>::__type
2417  arg(_Tp __x)
2418  {
2419  typedef typename __gnu_cxx::__promote<_Tp>::__type __type;
2420 #if (_GLIBCXX11_USE_C99_MATH && !_GLIBCXX_USE_C99_FP_MACROS_DYNAMIC)
2421  return std::signbit(__x) ? __type(3.1415926535897932384626433832795029L)
2422  : __type();
2423 #else
2424  return std::arg(std::complex<__type>(__x));
2425 #endif
2426  }
2427 
2428  template<typename _Tp>
2429  _GLIBCXX_CONSTEXPR inline typename __gnu_cxx::__promote<_Tp>::__type
2430  imag(_Tp)
2431  { return _Tp(); }
2432 
2433  template<typename _Tp>
2434  _GLIBCXX20_CONSTEXPR inline typename __gnu_cxx::__promote<_Tp>::__type
2435  norm(_Tp __x)
2436  {
2437  typedef typename __gnu_cxx::__promote<_Tp>::__type __type;
2438  return __type(__x) * __type(__x);
2439  }
2440 
2441  template<typename _Tp>
2442  _GLIBCXX_CONSTEXPR inline typename __gnu_cxx::__promote<_Tp>::__type
2443  real(_Tp __x)
2444  { return __x; }
2445 
2446  template<typename _Tp, typename _Up>
2447  inline std::complex<typename __gnu_cxx::__promote_2<_Tp, _Up>::__type>
2448  pow(const std::complex<_Tp>& __x, const _Up& __y)
2449  {
2450  typedef typename __gnu_cxx::__promote_2<_Tp, _Up>::__type __type;
2451  return std::pow(std::complex<__type>(__x), __type(__y));
2452  }
2453 
2454  template<typename _Tp, typename _Up>
2455  inline std::complex<typename __gnu_cxx::__promote_2<_Tp, _Up>::__type>
2456  pow(const _Tp& __x, const std::complex<_Up>& __y)
2457  {
2458  typedef typename __gnu_cxx::__promote_2<_Tp, _Up>::__type __type;
2459  return std::pow(__type(__x), std::complex<__type>(__y));
2460  }
2461 
2462  template<typename _Tp, typename _Up>
2463  inline std::complex<typename __gnu_cxx::__promote_2<_Tp, _Up>::__type>
2464  pow(const std::complex<_Tp>& __x, const std::complex<_Up>& __y)
2465  {
2466  typedef typename __gnu_cxx::__promote_2<_Tp, _Up>::__type __type;
2467  return std::pow(std::complex<__type>(__x),
2468  std::complex<__type>(__y));
2469  }
2470 
2471  // Forward declarations.
2472  // DR 781.
2473  template<typename _Tp>
2474  std::complex<_Tp> proj(const std::complex<_Tp>&);
2475 
2476  // Generic implementation of std::proj, does not work for infinities.
2477  template<typename _Tp>
2478  inline std::complex<_Tp>
2479  __complex_proj(const std::complex<_Tp>& __z)
2480  { return __z; }
2481 
2482 #if _GLIBCXX_USE_C99_COMPLEX
2483  inline complex<float>
2484  __complex_proj(const complex<float>& __z)
2485  { return __builtin_cprojf(__z.__rep()); }
2486 
2487  inline complex<double>
2488  __complex_proj(const complex<double>& __z)
2489  { return __builtin_cproj(__z.__rep()); }
2490 
2491  inline complex<long double>
2492  __complex_proj(const complex<long double>& __z)
2493  { return __builtin_cprojl(__z.__rep()); }
2494 
2495 #if __cplusplus > 202002L
2496 #if defined(__STDCPP_FLOAT16_T__) && defined(_GLIBCXX_FLOAT_IS_IEEE_BINARY32)
2497  inline __complex__ _Float16
2498  __complex_proj(__complex__ _Float16 __z)
2499  { return static_cast<__complex__ _Float16>(__builtin_cprojf(__z)); }
2500 #endif
2501 
2502 #if defined(__STDCPP_FLOAT32_T__) && defined(_GLIBCXX_FLOAT_IS_IEEE_BINARY32)
2503  inline __complex__ _Float32
2504  __complex_proj(__complex__ _Float32 __z)
2505  { return __builtin_cprojf(__z); }
2506 #endif
2507 
2508 #if defined(__STDCPP_FLOAT64_T__) && defined(_GLIBCXX_DOUBLE_IS_IEEE_BINARY64)
2509  inline __complex__ _Float64
2510  __complex_proj(__complex__ _Float64 __z)
2511  { return __builtin_cproj(__z); }
2512 #endif
2513 
2514 #if defined(__STDCPP_FLOAT128_T__) && defined(_GLIBCXX_LDOUBLE_IS_IEEE_BINARY128)
2515  inline __complex__ _Float128
2516  __complex_proj(__complex__ _Float128 __z)
2517  { return __builtin_cprojl(__z); }
2518 #elif defined(__STDCPP_FLOAT128_T__) && defined(_GLIBCXX_HAVE_FLOAT128_MATH)
2519  inline __complex__ _Float128
2520  __complex_proj(__complex__ _Float128 __z)
2521  { return __builtin_cprojf128(__z); }
2522 #endif
2523 
2524 #if defined(__STDCPP_BFLOAT16_T__) && defined(_GLIBCXX_FLOAT_IS_IEEE_BINARY32)
2525  inline __complex__ decltype(0.0bf16)
2526  __complex_proj(__complex__ decltype(0.0bf16) __z)
2527  { return static_cast<__complex__ decltype(0.0bf16)>(__builtin_cprojf(__z)); }
2528 #endif
2529 
2530  template<typename _Tp>
2531  requires requires { typename __complex_type<_Tp>::type; }
2532  inline complex<_Tp>
2533  __complex_proj(const complex<_Tp>& __z)
2534  { return __complex_proj(__z.__rep()); }
2535 #endif
2536 
2537 #elif defined _GLIBCXX_USE_C99_MATH_TR1
2538  inline complex<float>
2539  __complex_proj(const complex<float>& __z)
2540  {
2541  if (__builtin_isinf(__z.real()) || __builtin_isinf(__z.imag()))
2542  return complex<float>(__builtin_inff(),
2543  __builtin_copysignf(0.0f, __z.imag()));
2544  return __z;
2545  }
2546 
2547  inline complex<double>
2548  __complex_proj(const complex<double>& __z)
2549  {
2550  if (__builtin_isinf(__z.real()) || __builtin_isinf(__z.imag()))
2551  return complex<double>(__builtin_inf(),
2552  __builtin_copysign(0.0, __z.imag()));
2553  return __z;
2554  }
2555 
2556  inline complex<long double>
2557  __complex_proj(const complex<long double>& __z)
2558  {
2559  if (__builtin_isinf(__z.real()) || __builtin_isinf(__z.imag()))
2560  return complex<long double>(__builtin_infl(),
2561  __builtin_copysignl(0.0l, __z.imag()));
2562  return __z;
2563  }
2564 #endif
2565 
2566  template<typename _Tp>
2567  inline std::complex<_Tp>
2568  proj(const std::complex<_Tp>& __z)
2569  { return __complex_proj(__z); }
2570 
2571  // Overload for scalars
2572  template<typename _Tp>
2573  inline std::complex<typename __gnu_cxx::__promote<_Tp>::__type>
2574  proj(_Tp __x)
2575  {
2576  typedef typename __gnu_cxx::__promote<_Tp>::__type __type;
2577  return std::proj(std::complex<__type>(__x));
2578  }
2579 
2580  template<typename _Tp>
2581  inline _GLIBCXX20_CONSTEXPR
2582  std::complex<typename __gnu_cxx::__promote<_Tp>::__type>
2583  conj(_Tp __x)
2584  {
2585  typedef typename __gnu_cxx::__promote<_Tp>::__type __type;
2586  return std::complex<__type>(__x, -__type());
2587  }
2588 
2589 #if __cplusplus > 201103L
2590 
2591 inline namespace literals {
2592 inline namespace complex_literals {
2593 #pragma GCC diagnostic push
2594 #pragma GCC diagnostic ignored "-Wliteral-suffix"
2595 #define __cpp_lib_complex_udls 201309L
2596 
2597  constexpr std::complex<float>
2598  operator""if(long double __num)
2599  { return std::complex<float>{0.0F, static_cast<float>(__num)}; }
2600 
2601  constexpr std::complex<float>
2602  operator""if(unsigned long long __num)
2603  { return std::complex<float>{0.0F, static_cast<float>(__num)}; }
2604 
2605  constexpr std::complex<double>
2606  operator""i(long double __num)
2607  { return std::complex<double>{0.0, static_cast<double>(__num)}; }
2608 
2609  constexpr std::complex<double>
2610  operator""i(unsigned long long __num)
2611  { return std::complex<double>{0.0, static_cast<double>(__num)}; }
2612 
2613  constexpr std::complex<long double>
2614  operator""il(long double __num)
2615  { return std::complex<long double>{0.0L, __num}; }
2616 
2617  constexpr std::complex<long double>
2618  operator""il(unsigned long long __num)
2619  { return std::complex<long double>{0.0L, static_cast<long double>(__num)}; }
2620 
2621 #pragma GCC diagnostic pop
2622 } // inline namespace complex_literals
2623 } // inline namespace literals
2624 
2625 #endif // C++14
2626 
2627 _GLIBCXX_END_NAMESPACE_VERSION
2628 } // namespace
2629 
2630 #endif // C++11
2631 
2632 #endif /* _GLIBCXX_COMPLEX */