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 template<typename _Tp
> _Tp
abs(const complex<_Tp
>&);
62 template<typename _Tp
> _Tp
arg(const complex<_Tp
>&);
63 template<typename _Tp
> _Tp
norm(const complex<_Tp
>&);
65 template<typename _Tp
> complex<_Tp
> conj(const complex<_Tp
>&);
66 template<typename _Tp
> complex<_Tp
> polar(const _Tp
&, const _Tp
& = 0);
69 template<typename _Tp
> complex<_Tp
> cos(const complex<_Tp
>&);
70 template<typename _Tp
> complex<_Tp
> cosh(const complex<_Tp
>&);
71 template<typename _Tp
> complex<_Tp
> exp(const complex<_Tp
>&);
72 template<typename _Tp
> complex<_Tp
> log(const complex<_Tp
>&);
73 template<typename _Tp
> complex<_Tp
> log10(const complex<_Tp
>&);
74 template<typename _Tp
> complex<_Tp
> pow(const complex<_Tp
>&, int);
75 template<typename _Tp
> complex<_Tp
> pow(const complex<_Tp
>&, const _Tp
&);
76 template<typename _Tp
> complex<_Tp
> pow(const complex<_Tp
>&,
78 template<typename _Tp
> complex<_Tp
> pow(const _Tp
&, const complex<_Tp
>&);
79 template<typename _Tp
> complex<_Tp
> sin(const complex<_Tp
>&);
80 template<typename _Tp
> complex<_Tp
> sinh(const complex<_Tp
>&);
81 template<typename _Tp
> complex<_Tp
> sqrt(const complex<_Tp
>&);
82 template<typename _Tp
> complex<_Tp
> tan(const complex<_Tp
>&);
83 template<typename _Tp
> complex<_Tp
> tanh(const complex<_Tp
>&);
86 // 26.2.2 Primary template class complex
87 template<typename _Tp
>
91 typedef _Tp value_type
;
93 complex(const _Tp
& = _Tp(), const _Tp
& = _Tp());
95 // Let's the compiler synthetize the copy constructor
96 // complex (const complex<_Tp>&);
97 template<typename _Up
>
98 complex(const complex<_Up
>&);
101 const _Tp
& real() const;
103 const _Tp
& imag() const;
105 complex<_Tp
>& operator=(const _Tp
&);
106 complex<_Tp
>& operator+=(const _Tp
&);
107 complex<_Tp
>& operator-=(const _Tp
&);
108 complex<_Tp
>& operator*=(const _Tp
&);
109 complex<_Tp
>& operator/=(const _Tp
&);
111 // Let's the compiler synthetize the
112 // copy and assignment operator
113 // complex<_Tp>& operator= (const complex<_Tp>&);
114 template<typename _Up
>
115 complex<_Tp
>& operator=(const complex<_Up
>&);
116 template<typename _Up
>
117 complex<_Tp
>& operator+=(const complex<_Up
>&);
118 template<typename _Up
>
119 complex<_Tp
>& operator-=(const complex<_Up
>&);
120 template<typename _Up
>
121 complex<_Tp
>& operator*=(const complex<_Up
>&);
122 template<typename _Up
>
123 complex<_Tp
>& operator/=(const complex<_Up
>&);
130 template<typename _Tp
>
132 complex<_Tp
>::real() { return _M_real
; }
134 template<typename _Tp
>
136 complex<_Tp
>::real() const { return _M_real
; }
138 template<typename _Tp
>
140 complex<_Tp
>::imag() { return _M_imag
; }
142 template<typename _Tp
>
144 complex<_Tp
>::imag() const { return _M_imag
; }
146 template<typename _Tp
>
148 complex<_Tp
>::complex(const _Tp
& __r
, const _Tp
& __i
)
149 : _M_real(__r
), _M_imag(__i
) { }
151 template<typename _Tp
>
152 template<typename _Up
>
154 complex<_Tp
>::complex(const complex<_Up
>& __z
)
155 : _M_real(__z
.real()), _M_imag(__z
.imag()) { }
157 template<typename _Tp
>
159 complex<_Tp
>::operator=(const _Tp
& __t
)
167 template<typename _Tp
>
169 complex<_Tp
>::operator+=(const _Tp
& __t
)
176 template<typename _Tp
>
178 complex<_Tp
>::operator-=(const _Tp
& __t
)
185 template<typename _Tp
>
187 complex<_Tp
>::operator*=(const _Tp
& __t
)
195 template<typename _Tp
>
197 complex<_Tp
>::operator/=(const _Tp
& __t
)
204 template<typename _Tp
>
205 template<typename _Up
>
207 complex<_Tp
>::operator=(const complex<_Up
>& __z
)
209 _M_real
= __z
.real();
210 _M_imag
= __z
.imag();
215 template<typename _Tp
>
216 template<typename _Up
>
218 complex<_Tp
>::operator+=(const complex<_Up
>& __z
)
220 _M_real
+= __z
.real();
221 _M_imag
+= __z
.imag();
226 template<typename _Tp
>
227 template<typename _Up
>
229 complex<_Tp
>::operator-=(const complex<_Up
>& __z
)
231 _M_real
-= __z
.real();
232 _M_imag
-= __z
.imag();
237 // XXX: This is a grammar school implementation.
238 template<typename _Tp
>
239 template<typename _Up
>
241 complex<_Tp
>::operator*=(const complex<_Up
>& __z
)
243 const _Tp __r
= _M_real
* __z
.real() - _M_imag
* __z
.imag();
244 _M_imag
= _M_real
* __z
.imag() + _M_imag
* __z
.real();
250 // XXX: This is a grammar school implementation.
251 template<typename _Tp
>
252 template<typename _Up
>
254 complex<_Tp
>::operator/=(const complex<_Up
>& __z
)
256 const _Tp __r
= _M_real
* __z
.real() + _M_imag
* __z
.imag();
257 const _Tp __n
= std::norm(__z
);
258 _M_imag
= (_M_imag
* __z
.real() - _M_real
* __z
.imag()) / __n
;
264 template<typename _Tp
>
266 operator+(const complex<_Tp
>& __x
, const complex<_Tp
>& __y
)
268 complex<_Tp
> __r
= __x
;
273 template<typename _Tp
>
275 operator+(const complex<_Tp
>& __x
, const _Tp
& __y
)
277 complex<_Tp
> __r
= __x
;
282 template<typename _Tp
>
284 operator+(const _Tp
& __x
, const complex<_Tp
>& __y
)
286 complex<_Tp
> __r
= __y
;
291 template<typename _Tp
>
293 operator-(const complex<_Tp
>& __x
, const complex<_Tp
>& __y
)
295 complex<_Tp
> __r
= __x
;
300 template<typename _Tp
>
302 operator-(const complex<_Tp
>& __x
, const _Tp
& __y
)
304 complex<_Tp
> __r
= __x
;
309 template<typename _Tp
>
311 operator-(const _Tp
& __x
, const complex<_Tp
>& __y
)
313 complex<_Tp
> __r(__x
, -__y
.imag());
314 __r
.real() -= __y
.real();
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
;
345 template<typename _Tp
>
347 operator/(const complex<_Tp
>& __x
, const complex<_Tp
>& __y
)
349 complex<_Tp
> __r
= __x
;
354 template<typename _Tp
>
356 operator/(const complex<_Tp
>& __x
, const _Tp
& __y
)
358 complex<_Tp
> __r
= __x
;
363 template<typename _Tp
>
365 operator/(const _Tp
& __x
, const complex<_Tp
>& __y
)
367 complex<_Tp
> __r
= __x
;
372 template<typename _Tp
>
374 operator+(const complex<_Tp
>& __x
)
377 template<typename _Tp
>
379 operator-(const complex<_Tp
>& __x
)
380 { return complex<_Tp
>(-__x
.real(), -__x
.imag()); }
382 template<typename _Tp
>
384 operator==(const complex<_Tp
>& __x
, const complex<_Tp
>& __y
)
385 { return __x
.real() == __y
.real() && __x
.imag() == __y
.imag(); }
387 template<typename _Tp
>
389 operator==(const complex<_Tp
>& __x
, const _Tp
& __y
)
390 { return __x
.real() == __y
&& __x
.imag() == _Tp(); }
392 template<typename _Tp
>
394 operator==(const _Tp
& __x
, const complex<_Tp
>& __y
)
395 { return __x
== __y
.real() && _Tp() == __y
.imag(); }
397 template<typename _Tp
>
399 operator!=(const complex<_Tp
>& __x
, const complex<_Tp
>& __y
)
400 { return __x
.real() != __y
.real() || __x
.imag() != __y
.imag(); }
402 template<typename _Tp
>
404 operator!=(const complex<_Tp
>& __x
, const _Tp
& __y
)
405 { return __x
.real() != __y
|| __x
.imag() != _Tp(); }
407 template<typename _Tp
>
409 operator!=(const _Tp
& __x
, const complex<_Tp
>& __y
)
410 { return __x
!= __y
.real() || _Tp() != __y
.imag(); }
412 template<typename _Tp
, typename _CharT
, class _Traits
>
413 basic_istream
<_CharT
, _Traits
>&
414 operator>>(basic_istream
<_CharT
, _Traits
>& __is
, complex<_Tp
>& __x
)
421 __is
>> __re_x
>> __ch
;
424 __is
>> __im_x
>> __ch
;
426 __x
= complex<_Tp
>(__re_x
, __im_x
);
428 __is
.setstate(ios_base::failbit
);
430 else if (__ch
== ')')
433 __is
.setstate(ios_base::failbit
);
444 template<typename _Tp
, typename _CharT
, class _Traits
>
445 basic_ostream
<_CharT
, _Traits
>&
446 operator<<(basic_ostream
<_CharT
, _Traits
>& __os
, const complex<_Tp
>& __x
)
448 basic_ostringstream
<_CharT
, _Traits
> __s
;
449 __s
.flags(__os
.flags());
450 __s
.imbue(__os
.getloc());
451 __s
.precision(__os
.precision());
452 __s
<< '(' << __x
.real() << ',' << __x
.imag() << ')';
453 return __os
<< __s
.str();
457 template<typename _Tp
>
459 real(complex<_Tp
>& __z
)
460 { return __z
.real(); }
462 template<typename _Tp
>
464 real(const complex<_Tp
>& __z
)
465 { return __z
.real(); }
467 template<typename _Tp
>
469 imag(complex<_Tp
>& __z
)
470 { return __z
.imag(); }
472 template<typename _Tp
>
474 imag(const complex<_Tp
>& __z
)
475 { return __z
.imag(); }
477 template<typename _Tp
>
479 abs(const complex<_Tp
>& __z
)
481 _Tp __x
= __z
.real();
482 _Tp __y
= __z
.imag();
483 const _Tp __s
= std::max(abs(__x
), abs(__y
));
484 if (__s
== _Tp()) // well ...
488 return __s
* sqrt(__x
* __x
+ __y
* __y
);
491 template<typename _Tp
>
493 arg(const complex<_Tp
>& __z
)
494 { return atan2(__z
.imag(), __z
.real()); }
496 // 26.2.7/5: norm(__z) returns the squared magintude of __z.
497 // As defined, norm() is -not- a norm is the common mathematical
498 // sens used in numerics. The helper class _Norm_helper<> tries to
499 // distinguish between builtin floating point and the rest, so as
500 // to deliver an answer as close as possible to the real value.
504 template<typename _Tp
>
505 static inline _Tp
_S_do_it(const complex<_Tp
>& __z
)
507 const _Tp __x
= __z
.real();
508 const _Tp __y
= __z
.imag();
509 return __x
* __x
+ __y
* __y
;
514 struct _Norm_helper
<true>
516 template<typename _Tp
>
517 static inline _Tp
_S_do_it(const complex<_Tp
>& __z
)
519 _Tp __res
= std::abs(__z
);
520 return __res
* __res
;
524 template<typename _Tp
>
526 norm(const complex<_Tp
>& __z
)
528 return _Norm_helper
<__is_floating
<_Tp
>::_M_type
&& !_GLIBCXX_FAST_MATH
>::_S_do_it(__z
);
531 template<typename _Tp
>
533 polar(const _Tp
& __rho
, const _Tp
& __theta
)
534 { return complex<_Tp
>(__rho
* cos(__theta
), __rho
* sin(__theta
)); }
536 template<typename _Tp
>
538 conj(const complex<_Tp
>& __z
)
539 { return complex<_Tp
>(__z
.real(), -__z
.imag()); }
542 template<typename _Tp
>
544 cos(const complex<_Tp
>& __z
)
546 const _Tp __x
= __z
.real();
547 const _Tp __y
= __z
.imag();
548 return complex<_Tp
>(cos(__x
) * cosh(__y
), -sin(__x
) * sinh(__y
));
551 template<typename _Tp
>
553 cosh(const complex<_Tp
>& __z
)
555 const _Tp __x
= __z
.real();
556 const _Tp __y
= __z
.imag();
557 return complex<_Tp
>(cosh(__x
) * cos(__y
), sinh(__x
) * sin(__y
));
560 template<typename _Tp
>
562 exp(const complex<_Tp
>& __z
)
563 { return std::polar(exp(__z
.real()), __z
.imag()); }
565 template<typename _Tp
>
567 log(const complex<_Tp
>& __z
)
568 { return complex<_Tp
>(log(std::abs(__z
)), std::arg(__z
)); }
570 template<typename _Tp
>
572 log10(const complex<_Tp
>& __z
)
573 { return std::log(__z
) / log(_Tp(10.0)); }
575 template<typename _Tp
>
577 sin(const complex<_Tp
>& __z
)
579 const _Tp __x
= __z
.real();
580 const _Tp __y
= __z
.imag();
581 return complex<_Tp
>(sin(__x
) * cosh(__y
), cos(__x
) * sinh(__y
));
584 template<typename _Tp
>
586 sinh(const complex<_Tp
>& __z
)
588 const _Tp __x
= __z
.real();
589 const _Tp __y
= __z
.imag();
590 return complex<_Tp
>(sinh(__x
) * cos(__y
), cosh(__x
) * sin(__y
));
593 template<typename _Tp
>
595 sqrt(const complex<_Tp
>& __z
)
597 _Tp __x
= __z
.real();
598 _Tp __y
= __z
.imag();
602 _Tp __t
= sqrt(abs(__y
) / 2);
603 return complex<_Tp
>(__t
, __y
< _Tp() ? -__t
: __t
);
607 _Tp __t
= sqrt(2 * (std::abs(__z
) + abs(__x
)));
610 ? complex<_Tp
>(__u
, __y
/ __t
)
611 : complex<_Tp
>(abs(__y
) / __t
, __y
< _Tp() ? -__u
: __u
);
615 template<typename _Tp
>
617 tan(const complex<_Tp
>& __z
)
619 return std::sin(__z
) / std::cos(__z
);
622 template<typename _Tp
>
624 tanh(const complex<_Tp
>& __z
)
626 return std::sinh(__z
) / std::cosh(__z
);
629 template<typename _Tp
>
631 pow(const complex<_Tp
>& __z
, int __n
)
633 return std::__pow_helper(__z
, __n
);
636 template<typename _Tp
>
638 pow(const complex<_Tp
>& __x
, const _Tp
& __y
)
640 if (__x
.imag() == _Tp())
641 return pow(__x
.real(), __y
);
643 complex<_Tp
> __t
= log(__x
);
644 return std::polar(exp(__y
* __t
.real()), __y
* __t
.imag());
647 template<typename _Tp
>
649 pow(const complex<_Tp
>& __x
, const complex<_Tp
>& __y
)
651 return __x
== _Tp() ? _Tp() : exp(__y
* log(__x
));
654 template<typename _Tp
>
656 pow(const _Tp
& __x
, const complex<_Tp
>& __y
)
660 : std::polar(pow(__x
, __y
.real()), __y
.imag() * log(__x
));
663 // 26.2.3 complex specializations
664 // complex<float> specialization
665 template<> class complex<float>
668 typedef float value_type
;
670 complex(float = 0.0f
, float = 0.0f
);
671 #ifdef _GLIBCXX_BUGGY_COMPLEX
672 complex(const complex& __z
) : _M_value(__z
._M_value
) { }
674 explicit complex(const complex<double>&);
675 explicit complex(const complex<long double>&);
678 const float& real() const;
680 const float& imag() const;
682 complex<float>& operator=(float);
683 complex<float>& operator+=(float);
684 complex<float>& operator-=(float);
685 complex<float>& operator*=(float);
686 complex<float>& operator/=(float);
688 // Let's the compiler synthetize the copy and assignment
689 // operator. It always does a pretty good job.
690 // complex& operator= (const complex&);
691 template<typename _Tp
>
692 complex<float>&operator=(const complex<_Tp
>&);
693 template<typename _Tp
>
694 complex<float>& operator+=(const complex<_Tp
>&);
696 complex<float>& operator-=(const complex<_Tp
>&);
698 complex<float>& operator*=(const complex<_Tp
>&);
700 complex<float>&operator/=(const complex<_Tp
>&);
703 typedef __complex__
float _ComplexT
;
706 complex(_ComplexT __z
) : _M_value(__z
) { }
708 friend class complex<double>;
709 friend class complex<long double>;
713 complex<float>::real()
714 { return __real__ _M_value
; }
717 complex<float>::real() const
718 { return __real__ _M_value
; }
721 complex<float>::imag()
722 { return __imag__ _M_value
; }
725 complex<float>::imag() const
726 { return __imag__ _M_value
; }
729 complex<float>::complex(float r
, float i
)
731 __real__ _M_value
= r
;
732 __imag__ _M_value
= i
;
735 inline complex<float>&
736 complex<float>::operator=(float __f
)
738 __real__ _M_value
= __f
;
739 __imag__ _M_value
= 0.0f
;
743 inline complex<float>&
744 complex<float>::operator+=(float __f
)
746 __real__ _M_value
+= __f
;
750 inline complex<float>&
751 complex<float>::operator-=(float __f
)
753 __real__ _M_value
-= __f
;
757 inline complex<float>&
758 complex<float>::operator*=(float __f
)
764 inline complex<float>&
765 complex<float>::operator/=(float __f
)
771 template<typename _Tp
>
772 inline complex<float>&
773 complex<float>::operator=(const complex<_Tp
>& __z
)
775 __real__ _M_value
= __z
.real();
776 __imag__ _M_value
= __z
.imag();
780 template<typename _Tp
>
781 inline complex<float>&
782 complex<float>::operator+=(const complex<_Tp
>& __z
)
784 __real__ _M_value
+= __z
.real();
785 __imag__ _M_value
+= __z
.imag();
789 template<typename _Tp
>
790 inline complex<float>&
791 complex<float>::operator-=(const complex<_Tp
>& __z
)
793 __real__ _M_value
-= __z
.real();
794 __imag__ _M_value
-= __z
.imag();
798 template<typename _Tp
>
799 inline complex<float>&
800 complex<float>::operator*=(const complex<_Tp
>& __z
)
803 __real__ __t
= __z
.real();
804 __imag__ __t
= __z
.imag();
809 template<typename _Tp
>
810 inline complex<float>&
811 complex<float>::operator/=(const complex<_Tp
>& __z
)
814 __real__ __t
= __z
.real();
815 __imag__ __t
= __z
.imag();
820 // 26.2.3 complex specializations
821 // complex<double> specialization
822 template<> class complex<double>
825 typedef double value_type
;
827 complex(double =0.0, double =0.0);
828 #ifdef _GLIBCXX_BUGGY_COMPLEX
829 complex(const complex& __z
) : _M_value(__z
._M_value
) { }
831 complex(const complex<float>&);
832 explicit complex(const complex<long double>&);
835 const double& real() const;
837 const double& imag() const;
839 complex<double>& operator=(double);
840 complex<double>& operator+=(double);
841 complex<double>& operator-=(double);
842 complex<double>& operator*=(double);
843 complex<double>& operator/=(double);
845 // The compiler will synthetize this, efficiently.
846 // complex& operator= (const complex&);
847 template<typename _Tp
>
848 complex<double>& operator=(const complex<_Tp
>&);
849 template<typename _Tp
>
850 complex<double>& operator+=(const complex<_Tp
>&);
851 template<typename _Tp
>
852 complex<double>& operator-=(const complex<_Tp
>&);
853 template<typename _Tp
>
854 complex<double>& operator*=(const complex<_Tp
>&);
855 template<typename _Tp
>
856 complex<double>& operator/=(const complex<_Tp
>&);
859 typedef __complex__
double _ComplexT
;
862 complex(_ComplexT __z
) : _M_value(__z
) { }
864 friend class complex<float>;
865 friend class complex<long double>;
869 complex<double>::real()
870 { return __real__ _M_value
; }
873 complex<double>::real() const
874 { return __real__ _M_value
; }
877 complex<double>::imag()
878 { return __imag__ _M_value
; }
881 complex<double>::imag() const
882 { return __imag__ _M_value
; }
885 complex<double>::complex(double __r
, double __i
)
887 __real__ _M_value
= __r
;
888 __imag__ _M_value
= __i
;
891 inline complex<double>&
892 complex<double>::operator=(double __d
)
894 __real__ _M_value
= __d
;
895 __imag__ _M_value
= 0.0;
899 inline complex<double>&
900 complex<double>::operator+=(double __d
)
902 __real__ _M_value
+= __d
;
906 inline complex<double>&
907 complex<double>::operator-=(double __d
)
909 __real__ _M_value
-= __d
;
913 inline complex<double>&
914 complex<double>::operator*=(double __d
)
920 inline complex<double>&
921 complex<double>::operator/=(double __d
)
927 template<typename _Tp
>
928 inline complex<double>&
929 complex<double>::operator=(const complex<_Tp
>& __z
)
931 __real__ _M_value
= __z
.real();
932 __imag__ _M_value
= __z
.imag();
936 template<typename _Tp
>
937 inline complex<double>&
938 complex<double>::operator+=(const complex<_Tp
>& __z
)
940 __real__ _M_value
+= __z
.real();
941 __imag__ _M_value
+= __z
.imag();
945 template<typename _Tp
>
946 inline complex<double>&
947 complex<double>::operator-=(const complex<_Tp
>& __z
)
949 __real__ _M_value
-= __z
.real();
950 __imag__ _M_value
-= __z
.imag();
954 template<typename _Tp
>
955 inline complex<double>&
956 complex<double>::operator*=(const complex<_Tp
>& __z
)
959 __real__ __t
= __z
.real();
960 __imag__ __t
= __z
.imag();
965 template<typename _Tp
>
966 inline complex<double>&
967 complex<double>::operator/=(const complex<_Tp
>& __z
)
970 __real__ __t
= __z
.real();
971 __imag__ __t
= __z
.imag();
976 // 26.2.3 complex specializations
977 // complex<long double> specialization
978 template<> class complex<long double>
981 typedef long double value_type
;
983 complex(long double = 0.0L, long double = 0.0L);
984 #ifdef _GLIBCXX_BUGGY_COMPLEX
985 complex(const complex& __z
) : _M_value(__z
._M_value
) { }
987 complex(const complex<float>&);
988 complex(const complex<double>&);
991 const long double& real() const;
993 const long double& imag() const;
995 complex<long double>& operator= (long double);
996 complex<long double>& operator+= (long double);
997 complex<long double>& operator-= (long double);
998 complex<long double>& operator*= (long double);
999 complex<long double>& operator/= (long double);
1001 // The compiler knows how to do this efficiently
1002 // complex& operator= (const complex&);
1003 template<typename _Tp
>
1004 complex<long double>& operator=(const complex<_Tp
>&);
1005 template<typename _Tp
>
1006 complex<long double>& operator+=(const complex<_Tp
>&);
1007 template<typename _Tp
>
1008 complex<long double>& operator-=(const complex<_Tp
>&);
1009 template<typename _Tp
>
1010 complex<long double>& operator*=(const complex<_Tp
>&);
1011 template<typename _Tp
>
1012 complex<long double>& operator/=(const complex<_Tp
>&);
1015 typedef __complex__
long double _ComplexT
;
1018 complex(_ComplexT __z
) : _M_value(__z
) { }
1020 friend class complex<float>;
1021 friend class complex<double>;
1025 complex<long double>::complex(long double __r
, long double __i
)
1027 __real__ _M_value
= __r
;
1028 __imag__ _M_value
= __i
;
1032 complex<long double>::real()
1033 { return __real__ _M_value
; }
1035 inline const long double&
1036 complex<long double>::real() const
1037 { return __real__ _M_value
; }
1040 complex<long double>::imag()
1041 { return __imag__ _M_value
; }
1043 inline const long double&
1044 complex<long double>::imag() const
1045 { return __imag__ _M_value
; }
1047 inline complex<long double>&
1048 complex<long double>::operator=(long double __r
)
1050 __real__ _M_value
= __r
;
1051 __imag__ _M_value
= 0.0L;
1055 inline complex<long double>&
1056 complex<long double>::operator+=(long double __r
)
1058 __real__ _M_value
+= __r
;
1062 inline complex<long double>&
1063 complex<long double>::operator-=(long double __r
)
1065 __real__ _M_value
-= __r
;
1069 inline complex<long double>&
1070 complex<long double>::operator*=(long double __r
)
1076 inline complex<long double>&
1077 complex<long double>::operator/=(long double __r
)
1083 template<typename _Tp
>
1084 inline complex<long double>&
1085 complex<long double>::operator=(const complex<_Tp
>& __z
)
1087 __real__ _M_value
= __z
.real();
1088 __imag__ _M_value
= __z
.imag();
1092 template<typename _Tp
>
1093 inline complex<long double>&
1094 complex<long double>::operator+=(const complex<_Tp
>& __z
)
1096 __real__ _M_value
+= __z
.real();
1097 __imag__ _M_value
+= __z
.imag();
1101 template<typename _Tp
>
1102 inline complex<long double>&
1103 complex<long double>::operator-=(const complex<_Tp
>& __z
)
1105 __real__ _M_value
-= __z
.real();
1106 __imag__ _M_value
-= __z
.imag();
1110 template<typename _Tp
>
1111 inline complex<long double>&
1112 complex<long double>::operator*=(const complex<_Tp
>& __z
)
1115 __real__ __t
= __z
.real();
1116 __imag__ __t
= __z
.imag();
1121 template<typename _Tp
>
1122 inline complex<long double>&
1123 complex<long double>::operator/=(const complex<_Tp
>& __z
)
1126 __real__ __t
= __z
.real();
1127 __imag__ __t
= __z
.imag();
1132 // These bits have to be at the end of this file, so that the
1133 // specializations have all been defined.
1134 // ??? No, they have to be there because of compiler limitation at
1135 // inlining. It suffices that class specializations be defined.
1137 complex<float>::complex(const complex<double>& __z
)
1138 : _M_value(_ComplexT(__z
._M_value
)) { }
1141 complex<float>::complex(const complex<long double>& __z
)
1142 : _M_value(_ComplexT(__z
._M_value
)) { }
1145 complex<double>::complex(const complex<float>& __z
)
1146 : _M_value(_ComplexT(__z
._M_value
)) { }
1149 complex<double>::complex(const complex<long double>& __z
)
1151 __real__ _M_value
= __z
.real();
1152 __imag__ _M_value
= __z
.imag();
1156 complex<long double>::complex(const complex<float>& __z
)
1157 : _M_value(_ComplexT(__z
._M_value
)) { }
1160 complex<long double>::complex(const complex<double>& __z
)
1161 : _M_value(_ComplexT(__z
._M_value
)) { }
1164 #endif /* _GLIBCXX_COMPLEX */