1 // The template and inlines for the -*- C++ -*- complex number classes.
3 // Copyright (C) 1997-2013 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.
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 #if __cplusplus < 201103L
93 /// Return @a x to the @a y'th power.
94 template<typename _Tp> complex<_Tp> pow(const complex<_Tp>&, int);
96 /// Return @a x to the @a y'th power.
97 template<typename _Tp> complex<_Tp> pow(const complex<_Tp>&, const _Tp&);
98 /// Return @a x to the @a y'th power.
99 template<typename _Tp> complex<_Tp> pow(const complex<_Tp>&,
100 const complex<_Tp>&);
101 /// Return @a x to the @a y'th power.
102 template<typename _Tp> complex<_Tp> pow(const _Tp&, const complex<_Tp>&);
103 /// Return complex sine of @a z.
104 template<typename _Tp> complex<_Tp> sin(const complex<_Tp>&);
105 /// Return complex hyperbolic sine of @a z.
106 template<typename _Tp> complex<_Tp> sinh(const complex<_Tp>&);
107 /// Return complex square root of @a z.
108 template<typename _Tp> complex<_Tp> sqrt(const complex<_Tp>&);
109 /// Return complex tangent of @a z.
110 template<typename _Tp> complex<_Tp> tan(const complex<_Tp>&);
111 /// Return complex hyperbolic tangent of @a z.
112 template<typename _Tp> complex<_Tp> tanh(const complex<_Tp>&);
115 // 26.2.2 Primary template class complex
117 * Template to represent complex numbers.
119 * Specializations for float, double, and long double are part of the
120 * library. Results with any other type are not guaranteed.
122 * @param Tp Type of real and imaginary values.
124 template<typename _Tp>
128 typedef _Tp value_type;
130 /// Default constructor. First parameter is x, second parameter is y.
131 /// Unspecified parameters default to 0.
132 _GLIBCXX_CONSTEXPR complex(const _Tp& __r = _Tp(), const _Tp& __i = _Tp())
133 : _M_real(__r), _M_imag(__i) { }
135 // Lets the compiler synthesize the copy constructor
136 // complex (const complex<_Tp>&);
137 /// Copy 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() { return _M_real; }
149 _GLIBCXX_ABI_TAG_CXX11
151 imag() { 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 this complex number to scalar @a t.
179 complex<_Tp>& operator=(const _Tp&);
181 /// Add @a t to this complex number.
184 operator+=(const _Tp& __t)
190 /// Subtract @a t from this complex number.
193 operator-=(const _Tp& __t)
199 /// Multiply this complex number by @a t.
200 complex<_Tp>& operator*=(const _Tp&);
201 /// Divide this complex number by @a t.
202 complex<_Tp>& operator/=(const _Tp&);
204 // Lets the compiler synthesize the
205 // copy and assignment operator
206 // complex<_Tp>& operator= (const complex<_Tp>&);
207 /// Assign this complex number to complex @a z.
208 template<typename _Up>
209 complex<_Tp>& operator=(const complex<_Up>&);
210 /// Add @a z to this complex number.
211 template<typename _Up>
212 complex<_Tp>& operator+=(const complex<_Up>&);
213 /// Subtract @a z from this complex number.
214 template<typename _Up>
215 complex<_Tp>& operator-=(const complex<_Up>&);
216 /// Multiply this complex number by @a z.
217 template<typename _Up>
218 complex<_Tp>& operator*=(const complex<_Up>&);
219 /// Divide this complex number by @a z.
220 template<typename _Up>
221 complex<_Tp>& operator/=(const complex<_Up>&);
223 _GLIBCXX_USE_CONSTEXPR complex __rep() const
231 template<typename _Tp>
233 complex<_Tp>::operator=(const _Tp& __t)
241 template<typename _Tp>
243 complex<_Tp>::operator*=(const _Tp& __t)
251 template<typename _Tp>
253 complex<_Tp>::operator/=(const _Tp& __t)
260 template<typename _Tp>
261 template<typename _Up>
263 complex<_Tp>::operator=(const complex<_Up>& __z)
265 _M_real = __z.real();
266 _M_imag = __z.imag();
271 template<typename _Tp>
272 template<typename _Up>
274 complex<_Tp>::operator+=(const complex<_Up>& __z)
276 _M_real += __z.real();
277 _M_imag += __z.imag();
282 template<typename _Tp>
283 template<typename _Up>
285 complex<_Tp>::operator-=(const complex<_Up>& __z)
287 _M_real -= __z.real();
288 _M_imag -= __z.imag();
293 // XXX: This is a grammar school implementation.
294 template<typename _Tp>
295 template<typename _Up>
297 complex<_Tp>::operator*=(const complex<_Up>& __z)
299 const _Tp __r = _M_real * __z.real() - _M_imag * __z.imag();
300 _M_imag = _M_real * __z.imag() + _M_imag * __z.real();
306 // XXX: This is a grammar school implementation.
307 template<typename _Tp>
308 template<typename _Up>
310 complex<_Tp>::operator/=(const complex<_Up>& __z)
312 const _Tp __r = _M_real * __z.real() + _M_imag * __z.imag();
313 const _Tp __n = std::norm(__z);
314 _M_imag = (_M_imag * __z.real() - _M_real * __z.imag()) / __n;
321 /// Return new complex value @a x plus @a y.
322 template<typename _Tp>
324 operator+(const complex<_Tp>& __x, const complex<_Tp>& __y)
326 complex<_Tp> __r = __x;
331 template<typename _Tp>
333 operator+(const complex<_Tp>& __x, const _Tp& __y)
335 complex<_Tp> __r = __x;
340 template<typename _Tp>
342 operator+(const _Tp& __x, const complex<_Tp>& __y)
344 complex<_Tp> __r = __y;
351 /// Return new complex value @a x minus @a y.
352 template<typename _Tp>
354 operator-(const complex<_Tp>& __x, const complex<_Tp>& __y)
356 complex<_Tp> __r = __x;
361 template<typename _Tp>
363 operator-(const complex<_Tp>& __x, const _Tp& __y)
365 complex<_Tp> __r = __x;
370 template<typename _Tp>
372 operator-(const _Tp& __x, const complex<_Tp>& __y)
374 complex<_Tp> __r(__x, -__y.imag());
381 /// Return new complex value @a x times @a y.
382 template<typename _Tp>
384 operator*(const complex<_Tp>& __x, const complex<_Tp>& __y)
386 complex<_Tp> __r = __x;
391 template<typename _Tp>
393 operator*(const complex<_Tp>& __x, const _Tp& __y)
395 complex<_Tp> __r = __x;
400 template<typename _Tp>
402 operator*(const _Tp& __x, const complex<_Tp>& __y)
404 complex<_Tp> __r = __y;
411 /// Return new complex value @a x divided by @a y.
412 template<typename _Tp>
414 operator/(const complex<_Tp>& __x, const complex<_Tp>& __y)
416 complex<_Tp> __r = __x;
421 template<typename _Tp>
423 operator/(const complex<_Tp>& __x, const _Tp& __y)
425 complex<_Tp> __r = __x;
430 template<typename _Tp>
432 operator/(const _Tp& __x, const complex<_Tp>& __y)
434 complex<_Tp> __r = __x;
441 template<typename _Tp>
443 operator+(const complex<_Tp>& __x)
446 /// Return complex negation of @a x.
447 template<typename _Tp>
449 operator-(const complex<_Tp>& __x)
450 { return complex<_Tp>(-__x.real(), -__x.imag()); }
453 /// Return true if @a x is equal to @a y.
454 template<typename _Tp>
455 inline _GLIBCXX_CONSTEXPR bool
456 operator==(const complex<_Tp>& __x, const complex<_Tp>& __y)
457 { return __x.real() == __y.real() && __x.imag() == __y.imag(); }
459 template<typename _Tp>
460 inline _GLIBCXX_CONSTEXPR bool
461 operator==(const complex<_Tp>& __x, const _Tp& __y)
462 { return __x.real() == __y && __x.imag() == _Tp(); }
464 template<typename _Tp>
465 inline _GLIBCXX_CONSTEXPR bool
466 operator==(const _Tp& __x, const complex<_Tp>& __y)
467 { return __x == __y.real() && _Tp() == __y.imag(); }
471 /// Return false if @a x is equal to @a y.
472 template<typename _Tp>
473 inline _GLIBCXX_CONSTEXPR bool
474 operator!=(const complex<_Tp>& __x, const complex<_Tp>& __y)
475 { return __x.real() != __y.real() || __x.imag() != __y.imag(); }
477 template<typename _Tp>
478 inline _GLIBCXX_CONSTEXPR bool
479 operator!=(const complex<_Tp>& __x, const _Tp& __y)
480 { return __x.real() != __y || __x.imag() != _Tp(); }
482 template<typename _Tp>
483 inline _GLIBCXX_CONSTEXPR bool
484 operator!=(const _Tp& __x, const complex<_Tp>& __y)
485 { return __x != __y.real() || _Tp() != __y.imag(); }
488 /// Extraction operator for complex values.
489 template<typename _Tp, typename _CharT, class _Traits>
490 basic_istream<_CharT, _Traits>&
491 operator>>(basic_istream<_CharT, _Traits>& __is, complex<_Tp>& __x)
498 __is >> __re_x >> __ch;
501 __is >> __im_x >> __ch;
503 __x = complex<_Tp>(__re_x, __im_x);
505 __is.setstate(ios_base::failbit);
507 else if (__ch == ')')
510 __is.setstate(ios_base::failbit);
521 /// Insertion operator for complex values.
522 template<typename _Tp, typename _CharT, class _Traits>
523 basic_ostream<_CharT, _Traits>&
524 operator<<(basic_ostream<_CharT, _Traits>& __os, const complex<_Tp>& __x)
526 basic_ostringstream<_CharT, _Traits> __s;
527 __s.flags(__os.flags());
528 __s.imbue(__os.getloc());
529 __s.precision(__os.precision());
530 __s << '(' << __x.real() << ',' << __x.imag() << ')';
531 return __os << __s.str();
535 #if __cplusplus >= 201103L
536 template<typename _Tp>
538 real(const complex<_Tp>& __z)
539 { return __z.real(); }
541 template<typename _Tp>
543 imag(const complex<_Tp>& __z)
544 { return __z.imag(); }
546 template<typename _Tp>
548 real(complex<_Tp>& __z)
549 { return __z.real(); }
551 template<typename _Tp>
553 real(const complex<_Tp>& __z)
554 { return __z.real(); }
556 template<typename _Tp>
558 imag(complex<_Tp>& __z)
559 { return __z.imag(); }
561 template<typename _Tp>
563 imag(const complex<_Tp>& __z)
564 { return __z.imag(); }
567 // 26.2.7/3 abs(__z): Returns the magnitude of __z.
568 template<typename _Tp>
570 __complex_abs(const complex<_Tp>& __z)
572 _Tp __x = __z.real();
573 _Tp __y = __z.imag();
574 const _Tp __s = std::max(abs(__x), abs(__y));
575 if (__s == _Tp()) // well ...
579 return __s * sqrt(__x * __x + __y * __y);
582 #if _GLIBCXX_USE_C99_COMPLEX
584 __complex_abs(__complex__ float __z) { return __builtin_cabsf(__z); }
587 __complex_abs(__complex__ double __z) { return __builtin_cabs(__z); }
590 __complex_abs(const __complex__ long double& __z)
591 { return __builtin_cabsl(__z); }
593 template<typename _Tp>
595 abs(const complex<_Tp>& __z) { return __complex_abs(__z.__rep()); }
597 template<typename _Tp>
599 abs(const complex<_Tp>& __z) { return __complex_abs(__z); }
603 // 26.2.7/4: arg(__z): Returns the phase angle of __z.
604 template<typename _Tp>
606 __complex_arg(const complex<_Tp>& __z)
607 { return atan2(__z.imag(), __z.real()); }
609 #if _GLIBCXX_USE_C99_COMPLEX
611 __complex_arg(__complex__ float __z) { return __builtin_cargf(__z); }
614 __complex_arg(__complex__ double __z) { return __builtin_carg(__z); }
617 __complex_arg(const __complex__ long double& __z)
618 { return __builtin_cargl(__z); }
620 template<typename _Tp>
622 arg(const complex<_Tp>& __z) { return __complex_arg(__z.__rep()); }
624 template<typename _Tp>
626 arg(const complex<_Tp>& __z) { return __complex_arg(__z); }
629 // 26.2.7/5: norm(__z) returns the squared magnitude of __z.
630 // As defined, norm() is -not- a norm is the common mathematical
631 // sens used in numerics. The helper class _Norm_helper<> tries to
632 // distinguish between builtin floating point and the rest, so as
633 // to deliver an answer as close as possible to the real value.
637 template<typename _Tp>
638 static inline _Tp _S_do_it(const complex<_Tp>& __z)
640 const _Tp __x = __z.real();
641 const _Tp __y = __z.imag();
642 return __x * __x + __y * __y;
647 struct _Norm_helper<true>
649 template<typename _Tp>
650 static inline _Tp _S_do_it(const complex<_Tp>& __z)
652 _Tp __res = std::abs(__z);
653 return __res * __res;
657 template<typename _Tp>
659 norm(const complex<_Tp>& __z)
661 return _Norm_helper<__is_floating<_Tp>::__value
662 && !_GLIBCXX_FAST_MATH>::_S_do_it(__z);
665 template<typename _Tp>
667 polar(const _Tp& __rho, const _Tp& __theta)
668 { return complex<_Tp>(__rho * cos(__theta), __rho * sin(__theta)); }
670 template<typename _Tp>
672 conj(const complex<_Tp>& __z)
673 { return complex<_Tp>(__z.real(), -__z.imag()); }
677 // 26.2.8/1 cos(__z): Returns the cosine of __z.
678 template<typename _Tp>
680 __complex_cos(const complex<_Tp>& __z)
682 const _Tp __x = __z.real();
683 const _Tp __y = __z.imag();
684 return complex<_Tp>(cos(__x) * cosh(__y), -sin(__x) * sinh(__y));
687 #if _GLIBCXX_USE_C99_COMPLEX
688 inline __complex__ float
689 __complex_cos(__complex__ float __z) { return __builtin_ccosf(__z); }
691 inline __complex__ double
692 __complex_cos(__complex__ double __z) { return __builtin_ccos(__z); }
694 inline __complex__ long double
695 __complex_cos(const __complex__ long double& __z)
696 { return __builtin_ccosl(__z); }
698 template<typename _Tp>
700 cos(const complex<_Tp>& __z) { return __complex_cos(__z.__rep()); }
702 template<typename _Tp>
704 cos(const complex<_Tp>& __z) { return __complex_cos(__z); }
707 // 26.2.8/2 cosh(__z): Returns the hyperbolic cosine of __z.
708 template<typename _Tp>
710 __complex_cosh(const complex<_Tp>& __z)
712 const _Tp __x = __z.real();
713 const _Tp __y = __z.imag();
714 return complex<_Tp>(cosh(__x) * cos(__y), sinh(__x) * sin(__y));
717 #if _GLIBCXX_USE_C99_COMPLEX
718 inline __complex__ float
719 __complex_cosh(__complex__ float __z) { return __builtin_ccoshf(__z); }
721 inline __complex__ double
722 __complex_cosh(__complex__ double __z) { return __builtin_ccosh(__z); }
724 inline __complex__ long double
725 __complex_cosh(const __complex__ long double& __z)
726 { return __builtin_ccoshl(__z); }
728 template<typename _Tp>
730 cosh(const complex<_Tp>& __z) { return __complex_cosh(__z.__rep()); }
732 template<typename _Tp>
734 cosh(const complex<_Tp>& __z) { return __complex_cosh(__z); }
737 // 26.2.8/3 exp(__z): Returns the complex base e exponential of x
738 template<typename _Tp>
740 __complex_exp(const complex<_Tp>& __z)
741 { return std::polar(exp(__z.real()), __z.imag()); }
743 #if _GLIBCXX_USE_C99_COMPLEX
744 inline __complex__ float
745 __complex_exp(__complex__ float __z) { return __builtin_cexpf(__z); }
747 inline __complex__ double
748 __complex_exp(__complex__ double __z) { return __builtin_cexp(__z); }
750 inline __complex__ long double
751 __complex_exp(const __complex__ long double& __z)
752 { return __builtin_cexpl(__z); }
754 template<typename _Tp>
756 exp(const complex<_Tp>& __z) { return __complex_exp(__z.__rep()); }
758 template<typename _Tp>
760 exp(const complex<_Tp>& __z) { return __complex_exp(__z); }
763 // 26.2.8/5 log(__z): Returns the natural complex logarithm of __z.
764 // The branch cut is along the negative axis.
765 template<typename _Tp>
767 __complex_log(const complex<_Tp>& __z)
768 { return complex<_Tp>(log(std::abs(__z)), std::arg(__z)); }
770 #if _GLIBCXX_USE_C99_COMPLEX
771 inline __complex__ float
772 __complex_log(__complex__ float __z) { return __builtin_clogf(__z); }
774 inline __complex__ double
775 __complex_log(__complex__ double __z) { return __builtin_clog(__z); }
777 inline __complex__ long double
778 __complex_log(const __complex__ long double& __z)
779 { return __builtin_clogl(__z); }
781 template<typename _Tp>
783 log(const complex<_Tp>& __z) { return __complex_log(__z.__rep()); }
785 template<typename _Tp>
787 log(const complex<_Tp>& __z) { return __complex_log(__z); }
790 template<typename _Tp>
792 log10(const complex<_Tp>& __z)
793 { return std::log(__z) / log(_Tp(10.0)); }
795 // 26.2.8/10 sin(__z): Returns the sine of __z.
796 template<typename _Tp>
798 __complex_sin(const complex<_Tp>& __z)
800 const _Tp __x = __z.real();
801 const _Tp __y = __z.imag();
802 return complex<_Tp>(sin(__x) * cosh(__y), cos(__x) * sinh(__y));
805 #if _GLIBCXX_USE_C99_COMPLEX
806 inline __complex__ float
807 __complex_sin(__complex__ float __z) { return __builtin_csinf(__z); }
809 inline __complex__ double
810 __complex_sin(__complex__ double __z) { return __builtin_csin(__z); }
812 inline __complex__ long double
813 __complex_sin(const __complex__ long double& __z)
814 { return __builtin_csinl(__z); }
816 template<typename _Tp>
818 sin(const complex<_Tp>& __z) { return __complex_sin(__z.__rep()); }
820 template<typename _Tp>
822 sin(const complex<_Tp>& __z) { return __complex_sin(__z); }
825 // 26.2.8/11 sinh(__z): Returns the hyperbolic sine of __z.
826 template<typename _Tp>
828 __complex_sinh(const complex<_Tp>& __z)
830 const _Tp __x = __z.real();
831 const _Tp __y = __z.imag();
832 return complex<_Tp>(sinh(__x) * cos(__y), cosh(__x) * sin(__y));
835 #if _GLIBCXX_USE_C99_COMPLEX
836 inline __complex__ float
837 __complex_sinh(__complex__ float __z) { return __builtin_csinhf(__z); }
839 inline __complex__ double
840 __complex_sinh(__complex__ double __z) { return __builtin_csinh(__z); }
842 inline __complex__ long double
843 __complex_sinh(const __complex__ long double& __z)
844 { return __builtin_csinhl(__z); }
846 template<typename _Tp>
848 sinh(const complex<_Tp>& __z) { return __complex_sinh(__z.__rep()); }
850 template<typename _Tp>
852 sinh(const complex<_Tp>& __z) { return __complex_sinh(__z); }
855 // 26.2.8/13 sqrt(__z): Returns the complex square root of __z.
856 // The branch cut is on the negative axis.
857 template<typename _Tp>
859 __complex_sqrt(const complex<_Tp>& __z)
861 _Tp __x = __z.real();
862 _Tp __y = __z.imag();
866 _Tp __t = sqrt(abs(__y) / 2);
867 return complex<_Tp>(__t, __y < _Tp() ? -__t : __t);
871 _Tp __t = sqrt(2 * (std::abs(__z) + abs(__x)));
874 ? complex<_Tp>(__u, __y / __t)
875 : complex<_Tp>(abs(__y) / __t, __y < _Tp() ? -__u : __u);
879 #if _GLIBCXX_USE_C99_COMPLEX
880 inline __complex__ float
881 __complex_sqrt(__complex__ float __z) { return __builtin_csqrtf(__z); }
883 inline __complex__ double
884 __complex_sqrt(__complex__ double __z) { return __builtin_csqrt(__z); }
886 inline __complex__ long double
887 __complex_sqrt(const __complex__ long double& __z)
888 { return __builtin_csqrtl(__z); }
890 template<typename _Tp>
892 sqrt(const complex<_Tp>& __z) { return __complex_sqrt(__z.__rep()); }
894 template<typename _Tp>
896 sqrt(const complex<_Tp>& __z) { return __complex_sqrt(__z); }
899 // 26.2.8/14 tan(__z): Return the complex tangent of __z.
901 template<typename _Tp>
903 __complex_tan(const complex<_Tp>& __z)
904 { return std::sin(__z) / std::cos(__z); }
906 #if _GLIBCXX_USE_C99_COMPLEX
907 inline __complex__ float
908 __complex_tan(__complex__ float __z) { return __builtin_ctanf(__z); }
910 inline __complex__ double
911 __complex_tan(__complex__ double __z) { return __builtin_ctan(__z); }
913 inline __complex__ long double
914 __complex_tan(const __complex__ long double& __z)
915 { return __builtin_ctanl(__z); }
917 template<typename _Tp>
919 tan(const complex<_Tp>& __z) { return __complex_tan(__z.__rep()); }
921 template<typename _Tp>
923 tan(const complex<_Tp>& __z) { return __complex_tan(__z); }
927 // 26.2.8/15 tanh(__z): Returns the hyperbolic tangent of __z.
929 template<typename _Tp>
931 __complex_tanh(const complex<_Tp>& __z)
932 { return std::sinh(__z) / std::cosh(__z); }
934 #if _GLIBCXX_USE_C99_COMPLEX
935 inline __complex__ float
936 __complex_tanh(__complex__ float __z) { return __builtin_ctanhf(__z); }
938 inline __complex__ double
939 __complex_tanh(__complex__ double __z) { return __builtin_ctanh(__z); }
941 inline __complex__ long double
942 __complex_tanh(const __complex__ long double& __z)
943 { return __builtin_ctanhl(__z); }
945 template<typename _Tp>
947 tanh(const complex<_Tp>& __z) { return __complex_tanh(__z.__rep()); }
949 template<typename _Tp>
951 tanh(const complex<_Tp>& __z) { return __complex_tanh(__z); }
955 // 26.2.8/9 pow(__x, __y): Returns the complex power base of __x
956 // raised to the __y-th power. The branch
957 // cut is on the negative axis.
958 #if __cplusplus < 201103L
959 template<typename _Tp>
961 __complex_pow_unsigned(complex<_Tp> __x, unsigned __n)
963 complex<_Tp> __y = __n % 2 ? __x : complex<_Tp>(1);
975 // _GLIBCXX_RESOLVE_LIB_DEFECTS
976 // DR 844. complex pow return type is ambiguous.
977 template<typename _Tp>
979 pow(const complex<_Tp>& __z, int __n)
982 ? complex<_Tp>(1) / std::__complex_pow_unsigned(__z, -(unsigned)__n)
983 : std::__complex_pow_unsigned(__z, __n);
987 template<typename _Tp>
989 pow(const complex<_Tp>& __x, const _Tp& __y)
991 #ifndef _GLIBCXX_USE_C99_COMPLEX
995 if (__x.imag() == _Tp() && __x.real() > _Tp())
996 return pow(__x.real(), __y);
998 complex<_Tp> __t = std::log(__x);
999 return std::polar(exp(__y * __t.real()), __y * __t.imag());
1002 template<typename _Tp>
1004 __complex_pow(const complex<_Tp>& __x, const complex<_Tp>& __y)
1005 { return __x == _Tp() ? _Tp() : std::exp(__y * std::log(__x)); }
1007 #if _GLIBCXX_USE_C99_COMPLEX
1008 inline __complex__ float
1009 __complex_pow(__complex__ float __x, __complex__ float __y)
1010 { return __builtin_cpowf(__x, __y); }
1012 inline __complex__ double
1013 __complex_pow(__complex__ double __x, __complex__ double __y)
1014 { return __builtin_cpow(__x, __y); }
1016 inline __complex__ long double
1017 __complex_pow(const __complex__ long double& __x,
1018 const __complex__ long double& __y)
1019 { return __builtin_cpowl(__x, __y); }
1021 template<typename _Tp>
1023 pow(const complex<_Tp>& __x, const complex<_Tp>& __y)
1024 { return __complex_pow(__x.__rep(), __y.__rep()); }
1026 template<typename _Tp>
1028 pow(const complex<_Tp>& __x, const complex<_Tp>& __y)
1029 { return __complex_pow(__x, __y); }
1032 template<typename _Tp>
1034 pow(const _Tp& __x, const complex<_Tp>& __y)
1036 return __x > _Tp() ? std::polar(pow(__x, __y.real()),
1037 __y.imag() * log(__x))
1038 : std::pow(complex<_Tp>(__x), __y);
1041 /// 26.2.3 complex specializations
1042 /// complex<float> specialization
1044 struct complex<float>
1046 typedef float value_type;
1047 typedef __complex__ float _ComplexT;
1049 _GLIBCXX_CONSTEXPR complex(_ComplexT __z) : _M_value(__z) { }
1051 _GLIBCXX_CONSTEXPR complex(float __r = 0.0f, float __i = 0.0f)
1052 #if __cplusplus >= 201103L
1053 : _M_value{ __r, __i } { }
1056 __real__ _M_value = __r;
1057 __imag__ _M_value = __i;
1061 explicit _GLIBCXX_CONSTEXPR complex(const complex<double>&);
1062 explicit _GLIBCXX_CONSTEXPR complex(const complex<long double>&);
1064 #if __cplusplus >= 201103L
1065 // _GLIBCXX_RESOLVE_LIB_DEFECTS
1066 // DR 387. std::complex over-encapsulated.
1067 __attribute ((__abi_tag__ ("cxx11")))
1069 real() { return __real__ _M_value; }
1071 __attribute ((__abi_tag__ ("cxx11")))
1073 imag() { return __imag__ _M_value; }
1076 real() { return __real__ _M_value; }
1079 real() const { return __real__ _M_value; }
1082 imag() { return __imag__ _M_value; }
1085 imag() const { return __imag__ _M_value; }
1088 // _GLIBCXX_RESOLVE_LIB_DEFECTS
1089 // DR 387. std::complex over-encapsulated.
1091 real(float __val) { __real__ _M_value = __val; }
1094 imag(float __val) { __imag__ _M_value = __val; }
1097 operator=(float __f)
1104 operator+=(float __f)
1111 operator-=(float __f)
1118 operator*=(float __f)
1125 operator/=(float __f)
1131 // Let the compiler synthesize the copy and assignment
1132 // operator. It always does a pretty good job.
1133 // complex& operator=(const complex&);
1135 template<typename _Tp>
1137 operator=(const complex<_Tp>& __z)
1139 __real__ _M_value = __z.real();
1140 __imag__ _M_value = __z.imag();
1144 template<typename _Tp>
1146 operator+=(const complex<_Tp>& __z)
1148 __real__ _M_value += __z.real();
1149 __imag__ _M_value += __z.imag();
1155 operator-=(const complex<_Tp>& __z)
1157 __real__ _M_value -= __z.real();
1158 __imag__ _M_value -= __z.imag();
1164 operator*=(const complex<_Tp>& __z)
1167 __real__ __t = __z.real();
1168 __imag__ __t = __z.imag();
1175 operator/=(const complex<_Tp>& __z)
1178 __real__ __t = __z.real();
1179 __imag__ __t = __z.imag();
1184 _GLIBCXX_USE_CONSTEXPR _ComplexT __rep() const { return _M_value; }
1190 /// 26.2.3 complex specializations
1191 /// complex<double> specialization
1193 struct complex<double>
1195 typedef double value_type;
1196 typedef __complex__ double _ComplexT;
1198 _GLIBCXX_CONSTEXPR complex(_ComplexT __z) : _M_value(__z) { }
1200 _GLIBCXX_CONSTEXPR complex(double __r = 0.0, double __i = 0.0)
1201 #if __cplusplus >= 201103L
1202 : _M_value{ __r, __i } { }
1205 __real__ _M_value = __r;
1206 __imag__ _M_value = __i;
1210 _GLIBCXX_CONSTEXPR complex(const complex<float>& __z)
1211 : _M_value(__z.__rep()) { }
1213 explicit _GLIBCXX_CONSTEXPR complex(const complex<long double>&);
1215 #if __cplusplus >= 201103L
1216 // _GLIBCXX_RESOLVE_LIB_DEFECTS
1217 // DR 387. std::complex over-encapsulated.
1218 __attribute ((__abi_tag__ ("cxx11")))
1220 real() { return __real__ _M_value; }
1222 __attribute ((__abi_tag__ ("cxx11")))
1224 imag() { return __imag__ _M_value; }
1227 real() { return __real__ _M_value; }
1230 real() const { return __real__ _M_value; }
1233 imag() { return __imag__ _M_value; }
1236 imag() const { return __imag__ _M_value; }
1239 // _GLIBCXX_RESOLVE_LIB_DEFECTS
1240 // DR 387. std::complex over-encapsulated.
1242 real(double __val) { __real__ _M_value = __val; }
1245 imag(double __val) { __imag__ _M_value = __val; }
1248 operator=(double __d)
1255 operator+=(double __d)
1262 operator-=(double __d)
1269 operator*=(double __d)
1276 operator/=(double __d)
1282 // The compiler will synthesize this, efficiently.
1283 // complex& operator=(const complex&);
1285 template<typename _Tp>
1287 operator=(const complex<_Tp>& __z)
1289 __real__ _M_value = __z.real();
1290 __imag__ _M_value = __z.imag();
1294 template<typename _Tp>
1296 operator+=(const complex<_Tp>& __z)
1298 __real__ _M_value += __z.real();
1299 __imag__ _M_value += __z.imag();
1303 template<typename _Tp>
1305 operator-=(const complex<_Tp>& __z)
1307 __real__ _M_value -= __z.real();
1308 __imag__ _M_value -= __z.imag();
1312 template<typename _Tp>
1314 operator*=(const complex<_Tp>& __z)
1317 __real__ __t = __z.real();
1318 __imag__ __t = __z.imag();
1323 template<typename _Tp>
1325 operator/=(const complex<_Tp>& __z)
1328 __real__ __t = __z.real();
1329 __imag__ __t = __z.imag();
1334 _GLIBCXX_USE_CONSTEXPR _ComplexT __rep() const { return _M_value; }
1340 /// 26.2.3 complex specializations
1341 /// complex<long double> specialization
1343 struct complex<long double>
1345 typedef long double value_type;
1346 typedef __complex__ long double _ComplexT;
1348 _GLIBCXX_CONSTEXPR complex(_ComplexT __z) : _M_value(__z) { }
1350 _GLIBCXX_CONSTEXPR complex(long double __r = 0.0L,
1351 long double __i = 0.0L)
1352 #if __cplusplus >= 201103L
1353 : _M_value{ __r, __i } { }
1356 __real__ _M_value = __r;
1357 __imag__ _M_value = __i;
1361 _GLIBCXX_CONSTEXPR complex(const complex<float>& __z)
1362 : _M_value(__z.__rep()) { }
1364 _GLIBCXX_CONSTEXPR complex(const complex<double>& __z)
1365 : _M_value(__z.__rep()) { }
1367 #if __cplusplus >= 201103L
1368 // _GLIBCXX_RESOLVE_LIB_DEFECTS
1369 // DR 387. std::complex over-encapsulated.
1370 __attribute ((__abi_tag__ ("cxx11")))
1371 constexpr long double
1372 real() { return __real__ _M_value; }
1374 __attribute ((__abi_tag__ ("cxx11")))
1375 constexpr long double
1376 imag() { return __imag__ _M_value; }
1379 real() { return __real__ _M_value; }
1382 real() const { return __real__ _M_value; }
1385 imag() { return __imag__ _M_value; }
1388 imag() const { return __imag__ _M_value; }
1391 // _GLIBCXX_RESOLVE_LIB_DEFECTS
1392 // DR 387. std::complex over-encapsulated.
1394 real(long double __val) { __real__ _M_value = __val; }
1397 imag(long double __val) { __imag__ _M_value = __val; }
1400 operator=(long double __r)
1407 operator+=(long double __r)
1414 operator-=(long double __r)
1421 operator*=(long double __r)
1428 operator/=(long double __r)
1434 // The compiler knows how to do this efficiently
1435 // complex& operator=(const complex&);
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)
1459 __real__ _M_value -= __z.real();
1460 __imag__ _M_value -= __z.imag();
1464 template<typename _Tp>
1466 operator*=(const complex<_Tp>& __z)
1469 __real__ __t = __z.real();
1470 __imag__ __t = __z.imag();
1475 template<typename _Tp>
1477 operator/=(const complex<_Tp>& __z)
1480 __real__ __t = __z.real();
1481 __imag__ __t = __z.imag();
1486 _GLIBCXX_USE_CONSTEXPR _ComplexT __rep() const { return _M_value; }
1492 // These bits have to be at the end of this file, so that the
1493 // specializations have all been defined.
1494 inline _GLIBCXX_CONSTEXPR
1495 complex<float>::complex(const complex<double>& __z)
1496 : _M_value(__z.__rep()) { }
1498 inline _GLIBCXX_CONSTEXPR
1499 complex<float>::complex(const complex<long double>& __z)
1500 : _M_value(__z.__rep()) { }
1502 inline _GLIBCXX_CONSTEXPR
1503 complex<double>::complex(const complex<long double>& __z)
1504 : _M_value(__z.__rep()) { }
1506 // Inhibit implicit instantiations for required instantiations,
1507 // which are defined via explicit instantiations elsewhere.
1508 // NB: This syntax is a GNU extension.
1509 #if _GLIBCXX_EXTERN_TEMPLATE
1510 extern template istream& operator>>(istream&, complex<float>&);
1511 extern template ostream& operator<<(ostream&, const complex<float>&);
1512 extern template istream& operator>>(istream&, complex<double>&);
1513 extern template ostream& operator<<(ostream&, const complex<double>&);
1514 extern template istream& operator>>(istream&, complex<long double>&);
1515 extern template ostream& operator<<(ostream&, const complex<long double>&);
1517 #ifdef _GLIBCXX_USE_WCHAR_T
1518 extern template wistream& operator>>(wistream&, complex<float>&);
1519 extern template wostream& operator<<(wostream&, const complex<float>&);
1520 extern template wistream& operator>>(wistream&, complex<double>&);
1521 extern template wostream& operator<<(wostream&, const complex<double>&);
1522 extern template wistream& operator>>(wistream&, complex<long double>&);
1523 extern template wostream& operator<<(wostream&, const complex<long double>&);
1527 // @} group complex_numbers
1529 _GLIBCXX_END_NAMESPACE_VERSION
1532 namespace __gnu_cxx _GLIBCXX_VISIBILITY(default)
1534 _GLIBCXX_BEGIN_NAMESPACE_VERSION
1536 // See ext/type_traits.h for the primary template.
1537 template<typename _Tp, typename _Up>
1538 struct __promote_2<std::complex<_Tp>, _Up>
1541 typedef std::complex<typename __promote_2<_Tp, _Up>::__type> __type;
1544 template<typename _Tp, typename _Up>
1545 struct __promote_2<_Tp, std::complex<_Up> >
1548 typedef std::complex<typename __promote_2<_Tp, _Up>::__type> __type;
1551 template<typename _Tp, typename _Up>
1552 struct __promote_2<std::complex<_Tp>, std::complex<_Up> >
1555 typedef std::complex<typename __promote_2<_Tp, _Up>::__type> __type;
1558 _GLIBCXX_END_NAMESPACE_VERSION
1561 #if __cplusplus >= 201103L
1563 namespace std _GLIBCXX_VISIBILITY(default)
1565 _GLIBCXX_BEGIN_NAMESPACE_VERSION
1567 // Forward declarations.
1568 template<typename _Tp> std::complex<_Tp> acos(const std::complex<_Tp>&);
1569 template<typename _Tp> std::complex<_Tp> asin(const std::complex<_Tp>&);
1570 template<typename _Tp> std::complex<_Tp> atan(const std::complex<_Tp>&);
1572 template<typename _Tp> std::complex<_Tp> acosh(const std::complex<_Tp>&);
1573 template<typename _Tp> std::complex<_Tp> asinh(const std::complex<_Tp>&);
1574 template<typename _Tp> std::complex<_Tp> atanh(const std::complex<_Tp>&);
1576 template<typename _Tp> _Tp fabs(const std::complex<_Tp>&);
1578 template<typename _Tp>
1579 inline std::complex<_Tp>
1580 __complex_acos(const std::complex<_Tp>& __z)
1582 const std::complex<_Tp> __t = std::asin(__z);
1583 const _Tp __pi_2 = 1.5707963267948966192313216916397514L;
1584 return std::complex<_Tp>(__pi_2 - __t.real(), -__t.imag());
1587 #if _GLIBCXX_USE_C99_COMPLEX_TR1
1588 inline __complex__ float
1589 __complex_acos(__complex__ float __z)
1590 { return __builtin_cacosf(__z); }
1592 inline __complex__ double
1593 __complex_acos(__complex__ double __z)
1594 { return __builtin_cacos(__z); }
1596 inline __complex__ long double
1597 __complex_acos(const __complex__ long double& __z)
1598 { return __builtin_cacosl(__z); }
1600 template<typename _Tp>
1601 inline std::complex<_Tp>
1602 acos(const std::complex<_Tp>& __z)
1603 { return __complex_acos(__z.__rep()); }
1605 /// acos(__z) [8.1.2].
1606 // Effects: Behaves the same as C99 function cacos, defined
1607 // in subclause 7.3.5.1.
1608 template<typename _Tp>
1609 inline std::complex<_Tp>
1610 acos(const std::complex<_Tp>& __z)
1611 { return __complex_acos(__z); }
1614 template<typename _Tp>
1615 inline std::complex<_Tp>
1616 __complex_asin(const std::complex<_Tp>& __z)
1618 std::complex<_Tp> __t(-__z.imag(), __z.real());
1619 __t = std::asinh(__t);
1620 return std::complex<_Tp>(__t.imag(), -__t.real());
1623 #if _GLIBCXX_USE_C99_COMPLEX_TR1
1624 inline __complex__ float
1625 __complex_asin(__complex__ float __z)
1626 { return __builtin_casinf(__z); }
1628 inline __complex__ double
1629 __complex_asin(__complex__ double __z)
1630 { return __builtin_casin(__z); }
1632 inline __complex__ long double
1633 __complex_asin(const __complex__ long double& __z)
1634 { return __builtin_casinl(__z); }
1636 template<typename _Tp>
1637 inline std::complex<_Tp>
1638 asin(const std::complex<_Tp>& __z)
1639 { return __complex_asin(__z.__rep()); }
1641 /// asin(__z) [8.1.3].
1642 // Effects: Behaves the same as C99 function casin, defined
1643 // in subclause 7.3.5.2.
1644 template<typename _Tp>
1645 inline std::complex<_Tp>
1646 asin(const std::complex<_Tp>& __z)
1647 { return __complex_asin(__z); }
1650 template<typename _Tp>
1652 __complex_atan(const std::complex<_Tp>& __z)
1654 const _Tp __r2 = __z.real() * __z.real();
1655 const _Tp __x = _Tp(1.0) - __r2 - __z.imag() * __z.imag();
1657 _Tp __num = __z.imag() + _Tp(1.0);
1658 _Tp __den = __z.imag() - _Tp(1.0);
1660 __num = __r2 + __num * __num;
1661 __den = __r2 + __den * __den;
1663 return std::complex<_Tp>(_Tp(0.5) * atan2(_Tp(2.0) * __z.real(), __x),
1664 _Tp(0.25) * log(__num / __den));
1667 #if _GLIBCXX_USE_C99_COMPLEX_TR1
1668 inline __complex__ float
1669 __complex_atan(__complex__ float __z)
1670 { return __builtin_catanf(__z); }
1672 inline __complex__ double
1673 __complex_atan(__complex__ double __z)
1674 { return __builtin_catan(__z); }
1676 inline __complex__ long double
1677 __complex_atan(const __complex__ long double& __z)
1678 { return __builtin_catanl(__z); }
1680 template<typename _Tp>
1681 inline std::complex<_Tp>
1682 atan(const std::complex<_Tp>& __z)
1683 { return __complex_atan(__z.__rep()); }
1685 /// atan(__z) [8.1.4].
1686 // Effects: Behaves the same as C99 function catan, defined
1687 // in subclause 7.3.5.3.
1688 template<typename _Tp>
1689 inline std::complex<_Tp>
1690 atan(const std::complex<_Tp>& __z)
1691 { return __complex_atan(__z); }
1694 template<typename _Tp>
1696 __complex_acosh(const std::complex<_Tp>& __z)
1699 return _Tp(2.0) * std::log(std::sqrt(_Tp(0.5) * (__z + _Tp(1.0)))
1700 + std::sqrt(_Tp(0.5) * (__z - _Tp(1.0))));
1703 #if _GLIBCXX_USE_C99_COMPLEX_TR1
1704 inline __complex__ float
1705 __complex_acosh(__complex__ float __z)
1706 { return __builtin_cacoshf(__z); }
1708 inline __complex__ double
1709 __complex_acosh(__complex__ double __z)
1710 { return __builtin_cacosh(__z); }
1712 inline __complex__ long double
1713 __complex_acosh(const __complex__ long double& __z)
1714 { return __builtin_cacoshl(__z); }
1716 template<typename _Tp>
1717 inline std::complex<_Tp>
1718 acosh(const std::complex<_Tp>& __z)
1719 { return __complex_acosh(__z.__rep()); }
1721 /// acosh(__z) [8.1.5].
1722 // Effects: Behaves the same as C99 function cacosh, defined
1723 // in subclause 7.3.6.1.
1724 template<typename _Tp>
1725 inline std::complex<_Tp>
1726 acosh(const std::complex<_Tp>& __z)
1727 { return __complex_acosh(__z); }
1730 template<typename _Tp>
1732 __complex_asinh(const std::complex<_Tp>& __z)
1734 std::complex<_Tp> __t((__z.real() - __z.imag())
1735 * (__z.real() + __z.imag()) + _Tp(1.0),
1736 _Tp(2.0) * __z.real() * __z.imag());
1737 __t = std::sqrt(__t);
1739 return std::log(__t + __z);
1742 #if _GLIBCXX_USE_C99_COMPLEX_TR1
1743 inline __complex__ float
1744 __complex_asinh(__complex__ float __z)
1745 { return __builtin_casinhf(__z); }
1747 inline __complex__ double
1748 __complex_asinh(__complex__ double __z)
1749 { return __builtin_casinh(__z); }
1751 inline __complex__ long double
1752 __complex_asinh(const __complex__ long double& __z)
1753 { return __builtin_casinhl(__z); }
1755 template<typename _Tp>
1756 inline std::complex<_Tp>
1757 asinh(const std::complex<_Tp>& __z)
1758 { return __complex_asinh(__z.__rep()); }
1760 /// asinh(__z) [8.1.6].
1761 // Effects: Behaves the same as C99 function casin, defined
1762 // in subclause 7.3.6.2.
1763 template<typename _Tp>
1764 inline std::complex<_Tp>
1765 asinh(const std::complex<_Tp>& __z)
1766 { return __complex_asinh(__z); }
1769 template<typename _Tp>
1771 __complex_atanh(const std::complex<_Tp>& __z)
1773 const _Tp __i2 = __z.imag() * __z.imag();
1774 const _Tp __x = _Tp(1.0) - __i2 - __z.real() * __z.real();
1776 _Tp __num = _Tp(1.0) + __z.real();
1777 _Tp __den = _Tp(1.0) - __z.real();
1779 __num = __i2 + __num * __num;
1780 __den = __i2 + __den * __den;
1782 return std::complex<_Tp>(_Tp(0.25) * (log(__num) - log(__den)),
1783 _Tp(0.5) * atan2(_Tp(2.0) * __z.imag(), __x));
1786 #if _GLIBCXX_USE_C99_COMPLEX_TR1
1787 inline __complex__ float
1788 __complex_atanh(__complex__ float __z)
1789 { return __builtin_catanhf(__z); }
1791 inline __complex__ double
1792 __complex_atanh(__complex__ double __z)
1793 { return __builtin_catanh(__z); }
1795 inline __complex__ long double
1796 __complex_atanh(const __complex__ long double& __z)
1797 { return __builtin_catanhl(__z); }
1799 template<typename _Tp>
1800 inline std::complex<_Tp>
1801 atanh(const std::complex<_Tp>& __z)
1802 { return __complex_atanh(__z.__rep()); }
1804 /// atanh(__z) [8.1.7].
1805 // Effects: Behaves the same as C99 function catanh, defined
1806 // in subclause 7.3.6.3.
1807 template<typename _Tp>
1808 inline std::complex<_Tp>
1809 atanh(const std::complex<_Tp>& __z)
1810 { return __complex_atanh(__z); }
1813 template<typename _Tp>
1815 /// fabs(__z) [8.1.8].
1816 // Effects: Behaves the same as C99 function cabs, defined
1817 // in subclause 7.3.8.1.
1818 fabs(const std::complex<_Tp>& __z)
1819 { return std::abs(__z); }
1821 /// Additional overloads [8.1.9].
1822 template<typename _Tp>
1823 inline typename __gnu_cxx::__promote<_Tp>::__type
1826 typedef typename __gnu_cxx::__promote<_Tp>::__type __type;
1827 #if (_GLIBCXX_USE_C99_MATH && !_GLIBCXX_USE_C99_FP_MACROS_DYNAMIC)
1828 return std::signbit(__x) ? __type(3.1415926535897932384626433832795029L)
1831 return std::arg(std::complex<__type>(__x));
1835 template<typename _Tp>
1836 inline typename __gnu_cxx::__promote<_Tp>::__type
1840 template<typename _Tp>
1841 inline typename __gnu_cxx::__promote<_Tp>::__type
1844 typedef typename __gnu_cxx::__promote<_Tp>::__type __type;
1845 return __type(__x) * __type(__x);
1848 template<typename _Tp>
1849 inline typename __gnu_cxx::__promote<_Tp>::__type
1853 template<typename _Tp, typename _Up>
1854 inline std::complex<typename __gnu_cxx::__promote_2<_Tp, _Up>::__type>
1855 pow(const std::complex<_Tp>& __x, const _Up& __y)
1857 typedef typename __gnu_cxx::__promote_2<_Tp, _Up>::__type __type;
1858 return std::pow(std::complex<__type>(__x), __type(__y));
1861 template<typename _Tp, typename _Up>
1862 inline std::complex<typename __gnu_cxx::__promote_2<_Tp, _Up>::__type>
1863 pow(const _Tp& __x, const std::complex<_Up>& __y)
1865 typedef typename __gnu_cxx::__promote_2<_Tp, _Up>::__type __type;
1866 return std::pow(__type(__x), std::complex<__type>(__y));
1869 template<typename _Tp, typename _Up>
1870 inline std::complex<typename __gnu_cxx::__promote_2<_Tp, _Up>::__type>
1871 pow(const std::complex<_Tp>& __x, const std::complex<_Up>& __y)
1873 typedef typename __gnu_cxx::__promote_2<_Tp, _Up>::__type __type;
1874 return std::pow(std::complex<__type>(__x),
1875 std::complex<__type>(__y));
1878 // Forward declarations.
1880 template<typename _Tp> std::complex<_Tp> proj(const std::complex<_Tp>&);
1882 template<typename _Tp>
1884 __complex_proj(const std::complex<_Tp>& __z)
1886 const _Tp __den = (__z.real() * __z.real()
1887 + __z.imag() * __z.imag() + _Tp(1.0));
1889 return std::complex<_Tp>((_Tp(2.0) * __z.real()) / __den,
1890 (_Tp(2.0) * __z.imag()) / __den);
1893 #if _GLIBCXX_USE_C99_COMPLEX
1894 inline __complex__ float
1895 __complex_proj(__complex__ float __z)
1896 { return __builtin_cprojf(__z); }
1898 inline __complex__ double
1899 __complex_proj(__complex__ double __z)
1900 { return __builtin_cproj(__z); }
1902 inline __complex__ long double
1903 __complex_proj(const __complex__ long double& __z)
1904 { return __builtin_cprojl(__z); }
1906 template<typename _Tp>
1907 inline std::complex<_Tp>
1908 proj(const std::complex<_Tp>& __z)
1909 { return __complex_proj(__z.__rep()); }
1911 template<typename _Tp>
1912 inline std::complex<_Tp>
1913 proj(const std::complex<_Tp>& __z)
1914 { return __complex_proj(__z); }
1918 template<typename _Tp>
1919 inline typename __gnu_cxx::__promote<_Tp>::__type
1923 template<typename _Tp>
1924 inline typename __gnu_cxx::__promote<_Tp>::__type
1928 _GLIBCXX_END_NAMESPACE_VERSION
1933 #endif /* _GLIBCXX_COMPLEX */