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, 2011
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 namespace std _GLIBCXX_VISIBILITY(default)
51 _GLIBCXX_BEGIN_NAMESPACE_VERSION
54 * @defgroup complex_numbers Complex Numbers
57 * Classes and functions for complex numbers.
61 // Forward declarations.
62 template<typename _Tp> class complex;
63 template<> class complex<float>;
64 template<> class complex<double>;
65 template<> class complex<long double>;
67 /// Return magnitude of @a z.
68 template<typename _Tp> _Tp abs(const complex<_Tp>&);
69 /// Return phase angle of @a z.
70 template<typename _Tp> _Tp arg(const complex<_Tp>&);
71 /// Return @a z magnitude squared.
72 template<typename _Tp> _Tp norm(const complex<_Tp>&);
74 /// Return complex conjugate of @a z.
75 template<typename _Tp> complex<_Tp> conj(const complex<_Tp>&);
76 /// Return complex with magnitude @a rho and angle @a theta.
77 template<typename _Tp> complex<_Tp> polar(const _Tp&, const _Tp& = 0);
80 /// Return complex cosine of @a z.
81 template<typename _Tp> complex<_Tp> cos(const complex<_Tp>&);
82 /// Return complex hyperbolic cosine of @a z.
83 template<typename _Tp> complex<_Tp> cosh(const complex<_Tp>&);
84 /// Return complex base e exponential of @a z.
85 template<typename _Tp> complex<_Tp> exp(const complex<_Tp>&);
86 /// Return complex natural logarithm of @a z.
87 template<typename _Tp> complex<_Tp> log(const complex<_Tp>&);
88 /// Return complex base 10 logarithm of @a z.
89 template<typename _Tp> complex<_Tp> log10(const complex<_Tp>&);
90 #ifndef __GXX_EXPERIMENTAL_CXX0X__
92 /// Return @a x to the @a y'th power.
93 template<typename _Tp> complex<_Tp> pow(const complex<_Tp>&, int);
95 /// Return @a x to the @a y'th power.
96 template<typename _Tp> complex<_Tp> pow(const complex<_Tp>&, const _Tp&);
97 /// Return @a x to the @a y'th power.
98 template<typename _Tp> complex<_Tp> pow(const complex<_Tp>&,
100 /// Return @a x to the @a y'th power.
101 template<typename _Tp> complex<_Tp> pow(const _Tp&, const complex<_Tp>&);
102 /// Return complex sine of @a z.
103 template<typename _Tp> complex<_Tp> sin(const complex<_Tp>&);
104 /// Return complex hyperbolic sine of @a z.
105 template<typename _Tp> complex<_Tp> sinh(const complex<_Tp>&);
106 /// Return complex square root of @a z.
107 template<typename _Tp> complex<_Tp> sqrt(const complex<_Tp>&);
108 /// Return complex tangent of @a z.
109 template<typename _Tp> complex<_Tp> tan(const complex<_Tp>&);
110 /// Return complex hyperbolic tangent of @a z.
111 template<typename _Tp> complex<_Tp> tanh(const complex<_Tp>&);
114 // 26.2.2 Primary template class complex
116 * Template to represent complex numbers.
118 * Specializations for float, double, and long double are part of the
119 * library. Results with any other type are not guaranteed.
121 * @param Tp Type of real and imaginary values.
123 template<typename _Tp>
127 typedef _Tp value_type;
129 /// Default constructor. First parameter is x, second parameter is y.
130 /// Unspecified parameters default to 0.
131 _GLIBCXX_CONSTEXPR complex(const _Tp& __r = _Tp(), const _Tp& __i = _Tp())
132 : _M_real(__r), _M_imag(__i) { }
134 // Lets the compiler synthesize the copy constructor
135 // complex (const complex<_Tp>&);
136 /// Copy constructor.
137 template<typename _Up>
138 _GLIBCXX_CONSTEXPR complex(const complex<_Up>& __z)
139 : _M_real(__z.real()), _M_imag(__z.imag()) { }
141 #ifdef __GXX_EXPERIMENTAL_CXX0X__
142 // _GLIBCXX_RESOLVE_LIB_DEFECTS
143 // DR 387. std::complex over-encapsulated.
145 real() const { return _M_real; }
148 imag() const { return _M_imag; }
150 /// Return real part of complex number.
152 real() { return _M_real; }
154 /// Return real part of complex number.
156 real() const { return _M_real; }
158 /// Return imaginary part of complex number.
160 imag() { return _M_imag; }
162 /// Return imaginary part of complex number.
164 imag() const { return _M_imag; }
167 // _GLIBCXX_RESOLVE_LIB_DEFECTS
168 // DR 387. std::complex over-encapsulated.
170 real(_Tp __val) { _M_real = __val; }
173 imag(_Tp __val) { _M_imag = __val; }
175 /// Assign this complex number to scalar @a t.
176 complex<_Tp>& operator=(const _Tp&);
178 /// Add @a t to this complex number.
181 operator+=(const _Tp& __t)
187 /// Subtract @a t from this complex number.
190 operator-=(const _Tp& __t)
196 /// Multiply this complex number by @a t.
197 complex<_Tp>& operator*=(const _Tp&);
198 /// Divide this complex number by @a t.
199 complex<_Tp>& operator/=(const _Tp&);
201 // Lets the compiler synthesize the
202 // copy and assignment operator
203 // complex<_Tp>& operator= (const complex<_Tp>&);
204 /// Assign this complex number to complex @a z.
205 template<typename _Up>
206 complex<_Tp>& operator=(const complex<_Up>&);
207 /// Add @a z to this complex number.
208 template<typename _Up>
209 complex<_Tp>& operator+=(const complex<_Up>&);
210 /// Subtract @a z from this complex number.
211 template<typename _Up>
212 complex<_Tp>& operator-=(const complex<_Up>&);
213 /// Multiply this complex number by @a z.
214 template<typename _Up>
215 complex<_Tp>& operator*=(const complex<_Up>&);
216 /// Divide this complex number by @a z.
217 template<typename _Up>
218 complex<_Tp>& operator/=(const complex<_Up>&);
220 _GLIBCXX_USE_CONSTEXPR complex __rep() const
228 template<typename _Tp>
230 complex<_Tp>::operator=(const _Tp& __t)
238 template<typename _Tp>
240 complex<_Tp>::operator*=(const _Tp& __t)
248 template<typename _Tp>
250 complex<_Tp>::operator/=(const _Tp& __t)
257 template<typename _Tp>
258 template<typename _Up>
260 complex<_Tp>::operator=(const complex<_Up>& __z)
262 _M_real = __z.real();
263 _M_imag = __z.imag();
268 template<typename _Tp>
269 template<typename _Up>
271 complex<_Tp>::operator+=(const complex<_Up>& __z)
273 _M_real += __z.real();
274 _M_imag += __z.imag();
279 template<typename _Tp>
280 template<typename _Up>
282 complex<_Tp>::operator-=(const complex<_Up>& __z)
284 _M_real -= __z.real();
285 _M_imag -= __z.imag();
290 // XXX: This is a grammar school implementation.
291 template<typename _Tp>
292 template<typename _Up>
294 complex<_Tp>::operator*=(const complex<_Up>& __z)
296 const _Tp __r = _M_real * __z.real() - _M_imag * __z.imag();
297 _M_imag = _M_real * __z.imag() + _M_imag * __z.real();
303 // XXX: This is a grammar school implementation.
304 template<typename _Tp>
305 template<typename _Up>
307 complex<_Tp>::operator/=(const complex<_Up>& __z)
309 const _Tp __r = _M_real * __z.real() + _M_imag * __z.imag();
310 const _Tp __n = std::norm(__z);
311 _M_imag = (_M_imag * __z.real() - _M_real * __z.imag()) / __n;
318 /// Return new complex value @a x plus @a y.
319 template<typename _Tp>
321 operator+(const complex<_Tp>& __x, const complex<_Tp>& __y)
323 complex<_Tp> __r = __x;
328 template<typename _Tp>
330 operator+(const complex<_Tp>& __x, const _Tp& __y)
332 complex<_Tp> __r = __x;
337 template<typename _Tp>
339 operator+(const _Tp& __x, const complex<_Tp>& __y)
341 complex<_Tp> __r = __y;
348 /// Return new complex value @a x minus @a y.
349 template<typename _Tp>
351 operator-(const complex<_Tp>& __x, const complex<_Tp>& __y)
353 complex<_Tp> __r = __x;
358 template<typename _Tp>
360 operator-(const complex<_Tp>& __x, const _Tp& __y)
362 complex<_Tp> __r = __x;
367 template<typename _Tp>
369 operator-(const _Tp& __x, const complex<_Tp>& __y)
371 complex<_Tp> __r(__x, -__y.imag());
378 /// Return new complex value @a x times @a y.
379 template<typename _Tp>
381 operator*(const complex<_Tp>& __x, const complex<_Tp>& __y)
383 complex<_Tp> __r = __x;
388 template<typename _Tp>
390 operator*(const complex<_Tp>& __x, const _Tp& __y)
392 complex<_Tp> __r = __x;
397 template<typename _Tp>
399 operator*(const _Tp& __x, const complex<_Tp>& __y)
401 complex<_Tp> __r = __y;
408 /// Return new complex value @a x divided by @a y.
409 template<typename _Tp>
411 operator/(const complex<_Tp>& __x, const complex<_Tp>& __y)
413 complex<_Tp> __r = __x;
418 template<typename _Tp>
420 operator/(const complex<_Tp>& __x, const _Tp& __y)
422 complex<_Tp> __r = __x;
427 template<typename _Tp>
429 operator/(const _Tp& __x, const complex<_Tp>& __y)
431 complex<_Tp> __r = __x;
438 template<typename _Tp>
440 operator+(const complex<_Tp>& __x)
443 /// Return complex negation of @a x.
444 template<typename _Tp>
446 operator-(const complex<_Tp>& __x)
447 { return complex<_Tp>(-__x.real(), -__x.imag()); }
450 /// Return true if @a x is equal to @a y.
451 template<typename _Tp>
452 inline _GLIBCXX_CONSTEXPR bool
453 operator==(const complex<_Tp>& __x, const complex<_Tp>& __y)
454 { return __x.real() == __y.real() && __x.imag() == __y.imag(); }
456 template<typename _Tp>
457 inline _GLIBCXX_CONSTEXPR bool
458 operator==(const complex<_Tp>& __x, const _Tp& __y)
459 { return __x.real() == __y && __x.imag() == _Tp(); }
461 template<typename _Tp>
462 inline _GLIBCXX_CONSTEXPR bool
463 operator==(const _Tp& __x, const complex<_Tp>& __y)
464 { return __x == __y.real() && _Tp() == __y.imag(); }
468 /// Return false if @a x is equal to @a y.
469 template<typename _Tp>
470 inline _GLIBCXX_CONSTEXPR bool
471 operator!=(const complex<_Tp>& __x, const complex<_Tp>& __y)
472 { return __x.real() != __y.real() || __x.imag() != __y.imag(); }
474 template<typename _Tp>
475 inline _GLIBCXX_CONSTEXPR bool
476 operator!=(const complex<_Tp>& __x, const _Tp& __y)
477 { return __x.real() != __y || __x.imag() != _Tp(); }
479 template<typename _Tp>
480 inline _GLIBCXX_CONSTEXPR bool
481 operator!=(const _Tp& __x, const complex<_Tp>& __y)
482 { return __x != __y.real() || _Tp() != __y.imag(); }
485 /// Extraction operator for complex values.
486 template<typename _Tp, typename _CharT, class _Traits>
487 basic_istream<_CharT, _Traits>&
488 operator>>(basic_istream<_CharT, _Traits>& __is, complex<_Tp>& __x)
495 __is >> __re_x >> __ch;
498 __is >> __im_x >> __ch;
500 __x = complex<_Tp>(__re_x, __im_x);
502 __is.setstate(ios_base::failbit);
504 else if (__ch == ')')
507 __is.setstate(ios_base::failbit);
518 /// Insertion operator for complex values.
519 template<typename _Tp, typename _CharT, class _Traits>
520 basic_ostream<_CharT, _Traits>&
521 operator<<(basic_ostream<_CharT, _Traits>& __os, const complex<_Tp>& __x)
523 basic_ostringstream<_CharT, _Traits> __s;
524 __s.flags(__os.flags());
525 __s.imbue(__os.getloc());
526 __s.precision(__os.precision());
527 __s << '(' << __x.real() << ',' << __x.imag() << ')';
528 return __os << __s.str();
532 #ifdef __GXX_EXPERIMENTAL_CXX0X__
533 template<typename _Tp>
535 real(const complex<_Tp>& __z)
536 { return __z.real(); }
538 template<typename _Tp>
540 imag(const complex<_Tp>& __z)
541 { return __z.imag(); }
543 template<typename _Tp>
545 real(complex<_Tp>& __z)
546 { return __z.real(); }
548 template<typename _Tp>
550 real(const complex<_Tp>& __z)
551 { return __z.real(); }
553 template<typename _Tp>
555 imag(complex<_Tp>& __z)
556 { return __z.imag(); }
558 template<typename _Tp>
560 imag(const complex<_Tp>& __z)
561 { return __z.imag(); }
564 // 26.2.7/3 abs(__z): Returns the magnitude of __z.
565 template<typename _Tp>
567 __complex_abs(const complex<_Tp>& __z)
569 _Tp __x = __z.real();
570 _Tp __y = __z.imag();
571 const _Tp __s = std::max(abs(__x), abs(__y));
572 if (__s == _Tp()) // well ...
576 return __s * sqrt(__x * __x + __y * __y);
579 #if _GLIBCXX_USE_C99_COMPLEX
581 __complex_abs(__complex__ float __z) { return __builtin_cabsf(__z); }
584 __complex_abs(__complex__ double __z) { return __builtin_cabs(__z); }
587 __complex_abs(const __complex__ long double& __z)
588 { return __builtin_cabsl(__z); }
590 template<typename _Tp>
592 abs(const complex<_Tp>& __z) { return __complex_abs(__z.__rep()); }
594 template<typename _Tp>
596 abs(const complex<_Tp>& __z) { return __complex_abs(__z); }
600 // 26.2.7/4: arg(__z): Returns the phase angle of __z.
601 template<typename _Tp>
603 __complex_arg(const complex<_Tp>& __z)
604 { return atan2(__z.imag(), __z.real()); }
606 #if _GLIBCXX_USE_C99_COMPLEX
608 __complex_arg(__complex__ float __z) { return __builtin_cargf(__z); }
611 __complex_arg(__complex__ double __z) { return __builtin_carg(__z); }
614 __complex_arg(const __complex__ long double& __z)
615 { return __builtin_cargl(__z); }
617 template<typename _Tp>
619 arg(const complex<_Tp>& __z) { return __complex_arg(__z.__rep()); }
621 template<typename _Tp>
623 arg(const complex<_Tp>& __z) { return __complex_arg(__z); }
626 // 26.2.7/5: norm(__z) returns the squared magnitude of __z.
627 // As defined, norm() is -not- a norm is the common mathematical
628 // sens used in numerics. The helper class _Norm_helper<> tries to
629 // distinguish between builtin floating point and the rest, so as
630 // to deliver an answer as close as possible to the real value.
634 template<typename _Tp>
635 static inline _Tp _S_do_it(const complex<_Tp>& __z)
637 const _Tp __x = __z.real();
638 const _Tp __y = __z.imag();
639 return __x * __x + __y * __y;
644 struct _Norm_helper<true>
646 template<typename _Tp>
647 static inline _Tp _S_do_it(const complex<_Tp>& __z)
649 _Tp __res = std::abs(__z);
650 return __res * __res;
654 template<typename _Tp>
656 norm(const complex<_Tp>& __z)
658 return _Norm_helper<__is_floating<_Tp>::__value
659 && !_GLIBCXX_FAST_MATH>::_S_do_it(__z);
662 template<typename _Tp>
664 polar(const _Tp& __rho, const _Tp& __theta)
665 { return complex<_Tp>(__rho * cos(__theta), __rho * sin(__theta)); }
667 template<typename _Tp>
669 conj(const complex<_Tp>& __z)
670 { return complex<_Tp>(__z.real(), -__z.imag()); }
674 // 26.2.8/1 cos(__z): Returns the cosine of __z.
675 template<typename _Tp>
677 __complex_cos(const complex<_Tp>& __z)
679 const _Tp __x = __z.real();
680 const _Tp __y = __z.imag();
681 return complex<_Tp>(cos(__x) * cosh(__y), -sin(__x) * sinh(__y));
684 #if _GLIBCXX_USE_C99_COMPLEX
685 inline __complex__ float
686 __complex_cos(__complex__ float __z) { return __builtin_ccosf(__z); }
688 inline __complex__ double
689 __complex_cos(__complex__ double __z) { return __builtin_ccos(__z); }
691 inline __complex__ long double
692 __complex_cos(const __complex__ long double& __z)
693 { return __builtin_ccosl(__z); }
695 template<typename _Tp>
697 cos(const complex<_Tp>& __z) { return __complex_cos(__z.__rep()); }
699 template<typename _Tp>
701 cos(const complex<_Tp>& __z) { return __complex_cos(__z); }
704 // 26.2.8/2 cosh(__z): Returns the hyperbolic cosine of __z.
705 template<typename _Tp>
707 __complex_cosh(const complex<_Tp>& __z)
709 const _Tp __x = __z.real();
710 const _Tp __y = __z.imag();
711 return complex<_Tp>(cosh(__x) * cos(__y), sinh(__x) * sin(__y));
714 #if _GLIBCXX_USE_C99_COMPLEX
715 inline __complex__ float
716 __complex_cosh(__complex__ float __z) { return __builtin_ccoshf(__z); }
718 inline __complex__ double
719 __complex_cosh(__complex__ double __z) { return __builtin_ccosh(__z); }
721 inline __complex__ long double
722 __complex_cosh(const __complex__ long double& __z)
723 { return __builtin_ccoshl(__z); }
725 template<typename _Tp>
727 cosh(const complex<_Tp>& __z) { return __complex_cosh(__z.__rep()); }
729 template<typename _Tp>
731 cosh(const complex<_Tp>& __z) { return __complex_cosh(__z); }
734 // 26.2.8/3 exp(__z): Returns the complex base e exponential of x
735 template<typename _Tp>
737 __complex_exp(const complex<_Tp>& __z)
738 { return std::polar(exp(__z.real()), __z.imag()); }
740 #if _GLIBCXX_USE_C99_COMPLEX
741 inline __complex__ float
742 __complex_exp(__complex__ float __z) { return __builtin_cexpf(__z); }
744 inline __complex__ double
745 __complex_exp(__complex__ double __z) { return __builtin_cexp(__z); }
747 inline __complex__ long double
748 __complex_exp(const __complex__ long double& __z)
749 { return __builtin_cexpl(__z); }
751 template<typename _Tp>
753 exp(const complex<_Tp>& __z) { return __complex_exp(__z.__rep()); }
755 template<typename _Tp>
757 exp(const complex<_Tp>& __z) { return __complex_exp(__z); }
760 // 26.2.8/5 log(__z): Returns the natural complex logarithm of __z.
761 // The branch cut is along the negative axis.
762 template<typename _Tp>
764 __complex_log(const complex<_Tp>& __z)
765 { return complex<_Tp>(log(std::abs(__z)), std::arg(__z)); }
767 #if _GLIBCXX_USE_C99_COMPLEX
768 inline __complex__ float
769 __complex_log(__complex__ float __z) { return __builtin_clogf(__z); }
771 inline __complex__ double
772 __complex_log(__complex__ double __z) { return __builtin_clog(__z); }
774 inline __complex__ long double
775 __complex_log(const __complex__ long double& __z)
776 { return __builtin_clogl(__z); }
778 template<typename _Tp>
780 log(const complex<_Tp>& __z) { return __complex_log(__z.__rep()); }
782 template<typename _Tp>
784 log(const complex<_Tp>& __z) { return __complex_log(__z); }
787 template<typename _Tp>
789 log10(const complex<_Tp>& __z)
790 { return std::log(__z) / log(_Tp(10.0)); }
792 // 26.2.8/10 sin(__z): Returns the sine of __z.
793 template<typename _Tp>
795 __complex_sin(const complex<_Tp>& __z)
797 const _Tp __x = __z.real();
798 const _Tp __y = __z.imag();
799 return complex<_Tp>(sin(__x) * cosh(__y), cos(__x) * sinh(__y));
802 #if _GLIBCXX_USE_C99_COMPLEX
803 inline __complex__ float
804 __complex_sin(__complex__ float __z) { return __builtin_csinf(__z); }
806 inline __complex__ double
807 __complex_sin(__complex__ double __z) { return __builtin_csin(__z); }
809 inline __complex__ long double
810 __complex_sin(const __complex__ long double& __z)
811 { return __builtin_csinl(__z); }
813 template<typename _Tp>
815 sin(const complex<_Tp>& __z) { return __complex_sin(__z.__rep()); }
817 template<typename _Tp>
819 sin(const complex<_Tp>& __z) { return __complex_sin(__z); }
822 // 26.2.8/11 sinh(__z): Returns the hyperbolic sine of __z.
823 template<typename _Tp>
825 __complex_sinh(const complex<_Tp>& __z)
827 const _Tp __x = __z.real();
828 const _Tp __y = __z.imag();
829 return complex<_Tp>(sinh(__x) * cos(__y), cosh(__x) * sin(__y));
832 #if _GLIBCXX_USE_C99_COMPLEX
833 inline __complex__ float
834 __complex_sinh(__complex__ float __z) { return __builtin_csinhf(__z); }
836 inline __complex__ double
837 __complex_sinh(__complex__ double __z) { return __builtin_csinh(__z); }
839 inline __complex__ long double
840 __complex_sinh(const __complex__ long double& __z)
841 { return __builtin_csinhl(__z); }
843 template<typename _Tp>
845 sinh(const complex<_Tp>& __z) { return __complex_sinh(__z.__rep()); }
847 template<typename _Tp>
849 sinh(const complex<_Tp>& __z) { return __complex_sinh(__z); }
852 // 26.2.8/13 sqrt(__z): Returns the complex square root of __z.
853 // The branch cut is on the negative axis.
854 template<typename _Tp>
856 __complex_sqrt(const complex<_Tp>& __z)
858 _Tp __x = __z.real();
859 _Tp __y = __z.imag();
863 _Tp __t = sqrt(abs(__y) / 2);
864 return complex<_Tp>(__t, __y < _Tp() ? -__t : __t);
868 _Tp __t = sqrt(2 * (std::abs(__z) + abs(__x)));
871 ? complex<_Tp>(__u, __y / __t)
872 : complex<_Tp>(abs(__y) / __t, __y < _Tp() ? -__u : __u);
876 #if _GLIBCXX_USE_C99_COMPLEX
877 inline __complex__ float
878 __complex_sqrt(__complex__ float __z) { return __builtin_csqrtf(__z); }
880 inline __complex__ double
881 __complex_sqrt(__complex__ double __z) { return __builtin_csqrt(__z); }
883 inline __complex__ long double
884 __complex_sqrt(const __complex__ long double& __z)
885 { return __builtin_csqrtl(__z); }
887 template<typename _Tp>
889 sqrt(const complex<_Tp>& __z) { return __complex_sqrt(__z.__rep()); }
891 template<typename _Tp>
893 sqrt(const complex<_Tp>& __z) { return __complex_sqrt(__z); }
896 // 26.2.8/14 tan(__z): Return the complex tangent of __z.
898 template<typename _Tp>
900 __complex_tan(const complex<_Tp>& __z)
901 { return std::sin(__z) / std::cos(__z); }
903 #if _GLIBCXX_USE_C99_COMPLEX
904 inline __complex__ float
905 __complex_tan(__complex__ float __z) { return __builtin_ctanf(__z); }
907 inline __complex__ double
908 __complex_tan(__complex__ double __z) { return __builtin_ctan(__z); }
910 inline __complex__ long double
911 __complex_tan(const __complex__ long double& __z)
912 { return __builtin_ctanl(__z); }
914 template<typename _Tp>
916 tan(const complex<_Tp>& __z) { return __complex_tan(__z.__rep()); }
918 template<typename _Tp>
920 tan(const complex<_Tp>& __z) { return __complex_tan(__z); }
924 // 26.2.8/15 tanh(__z): Returns the hyperbolic tangent of __z.
926 template<typename _Tp>
928 __complex_tanh(const complex<_Tp>& __z)
929 { return std::sinh(__z) / std::cosh(__z); }
931 #if _GLIBCXX_USE_C99_COMPLEX
932 inline __complex__ float
933 __complex_tanh(__complex__ float __z) { return __builtin_ctanhf(__z); }
935 inline __complex__ double
936 __complex_tanh(__complex__ double __z) { return __builtin_ctanh(__z); }
938 inline __complex__ long double
939 __complex_tanh(const __complex__ long double& __z)
940 { return __builtin_ctanhl(__z); }
942 template<typename _Tp>
944 tanh(const complex<_Tp>& __z) { return __complex_tanh(__z.__rep()); }
946 template<typename _Tp>
948 tanh(const complex<_Tp>& __z) { return __complex_tanh(__z); }
952 // 26.2.8/9 pow(__x, __y): Returns the complex power base of __x
953 // raised to the __y-th power. The branch
954 // cut is on the negative axis.
955 #ifndef __GXX_EXPERIMENTAL_CXX0X__
956 template<typename _Tp>
958 __complex_pow_unsigned(complex<_Tp> __x, unsigned __n)
960 complex<_Tp> __y = __n % 2 ? __x : complex<_Tp>(1);
972 // _GLIBCXX_RESOLVE_LIB_DEFECTS
973 // DR 844. complex pow return type is ambiguous.
974 template<typename _Tp>
976 pow(const complex<_Tp>& __z, int __n)
979 ? complex<_Tp>(1) / std::__complex_pow_unsigned(__z, -__n)
980 : std::__complex_pow_unsigned(__z, __n);
984 template<typename _Tp>
986 pow(const complex<_Tp>& __x, const _Tp& __y)
988 #ifndef _GLIBCXX_USE_C99_COMPLEX
992 if (__x.imag() == _Tp() && __x.real() > _Tp())
993 return pow(__x.real(), __y);
995 complex<_Tp> __t = std::log(__x);
996 return std::polar(exp(__y * __t.real()), __y * __t.imag());
999 template<typename _Tp>
1001 __complex_pow(const complex<_Tp>& __x, const complex<_Tp>& __y)
1002 { return __x == _Tp() ? _Tp() : std::exp(__y * std::log(__x)); }
1004 #if _GLIBCXX_USE_C99_COMPLEX
1005 inline __complex__ float
1006 __complex_pow(__complex__ float __x, __complex__ float __y)
1007 { return __builtin_cpowf(__x, __y); }
1009 inline __complex__ double
1010 __complex_pow(__complex__ double __x, __complex__ double __y)
1011 { return __builtin_cpow(__x, __y); }
1013 inline __complex__ long double
1014 __complex_pow(const __complex__ long double& __x,
1015 const __complex__ long double& __y)
1016 { return __builtin_cpowl(__x, __y); }
1018 template<typename _Tp>
1020 pow(const complex<_Tp>& __x, const complex<_Tp>& __y)
1021 { return __complex_pow(__x.__rep(), __y.__rep()); }
1023 template<typename _Tp>
1025 pow(const complex<_Tp>& __x, const complex<_Tp>& __y)
1026 { return __complex_pow(__x, __y); }
1029 template<typename _Tp>
1031 pow(const _Tp& __x, const complex<_Tp>& __y)
1033 return __x > _Tp() ? std::polar(pow(__x, __y.real()),
1034 __y.imag() * log(__x))
1035 : std::pow(complex<_Tp>(__x), __y);
1038 // 26.2.3 complex specializations
1039 // complex<float> specialization
1041 struct complex<float>
1043 typedef float value_type;
1044 typedef __complex__ float _ComplexT;
1046 _GLIBCXX_CONSTEXPR complex(_ComplexT __z) : _M_value(__z) { }
1048 _GLIBCXX_CONSTEXPR complex(float __r = 0.0f, float __i = 0.0f)
1049 #ifdef __GXX_EXPERIMENTAL_CXX0X__
1050 : _M_value{ __r, __i } { }
1053 __real__ _M_value = __r;
1054 __imag__ _M_value = __i;
1058 explicit _GLIBCXX_CONSTEXPR complex(const complex<double>&);
1059 explicit _GLIBCXX_CONSTEXPR complex(const complex<long double>&);
1061 #ifdef __GXX_EXPERIMENTAL_CXX0X__
1062 // _GLIBCXX_RESOLVE_LIB_DEFECTS
1063 // DR 387. std::complex over-encapsulated.
1065 real() const { return __real__ _M_value; }
1068 imag() const { return __imag__ _M_value; }
1071 real() { return __real__ _M_value; }
1074 real() const { return __real__ _M_value; }
1077 imag() { return __imag__ _M_value; }
1080 imag() const { return __imag__ _M_value; }
1083 // _GLIBCXX_RESOLVE_LIB_DEFECTS
1084 // DR 387. std::complex over-encapsulated.
1086 real(float __val) { __real__ _M_value = __val; }
1089 imag(float __val) { __imag__ _M_value = __val; }
1092 operator=(float __f)
1099 operator+=(float __f)
1106 operator-=(float __f)
1113 operator*=(float __f)
1120 operator/=(float __f)
1126 // Let the compiler synthesize the copy and assignment
1127 // operator. It always does a pretty good job.
1128 // complex& operator=(const complex&);
1130 template<typename _Tp>
1132 operator=(const complex<_Tp>& __z)
1134 __real__ _M_value = __z.real();
1135 __imag__ _M_value = __z.imag();
1139 template<typename _Tp>
1141 operator+=(const complex<_Tp>& __z)
1143 __real__ _M_value += __z.real();
1144 __imag__ _M_value += __z.imag();
1150 operator-=(const complex<_Tp>& __z)
1152 __real__ _M_value -= __z.real();
1153 __imag__ _M_value -= __z.imag();
1159 operator*=(const complex<_Tp>& __z)
1162 __real__ __t = __z.real();
1163 __imag__ __t = __z.imag();
1170 operator/=(const complex<_Tp>& __z)
1173 __real__ __t = __z.real();
1174 __imag__ __t = __z.imag();
1179 _GLIBCXX_USE_CONSTEXPR _ComplexT __rep() const { return _M_value; }
1185 // 26.2.3 complex specializations
1186 // complex<double> specialization
1188 struct complex<double>
1190 typedef double value_type;
1191 typedef __complex__ double _ComplexT;
1193 _GLIBCXX_CONSTEXPR complex(_ComplexT __z) : _M_value(__z) { }
1195 _GLIBCXX_CONSTEXPR complex(double __r = 0.0, double __i = 0.0)
1196 #ifdef __GXX_EXPERIMENTAL_CXX0X__
1197 : _M_value{ __r, __i } { }
1200 __real__ _M_value = __r;
1201 __imag__ _M_value = __i;
1205 _GLIBCXX_CONSTEXPR complex(const complex<float>& __z)
1206 : _M_value(__z.__rep()) { }
1208 explicit _GLIBCXX_CONSTEXPR complex(const complex<long double>&);
1210 #ifdef __GXX_EXPERIMENTAL_CXX0X__
1211 // _GLIBCXX_RESOLVE_LIB_DEFECTS
1212 // DR 387. std::complex over-encapsulated.
1214 real() const { return __real__ _M_value; }
1217 imag() const { return __imag__ _M_value; }
1220 real() { return __real__ _M_value; }
1223 real() const { return __real__ _M_value; }
1226 imag() { return __imag__ _M_value; }
1229 imag() const { return __imag__ _M_value; }
1232 // _GLIBCXX_RESOLVE_LIB_DEFECTS
1233 // DR 387. std::complex over-encapsulated.
1235 real(double __val) { __real__ _M_value = __val; }
1238 imag(double __val) { __imag__ _M_value = __val; }
1241 operator=(double __d)
1248 operator+=(double __d)
1255 operator-=(double __d)
1262 operator*=(double __d)
1269 operator/=(double __d)
1275 // The compiler will synthesize this, efficiently.
1276 // complex& operator=(const complex&);
1278 template<typename _Tp>
1280 operator=(const complex<_Tp>& __z)
1282 __real__ _M_value = __z.real();
1283 __imag__ _M_value = __z.imag();
1287 template<typename _Tp>
1289 operator+=(const complex<_Tp>& __z)
1291 __real__ _M_value += __z.real();
1292 __imag__ _M_value += __z.imag();
1296 template<typename _Tp>
1298 operator-=(const complex<_Tp>& __z)
1300 __real__ _M_value -= __z.real();
1301 __imag__ _M_value -= __z.imag();
1305 template<typename _Tp>
1307 operator*=(const complex<_Tp>& __z)
1310 __real__ __t = __z.real();
1311 __imag__ __t = __z.imag();
1316 template<typename _Tp>
1318 operator/=(const complex<_Tp>& __z)
1321 __real__ __t = __z.real();
1322 __imag__ __t = __z.imag();
1327 _GLIBCXX_USE_CONSTEXPR _ComplexT __rep() const { return _M_value; }
1333 // 26.2.3 complex specializations
1334 // complex<long double> specialization
1336 struct complex<long double>
1338 typedef long double value_type;
1339 typedef __complex__ long double _ComplexT;
1341 _GLIBCXX_CONSTEXPR complex(_ComplexT __z) : _M_value(__z) { }
1343 _GLIBCXX_CONSTEXPR complex(long double __r = 0.0L,
1344 long double __i = 0.0L)
1345 #ifdef __GXX_EXPERIMENTAL_CXX0X__
1346 : _M_value{ __r, __i } { }
1349 __real__ _M_value = __r;
1350 __imag__ _M_value = __i;
1354 _GLIBCXX_CONSTEXPR complex(const complex<float>& __z)
1355 : _M_value(__z.__rep()) { }
1357 _GLIBCXX_CONSTEXPR complex(const complex<double>& __z)
1358 : _M_value(__z.__rep()) { }
1360 #ifdef __GXX_EXPERIMENTAL_CXX0X__
1361 // _GLIBCXX_RESOLVE_LIB_DEFECTS
1362 // DR 387. std::complex over-encapsulated.
1363 constexpr long double
1364 real() const { return __real__ _M_value; }
1366 constexpr long double
1367 imag() const { return __imag__ _M_value; }
1370 real() { return __real__ _M_value; }
1373 real() const { return __real__ _M_value; }
1376 imag() { return __imag__ _M_value; }
1379 imag() const { return __imag__ _M_value; }
1382 // _GLIBCXX_RESOLVE_LIB_DEFECTS
1383 // DR 387. std::complex over-encapsulated.
1385 real(long double __val) { __real__ _M_value = __val; }
1388 imag(long double __val) { __imag__ _M_value = __val; }
1391 operator=(long double __r)
1398 operator+=(long double __r)
1405 operator-=(long double __r)
1412 operator*=(long double __r)
1419 operator/=(long double __r)
1425 // The compiler knows how to do this efficiently
1426 // complex& operator=(const complex&);
1428 template<typename _Tp>
1430 operator=(const complex<_Tp>& __z)
1432 __real__ _M_value = __z.real();
1433 __imag__ _M_value = __z.imag();
1437 template<typename _Tp>
1439 operator+=(const complex<_Tp>& __z)
1441 __real__ _M_value += __z.real();
1442 __imag__ _M_value += __z.imag();
1446 template<typename _Tp>
1448 operator-=(const complex<_Tp>& __z)
1450 __real__ _M_value -= __z.real();
1451 __imag__ _M_value -= __z.imag();
1455 template<typename _Tp>
1457 operator*=(const complex<_Tp>& __z)
1460 __real__ __t = __z.real();
1461 __imag__ __t = __z.imag();
1466 template<typename _Tp>
1468 operator/=(const complex<_Tp>& __z)
1471 __real__ __t = __z.real();
1472 __imag__ __t = __z.imag();
1477 _GLIBCXX_USE_CONSTEXPR _ComplexT __rep() const { return _M_value; }
1483 // These bits have to be at the end of this file, so that the
1484 // specializations have all been defined.
1485 inline _GLIBCXX_CONSTEXPR
1486 complex<float>::complex(const complex<double>& __z)
1487 : _M_value(__z.__rep()) { }
1489 inline _GLIBCXX_CONSTEXPR
1490 complex<float>::complex(const complex<long double>& __z)
1491 : _M_value(__z.__rep()) { }
1493 inline _GLIBCXX_CONSTEXPR
1494 complex<double>::complex(const complex<long double>& __z)
1495 : _M_value(__z.__rep()) { }
1497 // Inhibit implicit instantiations for required instantiations,
1498 // which are defined via explicit instantiations elsewhere.
1499 // NB: This syntax is a GNU extension.
1500 #if _GLIBCXX_EXTERN_TEMPLATE
1501 extern template istream& operator>>(istream&, complex<float>&);
1502 extern template ostream& operator<<(ostream&, const complex<float>&);
1503 extern template istream& operator>>(istream&, complex<double>&);
1504 extern template ostream& operator<<(ostream&, const complex<double>&);
1505 extern template istream& operator>>(istream&, complex<long double>&);
1506 extern template ostream& operator<<(ostream&, const complex<long double>&);
1508 #ifdef _GLIBCXX_USE_WCHAR_T
1509 extern template wistream& operator>>(wistream&, complex<float>&);
1510 extern template wostream& operator<<(wostream&, const complex<float>&);
1511 extern template wistream& operator>>(wistream&, complex<double>&);
1512 extern template wostream& operator<<(wostream&, const complex<double>&);
1513 extern template wistream& operator>>(wistream&, complex<long double>&);
1514 extern template wostream& operator<<(wostream&, const complex<long double>&);
1518 // @} group complex_numbers
1520 _GLIBCXX_END_NAMESPACE_VERSION
1523 namespace __gnu_cxx _GLIBCXX_VISIBILITY(default)
1525 _GLIBCXX_BEGIN_NAMESPACE_VERSION
1527 // See ext/type_traits.h for the primary template.
1528 template<typename _Tp, typename _Up>
1529 struct __promote_2<std::complex<_Tp>, _Up>
1532 typedef std::complex<typename __promote_2<_Tp, _Up>::__type> __type;
1535 template<typename _Tp, typename _Up>
1536 struct __promote_2<_Tp, std::complex<_Up> >
1539 typedef std::complex<typename __promote_2<_Tp, _Up>::__type> __type;
1542 template<typename _Tp, typename _Up>
1543 struct __promote_2<std::complex<_Tp>, std::complex<_Up> >
1546 typedef std::complex<typename __promote_2<_Tp, _Up>::__type> __type;
1549 _GLIBCXX_END_NAMESPACE_VERSION
1552 #ifdef __GXX_EXPERIMENTAL_CXX0X__
1554 namespace std _GLIBCXX_VISIBILITY(default)
1556 _GLIBCXX_BEGIN_NAMESPACE_VERSION
1558 // Forward declarations.
1559 template<typename _Tp> std::complex<_Tp> acos(const std::complex<_Tp>&);
1560 template<typename _Tp> std::complex<_Tp> asin(const std::complex<_Tp>&);
1561 template<typename _Tp> std::complex<_Tp> atan(const std::complex<_Tp>&);
1563 template<typename _Tp> std::complex<_Tp> acosh(const std::complex<_Tp>&);
1564 template<typename _Tp> std::complex<_Tp> asinh(const std::complex<_Tp>&);
1565 template<typename _Tp> std::complex<_Tp> atanh(const std::complex<_Tp>&);
1567 template<typename _Tp> _Tp fabs(const std::complex<_Tp>&);
1569 template<typename _Tp>
1570 inline std::complex<_Tp>
1571 __complex_acos(const std::complex<_Tp>& __z)
1573 const std::complex<_Tp> __t = std::asin(__z);
1574 const _Tp __pi_2 = 1.5707963267948966192313216916397514L;
1575 return std::complex<_Tp>(__pi_2 - __t.real(), -__t.imag());
1578 #if _GLIBCXX_USE_C99_COMPLEX_TR1
1579 inline __complex__ float
1580 __complex_acos(__complex__ float __z)
1581 { return __builtin_cacosf(__z); }
1583 inline __complex__ double
1584 __complex_acos(__complex__ double __z)
1585 { return __builtin_cacos(__z); }
1587 inline __complex__ long double
1588 __complex_acos(const __complex__ long double& __z)
1589 { return __builtin_cacosl(__z); }
1591 template<typename _Tp>
1592 inline std::complex<_Tp>
1593 acos(const std::complex<_Tp>& __z)
1594 { return __complex_acos(__z.__rep()); }
1596 /// acos(__z) [8.1.2].
1597 // Effects: Behaves the same as C99 function cacos, defined
1598 // in subclause 7.3.5.1.
1599 template<typename _Tp>
1600 inline std::complex<_Tp>
1601 acos(const std::complex<_Tp>& __z)
1602 { return __complex_acos(__z); }
1605 template<typename _Tp>
1606 inline std::complex<_Tp>
1607 __complex_asin(const std::complex<_Tp>& __z)
1609 std::complex<_Tp> __t(-__z.imag(), __z.real());
1610 __t = std::asinh(__t);
1611 return std::complex<_Tp>(__t.imag(), -__t.real());
1614 #if _GLIBCXX_USE_C99_COMPLEX_TR1
1615 inline __complex__ float
1616 __complex_asin(__complex__ float __z)
1617 { return __builtin_casinf(__z); }
1619 inline __complex__ double
1620 __complex_asin(__complex__ double __z)
1621 { return __builtin_casin(__z); }
1623 inline __complex__ long double
1624 __complex_asin(const __complex__ long double& __z)
1625 { return __builtin_casinl(__z); }
1627 template<typename _Tp>
1628 inline std::complex<_Tp>
1629 asin(const std::complex<_Tp>& __z)
1630 { return __complex_asin(__z.__rep()); }
1632 /// asin(__z) [8.1.3].
1633 // Effects: Behaves the same as C99 function casin, defined
1634 // in subclause 7.3.5.2.
1635 template<typename _Tp>
1636 inline std::complex<_Tp>
1637 asin(const std::complex<_Tp>& __z)
1638 { return __complex_asin(__z); }
1641 template<typename _Tp>
1643 __complex_atan(const std::complex<_Tp>& __z)
1645 const _Tp __r2 = __z.real() * __z.real();
1646 const _Tp __x = _Tp(1.0) - __r2 - __z.imag() * __z.imag();
1648 _Tp __num = __z.imag() + _Tp(1.0);
1649 _Tp __den = __z.imag() - _Tp(1.0);
1651 __num = __r2 + __num * __num;
1652 __den = __r2 + __den * __den;
1654 return std::complex<_Tp>(_Tp(0.5) * atan2(_Tp(2.0) * __z.real(), __x),
1655 _Tp(0.25) * log(__num / __den));
1658 #if _GLIBCXX_USE_C99_COMPLEX_TR1
1659 inline __complex__ float
1660 __complex_atan(__complex__ float __z)
1661 { return __builtin_catanf(__z); }
1663 inline __complex__ double
1664 __complex_atan(__complex__ double __z)
1665 { return __builtin_catan(__z); }
1667 inline __complex__ long double
1668 __complex_atan(const __complex__ long double& __z)
1669 { return __builtin_catanl(__z); }
1671 template<typename _Tp>
1672 inline std::complex<_Tp>
1673 atan(const std::complex<_Tp>& __z)
1674 { return __complex_atan(__z.__rep()); }
1676 /// atan(__z) [8.1.4].
1677 // Effects: Behaves the same as C99 function catan, defined
1678 // in subclause 7.3.5.3.
1679 template<typename _Tp>
1680 inline std::complex<_Tp>
1681 atan(const std::complex<_Tp>& __z)
1682 { return __complex_atan(__z); }
1685 template<typename _Tp>
1687 __complex_acosh(const std::complex<_Tp>& __z)
1690 return _Tp(2.0) * std::log(std::sqrt(_Tp(0.5) * (__z + _Tp(1.0)))
1691 + std::sqrt(_Tp(0.5) * (__z - _Tp(1.0))));
1694 #if _GLIBCXX_USE_C99_COMPLEX_TR1
1695 inline __complex__ float
1696 __complex_acosh(__complex__ float __z)
1697 { return __builtin_cacoshf(__z); }
1699 inline __complex__ double
1700 __complex_acosh(__complex__ double __z)
1701 { return __builtin_cacosh(__z); }
1703 inline __complex__ long double
1704 __complex_acosh(const __complex__ long double& __z)
1705 { return __builtin_cacoshl(__z); }
1707 template<typename _Tp>
1708 inline std::complex<_Tp>
1709 acosh(const std::complex<_Tp>& __z)
1710 { return __complex_acosh(__z.__rep()); }
1712 /// acosh(__z) [8.1.5].
1713 // Effects: Behaves the same as C99 function cacosh, defined
1714 // in subclause 7.3.6.1.
1715 template<typename _Tp>
1716 inline std::complex<_Tp>
1717 acosh(const std::complex<_Tp>& __z)
1718 { return __complex_acosh(__z); }
1721 template<typename _Tp>
1723 __complex_asinh(const std::complex<_Tp>& __z)
1725 std::complex<_Tp> __t((__z.real() - __z.imag())
1726 * (__z.real() + __z.imag()) + _Tp(1.0),
1727 _Tp(2.0) * __z.real() * __z.imag());
1728 __t = std::sqrt(__t);
1730 return std::log(__t + __z);
1733 #if _GLIBCXX_USE_C99_COMPLEX_TR1
1734 inline __complex__ float
1735 __complex_asinh(__complex__ float __z)
1736 { return __builtin_casinhf(__z); }
1738 inline __complex__ double
1739 __complex_asinh(__complex__ double __z)
1740 { return __builtin_casinh(__z); }
1742 inline __complex__ long double
1743 __complex_asinh(const __complex__ long double& __z)
1744 { return __builtin_casinhl(__z); }
1746 template<typename _Tp>
1747 inline std::complex<_Tp>
1748 asinh(const std::complex<_Tp>& __z)
1749 { return __complex_asinh(__z.__rep()); }
1751 /// asinh(__z) [8.1.6].
1752 // Effects: Behaves the same as C99 function casin, defined
1753 // in subclause 7.3.6.2.
1754 template<typename _Tp>
1755 inline std::complex<_Tp>
1756 asinh(const std::complex<_Tp>& __z)
1757 { return __complex_asinh(__z); }
1760 template<typename _Tp>
1762 __complex_atanh(const std::complex<_Tp>& __z)
1764 const _Tp __i2 = __z.imag() * __z.imag();
1765 const _Tp __x = _Tp(1.0) - __i2 - __z.real() * __z.real();
1767 _Tp __num = _Tp(1.0) + __z.real();
1768 _Tp __den = _Tp(1.0) - __z.real();
1770 __num = __i2 + __num * __num;
1771 __den = __i2 + __den * __den;
1773 return std::complex<_Tp>(_Tp(0.25) * (log(__num) - log(__den)),
1774 _Tp(0.5) * atan2(_Tp(2.0) * __z.imag(), __x));
1777 #if _GLIBCXX_USE_C99_COMPLEX_TR1
1778 inline __complex__ float
1779 __complex_atanh(__complex__ float __z)
1780 { return __builtin_catanhf(__z); }
1782 inline __complex__ double
1783 __complex_atanh(__complex__ double __z)
1784 { return __builtin_catanh(__z); }
1786 inline __complex__ long double
1787 __complex_atanh(const __complex__ long double& __z)
1788 { return __builtin_catanhl(__z); }
1790 template<typename _Tp>
1791 inline std::complex<_Tp>
1792 atanh(const std::complex<_Tp>& __z)
1793 { return __complex_atanh(__z.__rep()); }
1795 /// atanh(__z) [8.1.7].
1796 // Effects: Behaves the same as C99 function catanh, defined
1797 // in subclause 7.3.6.3.
1798 template<typename _Tp>
1799 inline std::complex<_Tp>
1800 atanh(const std::complex<_Tp>& __z)
1801 { return __complex_atanh(__z); }
1804 template<typename _Tp>
1806 /// fabs(__z) [8.1.8].
1807 // Effects: Behaves the same as C99 function cabs, defined
1808 // in subclause 7.3.8.1.
1809 fabs(const std::complex<_Tp>& __z)
1810 { return std::abs(__z); }
1812 /// Additional overloads [8.1.9].
1813 template<typename _Tp>
1814 inline typename __gnu_cxx::__promote<_Tp>::__type
1817 typedef typename __gnu_cxx::__promote<_Tp>::__type __type;
1818 #if (_GLIBCXX_USE_C99_MATH && !_GLIBCXX_USE_C99_FP_MACROS_DYNAMIC)
1819 return std::signbit(__x) ? __type(3.1415926535897932384626433832795029L)
1822 return std::arg(std::complex<__type>(__x));
1826 template<typename _Tp>
1827 inline typename __gnu_cxx::__promote<_Tp>::__type
1831 template<typename _Tp>
1832 inline typename __gnu_cxx::__promote<_Tp>::__type
1835 typedef typename __gnu_cxx::__promote<_Tp>::__type __type;
1836 return __type(__x) * __type(__x);
1839 template<typename _Tp>
1840 inline typename __gnu_cxx::__promote<_Tp>::__type
1844 template<typename _Tp, typename _Up>
1845 inline std::complex<typename __gnu_cxx::__promote_2<_Tp, _Up>::__type>
1846 pow(const std::complex<_Tp>& __x, const _Up& __y)
1848 typedef typename __gnu_cxx::__promote_2<_Tp, _Up>::__type __type;
1849 return std::pow(std::complex<__type>(__x), __type(__y));
1852 template<typename _Tp, typename _Up>
1853 inline std::complex<typename __gnu_cxx::__promote_2<_Tp, _Up>::__type>
1854 pow(const _Tp& __x, const std::complex<_Up>& __y)
1856 typedef typename __gnu_cxx::__promote_2<_Tp, _Up>::__type __type;
1857 return std::pow(__type(__x), std::complex<__type>(__y));
1860 template<typename _Tp, typename _Up>
1861 inline std::complex<typename __gnu_cxx::__promote_2<_Tp, _Up>::__type>
1862 pow(const std::complex<_Tp>& __x, const std::complex<_Up>& __y)
1864 typedef typename __gnu_cxx::__promote_2<_Tp, _Up>::__type __type;
1865 return std::pow(std::complex<__type>(__x),
1866 std::complex<__type>(__y));
1869 // Forward declarations.
1871 template<typename _Tp> std::complex<_Tp> proj(const std::complex<_Tp>&);
1873 template<typename _Tp>
1875 __complex_proj(const std::complex<_Tp>& __z)
1877 const _Tp __den = (__z.real() * __z.real()
1878 + __z.imag() * __z.imag() + _Tp(1.0));
1880 return std::complex<_Tp>((_Tp(2.0) * __z.real()) / __den,
1881 (_Tp(2.0) * __z.imag()) / __den);
1884 #if _GLIBCXX_USE_C99_COMPLEX
1885 inline __complex__ float
1886 __complex_proj(__complex__ float __z)
1887 { return __builtin_cprojf(__z); }
1889 inline __complex__ double
1890 __complex_proj(__complex__ double __z)
1891 { return __builtin_cproj(__z); }
1893 inline __complex__ long double
1894 __complex_proj(const __complex__ long double& __z)
1895 { return __builtin_cprojl(__z); }
1897 template<typename _Tp>
1898 inline std::complex<_Tp>
1899 proj(const std::complex<_Tp>& __z)
1900 { return __complex_proj(__z.__rep()); }
1902 template<typename _Tp>
1903 inline std::complex<_Tp>
1904 proj(const std::complex<_Tp>& __z)
1905 { return __complex_proj(__z); }
1909 template<typename _Tp>
1910 inline typename __gnu_cxx::__promote<_Tp>::__type
1914 template<typename _Tp>
1915 inline typename __gnu_cxx::__promote<_Tp>::__type
1919 _GLIBCXX_END_NAMESPACE_VERSION
1922 #endif // __GXX_EXPERIMENTAL_CXX0X__
1924 #endif /* _GLIBCXX_COMPLEX */