1 // The template and inlines for the -*- C++ -*- complex number classes.
3 // Copyright (C) 1997-2018 Free Software Foundation, Inc.
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)
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.
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.
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/>.
25 /** @file include/complex
26 * This is a Standard C++ Library header.
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>
36 #ifndef _GLIBCXX_COMPLEX
37 #define _GLIBCXX_COMPLEX 1
39 #pragma GCC system_header
41 #include <bits/c++config.h>
42 #include <bits/cpp_type_traits.h>
43 #include <ext/type_traits.h>
47 // Get rid of a macro possibly defined in <complex.h>
50 namespace std _GLIBCXX_VISIBILITY(default)
52 _GLIBCXX_BEGIN_NAMESPACE_VERSION
55 * @defgroup complex_numbers Complex Numbers
58 * Classes and functions for complex numbers.
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>;
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>&);
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);
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>&,
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>&);
112 // 26.2.2 Primary template class complex
114 * Template to represent complex numbers.
116 * Specializations for float, double, and long double are part of the
117 * library. Results with any other type are not guaranteed.
119 * @param Tp Type of real and imaginary values.
121 template<typename _Tp>
125 typedef _Tp value_type;
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) { }
132 // Let the compiler synthesize the copy constructor
133 #if __cplusplus >= 201103L
134 constexpr complex(const complex&) = default;
137 /// Converting constructor.
138 template<typename _Up>
139 _GLIBCXX_CONSTEXPR complex(const complex<_Up>& __z)
140 : _M_real(__z.real()), _M_imag(__z.imag()) { }
142 #if __cplusplus >= 201103L
143 // _GLIBCXX_RESOLVE_LIB_DEFECTS
144 // DR 387. std::complex over-encapsulated.
145 _GLIBCXX_ABI_TAG_CXX11
147 real() const { return _M_real; }
149 _GLIBCXX_ABI_TAG_CXX11
151 imag() const { return _M_imag; }
153 /// Return real part of complex number.
155 real() { return _M_real; }
157 /// Return real part of complex number.
159 real() const { return _M_real; }
161 /// Return imaginary part of complex number.
163 imag() { return _M_imag; }
165 /// Return imaginary part of complex number.
167 imag() const { return _M_imag; }
170 // _GLIBCXX_RESOLVE_LIB_DEFECTS
171 // DR 387. std::complex over-encapsulated.
173 real(_Tp __val) { _M_real = __val; }
176 imag(_Tp __val) { _M_imag = __val; }
178 /// Assign a scalar to this complex number.
179 complex<_Tp>& operator=(const _Tp&);
181 /// Add a scalar to this complex number.
184 operator+=(const _Tp& __t)
190 /// Subtract a scalar from this complex number.
193 operator-=(const _Tp& __t)
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&);
204 // Let the compiler synthesize the copy assignment operator
205 #if __cplusplus >= 201103L
206 complex& operator=(const complex&) = default;
209 /// Assign another complex number to this one.
210 template<typename _Up>
211 complex<_Tp>& operator=(const complex<_Up>&);
212 /// Add another complex number to this one.
213 template<typename _Up>
214 complex<_Tp>& operator+=(const complex<_Up>&);
215 /// Subtract another complex number from this one.
216 template<typename _Up>
217 complex<_Tp>& operator-=(const complex<_Up>&);
218 /// Multiply this complex number by another.
219 template<typename _Up>
220 complex<_Tp>& operator*=(const complex<_Up>&);
221 /// Divide this complex number by another.
222 template<typename _Up>
223 complex<_Tp>& operator/=(const complex<_Up>&);
225 _GLIBCXX_CONSTEXPR complex __rep() const
233 template<typename _Tp>
235 complex<_Tp>::operator=(const _Tp& __t)
243 template<typename _Tp>
245 complex<_Tp>::operator*=(const _Tp& __t)
253 template<typename _Tp>
255 complex<_Tp>::operator/=(const _Tp& __t)
262 template<typename _Tp>
263 template<typename _Up>
265 complex<_Tp>::operator=(const complex<_Up>& __z)
267 _M_real = __z.real();
268 _M_imag = __z.imag();
273 template<typename _Tp>
274 template<typename _Up>
276 complex<_Tp>::operator+=(const complex<_Up>& __z)
278 _M_real += __z.real();
279 _M_imag += __z.imag();
284 template<typename _Tp>
285 template<typename _Up>
287 complex<_Tp>::operator-=(const complex<_Up>& __z)
289 _M_real -= __z.real();
290 _M_imag -= __z.imag();
295 // XXX: This is a grammar school implementation.
296 template<typename _Tp>
297 template<typename _Up>
299 complex<_Tp>::operator*=(const complex<_Up>& __z)
301 const _Tp __r = _M_real * __z.real() - _M_imag * __z.imag();
302 _M_imag = _M_real * __z.imag() + _M_imag * __z.real();
308 // XXX: This is a grammar school implementation.
309 template<typename _Tp>
310 template<typename _Up>
312 complex<_Tp>::operator/=(const complex<_Up>& __z)
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;
323 /// Return new complex value @a x plus @a y.
324 template<typename _Tp>
326 operator+(const complex<_Tp>& __x, const complex<_Tp>& __y)
328 complex<_Tp> __r = __x;
333 template<typename _Tp>
335 operator+(const complex<_Tp>& __x, const _Tp& __y)
337 complex<_Tp> __r = __x;
342 template<typename _Tp>
344 operator+(const _Tp& __x, const complex<_Tp>& __y)
346 complex<_Tp> __r = __y;
353 /// Return new complex value @a x minus @a y.
354 template<typename _Tp>
356 operator-(const complex<_Tp>& __x, const complex<_Tp>& __y)
358 complex<_Tp> __r = __x;
363 template<typename _Tp>
365 operator-(const complex<_Tp>& __x, const _Tp& __y)
367 complex<_Tp> __r = __x;
372 template<typename _Tp>
374 operator-(const _Tp& __x, const complex<_Tp>& __y)
376 complex<_Tp> __r(__x, -__y.imag());
383 /// Return new complex value @a x times @a y.
384 template<typename _Tp>
386 operator*(const complex<_Tp>& __x, const complex<_Tp>& __y)
388 complex<_Tp> __r = __x;
393 template<typename _Tp>
395 operator*(const complex<_Tp>& __x, const _Tp& __y)
397 complex<_Tp> __r = __x;
402 template<typename _Tp>
404 operator*(const _Tp& __x, const complex<_Tp>& __y)
406 complex<_Tp> __r = __y;
413 /// Return new complex value @a x divided by @a y.
414 template<typename _Tp>
416 operator/(const complex<_Tp>& __x, const complex<_Tp>& __y)
418 complex<_Tp> __r = __x;
423 template<typename _Tp>
425 operator/(const complex<_Tp>& __x, const _Tp& __y)
427 complex<_Tp> __r = __x;
432 template<typename _Tp>
434 operator/(const _Tp& __x, const complex<_Tp>& __y)
436 complex<_Tp> __r = __x;
443 template<typename _Tp>
445 operator+(const complex<_Tp>& __x)
448 /// Return complex negation of @a x.
449 template<typename _Tp>
451 operator-(const complex<_Tp>& __x)
452 { return complex<_Tp>(-__x.real(), -__x.imag()); }
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(); }
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(); }
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(); }
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(); }
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(); }
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(); }
490 /// Extraction operator for complex values.
491 template<typename _Tp, typename _CharT, class _Traits>
492 basic_istream<_CharT, _Traits>&
493 operator>>(basic_istream<_CharT, _Traits>& __is, complex<_Tp>& __x)
499 if (_Traits::eq(__ch, __is.widen('(')))
502 if (__is >> __u >> __ch)
504 const _CharT __rparen = __is.widen(')');
505 if (_Traits::eq(__ch, __rparen))
510 else if (_Traits::eq(__ch, __is.widen(',')))
513 if (__is >> __v >> __ch)
515 if (_Traits::eq(__ch, __rparen))
517 __x = complex<_Tp>(__u, __v);
540 __is.setstate(ios_base::failbit);
544 /// Insertion operator for complex values.
545 template<typename _Tp, typename _CharT, class _Traits>
546 basic_ostream<_CharT, _Traits>&
547 operator<<(basic_ostream<_CharT, _Traits>& __os, const complex<_Tp>& __x)
549 basic_ostringstream<_CharT, _Traits> __s;
550 __s.flags(__os.flags());
551 __s.imbue(__os.getloc());
552 __s.precision(__os.precision());
553 __s << '(' << __x.real() << ',' << __x.imag() << ')';
554 return __os << __s.str();
558 #if __cplusplus >= 201103L
559 template<typename _Tp>
561 real(const complex<_Tp>& __z)
562 { return __z.real(); }
564 template<typename _Tp>
566 imag(const complex<_Tp>& __z)
567 { return __z.imag(); }
569 template<typename _Tp>
571 real(complex<_Tp>& __z)
572 { return __z.real(); }
574 template<typename _Tp>
576 real(const complex<_Tp>& __z)
577 { return __z.real(); }
579 template<typename _Tp>
581 imag(complex<_Tp>& __z)
582 { return __z.imag(); }
584 template<typename _Tp>
586 imag(const complex<_Tp>& __z)
587 { return __z.imag(); }
590 // 26.2.7/3 abs(__z): Returns the magnitude of __z.
591 template<typename _Tp>
593 __complex_abs(const complex<_Tp>& __z)
595 _Tp __x = __z.real();
596 _Tp __y = __z.imag();
597 const _Tp __s = std::max(abs(__x), abs(__y));
598 if (__s == _Tp()) // well ...
602 return __s * sqrt(__x * __x + __y * __y);
605 #if _GLIBCXX_USE_C99_COMPLEX
607 __complex_abs(__complex__ float __z) { return __builtin_cabsf(__z); }
610 __complex_abs(__complex__ double __z) { return __builtin_cabs(__z); }
613 __complex_abs(const __complex__ long double& __z)
614 { return __builtin_cabsl(__z); }
616 template<typename _Tp>
618 abs(const complex<_Tp>& __z) { return __complex_abs(__z.__rep()); }
620 template<typename _Tp>
622 abs(const complex<_Tp>& __z) { return __complex_abs(__z); }
626 // 26.2.7/4: arg(__z): Returns the phase angle of __z.
627 template<typename _Tp>
629 __complex_arg(const complex<_Tp>& __z)
630 { return atan2(__z.imag(), __z.real()); }
632 #if _GLIBCXX_USE_C99_COMPLEX
634 __complex_arg(__complex__ float __z) { return __builtin_cargf(__z); }
637 __complex_arg(__complex__ double __z) { return __builtin_carg(__z); }
640 __complex_arg(const __complex__ long double& __z)
641 { return __builtin_cargl(__z); }
643 template<typename _Tp>
645 arg(const complex<_Tp>& __z) { return __complex_arg(__z.__rep()); }
647 template<typename _Tp>
649 arg(const complex<_Tp>& __z) { return __complex_arg(__z); }
652 // 26.2.7/5: norm(__z) returns the squared magnitude of __z.
653 // As defined, norm() is -not- a norm is the common mathematical
654 // sense used in numerics. The helper class _Norm_helper<> tries to
655 // distinguish between builtin floating point and the rest, so as
656 // to deliver an answer as close as possible to the real value.
660 template<typename _Tp>
661 static inline _Tp _S_do_it(const complex<_Tp>& __z)
663 const _Tp __x = __z.real();
664 const _Tp __y = __z.imag();
665 return __x * __x + __y * __y;
670 struct _Norm_helper<true>
672 template<typename _Tp>
673 static inline _Tp _S_do_it(const complex<_Tp>& __z)
675 _Tp __res = std::abs(__z);
676 return __res * __res;
680 template<typename _Tp>
682 norm(const complex<_Tp>& __z)
684 return _Norm_helper<__is_floating<_Tp>::__value
685 && !_GLIBCXX_FAST_MATH>::_S_do_it(__z);
688 template<typename _Tp>
690 polar(const _Tp& __rho, const _Tp& __theta)
692 __glibcxx_assert( __rho >= 0 );
693 return complex<_Tp>(__rho * cos(__theta), __rho * sin(__theta));
696 template<typename _Tp>
698 conj(const complex<_Tp>& __z)
699 { return complex<_Tp>(__z.real(), -__z.imag()); }
703 // 26.2.8/1 cos(__z): Returns the cosine of __z.
704 template<typename _Tp>
706 __complex_cos(const complex<_Tp>& __z)
708 const _Tp __x = __z.real();
709 const _Tp __y = __z.imag();
710 return complex<_Tp>(cos(__x) * cosh(__y), -sin(__x) * sinh(__y));
713 #if _GLIBCXX_USE_C99_COMPLEX
714 inline __complex__ float
715 __complex_cos(__complex__ float __z) { return __builtin_ccosf(__z); }
717 inline __complex__ double
718 __complex_cos(__complex__ double __z) { return __builtin_ccos(__z); }
720 inline __complex__ long double
721 __complex_cos(const __complex__ long double& __z)
722 { return __builtin_ccosl(__z); }
724 template<typename _Tp>
726 cos(const complex<_Tp>& __z) { return __complex_cos(__z.__rep()); }
728 template<typename _Tp>
730 cos(const complex<_Tp>& __z) { return __complex_cos(__z); }
733 // 26.2.8/2 cosh(__z): Returns the hyperbolic cosine of __z.
734 template<typename _Tp>
736 __complex_cosh(const complex<_Tp>& __z)
738 const _Tp __x = __z.real();
739 const _Tp __y = __z.imag();
740 return complex<_Tp>(cosh(__x) * cos(__y), sinh(__x) * sin(__y));
743 #if _GLIBCXX_USE_C99_COMPLEX
744 inline __complex__ float
745 __complex_cosh(__complex__ float __z) { return __builtin_ccoshf(__z); }
747 inline __complex__ double
748 __complex_cosh(__complex__ double __z) { return __builtin_ccosh(__z); }
750 inline __complex__ long double
751 __complex_cosh(const __complex__ long double& __z)
752 { return __builtin_ccoshl(__z); }
754 template<typename _Tp>
756 cosh(const complex<_Tp>& __z) { return __complex_cosh(__z.__rep()); }
758 template<typename _Tp>
760 cosh(const complex<_Tp>& __z) { return __complex_cosh(__z); }
763 // 26.2.8/3 exp(__z): Returns the complex base e exponential of x
764 template<typename _Tp>
766 __complex_exp(const complex<_Tp>& __z)
767 { return std::polar<_Tp>(exp(__z.real()), __z.imag()); }
769 #if _GLIBCXX_USE_C99_COMPLEX
770 inline __complex__ float
771 __complex_exp(__complex__ float __z) { return __builtin_cexpf(__z); }
773 inline __complex__ double
774 __complex_exp(__complex__ double __z) { return __builtin_cexp(__z); }
776 inline __complex__ long double
777 __complex_exp(const __complex__ long double& __z)
778 { return __builtin_cexpl(__z); }
780 template<typename _Tp>
782 exp(const complex<_Tp>& __z) { return __complex_exp(__z.__rep()); }
784 template<typename _Tp>
786 exp(const complex<_Tp>& __z) { return __complex_exp(__z); }
789 // 26.2.8/5 log(__z): Returns the natural complex logarithm of __z.
790 // The branch cut is along the negative axis.
791 template<typename _Tp>
793 __complex_log(const complex<_Tp>& __z)
794 { return complex<_Tp>(log(std::abs(__z)), std::arg(__z)); }
796 #if _GLIBCXX_USE_C99_COMPLEX
797 inline __complex__ float
798 __complex_log(__complex__ float __z) { return __builtin_clogf(__z); }
800 inline __complex__ double
801 __complex_log(__complex__ double __z) { return __builtin_clog(__z); }
803 inline __complex__ long double
804 __complex_log(const __complex__ long double& __z)
805 { return __builtin_clogl(__z); }
807 template<typename _Tp>
809 log(const complex<_Tp>& __z) { return __complex_log(__z.__rep()); }
811 template<typename _Tp>
813 log(const complex<_Tp>& __z) { return __complex_log(__z); }
816 template<typename _Tp>
818 log10(const complex<_Tp>& __z)
819 { return std::log(__z) / log(_Tp(10.0)); }
821 // 26.2.8/10 sin(__z): Returns the sine of __z.
822 template<typename _Tp>
824 __complex_sin(const complex<_Tp>& __z)
826 const _Tp __x = __z.real();
827 const _Tp __y = __z.imag();
828 return complex<_Tp>(sin(__x) * cosh(__y), cos(__x) * sinh(__y));
831 #if _GLIBCXX_USE_C99_COMPLEX
832 inline __complex__ float
833 __complex_sin(__complex__ float __z) { return __builtin_csinf(__z); }
835 inline __complex__ double
836 __complex_sin(__complex__ double __z) { return __builtin_csin(__z); }
838 inline __complex__ long double
839 __complex_sin(const __complex__ long double& __z)
840 { return __builtin_csinl(__z); }
842 template<typename _Tp>
844 sin(const complex<_Tp>& __z) { return __complex_sin(__z.__rep()); }
846 template<typename _Tp>
848 sin(const complex<_Tp>& __z) { return __complex_sin(__z); }
851 // 26.2.8/11 sinh(__z): Returns the hyperbolic sine of __z.
852 template<typename _Tp>
854 __complex_sinh(const complex<_Tp>& __z)
856 const _Tp __x = __z.real();
857 const _Tp __y = __z.imag();
858 return complex<_Tp>(sinh(__x) * cos(__y), cosh(__x) * sin(__y));
861 #if _GLIBCXX_USE_C99_COMPLEX
862 inline __complex__ float
863 __complex_sinh(__complex__ float __z) { return __builtin_csinhf(__z); }
865 inline __complex__ double
866 __complex_sinh(__complex__ double __z) { return __builtin_csinh(__z); }
868 inline __complex__ long double
869 __complex_sinh(const __complex__ long double& __z)
870 { return __builtin_csinhl(__z); }
872 template<typename _Tp>
874 sinh(const complex<_Tp>& __z) { return __complex_sinh(__z.__rep()); }
876 template<typename _Tp>
878 sinh(const complex<_Tp>& __z) { return __complex_sinh(__z); }
881 // 26.2.8/13 sqrt(__z): Returns the complex square root of __z.
882 // The branch cut is on the negative axis.
883 template<typename _Tp>
885 __complex_sqrt(const complex<_Tp>& __z)
887 _Tp __x = __z.real();
888 _Tp __y = __z.imag();
892 _Tp __t = sqrt(abs(__y) / 2);
893 return complex<_Tp>(__t, __y < _Tp() ? -__t : __t);
897 _Tp __t = sqrt(2 * (std::abs(__z) + abs(__x)));
900 ? complex<_Tp>(__u, __y / __t)
901 : complex<_Tp>(abs(__y) / __t, __y < _Tp() ? -__u : __u);
905 #if _GLIBCXX_USE_C99_COMPLEX
906 inline __complex__ float
907 __complex_sqrt(__complex__ float __z) { return __builtin_csqrtf(__z); }
909 inline __complex__ double
910 __complex_sqrt(__complex__ double __z) { return __builtin_csqrt(__z); }
912 inline __complex__ long double
913 __complex_sqrt(const __complex__ long double& __z)
914 { return __builtin_csqrtl(__z); }
916 template<typename _Tp>
918 sqrt(const complex<_Tp>& __z) { return __complex_sqrt(__z.__rep()); }
920 template<typename _Tp>
922 sqrt(const complex<_Tp>& __z) { return __complex_sqrt(__z); }
925 // 26.2.8/14 tan(__z): Return the complex tangent of __z.
927 template<typename _Tp>
929 __complex_tan(const complex<_Tp>& __z)
930 { return std::sin(__z) / std::cos(__z); }
932 #if _GLIBCXX_USE_C99_COMPLEX
933 inline __complex__ float
934 __complex_tan(__complex__ float __z) { return __builtin_ctanf(__z); }
936 inline __complex__ double
937 __complex_tan(__complex__ double __z) { return __builtin_ctan(__z); }
939 inline __complex__ long double
940 __complex_tan(const __complex__ long double& __z)
941 { return __builtin_ctanl(__z); }
943 template<typename _Tp>
945 tan(const complex<_Tp>& __z) { return __complex_tan(__z.__rep()); }
947 template<typename _Tp>
949 tan(const complex<_Tp>& __z) { return __complex_tan(__z); }
953 // 26.2.8/15 tanh(__z): Returns the hyperbolic tangent of __z.
955 template<typename _Tp>
957 __complex_tanh(const complex<_Tp>& __z)
958 { return std::sinh(__z) / std::cosh(__z); }
960 #if _GLIBCXX_USE_C99_COMPLEX
961 inline __complex__ float
962 __complex_tanh(__complex__ float __z) { return __builtin_ctanhf(__z); }
964 inline __complex__ double
965 __complex_tanh(__complex__ double __z) { return __builtin_ctanh(__z); }
967 inline __complex__ long double
968 __complex_tanh(const __complex__ long double& __z)
969 { return __builtin_ctanhl(__z); }
971 template<typename _Tp>
973 tanh(const complex<_Tp>& __z) { return __complex_tanh(__z.__rep()); }
975 template<typename _Tp>
977 tanh(const complex<_Tp>& __z) { return __complex_tanh(__z); }
981 // 26.2.8/9 pow(__x, __y): Returns the complex power base of __x
982 // raised to the __y-th power. The branch
983 // cut is on the negative axis.
984 template<typename _Tp>
986 __complex_pow_unsigned(complex<_Tp> __x, unsigned __n)
988 complex<_Tp> __y = __n % 2 ? __x : complex<_Tp>(1);
1000 // In C++11 mode we used to implement the resolution of
1001 // DR 844. complex pow return type is ambiguous.
1002 // thus the following overload was disabled in that mode. However, doing
1003 // that causes all sorts of issues, see, for example:
1004 // http://gcc.gnu.org/ml/libstdc++/2013-01/msg00058.html
1005 // and also PR57974.
1006 template<typename _Tp>
1008 pow(const complex<_Tp>& __z, int __n)
1011 ? complex<_Tp>(1) / std::__complex_pow_unsigned(__z, -(unsigned)__n)
1012 : std::__complex_pow_unsigned(__z, __n);
1015 template<typename _Tp>
1017 pow(const complex<_Tp>& __x, const _Tp& __y)
1019 #if ! _GLIBCXX_USE_C99_COMPLEX
1023 if (__x.imag() == _Tp() && __x.real() > _Tp())
1024 return pow(__x.real(), __y);
1026 complex<_Tp> __t = std::log(__x);
1027 return std::polar<_Tp>(exp(__y * __t.real()), __y * __t.imag());
1030 template<typename _Tp>
1032 __complex_pow(const complex<_Tp>& __x, const complex<_Tp>& __y)
1033 { return __x == _Tp() ? _Tp() : std::exp(__y * std::log(__x)); }
1035 #if _GLIBCXX_USE_C99_COMPLEX
1036 inline __complex__ float
1037 __complex_pow(__complex__ float __x, __complex__ float __y)
1038 { return __builtin_cpowf(__x, __y); }
1040 inline __complex__ double
1041 __complex_pow(__complex__ double __x, __complex__ double __y)
1042 { return __builtin_cpow(__x, __y); }
1044 inline __complex__ long double
1045 __complex_pow(const __complex__ long double& __x,
1046 const __complex__ long double& __y)
1047 { return __builtin_cpowl(__x, __y); }
1049 template<typename _Tp>
1051 pow(const complex<_Tp>& __x, const complex<_Tp>& __y)
1052 { return __complex_pow(__x.__rep(), __y.__rep()); }
1054 template<typename _Tp>
1056 pow(const complex<_Tp>& __x, const complex<_Tp>& __y)
1057 { return __complex_pow(__x, __y); }
1060 template<typename _Tp>
1062 pow(const _Tp& __x, const complex<_Tp>& __y)
1064 return __x > _Tp() ? std::polar<_Tp>(pow(__x, __y.real()),
1065 __y.imag() * log(__x))
1066 : std::pow(complex<_Tp>(__x), __y);
1069 /// 26.2.3 complex specializations
1070 /// complex<float> specialization
1072 struct complex<float>
1074 typedef float value_type;
1075 typedef __complex__ float _ComplexT;
1077 _GLIBCXX_CONSTEXPR complex(_ComplexT __z) : _M_value(__z) { }
1079 _GLIBCXX_CONSTEXPR complex(float __r = 0.0f, float __i = 0.0f)
1080 #if __cplusplus >= 201103L
1081 : _M_value{ __r, __i } { }
1084 __real__ _M_value = __r;
1085 __imag__ _M_value = __i;
1089 explicit _GLIBCXX_CONSTEXPR complex(const complex<double>&);
1090 explicit _GLIBCXX_CONSTEXPR complex(const complex<long double>&);
1092 #if __cplusplus >= 201103L
1093 // _GLIBCXX_RESOLVE_LIB_DEFECTS
1094 // DR 387. std::complex over-encapsulated.
1095 __attribute ((__abi_tag__ ("cxx11")))
1097 real() const { return __real__ _M_value; }
1099 __attribute ((__abi_tag__ ("cxx11")))
1101 imag() const { return __imag__ _M_value; }
1104 real() { return __real__ _M_value; }
1107 real() const { return __real__ _M_value; }
1110 imag() { return __imag__ _M_value; }
1113 imag() const { return __imag__ _M_value; }
1116 // _GLIBCXX_RESOLVE_LIB_DEFECTS
1117 // DR 387. std::complex over-encapsulated.
1119 real(float __val) { __real__ _M_value = __val; }
1122 imag(float __val) { __imag__ _M_value = __val; }
1125 operator=(float __f)
1132 operator+=(float __f)
1139 operator-=(float __f)
1146 operator*=(float __f)
1153 operator/=(float __f)
1159 // Let the compiler synthesize the copy and assignment
1160 // operator. It always does a pretty good job.
1161 #if __cplusplus >= 201103L
1162 _GLIBCXX14_CONSTEXPR complex& operator=(const complex&) = default;
1165 template<typename _Tp>
1167 operator=(const complex<_Tp>& __z)
1169 __real__ _M_value = __z.real();
1170 __imag__ _M_value = __z.imag();
1174 template<typename _Tp>
1176 operator+=(const complex<_Tp>& __z)
1178 __real__ _M_value += __z.real();
1179 __imag__ _M_value += __z.imag();
1185 operator-=(const complex<_Tp>& __z)
1187 __real__ _M_value -= __z.real();
1188 __imag__ _M_value -= __z.imag();
1194 operator*=(const complex<_Tp>& __z)
1197 __real__ __t = __z.real();
1198 __imag__ __t = __z.imag();
1205 operator/=(const complex<_Tp>& __z)
1208 __real__ __t = __z.real();
1209 __imag__ __t = __z.imag();
1214 _GLIBCXX_CONSTEXPR _ComplexT __rep() const { return _M_value; }
1220 /// 26.2.3 complex specializations
1221 /// complex<double> specialization
1223 struct complex<double>
1225 typedef double value_type;
1226 typedef __complex__ double _ComplexT;
1228 _GLIBCXX_CONSTEXPR complex(_ComplexT __z) : _M_value(__z) { }
1230 _GLIBCXX_CONSTEXPR complex(double __r = 0.0, double __i = 0.0)
1231 #if __cplusplus >= 201103L
1232 : _M_value{ __r, __i } { }
1235 __real__ _M_value = __r;
1236 __imag__ _M_value = __i;
1240 _GLIBCXX_CONSTEXPR complex(const complex<float>& __z)
1241 : _M_value(__z.__rep()) { }
1243 explicit _GLIBCXX_CONSTEXPR complex(const complex<long double>&);
1245 #if __cplusplus >= 201103L
1246 // _GLIBCXX_RESOLVE_LIB_DEFECTS
1247 // DR 387. std::complex over-encapsulated.
1248 __attribute ((__abi_tag__ ("cxx11")))
1250 real() const { return __real__ _M_value; }
1252 __attribute ((__abi_tag__ ("cxx11")))
1254 imag() const { return __imag__ _M_value; }
1257 real() { return __real__ _M_value; }
1260 real() const { return __real__ _M_value; }
1263 imag() { return __imag__ _M_value; }
1266 imag() const { return __imag__ _M_value; }
1269 // _GLIBCXX_RESOLVE_LIB_DEFECTS
1270 // DR 387. std::complex over-encapsulated.
1272 real(double __val) { __real__ _M_value = __val; }
1275 imag(double __val) { __imag__ _M_value = __val; }
1278 operator=(double __d)
1285 operator+=(double __d)
1292 operator-=(double __d)
1299 operator*=(double __d)
1306 operator/=(double __d)
1312 // The compiler will synthesize this, efficiently.
1313 #if __cplusplus >= 201103L
1314 _GLIBCXX14_CONSTEXPR complex& operator=(const complex&) = default;
1317 template<typename _Tp>
1319 operator=(const complex<_Tp>& __z)
1321 __real__ _M_value = __z.real();
1322 __imag__ _M_value = __z.imag();
1326 template<typename _Tp>
1328 operator+=(const complex<_Tp>& __z)
1330 __real__ _M_value += __z.real();
1331 __imag__ _M_value += __z.imag();
1335 template<typename _Tp>
1337 operator-=(const complex<_Tp>& __z)
1339 __real__ _M_value -= __z.real();
1340 __imag__ _M_value -= __z.imag();
1344 template<typename _Tp>
1346 operator*=(const complex<_Tp>& __z)
1349 __real__ __t = __z.real();
1350 __imag__ __t = __z.imag();
1355 template<typename _Tp>
1357 operator/=(const complex<_Tp>& __z)
1360 __real__ __t = __z.real();
1361 __imag__ __t = __z.imag();
1366 _GLIBCXX_CONSTEXPR _ComplexT __rep() const { return _M_value; }
1372 /// 26.2.3 complex specializations
1373 /// complex<long double> specialization
1375 struct complex<long double>
1377 typedef long double value_type;
1378 typedef __complex__ long double _ComplexT;
1380 _GLIBCXX_CONSTEXPR complex(_ComplexT __z) : _M_value(__z) { }
1382 _GLIBCXX_CONSTEXPR complex(long double __r = 0.0L,
1383 long double __i = 0.0L)
1384 #if __cplusplus >= 201103L
1385 : _M_value{ __r, __i } { }
1388 __real__ _M_value = __r;
1389 __imag__ _M_value = __i;
1393 _GLIBCXX_CONSTEXPR complex(const complex<float>& __z)
1394 : _M_value(__z.__rep()) { }
1396 _GLIBCXX_CONSTEXPR complex(const complex<double>& __z)
1397 : _M_value(__z.__rep()) { }
1399 #if __cplusplus >= 201103L
1400 // _GLIBCXX_RESOLVE_LIB_DEFECTS
1401 // DR 387. std::complex over-encapsulated.
1402 __attribute ((__abi_tag__ ("cxx11")))
1403 constexpr long double
1404 real() const { return __real__ _M_value; }
1406 __attribute ((__abi_tag__ ("cxx11")))
1407 constexpr long double
1408 imag() const { return __imag__ _M_value; }
1411 real() { return __real__ _M_value; }
1414 real() const { return __real__ _M_value; }
1417 imag() { return __imag__ _M_value; }
1420 imag() const { return __imag__ _M_value; }
1423 // _GLIBCXX_RESOLVE_LIB_DEFECTS
1424 // DR 387. std::complex over-encapsulated.
1426 real(long double __val) { __real__ _M_value = __val; }
1429 imag(long double __val) { __imag__ _M_value = __val; }
1432 operator=(long double __r)
1439 operator+=(long double __r)
1446 operator-=(long double __r)
1453 operator*=(long double __r)
1460 operator/=(long double __r)
1466 // The compiler knows how to do this efficiently
1467 #if __cplusplus >= 201103L
1468 _GLIBCXX14_CONSTEXPR complex& operator=(const complex&) = default;
1471 template<typename _Tp>
1473 operator=(const complex<_Tp>& __z)
1475 __real__ _M_value = __z.real();
1476 __imag__ _M_value = __z.imag();
1480 template<typename _Tp>
1482 operator+=(const complex<_Tp>& __z)
1484 __real__ _M_value += __z.real();
1485 __imag__ _M_value += __z.imag();
1489 template<typename _Tp>
1491 operator-=(const complex<_Tp>& __z)
1493 __real__ _M_value -= __z.real();
1494 __imag__ _M_value -= __z.imag();
1498 template<typename _Tp>
1500 operator*=(const complex<_Tp>& __z)
1503 __real__ __t = __z.real();
1504 __imag__ __t = __z.imag();
1509 template<typename _Tp>
1511 operator/=(const complex<_Tp>& __z)
1514 __real__ __t = __z.real();
1515 __imag__ __t = __z.imag();
1520 _GLIBCXX_CONSTEXPR _ComplexT __rep() const { return _M_value; }
1526 // These bits have to be at the end of this file, so that the
1527 // specializations have all been defined.
1528 inline _GLIBCXX_CONSTEXPR
1529 complex<float>::complex(const complex<double>& __z)
1530 : _M_value(__z.__rep()) { }
1532 inline _GLIBCXX_CONSTEXPR
1533 complex<float>::complex(const complex<long double>& __z)
1534 : _M_value(__z.__rep()) { }
1536 inline _GLIBCXX_CONSTEXPR
1537 complex<double>::complex(const complex<long double>& __z)
1538 : _M_value(__z.__rep()) { }
1540 // Inhibit implicit instantiations for required instantiations,
1541 // which are defined via explicit instantiations elsewhere.
1542 // NB: This syntax is a GNU extension.
1543 #if _GLIBCXX_EXTERN_TEMPLATE
1544 extern template istream& operator>>(istream&, complex<float>&);
1545 extern template ostream& operator<<(ostream&, const complex<float>&);
1546 extern template istream& operator>>(istream&, complex<double>&);
1547 extern template ostream& operator<<(ostream&, const complex<double>&);
1548 extern template istream& operator>>(istream&, complex<long double>&);
1549 extern template ostream& operator<<(ostream&, const complex<long double>&);
1551 #ifdef _GLIBCXX_USE_WCHAR_T
1552 extern template wistream& operator>>(wistream&, complex<float>&);
1553 extern template wostream& operator<<(wostream&, const complex<float>&);
1554 extern template wistream& operator>>(wistream&, complex<double>&);
1555 extern template wostream& operator<<(wostream&, const complex<double>&);
1556 extern template wistream& operator>>(wistream&, complex<long double>&);
1557 extern template wostream& operator<<(wostream&, const complex<long double>&);
1561 // @} group complex_numbers
1563 _GLIBCXX_END_NAMESPACE_VERSION
1566 namespace __gnu_cxx _GLIBCXX_VISIBILITY(default)
1568 _GLIBCXX_BEGIN_NAMESPACE_VERSION
1570 // See ext/type_traits.h for the primary template.
1571 template<typename _Tp, typename _Up>
1572 struct __promote_2<std::complex<_Tp>, _Up>
1575 typedef std::complex<typename __promote_2<_Tp, _Up>::__type> __type;
1578 template<typename _Tp, typename _Up>
1579 struct __promote_2<_Tp, std::complex<_Up> >
1582 typedef std::complex<typename __promote_2<_Tp, _Up>::__type> __type;
1585 template<typename _Tp, typename _Up>
1586 struct __promote_2<std::complex<_Tp>, std::complex<_Up> >
1589 typedef std::complex<typename __promote_2<_Tp, _Up>::__type> __type;
1592 _GLIBCXX_END_NAMESPACE_VERSION
1595 #if __cplusplus >= 201103L
1597 namespace std _GLIBCXX_VISIBILITY(default)
1599 _GLIBCXX_BEGIN_NAMESPACE_VERSION
1601 // Forward declarations.
1602 template<typename _Tp> std::complex<_Tp> acos(const std::complex<_Tp>&);
1603 template<typename _Tp> std::complex<_Tp> asin(const std::complex<_Tp>&);
1604 template<typename _Tp> std::complex<_Tp> atan(const std::complex<_Tp>&);
1606 template<typename _Tp> std::complex<_Tp> acosh(const std::complex<_Tp>&);
1607 template<typename _Tp> std::complex<_Tp> asinh(const std::complex<_Tp>&);
1608 template<typename _Tp> std::complex<_Tp> atanh(const std::complex<_Tp>&);
1610 template<typename _Tp> _Tp fabs(const std::complex<_Tp>&);
1612 template<typename _Tp>
1613 inline std::complex<_Tp>
1614 __complex_acos(const std::complex<_Tp>& __z)
1616 const std::complex<_Tp> __t = std::asin(__z);
1617 const _Tp __pi_2 = 1.5707963267948966192313216916397514L;
1618 return std::complex<_Tp>(__pi_2 - __t.real(), -__t.imag());
1621 #if _GLIBCXX_USE_C99_COMPLEX_TR1
1622 inline __complex__ float
1623 __complex_acos(__complex__ float __z)
1624 { return __builtin_cacosf(__z); }
1626 inline __complex__ double
1627 __complex_acos(__complex__ double __z)
1628 { return __builtin_cacos(__z); }
1630 inline __complex__ long double
1631 __complex_acos(const __complex__ long double& __z)
1632 { return __builtin_cacosl(__z); }
1634 template<typename _Tp>
1635 inline std::complex<_Tp>
1636 acos(const std::complex<_Tp>& __z)
1637 { return __complex_acos(__z.__rep()); }
1639 /// acos(__z) [8.1.2].
1640 // Effects: Behaves the same as C99 function cacos, defined
1641 // in subclause 7.3.5.1.
1642 template<typename _Tp>
1643 inline std::complex<_Tp>
1644 acos(const std::complex<_Tp>& __z)
1645 { return __complex_acos(__z); }
1648 template<typename _Tp>
1649 inline std::complex<_Tp>
1650 __complex_asin(const std::complex<_Tp>& __z)
1652 std::complex<_Tp> __t(-__z.imag(), __z.real());
1653 __t = std::asinh(__t);
1654 return std::complex<_Tp>(__t.imag(), -__t.real());
1657 #if _GLIBCXX_USE_C99_COMPLEX_TR1
1658 inline __complex__ float
1659 __complex_asin(__complex__ float __z)
1660 { return __builtin_casinf(__z); }
1662 inline __complex__ double
1663 __complex_asin(__complex__ double __z)
1664 { return __builtin_casin(__z); }
1666 inline __complex__ long double
1667 __complex_asin(const __complex__ long double& __z)
1668 { return __builtin_casinl(__z); }
1670 template<typename _Tp>
1671 inline std::complex<_Tp>
1672 asin(const std::complex<_Tp>& __z)
1673 { return __complex_asin(__z.__rep()); }
1675 /// asin(__z) [8.1.3].
1676 // Effects: Behaves the same as C99 function casin, defined
1677 // in subclause 7.3.5.2.
1678 template<typename _Tp>
1679 inline std::complex<_Tp>
1680 asin(const std::complex<_Tp>& __z)
1681 { return __complex_asin(__z); }
1684 template<typename _Tp>
1686 __complex_atan(const std::complex<_Tp>& __z)
1688 const _Tp __r2 = __z.real() * __z.real();
1689 const _Tp __x = _Tp(1.0) - __r2 - __z.imag() * __z.imag();
1691 _Tp __num = __z.imag() + _Tp(1.0);
1692 _Tp __den = __z.imag() - _Tp(1.0);
1694 __num = __r2 + __num * __num;
1695 __den = __r2 + __den * __den;
1697 return std::complex<_Tp>(_Tp(0.5) * atan2(_Tp(2.0) * __z.real(), __x),
1698 _Tp(0.25) * log(__num / __den));
1701 #if _GLIBCXX_USE_C99_COMPLEX_TR1
1702 inline __complex__ float
1703 __complex_atan(__complex__ float __z)
1704 { return __builtin_catanf(__z); }
1706 inline __complex__ double
1707 __complex_atan(__complex__ double __z)
1708 { return __builtin_catan(__z); }
1710 inline __complex__ long double
1711 __complex_atan(const __complex__ long double& __z)
1712 { return __builtin_catanl(__z); }
1714 template<typename _Tp>
1715 inline std::complex<_Tp>
1716 atan(const std::complex<_Tp>& __z)
1717 { return __complex_atan(__z.__rep()); }
1719 /// atan(__z) [8.1.4].
1720 // Effects: Behaves the same as C99 function catan, defined
1721 // in subclause 7.3.5.3.
1722 template<typename _Tp>
1723 inline std::complex<_Tp>
1724 atan(const std::complex<_Tp>& __z)
1725 { return __complex_atan(__z); }
1728 template<typename _Tp>
1730 __complex_acosh(const std::complex<_Tp>& __z)
1733 return _Tp(2.0) * std::log(std::sqrt(_Tp(0.5) * (__z + _Tp(1.0)))
1734 + std::sqrt(_Tp(0.5) * (__z - _Tp(1.0))));
1737 #if _GLIBCXX_USE_C99_COMPLEX_TR1
1738 inline __complex__ float
1739 __complex_acosh(__complex__ float __z)
1740 { return __builtin_cacoshf(__z); }
1742 inline __complex__ double
1743 __complex_acosh(__complex__ double __z)
1744 { return __builtin_cacosh(__z); }
1746 inline __complex__ long double
1747 __complex_acosh(const __complex__ long double& __z)
1748 { return __builtin_cacoshl(__z); }
1750 template<typename _Tp>
1751 inline std::complex<_Tp>
1752 acosh(const std::complex<_Tp>& __z)
1753 { return __complex_acosh(__z.__rep()); }
1755 /// acosh(__z) [8.1.5].
1756 // Effects: Behaves the same as C99 function cacosh, defined
1757 // in subclause 7.3.6.1.
1758 template<typename _Tp>
1759 inline std::complex<_Tp>
1760 acosh(const std::complex<_Tp>& __z)
1761 { return __complex_acosh(__z); }
1764 template<typename _Tp>
1766 __complex_asinh(const std::complex<_Tp>& __z)
1768 std::complex<_Tp> __t((__z.real() - __z.imag())
1769 * (__z.real() + __z.imag()) + _Tp(1.0),
1770 _Tp(2.0) * __z.real() * __z.imag());
1771 __t = std::sqrt(__t);
1773 return std::log(__t + __z);
1776 #if _GLIBCXX_USE_C99_COMPLEX_TR1
1777 inline __complex__ float
1778 __complex_asinh(__complex__ float __z)
1779 { return __builtin_casinhf(__z); }
1781 inline __complex__ double
1782 __complex_asinh(__complex__ double __z)
1783 { return __builtin_casinh(__z); }
1785 inline __complex__ long double
1786 __complex_asinh(const __complex__ long double& __z)
1787 { return __builtin_casinhl(__z); }
1789 template<typename _Tp>
1790 inline std::complex<_Tp>
1791 asinh(const std::complex<_Tp>& __z)
1792 { return __complex_asinh(__z.__rep()); }
1794 /// asinh(__z) [8.1.6].
1795 // Effects: Behaves the same as C99 function casin, defined
1796 // in subclause 7.3.6.2.
1797 template<typename _Tp>
1798 inline std::complex<_Tp>
1799 asinh(const std::complex<_Tp>& __z)
1800 { return __complex_asinh(__z); }
1803 template<typename _Tp>
1805 __complex_atanh(const std::complex<_Tp>& __z)
1807 const _Tp __i2 = __z.imag() * __z.imag();
1808 const _Tp __x = _Tp(1.0) - __i2 - __z.real() * __z.real();
1810 _Tp __num = _Tp(1.0) + __z.real();
1811 _Tp __den = _Tp(1.0) - __z.real();
1813 __num = __i2 + __num * __num;
1814 __den = __i2 + __den * __den;
1816 return std::complex<_Tp>(_Tp(0.25) * (log(__num) - log(__den)),
1817 _Tp(0.5) * atan2(_Tp(2.0) * __z.imag(), __x));
1820 #if _GLIBCXX_USE_C99_COMPLEX_TR1
1821 inline __complex__ float
1822 __complex_atanh(__complex__ float __z)
1823 { return __builtin_catanhf(__z); }
1825 inline __complex__ double
1826 __complex_atanh(__complex__ double __z)
1827 { return __builtin_catanh(__z); }
1829 inline __complex__ long double
1830 __complex_atanh(const __complex__ long double& __z)
1831 { return __builtin_catanhl(__z); }
1833 template<typename _Tp>
1834 inline std::complex<_Tp>
1835 atanh(const std::complex<_Tp>& __z)
1836 { return __complex_atanh(__z.__rep()); }
1838 /// atanh(__z) [8.1.7].
1839 // Effects: Behaves the same as C99 function catanh, defined
1840 // in subclause 7.3.6.3.
1841 template<typename _Tp>
1842 inline std::complex<_Tp>
1843 atanh(const std::complex<_Tp>& __z)
1844 { return __complex_atanh(__z); }
1847 template<typename _Tp>
1849 /// fabs(__z) [8.1.8].
1850 // Effects: Behaves the same as C99 function cabs, defined
1851 // in subclause 7.3.8.1.
1852 fabs(const std::complex<_Tp>& __z)
1853 { return std::abs(__z); }
1855 /// Additional overloads [8.1.9].
1856 template<typename _Tp>
1857 inline typename __gnu_cxx::__promote<_Tp>::__type
1860 typedef typename __gnu_cxx::__promote<_Tp>::__type __type;
1861 #if (_GLIBCXX11_USE_C99_MATH && !_GLIBCXX_USE_C99_FP_MACROS_DYNAMIC)
1862 return std::signbit(__x) ? __type(3.1415926535897932384626433832795029L)
1865 return std::arg(std::complex<__type>(__x));
1869 template<typename _Tp>
1870 _GLIBCXX_CONSTEXPR inline typename __gnu_cxx::__promote<_Tp>::__type
1874 template<typename _Tp>
1875 inline typename __gnu_cxx::__promote<_Tp>::__type
1878 typedef typename __gnu_cxx::__promote<_Tp>::__type __type;
1879 return __type(__x) * __type(__x);
1882 template<typename _Tp>
1883 _GLIBCXX_CONSTEXPR inline typename __gnu_cxx::__promote<_Tp>::__type
1887 template<typename _Tp, typename _Up>
1888 inline std::complex<typename __gnu_cxx::__promote_2<_Tp, _Up>::__type>
1889 pow(const std::complex<_Tp>& __x, const _Up& __y)
1891 typedef typename __gnu_cxx::__promote_2<_Tp, _Up>::__type __type;
1892 return std::pow(std::complex<__type>(__x), __type(__y));
1895 template<typename _Tp, typename _Up>
1896 inline std::complex<typename __gnu_cxx::__promote_2<_Tp, _Up>::__type>
1897 pow(const _Tp& __x, const std::complex<_Up>& __y)
1899 typedef typename __gnu_cxx::__promote_2<_Tp, _Up>::__type __type;
1900 return std::pow(__type(__x), std::complex<__type>(__y));
1903 template<typename _Tp, typename _Up>
1904 inline std::complex<typename __gnu_cxx::__promote_2<_Tp, _Up>::__type>
1905 pow(const std::complex<_Tp>& __x, const std::complex<_Up>& __y)
1907 typedef typename __gnu_cxx::__promote_2<_Tp, _Up>::__type __type;
1908 return std::pow(std::complex<__type>(__x),
1909 std::complex<__type>(__y));
1912 // Forward declarations.
1914 template<typename _Tp> std::complex<_Tp> proj(const std::complex<_Tp>&);
1916 template<typename _Tp>
1918 __complex_proj(const std::complex<_Tp>& __z)
1920 const _Tp __den = (__z.real() * __z.real()
1921 + __z.imag() * __z.imag() + _Tp(1.0));
1923 return std::complex<_Tp>((_Tp(2.0) * __z.real()) / __den,
1924 (_Tp(2.0) * __z.imag()) / __den);
1927 #if _GLIBCXX_USE_C99_COMPLEX
1928 inline __complex__ float
1929 __complex_proj(__complex__ float __z)
1930 { return __builtin_cprojf(__z); }
1932 inline __complex__ double
1933 __complex_proj(__complex__ double __z)
1934 { return __builtin_cproj(__z); }
1936 inline __complex__ long double
1937 __complex_proj(const __complex__ long double& __z)
1938 { return __builtin_cprojl(__z); }
1940 template<typename _Tp>
1941 inline std::complex<_Tp>
1942 proj(const std::complex<_Tp>& __z)
1943 { return __complex_proj(__z.__rep()); }
1945 template<typename _Tp>
1946 inline std::complex<_Tp>
1947 proj(const std::complex<_Tp>& __z)
1948 { return __complex_proj(__z); }
1951 template<typename _Tp>
1952 inline std::complex<typename __gnu_cxx::__promote<_Tp>::__type>
1955 typedef typename __gnu_cxx::__promote<_Tp>::__type __type;
1956 return std::proj(std::complex<__type>(__x));
1959 template<typename _Tp>
1960 inline std::complex<typename __gnu_cxx::__promote<_Tp>::__type>
1963 typedef typename __gnu_cxx::__promote<_Tp>::__type __type;
1964 return std::complex<__type>(__x, -__type());
1967 #if __cplusplus > 201103L
1969 inline namespace literals {
1970 inline namespace complex_literals {
1971 #pragma GCC diagnostic push
1972 #pragma GCC diagnostic ignored "-Wliteral-suffix"
1973 #define __cpp_lib_complex_udls 201309
1975 constexpr std::complex<float>
1976 operator""if(long double __num)
1977 { return std::complex<float>{0.0F, static_cast<float>(__num)}; }
1979 constexpr std::complex<float>
1980 operator""if(unsigned long long __num)
1981 { return std::complex<float>{0.0F, static_cast<float>(__num)}; }
1983 constexpr std::complex<double>
1984 operator""i(long double __num)
1985 { return std::complex<double>{0.0, static_cast<double>(__num)}; }
1987 constexpr std::complex<double>
1988 operator""i(unsigned long long __num)
1989 { return std::complex<double>{0.0, static_cast<double>(__num)}; }
1991 constexpr std::complex<long double>
1992 operator""il(long double __num)
1993 { return std::complex<long double>{0.0L, __num}; }
1995 constexpr std::complex<long double>
1996 operator""il(unsigned long long __num)
1997 { return std::complex<long double>{0.0L, static_cast<long double>(__num)}; }
1999 #pragma GCC diagnostic pop
2000 } // inline namespace complex_literals
2001 } // inline namespace literals
2005 _GLIBCXX_END_NAMESPACE_VERSION
2010 #endif /* _GLIBCXX_COMPLEX */