1 // The template and inlines for the -*- C++ -*- complex number classes.
3 // Copyright (C) 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005,
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 2, 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 // You should have received a copy of the GNU General Public License
19 // along with this library; see the file COPYING. If not, write to
20 // the Free Software Foundation, 51 Franklin Street, Fifth Floor,
21 // Boston, MA 02110-1301, USA.
23 // As a special exception, you may use this file as part of a free software
24 // library without restriction. Specifically, if other files instantiate
25 // templates or use macros or inline functions from this file, or you compile
26 // this file and link it with other files to produce an executable, this
27 // file does not by itself cause the resulting executable to be covered by
28 // the GNU General Public License. This exception does not however
29 // invalidate any other reasons why the executable file might be covered by
30 // the GNU General Public License.
33 * This is a Standard C++ Library header.
37 // ISO C++ 14882: 26.2 Complex Numbers
38 // Note: this is not a conforming implementation.
39 // Initially implemented by Ulrich Drepper <drepper@cygnus.com>
40 // Improved by Gabriel Dos Reis <dosreis@cmla.ens-cachan.fr>
43 #ifndef _GLIBCXX_COMPLEX
44 #define _GLIBCXX_COMPLEX 1
46 #pragma GCC system_header
48 #include <bits/c++config.h>
49 #include <bits/cpp_type_traits.h>
50 #include <ext/type_traits.h>
54 _GLIBCXX_BEGIN_NAMESPACE(std)
56 // Forward declarations.
57 template<typename _Tp> class complex;
58 template<> class complex<float>;
59 template<> class complex<double>;
60 template<> class complex<long double>;
62 /// Return magnitude of @a z.
63 template<typename _Tp> _Tp abs(const complex<_Tp>&);
64 /// Return phase angle of @a z.
65 template<typename _Tp> _Tp arg(const complex<_Tp>&);
66 /// Return @a z magnitude squared.
67 template<typename _Tp> _Tp norm(const complex<_Tp>&);
69 /// Return complex conjugate of @a z.
70 template<typename _Tp> complex<_Tp> conj(const complex<_Tp>&);
71 /// Return complex with magnitude @a rho and angle @a theta.
72 template<typename _Tp> complex<_Tp> polar(const _Tp&, const _Tp& = 0);
75 /// Return complex cosine of @a z.
76 template<typename _Tp> complex<_Tp> cos(const complex<_Tp>&);
77 /// Return complex hyperbolic cosine of @a z.
78 template<typename _Tp> complex<_Tp> cosh(const complex<_Tp>&);
79 /// Return complex base e exponential of @a z.
80 template<typename _Tp> complex<_Tp> exp(const complex<_Tp>&);
81 /// Return complex natural logarithm of @a z.
82 template<typename _Tp> complex<_Tp> log(const complex<_Tp>&);
83 /// Return complex base 10 logarithm of @a z.
84 template<typename _Tp> complex<_Tp> log10(const complex<_Tp>&);
85 /// Return complex cosine of @a z.
86 template<typename _Tp> complex<_Tp> pow(const complex<_Tp>&, int);
87 /// Return @a x to the @a y'th power.
88 template<typename _Tp> complex<_Tp> pow(const complex<_Tp>&, const _Tp&);
89 /// Return @a x to the @a y'th power.
90 template<typename _Tp> complex<_Tp> pow(const complex<_Tp>&,
92 /// Return @a x to the @a y'th power.
93 template<typename _Tp> complex<_Tp> pow(const _Tp&, const complex<_Tp>&);
94 /// Return complex sine of @a z.
95 template<typename _Tp> complex<_Tp> sin(const complex<_Tp>&);
96 /// Return complex hyperbolic sine of @a z.
97 template<typename _Tp> complex<_Tp> sinh(const complex<_Tp>&);
98 /// Return complex square root of @a z.
99 template<typename _Tp> complex<_Tp> sqrt(const complex<_Tp>&);
100 /// Return complex tangent of @a z.
101 template<typename _Tp> complex<_Tp> tan(const complex<_Tp>&);
102 /// Return complex hyperbolic tangent of @a z.
103 template<typename _Tp> complex<_Tp> tanh(const complex<_Tp>&);
107 // 26.2.2 Primary template class complex
109 * Template to represent complex numbers.
111 * Specializations for float, double, and long double are part of the
112 * library. Results with any other type are not guaranteed.
114 * @param Tp Type of real and imaginary values.
116 template<typename _Tp>
120 typedef _Tp value_type;
122 /// Default constructor. First parameter is x, second parameter is y.
123 /// Unspecified parameters default to 0.
124 complex(const _Tp& __r = _Tp(), const _Tp& __i = _Tp())
125 : _M_real(__r), _M_imag(__i) { }
127 // Lets the compiler synthesize the copy constructor
128 // complex (const complex<_Tp>&);
129 /// Copy constructor.
130 template<typename _Up>
131 complex(const complex<_Up>& __z)
132 : _M_real(__z.real()), _M_imag(__z.imag()) { }
134 #ifdef __GXX_EXPERIMENTAL_CXX0X__
135 // _GLIBCXX_RESOLVE_LIB_DEFECTS
136 // DR 387. std::complex over-encapsulated.
143 /// Return real part of complex number.
147 /// Return real part of complex number.
148 const _Tp& real() const
151 /// Return imaginary part of complex number.
155 /// Return imaginary part of complex number.
156 const _Tp& imag() const
160 // _GLIBCXX_RESOLVE_LIB_DEFECTS
161 // DR 387. std::complex over-encapsulated.
168 /// Assign this complex number to scalar @a t.
169 complex<_Tp>& operator=(const _Tp&);
171 /// Add @a t to this complex number.
174 operator+=(const _Tp& __t)
180 /// Subtract @a t from this complex number.
183 operator-=(const _Tp& __t)
189 /// Multiply this complex number by @a t.
190 complex<_Tp>& operator*=(const _Tp&);
191 /// Divide this complex number by @a t.
192 complex<_Tp>& operator/=(const _Tp&);
194 // Lets the compiler synthesize the
195 // copy and assignment operator
196 // complex<_Tp>& operator= (const complex<_Tp>&);
197 /// Assign this complex number to complex @a z.
198 template<typename _Up>
199 complex<_Tp>& operator=(const complex<_Up>&);
200 /// Add @a z to this complex number.
201 template<typename _Up>
202 complex<_Tp>& operator+=(const complex<_Up>&);
203 /// Subtract @a z from this complex number.
204 template<typename _Up>
205 complex<_Tp>& operator-=(const complex<_Up>&);
206 /// Multiply this complex number by @a z.
207 template<typename _Up>
208 complex<_Tp>& operator*=(const complex<_Up>&);
209 /// Divide this complex number by @a z.
210 template<typename _Up>
211 complex<_Tp>& operator/=(const complex<_Up>&);
213 const complex& __rep() const
221 template<typename _Tp>
223 complex<_Tp>::operator=(const _Tp& __t)
231 template<typename _Tp>
233 complex<_Tp>::operator*=(const _Tp& __t)
241 template<typename _Tp>
243 complex<_Tp>::operator/=(const _Tp& __t)
250 template<typename _Tp>
251 template<typename _Up>
253 complex<_Tp>::operator=(const complex<_Up>& __z)
255 _M_real = __z.real();
256 _M_imag = __z.imag();
261 template<typename _Tp>
262 template<typename _Up>
264 complex<_Tp>::operator+=(const complex<_Up>& __z)
266 _M_real += __z.real();
267 _M_imag += __z.imag();
272 template<typename _Tp>
273 template<typename _Up>
275 complex<_Tp>::operator-=(const complex<_Up>& __z)
277 _M_real -= __z.real();
278 _M_imag -= __z.imag();
283 // XXX: This is a grammar school implementation.
284 template<typename _Tp>
285 template<typename _Up>
287 complex<_Tp>::operator*=(const complex<_Up>& __z)
289 const _Tp __r = _M_real * __z.real() - _M_imag * __z.imag();
290 _M_imag = _M_real * __z.imag() + _M_imag * __z.real();
296 // XXX: This is a grammar school implementation.
297 template<typename _Tp>
298 template<typename _Up>
300 complex<_Tp>::operator/=(const complex<_Up>& __z)
302 const _Tp __r = _M_real * __z.real() + _M_imag * __z.imag();
303 const _Tp __n = std::norm(__z);
304 _M_imag = (_M_imag * __z.real() - _M_real * __z.imag()) / __n;
311 /// Return new complex value @a x plus @a y.
312 template<typename _Tp>
314 operator+(const complex<_Tp>& __x, const complex<_Tp>& __y)
316 complex<_Tp> __r = __x;
321 template<typename _Tp>
323 operator+(const complex<_Tp>& __x, const _Tp& __y)
325 complex<_Tp> __r = __x;
330 template<typename _Tp>
332 operator+(const _Tp& __x, const complex<_Tp>& __y)
334 complex<_Tp> __r = __y;
341 /// Return new complex value @a x minus @a y.
342 template<typename _Tp>
344 operator-(const complex<_Tp>& __x, const complex<_Tp>& __y)
346 complex<_Tp> __r = __x;
351 template<typename _Tp>
353 operator-(const complex<_Tp>& __x, const _Tp& __y)
355 complex<_Tp> __r = __x;
360 template<typename _Tp>
362 operator-(const _Tp& __x, const complex<_Tp>& __y)
364 complex<_Tp> __r(__x, -__y.imag());
365 __r.real() -= __y.real();
371 /// Return new complex value @a x times @a y.
372 template<typename _Tp>
374 operator*(const complex<_Tp>& __x, const complex<_Tp>& __y)
376 complex<_Tp> __r = __x;
381 template<typename _Tp>
383 operator*(const complex<_Tp>& __x, const _Tp& __y)
385 complex<_Tp> __r = __x;
390 template<typename _Tp>
392 operator*(const _Tp& __x, const complex<_Tp>& __y)
394 complex<_Tp> __r = __y;
401 /// Return new complex value @a x divided by @a y.
402 template<typename _Tp>
404 operator/(const complex<_Tp>& __x, const complex<_Tp>& __y)
406 complex<_Tp> __r = __x;
411 template<typename _Tp>
413 operator/(const complex<_Tp>& __x, const _Tp& __y)
415 complex<_Tp> __r = __x;
420 template<typename _Tp>
422 operator/(const _Tp& __x, const complex<_Tp>& __y)
424 complex<_Tp> __r = __x;
431 template<typename _Tp>
433 operator+(const complex<_Tp>& __x)
436 /// Return complex negation of @a x.
437 template<typename _Tp>
439 operator-(const complex<_Tp>& __x)
440 { return complex<_Tp>(-__x.real(), -__x.imag()); }
443 /// Return true if @a x is equal to @a y.
444 template<typename _Tp>
446 operator==(const complex<_Tp>& __x, const complex<_Tp>& __y)
447 { return __x.real() == __y.real() && __x.imag() == __y.imag(); }
449 template<typename _Tp>
451 operator==(const complex<_Tp>& __x, const _Tp& __y)
452 { return __x.real() == __y && __x.imag() == _Tp(); }
454 template<typename _Tp>
456 operator==(const _Tp& __x, const complex<_Tp>& __y)
457 { return __x == __y.real() && _Tp() == __y.imag(); }
461 /// Return false if @a x is equal to @a y.
462 template<typename _Tp>
464 operator!=(const complex<_Tp>& __x, const complex<_Tp>& __y)
465 { return __x.real() != __y.real() || __x.imag() != __y.imag(); }
467 template<typename _Tp>
469 operator!=(const complex<_Tp>& __x, const _Tp& __y)
470 { return __x.real() != __y || __x.imag() != _Tp(); }
472 template<typename _Tp>
474 operator!=(const _Tp& __x, const complex<_Tp>& __y)
475 { return __x != __y.real() || _Tp() != __y.imag(); }
478 /// Extraction operator for complex values.
479 template<typename _Tp, typename _CharT, class _Traits>
480 basic_istream<_CharT, _Traits>&
481 operator>>(basic_istream<_CharT, _Traits>& __is, complex<_Tp>& __x)
488 __is >> __re_x >> __ch;
491 __is >> __im_x >> __ch;
493 __x = complex<_Tp>(__re_x, __im_x);
495 __is.setstate(ios_base::failbit);
497 else if (__ch == ')')
500 __is.setstate(ios_base::failbit);
511 /// Insertion operator for complex values.
512 template<typename _Tp, typename _CharT, class _Traits>
513 basic_ostream<_CharT, _Traits>&
514 operator<<(basic_ostream<_CharT, _Traits>& __os, const complex<_Tp>& __x)
516 basic_ostringstream<_CharT, _Traits> __s;
517 __s.flags(__os.flags());
518 __s.imbue(__os.getloc());
519 __s.precision(__os.precision());
520 __s << '(' << __x.real() << ',' << __x.imag() << ')';
521 return __os << __s.str();
525 #ifdef __GXX_EXPERIMENTAL_CXX0X__
526 template<typename _Tp>
528 real(const complex<_Tp>& __z)
529 { return __z.real(); }
531 template<typename _Tp>
533 imag(const complex<_Tp>& __z)
534 { return __z.imag(); }
536 template<typename _Tp>
538 real(complex<_Tp>& __z)
539 { return __z.real(); }
541 template<typename _Tp>
543 real(const complex<_Tp>& __z)
544 { return __z.real(); }
546 template<typename _Tp>
548 imag(complex<_Tp>& __z)
549 { return __z.imag(); }
551 template<typename _Tp>
553 imag(const complex<_Tp>& __z)
554 { return __z.imag(); }
557 // 26.2.7/3 abs(__z): Returns the magnitude of __z.
558 template<typename _Tp>
560 __complex_abs(const complex<_Tp>& __z)
562 _Tp __x = __z.real();
563 _Tp __y = __z.imag();
564 const _Tp __s = std::max(abs(__x), abs(__y));
565 if (__s == _Tp()) // well ...
569 return __s * sqrt(__x * __x + __y * __y);
572 #if _GLIBCXX_USE_C99_COMPLEX
574 __complex_abs(__complex__ float __z) { return __builtin_cabsf(__z); }
577 __complex_abs(__complex__ double __z) { return __builtin_cabs(__z); }
580 __complex_abs(const __complex__ long double& __z)
581 { return __builtin_cabsl(__z); }
583 template<typename _Tp>
585 abs(const complex<_Tp>& __z) { return __complex_abs(__z.__rep()); }
587 template<typename _Tp>
589 abs(const complex<_Tp>& __z) { return __complex_abs(__z); }
593 // 26.2.7/4: arg(__z): Returns the phase angle of __z.
594 template<typename _Tp>
596 __complex_arg(const complex<_Tp>& __z)
597 { return atan2(__z.imag(), __z.real()); }
599 #if _GLIBCXX_USE_C99_COMPLEX
601 __complex_arg(__complex__ float __z) { return __builtin_cargf(__z); }
604 __complex_arg(__complex__ double __z) { return __builtin_carg(__z); }
607 __complex_arg(const __complex__ long double& __z)
608 { return __builtin_cargl(__z); }
610 template<typename _Tp>
612 arg(const complex<_Tp>& __z) { return __complex_arg(__z.__rep()); }
614 template<typename _Tp>
616 arg(const complex<_Tp>& __z) { return __complex_arg(__z); }
619 // 26.2.7/5: norm(__z) returns the squared magnitude of __z.
620 // As defined, norm() is -not- a norm is the common mathematical
621 // sens used in numerics. The helper class _Norm_helper<> tries to
622 // distinguish between builtin floating point and the rest, so as
623 // to deliver an answer as close as possible to the real value.
627 template<typename _Tp>
628 static inline _Tp _S_do_it(const complex<_Tp>& __z)
630 const _Tp __x = __z.real();
631 const _Tp __y = __z.imag();
632 return __x * __x + __y * __y;
637 struct _Norm_helper<true>
639 template<typename _Tp>
640 static inline _Tp _S_do_it(const complex<_Tp>& __z)
642 _Tp __res = std::abs(__z);
643 return __res * __res;
647 template<typename _Tp>
649 norm(const complex<_Tp>& __z)
651 return _Norm_helper<__is_floating<_Tp>::__value
652 && !_GLIBCXX_FAST_MATH>::_S_do_it(__z);
655 template<typename _Tp>
657 polar(const _Tp& __rho, const _Tp& __theta)
658 { return complex<_Tp>(__rho * cos(__theta), __rho * sin(__theta)); }
660 template<typename _Tp>
662 conj(const complex<_Tp>& __z)
663 { return complex<_Tp>(__z.real(), -__z.imag()); }
667 // 26.2.8/1 cos(__z): Returns the cosine of __z.
668 template<typename _Tp>
670 __complex_cos(const complex<_Tp>& __z)
672 const _Tp __x = __z.real();
673 const _Tp __y = __z.imag();
674 return complex<_Tp>(cos(__x) * cosh(__y), -sin(__x) * sinh(__y));
677 #if _GLIBCXX_USE_C99_COMPLEX
678 inline __complex__ float
679 __complex_cos(__complex__ float __z) { return __builtin_ccosf(__z); }
681 inline __complex__ double
682 __complex_cos(__complex__ double __z) { return __builtin_ccos(__z); }
684 inline __complex__ long double
685 __complex_cos(const __complex__ long double& __z)
686 { return __builtin_ccosl(__z); }
688 template<typename _Tp>
690 cos(const complex<_Tp>& __z) { return __complex_cos(__z.__rep()); }
692 template<typename _Tp>
694 cos(const complex<_Tp>& __z) { return __complex_cos(__z); }
697 // 26.2.8/2 cosh(__z): Returns the hyperbolic cosine of __z.
698 template<typename _Tp>
700 __complex_cosh(const complex<_Tp>& __z)
702 const _Tp __x = __z.real();
703 const _Tp __y = __z.imag();
704 return complex<_Tp>(cosh(__x) * cos(__y), sinh(__x) * sin(__y));
707 #if _GLIBCXX_USE_C99_COMPLEX
708 inline __complex__ float
709 __complex_cosh(__complex__ float __z) { return __builtin_ccoshf(__z); }
711 inline __complex__ double
712 __complex_cosh(__complex__ double __z) { return __builtin_ccosh(__z); }
714 inline __complex__ long double
715 __complex_cosh(const __complex__ long double& __z)
716 { return __builtin_ccoshl(__z); }
718 template<typename _Tp>
720 cosh(const complex<_Tp>& __z) { return __complex_cosh(__z.__rep()); }
722 template<typename _Tp>
724 cosh(const complex<_Tp>& __z) { return __complex_cosh(__z); }
727 // 26.2.8/3 exp(__z): Returns the complex base e exponential of x
728 template<typename _Tp>
730 __complex_exp(const complex<_Tp>& __z)
731 { return std::polar(exp(__z.real()), __z.imag()); }
733 #if _GLIBCXX_USE_C99_COMPLEX
734 inline __complex__ float
735 __complex_exp(__complex__ float __z) { return __builtin_cexpf(__z); }
737 inline __complex__ double
738 __complex_exp(__complex__ double __z) { return __builtin_cexp(__z); }
740 inline __complex__ long double
741 __complex_exp(const __complex__ long double& __z)
742 { return __builtin_cexpl(__z); }
744 template<typename _Tp>
746 exp(const complex<_Tp>& __z) { return __complex_exp(__z.__rep()); }
748 template<typename _Tp>
750 exp(const complex<_Tp>& __z) { return __complex_exp(__z); }
753 // 26.2.8/5 log(__z): Returns the natural complex logarithm of __z.
754 // The branch cut is along the negative axis.
755 template<typename _Tp>
757 __complex_log(const complex<_Tp>& __z)
758 { return complex<_Tp>(log(std::abs(__z)), std::arg(__z)); }
760 #if _GLIBCXX_USE_C99_COMPLEX
761 inline __complex__ float
762 __complex_log(__complex__ float __z) { return __builtin_clogf(__z); }
764 inline __complex__ double
765 __complex_log(__complex__ double __z) { return __builtin_clog(__z); }
767 inline __complex__ long double
768 __complex_log(const __complex__ long double& __z)
769 { return __builtin_clogl(__z); }
771 template<typename _Tp>
773 log(const complex<_Tp>& __z) { return __complex_log(__z.__rep()); }
775 template<typename _Tp>
777 log(const complex<_Tp>& __z) { return __complex_log(__z); }
780 template<typename _Tp>
782 log10(const complex<_Tp>& __z)
783 { return std::log(__z) / log(_Tp(10.0)); }
785 // 26.2.8/10 sin(__z): Returns the sine of __z.
786 template<typename _Tp>
788 __complex_sin(const complex<_Tp>& __z)
790 const _Tp __x = __z.real();
791 const _Tp __y = __z.imag();
792 return complex<_Tp>(sin(__x) * cosh(__y), cos(__x) * sinh(__y));
795 #if _GLIBCXX_USE_C99_COMPLEX
796 inline __complex__ float
797 __complex_sin(__complex__ float __z) { return __builtin_csinf(__z); }
799 inline __complex__ double
800 __complex_sin(__complex__ double __z) { return __builtin_csin(__z); }
802 inline __complex__ long double
803 __complex_sin(const __complex__ long double& __z)
804 { return __builtin_csinl(__z); }
806 template<typename _Tp>
808 sin(const complex<_Tp>& __z) { return __complex_sin(__z.__rep()); }
810 template<typename _Tp>
812 sin(const complex<_Tp>& __z) { return __complex_sin(__z); }
815 // 26.2.8/11 sinh(__z): Returns the hyperbolic sine of __z.
816 template<typename _Tp>
818 __complex_sinh(const complex<_Tp>& __z)
820 const _Tp __x = __z.real();
821 const _Tp __y = __z.imag();
822 return complex<_Tp>(sinh(__x) * cos(__y), cosh(__x) * sin(__y));
825 #if _GLIBCXX_USE_C99_COMPLEX
826 inline __complex__ float
827 __complex_sinh(__complex__ float __z) { return __builtin_csinhf(__z); }
829 inline __complex__ double
830 __complex_sinh(__complex__ double __z) { return __builtin_csinh(__z); }
832 inline __complex__ long double
833 __complex_sinh(const __complex__ long double& __z)
834 { return __builtin_csinhl(__z); }
836 template<typename _Tp>
838 sinh(const complex<_Tp>& __z) { return __complex_sinh(__z.__rep()); }
840 template<typename _Tp>
842 sinh(const complex<_Tp>& __z) { return __complex_sinh(__z); }
845 // 26.2.8/13 sqrt(__z): Returns the complex square root of __z.
846 // The branch cut is on the negative axis.
847 template<typename _Tp>
849 __complex_sqrt(const complex<_Tp>& __z)
851 _Tp __x = __z.real();
852 _Tp __y = __z.imag();
856 _Tp __t = sqrt(abs(__y) / 2);
857 return complex<_Tp>(__t, __y < _Tp() ? -__t : __t);
861 _Tp __t = sqrt(2 * (std::abs(__z) + abs(__x)));
864 ? complex<_Tp>(__u, __y / __t)
865 : complex<_Tp>(abs(__y) / __t, __y < _Tp() ? -__u : __u);
869 #if _GLIBCXX_USE_C99_COMPLEX
870 inline __complex__ float
871 __complex_sqrt(__complex__ float __z) { return __builtin_csqrtf(__z); }
873 inline __complex__ double
874 __complex_sqrt(__complex__ double __z) { return __builtin_csqrt(__z); }
876 inline __complex__ long double
877 __complex_sqrt(const __complex__ long double& __z)
878 { return __builtin_csqrtl(__z); }
880 template<typename _Tp>
882 sqrt(const complex<_Tp>& __z) { return __complex_sqrt(__z.__rep()); }
884 template<typename _Tp>
886 sqrt(const complex<_Tp>& __z) { return __complex_sqrt(__z); }
889 // 26.2.8/14 tan(__z): Return the complex tangent of __z.
891 template<typename _Tp>
893 __complex_tan(const complex<_Tp>& __z)
894 { return std::sin(__z) / std::cos(__z); }
896 #if _GLIBCXX_USE_C99_COMPLEX
897 inline __complex__ float
898 __complex_tan(__complex__ float __z) { return __builtin_ctanf(__z); }
900 inline __complex__ double
901 __complex_tan(__complex__ double __z) { return __builtin_ctan(__z); }
903 inline __complex__ long double
904 __complex_tan(const __complex__ long double& __z)
905 { return __builtin_ctanl(__z); }
907 template<typename _Tp>
909 tan(const complex<_Tp>& __z) { return __complex_tan(__z.__rep()); }
911 template<typename _Tp>
913 tan(const complex<_Tp>& __z) { return __complex_tan(__z); }
917 // 26.2.8/15 tanh(__z): Returns the hyperbolic tangent of __z.
919 template<typename _Tp>
921 __complex_tanh(const complex<_Tp>& __z)
922 { return std::sinh(__z) / std::cosh(__z); }
924 #if _GLIBCXX_USE_C99_COMPLEX
925 inline __complex__ float
926 __complex_tanh(__complex__ float __z) { return __builtin_ctanhf(__z); }
928 inline __complex__ double
929 __complex_tanh(__complex__ double __z) { return __builtin_ctanh(__z); }
931 inline __complex__ long double
932 __complex_tanh(const __complex__ long double& __z)
933 { return __builtin_ctanhl(__z); }
935 template<typename _Tp>
937 tanh(const complex<_Tp>& __z) { return __complex_tanh(__z.__rep()); }
939 template<typename _Tp>
941 tanh(const complex<_Tp>& __z) { return __complex_tanh(__z); }
945 // 26.2.8/9 pow(__x, __y): Returns the complex power base of __x
946 // raised to the __y-th power. The branch
947 // cut is on the negative axis.
948 template<typename _Tp>
950 pow(const complex<_Tp>& __z, int __n)
951 { return std::__pow_helper(__z, __n); }
953 template<typename _Tp>
955 pow(const complex<_Tp>& __x, const _Tp& __y)
957 #ifndef _GLIBCXX_USE_C99_COMPLEX
961 if (__x.imag() == _Tp() && __x.real() > _Tp())
962 return pow(__x.real(), __y);
964 complex<_Tp> __t = std::log(__x);
965 return std::polar(exp(__y * __t.real()), __y * __t.imag());
968 template<typename _Tp>
970 __complex_pow(const complex<_Tp>& __x, const complex<_Tp>& __y)
971 { return __x == _Tp() ? _Tp() : std::exp(__y * std::log(__x)); }
973 #if _GLIBCXX_USE_C99_COMPLEX
974 inline __complex__ float
975 __complex_pow(__complex__ float __x, __complex__ float __y)
976 { return __builtin_cpowf(__x, __y); }
978 inline __complex__ double
979 __complex_pow(__complex__ double __x, __complex__ double __y)
980 { return __builtin_cpow(__x, __y); }
982 inline __complex__ long double
983 __complex_pow(const __complex__ long double& __x,
984 const __complex__ long double& __y)
985 { return __builtin_cpowl(__x, __y); }
987 template<typename _Tp>
989 pow(const complex<_Tp>& __x, const complex<_Tp>& __y)
990 { return __complex_pow(__x.__rep(), __y.__rep()); }
992 template<typename _Tp>
994 pow(const complex<_Tp>& __x, const complex<_Tp>& __y)
995 { return __complex_pow(__x, __y); }
998 template<typename _Tp>
1000 pow(const _Tp& __x, const complex<_Tp>& __y)
1002 return __x > _Tp() ? std::polar(pow(__x, __y.real()),
1003 __y.imag() * log(__x))
1004 : std::pow(complex<_Tp>(__x), __y);
1007 // 26.2.3 complex specializations
1008 // complex<float> specialization
1010 struct complex<float>
1012 typedef float value_type;
1013 typedef __complex__ float _ComplexT;
1015 complex(_ComplexT __z) : _M_value(__z) { }
1017 complex(float __r = 0.0f, float __i = 0.0f)
1019 __real__ _M_value = __r;
1020 __imag__ _M_value = __i;
1023 explicit complex(const complex<double>&);
1024 explicit complex(const complex<long double>&);
1026 #ifdef __GXX_EXPERIMENTAL_CXX0X__
1027 // _GLIBCXX_RESOLVE_LIB_DEFECTS
1028 // DR 387. std::complex over-encapsulated.
1030 { return __real__ _M_value; }
1033 { return __imag__ _M_value; }
1036 { return __real__ _M_value; }
1038 const float& real() const
1039 { return __real__ _M_value; }
1042 { return __imag__ _M_value; }
1044 const float& imag() const
1045 { return __imag__ _M_value; }
1048 // _GLIBCXX_RESOLVE_LIB_DEFECTS
1049 // DR 387. std::complex over-encapsulated.
1050 void real(float __val)
1051 { __real__ _M_value = __val; }
1053 void imag(float __val)
1054 { __imag__ _M_value = __val; }
1057 operator=(float __f)
1059 __real__ _M_value = __f;
1060 __imag__ _M_value = 0.0f;
1065 operator+=(float __f)
1067 __real__ _M_value += __f;
1072 operator-=(float __f)
1074 __real__ _M_value -= __f;
1079 operator*=(float __f)
1086 operator/=(float __f)
1092 // Let the compiler synthesize the copy and assignment
1093 // operator. It always does a pretty good job.
1094 // complex& operator=(const complex&);
1096 template<typename _Tp>
1098 operator=(const complex<_Tp>& __z)
1100 __real__ _M_value = __z.real();
1101 __imag__ _M_value = __z.imag();
1105 template<typename _Tp>
1107 operator+=(const complex<_Tp>& __z)
1109 __real__ _M_value += __z.real();
1110 __imag__ _M_value += __z.imag();
1116 operator-=(const complex<_Tp>& __z)
1118 __real__ _M_value -= __z.real();
1119 __imag__ _M_value -= __z.imag();
1125 operator*=(const complex<_Tp>& __z)
1128 __real__ __t = __z.real();
1129 __imag__ __t = __z.imag();
1136 operator/=(const complex<_Tp>& __z)
1139 __real__ __t = __z.real();
1140 __imag__ __t = __z.imag();
1145 const _ComplexT& __rep() const { return _M_value; }
1151 // 26.2.3 complex specializations
1152 // complex<double> specialization
1154 struct complex<double>
1156 typedef double value_type;
1157 typedef __complex__ double _ComplexT;
1159 complex(_ComplexT __z) : _M_value(__z) { }
1161 complex(double __r = 0.0, double __i = 0.0)
1163 __real__ _M_value = __r;
1164 __imag__ _M_value = __i;
1167 complex(const complex<float>& __z)
1168 : _M_value(__z.__rep()) { }
1170 explicit complex(const complex<long double>&);
1172 #ifdef __GXX_EXPERIMENTAL_CXX0X__
1173 // _GLIBCXX_RESOLVE_LIB_DEFECTS
1174 // DR 387. std::complex over-encapsulated.
1176 { return __real__ _M_value; }
1179 { return __imag__ _M_value; }
1182 { return __real__ _M_value; }
1184 const double& real() const
1185 { return __real__ _M_value; }
1188 { return __imag__ _M_value; }
1190 const double& imag() const
1191 { return __imag__ _M_value; }
1194 // _GLIBCXX_RESOLVE_LIB_DEFECTS
1195 // DR 387. std::complex over-encapsulated.
1196 void real(double __val)
1197 { __real__ _M_value = __val; }
1199 void imag(double __val)
1200 { __imag__ _M_value = __val; }
1203 operator=(double __d)
1205 __real__ _M_value = __d;
1206 __imag__ _M_value = 0.0;
1211 operator+=(double __d)
1213 __real__ _M_value += __d;
1218 operator-=(double __d)
1220 __real__ _M_value -= __d;
1225 operator*=(double __d)
1232 operator/=(double __d)
1238 // The compiler will synthesize this, efficiently.
1239 // complex& operator=(const complex&);
1241 template<typename _Tp>
1243 operator=(const complex<_Tp>& __z)
1245 __real__ _M_value = __z.real();
1246 __imag__ _M_value = __z.imag();
1250 template<typename _Tp>
1252 operator+=(const complex<_Tp>& __z)
1254 __real__ _M_value += __z.real();
1255 __imag__ _M_value += __z.imag();
1259 template<typename _Tp>
1261 operator-=(const complex<_Tp>& __z)
1263 __real__ _M_value -= __z.real();
1264 __imag__ _M_value -= __z.imag();
1268 template<typename _Tp>
1270 operator*=(const complex<_Tp>& __z)
1273 __real__ __t = __z.real();
1274 __imag__ __t = __z.imag();
1279 template<typename _Tp>
1281 operator/=(const complex<_Tp>& __z)
1284 __real__ __t = __z.real();
1285 __imag__ __t = __z.imag();
1290 const _ComplexT& __rep() const { return _M_value; }
1296 // 26.2.3 complex specializations
1297 // complex<long double> specialization
1299 struct complex<long double>
1301 typedef long double value_type;
1302 typedef __complex__ long double _ComplexT;
1304 complex(_ComplexT __z) : _M_value(__z) { }
1306 complex(long double __r = 0.0L, long double __i = 0.0L)
1308 __real__ _M_value = __r;
1309 __imag__ _M_value = __i;
1312 complex(const complex<float>& __z)
1313 : _M_value(__z.__rep()) { }
1315 complex(const complex<double>& __z)
1316 : _M_value(__z.__rep()) { }
1318 #ifdef __GXX_EXPERIMENTAL_CXX0X__
1319 // _GLIBCXX_RESOLVE_LIB_DEFECTS
1320 // DR 387. std::complex over-encapsulated.
1321 long double real() const
1322 { return __real__ _M_value; }
1324 long double imag() const
1325 { return __imag__ _M_value; }
1328 { return __real__ _M_value; }
1330 const long double& real() const
1331 { return __real__ _M_value; }
1334 { return __imag__ _M_value; }
1336 const long double& imag() const
1337 { return __imag__ _M_value; }
1340 // _GLIBCXX_RESOLVE_LIB_DEFECTS
1341 // DR 387. std::complex over-encapsulated.
1342 void real(long double __val)
1343 { __real__ _M_value = __val; }
1345 void imag(long double __val)
1346 { __imag__ _M_value = __val; }
1348 complex<long double>&
1349 operator=(long double __r)
1351 __real__ _M_value = __r;
1352 __imag__ _M_value = 0.0L;
1356 complex<long double>&
1357 operator+=(long double __r)
1359 __real__ _M_value += __r;
1363 complex<long double>&
1364 operator-=(long double __r)
1366 __real__ _M_value -= __r;
1370 complex<long double>&
1371 operator*=(long double __r)
1377 complex<long double>&
1378 operator/=(long double __r)
1384 // The compiler knows how to do this efficiently
1385 // complex& operator=(const complex&);
1387 template<typename _Tp>
1388 complex<long double>&
1389 operator=(const complex<_Tp>& __z)
1391 __real__ _M_value = __z.real();
1392 __imag__ _M_value = __z.imag();
1396 template<typename _Tp>
1397 complex<long double>&
1398 operator+=(const complex<_Tp>& __z)
1400 __real__ _M_value += __z.real();
1401 __imag__ _M_value += __z.imag();
1405 template<typename _Tp>
1406 complex<long double>&
1407 operator-=(const complex<_Tp>& __z)
1409 __real__ _M_value -= __z.real();
1410 __imag__ _M_value -= __z.imag();
1414 template<typename _Tp>
1415 complex<long double>&
1416 operator*=(const complex<_Tp>& __z)
1419 __real__ __t = __z.real();
1420 __imag__ __t = __z.imag();
1425 template<typename _Tp>
1426 complex<long double>&
1427 operator/=(const complex<_Tp>& __z)
1430 __real__ __t = __z.real();
1431 __imag__ __t = __z.imag();
1436 const _ComplexT& __rep() const { return _M_value; }
1442 // These bits have to be at the end of this file, so that the
1443 // specializations have all been defined.
1444 // ??? No, they have to be there because of compiler limitation at
1445 // inlining. It suffices that class specializations be defined.
1447 complex<float>::complex(const complex<double>& __z)
1448 : _M_value(__z.__rep()) { }
1451 complex<float>::complex(const complex<long double>& __z)
1452 : _M_value(__z.__rep()) { }
1455 complex<double>::complex(const complex<long double>& __z)
1456 : _M_value(__z.__rep()) { }
1458 // Inhibit implicit instantiations for required instantiations,
1459 // which are defined via explicit instantiations elsewhere.
1460 // NB: This syntax is a GNU extension.
1461 #if _GLIBCXX_EXTERN_TEMPLATE
1462 extern template istream& operator>>(istream&, complex<float>&);
1463 extern template ostream& operator<<(ostream&, const complex<float>&);
1464 extern template istream& operator>>(istream&, complex<double>&);
1465 extern template ostream& operator<<(ostream&, const complex<double>&);
1466 extern template istream& operator>>(istream&, complex<long double>&);
1467 extern template ostream& operator<<(ostream&, const complex<long double>&);
1469 #ifdef _GLIBCXX_USE_WCHAR_T
1470 extern template wistream& operator>>(wistream&, complex<float>&);
1471 extern template wostream& operator<<(wostream&, const complex<float>&);
1472 extern template wistream& operator>>(wistream&, complex<double>&);
1473 extern template wostream& operator<<(wostream&, const complex<double>&);
1474 extern template wistream& operator>>(wistream&, complex<long double>&);
1475 extern template wostream& operator<<(wostream&, const complex<long double>&);
1479 _GLIBCXX_END_NAMESPACE
1481 _GLIBCXX_BEGIN_NAMESPACE(__gnu_cxx)
1483 // See ext/type_traits.h for the primary template.
1484 template<typename _Tp, typename _Up>
1485 struct __promote_2<std::complex<_Tp>, _Up>
1488 typedef std::complex<typename __promote_2<_Tp, _Up>::__type> __type;
1491 template<typename _Tp, typename _Up>
1492 struct __promote_2<_Tp, std::complex<_Up> >
1495 typedef std::complex<typename __promote_2<_Tp, _Up>::__type> __type;
1498 template<typename _Tp, typename _Up>
1499 struct __promote_2<std::complex<_Tp>, std::complex<_Up> >
1502 typedef std::complex<typename __promote_2<_Tp, _Up>::__type> __type;
1505 _GLIBCXX_END_NAMESPACE
1507 #ifdef __GXX_EXPERIMENTAL_CXX0X__
1508 # if defined(_GLIBCXX_INCLUDE_AS_TR1)
1509 # error C++0x header cannot be included from TR1 header
1511 # if defined(_GLIBCXX_INCLUDE_AS_CXX0X)
1512 # include <tr1_impl/complex>
1514 # define _GLIBCXX_INCLUDE_AS_CXX0X
1515 # define _GLIBCXX_BEGIN_NAMESPACE_TR1
1516 # define _GLIBCXX_END_NAMESPACE_TR1
1517 # define _GLIBCXX_TR1
1518 # include <tr1_impl/complex>
1519 # undef _GLIBCXX_TR1
1520 # undef _GLIBCXX_END_NAMESPACE_TR1
1521 # undef _GLIBCXX_BEGIN_NAMESPACE_TR1
1522 # undef _GLIBCXX_INCLUDE_AS_CXX0X
1525 _GLIBCXX_BEGIN_NAMESPACE(std)
1527 // Forward declarations.
1529 template<typename _Tp> std::complex<_Tp> proj(const std::complex<_Tp>&);
1531 template<typename _Tp>
1533 __complex_proj(const std::complex<_Tp>& __z)
1535 const _Tp __den = (__z.real() * __z.real()
1536 + __z.imag() * __z.imag() + _Tp(1.0));
1538 return std::complex<_Tp>((_Tp(2.0) * __z.real()) / __den,
1539 (_Tp(2.0) * __z.imag()) / __den);
1542 #if _GLIBCXX_USE_C99_COMPLEX
1543 inline __complex__ float
1544 __complex_proj(__complex__ float __z)
1545 { return __builtin_cprojf(__z); }
1547 inline __complex__ double
1548 __complex_proj(__complex__ double __z)
1549 { return __builtin_cproj(__z); }
1551 inline __complex__ long double
1552 __complex_proj(const __complex__ long double& __z)
1553 { return __builtin_cprojl(__z); }
1555 template<typename _Tp>
1556 inline std::complex<_Tp>
1557 proj(const std::complex<_Tp>& __z)
1558 { return __complex_proj(__z.__rep()); }
1560 template<typename _Tp>
1561 inline std::complex<_Tp>
1562 proj(const std::complex<_Tp>& __z)
1563 { return __complex_proj(__z); }
1566 template<typename _Tp>
1567 inline std::complex<typename __gnu_cxx::__promote<_Tp>::__type>
1570 typedef typename __gnu_cxx::__promote<_Tp>::__type __type;
1571 return std::proj(std::complex<__type>(__x));
1574 _GLIBCXX_END_NAMESPACE
1578 #endif /* _GLIBCXX_COMPLEX */