1 // The template and inlines for the -*- C++ -*- complex number classes.
3 // Copyright (C) 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004
4 // Free Software Foundation, Inc.
6 // This file is part of the GNU ISO C++ Library. This library is free
7 // software; you can redistribute it and/or modify it under the
8 // terms of the GNU General Public License as published by the
9 // Free Software Foundation; either version 2, or (at your option)
12 // This library is distributed in the hope that it will be useful,
13 // but WITHOUT ANY WARRANTY; without even the implied warranty of
14 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 // GNU General Public License for more details.
17 // You should have received a copy of the GNU General Public License along
18 // with this library; see the file COPYING. If not, write to the Free
19 // Software Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307,
22 // As a special exception, you may use this file as part of a free software
23 // library without restriction. Specifically, if other files instantiate
24 // templates or use macros or inline functions from this file, or you compile
25 // this file and link it with other files to produce an executable, this
26 // file does not by itself cause the resulting executable to be covered by
27 // the GNU General Public License. This exception does not however
28 // invalidate any other reasons why the executable file might be covered by
29 // the GNU General Public License.
32 // ISO C++ 14882: 26.2 Complex Numbers
33 // Note: this is not a conforming implementation.
34 // Initially implemented by Ulrich Drepper <drepper@cygnus.com>
35 // Improved by Gabriel Dos Reis <dosreis@cmla.ens-cachan.fr>
39 * This is a Standard C++ Library header. You should @c #include this header
40 * in your programs, rather than any of the "st[dl]_*.h" implementation files.
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>
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>
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>&);
176 template<typename _Tp>
178 complex<_Tp>::real() { return _M_real; }
180 template<typename _Tp>
182 complex<_Tp>::real() const { return _M_real; }
184 template<typename _Tp>
186 complex<_Tp>::imag() { return _M_imag; }
188 template<typename _Tp>
190 complex<_Tp>::imag() const { return _M_imag; }
192 template<typename _Tp>
194 complex<_Tp>::complex(const _Tp& __r, const _Tp& __i)
195 : _M_real(__r), _M_imag(__i) { }
197 template<typename _Tp>
198 template<typename _Up>
200 complex<_Tp>::complex(const complex<_Up>& __z)
201 : _M_real(__z.real()), _M_imag(__z.imag()) { }
203 template<typename _Tp>
205 complex<_Tp>::operator=(const _Tp& __t)
213 template<typename _Tp>
215 complex<_Tp>::operator+=(const _Tp& __t)
222 template<typename _Tp>
224 complex<_Tp>::operator-=(const _Tp& __t)
231 template<typename _Tp>
233 complex<_Tp>::operator*=(const _Tp& __t)
241 template<typename _Tp>
243 complex<_Tp>::operator/=(const _Tp& __t)
250 template<typename _Tp>
251 template<typename _Up>
253 complex<_Tp>::operator=(const complex<_Up>& __z)
255 _M_real = __z.real();
256 _M_imag = __z.imag();
261 template<typename _Tp>
262 template<typename _Up>
264 complex<_Tp>::operator+=(const complex<_Up>& __z)
266 _M_real += __z.real();
267 _M_imag += __z.imag();
272 template<typename _Tp>
273 template<typename _Up>
275 complex<_Tp>::operator-=(const complex<_Up>& __z)
277 _M_real -= __z.real();
278 _M_imag -= __z.imag();
283 // XXX: This is a grammar school implementation.
284 template<typename _Tp>
285 template<typename _Up>
287 complex<_Tp>::operator*=(const complex<_Up>& __z)
289 const _Tp __r = _M_real * __z.real() - _M_imag * __z.imag();
290 _M_imag = _M_real * __z.imag() + _M_imag * __z.real();
296 // XXX: This is a grammar school implementation.
297 template<typename _Tp>
298 template<typename _Up>
300 complex<_Tp>::operator/=(const complex<_Up>& __z)
302 const _Tp __r = _M_real * __z.real() + _M_imag * __z.imag();
303 const _Tp __n = std::norm(__z);
304 _M_imag = (_M_imag * __z.real() - _M_real * __z.imag()) / __n;
311 /// Return new complex value @a x plus @a y.
312 template<typename _Tp>
314 operator+(const complex<_Tp>& __x, const complex<_Tp>& __y)
316 complex<_Tp> __r = __x;
321 template<typename _Tp>
323 operator+(const complex<_Tp>& __x, const _Tp& __y)
325 complex<_Tp> __r = __x;
330 template<typename _Tp>
332 operator+(const _Tp& __x, const complex<_Tp>& __y)
334 complex<_Tp> __r = __y;
341 /// Return new complex value @a x minus @a y.
342 template<typename _Tp>
344 operator-(const complex<_Tp>& __x, const complex<_Tp>& __y)
346 complex<_Tp> __r = __x;
351 template<typename _Tp>
353 operator-(const complex<_Tp>& __x, const _Tp& __y)
355 complex<_Tp> __r = __x;
360 template<typename _Tp>
362 operator-(const _Tp& __x, const complex<_Tp>& __y)
364 complex<_Tp> __r(__x, -__y.imag());
365 __r.real() -= __y.real();
371 /// Return new complex value @a x times @a y.
372 template<typename _Tp>
374 operator*(const complex<_Tp>& __x, const complex<_Tp>& __y)
376 complex<_Tp> __r = __x;
381 template<typename _Tp>
383 operator*(const complex<_Tp>& __x, const _Tp& __y)
385 complex<_Tp> __r = __x;
390 template<typename _Tp>
392 operator*(const _Tp& __x, const complex<_Tp>& __y)
394 complex<_Tp> __r = __y;
401 /// Return new complex value @a x divided by @a y.
402 template<typename _Tp>
404 operator/(const complex<_Tp>& __x, const complex<_Tp>& __y)
406 complex<_Tp> __r = __x;
411 template<typename _Tp>
413 operator/(const complex<_Tp>& __x, const _Tp& __y)
415 complex<_Tp> __r = __x;
420 template<typename _Tp>
422 operator/(const _Tp& __x, const complex<_Tp>& __y)
424 complex<_Tp> __r = __x;
431 template<typename _Tp>
433 operator+(const complex<_Tp>& __x)
436 /// Return complex negation of @a x.
437 template<typename _Tp>
439 operator-(const complex<_Tp>& __x)
440 { return complex<_Tp>(-__x.real(), -__x.imag()); }
443 /// Return true if @a x is equal to @a y.
444 template<typename _Tp>
446 operator==(const complex<_Tp>& __x, const complex<_Tp>& __y)
447 { return __x.real() == __y.real() && __x.imag() == __y.imag(); }
449 template<typename _Tp>
451 operator==(const complex<_Tp>& __x, const _Tp& __y)
452 { return __x.real() == __y && __x.imag() == _Tp(); }
454 template<typename _Tp>
456 operator==(const _Tp& __x, const complex<_Tp>& __y)
457 { return __x == __y.real() && _Tp() == __y.imag(); }
461 /// Return false if @a x is equal to @a y.
462 template<typename _Tp>
464 operator!=(const complex<_Tp>& __x, const complex<_Tp>& __y)
465 { return __x.real() != __y.real() || __x.imag() != __y.imag(); }
467 template<typename _Tp>
469 operator!=(const complex<_Tp>& __x, const _Tp& __y)
470 { return __x.real() != __y || __x.imag() != _Tp(); }
472 template<typename _Tp>
474 operator!=(const _Tp& __x, const complex<_Tp>& __y)
475 { return __x != __y.real() || _Tp() != __y.imag(); }
478 /// Extraction operator for complex values.
479 template<typename _Tp, typename _CharT, class _Traits>
480 basic_istream<_CharT, _Traits>&
481 operator>>(basic_istream<_CharT, _Traits>& __is, complex<_Tp>& __x)
488 __is >> __re_x >> __ch;
491 __is >> __im_x >> __ch;
493 __x = complex<_Tp>(__re_x, __im_x);
495 __is.setstate(ios_base::failbit);
497 else if (__ch == ')')
500 __is.setstate(ios_base::failbit);
511 /// Insertion operator for complex values.
512 template<typename _Tp, typename _CharT, class _Traits>
513 basic_ostream<_CharT, _Traits>&
514 operator<<(basic_ostream<_CharT, _Traits>& __os, const complex<_Tp>& __x)
516 basic_ostringstream<_CharT, _Traits> __s;
517 __s.flags(__os.flags());
518 __s.imbue(__os.getloc());
519 __s.precision(__os.precision());
520 __s << '(' << __x.real() << ',' << __x.imag() << ')';
521 return __os << __s.str();
525 template<typename _Tp>
527 real(complex<_Tp>& __z)
528 { return __z.real(); }
530 template<typename _Tp>
532 real(const complex<_Tp>& __z)
533 { return __z.real(); }
535 template<typename _Tp>
537 imag(complex<_Tp>& __z)
538 { return __z.imag(); }
540 template<typename _Tp>
542 imag(const complex<_Tp>& __z)
543 { return __z.imag(); }
545 template<typename _Tp>
547 abs(const complex<_Tp>& __z)
549 _Tp __x = __z.real();
550 _Tp __y = __z.imag();
551 const _Tp __s = std::max(abs(__x), abs(__y));
552 if (__s == _Tp()) // well ...
556 return __s * sqrt(__x * __x + __y * __y);
559 template<typename _Tp>
561 arg(const complex<_Tp>& __z)
562 { return atan2(__z.imag(), __z.real()); }
564 // 26.2.7/5: norm(__z) returns the squared magintude of __z.
565 // As defined, norm() is -not- a norm is the common mathematical
566 // sens used in numerics. The helper class _Norm_helper<> tries to
567 // distinguish between builtin floating point and the rest, so as
568 // to deliver an answer as close as possible to the real value.
572 template<typename _Tp>
573 static inline _Tp _S_do_it(const complex<_Tp>& __z)
575 const _Tp __x = __z.real();
576 const _Tp __y = __z.imag();
577 return __x * __x + __y * __y;
582 struct _Norm_helper<true>
584 template<typename _Tp>
585 static inline _Tp _S_do_it(const complex<_Tp>& __z)
587 _Tp __res = std::abs(__z);
588 return __res * __res;
592 template<typename _Tp>
594 norm(const complex<_Tp>& __z)
596 return _Norm_helper<__is_floating<_Tp>::_M_type && !_GLIBCXX_FAST_MATH>::_S_do_it(__z);
599 template<typename _Tp>
601 polar(const _Tp& __rho, const _Tp& __theta)
602 { return complex<_Tp>(__rho * cos(__theta), __rho * sin(__theta)); }
604 template<typename _Tp>
606 conj(const complex<_Tp>& __z)
607 { return complex<_Tp>(__z.real(), -__z.imag()); }
610 template<typename _Tp>
612 cos(const complex<_Tp>& __z)
614 const _Tp __x = __z.real();
615 const _Tp __y = __z.imag();
616 return complex<_Tp>(cos(__x) * cosh(__y), -sin(__x) * sinh(__y));
619 template<typename _Tp>
621 cosh(const complex<_Tp>& __z)
623 const _Tp __x = __z.real();
624 const _Tp __y = __z.imag();
625 return complex<_Tp>(cosh(__x) * cos(__y), sinh(__x) * sin(__y));
628 template<typename _Tp>
630 exp(const complex<_Tp>& __z)
631 { return std::polar(exp(__z.real()), __z.imag()); }
633 template<typename _Tp>
635 log(const complex<_Tp>& __z)
636 { return complex<_Tp>(log(std::abs(__z)), std::arg(__z)); }
638 template<typename _Tp>
640 log10(const complex<_Tp>& __z)
641 { return std::log(__z) / log(_Tp(10.0)); }
643 template<typename _Tp>
645 sin(const complex<_Tp>& __z)
647 const _Tp __x = __z.real();
648 const _Tp __y = __z.imag();
649 return complex<_Tp>(sin(__x) * cosh(__y), cos(__x) * sinh(__y));
652 template<typename _Tp>
654 sinh(const complex<_Tp>& __z)
656 const _Tp __x = __z.real();
657 const _Tp __y = __z.imag();
658 return complex<_Tp>(sinh(__x) * cos(__y), cosh(__x) * sin(__y));
661 template<typename _Tp>
663 sqrt(const complex<_Tp>& __z)
665 _Tp __x = __z.real();
666 _Tp __y = __z.imag();
670 _Tp __t = sqrt(abs(__y) / 2);
671 return complex<_Tp>(__t, __y < _Tp() ? -__t : __t);
675 _Tp __t = sqrt(2 * (std::abs(__z) + abs(__x)));
678 ? complex<_Tp>(__u, __y / __t)
679 : complex<_Tp>(abs(__y) / __t, __y < _Tp() ? -__u : __u);
683 template<typename _Tp>
685 tan(const complex<_Tp>& __z)
687 return std::sin(__z) / std::cos(__z);
690 template<typename _Tp>
692 tanh(const complex<_Tp>& __z)
694 return std::sinh(__z) / std::cosh(__z);
697 template<typename _Tp>
699 pow(const complex<_Tp>& __z, int __n)
701 return std::__pow_helper(__z, __n);
704 template<typename _Tp>
706 pow(const complex<_Tp>& __x, const _Tp& __y)
708 if (__x.imag() == _Tp() && __x.real() > _Tp())
709 return pow(__x.real(), __y);
711 complex<_Tp> __t = std::log(__x);
712 return std::polar(exp(__y * __t.real()), __y * __t.imag());
715 template<typename _Tp>
717 pow(const complex<_Tp>& __x, const complex<_Tp>& __y)
719 return __x == _Tp() ? _Tp() : std::exp(__y * std::log(__x));
722 template<typename _Tp>
724 pow(const _Tp& __x, const complex<_Tp>& __y)
726 return __x > _Tp() ? std::polar(pow(__x, __y.real()),
727 __y.imag() * log(__x))
728 : std::pow(complex<_Tp>(__x, _Tp()), __y);
731 // 26.2.3 complex specializations
732 // complex<float> specialization
733 template<> class complex<float>
736 typedef float value_type;
738 complex(float = 0.0f, float = 0.0f);
739 #ifdef _GLIBCXX_BUGGY_COMPLEX
740 complex(const complex& __z) : _M_value(__z._M_value) { }
742 explicit complex(const complex<double>&);
743 explicit complex(const complex<long double>&);
746 const float& real() const;
748 const float& imag() const;
750 complex<float>& operator=(float);
751 complex<float>& operator+=(float);
752 complex<float>& operator-=(float);
753 complex<float>& operator*=(float);
754 complex<float>& operator/=(float);
756 // Let's the compiler synthetize the copy and assignment
757 // operator. It always does a pretty good job.
758 // complex& operator= (const complex&);
759 template<typename _Tp>
760 complex<float>&operator=(const complex<_Tp>&);
761 template<typename _Tp>
762 complex<float>& operator+=(const complex<_Tp>&);
764 complex<float>& operator-=(const complex<_Tp>&);
766 complex<float>& operator*=(const complex<_Tp>&);
768 complex<float>&operator/=(const complex<_Tp>&);
771 typedef __complex__ float _ComplexT;
774 complex(_ComplexT __z) : _M_value(__z) { }
776 friend class complex<double>;
777 friend class complex<long double>;
781 complex<float>::real()
782 { return __real__ _M_value; }
785 complex<float>::real() const
786 { return __real__ _M_value; }
789 complex<float>::imag()
790 { return __imag__ _M_value; }
793 complex<float>::imag() const
794 { return __imag__ _M_value; }
797 complex<float>::complex(float r, float i)
799 __real__ _M_value = r;
800 __imag__ _M_value = i;
803 inline complex<float>&
804 complex<float>::operator=(float __f)
806 __real__ _M_value = __f;
807 __imag__ _M_value = 0.0f;
811 inline complex<float>&
812 complex<float>::operator+=(float __f)
814 __real__ _M_value += __f;
818 inline complex<float>&
819 complex<float>::operator-=(float __f)
821 __real__ _M_value -= __f;
825 inline complex<float>&
826 complex<float>::operator*=(float __f)
832 inline complex<float>&
833 complex<float>::operator/=(float __f)
839 template<typename _Tp>
840 inline complex<float>&
841 complex<float>::operator=(const complex<_Tp>& __z)
843 __real__ _M_value = __z.real();
844 __imag__ _M_value = __z.imag();
848 template<typename _Tp>
849 inline complex<float>&
850 complex<float>::operator+=(const complex<_Tp>& __z)
852 __real__ _M_value += __z.real();
853 __imag__ _M_value += __z.imag();
857 template<typename _Tp>
858 inline complex<float>&
859 complex<float>::operator-=(const complex<_Tp>& __z)
861 __real__ _M_value -= __z.real();
862 __imag__ _M_value -= __z.imag();
866 template<typename _Tp>
867 inline complex<float>&
868 complex<float>::operator*=(const complex<_Tp>& __z)
871 __real__ __t = __z.real();
872 __imag__ __t = __z.imag();
877 template<typename _Tp>
878 inline complex<float>&
879 complex<float>::operator/=(const complex<_Tp>& __z)
882 __real__ __t = __z.real();
883 __imag__ __t = __z.imag();
888 // 26.2.3 complex specializations
889 // complex<double> specialization
890 template<> class complex<double>
893 typedef double value_type;
895 complex(double =0.0, double =0.0);
896 #ifdef _GLIBCXX_BUGGY_COMPLEX
897 complex(const complex& __z) : _M_value(__z._M_value) { }
899 complex(const complex<float>&);
900 explicit complex(const complex<long double>&);
903 const double& real() const;
905 const double& imag() const;
907 complex<double>& operator=(double);
908 complex<double>& operator+=(double);
909 complex<double>& operator-=(double);
910 complex<double>& operator*=(double);
911 complex<double>& operator/=(double);
913 // The compiler will synthetize this, efficiently.
914 // complex& operator= (const complex&);
915 template<typename _Tp>
916 complex<double>& operator=(const complex<_Tp>&);
917 template<typename _Tp>
918 complex<double>& operator+=(const complex<_Tp>&);
919 template<typename _Tp>
920 complex<double>& operator-=(const complex<_Tp>&);
921 template<typename _Tp>
922 complex<double>& operator*=(const complex<_Tp>&);
923 template<typename _Tp>
924 complex<double>& operator/=(const complex<_Tp>&);
927 typedef __complex__ double _ComplexT;
930 complex(_ComplexT __z) : _M_value(__z) { }
932 friend class complex<float>;
933 friend class complex<long double>;
937 complex<double>::real()
938 { return __real__ _M_value; }
941 complex<double>::real() const
942 { return __real__ _M_value; }
945 complex<double>::imag()
946 { return __imag__ _M_value; }
949 complex<double>::imag() const
950 { return __imag__ _M_value; }
953 complex<double>::complex(double __r, double __i)
955 __real__ _M_value = __r;
956 __imag__ _M_value = __i;
959 inline complex<double>&
960 complex<double>::operator=(double __d)
962 __real__ _M_value = __d;
963 __imag__ _M_value = 0.0;
967 inline complex<double>&
968 complex<double>::operator+=(double __d)
970 __real__ _M_value += __d;
974 inline complex<double>&
975 complex<double>::operator-=(double __d)
977 __real__ _M_value -= __d;
981 inline complex<double>&
982 complex<double>::operator*=(double __d)
988 inline complex<double>&
989 complex<double>::operator/=(double __d)
995 template<typename _Tp>
996 inline complex<double>&
997 complex<double>::operator=(const complex<_Tp>& __z)
999 __real__ _M_value = __z.real();
1000 __imag__ _M_value = __z.imag();
1004 template<typename _Tp>
1005 inline complex<double>&
1006 complex<double>::operator+=(const complex<_Tp>& __z)
1008 __real__ _M_value += __z.real();
1009 __imag__ _M_value += __z.imag();
1013 template<typename _Tp>
1014 inline complex<double>&
1015 complex<double>::operator-=(const complex<_Tp>& __z)
1017 __real__ _M_value -= __z.real();
1018 __imag__ _M_value -= __z.imag();
1022 template<typename _Tp>
1023 inline complex<double>&
1024 complex<double>::operator*=(const complex<_Tp>& __z)
1027 __real__ __t = __z.real();
1028 __imag__ __t = __z.imag();
1033 template<typename _Tp>
1034 inline complex<double>&
1035 complex<double>::operator/=(const complex<_Tp>& __z)
1038 __real__ __t = __z.real();
1039 __imag__ __t = __z.imag();
1044 // 26.2.3 complex specializations
1045 // complex<long double> specialization
1046 template<> class complex<long double>
1049 typedef long double value_type;
1051 complex(long double = 0.0L, long double = 0.0L);
1052 #ifdef _GLIBCXX_BUGGY_COMPLEX
1053 complex(const complex& __z) : _M_value(__z._M_value) { }
1055 complex(const complex<float>&);
1056 complex(const complex<double>&);
1058 long double& real();
1059 const long double& real() const;
1060 long double& imag();
1061 const long double& imag() const;
1063 complex<long double>& operator= (long double);
1064 complex<long double>& operator+= (long double);
1065 complex<long double>& operator-= (long double);
1066 complex<long double>& operator*= (long double);
1067 complex<long double>& operator/= (long double);
1069 // The compiler knows how to do this efficiently
1070 // complex& operator= (const complex&);
1071 template<typename _Tp>
1072 complex<long double>& operator=(const complex<_Tp>&);
1073 template<typename _Tp>
1074 complex<long double>& operator+=(const complex<_Tp>&);
1075 template<typename _Tp>
1076 complex<long double>& operator-=(const complex<_Tp>&);
1077 template<typename _Tp>
1078 complex<long double>& operator*=(const complex<_Tp>&);
1079 template<typename _Tp>
1080 complex<long double>& operator/=(const complex<_Tp>&);
1083 typedef __complex__ long double _ComplexT;
1086 complex(_ComplexT __z) : _M_value(__z) { }
1088 friend class complex<float>;
1089 friend class complex<double>;
1093 complex<long double>::complex(long double __r, long double __i)
1095 __real__ _M_value = __r;
1096 __imag__ _M_value = __i;
1100 complex<long double>::real()
1101 { return __real__ _M_value; }
1103 inline const long double&
1104 complex<long double>::real() const
1105 { return __real__ _M_value; }
1108 complex<long double>::imag()
1109 { return __imag__ _M_value; }
1111 inline const long double&
1112 complex<long double>::imag() const
1113 { return __imag__ _M_value; }
1115 inline complex<long double>&
1116 complex<long double>::operator=(long double __r)
1118 __real__ _M_value = __r;
1119 __imag__ _M_value = 0.0L;
1123 inline complex<long double>&
1124 complex<long double>::operator+=(long double __r)
1126 __real__ _M_value += __r;
1130 inline complex<long double>&
1131 complex<long double>::operator-=(long double __r)
1133 __real__ _M_value -= __r;
1137 inline complex<long double>&
1138 complex<long double>::operator*=(long double __r)
1144 inline complex<long double>&
1145 complex<long double>::operator/=(long double __r)
1151 template<typename _Tp>
1152 inline complex<long double>&
1153 complex<long double>::operator=(const complex<_Tp>& __z)
1155 __real__ _M_value = __z.real();
1156 __imag__ _M_value = __z.imag();
1160 template<typename _Tp>
1161 inline complex<long double>&
1162 complex<long double>::operator+=(const complex<_Tp>& __z)
1164 __real__ _M_value += __z.real();
1165 __imag__ _M_value += __z.imag();
1169 template<typename _Tp>
1170 inline complex<long double>&
1171 complex<long double>::operator-=(const complex<_Tp>& __z)
1173 __real__ _M_value -= __z.real();
1174 __imag__ _M_value -= __z.imag();
1178 template<typename _Tp>
1179 inline complex<long double>&
1180 complex<long double>::operator*=(const complex<_Tp>& __z)
1183 __real__ __t = __z.real();
1184 __imag__ __t = __z.imag();
1189 template<typename _Tp>
1190 inline complex<long double>&
1191 complex<long double>::operator/=(const complex<_Tp>& __z)
1194 __real__ __t = __z.real();
1195 __imag__ __t = __z.imag();
1200 // These bits have to be at the end of this file, so that the
1201 // specializations have all been defined.
1202 // ??? No, they have to be there because of compiler limitation at
1203 // inlining. It suffices that class specializations be defined.
1205 complex<float>::complex(const complex<double>& __z)
1206 : _M_value(_ComplexT(__z._M_value)) { }
1209 complex<float>::complex(const complex<long double>& __z)
1210 : _M_value(_ComplexT(__z._M_value)) { }
1213 complex<double>::complex(const complex<float>& __z)
1214 : _M_value(_ComplexT(__z._M_value)) { }
1217 complex<double>::complex(const complex<long double>& __z)
1219 __real__ _M_value = __z.real();
1220 __imag__ _M_value = __z.imag();
1224 complex<long double>::complex(const complex<float>& __z)
1225 : _M_value(_ComplexT(__z._M_value)) { }
1228 complex<long double>::complex(const complex<double>& __z)
1229 : _M_value(_ComplexT(__z._M_value)) { }
1232 #endif /* _GLIBCXX_COMPLEX */