1 // The template and inlines for the -*- C++ -*- complex number classes.
3 // Copyright (C) 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005,
5 // Free Software Foundation, Inc.
7 // This file is part of the GNU ISO C++ Library. This library is free
8 // software; you can redistribute it and/or modify it under the
9 // terms of the GNU General Public License as published by the
10 // Free Software Foundation; either version 2, or (at your option)
13 // This library is distributed in the hope that it will be useful,
14 // but WITHOUT ANY WARRANTY; without even the implied warranty of
15 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 // GNU General Public License for more details.
18 // You should have received a copy of the GNU General Public License
19 // along with this library; see the file COPYING. If not, write to
20 // the Free Software Foundation, 51 Franklin Street, Fifth Floor,
21 // Boston, MA 02110-1301, USA.
23 // As a special exception, you may use this file as part of a free software
24 // library without restriction. Specifically, if other files instantiate
25 // templates or use macros or inline functions from this file, or you compile
26 // this file and link it with other files to produce an executable, this
27 // file does not by itself cause the resulting executable to be covered by
28 // the GNU General Public License. This exception does not however
29 // invalidate any other reasons why the executable file might be covered by
30 // the GNU General Public License.
33 * This is a Standard C++ Library header.
37 // ISO C++ 14882: 26.2 Complex Numbers
38 // Note: this is not a conforming implementation.
39 // Initially implemented by Ulrich Drepper <drepper@cygnus.com>
40 // Improved by Gabriel Dos Reis <dosreis@cmla.ens-cachan.fr>
43 #ifndef _GLIBCXX_COMPLEX
44 #define _GLIBCXX_COMPLEX 1
46 #pragma GCC system_header
48 #include <bits/c++config.h>
49 #include <bits/cpp_type_traits.h>
50 #include <ext/type_traits.h>
54 _GLIBCXX_BEGIN_NAMESPACE(std)
56 // Forward declarations.
57 template<typename _Tp> class complex;
58 template<> class complex<float>;
59 template<> class complex<double>;
60 template<> class complex<long double>;
62 /// Return magnitude of @a z.
63 template<typename _Tp> _Tp abs(const complex<_Tp>&);
64 /// Return phase angle of @a z.
65 template<typename _Tp> _Tp arg(const complex<_Tp>&);
66 /// Return @a z magnitude squared.
67 template<typename _Tp> _Tp norm(const complex<_Tp>&);
69 /// Return complex conjugate of @a z.
70 template<typename _Tp> complex<_Tp> conj(const complex<_Tp>&);
71 /// Return complex with magnitude @a rho and angle @a theta.
72 template<typename _Tp> complex<_Tp> polar(const _Tp&, const _Tp& = 0);
75 /// Return complex cosine of @a z.
76 template<typename _Tp> complex<_Tp> cos(const complex<_Tp>&);
77 /// Return complex hyperbolic cosine of @a z.
78 template<typename _Tp> complex<_Tp> cosh(const complex<_Tp>&);
79 /// Return complex base e exponential of @a z.
80 template<typename _Tp> complex<_Tp> exp(const complex<_Tp>&);
81 /// Return complex natural logarithm of @a z.
82 template<typename _Tp> complex<_Tp> log(const complex<_Tp>&);
83 /// Return complex base 10 logarithm of @a z.
84 template<typename _Tp> complex<_Tp> log10(const complex<_Tp>&);
85 /// Return complex cosine of @a z.
86 template<typename _Tp> complex<_Tp> pow(const complex<_Tp>&, int);
87 /// Return @a x to the @a y'th power.
88 template<typename _Tp> complex<_Tp> pow(const complex<_Tp>&, const _Tp&);
89 /// Return @a x to the @a y'th power.
90 template<typename _Tp> complex<_Tp> pow(const complex<_Tp>&,
92 /// Return @a x to the @a y'th power.
93 template<typename _Tp> complex<_Tp> pow(const _Tp&, const complex<_Tp>&);
94 /// Return complex sine of @a z.
95 template<typename _Tp> complex<_Tp> sin(const complex<_Tp>&);
96 /// Return complex hyperbolic sine of @a z.
97 template<typename _Tp> complex<_Tp> sinh(const complex<_Tp>&);
98 /// Return complex square root of @a z.
99 template<typename _Tp> complex<_Tp> sqrt(const complex<_Tp>&);
100 /// Return complex tangent of @a z.
101 template<typename _Tp> complex<_Tp> tan(const complex<_Tp>&);
102 /// Return complex hyperbolic tangent of @a z.
103 template<typename _Tp> complex<_Tp> tanh(const complex<_Tp>&);
107 // 26.2.2 Primary template class complex
109 * Template to represent complex numbers.
111 * Specializations for float, double, and long double are part of the
112 * library. Results with any other type are not guaranteed.
114 * @param Tp Type of real and imaginary values.
116 template<typename _Tp>
120 typedef _Tp value_type;
122 /// Default constructor. First parameter is x, second parameter is y.
123 /// Unspecified parameters default to 0.
124 complex(const _Tp& = _Tp(), const _Tp & = _Tp());
126 // Lets the compiler synthesize the copy constructor
127 // complex (const complex<_Tp>&);
128 /// Copy constructor.
129 template<typename _Up>
130 complex(const complex<_Up>&);
132 /// Return real part of complex number.
134 /// Return real part of complex number.
135 const _Tp& real() const;
136 /// Return imaginary part of complex number.
138 /// Return imaginary part of complex number.
139 const _Tp& imag() const;
141 /// Assign this complex number to scalar @a t.
142 complex<_Tp>& operator=(const _Tp&);
143 /// Add @a t to this complex number.
144 complex<_Tp>& operator+=(const _Tp&);
145 /// Subtract @a t from this complex number.
146 complex<_Tp>& operator-=(const _Tp&);
147 /// Multiply this complex number by @a t.
148 complex<_Tp>& operator*=(const _Tp&);
149 /// Divide this complex number by @a t.
150 complex<_Tp>& operator/=(const _Tp&);
152 // Lets the compiler synthesize the
153 // copy and assignment operator
154 // complex<_Tp>& operator= (const complex<_Tp>&);
155 /// Assign this complex number to complex @a z.
156 template<typename _Up>
157 complex<_Tp>& operator=(const complex<_Up>&);
158 /// Add @a z to this complex number.
159 template<typename _Up>
160 complex<_Tp>& operator+=(const complex<_Up>&);
161 /// Subtract @a z from this complex number.
162 template<typename _Up>
163 complex<_Tp>& operator-=(const complex<_Up>&);
164 /// Multiply this complex number by @a z.
165 template<typename _Up>
166 complex<_Tp>& operator*=(const complex<_Up>&);
167 /// Divide this complex number by @a z.
168 template<typename _Up>
169 complex<_Tp>& operator/=(const complex<_Up>&);
171 const complex& __rep() const;
178 template<typename _Tp>
180 complex<_Tp>::real() { return _M_real; }
182 template<typename _Tp>
184 complex<_Tp>::real() const { return _M_real; }
186 template<typename _Tp>
188 complex<_Tp>::imag() { return _M_imag; }
190 template<typename _Tp>
192 complex<_Tp>::imag() const { return _M_imag; }
194 template<typename _Tp>
196 complex<_Tp>::complex(const _Tp& __r, const _Tp& __i)
197 : _M_real(__r), _M_imag(__i) { }
199 template<typename _Tp>
200 template<typename _Up>
202 complex<_Tp>::complex(const complex<_Up>& __z)
203 : _M_real(__z.real()), _M_imag(__z.imag()) { }
205 template<typename _Tp>
207 complex<_Tp>::operator=(const _Tp& __t)
215 template<typename _Tp>
217 complex<_Tp>::operator+=(const _Tp& __t)
224 template<typename _Tp>
226 complex<_Tp>::operator-=(const _Tp& __t)
233 template<typename _Tp>
235 complex<_Tp>::operator*=(const _Tp& __t)
243 template<typename _Tp>
245 complex<_Tp>::operator/=(const _Tp& __t)
252 template<typename _Tp>
253 template<typename _Up>
255 complex<_Tp>::operator=(const complex<_Up>& __z)
257 _M_real = __z.real();
258 _M_imag = __z.imag();
263 template<typename _Tp>
264 template<typename _Up>
266 complex<_Tp>::operator+=(const complex<_Up>& __z)
268 _M_real += __z.real();
269 _M_imag += __z.imag();
274 template<typename _Tp>
275 template<typename _Up>
277 complex<_Tp>::operator-=(const complex<_Up>& __z)
279 _M_real -= __z.real();
280 _M_imag -= __z.imag();
285 // XXX: This is a grammar school implementation.
286 template<typename _Tp>
287 template<typename _Up>
289 complex<_Tp>::operator*=(const complex<_Up>& __z)
291 const _Tp __r = _M_real * __z.real() - _M_imag * __z.imag();
292 _M_imag = _M_real * __z.imag() + _M_imag * __z.real();
298 // XXX: This is a grammar school implementation.
299 template<typename _Tp>
300 template<typename _Up>
302 complex<_Tp>::operator/=(const complex<_Up>& __z)
304 const _Tp __r = _M_real * __z.real() + _M_imag * __z.imag();
305 const _Tp __n = std::norm(__z);
306 _M_imag = (_M_imag * __z.real() - _M_real * __z.imag()) / __n;
311 template<typename _Tp>
312 inline const complex<_Tp>&
313 complex<_Tp>::__rep() const { return *this; }
317 /// Return new complex value @a x plus @a y.
318 template<typename _Tp>
320 operator+(const complex<_Tp>& __x, const complex<_Tp>& __y)
322 complex<_Tp> __r = __x;
327 template<typename _Tp>
329 operator+(const complex<_Tp>& __x, const _Tp& __y)
331 complex<_Tp> __r = __x;
336 template<typename _Tp>
338 operator+(const _Tp& __x, const complex<_Tp>& __y)
340 complex<_Tp> __r = __y;
347 /// Return new complex value @a x minus @a y.
348 template<typename _Tp>
350 operator-(const complex<_Tp>& __x, const complex<_Tp>& __y)
352 complex<_Tp> __r = __x;
357 template<typename _Tp>
359 operator-(const complex<_Tp>& __x, const _Tp& __y)
361 complex<_Tp> __r = __x;
366 template<typename _Tp>
368 operator-(const _Tp& __x, const complex<_Tp>& __y)
370 complex<_Tp> __r(__x, -__y.imag());
371 __r.real() -= __y.real();
377 /// Return new complex value @a x times @a y.
378 template<typename _Tp>
380 operator*(const complex<_Tp>& __x, const complex<_Tp>& __y)
382 complex<_Tp> __r = __x;
387 template<typename _Tp>
389 operator*(const complex<_Tp>& __x, const _Tp& __y)
391 complex<_Tp> __r = __x;
396 template<typename _Tp>
398 operator*(const _Tp& __x, const complex<_Tp>& __y)
400 complex<_Tp> __r = __y;
407 /// Return new complex value @a x divided by @a y.
408 template<typename _Tp>
410 operator/(const complex<_Tp>& __x, const complex<_Tp>& __y)
412 complex<_Tp> __r = __x;
417 template<typename _Tp>
419 operator/(const complex<_Tp>& __x, const _Tp& __y)
421 complex<_Tp> __r = __x;
426 template<typename _Tp>
428 operator/(const _Tp& __x, const complex<_Tp>& __y)
430 complex<_Tp> __r = __x;
437 template<typename _Tp>
439 operator+(const complex<_Tp>& __x)
442 /// Return complex negation of @a x.
443 template<typename _Tp>
445 operator-(const complex<_Tp>& __x)
446 { return complex<_Tp>(-__x.real(), -__x.imag()); }
449 /// Return true if @a x is equal to @a y.
450 template<typename _Tp>
452 operator==(const complex<_Tp>& __x, const complex<_Tp>& __y)
453 { return __x.real() == __y.real() && __x.imag() == __y.imag(); }
455 template<typename _Tp>
457 operator==(const complex<_Tp>& __x, const _Tp& __y)
458 { return __x.real() == __y && __x.imag() == _Tp(); }
460 template<typename _Tp>
462 operator==(const _Tp& __x, const complex<_Tp>& __y)
463 { return __x == __y.real() && _Tp() == __y.imag(); }
467 /// Return false if @a x is equal to @a y.
468 template<typename _Tp>
470 operator!=(const complex<_Tp>& __x, const complex<_Tp>& __y)
471 { return __x.real() != __y.real() || __x.imag() != __y.imag(); }
473 template<typename _Tp>
475 operator!=(const complex<_Tp>& __x, const _Tp& __y)
476 { return __x.real() != __y || __x.imag() != _Tp(); }
478 template<typename _Tp>
480 operator!=(const _Tp& __x, const complex<_Tp>& __y)
481 { return __x != __y.real() || _Tp() != __y.imag(); }
484 /// Extraction operator for complex values.
485 template<typename _Tp, typename _CharT, class _Traits>
486 basic_istream<_CharT, _Traits>&
487 operator>>(basic_istream<_CharT, _Traits>& __is, complex<_Tp>& __x)
494 __is >> __re_x >> __ch;
497 __is >> __im_x >> __ch;
499 __x = complex<_Tp>(__re_x, __im_x);
501 __is.setstate(ios_base::failbit);
503 else if (__ch == ')')
506 __is.setstate(ios_base::failbit);
517 /// Insertion operator for complex values.
518 template<typename _Tp, typename _CharT, class _Traits>
519 basic_ostream<_CharT, _Traits>&
520 operator<<(basic_ostream<_CharT, _Traits>& __os, const complex<_Tp>& __x)
522 basic_ostringstream<_CharT, _Traits> __s;
523 __s.flags(__os.flags());
524 __s.imbue(__os.getloc());
525 __s.precision(__os.precision());
526 __s << '(' << __x.real() << ',' << __x.imag() << ')';
527 return __os << __s.str();
531 template<typename _Tp>
533 real(complex<_Tp>& __z)
534 { return __z.real(); }
536 template<typename _Tp>
538 real(const complex<_Tp>& __z)
539 { return __z.real(); }
541 template<typename _Tp>
543 imag(complex<_Tp>& __z)
544 { return __z.imag(); }
546 template<typename _Tp>
548 imag(const complex<_Tp>& __z)
549 { return __z.imag(); }
551 // 26.2.7/3 abs(__z): Returns the magnitude of __z.
552 template<typename _Tp>
554 __complex_abs(const complex<_Tp>& __z)
556 _Tp __x = __z.real();
557 _Tp __y = __z.imag();
558 const _Tp __s = std::max(abs(__x), abs(__y));
559 if (__s == _Tp()) // well ...
563 return __s * sqrt(__x * __x + __y * __y);
566 #if _GLIBCXX_USE_C99_COMPLEX
568 __complex_abs(__complex__ float __z) { return __builtin_cabsf(__z); }
571 __complex_abs(__complex__ double __z) { return __builtin_cabs(__z); }
574 __complex_abs(const __complex__ long double& __z)
575 { return __builtin_cabsl(__z); }
577 template<typename _Tp>
579 abs(const complex<_Tp>& __z) { return __complex_abs(__z.__rep()); }
581 template<typename _Tp>
583 abs(const complex<_Tp>& __z) { return __complex_abs(__z); }
587 // 26.2.7/4: arg(__z): Returns the phase angle of __z.
588 template<typename _Tp>
590 __complex_arg(const complex<_Tp>& __z)
591 { return atan2(__z.imag(), __z.real()); }
593 #if _GLIBCXX_USE_C99_COMPLEX
595 __complex_arg(__complex__ float __z) { return __builtin_cargf(__z); }
598 __complex_arg(__complex__ double __z) { return __builtin_carg(__z); }
601 __complex_arg(const __complex__ long double& __z)
602 { return __builtin_cargl(__z); }
604 template<typename _Tp>
606 arg(const complex<_Tp>& __z) { return __complex_arg(__z.__rep()); }
608 template<typename _Tp>
610 arg(const complex<_Tp>& __z) { return __complex_arg(__z); }
613 // 26.2.7/5: norm(__z) returns the squared magnitude of __z.
614 // As defined, norm() is -not- a norm is the common mathematical
615 // sens used in numerics. The helper class _Norm_helper<> tries to
616 // distinguish between builtin floating point and the rest, so as
617 // to deliver an answer as close as possible to the real value.
621 template<typename _Tp>
622 static inline _Tp _S_do_it(const complex<_Tp>& __z)
624 const _Tp __x = __z.real();
625 const _Tp __y = __z.imag();
626 return __x * __x + __y * __y;
631 struct _Norm_helper<true>
633 template<typename _Tp>
634 static inline _Tp _S_do_it(const complex<_Tp>& __z)
636 _Tp __res = std::abs(__z);
637 return __res * __res;
641 template<typename _Tp>
643 norm(const complex<_Tp>& __z)
645 return _Norm_helper<__is_floating<_Tp>::__value
646 && !_GLIBCXX_FAST_MATH>::_S_do_it(__z);
649 template<typename _Tp>
651 polar(const _Tp& __rho, const _Tp& __theta)
652 { return complex<_Tp>(__rho * cos(__theta), __rho * sin(__theta)); }
654 template<typename _Tp>
656 conj(const complex<_Tp>& __z)
657 { return complex<_Tp>(__z.real(), -__z.imag()); }
661 // 26.2.8/1 cos(__z): Returns the cosine of __z.
662 template<typename _Tp>
664 __complex_cos(const complex<_Tp>& __z)
666 const _Tp __x = __z.real();
667 const _Tp __y = __z.imag();
668 return complex<_Tp>(cos(__x) * cosh(__y), -sin(__x) * sinh(__y));
671 #if _GLIBCXX_USE_C99_COMPLEX
672 inline __complex__ float
673 __complex_cos(__complex__ float __z) { return __builtin_ccosf(__z); }
675 inline __complex__ double
676 __complex_cos(__complex__ double __z) { return __builtin_ccos(__z); }
678 inline __complex__ long double
679 __complex_cos(const __complex__ long double& __z)
680 { return __builtin_ccosl(__z); }
682 template<typename _Tp>
684 cos(const complex<_Tp>& __z) { return __complex_cos(__z.__rep()); }
686 template<typename _Tp>
688 cos(const complex<_Tp>& __z) { return __complex_cos(__z); }
691 // 26.2.8/2 cosh(__z): Returns the hyperbolic cosine of __z.
692 template<typename _Tp>
694 __complex_cosh(const complex<_Tp>& __z)
696 const _Tp __x = __z.real();
697 const _Tp __y = __z.imag();
698 return complex<_Tp>(cosh(__x) * cos(__y), sinh(__x) * sin(__y));
701 #if _GLIBCXX_USE_C99_COMPLEX
702 inline __complex__ float
703 __complex_cosh(__complex__ float __z) { return __builtin_ccoshf(__z); }
705 inline __complex__ double
706 __complex_cosh(__complex__ double __z) { return __builtin_ccosh(__z); }
708 inline __complex__ long double
709 __complex_cosh(const __complex__ long double& __z)
710 { return __builtin_ccoshl(__z); }
712 template<typename _Tp>
714 cosh(const complex<_Tp>& __z) { return __complex_cosh(__z.__rep()); }
716 template<typename _Tp>
718 cosh(const complex<_Tp>& __z) { return __complex_cosh(__z); }
721 // 26.2.8/3 exp(__z): Returns the complex base e exponential of x
722 template<typename _Tp>
724 __complex_exp(const complex<_Tp>& __z)
725 { return std::polar(exp(__z.real()), __z.imag()); }
727 #if _GLIBCXX_USE_C99_COMPLEX
728 inline __complex__ float
729 __complex_exp(__complex__ float __z) { return __builtin_cexpf(__z); }
731 inline __complex__ double
732 __complex_exp(__complex__ double __z) { return __builtin_cexp(__z); }
734 inline __complex__ long double
735 __complex_exp(const __complex__ long double& __z)
736 { return __builtin_cexpl(__z); }
738 template<typename _Tp>
740 exp(const complex<_Tp>& __z) { return __complex_exp(__z.__rep()); }
742 template<typename _Tp>
744 exp(const complex<_Tp>& __z) { return __complex_exp(__z); }
747 // 26.2.8/5 log(__z): Returns the natural complex logarithm of __z.
748 // The branch cut is along the negative axis.
749 template<typename _Tp>
751 __complex_log(const complex<_Tp>& __z)
752 { return complex<_Tp>(log(std::abs(__z)), std::arg(__z)); }
754 #if _GLIBCXX_USE_C99_COMPLEX
755 inline __complex__ float
756 __complex_log(__complex__ float __z) { return __builtin_clogf(__z); }
758 inline __complex__ double
759 __complex_log(__complex__ double __z) { return __builtin_clog(__z); }
761 inline __complex__ long double
762 __complex_log(const __complex__ long double& __z)
763 { return __builtin_clogl(__z); }
765 template<typename _Tp>
767 log(const complex<_Tp>& __z) { return __complex_log(__z.__rep()); }
769 template<typename _Tp>
771 log(const complex<_Tp>& __z) { return __complex_log(__z); }
774 template<typename _Tp>
776 log10(const complex<_Tp>& __z)
777 { return std::log(__z) / log(_Tp(10.0)); }
779 // 26.2.8/10 sin(__z): Returns the sine of __z.
780 template<typename _Tp>
782 __complex_sin(const complex<_Tp>& __z)
784 const _Tp __x = __z.real();
785 const _Tp __y = __z.imag();
786 return complex<_Tp>(sin(__x) * cosh(__y), cos(__x) * sinh(__y));
789 #if _GLIBCXX_USE_C99_COMPLEX
790 inline __complex__ float
791 __complex_sin(__complex__ float __z) { return __builtin_csinf(__z); }
793 inline __complex__ double
794 __complex_sin(__complex__ double __z) { return __builtin_csin(__z); }
796 inline __complex__ long double
797 __complex_sin(const __complex__ long double& __z)
798 { return __builtin_csinl(__z); }
800 template<typename _Tp>
802 sin(const complex<_Tp>& __z) { return __complex_sin(__z.__rep()); }
804 template<typename _Tp>
806 sin(const complex<_Tp>& __z) { return __complex_sin(__z); }
809 // 26.2.8/11 sinh(__z): Returns the hyperbolic sine of __z.
810 template<typename _Tp>
812 __complex_sinh(const complex<_Tp>& __z)
814 const _Tp __x = __z.real();
815 const _Tp __y = __z.imag();
816 return complex<_Tp>(sinh(__x) * cos(__y), cosh(__x) * sin(__y));
819 #if _GLIBCXX_USE_C99_COMPLEX
820 inline __complex__ float
821 __complex_sinh(__complex__ float __z) { return __builtin_csinhf(__z); }
823 inline __complex__ double
824 __complex_sinh(__complex__ double __z) { return __builtin_csinh(__z); }
826 inline __complex__ long double
827 __complex_sinh(const __complex__ long double& __z)
828 { return __builtin_csinhl(__z); }
830 template<typename _Tp>
832 sinh(const complex<_Tp>& __z) { return __complex_sinh(__z.__rep()); }
834 template<typename _Tp>
836 sinh(const complex<_Tp>& __z) { return __complex_sinh(__z); }
839 // 26.2.8/13 sqrt(__z): Returns the complex square root of __z.
840 // The branch cut is on the negative axis.
841 template<typename _Tp>
843 __complex_sqrt(const complex<_Tp>& __z)
845 _Tp __x = __z.real();
846 _Tp __y = __z.imag();
850 _Tp __t = sqrt(abs(__y) / 2);
851 return complex<_Tp>(__t, __y < _Tp() ? -__t : __t);
855 _Tp __t = sqrt(2 * (std::abs(__z) + abs(__x)));
858 ? complex<_Tp>(__u, __y / __t)
859 : complex<_Tp>(abs(__y) / __t, __y < _Tp() ? -__u : __u);
863 #if _GLIBCXX_USE_C99_COMPLEX
864 inline __complex__ float
865 __complex_sqrt(__complex__ float __z) { return __builtin_csqrtf(__z); }
867 inline __complex__ double
868 __complex_sqrt(__complex__ double __z) { return __builtin_csqrt(__z); }
870 inline __complex__ long double
871 __complex_sqrt(const __complex__ long double& __z)
872 { return __builtin_csqrtl(__z); }
874 template<typename _Tp>
876 sqrt(const complex<_Tp>& __z) { return __complex_sqrt(__z.__rep()); }
878 template<typename _Tp>
880 sqrt(const complex<_Tp>& __z) { return __complex_sqrt(__z); }
883 // 26.2.8/14 tan(__z): Return the complex tangent of __z.
885 template<typename _Tp>
887 __complex_tan(const complex<_Tp>& __z)
888 { return std::sin(__z) / std::cos(__z); }
890 #if _GLIBCXX_USE_C99_COMPLEX
891 inline __complex__ float
892 __complex_tan(__complex__ float __z) { return __builtin_ctanf(__z); }
894 inline __complex__ double
895 __complex_tan(__complex__ double __z) { return __builtin_ctan(__z); }
897 inline __complex__ long double
898 __complex_tan(const __complex__ long double& __z)
899 { return __builtin_ctanl(__z); }
901 template<typename _Tp>
903 tan(const complex<_Tp>& __z) { return __complex_tan(__z.__rep()); }
905 template<typename _Tp>
907 tan(const complex<_Tp>& __z) { return __complex_tan(__z); }
911 // 26.2.8/15 tanh(__z): Returns the hyperbolic tangent of __z.
913 template<typename _Tp>
915 __complex_tanh(const complex<_Tp>& __z)
916 { return std::sinh(__z) / std::cosh(__z); }
918 #if _GLIBCXX_USE_C99_COMPLEX
919 inline __complex__ float
920 __complex_tanh(__complex__ float __z) { return __builtin_ctanhf(__z); }
922 inline __complex__ double
923 __complex_tanh(__complex__ double __z) { return __builtin_ctanh(__z); }
925 inline __complex__ long double
926 __complex_tanh(const __complex__ long double& __z)
927 { return __builtin_ctanhl(__z); }
929 template<typename _Tp>
931 tanh(const complex<_Tp>& __z) { return __complex_tanh(__z.__rep()); }
933 template<typename _Tp>
935 tanh(const complex<_Tp>& __z) { return __complex_tanh(__z); }
939 // 26.2.8/9 pow(__x, __y): Returns the complex power base of __x
940 // raised to the __y-th power. The branch
941 // cut is on the negative axis.
942 template<typename _Tp>
944 pow(const complex<_Tp>& __z, int __n)
945 { return std::__pow_helper(__z, __n); }
947 template<typename _Tp>
949 pow(const complex<_Tp>& __x, const _Tp& __y)
951 #ifndef _GLIBCXX_USE_C99_COMPLEX
955 if (__x.imag() == _Tp() && __x.real() > _Tp())
956 return pow(__x.real(), __y);
958 complex<_Tp> __t = std::log(__x);
959 return std::polar(exp(__y * __t.real()), __y * __t.imag());
962 template<typename _Tp>
964 __complex_pow(const complex<_Tp>& __x, const complex<_Tp>& __y)
965 { return __x == _Tp() ? _Tp() : std::exp(__y * std::log(__x)); }
967 #if _GLIBCXX_USE_C99_COMPLEX
968 inline __complex__ float
969 __complex_pow(__complex__ float __x, __complex__ float __y)
970 { return __builtin_cpowf(__x, __y); }
972 inline __complex__ double
973 __complex_pow(__complex__ double __x, __complex__ double __y)
974 { return __builtin_cpow(__x, __y); }
976 inline __complex__ long double
977 __complex_pow(const __complex__ long double& __x,
978 const __complex__ long double& __y)
979 { return __builtin_cpowl(__x, __y); }
981 template<typename _Tp>
983 pow(const complex<_Tp>& __x, const complex<_Tp>& __y)
984 { return __complex_pow(__x.__rep(), __y.__rep()); }
986 template<typename _Tp>
988 pow(const complex<_Tp>& __x, const complex<_Tp>& __y)
989 { return __complex_pow(__x, __y); }
992 template<typename _Tp>
994 pow(const _Tp& __x, const complex<_Tp>& __y)
996 return __x > _Tp() ? std::polar(pow(__x, __y.real()),
997 __y.imag() * log(__x))
998 : std::pow(complex<_Tp>(__x, _Tp()), __y);
1001 // 26.2.3 complex specializations
1002 // complex<float> specialization
1004 struct complex<float>
1006 typedef float value_type;
1007 typedef __complex__ float _ComplexT;
1009 complex(_ComplexT __z) : _M_value(__z) { }
1011 complex(float = 0.0f, float = 0.0f);
1013 explicit complex(const complex<double>&);
1014 explicit complex(const complex<long double>&);
1017 const float& real() const;
1019 const float& imag() const;
1021 complex<float>& operator=(float);
1022 complex<float>& operator+=(float);
1023 complex<float>& operator-=(float);
1024 complex<float>& operator*=(float);
1025 complex<float>& operator/=(float);
1027 // Let the compiler synthesize the copy and assignment
1028 // operator. It always does a pretty good job.
1029 // complex& operator= (const complex&);
1030 template<typename _Tp>
1031 complex<float>&operator=(const complex<_Tp>&);
1032 template<typename _Tp>
1033 complex<float>& operator+=(const complex<_Tp>&);
1035 complex<float>& operator-=(const complex<_Tp>&);
1037 complex<float>& operator*=(const complex<_Tp>&);
1039 complex<float>&operator/=(const complex<_Tp>&);
1041 const _ComplexT& __rep() const { return _M_value; }
1048 complex<float>::real()
1049 { return __real__ _M_value; }
1052 complex<float>::real() const
1053 { return __real__ _M_value; }
1056 complex<float>::imag()
1057 { return __imag__ _M_value; }
1060 complex<float>::imag() const
1061 { return __imag__ _M_value; }
1064 complex<float>::complex(float __r, float __i)
1066 __real__ _M_value = __r;
1067 __imag__ _M_value = __i;
1070 inline complex<float>&
1071 complex<float>::operator=(float __f)
1073 __real__ _M_value = __f;
1074 __imag__ _M_value = 0.0f;
1078 inline complex<float>&
1079 complex<float>::operator+=(float __f)
1081 __real__ _M_value += __f;
1085 inline complex<float>&
1086 complex<float>::operator-=(float __f)
1088 __real__ _M_value -= __f;
1092 inline complex<float>&
1093 complex<float>::operator*=(float __f)
1099 inline complex<float>&
1100 complex<float>::operator/=(float __f)
1106 template<typename _Tp>
1107 inline complex<float>&
1108 complex<float>::operator=(const complex<_Tp>& __z)
1110 __real__ _M_value = __z.real();
1111 __imag__ _M_value = __z.imag();
1115 template<typename _Tp>
1116 inline complex<float>&
1117 complex<float>::operator+=(const complex<_Tp>& __z)
1119 __real__ _M_value += __z.real();
1120 __imag__ _M_value += __z.imag();
1124 template<typename _Tp>
1125 inline complex<float>&
1126 complex<float>::operator-=(const complex<_Tp>& __z)
1128 __real__ _M_value -= __z.real();
1129 __imag__ _M_value -= __z.imag();
1133 template<typename _Tp>
1134 inline complex<float>&
1135 complex<float>::operator*=(const complex<_Tp>& __z)
1138 __real__ __t = __z.real();
1139 __imag__ __t = __z.imag();
1144 template<typename _Tp>
1145 inline complex<float>&
1146 complex<float>::operator/=(const complex<_Tp>& __z)
1149 __real__ __t = __z.real();
1150 __imag__ __t = __z.imag();
1155 // 26.2.3 complex specializations
1156 // complex<double> specialization
1158 struct complex<double>
1160 typedef double value_type;
1161 typedef __complex__ double _ComplexT;
1163 complex(_ComplexT __z) : _M_value(__z) { }
1165 complex(double = 0.0, double = 0.0);
1167 complex(const complex<float>&);
1168 explicit complex(const complex<long double>&);
1171 const double& real() const;
1173 const double& imag() const;
1175 complex<double>& operator=(double);
1176 complex<double>& operator+=(double);
1177 complex<double>& operator-=(double);
1178 complex<double>& operator*=(double);
1179 complex<double>& operator/=(double);
1181 // The compiler will synthesize this, efficiently.
1182 // complex& operator= (const complex&);
1183 template<typename _Tp>
1184 complex<double>& operator=(const complex<_Tp>&);
1185 template<typename _Tp>
1186 complex<double>& operator+=(const complex<_Tp>&);
1187 template<typename _Tp>
1188 complex<double>& operator-=(const complex<_Tp>&);
1189 template<typename _Tp>
1190 complex<double>& operator*=(const complex<_Tp>&);
1191 template<typename _Tp>
1192 complex<double>& operator/=(const complex<_Tp>&);
1194 const _ComplexT& __rep() const { return _M_value; }
1201 complex<double>::real()
1202 { return __real__ _M_value; }
1204 inline const double&
1205 complex<double>::real() const
1206 { return __real__ _M_value; }
1209 complex<double>::imag()
1210 { return __imag__ _M_value; }
1212 inline const double&
1213 complex<double>::imag() const
1214 { return __imag__ _M_value; }
1217 complex<double>::complex(double __r, double __i)
1219 __real__ _M_value = __r;
1220 __imag__ _M_value = __i;
1223 inline complex<double>&
1224 complex<double>::operator=(double __d)
1226 __real__ _M_value = __d;
1227 __imag__ _M_value = 0.0;
1231 inline complex<double>&
1232 complex<double>::operator+=(double __d)
1234 __real__ _M_value += __d;
1238 inline complex<double>&
1239 complex<double>::operator-=(double __d)
1241 __real__ _M_value -= __d;
1245 inline complex<double>&
1246 complex<double>::operator*=(double __d)
1252 inline complex<double>&
1253 complex<double>::operator/=(double __d)
1259 template<typename _Tp>
1260 inline complex<double>&
1261 complex<double>::operator=(const complex<_Tp>& __z)
1263 __real__ _M_value = __z.real();
1264 __imag__ _M_value = __z.imag();
1268 template<typename _Tp>
1269 inline complex<double>&
1270 complex<double>::operator+=(const complex<_Tp>& __z)
1272 __real__ _M_value += __z.real();
1273 __imag__ _M_value += __z.imag();
1277 template<typename _Tp>
1278 inline complex<double>&
1279 complex<double>::operator-=(const complex<_Tp>& __z)
1281 __real__ _M_value -= __z.real();
1282 __imag__ _M_value -= __z.imag();
1286 template<typename _Tp>
1287 inline complex<double>&
1288 complex<double>::operator*=(const complex<_Tp>& __z)
1291 __real__ __t = __z.real();
1292 __imag__ __t = __z.imag();
1297 template<typename _Tp>
1298 inline complex<double>&
1299 complex<double>::operator/=(const complex<_Tp>& __z)
1302 __real__ __t = __z.real();
1303 __imag__ __t = __z.imag();
1308 // 26.2.3 complex specializations
1309 // complex<long double> specialization
1311 struct complex<long double>
1313 typedef long double value_type;
1314 typedef __complex__ long double _ComplexT;
1316 complex(_ComplexT __z) : _M_value(__z) { }
1318 complex(long double = 0.0L, long double = 0.0L);
1320 complex(const complex<float>&);
1321 complex(const complex<double>&);
1323 long double& real();
1324 const long double& real() const;
1325 long double& imag();
1326 const long double& imag() const;
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);
1332 complex<long double>& operator/= (long double);
1334 // The compiler knows how to do this efficiently
1335 // complex& operator= (const complex&);
1336 template<typename _Tp>
1337 complex<long double>& operator=(const complex<_Tp>&);
1338 template<typename _Tp>
1339 complex<long double>& operator+=(const complex<_Tp>&);
1340 template<typename _Tp>
1341 complex<long double>& operator-=(const complex<_Tp>&);
1342 template<typename _Tp>
1343 complex<long double>& operator*=(const complex<_Tp>&);
1344 template<typename _Tp>
1345 complex<long double>& operator/=(const complex<_Tp>&);
1347 const _ComplexT& __rep() const { return _M_value; }
1354 complex<long double>::complex(long double __r, long double __i)
1356 __real__ _M_value = __r;
1357 __imag__ _M_value = __i;
1361 complex<long double>::real()
1362 { return __real__ _M_value; }
1364 inline const long double&
1365 complex<long double>::real() const
1366 { return __real__ _M_value; }
1369 complex<long double>::imag()
1370 { return __imag__ _M_value; }
1372 inline const long double&
1373 complex<long double>::imag() const
1374 { return __imag__ _M_value; }
1376 inline complex<long double>&
1377 complex<long double>::operator=(long double __r)
1379 __real__ _M_value = __r;
1380 __imag__ _M_value = 0.0L;
1384 inline complex<long double>&
1385 complex<long double>::operator+=(long double __r)
1387 __real__ _M_value += __r;
1391 inline complex<long double>&
1392 complex<long double>::operator-=(long double __r)
1394 __real__ _M_value -= __r;
1398 inline complex<long double>&
1399 complex<long double>::operator*=(long double __r)
1405 inline complex<long double>&
1406 complex<long double>::operator/=(long double __r)
1412 template<typename _Tp>
1413 inline complex<long double>&
1414 complex<long double>::operator=(const complex<_Tp>& __z)
1416 __real__ _M_value = __z.real();
1417 __imag__ _M_value = __z.imag();
1421 template<typename _Tp>
1422 inline complex<long double>&
1423 complex<long double>::operator+=(const complex<_Tp>& __z)
1425 __real__ _M_value += __z.real();
1426 __imag__ _M_value += __z.imag();
1430 template<typename _Tp>
1431 inline complex<long double>&
1432 complex<long double>::operator-=(const complex<_Tp>& __z)
1434 __real__ _M_value -= __z.real();
1435 __imag__ _M_value -= __z.imag();
1439 template<typename _Tp>
1440 inline complex<long double>&
1441 complex<long double>::operator*=(const complex<_Tp>& __z)
1444 __real__ __t = __z.real();
1445 __imag__ __t = __z.imag();
1450 template<typename _Tp>
1451 inline complex<long double>&
1452 complex<long double>::operator/=(const complex<_Tp>& __z)
1455 __real__ __t = __z.real();
1456 __imag__ __t = __z.imag();
1461 // These bits have to be at the end of this file, so that the
1462 // specializations have all been defined.
1463 // ??? No, they have to be there because of compiler limitation at
1464 // inlining. It suffices that class specializations be defined.
1466 complex<float>::complex(const complex<double>& __z)
1467 : _M_value(__z.__rep()) { }
1470 complex<float>::complex(const complex<long double>& __z)
1471 : _M_value(__z.__rep()) { }
1474 complex<double>::complex(const complex<float>& __z)
1475 : _M_value(__z.__rep()) { }
1478 complex<double>::complex(const complex<long double>& __z)
1479 : _M_value(__z.__rep()) { }
1482 complex<long double>::complex(const complex<float>& __z)
1483 : _M_value(__z.__rep()) { }
1486 complex<long double>::complex(const complex<double>& __z)
1487 : _M_value(__z.__rep()) { }
1489 // Inhibit implicit instantiations for required instantiations,
1490 // which are defined via explicit instantiations elsewhere.
1491 // NB: This syntax is a GNU extension.
1492 #if _GLIBCXX_EXTERN_TEMPLATE
1493 extern template istream& operator>>(istream&, complex<float>&);
1494 extern template ostream& operator<<(ostream&, const complex<float>&);
1495 extern template istream& operator>>(istream&, complex<double>&);
1496 extern template ostream& operator<<(ostream&, const complex<double>&);
1497 extern template istream& operator>>(istream&, complex<long double>&);
1498 extern template ostream& operator<<(ostream&, const complex<long double>&);
1500 #ifdef _GLIBCXX_USE_WCHAR_T
1501 extern template wistream& operator>>(wistream&, complex<float>&);
1502 extern template wostream& operator<<(wostream&, const complex<float>&);
1503 extern template wistream& operator>>(wistream&, complex<double>&);
1504 extern template wostream& operator<<(wostream&, const complex<double>&);
1505 extern template wistream& operator>>(wistream&, complex<long double>&);
1506 extern template wostream& operator<<(wostream&, const complex<long double>&);
1510 _GLIBCXX_END_NAMESPACE
1512 _GLIBCXX_BEGIN_NAMESPACE(__gnu_cxx)
1514 // See ext/type_traits.h for the primary template.
1515 template<typename _Tp, typename _Up>
1516 struct __promote_2<std::complex<_Tp>, _Up>
1519 typedef std::complex<typename __promote_2<_Tp, _Up>::__type> __type;
1522 template<typename _Tp, typename _Up>
1523 struct __promote_2<_Tp, std::complex<_Up> >
1526 typedef std::complex<typename __promote_2<_Tp, _Up>::__type> __type;
1529 template<typename _Tp, typename _Up>
1530 struct __promote_2<std::complex<_Tp>, std::complex<_Up> >
1533 typedef std::complex<typename __promote_2<_Tp, _Up>::__type> __type;
1536 _GLIBCXX_END_NAMESPACE
1538 #ifdef __GXX_EXPERIMENTAL_CXX0X__
1539 # if defined(_GLIBCXX_INCLUDE_AS_TR1)
1540 # error C++0x header cannot be included from TR1 header
1542 # if defined(_GLIBCXX_INCLUDE_AS_CXX0X)
1543 # include <tr1_impl/complex>
1545 # define _GLIBCXX_INCLUDE_AS_CXX0X
1546 # define _GLIBCXX_BEGIN_NAMESPACE_TR1
1547 # define _GLIBCXX_END_NAMESPACE_TR1
1548 # define _GLIBCXX_TR1
1549 # include <tr1_impl/complex>
1550 # undef _GLIBCXX_TR1
1551 # undef _GLIBCXX_END_NAMESPACE_TR1
1552 # undef _GLIBCXX_BEGIN_NAMESPACE_TR1
1553 # undef _GLIBCXX_INCLUDE_AS_CXX0X
1557 #endif /* _GLIBCXX_COMPLEX */