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 #ifndef __GXX_EXPERIMENTAL_CXX0X__
87 /// Return @a x to the @a y'th power.
88 template<typename _Tp> complex<_Tp> pow(const complex<_Tp>&, int);
90 /// Return @a x to the @a y'th power.
91 template<typename _Tp> complex<_Tp> pow(const complex<_Tp>&, const _Tp&);
92 /// Return @a x to the @a y'th power.
93 template<typename _Tp> complex<_Tp> pow(const complex<_Tp>&,
95 /// Return @a x to the @a y'th power.
96 template<typename _Tp> complex<_Tp> pow(const _Tp&, const complex<_Tp>&);
97 /// Return complex sine of @a z.
98 template<typename _Tp> complex<_Tp> sin(const complex<_Tp>&);
99 /// Return complex hyperbolic sine of @a z.
100 template<typename _Tp> complex<_Tp> sinh(const complex<_Tp>&);
101 /// Return complex square root of @a z.
102 template<typename _Tp> complex<_Tp> sqrt(const complex<_Tp>&);
103 /// Return complex tangent of @a z.
104 template<typename _Tp> complex<_Tp> tan(const complex<_Tp>&);
105 /// Return complex hyperbolic tangent of @a z.
106 template<typename _Tp> complex<_Tp> tanh(const complex<_Tp>&);
110 // 26.2.2 Primary template class complex
112 * Template to represent complex numbers.
114 * Specializations for float, double, and long double are part of the
115 * library. Results with any other type are not guaranteed.
117 * @param Tp Type of real and imaginary values.
119 template<typename _Tp>
123 typedef _Tp value_type;
125 /// Default constructor. First parameter is x, second parameter is y.
126 /// Unspecified parameters default to 0.
127 complex(const _Tp& __r = _Tp(), const _Tp& __i = _Tp())
128 : _M_real(__r), _M_imag(__i) { }
130 // Lets the compiler synthesize the copy constructor
131 // complex (const complex<_Tp>&);
132 /// Copy constructor.
133 template<typename _Up>
134 complex(const complex<_Up>& __z)
135 : _M_real(__z.real()), _M_imag(__z.imag()) { }
137 #ifdef __GXX_EXPERIMENTAL_CXX0X__
138 // _GLIBCXX_RESOLVE_LIB_DEFECTS
139 // DR 387. std::complex over-encapsulated.
146 /// Return real part of complex number.
150 /// Return real part of complex number.
151 const _Tp& real() const
154 /// Return imaginary part of complex number.
158 /// Return imaginary part of complex number.
159 const _Tp& imag() const
163 // _GLIBCXX_RESOLVE_LIB_DEFECTS
164 // DR 387. std::complex over-encapsulated.
171 /// Assign this complex number to scalar @a t.
172 complex<_Tp>& operator=(const _Tp&);
174 /// Add @a t to this complex number.
177 operator+=(const _Tp& __t)
183 /// Subtract @a t from this complex number.
186 operator-=(const _Tp& __t)
192 /// Multiply this complex number by @a t.
193 complex<_Tp>& operator*=(const _Tp&);
194 /// Divide this complex number by @a t.
195 complex<_Tp>& operator/=(const _Tp&);
197 // Lets the compiler synthesize the
198 // copy and assignment operator
199 // complex<_Tp>& operator= (const complex<_Tp>&);
200 /// Assign this complex number to complex @a z.
201 template<typename _Up>
202 complex<_Tp>& operator=(const complex<_Up>&);
203 /// Add @a z to this complex number.
204 template<typename _Up>
205 complex<_Tp>& operator+=(const complex<_Up>&);
206 /// Subtract @a z from this complex number.
207 template<typename _Up>
208 complex<_Tp>& operator-=(const complex<_Up>&);
209 /// Multiply this complex number by @a z.
210 template<typename _Up>
211 complex<_Tp>& operator*=(const complex<_Up>&);
212 /// Divide this complex number by @a z.
213 template<typename _Up>
214 complex<_Tp>& operator/=(const complex<_Up>&);
216 const complex& __rep() const
224 template<typename _Tp>
226 complex<_Tp>::operator=(const _Tp& __t)
234 template<typename _Tp>
236 complex<_Tp>::operator*=(const _Tp& __t)
244 template<typename _Tp>
246 complex<_Tp>::operator/=(const _Tp& __t)
253 template<typename _Tp>
254 template<typename _Up>
256 complex<_Tp>::operator=(const complex<_Up>& __z)
258 _M_real = __z.real();
259 _M_imag = __z.imag();
264 template<typename _Tp>
265 template<typename _Up>
267 complex<_Tp>::operator+=(const complex<_Up>& __z)
269 _M_real += __z.real();
270 _M_imag += __z.imag();
275 template<typename _Tp>
276 template<typename _Up>
278 complex<_Tp>::operator-=(const complex<_Up>& __z)
280 _M_real -= __z.real();
281 _M_imag -= __z.imag();
286 // XXX: This is a grammar school implementation.
287 template<typename _Tp>
288 template<typename _Up>
290 complex<_Tp>::operator*=(const complex<_Up>& __z)
292 const _Tp __r = _M_real * __z.real() - _M_imag * __z.imag();
293 _M_imag = _M_real * __z.imag() + _M_imag * __z.real();
299 // XXX: This is a grammar school implementation.
300 template<typename _Tp>
301 template<typename _Up>
303 complex<_Tp>::operator/=(const complex<_Up>& __z)
305 const _Tp __r = _M_real * __z.real() + _M_imag * __z.imag();
306 const _Tp __n = std::norm(__z);
307 _M_imag = (_M_imag * __z.real() - _M_real * __z.imag()) / __n;
314 /// Return new complex value @a x plus @a y.
315 template<typename _Tp>
317 operator+(const complex<_Tp>& __x, const complex<_Tp>& __y)
319 complex<_Tp> __r = __x;
324 template<typename _Tp>
326 operator+(const complex<_Tp>& __x, const _Tp& __y)
328 complex<_Tp> __r = __x;
333 template<typename _Tp>
335 operator+(const _Tp& __x, const complex<_Tp>& __y)
337 complex<_Tp> __r = __y;
344 /// Return new complex value @a x minus @a y.
345 template<typename _Tp>
347 operator-(const complex<_Tp>& __x, const complex<_Tp>& __y)
349 complex<_Tp> __r = __x;
354 template<typename _Tp>
356 operator-(const complex<_Tp>& __x, const _Tp& __y)
358 complex<_Tp> __r = __x;
363 template<typename _Tp>
365 operator-(const _Tp& __x, const complex<_Tp>& __y)
367 complex<_Tp> __r(__x, -__y.imag());
368 __r.real() -= __y.real();
374 /// Return new complex value @a x times @a y.
375 template<typename _Tp>
377 operator*(const complex<_Tp>& __x, const complex<_Tp>& __y)
379 complex<_Tp> __r = __x;
384 template<typename _Tp>
386 operator*(const complex<_Tp>& __x, const _Tp& __y)
388 complex<_Tp> __r = __x;
393 template<typename _Tp>
395 operator*(const _Tp& __x, const complex<_Tp>& __y)
397 complex<_Tp> __r = __y;
404 /// Return new complex value @a x divided by @a y.
405 template<typename _Tp>
407 operator/(const complex<_Tp>& __x, const complex<_Tp>& __y)
409 complex<_Tp> __r = __x;
414 template<typename _Tp>
416 operator/(const complex<_Tp>& __x, const _Tp& __y)
418 complex<_Tp> __r = __x;
423 template<typename _Tp>
425 operator/(const _Tp& __x, const complex<_Tp>& __y)
427 complex<_Tp> __r = __x;
434 template<typename _Tp>
436 operator+(const complex<_Tp>& __x)
439 /// Return complex negation of @a x.
440 template<typename _Tp>
442 operator-(const complex<_Tp>& __x)
443 { return complex<_Tp>(-__x.real(), -__x.imag()); }
446 /// Return true if @a x is equal to @a y.
447 template<typename _Tp>
449 operator==(const complex<_Tp>& __x, const complex<_Tp>& __y)
450 { return __x.real() == __y.real() && __x.imag() == __y.imag(); }
452 template<typename _Tp>
454 operator==(const complex<_Tp>& __x, const _Tp& __y)
455 { return __x.real() == __y && __x.imag() == _Tp(); }
457 template<typename _Tp>
459 operator==(const _Tp& __x, const complex<_Tp>& __y)
460 { return __x == __y.real() && _Tp() == __y.imag(); }
464 /// Return false if @a x is equal to @a y.
465 template<typename _Tp>
467 operator!=(const complex<_Tp>& __x, const complex<_Tp>& __y)
468 { return __x.real() != __y.real() || __x.imag() != __y.imag(); }
470 template<typename _Tp>
472 operator!=(const complex<_Tp>& __x, const _Tp& __y)
473 { return __x.real() != __y || __x.imag() != _Tp(); }
475 template<typename _Tp>
477 operator!=(const _Tp& __x, const complex<_Tp>& __y)
478 { return __x != __y.real() || _Tp() != __y.imag(); }
481 /// Extraction operator for complex values.
482 template<typename _Tp, typename _CharT, class _Traits>
483 basic_istream<_CharT, _Traits>&
484 operator>>(basic_istream<_CharT, _Traits>& __is, complex<_Tp>& __x)
491 __is >> __re_x >> __ch;
494 __is >> __im_x >> __ch;
496 __x = complex<_Tp>(__re_x, __im_x);
498 __is.setstate(ios_base::failbit);
500 else if (__ch == ')')
503 __is.setstate(ios_base::failbit);
514 /// Insertion operator for complex values.
515 template<typename _Tp, typename _CharT, class _Traits>
516 basic_ostream<_CharT, _Traits>&
517 operator<<(basic_ostream<_CharT, _Traits>& __os, const complex<_Tp>& __x)
519 basic_ostringstream<_CharT, _Traits> __s;
520 __s.flags(__os.flags());
521 __s.imbue(__os.getloc());
522 __s.precision(__os.precision());
523 __s << '(' << __x.real() << ',' << __x.imag() << ')';
524 return __os << __s.str();
528 #ifdef __GXX_EXPERIMENTAL_CXX0X__
529 template<typename _Tp>
531 real(const complex<_Tp>& __z)
532 { return __z.real(); }
534 template<typename _Tp>
536 imag(const complex<_Tp>& __z)
537 { return __z.imag(); }
539 template<typename _Tp>
541 real(complex<_Tp>& __z)
542 { return __z.real(); }
544 template<typename _Tp>
546 real(const complex<_Tp>& __z)
547 { return __z.real(); }
549 template<typename _Tp>
551 imag(complex<_Tp>& __z)
552 { return __z.imag(); }
554 template<typename _Tp>
556 imag(const complex<_Tp>& __z)
557 { return __z.imag(); }
560 // 26.2.7/3 abs(__z): Returns the magnitude of __z.
561 template<typename _Tp>
563 __complex_abs(const complex<_Tp>& __z)
565 _Tp __x = __z.real();
566 _Tp __y = __z.imag();
567 const _Tp __s = std::max(abs(__x), abs(__y));
568 if (__s == _Tp()) // well ...
572 return __s * sqrt(__x * __x + __y * __y);
575 #if _GLIBCXX_USE_C99_COMPLEX
577 __complex_abs(__complex__ float __z) { return __builtin_cabsf(__z); }
580 __complex_abs(__complex__ double __z) { return __builtin_cabs(__z); }
583 __complex_abs(const __complex__ long double& __z)
584 { return __builtin_cabsl(__z); }
586 template<typename _Tp>
588 abs(const complex<_Tp>& __z) { return __complex_abs(__z.__rep()); }
590 template<typename _Tp>
592 abs(const complex<_Tp>& __z) { return __complex_abs(__z); }
596 // 26.2.7/4: arg(__z): Returns the phase angle of __z.
597 template<typename _Tp>
599 __complex_arg(const complex<_Tp>& __z)
600 { return atan2(__z.imag(), __z.real()); }
602 #if _GLIBCXX_USE_C99_COMPLEX
604 __complex_arg(__complex__ float __z) { return __builtin_cargf(__z); }
607 __complex_arg(__complex__ double __z) { return __builtin_carg(__z); }
610 __complex_arg(const __complex__ long double& __z)
611 { return __builtin_cargl(__z); }
613 template<typename _Tp>
615 arg(const complex<_Tp>& __z) { return __complex_arg(__z.__rep()); }
617 template<typename _Tp>
619 arg(const complex<_Tp>& __z) { return __complex_arg(__z); }
622 // 26.2.7/5: norm(__z) returns the squared magnitude of __z.
623 // As defined, norm() is -not- a norm is the common mathematical
624 // sens used in numerics. The helper class _Norm_helper<> tries to
625 // distinguish between builtin floating point and the rest, so as
626 // to deliver an answer as close as possible to the real value.
630 template<typename _Tp>
631 static inline _Tp _S_do_it(const complex<_Tp>& __z)
633 const _Tp __x = __z.real();
634 const _Tp __y = __z.imag();
635 return __x * __x + __y * __y;
640 struct _Norm_helper<true>
642 template<typename _Tp>
643 static inline _Tp _S_do_it(const complex<_Tp>& __z)
645 _Tp __res = std::abs(__z);
646 return __res * __res;
650 template<typename _Tp>
652 norm(const complex<_Tp>& __z)
654 return _Norm_helper<__is_floating<_Tp>::__value
655 && !_GLIBCXX_FAST_MATH>::_S_do_it(__z);
658 template<typename _Tp>
660 polar(const _Tp& __rho, const _Tp& __theta)
661 { return complex<_Tp>(__rho * cos(__theta), __rho * sin(__theta)); }
663 template<typename _Tp>
665 conj(const complex<_Tp>& __z)
666 { return complex<_Tp>(__z.real(), -__z.imag()); }
670 // 26.2.8/1 cos(__z): Returns the cosine of __z.
671 template<typename _Tp>
673 __complex_cos(const complex<_Tp>& __z)
675 const _Tp __x = __z.real();
676 const _Tp __y = __z.imag();
677 return complex<_Tp>(cos(__x) * cosh(__y), -sin(__x) * sinh(__y));
680 #if _GLIBCXX_USE_C99_COMPLEX
681 inline __complex__ float
682 __complex_cos(__complex__ float __z) { return __builtin_ccosf(__z); }
684 inline __complex__ double
685 __complex_cos(__complex__ double __z) { return __builtin_ccos(__z); }
687 inline __complex__ long double
688 __complex_cos(const __complex__ long double& __z)
689 { return __builtin_ccosl(__z); }
691 template<typename _Tp>
693 cos(const complex<_Tp>& __z) { return __complex_cos(__z.__rep()); }
695 template<typename _Tp>
697 cos(const complex<_Tp>& __z) { return __complex_cos(__z); }
700 // 26.2.8/2 cosh(__z): Returns the hyperbolic cosine of __z.
701 template<typename _Tp>
703 __complex_cosh(const complex<_Tp>& __z)
705 const _Tp __x = __z.real();
706 const _Tp __y = __z.imag();
707 return complex<_Tp>(cosh(__x) * cos(__y), sinh(__x) * sin(__y));
710 #if _GLIBCXX_USE_C99_COMPLEX
711 inline __complex__ float
712 __complex_cosh(__complex__ float __z) { return __builtin_ccoshf(__z); }
714 inline __complex__ double
715 __complex_cosh(__complex__ double __z) { return __builtin_ccosh(__z); }
717 inline __complex__ long double
718 __complex_cosh(const __complex__ long double& __z)
719 { return __builtin_ccoshl(__z); }
721 template<typename _Tp>
723 cosh(const complex<_Tp>& __z) { return __complex_cosh(__z.__rep()); }
725 template<typename _Tp>
727 cosh(const complex<_Tp>& __z) { return __complex_cosh(__z); }
730 // 26.2.8/3 exp(__z): Returns the complex base e exponential of x
731 template<typename _Tp>
733 __complex_exp(const complex<_Tp>& __z)
734 { return std::polar(exp(__z.real()), __z.imag()); }
736 #if _GLIBCXX_USE_C99_COMPLEX
737 inline __complex__ float
738 __complex_exp(__complex__ float __z) { return __builtin_cexpf(__z); }
740 inline __complex__ double
741 __complex_exp(__complex__ double __z) { return __builtin_cexp(__z); }
743 inline __complex__ long double
744 __complex_exp(const __complex__ long double& __z)
745 { return __builtin_cexpl(__z); }
747 template<typename _Tp>
749 exp(const complex<_Tp>& __z) { return __complex_exp(__z.__rep()); }
751 template<typename _Tp>
753 exp(const complex<_Tp>& __z) { return __complex_exp(__z); }
756 // 26.2.8/5 log(__z): Returns the natural complex logarithm of __z.
757 // The branch cut is along the negative axis.
758 template<typename _Tp>
760 __complex_log(const complex<_Tp>& __z)
761 { return complex<_Tp>(log(std::abs(__z)), std::arg(__z)); }
763 #if _GLIBCXX_USE_C99_COMPLEX
764 inline __complex__ float
765 __complex_log(__complex__ float __z) { return __builtin_clogf(__z); }
767 inline __complex__ double
768 __complex_log(__complex__ double __z) { return __builtin_clog(__z); }
770 inline __complex__ long double
771 __complex_log(const __complex__ long double& __z)
772 { return __builtin_clogl(__z); }
774 template<typename _Tp>
776 log(const complex<_Tp>& __z) { return __complex_log(__z.__rep()); }
778 template<typename _Tp>
780 log(const complex<_Tp>& __z) { return __complex_log(__z); }
783 template<typename _Tp>
785 log10(const complex<_Tp>& __z)
786 { return std::log(__z) / log(_Tp(10.0)); }
788 // 26.2.8/10 sin(__z): Returns the sine of __z.
789 template<typename _Tp>
791 __complex_sin(const complex<_Tp>& __z)
793 const _Tp __x = __z.real();
794 const _Tp __y = __z.imag();
795 return complex<_Tp>(sin(__x) * cosh(__y), cos(__x) * sinh(__y));
798 #if _GLIBCXX_USE_C99_COMPLEX
799 inline __complex__ float
800 __complex_sin(__complex__ float __z) { return __builtin_csinf(__z); }
802 inline __complex__ double
803 __complex_sin(__complex__ double __z) { return __builtin_csin(__z); }
805 inline __complex__ long double
806 __complex_sin(const __complex__ long double& __z)
807 { return __builtin_csinl(__z); }
809 template<typename _Tp>
811 sin(const complex<_Tp>& __z) { return __complex_sin(__z.__rep()); }
813 template<typename _Tp>
815 sin(const complex<_Tp>& __z) { return __complex_sin(__z); }
818 // 26.2.8/11 sinh(__z): Returns the hyperbolic sine of __z.
819 template<typename _Tp>
821 __complex_sinh(const complex<_Tp>& __z)
823 const _Tp __x = __z.real();
824 const _Tp __y = __z.imag();
825 return complex<_Tp>(sinh(__x) * cos(__y), cosh(__x) * sin(__y));
828 #if _GLIBCXX_USE_C99_COMPLEX
829 inline __complex__ float
830 __complex_sinh(__complex__ float __z) { return __builtin_csinhf(__z); }
832 inline __complex__ double
833 __complex_sinh(__complex__ double __z) { return __builtin_csinh(__z); }
835 inline __complex__ long double
836 __complex_sinh(const __complex__ long double& __z)
837 { return __builtin_csinhl(__z); }
839 template<typename _Tp>
841 sinh(const complex<_Tp>& __z) { return __complex_sinh(__z.__rep()); }
843 template<typename _Tp>
845 sinh(const complex<_Tp>& __z) { return __complex_sinh(__z); }
848 // 26.2.8/13 sqrt(__z): Returns the complex square root of __z.
849 // The branch cut is on the negative axis.
850 template<typename _Tp>
852 __complex_sqrt(const complex<_Tp>& __z)
854 _Tp __x = __z.real();
855 _Tp __y = __z.imag();
859 _Tp __t = sqrt(abs(__y) / 2);
860 return complex<_Tp>(__t, __y < _Tp() ? -__t : __t);
864 _Tp __t = sqrt(2 * (std::abs(__z) + abs(__x)));
867 ? complex<_Tp>(__u, __y / __t)
868 : complex<_Tp>(abs(__y) / __t, __y < _Tp() ? -__u : __u);
872 #if _GLIBCXX_USE_C99_COMPLEX
873 inline __complex__ float
874 __complex_sqrt(__complex__ float __z) { return __builtin_csqrtf(__z); }
876 inline __complex__ double
877 __complex_sqrt(__complex__ double __z) { return __builtin_csqrt(__z); }
879 inline __complex__ long double
880 __complex_sqrt(const __complex__ long double& __z)
881 { return __builtin_csqrtl(__z); }
883 template<typename _Tp>
885 sqrt(const complex<_Tp>& __z) { return __complex_sqrt(__z.__rep()); }
887 template<typename _Tp>
889 sqrt(const complex<_Tp>& __z) { return __complex_sqrt(__z); }
892 // 26.2.8/14 tan(__z): Return the complex tangent of __z.
894 template<typename _Tp>
896 __complex_tan(const complex<_Tp>& __z)
897 { return std::sin(__z) / std::cos(__z); }
899 #if _GLIBCXX_USE_C99_COMPLEX
900 inline __complex__ float
901 __complex_tan(__complex__ float __z) { return __builtin_ctanf(__z); }
903 inline __complex__ double
904 __complex_tan(__complex__ double __z) { return __builtin_ctan(__z); }
906 inline __complex__ long double
907 __complex_tan(const __complex__ long double& __z)
908 { return __builtin_ctanl(__z); }
910 template<typename _Tp>
912 tan(const complex<_Tp>& __z) { return __complex_tan(__z.__rep()); }
914 template<typename _Tp>
916 tan(const complex<_Tp>& __z) { return __complex_tan(__z); }
920 // 26.2.8/15 tanh(__z): Returns the hyperbolic tangent of __z.
922 template<typename _Tp>
924 __complex_tanh(const complex<_Tp>& __z)
925 { return std::sinh(__z) / std::cosh(__z); }
927 #if _GLIBCXX_USE_C99_COMPLEX
928 inline __complex__ float
929 __complex_tanh(__complex__ float __z) { return __builtin_ctanhf(__z); }
931 inline __complex__ double
932 __complex_tanh(__complex__ double __z) { return __builtin_ctanh(__z); }
934 inline __complex__ long double
935 __complex_tanh(const __complex__ long double& __z)
936 { return __builtin_ctanhl(__z); }
938 template<typename _Tp>
940 tanh(const complex<_Tp>& __z) { return __complex_tanh(__z.__rep()); }
942 template<typename _Tp>
944 tanh(const complex<_Tp>& __z) { return __complex_tanh(__z); }
948 // 26.2.8/9 pow(__x, __y): Returns the complex power base of __x
949 // raised to the __y-th power. The branch
950 // cut is on the negative axis.
951 #ifndef __GXX_EXPERIMENTAL_CXX0X__
952 // _GLIBCXX_RESOLVE_LIB_DEFECTS
953 // DR 844. complex pow return type is ambiguous.
954 template<typename _Tp>
956 pow(const complex<_Tp>& __z, int __n)
957 { return std::__pow_helper(__z, __n); }
960 template<typename _Tp>
962 pow(const complex<_Tp>& __x, const _Tp& __y)
964 #ifndef _GLIBCXX_USE_C99_COMPLEX
968 if (__x.imag() == _Tp() && __x.real() > _Tp())
969 return pow(__x.real(), __y);
971 complex<_Tp> __t = std::log(__x);
972 return std::polar(exp(__y * __t.real()), __y * __t.imag());
975 template<typename _Tp>
977 __complex_pow(const complex<_Tp>& __x, const complex<_Tp>& __y)
978 { return __x == _Tp() ? _Tp() : std::exp(__y * std::log(__x)); }
980 #if _GLIBCXX_USE_C99_COMPLEX
981 inline __complex__ float
982 __complex_pow(__complex__ float __x, __complex__ float __y)
983 { return __builtin_cpowf(__x, __y); }
985 inline __complex__ double
986 __complex_pow(__complex__ double __x, __complex__ double __y)
987 { return __builtin_cpow(__x, __y); }
989 inline __complex__ long double
990 __complex_pow(const __complex__ long double& __x,
991 const __complex__ long double& __y)
992 { return __builtin_cpowl(__x, __y); }
994 template<typename _Tp>
996 pow(const complex<_Tp>& __x, const complex<_Tp>& __y)
997 { return __complex_pow(__x.__rep(), __y.__rep()); }
999 template<typename _Tp>
1001 pow(const complex<_Tp>& __x, const complex<_Tp>& __y)
1002 { return __complex_pow(__x, __y); }
1005 template<typename _Tp>
1007 pow(const _Tp& __x, const complex<_Tp>& __y)
1009 return __x > _Tp() ? std::polar(pow(__x, __y.real()),
1010 __y.imag() * log(__x))
1011 : std::pow(complex<_Tp>(__x), __y);
1014 // 26.2.3 complex specializations
1015 // complex<float> specialization
1017 struct complex<float>
1019 typedef float value_type;
1020 typedef __complex__ float _ComplexT;
1022 complex(_ComplexT __z) : _M_value(__z) { }
1024 complex(float __r = 0.0f, float __i = 0.0f)
1026 __real__ _M_value = __r;
1027 __imag__ _M_value = __i;
1030 explicit complex(const complex<double>&);
1031 explicit complex(const complex<long double>&);
1033 #ifdef __GXX_EXPERIMENTAL_CXX0X__
1034 // _GLIBCXX_RESOLVE_LIB_DEFECTS
1035 // DR 387. std::complex over-encapsulated.
1037 { return __real__ _M_value; }
1040 { return __imag__ _M_value; }
1043 { return __real__ _M_value; }
1045 const float& real() const
1046 { return __real__ _M_value; }
1049 { return __imag__ _M_value; }
1051 const float& imag() const
1052 { return __imag__ _M_value; }
1055 // _GLIBCXX_RESOLVE_LIB_DEFECTS
1056 // DR 387. std::complex over-encapsulated.
1057 void real(float __val)
1058 { __real__ _M_value = __val; }
1060 void imag(float __val)
1061 { __imag__ _M_value = __val; }
1064 operator=(float __f)
1066 __real__ _M_value = __f;
1067 __imag__ _M_value = 0.0f;
1072 operator+=(float __f)
1074 __real__ _M_value += __f;
1079 operator-=(float __f)
1081 __real__ _M_value -= __f;
1086 operator*=(float __f)
1093 operator/=(float __f)
1099 // Let the compiler synthesize the copy and assignment
1100 // operator. It always does a pretty good job.
1101 // complex& operator=(const complex&);
1103 template<typename _Tp>
1105 operator=(const complex<_Tp>& __z)
1107 __real__ _M_value = __z.real();
1108 __imag__ _M_value = __z.imag();
1112 template<typename _Tp>
1114 operator+=(const complex<_Tp>& __z)
1116 __real__ _M_value += __z.real();
1117 __imag__ _M_value += __z.imag();
1123 operator-=(const complex<_Tp>& __z)
1125 __real__ _M_value -= __z.real();
1126 __imag__ _M_value -= __z.imag();
1132 operator*=(const complex<_Tp>& __z)
1135 __real__ __t = __z.real();
1136 __imag__ __t = __z.imag();
1143 operator/=(const complex<_Tp>& __z)
1146 __real__ __t = __z.real();
1147 __imag__ __t = __z.imag();
1152 const _ComplexT& __rep() const { return _M_value; }
1158 // 26.2.3 complex specializations
1159 // complex<double> specialization
1161 struct complex<double>
1163 typedef double value_type;
1164 typedef __complex__ double _ComplexT;
1166 complex(_ComplexT __z) : _M_value(__z) { }
1168 complex(double __r = 0.0, double __i = 0.0)
1170 __real__ _M_value = __r;
1171 __imag__ _M_value = __i;
1174 complex(const complex<float>& __z)
1175 : _M_value(__z.__rep()) { }
1177 explicit complex(const complex<long double>&);
1179 #ifdef __GXX_EXPERIMENTAL_CXX0X__
1180 // _GLIBCXX_RESOLVE_LIB_DEFECTS
1181 // DR 387. std::complex over-encapsulated.
1183 { return __real__ _M_value; }
1186 { return __imag__ _M_value; }
1189 { return __real__ _M_value; }
1191 const double& real() const
1192 { return __real__ _M_value; }
1195 { return __imag__ _M_value; }
1197 const double& imag() const
1198 { return __imag__ _M_value; }
1201 // _GLIBCXX_RESOLVE_LIB_DEFECTS
1202 // DR 387. std::complex over-encapsulated.
1203 void real(double __val)
1204 { __real__ _M_value = __val; }
1206 void imag(double __val)
1207 { __imag__ _M_value = __val; }
1210 operator=(double __d)
1212 __real__ _M_value = __d;
1213 __imag__ _M_value = 0.0;
1218 operator+=(double __d)
1220 __real__ _M_value += __d;
1225 operator-=(double __d)
1227 __real__ _M_value -= __d;
1232 operator*=(double __d)
1239 operator/=(double __d)
1245 // The compiler will synthesize this, efficiently.
1246 // complex& operator=(const complex&);
1248 template<typename _Tp>
1250 operator=(const complex<_Tp>& __z)
1252 __real__ _M_value = __z.real();
1253 __imag__ _M_value = __z.imag();
1257 template<typename _Tp>
1259 operator+=(const complex<_Tp>& __z)
1261 __real__ _M_value += __z.real();
1262 __imag__ _M_value += __z.imag();
1266 template<typename _Tp>
1268 operator-=(const complex<_Tp>& __z)
1270 __real__ _M_value -= __z.real();
1271 __imag__ _M_value -= __z.imag();
1275 template<typename _Tp>
1277 operator*=(const complex<_Tp>& __z)
1280 __real__ __t = __z.real();
1281 __imag__ __t = __z.imag();
1286 template<typename _Tp>
1288 operator/=(const complex<_Tp>& __z)
1291 __real__ __t = __z.real();
1292 __imag__ __t = __z.imag();
1297 const _ComplexT& __rep() const { return _M_value; }
1303 // 26.2.3 complex specializations
1304 // complex<long double> specialization
1306 struct complex<long double>
1308 typedef long double value_type;
1309 typedef __complex__ long double _ComplexT;
1311 complex(_ComplexT __z) : _M_value(__z) { }
1313 complex(long double __r = 0.0L, long double __i = 0.0L)
1315 __real__ _M_value = __r;
1316 __imag__ _M_value = __i;
1319 complex(const complex<float>& __z)
1320 : _M_value(__z.__rep()) { }
1322 complex(const complex<double>& __z)
1323 : _M_value(__z.__rep()) { }
1325 #ifdef __GXX_EXPERIMENTAL_CXX0X__
1326 // _GLIBCXX_RESOLVE_LIB_DEFECTS
1327 // DR 387. std::complex over-encapsulated.
1328 long double real() const
1329 { return __real__ _M_value; }
1331 long double imag() const
1332 { return __imag__ _M_value; }
1335 { return __real__ _M_value; }
1337 const long double& real() const
1338 { return __real__ _M_value; }
1341 { return __imag__ _M_value; }
1343 const long double& imag() const
1344 { return __imag__ _M_value; }
1347 // _GLIBCXX_RESOLVE_LIB_DEFECTS
1348 // DR 387. std::complex over-encapsulated.
1349 void real(long double __val)
1350 { __real__ _M_value = __val; }
1352 void imag(long double __val)
1353 { __imag__ _M_value = __val; }
1355 complex<long double>&
1356 operator=(long double __r)
1358 __real__ _M_value = __r;
1359 __imag__ _M_value = 0.0L;
1363 complex<long double>&
1364 operator+=(long double __r)
1366 __real__ _M_value += __r;
1370 complex<long double>&
1371 operator-=(long double __r)
1373 __real__ _M_value -= __r;
1377 complex<long double>&
1378 operator*=(long double __r)
1384 complex<long double>&
1385 operator/=(long double __r)
1391 // The compiler knows how to do this efficiently
1392 // complex& operator=(const complex&);
1394 template<typename _Tp>
1395 complex<long double>&
1396 operator=(const complex<_Tp>& __z)
1398 __real__ _M_value = __z.real();
1399 __imag__ _M_value = __z.imag();
1403 template<typename _Tp>
1404 complex<long double>&
1405 operator+=(const complex<_Tp>& __z)
1407 __real__ _M_value += __z.real();
1408 __imag__ _M_value += __z.imag();
1412 template<typename _Tp>
1413 complex<long double>&
1414 operator-=(const complex<_Tp>& __z)
1416 __real__ _M_value -= __z.real();
1417 __imag__ _M_value -= __z.imag();
1421 template<typename _Tp>
1422 complex<long double>&
1423 operator*=(const complex<_Tp>& __z)
1426 __real__ __t = __z.real();
1427 __imag__ __t = __z.imag();
1432 template<typename _Tp>
1433 complex<long double>&
1434 operator/=(const complex<_Tp>& __z)
1437 __real__ __t = __z.real();
1438 __imag__ __t = __z.imag();
1443 const _ComplexT& __rep() const { return _M_value; }
1449 // These bits have to be at the end of this file, so that the
1450 // specializations have all been defined.
1451 // ??? No, they have to be there because of compiler limitation at
1452 // inlining. It suffices that class specializations be defined.
1454 complex<float>::complex(const complex<double>& __z)
1455 : _M_value(__z.__rep()) { }
1458 complex<float>::complex(const complex<long double>& __z)
1459 : _M_value(__z.__rep()) { }
1462 complex<double>::complex(const complex<long double>& __z)
1463 : _M_value(__z.__rep()) { }
1465 // Inhibit implicit instantiations for required instantiations,
1466 // which are defined via explicit instantiations elsewhere.
1467 // NB: This syntax is a GNU extension.
1468 #if _GLIBCXX_EXTERN_TEMPLATE
1469 extern template istream& operator>>(istream&, complex<float>&);
1470 extern template ostream& operator<<(ostream&, const complex<float>&);
1471 extern template istream& operator>>(istream&, complex<double>&);
1472 extern template ostream& operator<<(ostream&, const complex<double>&);
1473 extern template istream& operator>>(istream&, complex<long double>&);
1474 extern template ostream& operator<<(ostream&, const complex<long double>&);
1476 #ifdef _GLIBCXX_USE_WCHAR_T
1477 extern template wistream& operator>>(wistream&, complex<float>&);
1478 extern template wostream& operator<<(wostream&, const complex<float>&);
1479 extern template wistream& operator>>(wistream&, complex<double>&);
1480 extern template wostream& operator<<(wostream&, const complex<double>&);
1481 extern template wistream& operator>>(wistream&, complex<long double>&);
1482 extern template wostream& operator<<(wostream&, const complex<long double>&);
1486 _GLIBCXX_END_NAMESPACE
1488 _GLIBCXX_BEGIN_NAMESPACE(__gnu_cxx)
1490 // See ext/type_traits.h for the primary template.
1491 template<typename _Tp, typename _Up>
1492 struct __promote_2<std::complex<_Tp>, _Up>
1495 typedef std::complex<typename __promote_2<_Tp, _Up>::__type> __type;
1498 template<typename _Tp, typename _Up>
1499 struct __promote_2<_Tp, std::complex<_Up> >
1502 typedef std::complex<typename __promote_2<_Tp, _Up>::__type> __type;
1505 template<typename _Tp, typename _Up>
1506 struct __promote_2<std::complex<_Tp>, std::complex<_Up> >
1509 typedef std::complex<typename __promote_2<_Tp, _Up>::__type> __type;
1512 _GLIBCXX_END_NAMESPACE
1514 #ifdef __GXX_EXPERIMENTAL_CXX0X__
1515 # if defined(_GLIBCXX_INCLUDE_AS_TR1)
1516 # error C++0x header cannot be included from TR1 header
1518 # if defined(_GLIBCXX_INCLUDE_AS_CXX0X)
1519 # include <tr1_impl/complex>
1521 # define _GLIBCXX_INCLUDE_AS_CXX0X
1522 # define _GLIBCXX_BEGIN_NAMESPACE_TR1
1523 # define _GLIBCXX_END_NAMESPACE_TR1
1524 # define _GLIBCXX_TR1
1525 # include <tr1_impl/complex>
1526 # undef _GLIBCXX_TR1
1527 # undef _GLIBCXX_END_NAMESPACE_TR1
1528 # undef _GLIBCXX_BEGIN_NAMESPACE_TR1
1529 # undef _GLIBCXX_INCLUDE_AS_CXX0X
1532 _GLIBCXX_BEGIN_NAMESPACE(std)
1534 // Forward declarations.
1536 template<typename _Tp> std::complex<_Tp> proj(const std::complex<_Tp>&);
1538 template<typename _Tp>
1540 __complex_proj(const std::complex<_Tp>& __z)
1542 const _Tp __den = (__z.real() * __z.real()
1543 + __z.imag() * __z.imag() + _Tp(1.0));
1545 return std::complex<_Tp>((_Tp(2.0) * __z.real()) / __den,
1546 (_Tp(2.0) * __z.imag()) / __den);
1549 #if _GLIBCXX_USE_C99_COMPLEX
1550 inline __complex__ float
1551 __complex_proj(__complex__ float __z)
1552 { return __builtin_cprojf(__z); }
1554 inline __complex__ double
1555 __complex_proj(__complex__ double __z)
1556 { return __builtin_cproj(__z); }
1558 inline __complex__ long double
1559 __complex_proj(const __complex__ long double& __z)
1560 { return __builtin_cprojl(__z); }
1562 template<typename _Tp>
1563 inline std::complex<_Tp>
1564 proj(const std::complex<_Tp>& __z)
1565 { return __complex_proj(__z.__rep()); }
1567 template<typename _Tp>
1568 inline std::complex<_Tp>
1569 proj(const std::complex<_Tp>& __z)
1570 { return __complex_proj(__z); }
1573 template<typename _Tp>
1574 inline std::complex<typename __gnu_cxx::__promote<_Tp>::__type>
1577 typedef typename __gnu_cxx::__promote<_Tp>::__type __type;
1578 return std::proj(std::complex<__type>(__x));
1581 _GLIBCXX_END_NAMESPACE
1585 #endif /* _GLIBCXX_COMPLEX */