1 // The template and inlines for the -*- C++ -*- complex number classes.
3 // Copyright (C) 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005,
4 // 2006, 2007, 2008, 2009, 2010
5 // Free Software Foundation, Inc.
7 // This file is part of the GNU ISO C++ Library. This library is free
8 // software; you can redistribute it and/or modify it under the
9 // terms of the GNU General Public License as published by the
10 // Free Software Foundation; either version 3, or (at your option)
13 // This library is distributed in the hope that it will be useful,
14 // but WITHOUT ANY WARRANTY; without even the implied warranty of
15 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 // GNU General Public License for more details.
18 // Under Section 7 of GPL version 3, you are granted additional
19 // permissions described in the GCC Runtime Library Exception, version
20 // 3.1, as published by the Free Software Foundation.
22 // You should have received a copy of the GNU General Public License and
23 // a copy of the GCC Runtime Library Exception along with this program;
24 // see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
25 // <http://www.gnu.org/licenses/>.
27 /** @file include/complex
28 * This is a Standard C++ Library header.
32 // ISO C++ 14882: 26.2 Complex Numbers
33 // Note: this is not a conforming implementation.
34 // Initially implemented by Ulrich Drepper <drepper@cygnus.com>
35 // Improved by Gabriel Dos Reis <dosreis@cmla.ens-cachan.fr>
38 #ifndef _GLIBCXX_COMPLEX
39 #define _GLIBCXX_COMPLEX 1
41 #pragma GCC system_header
43 #include <bits/c++config.h>
44 #include <bits/cpp_type_traits.h>
45 #include <ext/type_traits.h>
49 _GLIBCXX_BEGIN_NAMESPACE(std)
52 * @defgroup complex_numbers Complex Numbers
55 * Classes and functions for complex numbers.
59 // Forward declarations.
60 template<typename _Tp> class complex;
61 template<> class complex<float>;
62 template<> class complex<double>;
63 template<> class complex<long double>;
65 /// Return magnitude of @a z.
66 template<typename _Tp> _Tp abs(const complex<_Tp>&);
67 /// Return phase angle of @a z.
68 template<typename _Tp> _Tp arg(const complex<_Tp>&);
69 /// Return @a z magnitude squared.
70 template<typename _Tp> _Tp norm(const complex<_Tp>&);
72 /// Return complex conjugate of @a z.
73 template<typename _Tp> complex<_Tp> conj(const complex<_Tp>&);
74 /// Return complex with magnitude @a rho and angle @a theta.
75 template<typename _Tp> complex<_Tp> polar(const _Tp&, const _Tp& = 0);
78 /// Return complex cosine of @a z.
79 template<typename _Tp> complex<_Tp> cos(const complex<_Tp>&);
80 /// Return complex hyperbolic cosine of @a z.
81 template<typename _Tp> complex<_Tp> cosh(const complex<_Tp>&);
82 /// Return complex base e exponential of @a z.
83 template<typename _Tp> complex<_Tp> exp(const complex<_Tp>&);
84 /// Return complex natural logarithm of @a z.
85 template<typename _Tp> complex<_Tp> log(const complex<_Tp>&);
86 /// Return complex base 10 logarithm of @a z.
87 template<typename _Tp> complex<_Tp> log10(const complex<_Tp>&);
88 #ifndef __GXX_EXPERIMENTAL_CXX0X__
90 /// Return @a x to the @a y'th power.
91 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 // Lets the compiler synthesize the copy constructor
133 // complex (const complex<_Tp>&);
134 /// Copy constructor.
135 template<typename _Up>
136 _GLIBCXX_CONSTEXPR complex(const complex<_Up>& __z)
137 : _M_real(__z.real()), _M_imag(__z.imag()) { }
139 #ifdef __GXX_EXPERIMENTAL_CXX0X__
140 // _GLIBCXX_RESOLVE_LIB_DEFECTS
141 // DR 387. std::complex over-encapsulated.
143 real() const { return _M_real; }
146 imag() const { return _M_imag; }
148 /// Return real part of complex number.
150 real() { return _M_real; }
152 /// Return real part of complex number.
154 real() const { return _M_real; }
156 /// Return imaginary part of complex number.
158 imag() { return _M_imag; }
160 /// Return imaginary part of complex number.
162 imag() const { return _M_imag; }
165 // _GLIBCXX_RESOLVE_LIB_DEFECTS
166 // DR 387. std::complex over-encapsulated.
168 real(_Tp __val) { _M_real = __val; }
171 imag(_Tp __val) { _M_imag = __val; }
173 /// Assign this complex number to scalar @a t.
174 complex<_Tp>& operator=(const _Tp&);
176 /// Add @a t to this complex number.
179 operator+=(const _Tp& __t)
185 /// Subtract @a t from this complex number.
188 operator-=(const _Tp& __t)
194 /// Multiply this complex number by @a t.
195 complex<_Tp>& operator*=(const _Tp&);
196 /// Divide this complex number by @a t.
197 complex<_Tp>& operator/=(const _Tp&);
199 // Lets the compiler synthesize the
200 // copy and assignment operator
201 // complex<_Tp>& operator= (const complex<_Tp>&);
202 /// Assign this complex number to complex @a z.
203 template<typename _Up>
204 complex<_Tp>& operator=(const complex<_Up>&);
205 /// Add @a z to this complex number.
206 template<typename _Up>
207 complex<_Tp>& operator+=(const complex<_Up>&);
208 /// Subtract @a z from this complex number.
209 template<typename _Up>
210 complex<_Tp>& operator-=(const complex<_Up>&);
211 /// Multiply this complex number by @a z.
212 template<typename _Up>
213 complex<_Tp>& operator*=(const complex<_Up>&);
214 /// Divide this complex number by @a z.
215 template<typename _Up>
216 complex<_Tp>& operator/=(const complex<_Up>&);
218 _GLIBCXX_USE_CONSTEXPR complex __rep() const
226 template<typename _Tp>
228 complex<_Tp>::operator=(const _Tp& __t)
236 template<typename _Tp>
238 complex<_Tp>::operator*=(const _Tp& __t)
246 template<typename _Tp>
248 complex<_Tp>::operator/=(const _Tp& __t)
255 template<typename _Tp>
256 template<typename _Up>
258 complex<_Tp>::operator=(const complex<_Up>& __z)
260 _M_real = __z.real();
261 _M_imag = __z.imag();
266 template<typename _Tp>
267 template<typename _Up>
269 complex<_Tp>::operator+=(const complex<_Up>& __z)
271 _M_real += __z.real();
272 _M_imag += __z.imag();
277 template<typename _Tp>
278 template<typename _Up>
280 complex<_Tp>::operator-=(const complex<_Up>& __z)
282 _M_real -= __z.real();
283 _M_imag -= __z.imag();
288 // XXX: This is a grammar school implementation.
289 template<typename _Tp>
290 template<typename _Up>
292 complex<_Tp>::operator*=(const complex<_Up>& __z)
294 const _Tp __r = _M_real * __z.real() - _M_imag * __z.imag();
295 _M_imag = _M_real * __z.imag() + _M_imag * __z.real();
301 // XXX: This is a grammar school implementation.
302 template<typename _Tp>
303 template<typename _Up>
305 complex<_Tp>::operator/=(const complex<_Up>& __z)
307 const _Tp __r = _M_real * __z.real() + _M_imag * __z.imag();
308 const _Tp __n = std::norm(__z);
309 _M_imag = (_M_imag * __z.real() - _M_real * __z.imag()) / __n;
316 /// Return new complex value @a x plus @a y.
317 template<typename _Tp>
319 operator+(const complex<_Tp>& __x, const complex<_Tp>& __y)
321 complex<_Tp> __r = __x;
326 template<typename _Tp>
328 operator+(const complex<_Tp>& __x, const _Tp& __y)
330 complex<_Tp> __r = __x;
335 template<typename _Tp>
337 operator+(const _Tp& __x, const complex<_Tp>& __y)
339 complex<_Tp> __r = __y;
346 /// Return new complex value @a x minus @a y.
347 template<typename _Tp>
349 operator-(const complex<_Tp>& __x, const complex<_Tp>& __y)
351 complex<_Tp> __r = __x;
356 template<typename _Tp>
358 operator-(const complex<_Tp>& __x, const _Tp& __y)
360 complex<_Tp> __r = __x;
365 template<typename _Tp>
367 operator-(const _Tp& __x, const complex<_Tp>& __y)
369 complex<_Tp> __r(__x, -__y.imag());
376 /// Return new complex value @a x times @a y.
377 template<typename _Tp>
379 operator*(const complex<_Tp>& __x, const complex<_Tp>& __y)
381 complex<_Tp> __r = __x;
386 template<typename _Tp>
388 operator*(const complex<_Tp>& __x, const _Tp& __y)
390 complex<_Tp> __r = __x;
395 template<typename _Tp>
397 operator*(const _Tp& __x, const complex<_Tp>& __y)
399 complex<_Tp> __r = __y;
406 /// Return new complex value @a x divided by @a y.
407 template<typename _Tp>
409 operator/(const complex<_Tp>& __x, const complex<_Tp>& __y)
411 complex<_Tp> __r = __x;
416 template<typename _Tp>
418 operator/(const complex<_Tp>& __x, const _Tp& __y)
420 complex<_Tp> __r = __x;
425 template<typename _Tp>
427 operator/(const _Tp& __x, const complex<_Tp>& __y)
429 complex<_Tp> __r = __x;
436 template<typename _Tp>
438 operator+(const complex<_Tp>& __x)
441 /// Return complex negation of @a x.
442 template<typename _Tp>
444 operator-(const complex<_Tp>& __x)
445 { return complex<_Tp>(-__x.real(), -__x.imag()); }
448 /// Return true if @a x is equal to @a y.
449 template<typename _Tp>
450 inline _GLIBCXX_CONSTEXPR bool
451 operator==(const complex<_Tp>& __x, const complex<_Tp>& __y)
452 { return __x.real() == __y.real() && __x.imag() == __y.imag(); }
454 template<typename _Tp>
455 inline _GLIBCXX_CONSTEXPR bool
456 operator==(const complex<_Tp>& __x, const _Tp& __y)
457 { return __x.real() == __y && __x.imag() == _Tp(); }
459 template<typename _Tp>
460 inline _GLIBCXX_CONSTEXPR bool
461 operator==(const _Tp& __x, const complex<_Tp>& __y)
462 { return __x == __y.real() && _Tp() == __y.imag(); }
466 /// Return false if @a x is equal to @a y.
467 template<typename _Tp>
468 inline _GLIBCXX_CONSTEXPR bool
469 operator!=(const complex<_Tp>& __x, const complex<_Tp>& __y)
470 { return __x.real() != __y.real() || __x.imag() != __y.imag(); }
472 template<typename _Tp>
473 inline _GLIBCXX_CONSTEXPR bool
474 operator!=(const complex<_Tp>& __x, const _Tp& __y)
475 { return __x.real() != __y || __x.imag() != _Tp(); }
477 template<typename _Tp>
478 inline _GLIBCXX_CONSTEXPR bool
479 operator!=(const _Tp& __x, const complex<_Tp>& __y)
480 { return __x != __y.real() || _Tp() != __y.imag(); }
483 /// Extraction operator for complex values.
484 template<typename _Tp, typename _CharT, class _Traits>
485 basic_istream<_CharT, _Traits>&
486 operator>>(basic_istream<_CharT, _Traits>& __is, complex<_Tp>& __x)
493 __is >> __re_x >> __ch;
496 __is >> __im_x >> __ch;
498 __x = complex<_Tp>(__re_x, __im_x);
500 __is.setstate(ios_base::failbit);
502 else if (__ch == ')')
505 __is.setstate(ios_base::failbit);
516 /// Insertion operator for complex values.
517 template<typename _Tp, typename _CharT, class _Traits>
518 basic_ostream<_CharT, _Traits>&
519 operator<<(basic_ostream<_CharT, _Traits>& __os, const complex<_Tp>& __x)
521 basic_ostringstream<_CharT, _Traits> __s;
522 __s.flags(__os.flags());
523 __s.imbue(__os.getloc());
524 __s.precision(__os.precision());
525 __s << '(' << __x.real() << ',' << __x.imag() << ')';
526 return __os << __s.str();
530 #ifdef __GXX_EXPERIMENTAL_CXX0X__
531 template<typename _Tp>
533 real(const complex<_Tp>& __z)
534 { return __z.real(); }
536 template<typename _Tp>
538 imag(const complex<_Tp>& __z)
539 { return __z.imag(); }
541 template<typename _Tp>
543 real(complex<_Tp>& __z)
544 { return __z.real(); }
546 template<typename _Tp>
548 real(const complex<_Tp>& __z)
549 { return __z.real(); }
551 template<typename _Tp>
553 imag(complex<_Tp>& __z)
554 { return __z.imag(); }
556 template<typename _Tp>
558 imag(const complex<_Tp>& __z)
559 { return __z.imag(); }
562 // 26.2.7/3 abs(__z): Returns the magnitude of __z.
563 template<typename _Tp>
565 __complex_abs(const complex<_Tp>& __z)
567 _Tp __x = __z.real();
568 _Tp __y = __z.imag();
569 const _Tp __s = std::max(abs(__x), abs(__y));
570 if (__s == _Tp()) // well ...
574 return __s * sqrt(__x * __x + __y * __y);
577 #if _GLIBCXX_USE_C99_COMPLEX
579 __complex_abs(__complex__ float __z) { return __builtin_cabsf(__z); }
582 __complex_abs(__complex__ double __z) { return __builtin_cabs(__z); }
585 __complex_abs(const __complex__ long double& __z)
586 { return __builtin_cabsl(__z); }
588 template<typename _Tp>
590 abs(const complex<_Tp>& __z) { return __complex_abs(__z.__rep()); }
592 template<typename _Tp>
594 abs(const complex<_Tp>& __z) { return __complex_abs(__z); }
598 // 26.2.7/4: arg(__z): Returns the phase angle of __z.
599 template<typename _Tp>
601 __complex_arg(const complex<_Tp>& __z)
602 { return atan2(__z.imag(), __z.real()); }
604 #if _GLIBCXX_USE_C99_COMPLEX
606 __complex_arg(__complex__ float __z) { return __builtin_cargf(__z); }
609 __complex_arg(__complex__ double __z) { return __builtin_carg(__z); }
612 __complex_arg(const __complex__ long double& __z)
613 { return __builtin_cargl(__z); }
615 template<typename _Tp>
617 arg(const complex<_Tp>& __z) { return __complex_arg(__z.__rep()); }
619 template<typename _Tp>
621 arg(const complex<_Tp>& __z) { return __complex_arg(__z); }
624 // 26.2.7/5: norm(__z) returns the squared magnitude of __z.
625 // As defined, norm() is -not- a norm is the common mathematical
626 // sens used in numerics. The helper class _Norm_helper<> tries to
627 // distinguish between builtin floating point and the rest, so as
628 // to deliver an answer as close as possible to the real value.
632 template<typename _Tp>
633 static inline _Tp _S_do_it(const complex<_Tp>& __z)
635 const _Tp __x = __z.real();
636 const _Tp __y = __z.imag();
637 return __x * __x + __y * __y;
642 struct _Norm_helper<true>
644 template<typename _Tp>
645 static inline _Tp _S_do_it(const complex<_Tp>& __z)
647 _Tp __res = std::abs(__z);
648 return __res * __res;
652 template<typename _Tp>
654 norm(const complex<_Tp>& __z)
656 return _Norm_helper<__is_floating<_Tp>::__value
657 && !_GLIBCXX_FAST_MATH>::_S_do_it(__z);
660 template<typename _Tp>
662 polar(const _Tp& __rho, const _Tp& __theta)
663 { return complex<_Tp>(__rho * cos(__theta), __rho * sin(__theta)); }
665 template<typename _Tp>
667 conj(const complex<_Tp>& __z)
668 { return complex<_Tp>(__z.real(), -__z.imag()); }
672 // 26.2.8/1 cos(__z): Returns the cosine of __z.
673 template<typename _Tp>
675 __complex_cos(const complex<_Tp>& __z)
677 const _Tp __x = __z.real();
678 const _Tp __y = __z.imag();
679 return complex<_Tp>(cos(__x) * cosh(__y), -sin(__x) * sinh(__y));
682 #if _GLIBCXX_USE_C99_COMPLEX
683 inline __complex__ float
684 __complex_cos(__complex__ float __z) { return __builtin_ccosf(__z); }
686 inline __complex__ double
687 __complex_cos(__complex__ double __z) { return __builtin_ccos(__z); }
689 inline __complex__ long double
690 __complex_cos(const __complex__ long double& __z)
691 { return __builtin_ccosl(__z); }
693 template<typename _Tp>
695 cos(const complex<_Tp>& __z) { return __complex_cos(__z.__rep()); }
697 template<typename _Tp>
699 cos(const complex<_Tp>& __z) { return __complex_cos(__z); }
702 // 26.2.8/2 cosh(__z): Returns the hyperbolic cosine of __z.
703 template<typename _Tp>
705 __complex_cosh(const complex<_Tp>& __z)
707 const _Tp __x = __z.real();
708 const _Tp __y = __z.imag();
709 return complex<_Tp>(cosh(__x) * cos(__y), sinh(__x) * sin(__y));
712 #if _GLIBCXX_USE_C99_COMPLEX
713 inline __complex__ float
714 __complex_cosh(__complex__ float __z) { return __builtin_ccoshf(__z); }
716 inline __complex__ double
717 __complex_cosh(__complex__ double __z) { return __builtin_ccosh(__z); }
719 inline __complex__ long double
720 __complex_cosh(const __complex__ long double& __z)
721 { return __builtin_ccoshl(__z); }
723 template<typename _Tp>
725 cosh(const complex<_Tp>& __z) { return __complex_cosh(__z.__rep()); }
727 template<typename _Tp>
729 cosh(const complex<_Tp>& __z) { return __complex_cosh(__z); }
732 // 26.2.8/3 exp(__z): Returns the complex base e exponential of x
733 template<typename _Tp>
735 __complex_exp(const complex<_Tp>& __z)
736 { return std::polar(exp(__z.real()), __z.imag()); }
738 #if _GLIBCXX_USE_C99_COMPLEX
739 inline __complex__ float
740 __complex_exp(__complex__ float __z) { return __builtin_cexpf(__z); }
742 inline __complex__ double
743 __complex_exp(__complex__ double __z) { return __builtin_cexp(__z); }
745 inline __complex__ long double
746 __complex_exp(const __complex__ long double& __z)
747 { return __builtin_cexpl(__z); }
749 template<typename _Tp>
751 exp(const complex<_Tp>& __z) { return __complex_exp(__z.__rep()); }
753 template<typename _Tp>
755 exp(const complex<_Tp>& __z) { return __complex_exp(__z); }
758 // 26.2.8/5 log(__z): Returns the natural complex logarithm of __z.
759 // The branch cut is along the negative axis.
760 template<typename _Tp>
762 __complex_log(const complex<_Tp>& __z)
763 { return complex<_Tp>(log(std::abs(__z)), std::arg(__z)); }
765 #if _GLIBCXX_USE_C99_COMPLEX
766 inline __complex__ float
767 __complex_log(__complex__ float __z) { return __builtin_clogf(__z); }
769 inline __complex__ double
770 __complex_log(__complex__ double __z) { return __builtin_clog(__z); }
772 inline __complex__ long double
773 __complex_log(const __complex__ long double& __z)
774 { return __builtin_clogl(__z); }
776 template<typename _Tp>
778 log(const complex<_Tp>& __z) { return __complex_log(__z.__rep()); }
780 template<typename _Tp>
782 log(const complex<_Tp>& __z) { return __complex_log(__z); }
785 template<typename _Tp>
787 log10(const complex<_Tp>& __z)
788 { return std::log(__z) / log(_Tp(10.0)); }
790 // 26.2.8/10 sin(__z): Returns the sine of __z.
791 template<typename _Tp>
793 __complex_sin(const complex<_Tp>& __z)
795 const _Tp __x = __z.real();
796 const _Tp __y = __z.imag();
797 return complex<_Tp>(sin(__x) * cosh(__y), cos(__x) * sinh(__y));
800 #if _GLIBCXX_USE_C99_COMPLEX
801 inline __complex__ float
802 __complex_sin(__complex__ float __z) { return __builtin_csinf(__z); }
804 inline __complex__ double
805 __complex_sin(__complex__ double __z) { return __builtin_csin(__z); }
807 inline __complex__ long double
808 __complex_sin(const __complex__ long double& __z)
809 { return __builtin_csinl(__z); }
811 template<typename _Tp>
813 sin(const complex<_Tp>& __z) { return __complex_sin(__z.__rep()); }
815 template<typename _Tp>
817 sin(const complex<_Tp>& __z) { return __complex_sin(__z); }
820 // 26.2.8/11 sinh(__z): Returns the hyperbolic sine of __z.
821 template<typename _Tp>
823 __complex_sinh(const complex<_Tp>& __z)
825 const _Tp __x = __z.real();
826 const _Tp __y = __z.imag();
827 return complex<_Tp>(sinh(__x) * cos(__y), cosh(__x) * sin(__y));
830 #if _GLIBCXX_USE_C99_COMPLEX
831 inline __complex__ float
832 __complex_sinh(__complex__ float __z) { return __builtin_csinhf(__z); }
834 inline __complex__ double
835 __complex_sinh(__complex__ double __z) { return __builtin_csinh(__z); }
837 inline __complex__ long double
838 __complex_sinh(const __complex__ long double& __z)
839 { return __builtin_csinhl(__z); }
841 template<typename _Tp>
843 sinh(const complex<_Tp>& __z) { return __complex_sinh(__z.__rep()); }
845 template<typename _Tp>
847 sinh(const complex<_Tp>& __z) { return __complex_sinh(__z); }
850 // 26.2.8/13 sqrt(__z): Returns the complex square root of __z.
851 // The branch cut is on the negative axis.
852 template<typename _Tp>
854 __complex_sqrt(const complex<_Tp>& __z)
856 _Tp __x = __z.real();
857 _Tp __y = __z.imag();
861 _Tp __t = sqrt(abs(__y) / 2);
862 return complex<_Tp>(__t, __y < _Tp() ? -__t : __t);
866 _Tp __t = sqrt(2 * (std::abs(__z) + abs(__x)));
869 ? complex<_Tp>(__u, __y / __t)
870 : complex<_Tp>(abs(__y) / __t, __y < _Tp() ? -__u : __u);
874 #if _GLIBCXX_USE_C99_COMPLEX
875 inline __complex__ float
876 __complex_sqrt(__complex__ float __z) { return __builtin_csqrtf(__z); }
878 inline __complex__ double
879 __complex_sqrt(__complex__ double __z) { return __builtin_csqrt(__z); }
881 inline __complex__ long double
882 __complex_sqrt(const __complex__ long double& __z)
883 { return __builtin_csqrtl(__z); }
885 template<typename _Tp>
887 sqrt(const complex<_Tp>& __z) { return __complex_sqrt(__z.__rep()); }
889 template<typename _Tp>
891 sqrt(const complex<_Tp>& __z) { return __complex_sqrt(__z); }
894 // 26.2.8/14 tan(__z): Return the complex tangent of __z.
896 template<typename _Tp>
898 __complex_tan(const complex<_Tp>& __z)
899 { return std::sin(__z) / std::cos(__z); }
901 #if _GLIBCXX_USE_C99_COMPLEX
902 inline __complex__ float
903 __complex_tan(__complex__ float __z) { return __builtin_ctanf(__z); }
905 inline __complex__ double
906 __complex_tan(__complex__ double __z) { return __builtin_ctan(__z); }
908 inline __complex__ long double
909 __complex_tan(const __complex__ long double& __z)
910 { return __builtin_ctanl(__z); }
912 template<typename _Tp>
914 tan(const complex<_Tp>& __z) { return __complex_tan(__z.__rep()); }
916 template<typename _Tp>
918 tan(const complex<_Tp>& __z) { return __complex_tan(__z); }
922 // 26.2.8/15 tanh(__z): Returns the hyperbolic tangent of __z.
924 template<typename _Tp>
926 __complex_tanh(const complex<_Tp>& __z)
927 { return std::sinh(__z) / std::cosh(__z); }
929 #if _GLIBCXX_USE_C99_COMPLEX
930 inline __complex__ float
931 __complex_tanh(__complex__ float __z) { return __builtin_ctanhf(__z); }
933 inline __complex__ double
934 __complex_tanh(__complex__ double __z) { return __builtin_ctanh(__z); }
936 inline __complex__ long double
937 __complex_tanh(const __complex__ long double& __z)
938 { return __builtin_ctanhl(__z); }
940 template<typename _Tp>
942 tanh(const complex<_Tp>& __z) { return __complex_tanh(__z.__rep()); }
944 template<typename _Tp>
946 tanh(const complex<_Tp>& __z) { return __complex_tanh(__z); }
950 // 26.2.8/9 pow(__x, __y): Returns the complex power base of __x
951 // raised to the __y-th power. The branch
952 // cut is on the negative axis.
953 #ifndef __GXX_EXPERIMENTAL_CXX0X__
954 template<typename _Tp>
956 __complex_pow_unsigned(complex<_Tp> __x, unsigned __n)
958 complex<_Tp> __y = __n % 2 ? __x : complex<_Tp>(1);
970 // _GLIBCXX_RESOLVE_LIB_DEFECTS
971 // DR 844. complex pow return type is ambiguous.
972 template<typename _Tp>
974 pow(const complex<_Tp>& __z, int __n)
977 ? complex<_Tp>(1) / std::__complex_pow_unsigned(__z, -__n)
978 : std::__complex_pow_unsigned(__z, __n);
982 template<typename _Tp>
984 pow(const complex<_Tp>& __x, const _Tp& __y)
986 #ifndef _GLIBCXX_USE_C99_COMPLEX
990 if (__x.imag() == _Tp() && __x.real() > _Tp())
991 return pow(__x.real(), __y);
993 complex<_Tp> __t = std::log(__x);
994 return std::polar(exp(__y * __t.real()), __y * __t.imag());
997 template<typename _Tp>
999 __complex_pow(const complex<_Tp>& __x, const complex<_Tp>& __y)
1000 { return __x == _Tp() ? _Tp() : std::exp(__y * std::log(__x)); }
1002 #if _GLIBCXX_USE_C99_COMPLEX
1003 inline __complex__ float
1004 __complex_pow(__complex__ float __x, __complex__ float __y)
1005 { return __builtin_cpowf(__x, __y); }
1007 inline __complex__ double
1008 __complex_pow(__complex__ double __x, __complex__ double __y)
1009 { return __builtin_cpow(__x, __y); }
1011 inline __complex__ long double
1012 __complex_pow(const __complex__ long double& __x,
1013 const __complex__ long double& __y)
1014 { return __builtin_cpowl(__x, __y); }
1016 template<typename _Tp>
1018 pow(const complex<_Tp>& __x, const complex<_Tp>& __y)
1019 { return __complex_pow(__x.__rep(), __y.__rep()); }
1021 template<typename _Tp>
1023 pow(const complex<_Tp>& __x, const complex<_Tp>& __y)
1024 { return __complex_pow(__x, __y); }
1027 template<typename _Tp>
1029 pow(const _Tp& __x, const complex<_Tp>& __y)
1031 return __x > _Tp() ? std::polar(pow(__x, __y.real()),
1032 __y.imag() * log(__x))
1033 : std::pow(complex<_Tp>(__x), __y);
1036 // 26.2.3 complex specializations
1037 // complex<float> specialization
1039 struct complex<float>
1041 typedef float value_type;
1042 typedef __complex__ float _ComplexT;
1044 _GLIBCXX_CONSTEXPR complex(_ComplexT __z) : _M_value(__z) { }
1046 _GLIBCXX_CONSTEXPR complex(float __r = 0.0f, float __i = 0.0f)
1047 : _M_value(__r + __i * 1.0fi) { }
1049 explicit _GLIBCXX_CONSTEXPR complex(const complex<double>&);
1050 explicit _GLIBCXX_CONSTEXPR complex(const complex<long double>&);
1052 #ifdef __GXX_EXPERIMENTAL_CXX0X__
1053 // _GLIBCXX_RESOLVE_LIB_DEFECTS
1054 // DR 387. std::complex over-encapsulated.
1056 real() const { return __real__ _M_value; }
1059 imag() const { return __imag__ _M_value; }
1062 real() { return __real__ _M_value; }
1065 real() const { return __real__ _M_value; }
1068 imag() { return __imag__ _M_value; }
1071 imag() const { return __imag__ _M_value; }
1074 // _GLIBCXX_RESOLVE_LIB_DEFECTS
1075 // DR 387. std::complex over-encapsulated.
1077 real(float __val) { __real__ _M_value = __val; }
1080 imag(float __val) { __imag__ _M_value = __val; }
1083 operator=(float __f)
1090 operator+=(float __f)
1097 operator-=(float __f)
1104 operator*=(float __f)
1111 operator/=(float __f)
1117 // Let the compiler synthesize the copy and assignment
1118 // operator. It always does a pretty good job.
1119 // complex& operator=(const complex&);
1121 template<typename _Tp>
1123 operator=(const complex<_Tp>& __z)
1125 __real__ _M_value = __z.real();
1126 __imag__ _M_value = __z.imag();
1130 template<typename _Tp>
1132 operator+=(const complex<_Tp>& __z)
1134 __real__ _M_value += __z.real();
1135 __imag__ _M_value += __z.imag();
1141 operator-=(const complex<_Tp>& __z)
1143 __real__ _M_value -= __z.real();
1144 __imag__ _M_value -= __z.imag();
1150 operator*=(const complex<_Tp>& __z)
1153 __real__ __t = __z.real();
1154 __imag__ __t = __z.imag();
1161 operator/=(const complex<_Tp>& __z)
1164 __real__ __t = __z.real();
1165 __imag__ __t = __z.imag();
1170 _GLIBCXX_USE_CONSTEXPR _ComplexT __rep() const { return _M_value; }
1176 // 26.2.3 complex specializations
1177 // complex<double> specialization
1179 struct complex<double>
1181 typedef double value_type;
1182 typedef __complex__ double _ComplexT;
1184 _GLIBCXX_CONSTEXPR complex(_ComplexT __z) : _M_value(__z) { }
1186 _GLIBCXX_CONSTEXPR complex(double __r = 0.0, double __i = 0.0)
1187 : _M_value(__r + __i * 1.0i) { }
1189 _GLIBCXX_CONSTEXPR complex(const complex<float>& __z)
1190 : _M_value(__z.__rep()) { }
1192 explicit _GLIBCXX_CONSTEXPR complex(const complex<long double>&);
1194 #ifdef __GXX_EXPERIMENTAL_CXX0X__
1195 // _GLIBCXX_RESOLVE_LIB_DEFECTS
1196 // DR 387. std::complex over-encapsulated.
1198 real() const { return __real__ _M_value; }
1201 imag() const { return __imag__ _M_value; }
1204 real() { return __real__ _M_value; }
1207 real() const { return __real__ _M_value; }
1210 imag() { return __imag__ _M_value; }
1213 imag() const { return __imag__ _M_value; }
1216 // _GLIBCXX_RESOLVE_LIB_DEFECTS
1217 // DR 387. std::complex over-encapsulated.
1219 real(double __val) { __real__ _M_value = __val; }
1222 imag(double __val) { __imag__ _M_value = __val; }
1225 operator=(double __d)
1232 operator+=(double __d)
1239 operator-=(double __d)
1246 operator*=(double __d)
1253 operator/=(double __d)
1259 // The compiler will synthesize this, efficiently.
1260 // complex& operator=(const complex&);
1262 template<typename _Tp>
1264 operator=(const complex<_Tp>& __z)
1266 __real__ _M_value = __z.real();
1267 __imag__ _M_value = __z.imag();
1271 template<typename _Tp>
1273 operator+=(const complex<_Tp>& __z)
1275 __real__ _M_value += __z.real();
1276 __imag__ _M_value += __z.imag();
1280 template<typename _Tp>
1282 operator-=(const complex<_Tp>& __z)
1284 __real__ _M_value -= __z.real();
1285 __imag__ _M_value -= __z.imag();
1289 template<typename _Tp>
1291 operator*=(const complex<_Tp>& __z)
1294 __real__ __t = __z.real();
1295 __imag__ __t = __z.imag();
1300 template<typename _Tp>
1302 operator/=(const complex<_Tp>& __z)
1305 __real__ __t = __z.real();
1306 __imag__ __t = __z.imag();
1311 _GLIBCXX_USE_CONSTEXPR _ComplexT __rep() const { return _M_value; }
1317 // 26.2.3 complex specializations
1318 // complex<long double> specialization
1320 struct complex<long double>
1322 typedef long double value_type;
1323 typedef __complex__ long double _ComplexT;
1325 _GLIBCXX_CONSTEXPR complex(_ComplexT __z) : _M_value(__z) { }
1327 _GLIBCXX_CONSTEXPR complex(long double __r = 0.0L,
1328 long double __i = 0.0L)
1329 : _M_value(__r + __i * 1.0Li) { }
1331 _GLIBCXX_CONSTEXPR complex(const complex<float>& __z)
1332 : _M_value(__z.__rep()) { }
1334 _GLIBCXX_CONSTEXPR complex(const complex<double>& __z)
1335 : _M_value(__z.__rep()) { }
1337 #ifdef __GXX_EXPERIMENTAL_CXX0X__
1338 // _GLIBCXX_RESOLVE_LIB_DEFECTS
1339 // DR 387. std::complex over-encapsulated.
1340 constexpr long double
1341 real() const { return __real__ _M_value; }
1343 constexpr long double
1344 imag() const { return __imag__ _M_value; }
1347 real() { return __real__ _M_value; }
1350 real() const { return __real__ _M_value; }
1353 imag() { return __imag__ _M_value; }
1356 imag() const { return __imag__ _M_value; }
1359 // _GLIBCXX_RESOLVE_LIB_DEFECTS
1360 // DR 387. std::complex over-encapsulated.
1362 real(long double __val) { __real__ _M_value = __val; }
1365 imag(long double __val) { __imag__ _M_value = __val; }
1368 operator=(long double __r)
1375 operator+=(long double __r)
1382 operator-=(long double __r)
1389 operator*=(long double __r)
1396 operator/=(long double __r)
1402 // The compiler knows how to do this efficiently
1403 // complex& operator=(const complex&);
1405 template<typename _Tp>
1407 operator=(const complex<_Tp>& __z)
1409 __real__ _M_value = __z.real();
1410 __imag__ _M_value = __z.imag();
1414 template<typename _Tp>
1416 operator+=(const complex<_Tp>& __z)
1418 __real__ _M_value += __z.real();
1419 __imag__ _M_value += __z.imag();
1423 template<typename _Tp>
1425 operator-=(const complex<_Tp>& __z)
1427 __real__ _M_value -= __z.real();
1428 __imag__ _M_value -= __z.imag();
1432 template<typename _Tp>
1434 operator*=(const complex<_Tp>& __z)
1437 __real__ __t = __z.real();
1438 __imag__ __t = __z.imag();
1443 template<typename _Tp>
1445 operator/=(const complex<_Tp>& __z)
1448 __real__ __t = __z.real();
1449 __imag__ __t = __z.imag();
1454 _GLIBCXX_USE_CONSTEXPR _ComplexT __rep() const { return _M_value; }
1460 // These bits have to be at the end of this file, so that the
1461 // specializations have all been defined.
1462 inline _GLIBCXX_CONSTEXPR
1463 complex<float>::complex(const complex<double>& __z)
1464 : _M_value(__z.__rep()) { }
1466 inline _GLIBCXX_CONSTEXPR
1467 complex<float>::complex(const complex<long double>& __z)
1468 : _M_value(__z.__rep()) { }
1470 inline _GLIBCXX_CONSTEXPR
1471 complex<double>::complex(const complex<long double>& __z)
1472 : _M_value(__z.__rep()) { }
1474 // Inhibit implicit instantiations for required instantiations,
1475 // which are defined via explicit instantiations elsewhere.
1476 // NB: This syntax is a GNU extension.
1477 #if _GLIBCXX_EXTERN_TEMPLATE
1478 extern template istream& operator>>(istream&, complex<float>&);
1479 extern template ostream& operator<<(ostream&, const complex<float>&);
1480 extern template istream& operator>>(istream&, complex<double>&);
1481 extern template ostream& operator<<(ostream&, const complex<double>&);
1482 extern template istream& operator>>(istream&, complex<long double>&);
1483 extern template ostream& operator<<(ostream&, const complex<long double>&);
1485 #ifdef _GLIBCXX_USE_WCHAR_T
1486 extern template wistream& operator>>(wistream&, complex<float>&);
1487 extern template wostream& operator<<(wostream&, const complex<float>&);
1488 extern template wistream& operator>>(wistream&, complex<double>&);
1489 extern template wostream& operator<<(wostream&, const complex<double>&);
1490 extern template wistream& operator>>(wistream&, complex<long double>&);
1491 extern template wostream& operator<<(wostream&, const complex<long double>&);
1495 // @} group complex_numbers
1497 _GLIBCXX_END_NAMESPACE
1499 _GLIBCXX_BEGIN_NAMESPACE(__gnu_cxx)
1501 // See ext/type_traits.h for the primary template.
1502 template<typename _Tp, typename _Up>
1503 struct __promote_2<std::complex<_Tp>, _Up>
1506 typedef std::complex<typename __promote_2<_Tp, _Up>::__type> __type;
1509 template<typename _Tp, typename _Up>
1510 struct __promote_2<_Tp, std::complex<_Up> >
1513 typedef std::complex<typename __promote_2<_Tp, _Up>::__type> __type;
1516 template<typename _Tp, typename _Up>
1517 struct __promote_2<std::complex<_Tp>, std::complex<_Up> >
1520 typedef std::complex<typename __promote_2<_Tp, _Up>::__type> __type;
1523 _GLIBCXX_END_NAMESPACE
1525 #ifdef __GXX_EXPERIMENTAL_CXX0X__
1527 _GLIBCXX_BEGIN_NAMESPACE(std)
1529 // Forward declarations.
1530 template<typename _Tp> std::complex<_Tp> acos(const std::complex<_Tp>&);
1531 template<typename _Tp> std::complex<_Tp> asin(const std::complex<_Tp>&);
1532 template<typename _Tp> std::complex<_Tp> atan(const std::complex<_Tp>&);
1534 template<typename _Tp> std::complex<_Tp> acosh(const std::complex<_Tp>&);
1535 template<typename _Tp> std::complex<_Tp> asinh(const std::complex<_Tp>&);
1536 template<typename _Tp> std::complex<_Tp> atanh(const std::complex<_Tp>&);
1538 template<typename _Tp> _Tp fabs(const std::complex<_Tp>&);
1540 template<typename _Tp>
1541 inline std::complex<_Tp>
1542 __complex_acos(const std::complex<_Tp>& __z)
1544 const std::complex<_Tp> __t = std::asin(__z);
1545 const _Tp __pi_2 = 1.5707963267948966192313216916397514L;
1546 return std::complex<_Tp>(__pi_2 - __t.real(), -__t.imag());
1549 #if _GLIBCXX_USE_C99_COMPLEX_TR1
1550 inline __complex__ float
1551 __complex_acos(__complex__ float __z)
1552 { return __builtin_cacosf(__z); }
1554 inline __complex__ double
1555 __complex_acos(__complex__ double __z)
1556 { return __builtin_cacos(__z); }
1558 inline __complex__ long double
1559 __complex_acos(const __complex__ long double& __z)
1560 { return __builtin_cacosl(__z); }
1562 template<typename _Tp>
1563 inline std::complex<_Tp>
1564 acos(const std::complex<_Tp>& __z)
1565 { return __complex_acos(__z.__rep()); }
1567 /// acos(__z) [8.1.2].
1568 // Effects: Behaves the same as C99 function cacos, defined
1569 // in subclause 7.3.5.1.
1570 template<typename _Tp>
1571 inline std::complex<_Tp>
1572 acos(const std::complex<_Tp>& __z)
1573 { return __complex_acos(__z); }
1576 template<typename _Tp>
1577 inline std::complex<_Tp>
1578 __complex_asin(const std::complex<_Tp>& __z)
1580 std::complex<_Tp> __t(-__z.imag(), __z.real());
1581 __t = std::asinh(__t);
1582 return std::complex<_Tp>(__t.imag(), -__t.real());
1585 #if _GLIBCXX_USE_C99_COMPLEX_TR1
1586 inline __complex__ float
1587 __complex_asin(__complex__ float __z)
1588 { return __builtin_casinf(__z); }
1590 inline __complex__ double
1591 __complex_asin(__complex__ double __z)
1592 { return __builtin_casin(__z); }
1594 inline __complex__ long double
1595 __complex_asin(const __complex__ long double& __z)
1596 { return __builtin_casinl(__z); }
1598 template<typename _Tp>
1599 inline std::complex<_Tp>
1600 asin(const std::complex<_Tp>& __z)
1601 { return __complex_asin(__z.__rep()); }
1603 /// asin(__z) [8.1.3].
1604 // Effects: Behaves the same as C99 function casin, defined
1605 // in subclause 7.3.5.2.
1606 template<typename _Tp>
1607 inline std::complex<_Tp>
1608 asin(const std::complex<_Tp>& __z)
1609 { return __complex_asin(__z); }
1612 template<typename _Tp>
1614 __complex_atan(const std::complex<_Tp>& __z)
1616 const _Tp __r2 = __z.real() * __z.real();
1617 const _Tp __x = _Tp(1.0) - __r2 - __z.imag() * __z.imag();
1619 _Tp __num = __z.imag() + _Tp(1.0);
1620 _Tp __den = __z.imag() - _Tp(1.0);
1622 __num = __r2 + __num * __num;
1623 __den = __r2 + __den * __den;
1625 return std::complex<_Tp>(_Tp(0.5) * atan2(_Tp(2.0) * __z.real(), __x),
1626 _Tp(0.25) * log(__num / __den));
1629 #if _GLIBCXX_USE_C99_COMPLEX_TR1
1630 inline __complex__ float
1631 __complex_atan(__complex__ float __z)
1632 { return __builtin_catanf(__z); }
1634 inline __complex__ double
1635 __complex_atan(__complex__ double __z)
1636 { return __builtin_catan(__z); }
1638 inline __complex__ long double
1639 __complex_atan(const __complex__ long double& __z)
1640 { return __builtin_catanl(__z); }
1642 template<typename _Tp>
1643 inline std::complex<_Tp>
1644 atan(const std::complex<_Tp>& __z)
1645 { return __complex_atan(__z.__rep()); }
1647 /// atan(__z) [8.1.4].
1648 // Effects: Behaves the same as C99 function catan, defined
1649 // in subclause 7.3.5.3.
1650 template<typename _Tp>
1651 inline std::complex<_Tp>
1652 atan(const std::complex<_Tp>& __z)
1653 { return __complex_atan(__z); }
1656 template<typename _Tp>
1658 __complex_acosh(const std::complex<_Tp>& __z)
1660 std::complex<_Tp> __t((__z.real() - __z.imag())
1661 * (__z.real() + __z.imag()) - _Tp(1.0),
1662 _Tp(2.0) * __z.real() * __z.imag());
1663 __t = std::sqrt(__t);
1665 return std::log(__t + __z);
1668 #if _GLIBCXX_USE_C99_COMPLEX_TR1
1669 inline __complex__ float
1670 __complex_acosh(__complex__ float __z)
1671 { return __builtin_cacoshf(__z); }
1673 inline __complex__ double
1674 __complex_acosh(__complex__ double __z)
1675 { return __builtin_cacosh(__z); }
1677 inline __complex__ long double
1678 __complex_acosh(const __complex__ long double& __z)
1679 { return __builtin_cacoshl(__z); }
1681 template<typename _Tp>
1682 inline std::complex<_Tp>
1683 acosh(const std::complex<_Tp>& __z)
1684 { return __complex_acosh(__z.__rep()); }
1686 /// acosh(__z) [8.1.5].
1687 // Effects: Behaves the same as C99 function cacosh, defined
1688 // in subclause 7.3.6.1.
1689 template<typename _Tp>
1690 inline std::complex<_Tp>
1691 acosh(const std::complex<_Tp>& __z)
1692 { return __complex_acosh(__z); }
1695 template<typename _Tp>
1697 __complex_asinh(const std::complex<_Tp>& __z)
1699 std::complex<_Tp> __t((__z.real() - __z.imag())
1700 * (__z.real() + __z.imag()) + _Tp(1.0),
1701 _Tp(2.0) * __z.real() * __z.imag());
1702 __t = std::sqrt(__t);
1704 return std::log(__t + __z);
1707 #if _GLIBCXX_USE_C99_COMPLEX_TR1
1708 inline __complex__ float
1709 __complex_asinh(__complex__ float __z)
1710 { return __builtin_casinhf(__z); }
1712 inline __complex__ double
1713 __complex_asinh(__complex__ double __z)
1714 { return __builtin_casinh(__z); }
1716 inline __complex__ long double
1717 __complex_asinh(const __complex__ long double& __z)
1718 { return __builtin_casinhl(__z); }
1720 template<typename _Tp>
1721 inline std::complex<_Tp>
1722 asinh(const std::complex<_Tp>& __z)
1723 { return __complex_asinh(__z.__rep()); }
1725 /// asinh(__z) [8.1.6].
1726 // Effects: Behaves the same as C99 function casin, defined
1727 // in subclause 7.3.6.2.
1728 template<typename _Tp>
1729 inline std::complex<_Tp>
1730 asinh(const std::complex<_Tp>& __z)
1731 { return __complex_asinh(__z); }
1734 template<typename _Tp>
1736 __complex_atanh(const std::complex<_Tp>& __z)
1738 const _Tp __i2 = __z.imag() * __z.imag();
1739 const _Tp __x = _Tp(1.0) - __i2 - __z.real() * __z.real();
1741 _Tp __num = _Tp(1.0) + __z.real();
1742 _Tp __den = _Tp(1.0) - __z.real();
1744 __num = __i2 + __num * __num;
1745 __den = __i2 + __den * __den;
1747 return std::complex<_Tp>(_Tp(0.25) * (log(__num) - log(__den)),
1748 _Tp(0.5) * atan2(_Tp(2.0) * __z.imag(), __x));
1751 #if _GLIBCXX_USE_C99_COMPLEX_TR1
1752 inline __complex__ float
1753 __complex_atanh(__complex__ float __z)
1754 { return __builtin_catanhf(__z); }
1756 inline __complex__ double
1757 __complex_atanh(__complex__ double __z)
1758 { return __builtin_catanh(__z); }
1760 inline __complex__ long double
1761 __complex_atanh(const __complex__ long double& __z)
1762 { return __builtin_catanhl(__z); }
1764 template<typename _Tp>
1765 inline std::complex<_Tp>
1766 atanh(const std::complex<_Tp>& __z)
1767 { return __complex_atanh(__z.__rep()); }
1769 /// atanh(__z) [8.1.7].
1770 // Effects: Behaves the same as C99 function catanh, defined
1771 // in subclause 7.3.6.3.
1772 template<typename _Tp>
1773 inline std::complex<_Tp>
1774 atanh(const std::complex<_Tp>& __z)
1775 { return __complex_atanh(__z); }
1778 template<typename _Tp>
1780 /// fabs(__z) [8.1.8].
1781 // Effects: Behaves the same as C99 function cabs, defined
1782 // in subclause 7.3.8.1.
1783 fabs(const std::complex<_Tp>& __z)
1784 { return std::abs(__z); }
1786 /// Additional overloads [8.1.9].
1787 template<typename _Tp>
1788 inline typename __gnu_cxx::__promote<_Tp>::__type
1791 typedef typename __gnu_cxx::__promote<_Tp>::__type __type;
1792 #if (_GLIBCXX_USE_C99_MATH && !_GLIBCXX_USE_C99_FP_MACROS_DYNAMIC)
1793 return std::signbit(__x) ? __type(3.1415926535897932384626433832795029L)
1796 return std::arg(std::complex<__type>(__x));
1800 template<typename _Tp>
1801 inline typename __gnu_cxx::__promote<_Tp>::__type
1805 template<typename _Tp>
1806 inline typename __gnu_cxx::__promote<_Tp>::__type
1809 typedef typename __gnu_cxx::__promote<_Tp>::__type __type;
1810 return __type(__x) * __type(__x);
1813 template<typename _Tp>
1814 inline typename __gnu_cxx::__promote<_Tp>::__type
1818 template<typename _Tp, typename _Up>
1819 inline std::complex<typename __gnu_cxx::__promote_2<_Tp, _Up>::__type>
1820 pow(const std::complex<_Tp>& __x, const _Up& __y)
1822 typedef typename __gnu_cxx::__promote_2<_Tp, _Up>::__type __type;
1823 return std::pow(std::complex<__type>(__x), __type(__y));
1826 template<typename _Tp, typename _Up>
1827 inline std::complex<typename __gnu_cxx::__promote_2<_Tp, _Up>::__type>
1828 pow(const _Tp& __x, const std::complex<_Up>& __y)
1830 typedef typename __gnu_cxx::__promote_2<_Tp, _Up>::__type __type;
1831 return std::pow(__type(__x), std::complex<__type>(__y));
1834 template<typename _Tp, typename _Up>
1835 inline std::complex<typename __gnu_cxx::__promote_2<_Tp, _Up>::__type>
1836 pow(const std::complex<_Tp>& __x, const std::complex<_Up>& __y)
1838 typedef typename __gnu_cxx::__promote_2<_Tp, _Up>::__type __type;
1839 return std::pow(std::complex<__type>(__x),
1840 std::complex<__type>(__y));
1843 // Forward declarations.
1845 template<typename _Tp> std::complex<_Tp> proj(const std::complex<_Tp>&);
1847 template<typename _Tp>
1849 __complex_proj(const std::complex<_Tp>& __z)
1851 const _Tp __den = (__z.real() * __z.real()
1852 + __z.imag() * __z.imag() + _Tp(1.0));
1854 return std::complex<_Tp>((_Tp(2.0) * __z.real()) / __den,
1855 (_Tp(2.0) * __z.imag()) / __den);
1858 #if _GLIBCXX_USE_C99_COMPLEX
1859 inline __complex__ float
1860 __complex_proj(__complex__ float __z)
1861 { return __builtin_cprojf(__z); }
1863 inline __complex__ double
1864 __complex_proj(__complex__ double __z)
1865 { return __builtin_cproj(__z); }
1867 inline __complex__ long double
1868 __complex_proj(const __complex__ long double& __z)
1869 { return __builtin_cprojl(__z); }
1871 template<typename _Tp>
1872 inline std::complex<_Tp>
1873 proj(const std::complex<_Tp>& __z)
1874 { return __complex_proj(__z.__rep()); }
1876 template<typename _Tp>
1877 inline std::complex<_Tp>
1878 proj(const std::complex<_Tp>& __z)
1879 { return __complex_proj(__z); }
1883 template<typename _Tp>
1884 inline typename __gnu_cxx::__promote<_Tp>::__type
1888 template<typename _Tp>
1889 inline typename __gnu_cxx::__promote<_Tp>::__type
1893 _GLIBCXX_END_NAMESPACE
1895 #endif // __GXX_EXPERIMENTAL_CXX0X__
1897 #endif /* _GLIBCXX_COMPLEX */