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
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.
32 /** @file include/complex
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)
57 * @defgroup complex_numbers Complex Numbers
60 * Classes and functions for complex numbers.
64 // Forward declarations.
65 template<typename _Tp> class complex;
66 template<> class complex<float>;
67 template<> class complex<double>;
68 template<> class complex<long double>;
70 /// Return magnitude of @a z.
71 template<typename _Tp> _Tp abs(const complex<_Tp>&);
72 /// Return phase angle of @a z.
73 template<typename _Tp> _Tp arg(const complex<_Tp>&);
74 /// Return @a z magnitude squared.
75 template<typename _Tp> _Tp norm(const complex<_Tp>&);
77 /// Return complex conjugate of @a z.
78 template<typename _Tp> complex<_Tp> conj(const complex<_Tp>&);
79 /// Return complex with magnitude @a rho and angle @a theta.
80 template<typename _Tp> complex<_Tp> polar(const _Tp&, const _Tp& = 0);
83 /// Return complex cosine of @a z.
84 template<typename _Tp> complex<_Tp> cos(const complex<_Tp>&);
85 /// Return complex hyperbolic cosine of @a z.
86 template<typename _Tp> complex<_Tp> cosh(const complex<_Tp>&);
87 /// Return complex base e exponential of @a z.
88 template<typename _Tp> complex<_Tp> exp(const complex<_Tp>&);
89 /// Return complex natural logarithm of @a z.
90 template<typename _Tp> complex<_Tp> log(const complex<_Tp>&);
91 /// Return complex base 10 logarithm of @a z.
92 template<typename _Tp> complex<_Tp> log10(const complex<_Tp>&);
93 #ifndef __GXX_EXPERIMENTAL_CXX0X__
95 /// Return @a x to the @a y'th power.
96 template<typename _Tp> complex<_Tp> pow(const complex<_Tp>&, int);
98 /// Return @a x to the @a y'th power.
99 template<typename _Tp> complex<_Tp> pow(const complex<_Tp>&, const _Tp&);
100 /// Return @a x to the @a y'th power.
101 template<typename _Tp> complex<_Tp> pow(const complex<_Tp>&,
102 const complex<_Tp>&);
103 /// Return @a x to the @a y'th power.
104 template<typename _Tp> complex<_Tp> pow(const _Tp&, const complex<_Tp>&);
105 /// Return complex sine of @a z.
106 template<typename _Tp> complex<_Tp> sin(const complex<_Tp>&);
107 /// Return complex hyperbolic sine of @a z.
108 template<typename _Tp> complex<_Tp> sinh(const complex<_Tp>&);
109 /// Return complex square root of @a z.
110 template<typename _Tp> complex<_Tp> sqrt(const complex<_Tp>&);
111 /// Return complex tangent of @a z.
112 template<typename _Tp> complex<_Tp> tan(const complex<_Tp>&);
113 /// Return complex hyperbolic tangent of @a z.
114 template<typename _Tp> complex<_Tp> tanh(const complex<_Tp>&);
117 // 26.2.2 Primary template class complex
119 * Template to represent complex numbers.
121 * Specializations for float, double, and long double are part of the
122 * library. Results with any other type are not guaranteed.
124 * @param Tp Type of real and imaginary values.
126 template<typename _Tp>
130 typedef _Tp value_type;
132 /// Default constructor. First parameter is x, second parameter is y.
133 /// Unspecified parameters default to 0.
134 complex(const _Tp& __r = _Tp(), const _Tp& __i = _Tp())
135 : _M_real(__r), _M_imag(__i) { }
137 // Lets the compiler synthesize the copy constructor
138 // complex (const complex<_Tp>&);
139 /// Copy constructor.
140 template<typename _Up>
141 complex(const complex<_Up>& __z)
142 : _M_real(__z.real()), _M_imag(__z.imag()) { }
144 #ifdef __GXX_EXPERIMENTAL_CXX0X__
145 // _GLIBCXX_RESOLVE_LIB_DEFECTS
146 // DR 387. std::complex over-encapsulated.
153 /// Return real part of complex number.
157 /// Return real part of complex number.
158 const _Tp& real() const
161 /// Return imaginary part of complex number.
165 /// Return imaginary part of complex number.
166 const _Tp& imag() const
170 // _GLIBCXX_RESOLVE_LIB_DEFECTS
171 // DR 387. std::complex over-encapsulated.
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 const 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>
456 operator==(const complex<_Tp>& __x, const complex<_Tp>& __y)
457 { return __x.real() == __y.real() && __x.imag() == __y.imag(); }
459 template<typename _Tp>
461 operator==(const complex<_Tp>& __x, const _Tp& __y)
462 { return __x.real() == __y && __x.imag() == _Tp(); }
464 template<typename _Tp>
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>
474 operator!=(const complex<_Tp>& __x, const complex<_Tp>& __y)
475 { return __x.real() != __y.real() || __x.imag() != __y.imag(); }
477 template<typename _Tp>
479 operator!=(const complex<_Tp>& __x, const _Tp& __y)
480 { return __x.real() != __y || __x.imag() != _Tp(); }
482 template<typename _Tp>
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 #ifdef __GXX_EXPERIMENTAL_CXX0X__
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 #ifndef __GXX_EXPERIMENTAL_CXX0X__
959 // _GLIBCXX_RESOLVE_LIB_DEFECTS
960 // DR 844. complex pow return type is ambiguous.
961 template<typename _Tp>
963 pow(const complex<_Tp>& __z, int __n)
964 { return std::__pow_helper(__z, __n); }
967 template<typename _Tp>
969 pow(const complex<_Tp>& __x, const _Tp& __y)
971 #ifndef _GLIBCXX_USE_C99_COMPLEX
975 if (__x.imag() == _Tp() && __x.real() > _Tp())
976 return pow(__x.real(), __y);
978 complex<_Tp> __t = std::log(__x);
979 return std::polar(exp(__y * __t.real()), __y * __t.imag());
982 template<typename _Tp>
984 __complex_pow(const complex<_Tp>& __x, const complex<_Tp>& __y)
985 { return __x == _Tp() ? _Tp() : std::exp(__y * std::log(__x)); }
987 #if _GLIBCXX_USE_C99_COMPLEX
988 inline __complex__ float
989 __complex_pow(__complex__ float __x, __complex__ float __y)
990 { return __builtin_cpowf(__x, __y); }
992 inline __complex__ double
993 __complex_pow(__complex__ double __x, __complex__ double __y)
994 { return __builtin_cpow(__x, __y); }
996 inline __complex__ long double
997 __complex_pow(const __complex__ long double& __x,
998 const __complex__ long double& __y)
999 { return __builtin_cpowl(__x, __y); }
1001 template<typename _Tp>
1003 pow(const complex<_Tp>& __x, const complex<_Tp>& __y)
1004 { return __complex_pow(__x.__rep(), __y.__rep()); }
1006 template<typename _Tp>
1008 pow(const complex<_Tp>& __x, const complex<_Tp>& __y)
1009 { return __complex_pow(__x, __y); }
1012 template<typename _Tp>
1014 pow(const _Tp& __x, const complex<_Tp>& __y)
1016 return __x > _Tp() ? std::polar(pow(__x, __y.real()),
1017 __y.imag() * log(__x))
1018 : std::pow(complex<_Tp>(__x), __y);
1021 // 26.2.3 complex specializations
1022 // complex<float> specialization
1024 struct complex<float>
1026 typedef float value_type;
1027 typedef __complex__ float _ComplexT;
1029 complex(_ComplexT __z) : _M_value(__z) { }
1031 complex(float __r = 0.0f, float __i = 0.0f)
1033 __real__ _M_value = __r;
1034 __imag__ _M_value = __i;
1037 explicit complex(const complex<double>&);
1038 explicit complex(const complex<long double>&);
1040 #ifdef __GXX_EXPERIMENTAL_CXX0X__
1041 // _GLIBCXX_RESOLVE_LIB_DEFECTS
1042 // DR 387. std::complex over-encapsulated.
1044 { return __real__ _M_value; }
1047 { return __imag__ _M_value; }
1050 { return __real__ _M_value; }
1052 const float& real() const
1053 { return __real__ _M_value; }
1056 { return __imag__ _M_value; }
1058 const float& imag() const
1059 { return __imag__ _M_value; }
1062 // _GLIBCXX_RESOLVE_LIB_DEFECTS
1063 // DR 387. std::complex over-encapsulated.
1064 void real(float __val)
1065 { __real__ _M_value = __val; }
1067 void imag(float __val)
1068 { __imag__ _M_value = __val; }
1071 operator=(float __f)
1073 __real__ _M_value = __f;
1074 __imag__ _M_value = 0.0f;
1079 operator+=(float __f)
1081 __real__ _M_value += __f;
1086 operator-=(float __f)
1088 __real__ _M_value -= __f;
1093 operator*=(float __f)
1100 operator/=(float __f)
1106 // Let the compiler synthesize the copy and assignment
1107 // operator. It always does a pretty good job.
1108 // complex& operator=(const complex&);
1110 template<typename _Tp>
1112 operator=(const complex<_Tp>& __z)
1114 __real__ _M_value = __z.real();
1115 __imag__ _M_value = __z.imag();
1119 template<typename _Tp>
1121 operator+=(const complex<_Tp>& __z)
1123 __real__ _M_value += __z.real();
1124 __imag__ _M_value += __z.imag();
1130 operator-=(const complex<_Tp>& __z)
1132 __real__ _M_value -= __z.real();
1133 __imag__ _M_value -= __z.imag();
1139 operator*=(const complex<_Tp>& __z)
1142 __real__ __t = __z.real();
1143 __imag__ __t = __z.imag();
1150 operator/=(const complex<_Tp>& __z)
1153 __real__ __t = __z.real();
1154 __imag__ __t = __z.imag();
1159 const _ComplexT& __rep() const { return _M_value; }
1165 // 26.2.3 complex specializations
1166 // complex<double> specialization
1168 struct complex<double>
1170 typedef double value_type;
1171 typedef __complex__ double _ComplexT;
1173 complex(_ComplexT __z) : _M_value(__z) { }
1175 complex(double __r = 0.0, double __i = 0.0)
1177 __real__ _M_value = __r;
1178 __imag__ _M_value = __i;
1181 complex(const complex<float>& __z)
1182 : _M_value(__z.__rep()) { }
1184 explicit complex(const complex<long double>&);
1186 #ifdef __GXX_EXPERIMENTAL_CXX0X__
1187 // _GLIBCXX_RESOLVE_LIB_DEFECTS
1188 // DR 387. std::complex over-encapsulated.
1190 { return __real__ _M_value; }
1193 { return __imag__ _M_value; }
1196 { return __real__ _M_value; }
1198 const double& real() const
1199 { return __real__ _M_value; }
1202 { return __imag__ _M_value; }
1204 const double& imag() const
1205 { return __imag__ _M_value; }
1208 // _GLIBCXX_RESOLVE_LIB_DEFECTS
1209 // DR 387. std::complex over-encapsulated.
1210 void real(double __val)
1211 { __real__ _M_value = __val; }
1213 void imag(double __val)
1214 { __imag__ _M_value = __val; }
1217 operator=(double __d)
1219 __real__ _M_value = __d;
1220 __imag__ _M_value = 0.0;
1225 operator+=(double __d)
1227 __real__ _M_value += __d;
1232 operator-=(double __d)
1234 __real__ _M_value -= __d;
1239 operator*=(double __d)
1246 operator/=(double __d)
1252 // The compiler will synthesize this, efficiently.
1253 // complex& operator=(const complex&);
1255 template<typename _Tp>
1257 operator=(const complex<_Tp>& __z)
1259 __real__ _M_value = __z.real();
1260 __imag__ _M_value = __z.imag();
1264 template<typename _Tp>
1266 operator+=(const complex<_Tp>& __z)
1268 __real__ _M_value += __z.real();
1269 __imag__ _M_value += __z.imag();
1273 template<typename _Tp>
1275 operator-=(const complex<_Tp>& __z)
1277 __real__ _M_value -= __z.real();
1278 __imag__ _M_value -= __z.imag();
1282 template<typename _Tp>
1284 operator*=(const complex<_Tp>& __z)
1287 __real__ __t = __z.real();
1288 __imag__ __t = __z.imag();
1293 template<typename _Tp>
1295 operator/=(const complex<_Tp>& __z)
1298 __real__ __t = __z.real();
1299 __imag__ __t = __z.imag();
1304 const _ComplexT& __rep() const { return _M_value; }
1310 // 26.2.3 complex specializations
1311 // complex<long double> specialization
1313 struct complex<long double>
1315 typedef long double value_type;
1316 typedef __complex__ long double _ComplexT;
1318 complex(_ComplexT __z) : _M_value(__z) { }
1320 complex(long double __r = 0.0L, long double __i = 0.0L)
1322 __real__ _M_value = __r;
1323 __imag__ _M_value = __i;
1326 complex(const complex<float>& __z)
1327 : _M_value(__z.__rep()) { }
1329 complex(const complex<double>& __z)
1330 : _M_value(__z.__rep()) { }
1332 #ifdef __GXX_EXPERIMENTAL_CXX0X__
1333 // _GLIBCXX_RESOLVE_LIB_DEFECTS
1334 // DR 387. std::complex over-encapsulated.
1335 long double real() const
1336 { return __real__ _M_value; }
1338 long double imag() const
1339 { return __imag__ _M_value; }
1342 { return __real__ _M_value; }
1344 const long double& real() const
1345 { return __real__ _M_value; }
1348 { return __imag__ _M_value; }
1350 const long double& imag() const
1351 { return __imag__ _M_value; }
1354 // _GLIBCXX_RESOLVE_LIB_DEFECTS
1355 // DR 387. std::complex over-encapsulated.
1356 void real(long double __val)
1357 { __real__ _M_value = __val; }
1359 void imag(long double __val)
1360 { __imag__ _M_value = __val; }
1362 complex<long double>&
1363 operator=(long double __r)
1365 __real__ _M_value = __r;
1366 __imag__ _M_value = 0.0L;
1370 complex<long double>&
1371 operator+=(long double __r)
1373 __real__ _M_value += __r;
1377 complex<long double>&
1378 operator-=(long double __r)
1380 __real__ _M_value -= __r;
1384 complex<long double>&
1385 operator*=(long double __r)
1391 complex<long double>&
1392 operator/=(long double __r)
1398 // The compiler knows how to do this efficiently
1399 // complex& operator=(const complex&);
1401 template<typename _Tp>
1402 complex<long double>&
1403 operator=(const complex<_Tp>& __z)
1405 __real__ _M_value = __z.real();
1406 __imag__ _M_value = __z.imag();
1410 template<typename _Tp>
1411 complex<long double>&
1412 operator+=(const complex<_Tp>& __z)
1414 __real__ _M_value += __z.real();
1415 __imag__ _M_value += __z.imag();
1419 template<typename _Tp>
1420 complex<long double>&
1421 operator-=(const complex<_Tp>& __z)
1423 __real__ _M_value -= __z.real();
1424 __imag__ _M_value -= __z.imag();
1428 template<typename _Tp>
1429 complex<long double>&
1430 operator*=(const complex<_Tp>& __z)
1433 __real__ __t = __z.real();
1434 __imag__ __t = __z.imag();
1439 template<typename _Tp>
1440 complex<long double>&
1441 operator/=(const complex<_Tp>& __z)
1444 __real__ __t = __z.real();
1445 __imag__ __t = __z.imag();
1450 const _ComplexT& __rep() const { return _M_value; }
1456 // These bits have to be at the end of this file, so that the
1457 // specializations have all been defined.
1459 complex<float>::complex(const complex<double>& __z)
1460 : _M_value(__z.__rep()) { }
1463 complex<float>::complex(const complex<long double>& __z)
1464 : _M_value(__z.__rep()) { }
1467 complex<double>::complex(const complex<long double>& __z)
1468 : _M_value(__z.__rep()) { }
1470 // Inhibit implicit instantiations for required instantiations,
1471 // which are defined via explicit instantiations elsewhere.
1472 // NB: This syntax is a GNU extension.
1473 #if _GLIBCXX_EXTERN_TEMPLATE
1474 extern template istream& operator>>(istream&, complex<float>&);
1475 extern template ostream& operator<<(ostream&, const complex<float>&);
1476 extern template istream& operator>>(istream&, complex<double>&);
1477 extern template ostream& operator<<(ostream&, const complex<double>&);
1478 extern template istream& operator>>(istream&, complex<long double>&);
1479 extern template ostream& operator<<(ostream&, const complex<long double>&);
1481 #ifdef _GLIBCXX_USE_WCHAR_T
1482 extern template wistream& operator>>(wistream&, complex<float>&);
1483 extern template wostream& operator<<(wostream&, const complex<float>&);
1484 extern template wistream& operator>>(wistream&, complex<double>&);
1485 extern template wostream& operator<<(wostream&, const complex<double>&);
1486 extern template wistream& operator>>(wistream&, complex<long double>&);
1487 extern template wostream& operator<<(wostream&, const complex<long double>&);
1491 // @} group complex_numbers
1493 _GLIBCXX_END_NAMESPACE
1495 _GLIBCXX_BEGIN_NAMESPACE(__gnu_cxx)
1497 // See ext/type_traits.h for the primary template.
1498 template<typename _Tp, typename _Up>
1499 struct __promote_2<std::complex<_Tp>, _Up>
1502 typedef std::complex<typename __promote_2<_Tp, _Up>::__type> __type;
1505 template<typename _Tp, typename _Up>
1506 struct __promote_2<_Tp, std::complex<_Up> >
1509 typedef std::complex<typename __promote_2<_Tp, _Up>::__type> __type;
1512 template<typename _Tp, typename _Up>
1513 struct __promote_2<std::complex<_Tp>, std::complex<_Up> >
1516 typedef std::complex<typename __promote_2<_Tp, _Up>::__type> __type;
1519 _GLIBCXX_END_NAMESPACE
1521 #ifdef __GXX_EXPERIMENTAL_CXX0X__
1522 # if defined(_GLIBCXX_INCLUDE_AS_TR1)
1523 # error C++0x header cannot be included from TR1 header
1525 # if defined(_GLIBCXX_INCLUDE_AS_CXX0X)
1526 # include <tr1_impl/complex>
1528 # define _GLIBCXX_INCLUDE_AS_CXX0X
1529 # define _GLIBCXX_BEGIN_NAMESPACE_TR1
1530 # define _GLIBCXX_END_NAMESPACE_TR1
1531 # define _GLIBCXX_TR1
1532 # include <tr1_impl/complex>
1533 # undef _GLIBCXX_TR1
1534 # undef _GLIBCXX_END_NAMESPACE_TR1
1535 # undef _GLIBCXX_BEGIN_NAMESPACE_TR1
1536 # undef _GLIBCXX_INCLUDE_AS_CXX0X
1539 _GLIBCXX_BEGIN_NAMESPACE(std)
1541 // Forward declarations.
1543 template<typename _Tp> std::complex<_Tp> proj(const std::complex<_Tp>&);
1545 template<typename _Tp>
1547 __complex_proj(const std::complex<_Tp>& __z)
1549 const _Tp __den = (__z.real() * __z.real()
1550 + __z.imag() * __z.imag() + _Tp(1.0));
1552 return std::complex<_Tp>((_Tp(2.0) * __z.real()) / __den,
1553 (_Tp(2.0) * __z.imag()) / __den);
1556 #if _GLIBCXX_USE_C99_COMPLEX
1557 inline __complex__ float
1558 __complex_proj(__complex__ float __z)
1559 { return __builtin_cprojf(__z); }
1561 inline __complex__ double
1562 __complex_proj(__complex__ double __z)
1563 { return __builtin_cproj(__z); }
1565 inline __complex__ long double
1566 __complex_proj(const __complex__ long double& __z)
1567 { return __builtin_cprojl(__z); }
1569 template<typename _Tp>
1570 inline std::complex<_Tp>
1571 proj(const std::complex<_Tp>& __z)
1572 { return __complex_proj(__z.__rep()); }
1574 template<typename _Tp>
1575 inline std::complex<_Tp>
1576 proj(const std::complex<_Tp>& __z)
1577 { return __complex_proj(__z); }
1580 template<typename _Tp>
1581 inline std::complex<typename __gnu_cxx::__promote<_Tp>::__type>
1584 typedef typename __gnu_cxx::__promote<_Tp>::__type __type;
1585 return std::proj(std::complex<__type>(__x));
1588 _GLIBCXX_END_NAMESPACE
1592 #endif /* _GLIBCXX_COMPLEX */