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>
53 _GLIBCXX_BEGIN_NAMESPACE(std)
55 // Forward declarations.
56 template<typename _Tp> class complex;
57 template<> class complex<float>;
58 template<> class complex<double>;
59 template<> class complex<long double>;
61 /// Return magnitude of @a z.
62 template<typename _Tp> _Tp abs(const complex<_Tp>&);
63 /// Return phase angle of @a z.
64 template<typename _Tp> _Tp arg(const complex<_Tp>&);
65 /// Return @a z magnitude squared.
66 template<typename _Tp> _Tp norm(const complex<_Tp>&);
68 /// Return complex conjugate of @a z.
69 template<typename _Tp> complex<_Tp> conj(const complex<_Tp>&);
70 /// Return complex with magnitude @a rho and angle @a theta.
71 template<typename _Tp> complex<_Tp> polar(const _Tp&, const _Tp& = 0);
74 /// Return complex cosine of @a z.
75 template<typename _Tp> complex<_Tp> cos(const complex<_Tp>&);
76 /// Return complex hyperbolic cosine of @a z.
77 template<typename _Tp> complex<_Tp> cosh(const complex<_Tp>&);
78 /// Return complex base e exponential of @a z.
79 template<typename _Tp> complex<_Tp> exp(const complex<_Tp>&);
80 /// Return complex natural logarithm of @a z.
81 template<typename _Tp> complex<_Tp> log(const complex<_Tp>&);
82 /// Return complex base 10 logarithm of @a z.
83 template<typename _Tp> complex<_Tp> log10(const complex<_Tp>&);
84 /// Return complex cosine of @a z.
85 template<typename _Tp> complex<_Tp> pow(const complex<_Tp>&, int);
86 /// Return @a x to the @a y'th power.
87 template<typename _Tp> complex<_Tp> pow(const complex<_Tp>&, const _Tp&);
88 /// Return @a x to the @a y'th power.
89 template<typename _Tp> complex<_Tp> pow(const complex<_Tp>&,
91 /// Return @a x to the @a y'th power.
92 template<typename _Tp> complex<_Tp> pow(const _Tp&, const complex<_Tp>&);
93 /// Return complex sine of @a z.
94 template<typename _Tp> complex<_Tp> sin(const complex<_Tp>&);
95 /// Return complex hyperbolic sine of @a z.
96 template<typename _Tp> complex<_Tp> sinh(const complex<_Tp>&);
97 /// Return complex square root of @a z.
98 template<typename _Tp> complex<_Tp> sqrt(const complex<_Tp>&);
99 /// Return complex tangent of @a z.
100 template<typename _Tp> complex<_Tp> tan(const complex<_Tp>&);
101 /// Return complex hyperbolic tangent of @a z.
102 template<typename _Tp> complex<_Tp> tanh(const complex<_Tp>&);
106 // 26.2.2 Primary template class complex
108 * Template to represent complex numbers.
110 * Specializations for float, double, and long double are part of the
111 * library. Results with any other type are not guaranteed.
113 * @param Tp Type of real and imaginary values.
115 template<typename _Tp>
119 typedef _Tp value_type;
121 /// Default constructor. First parameter is x, second parameter is y.
122 /// Unspecified parameters default to 0.
123 complex(const _Tp& = _Tp(), const _Tp & = _Tp());
125 // Lets the compiler synthesize the copy constructor
126 // complex (const complex<_Tp>&);
127 /// Copy constructor.
128 template<typename _Up>
129 complex(const complex<_Up>&);
131 /// Return real part of complex number.
133 /// Return real part of complex number.
134 const _Tp& real() const;
135 /// Return imaginary part of complex number.
137 /// Return imaginary part of complex number.
138 const _Tp& imag() const;
140 /// Assign this complex number to scalar @a t.
141 complex<_Tp>& operator=(const _Tp&);
142 /// Add @a t to this complex number.
143 complex<_Tp>& operator+=(const _Tp&);
144 /// Subtract @a t from this complex number.
145 complex<_Tp>& operator-=(const _Tp&);
146 /// Multiply this complex number by @a t.
147 complex<_Tp>& operator*=(const _Tp&);
148 /// Divide this complex number by @a t.
149 complex<_Tp>& operator/=(const _Tp&);
151 // Lets the compiler synthesize the
152 // copy and assignment operator
153 // complex<_Tp>& operator= (const complex<_Tp>&);
154 /// Assign this complex number to complex @a z.
155 template<typename _Up>
156 complex<_Tp>& operator=(const complex<_Up>&);
157 /// Add @a z to this complex number.
158 template<typename _Up>
159 complex<_Tp>& operator+=(const complex<_Up>&);
160 /// Subtract @a z from this complex number.
161 template<typename _Up>
162 complex<_Tp>& operator-=(const complex<_Up>&);
163 /// Multiply this complex number by @a z.
164 template<typename _Up>
165 complex<_Tp>& operator*=(const complex<_Up>&);
166 /// Divide this complex number by @a z.
167 template<typename _Up>
168 complex<_Tp>& operator/=(const complex<_Up>&);
170 const complex& __rep() const;
177 template<typename _Tp>
179 complex<_Tp>::real() { return _M_real; }
181 template<typename _Tp>
183 complex<_Tp>::real() const { return _M_real; }
185 template<typename _Tp>
187 complex<_Tp>::imag() { return _M_imag; }
189 template<typename _Tp>
191 complex<_Tp>::imag() const { return _M_imag; }
193 template<typename _Tp>
195 complex<_Tp>::complex(const _Tp& __r, const _Tp& __i)
196 : _M_real(__r), _M_imag(__i) { }
198 template<typename _Tp>
199 template<typename _Up>
201 complex<_Tp>::complex(const complex<_Up>& __z)
202 : _M_real(__z.real()), _M_imag(__z.imag()) { }
204 template<typename _Tp>
206 complex<_Tp>::operator=(const _Tp& __t)
214 template<typename _Tp>
216 complex<_Tp>::operator+=(const _Tp& __t)
223 template<typename _Tp>
225 complex<_Tp>::operator-=(const _Tp& __t)
232 template<typename _Tp>
234 complex<_Tp>::operator*=(const _Tp& __t)
242 template<typename _Tp>
244 complex<_Tp>::operator/=(const _Tp& __t)
251 template<typename _Tp>
252 template<typename _Up>
254 complex<_Tp>::operator=(const complex<_Up>& __z)
256 _M_real = __z.real();
257 _M_imag = __z.imag();
262 template<typename _Tp>
263 template<typename _Up>
265 complex<_Tp>::operator+=(const complex<_Up>& __z)
267 _M_real += __z.real();
268 _M_imag += __z.imag();
273 template<typename _Tp>
274 template<typename _Up>
276 complex<_Tp>::operator-=(const complex<_Up>& __z)
278 _M_real -= __z.real();
279 _M_imag -= __z.imag();
284 // XXX: This is a grammar school implementation.
285 template<typename _Tp>
286 template<typename _Up>
288 complex<_Tp>::operator*=(const complex<_Up>& __z)
290 const _Tp __r = _M_real * __z.real() - _M_imag * __z.imag();
291 _M_imag = _M_real * __z.imag() + _M_imag * __z.real();
297 // XXX: This is a grammar school implementation.
298 template<typename _Tp>
299 template<typename _Up>
301 complex<_Tp>::operator/=(const complex<_Up>& __z)
303 const _Tp __r = _M_real * __z.real() + _M_imag * __z.imag();
304 const _Tp __n = std::norm(__z);
305 _M_imag = (_M_imag * __z.real() - _M_real * __z.imag()) / __n;
310 template<typename _Tp>
311 inline const complex<_Tp>&
312 complex<_Tp>::__rep() const { return *this; }
316 /// Return new complex value @a x plus @a y.
317 template<typename _Tp>
319 operator+(const complex<_Tp>& __x, const complex<_Tp>& __y)
321 complex<_Tp> __r = __x;
326 template<typename _Tp>
328 operator+(const complex<_Tp>& __x, const _Tp& __y)
330 complex<_Tp> __r = __x;
335 template<typename _Tp>
337 operator+(const _Tp& __x, const complex<_Tp>& __y)
339 complex<_Tp> __r = __y;
346 /// Return new complex value @a x minus @a y.
347 template<typename _Tp>
349 operator-(const complex<_Tp>& __x, const complex<_Tp>& __y)
351 complex<_Tp> __r = __x;
356 template<typename _Tp>
358 operator-(const complex<_Tp>& __x, const _Tp& __y)
360 complex<_Tp> __r = __x;
365 template<typename _Tp>
367 operator-(const _Tp& __x, const complex<_Tp>& __y)
369 complex<_Tp> __r(__x, -__y.imag());
370 __r.real() -= __y.real();
376 /// Return new complex value @a x times @a y.
377 template<typename _Tp>
379 operator*(const complex<_Tp>& __x, const complex<_Tp>& __y)
381 complex<_Tp> __r = __x;
386 template<typename _Tp>
388 operator*(const complex<_Tp>& __x, const _Tp& __y)
390 complex<_Tp> __r = __x;
395 template<typename _Tp>
397 operator*(const _Tp& __x, const complex<_Tp>& __y)
399 complex<_Tp> __r = __y;
406 /// Return new complex value @a x divided by @a y.
407 template<typename _Tp>
409 operator/(const complex<_Tp>& __x, const complex<_Tp>& __y)
411 complex<_Tp> __r = __x;
416 template<typename _Tp>
418 operator/(const complex<_Tp>& __x, const _Tp& __y)
420 complex<_Tp> __r = __x;
425 template<typename _Tp>
427 operator/(const _Tp& __x, const complex<_Tp>& __y)
429 complex<_Tp> __r = __x;
436 template<typename _Tp>
438 operator+(const complex<_Tp>& __x)
441 /// Return complex negation of @a x.
442 template<typename _Tp>
444 operator-(const complex<_Tp>& __x)
445 { return complex<_Tp>(-__x.real(), -__x.imag()); }
448 /// Return true if @a x is equal to @a y.
449 template<typename _Tp>
451 operator==(const complex<_Tp>& __x, const complex<_Tp>& __y)
452 { return __x.real() == __y.real() && __x.imag() == __y.imag(); }
454 template<typename _Tp>
456 operator==(const complex<_Tp>& __x, const _Tp& __y)
457 { return __x.real() == __y && __x.imag() == _Tp(); }
459 template<typename _Tp>
461 operator==(const _Tp& __x, const complex<_Tp>& __y)
462 { return __x == __y.real() && _Tp() == __y.imag(); }
466 /// Return false if @a x is equal to @a y.
467 template<typename _Tp>
469 operator!=(const complex<_Tp>& __x, const complex<_Tp>& __y)
470 { return __x.real() != __y.real() || __x.imag() != __y.imag(); }
472 template<typename _Tp>
474 operator!=(const complex<_Tp>& __x, const _Tp& __y)
475 { return __x.real() != __y || __x.imag() != _Tp(); }
477 template<typename _Tp>
479 operator!=(const _Tp& __x, const complex<_Tp>& __y)
480 { return __x != __y.real() || _Tp() != __y.imag(); }
483 /// Extraction operator for complex values.
484 template<typename _Tp, typename _CharT, class _Traits>
485 basic_istream<_CharT, _Traits>&
486 operator>>(basic_istream<_CharT, _Traits>& __is, complex<_Tp>& __x)
493 __is >> __re_x >> __ch;
496 __is >> __im_x >> __ch;
498 __x = complex<_Tp>(__re_x, __im_x);
500 __is.setstate(ios_base::failbit);
502 else if (__ch == ')')
505 __is.setstate(ios_base::failbit);
516 /// Insertion operator for complex values.
517 template<typename _Tp, typename _CharT, class _Traits>
518 basic_ostream<_CharT, _Traits>&
519 operator<<(basic_ostream<_CharT, _Traits>& __os, const complex<_Tp>& __x)
521 basic_ostringstream<_CharT, _Traits> __s;
522 __s.flags(__os.flags());
523 __s.imbue(__os.getloc());
524 __s.precision(__os.precision());
525 __s << '(' << __x.real() << ',' << __x.imag() << ')';
526 return __os << __s.str();
530 template<typename _Tp>
532 real(complex<_Tp>& __z)
533 { return __z.real(); }
535 template<typename _Tp>
537 real(const complex<_Tp>& __z)
538 { return __z.real(); }
540 template<typename _Tp>
542 imag(complex<_Tp>& __z)
543 { return __z.imag(); }
545 template<typename _Tp>
547 imag(const complex<_Tp>& __z)
548 { return __z.imag(); }
550 // 26.2.7/3 abs(__z): Returns the magnitude of __z.
551 template<typename _Tp>
553 __complex_abs(const complex<_Tp>& __z)
555 _Tp __x = __z.real();
556 _Tp __y = __z.imag();
557 const _Tp __s = std::max(abs(__x), abs(__y));
558 if (__s == _Tp()) // well ...
562 return __s * sqrt(__x * __x + __y * __y);
565 #if _GLIBCXX_USE_C99_COMPLEX
567 __complex_abs(__complex__ float __z) { return __builtin_cabsf(__z); }
570 __complex_abs(__complex__ double __z) { return __builtin_cabs(__z); }
573 __complex_abs(const __complex__ long double& __z)
574 { return __builtin_cabsl(__z); }
576 template<typename _Tp>
578 abs(const complex<_Tp>& __z) { return __complex_abs(__z.__rep()); }
580 template<typename _Tp>
582 abs(const complex<_Tp>& __z) { return __complex_abs(__z); }
586 // 26.2.7/4: arg(__z): Returns the phase angle of __z.
587 template<typename _Tp>
589 __complex_arg(const complex<_Tp>& __z)
590 { return atan2(__z.imag(), __z.real()); }
592 #if _GLIBCXX_USE_C99_COMPLEX
594 __complex_arg(__complex__ float __z) { return __builtin_cargf(__z); }
597 __complex_arg(__complex__ double __z) { return __builtin_carg(__z); }
600 __complex_arg(const __complex__ long double& __z)
601 { return __builtin_cargl(__z); }
603 template<typename _Tp>
605 arg(const complex<_Tp>& __z) { return __complex_arg(__z.__rep()); }
607 template<typename _Tp>
609 arg(const complex<_Tp>& __z) { return __complex_arg(__z); }
612 // 26.2.7/5: norm(__z) returns the squared magintude of __z.
613 // As defined, norm() is -not- a norm is the common mathematical
614 // sens used in numerics. The helper class _Norm_helper<> tries to
615 // distinguish between builtin floating point and the rest, so as
616 // to deliver an answer as close as possible to the real value.
620 template<typename _Tp>
621 static inline _Tp _S_do_it(const complex<_Tp>& __z)
623 const _Tp __x = __z.real();
624 const _Tp __y = __z.imag();
625 return __x * __x + __y * __y;
630 struct _Norm_helper<true>
632 template<typename _Tp>
633 static inline _Tp _S_do_it(const complex<_Tp>& __z)
635 _Tp __res = std::abs(__z);
636 return __res * __res;
640 template<typename _Tp>
642 norm(const complex<_Tp>& __z)
644 return _Norm_helper<__is_floating<_Tp>::__value
645 && !_GLIBCXX_FAST_MATH>::_S_do_it(__z);
648 template<typename _Tp>
650 polar(const _Tp& __rho, const _Tp& __theta)
651 { return complex<_Tp>(__rho * cos(__theta), __rho * sin(__theta)); }
653 template<typename _Tp>
655 conj(const complex<_Tp>& __z)
656 { return complex<_Tp>(__z.real(), -__z.imag()); }
660 // 26.2.8/1 cos(__z): Returns the cosine of __z.
661 template<typename _Tp>
663 __complex_cos(const complex<_Tp>& __z)
665 const _Tp __x = __z.real();
666 const _Tp __y = __z.imag();
667 return complex<_Tp>(cos(__x) * cosh(__y), -sin(__x) * sinh(__y));
670 #if _GLIBCXX_USE_C99_COMPLEX
671 inline __complex__ float
672 __complex_cos(__complex__ float __z) { return __builtin_ccosf(__z); }
674 inline __complex__ double
675 __complex_cos(__complex__ double __z) { return __builtin_ccos(__z); }
677 inline __complex__ long double
678 __complex_cos(const __complex__ long double& __z)
679 { return __builtin_ccosl(__z); }
681 template<typename _Tp>
683 cos(const complex<_Tp>& __z) { return __complex_cos(__z.__rep()); }
685 template<typename _Tp>
687 cos(const complex<_Tp>& __z) { return __complex_cos(__z); }
690 // 26.2.8/2 cosh(__z): Returns the hyperbolic cosine of __z.
691 template<typename _Tp>
693 __complex_cosh(const complex<_Tp>& __z)
695 const _Tp __x = __z.real();
696 const _Tp __y = __z.imag();
697 return complex<_Tp>(cosh(__x) * cos(__y), sinh(__x) * sin(__y));
700 #if _GLIBCXX_USE_C99_COMPLEX
701 inline __complex__ float
702 __complex_cosh(__complex__ float __z) { return __builtin_ccoshf(__z); }
704 inline __complex__ double
705 __complex_cosh(__complex__ double __z) { return __builtin_ccosh(__z); }
707 inline __complex__ long double
708 __complex_cosh(const __complex__ long double& __z)
709 { return __builtin_ccoshl(__z); }
711 template<typename _Tp>
713 cosh(const complex<_Tp>& __z) { return __complex_cosh(__z.__rep()); }
715 template<typename _Tp>
717 cosh(const complex<_Tp>& __z) { return __complex_cosh(__z); }
720 // 26.2.8/3 exp(__z): Returns the complex base e exponential of x
721 template<typename _Tp>
723 __complex_exp(const complex<_Tp>& __z)
724 { return std::polar(exp(__z.real()), __z.imag()); }
726 #if _GLIBCXX_USE_C99_COMPLEX
727 inline __complex__ float
728 __complex_exp(__complex__ float __z) { return __builtin_cexpf(__z); }
730 inline __complex__ double
731 __complex_exp(__complex__ double __z) { return __builtin_cexp(__z); }
733 inline __complex__ long double
734 __complex_exp(const __complex__ long double& __z)
735 { return __builtin_cexpl(__z); }
737 template<typename _Tp>
739 exp(const complex<_Tp>& __z) { return __complex_exp(__z.__rep()); }
741 template<typename _Tp>
743 exp(const complex<_Tp>& __z) { return __complex_exp(__z); }
746 // 26.2.8/5 log(__z): Reurns the natural complex logaritm of __z.
747 // The branch cut is along the negative axis.
748 template<typename _Tp>
750 __complex_log(const complex<_Tp>& __z)
751 { return complex<_Tp>(log(std::abs(__z)), std::arg(__z)); }
753 #if _GLIBCXX_USE_C99_COMPLEX
754 inline __complex__ float
755 __complex_log(__complex__ float __z) { return __builtin_clogf(__z); }
757 inline __complex__ double
758 __complex_log(__complex__ double __z) { return __builtin_clog(__z); }
760 inline __complex__ long double
761 __complex_log(const __complex__ long double& __z)
762 { return __builtin_clogl(__z); }
764 template<typename _Tp>
766 log(const complex<_Tp>& __z) { return __complex_log(__z.__rep()); }
768 template<typename _Tp>
770 log(const complex<_Tp>& __z) { return __complex_log(__z); }
773 template<typename _Tp>
775 log10(const complex<_Tp>& __z)
776 { return std::log(__z) / log(_Tp(10.0)); }
778 // 26.2.8/10 sin(__z): Returns the sine of __z.
779 template<typename _Tp>
781 __complex_sin(const complex<_Tp>& __z)
783 const _Tp __x = __z.real();
784 const _Tp __y = __z.imag();
785 return complex<_Tp>(sin(__x) * cosh(__y), cos(__x) * sinh(__y));
788 #if _GLIBCXX_USE_C99_COMPLEX
789 inline __complex__ float
790 __complex_sin(__complex__ float __z) { return __builtin_csinf(__z); }
792 inline __complex__ double
793 __complex_sin(__complex__ double __z) { return __builtin_csin(__z); }
795 inline __complex__ long double
796 __complex_sin(const __complex__ long double& __z)
797 { return __builtin_csinl(__z); }
799 template<typename _Tp>
801 sin(const complex<_Tp>& __z) { return __complex_sin(__z.__rep()); }
803 template<typename _Tp>
805 sin(const complex<_Tp>& __z) { return __complex_sin(__z); }
808 // 26.2.8/11 sinh(__z): Returns the hyperbolic sine of __z.
809 template<typename _Tp>
811 __complex_sinh(const complex<_Tp>& __z)
813 const _Tp __x = __z.real();
814 const _Tp __y = __z.imag();
815 return complex<_Tp>(sinh(__x) * cos(__y), cosh(__x) * sin(__y));
818 #if _GLIBCXX_USE_C99_COMPLEX
819 inline __complex__ float
820 __complex_sinh(__complex__ float __z) { return __builtin_csinhf(__z); }
822 inline __complex__ double
823 __complex_sinh(__complex__ double __z) { return __builtin_csinh(__z); }
825 inline __complex__ long double
826 __complex_sinh(const __complex__ long double& __z)
827 { return __builtin_csinhl(__z); }
829 template<typename _Tp>
831 sinh(const complex<_Tp>& __z) { return __complex_sinh(__z.__rep()); }
833 template<typename _Tp>
835 sinh(const complex<_Tp>& __z) { return __complex_sinh(__z); }
838 // 26.2.8/13 sqrt(__z): Returns the complex square root of __z.
839 // The branch cut is on the negative axis.
840 template<typename _Tp>
842 __complex_sqrt(const complex<_Tp>& __z)
844 _Tp __x = __z.real();
845 _Tp __y = __z.imag();
849 _Tp __t = sqrt(abs(__y) / 2);
850 return complex<_Tp>(__t, __y < _Tp() ? -__t : __t);
854 _Tp __t = sqrt(2 * (std::abs(__z) + abs(__x)));
857 ? complex<_Tp>(__u, __y / __t)
858 : complex<_Tp>(abs(__y) / __t, __y < _Tp() ? -__u : __u);
862 #if _GLIBCXX_USE_C99_COMPLEX
863 inline __complex__ float
864 __complex_sqrt(__complex__ float __z) { return __builtin_csqrtf(__z); }
866 inline __complex__ double
867 __complex_sqrt(__complex__ double __z) { return __builtin_csqrt(__z); }
869 inline __complex__ long double
870 __complex_sqrt(const __complex__ long double& __z)
871 { return __builtin_csqrtl(__z); }
873 template<typename _Tp>
875 sqrt(const complex<_Tp>& __z) { return __complex_sqrt(__z.__rep()); }
877 template<typename _Tp>
879 sqrt(const complex<_Tp>& __z) { return __complex_sqrt(__z); }
882 // 26.2.8/14 tan(__z): Return the complex tangent of __z.
884 template<typename _Tp>
886 __complex_tan(const complex<_Tp>& __z)
887 { return std::sin(__z) / std::cos(__z); }
889 #if _GLIBCXX_USE_C99_COMPLEX
890 inline __complex__ float
891 __complex_tan(__complex__ float __z) { return __builtin_ctanf(__z); }
893 inline __complex__ double
894 __complex_tan(__complex__ double __z) { return __builtin_ctan(__z); }
896 inline __complex__ long double
897 __complex_tan(const __complex__ long double& __z)
898 { return __builtin_ctanl(__z); }
900 template<typename _Tp>
902 tan(const complex<_Tp>& __z) { return __complex_tan(__z.__rep()); }
904 template<typename _Tp>
906 tan(const complex<_Tp>& __z) { return __complex_tan(__z); }
910 // 26.2.8/15 tanh(__z): Returns the hyperbolic tangent of __z.
912 template<typename _Tp>
914 __complex_tanh(const complex<_Tp>& __z)
915 { return std::sinh(__z) / std::cosh(__z); }
917 #if _GLIBCXX_USE_C99_COMPLEX
918 inline __complex__ float
919 __complex_tanh(__complex__ float __z) { return __builtin_ctanhf(__z); }
921 inline __complex__ double
922 __complex_tanh(__complex__ double __z) { return __builtin_ctanh(__z); }
924 inline __complex__ long double
925 __complex_tanh(const __complex__ long double& __z)
926 { return __builtin_ctanhl(__z); }
928 template<typename _Tp>
930 tanh(const complex<_Tp>& __z) { return __complex_tanh(__z.__rep()); }
932 template<typename _Tp>
934 tanh(const complex<_Tp>& __z) { return __complex_tanh(__z); }
938 // 26.2.8/9 pow(__x, __y): Returns the complex power base of __x
939 // raised to the __y-th power. The branch
940 // cut is on the negative axis.
941 template<typename _Tp>
943 pow(const complex<_Tp>& __z, int __n)
944 { return std::__pow_helper(__z, __n); }
946 template<typename _Tp>
948 pow(const complex<_Tp>& __x, const _Tp& __y)
950 #ifndef _GLIBCXX_USE_C99_COMPLEX
954 if (__x.imag() == _Tp() && __x.real() > _Tp())
955 return pow(__x.real(), __y);
957 complex<_Tp> __t = std::log(__x);
958 return std::polar(exp(__y * __t.real()), __y * __t.imag());
961 template<typename _Tp>
963 __complex_pow(const complex<_Tp>& __x, const complex<_Tp>& __y)
964 { return __x == _Tp() ? _Tp() : std::exp(__y * std::log(__x)); }
966 #if _GLIBCXX_USE_C99_COMPLEX
967 inline __complex__ float
968 __complex_pow(__complex__ float __x, __complex__ float __y)
969 { return __builtin_cpowf(__x, __y); }
971 inline __complex__ double
972 __complex_pow(__complex__ double __x, __complex__ double __y)
973 { return __builtin_cpow(__x, __y); }
975 inline __complex__ long double
976 __complex_pow(const __complex__ long double& __x,
977 const __complex__ long double& __y)
978 { return __builtin_cpowl(__x, __y); }
980 template<typename _Tp>
982 pow(const complex<_Tp>& __x, const complex<_Tp>& __y)
983 { return __complex_pow(__x.__rep(), __y.__rep()); }
985 template<typename _Tp>
987 pow(const complex<_Tp>& __x, const complex<_Tp>& __y)
988 { return __complex_pow(__x, __y); }
991 template<typename _Tp>
993 pow(const _Tp& __x, const complex<_Tp>& __y)
995 return __x > _Tp() ? std::polar(pow(__x, __y.real()),
996 __y.imag() * log(__x))
997 : std::pow(complex<_Tp>(__x, _Tp()), __y);
1000 // 26.2.3 complex specializations
1001 // complex<float> specialization
1003 struct complex<float>
1005 typedef float value_type;
1006 typedef __complex__ float _ComplexT;
1008 complex(_ComplexT __z) : _M_value(__z) { }
1010 complex(float = 0.0f, float = 0.0f);
1012 explicit complex(const complex<double>&);
1013 explicit complex(const complex<long double>&);
1016 const float& real() const;
1018 const float& imag() const;
1020 complex<float>& operator=(float);
1021 complex<float>& operator+=(float);
1022 complex<float>& operator-=(float);
1023 complex<float>& operator*=(float);
1024 complex<float>& operator/=(float);
1026 // Let's the compiler synthetize the copy and assignment
1027 // operator. It always does a pretty good job.
1028 // complex& operator= (const complex&);
1029 template<typename _Tp>
1030 complex<float>&operator=(const complex<_Tp>&);
1031 template<typename _Tp>
1032 complex<float>& operator+=(const complex<_Tp>&);
1034 complex<float>& operator-=(const complex<_Tp>&);
1036 complex<float>& operator*=(const complex<_Tp>&);
1038 complex<float>&operator/=(const complex<_Tp>&);
1040 const _ComplexT& __rep() const { return _M_value; }
1047 complex<float>::real()
1048 { return __real__ _M_value; }
1051 complex<float>::real() const
1052 { return __real__ _M_value; }
1055 complex<float>::imag()
1056 { return __imag__ _M_value; }
1059 complex<float>::imag() const
1060 { return __imag__ _M_value; }
1063 complex<float>::complex(float r, float i)
1065 __real__ _M_value = r;
1066 __imag__ _M_value = i;
1069 inline complex<float>&
1070 complex<float>::operator=(float __f)
1072 __real__ _M_value = __f;
1073 __imag__ _M_value = 0.0f;
1077 inline complex<float>&
1078 complex<float>::operator+=(float __f)
1080 __real__ _M_value += __f;
1084 inline complex<float>&
1085 complex<float>::operator-=(float __f)
1087 __real__ _M_value -= __f;
1091 inline complex<float>&
1092 complex<float>::operator*=(float __f)
1098 inline complex<float>&
1099 complex<float>::operator/=(float __f)
1105 template<typename _Tp>
1106 inline complex<float>&
1107 complex<float>::operator=(const complex<_Tp>& __z)
1109 __real__ _M_value = __z.real();
1110 __imag__ _M_value = __z.imag();
1114 template<typename _Tp>
1115 inline complex<float>&
1116 complex<float>::operator+=(const complex<_Tp>& __z)
1118 __real__ _M_value += __z.real();
1119 __imag__ _M_value += __z.imag();
1123 template<typename _Tp>
1124 inline complex<float>&
1125 complex<float>::operator-=(const complex<_Tp>& __z)
1127 __real__ _M_value -= __z.real();
1128 __imag__ _M_value -= __z.imag();
1132 template<typename _Tp>
1133 inline complex<float>&
1134 complex<float>::operator*=(const complex<_Tp>& __z)
1137 __real__ __t = __z.real();
1138 __imag__ __t = __z.imag();
1143 template<typename _Tp>
1144 inline complex<float>&
1145 complex<float>::operator/=(const complex<_Tp>& __z)
1148 __real__ __t = __z.real();
1149 __imag__ __t = __z.imag();
1154 // 26.2.3 complex specializations
1155 // complex<double> specialization
1157 struct complex<double>
1159 typedef double value_type;
1160 typedef __complex__ double _ComplexT;
1162 complex(_ComplexT __z) : _M_value(__z) { }
1164 complex(double = 0.0, double = 0.0);
1166 complex(const complex<float>&);
1167 explicit complex(const complex<long double>&);
1170 const double& real() const;
1172 const double& imag() const;
1174 complex<double>& operator=(double);
1175 complex<double>& operator+=(double);
1176 complex<double>& operator-=(double);
1177 complex<double>& operator*=(double);
1178 complex<double>& operator/=(double);
1180 // The compiler will synthetize this, efficiently.
1181 // complex& operator= (const complex&);
1182 template<typename _Tp>
1183 complex<double>& operator=(const complex<_Tp>&);
1184 template<typename _Tp>
1185 complex<double>& operator+=(const complex<_Tp>&);
1186 template<typename _Tp>
1187 complex<double>& operator-=(const complex<_Tp>&);
1188 template<typename _Tp>
1189 complex<double>& operator*=(const complex<_Tp>&);
1190 template<typename _Tp>
1191 complex<double>& operator/=(const complex<_Tp>&);
1193 const _ComplexT& __rep() const { return _M_value; }
1200 complex<double>::real()
1201 { return __real__ _M_value; }
1203 inline const double&
1204 complex<double>::real() const
1205 { return __real__ _M_value; }
1208 complex<double>::imag()
1209 { return __imag__ _M_value; }
1211 inline const double&
1212 complex<double>::imag() const
1213 { return __imag__ _M_value; }
1216 complex<double>::complex(double __r, double __i)
1218 __real__ _M_value = __r;
1219 __imag__ _M_value = __i;
1222 inline complex<double>&
1223 complex<double>::operator=(double __d)
1225 __real__ _M_value = __d;
1226 __imag__ _M_value = 0.0;
1230 inline complex<double>&
1231 complex<double>::operator+=(double __d)
1233 __real__ _M_value += __d;
1237 inline complex<double>&
1238 complex<double>::operator-=(double __d)
1240 __real__ _M_value -= __d;
1244 inline complex<double>&
1245 complex<double>::operator*=(double __d)
1251 inline complex<double>&
1252 complex<double>::operator/=(double __d)
1258 template<typename _Tp>
1259 inline complex<double>&
1260 complex<double>::operator=(const complex<_Tp>& __z)
1262 __real__ _M_value = __z.real();
1263 __imag__ _M_value = __z.imag();
1267 template<typename _Tp>
1268 inline complex<double>&
1269 complex<double>::operator+=(const complex<_Tp>& __z)
1271 __real__ _M_value += __z.real();
1272 __imag__ _M_value += __z.imag();
1276 template<typename _Tp>
1277 inline complex<double>&
1278 complex<double>::operator-=(const complex<_Tp>& __z)
1280 __real__ _M_value -= __z.real();
1281 __imag__ _M_value -= __z.imag();
1285 template<typename _Tp>
1286 inline complex<double>&
1287 complex<double>::operator*=(const complex<_Tp>& __z)
1290 __real__ __t = __z.real();
1291 __imag__ __t = __z.imag();
1296 template<typename _Tp>
1297 inline complex<double>&
1298 complex<double>::operator/=(const complex<_Tp>& __z)
1301 __real__ __t = __z.real();
1302 __imag__ __t = __z.imag();
1307 // 26.2.3 complex specializations
1308 // complex<long double> specialization
1310 struct complex<long double>
1312 typedef long double value_type;
1313 typedef __complex__ long double _ComplexT;
1315 complex(_ComplexT __z) : _M_value(__z) { }
1317 complex(long double = 0.0L, long double = 0.0L);
1319 complex(const complex<float>&);
1320 complex(const complex<double>&);
1322 long double& real();
1323 const long double& real() const;
1324 long double& imag();
1325 const long double& imag() const;
1327 complex<long double>& operator= (long double);
1328 complex<long double>& operator+= (long double);
1329 complex<long double>& operator-= (long double);
1330 complex<long double>& operator*= (long double);
1331 complex<long double>& operator/= (long double);
1333 // The compiler knows how to do this efficiently
1334 // complex& operator= (const complex&);
1335 template<typename _Tp>
1336 complex<long double>& operator=(const complex<_Tp>&);
1337 template<typename _Tp>
1338 complex<long double>& operator+=(const complex<_Tp>&);
1339 template<typename _Tp>
1340 complex<long double>& operator-=(const complex<_Tp>&);
1341 template<typename _Tp>
1342 complex<long double>& operator*=(const complex<_Tp>&);
1343 template<typename _Tp>
1344 complex<long double>& operator/=(const complex<_Tp>&);
1346 const _ComplexT& __rep() const { return _M_value; }
1353 complex<long double>::complex(long double __r, long double __i)
1355 __real__ _M_value = __r;
1356 __imag__ _M_value = __i;
1360 complex<long double>::real()
1361 { return __real__ _M_value; }
1363 inline const long double&
1364 complex<long double>::real() const
1365 { return __real__ _M_value; }
1368 complex<long double>::imag()
1369 { return __imag__ _M_value; }
1371 inline const long double&
1372 complex<long double>::imag() const
1373 { return __imag__ _M_value; }
1375 inline complex<long double>&
1376 complex<long double>::operator=(long double __r)
1378 __real__ _M_value = __r;
1379 __imag__ _M_value = 0.0L;
1383 inline complex<long double>&
1384 complex<long double>::operator+=(long double __r)
1386 __real__ _M_value += __r;
1390 inline complex<long double>&
1391 complex<long double>::operator-=(long double __r)
1393 __real__ _M_value -= __r;
1397 inline complex<long double>&
1398 complex<long double>::operator*=(long double __r)
1404 inline complex<long double>&
1405 complex<long double>::operator/=(long double __r)
1411 template<typename _Tp>
1412 inline complex<long double>&
1413 complex<long double>::operator=(const complex<_Tp>& __z)
1415 __real__ _M_value = __z.real();
1416 __imag__ _M_value = __z.imag();
1420 template<typename _Tp>
1421 inline complex<long double>&
1422 complex<long double>::operator+=(const complex<_Tp>& __z)
1424 __real__ _M_value += __z.real();
1425 __imag__ _M_value += __z.imag();
1429 template<typename _Tp>
1430 inline complex<long double>&
1431 complex<long double>::operator-=(const complex<_Tp>& __z)
1433 __real__ _M_value -= __z.real();
1434 __imag__ _M_value -= __z.imag();
1438 template<typename _Tp>
1439 inline complex<long double>&
1440 complex<long double>::operator*=(const complex<_Tp>& __z)
1443 __real__ __t = __z.real();
1444 __imag__ __t = __z.imag();
1449 template<typename _Tp>
1450 inline complex<long double>&
1451 complex<long double>::operator/=(const complex<_Tp>& __z)
1454 __real__ __t = __z.real();
1455 __imag__ __t = __z.imag();
1460 // These bits have to be at the end of this file, so that the
1461 // specializations have all been defined.
1462 // ??? No, they have to be there because of compiler limitation at
1463 // inlining. It suffices that class specializations be defined.
1465 complex<float>::complex(const complex<double>& __z)
1466 : _M_value(__z.__rep()) { }
1469 complex<float>::complex(const complex<long double>& __z)
1470 : _M_value(__z.__rep()) { }
1473 complex<double>::complex(const complex<float>& __z)
1474 : _M_value(__z.__rep()) { }
1477 complex<double>::complex(const complex<long double>& __z)
1478 : _M_value(__z.__rep()) { }
1481 complex<long double>::complex(const complex<float>& __z)
1482 : _M_value(__z.__rep()) { }
1485 complex<long double>::complex(const complex<double>& __z)
1486 : _M_value(__z.__rep()) { }
1488 // Inhibit implicit instantiations for required instantiations,
1489 // which are defined via explicit instantiations elsewhere.
1490 // NB: This syntax is a GNU extension.
1491 #if _GLIBCXX_EXTERN_TEMPLATE
1492 extern template istream& operator>>(istream&, complex<float>&);
1493 extern template ostream& operator<<(ostream&, const complex<float>&);
1494 extern template istream& operator>>(istream&, complex<double>&);
1495 extern template ostream& operator<<(ostream&, const complex<double>&);
1496 extern template istream& operator>>(istream&, complex<long double>&);
1497 extern template ostream& operator<<(ostream&, const complex<long double>&);
1499 #ifdef _GLIBCXX_USE_WCHAR_T
1500 extern template wistream& operator>>(wistream&, complex<float>&);
1501 extern template wostream& operator<<(wostream&, const complex<float>&);
1502 extern template wistream& operator>>(wistream&, complex<double>&);
1503 extern template wostream& operator<<(wostream&, const complex<double>&);
1504 extern template wistream& operator>>(wistream&, complex<long double>&);
1505 extern template wostream& operator<<(wostream&, const complex<long double>&);
1509 _GLIBCXX_END_NAMESPACE
1511 #ifdef __GXX_EXPERIMENTAL_CXX0X__
1512 # include <tr1/complex>
1515 #endif /* _GLIBCXX_COMPLEX */