2002-01-11 Phil Edwards <pme@gcc.gnu.org>
[official-gcc.git] / libstdc++-v3 / include / std / std_complex.h
blob18dd8675823767cfefef5b2e03869f7a54e6ea59
1 // The template and inlines for the -*- C++ -*- complex number classes.
3 // Copyright (C) 1997, 1998, 1999, 2000, 2001 Free Software Foundation, Inc.
4 //
5 // This file is part of the GNU ISO C++ Library. This library is free
6 // software; you can redistribute it and/or modify it under the
7 // terms of the GNU General Public License as published by the
8 // Free Software Foundation; either version 2, or (at your option)
9 // any later version.
11 // This library is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 // GNU General Public License for more details.
16 // You should have received a copy of the GNU General Public License along
17 // with this library; see the file COPYING. If not, write to the Free
18 // Software Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307,
19 // USA.
21 // As a special exception, you may use this file as part of a free software
22 // library without restriction. Specifically, if other files instantiate
23 // templates or use macros or inline functions from this file, or you compile
24 // this file and link it with other files to produce an executable, this
25 // file does not by itself cause the resulting executable to be covered by
26 // the GNU General Public License. This exception does not however
27 // invalidate any other reasons why the executable file might be covered by
28 // the GNU General Public License.
31 // ISO C++ 14882: 26.2 Complex Numbers
32 // Note: this is not a conforming implementation.
33 // Initially implemented by Ulrich Drepper <drepper@cygnus.com>
34 // Improved by Gabriel Dos Reis <dosreis@cmla.ens-cachan.fr>
37 /** @file std_complex.h
38 * This is an internal header file, included by other library headers.
39 * You should not attempt to use it directly.
42 #ifndef _CPP_COMPLEX
43 #define _CPP_COMPLEX 1
45 #pragma GCC system_header
47 #include <bits/c++config.h>
48 #include <bits/cpp_type_traits.h>
49 #include <cmath>
50 #include <sstream>
52 namespace std
54 // Forward declarations
55 template<typename _Tp> class complex;
56 template<> class complex<float>;
57 template<> class complex<double>;
58 template<> class complex<long double>;
60 template<typename _Tp> _Tp abs(const complex<_Tp>&);
61 template<typename _Tp> _Tp arg(const complex<_Tp>&);
62 template<typename _Tp> _Tp norm(const complex<_Tp>&);
64 template<typename _Tp> complex<_Tp> conj(const complex<_Tp>&);
65 template<typename _Tp> complex<_Tp> polar(const _Tp&, const _Tp& = 0);
67 // Transcendentals:
68 template<typename _Tp> complex<_Tp> cos(const complex<_Tp>&);
69 template<typename _Tp> complex<_Tp> cosh(const complex<_Tp>&);
70 template<typename _Tp> complex<_Tp> exp(const complex<_Tp>&);
71 template<typename _Tp> complex<_Tp> log(const complex<_Tp>&);
72 template<typename _Tp> complex<_Tp> log10(const complex<_Tp>&);
73 template<typename _Tp> complex<_Tp> pow(const complex<_Tp>&, int);
74 template<typename _Tp> complex<_Tp> pow(const complex<_Tp>&, const _Tp&);
75 template<typename _Tp> complex<_Tp> pow(const complex<_Tp>&,
76 const complex<_Tp>&);
77 template<typename _Tp> complex<_Tp> pow(const _Tp&, const complex<_Tp>&);
78 template<typename _Tp> complex<_Tp> sin(const complex<_Tp>&);
79 template<typename _Tp> complex<_Tp> sinh(const complex<_Tp>&);
80 template<typename _Tp> complex<_Tp> sqrt(const complex<_Tp>&);
81 template<typename _Tp> complex<_Tp> tan(const complex<_Tp>&);
82 template<typename _Tp> complex<_Tp> tanh(const complex<_Tp>&);
85 // 26.2.2 Primary template class complex
86 template<typename _Tp>
87 class complex
89 public:
90 typedef _Tp value_type;
92 complex(const _Tp& = _Tp(), const _Tp & = _Tp());
94 // Let's the compiler synthetize the copy constructor
95 // complex (const complex<_Tp>&);
96 template<typename _Up>
97 complex(const complex<_Up>&);
99 _Tp real() const;
100 _Tp imag() const;
102 complex<_Tp>& operator=(const _Tp&);
103 complex<_Tp>& operator+=(const _Tp&);
104 complex<_Tp>& operator-=(const _Tp&);
105 complex<_Tp>& operator*=(const _Tp&);
106 complex<_Tp>& operator/=(const _Tp&);
108 // Let's the compiler synthetize the
109 // copy and assignment operator
110 // complex<_Tp>& operator= (const complex<_Tp>&);
111 template<typename _Up>
112 complex<_Tp>& operator=(const complex<_Up>&);
113 template<typename _Up>
114 complex<_Tp>& operator+=(const complex<_Up>&);
115 template<typename _Up>
116 complex<_Tp>& operator-=(const complex<_Up>&);
117 template<typename _Up>
118 complex<_Tp>& operator*=(const complex<_Up>&);
119 template<typename _Up>
120 complex<_Tp>& operator/=(const complex<_Up>&);
122 private:
123 _Tp _M_real, _M_imag;
126 template<typename _Tp>
127 inline _Tp
128 complex<_Tp>::real() const { return _M_real; }
130 template<typename _Tp>
131 inline _Tp
132 complex<_Tp>::imag() const { return _M_imag; }
134 template<typename _Tp>
135 inline
136 complex<_Tp>::complex(const _Tp& __r, const _Tp& __i)
137 : _M_real(__r), _M_imag(__i) { }
139 template<typename _Tp>
140 template<typename _Up>
141 inline
142 complex<_Tp>::complex(const complex<_Up>& __z)
143 : _M_real(__z.real()), _M_imag(__z.imag()) { }
145 template<typename _Tp>
146 complex<_Tp>&
147 complex<_Tp>::operator=(const _Tp& __t)
149 _M_real = __t;
150 _M_imag = _Tp();
151 return *this;
154 // 26.2.5/1
155 template<typename _Tp>
156 inline complex<_Tp>&
157 complex<_Tp>::operator+=(const _Tp& __t)
159 _M_real += __t;
160 return *this;
163 // 26.2.5/3
164 template<typename _Tp>
165 inline complex<_Tp>&
166 complex<_Tp>::operator-=(const _Tp& __t)
168 _M_real -= __t;
169 return *this;
172 // 26.2.5/5
173 template<typename _Tp>
174 complex<_Tp>&
175 complex<_Tp>::operator*=(const _Tp& __t)
177 _M_real *= __t;
178 _M_imag *= __t;
179 return *this;
182 // 26.2.5/7
183 template<typename _Tp>
184 complex<_Tp>&
185 complex<_Tp>::operator/=(const _Tp& __t)
187 _M_real /= __t;
188 _M_imag /= __t;
189 return *this;
192 template<typename _Tp>
193 template<typename _Up>
194 complex<_Tp>&
195 complex<_Tp>::operator=(const complex<_Up>& __z)
197 _M_real = __z.real();
198 _M_imag = __z.imag();
199 return *this;
202 // 26.2.5/9
203 template<typename _Tp>
204 template<typename _Up>
205 complex<_Tp>&
206 complex<_Tp>::operator+=(const complex<_Up>& __z)
208 _M_real += __z.real();
209 _M_imag += __z.imag();
210 return *this;
213 // 26.2.5/11
214 template<typename _Tp>
215 template<typename _Up>
216 complex<_Tp>&
217 complex<_Tp>::operator-=(const complex<_Up>& __z)
219 _M_real -= __z.real();
220 _M_imag -= __z.imag();
221 return *this;
224 // 26.2.5/13
225 // XXX: This is a grammar school implementation.
226 template<typename _Tp>
227 template<typename _Up>
228 complex<_Tp>&
229 complex<_Tp>::operator*=(const complex<_Up>& __z)
231 const _Tp __r = _M_real * __z.real() - _M_imag * __z.imag();
232 _M_imag = _M_real * __z.imag() + _M_imag * __z.real();
233 _M_real = __r;
234 return *this;
237 // 26.2.5/15
238 // XXX: This is a grammar school implementation.
239 template<typename _Tp>
240 template<typename _Up>
241 complex<_Tp>&
242 complex<_Tp>::operator/=(const complex<_Up>& __z)
244 const _Tp __r = _M_real * __z.real() + _M_imag * __z.imag();
245 const _Tp __n = norm(__z);
246 _M_imag = (_M_imag * __z.real() - _M_real * __z.imag()) / __n;
247 _M_real = __r / __n;
248 return *this;
251 // Operators:
252 template<typename _Tp>
253 inline complex<_Tp>
254 operator+(const complex<_Tp>& __x, const complex<_Tp>& __y)
255 { return complex<_Tp> (__x) += __y; }
257 template<typename _Tp>
258 inline complex<_Tp>
259 operator+(const complex<_Tp>& __x, const _Tp& __y)
260 { return complex<_Tp> (__x) += __y; }
262 template<typename _Tp>
263 inline complex<_Tp>
264 operator+(const _Tp& __x, const complex<_Tp>& __y)
265 { return complex<_Tp> (__y) += __x; }
267 template<typename _Tp>
268 inline complex<_Tp>
269 operator-(const complex<_Tp>& __x, const complex<_Tp>& __y)
270 { return complex<_Tp> (__x) -= __y; }
272 template<typename _Tp>
273 inline complex<_Tp>
274 operator-(const complex<_Tp>& __x, const _Tp& __y)
275 { return complex<_Tp> (__x) -= __y; }
277 template<typename _Tp>
278 inline complex<_Tp>
279 operator-(const _Tp& __x, const complex<_Tp>& __y)
280 { return complex<_Tp> (__x) -= __y; }
282 template<typename _Tp>
283 inline complex<_Tp>
284 operator*(const complex<_Tp>& __x, const complex<_Tp>& __y)
285 { return complex<_Tp> (__x) *= __y; }
287 template<typename _Tp>
288 inline complex<_Tp>
289 operator*(const complex<_Tp>& __x, const _Tp& __y)
290 { return complex<_Tp> (__x) *= __y; }
292 template<typename _Tp>
293 inline complex<_Tp>
294 operator*(const _Tp& __x, const complex<_Tp>& __y)
295 { return complex<_Tp> (__y) *= __x; }
297 template<typename _Tp>
298 inline complex<_Tp>
299 operator/(const complex<_Tp>& __x, const complex<_Tp>& __y)
300 { return complex<_Tp> (__x) /= __y; }
302 template<typename _Tp>
303 inline complex<_Tp>
304 operator/(const complex<_Tp>& __x, const _Tp& __y)
305 { return complex<_Tp> (__x) /= __y; }
307 template<typename _Tp>
308 inline complex<_Tp>
309 operator/(const _Tp& __x, const complex<_Tp>& __y)
310 { return complex<_Tp> (__x) /= __y; }
312 template<typename _Tp>
313 inline complex<_Tp>
314 operator+(const complex<_Tp>& __x)
315 { return __x; }
317 template<typename _Tp>
318 inline complex<_Tp>
319 operator-(const complex<_Tp>& __x)
320 { return complex<_Tp>(-__x.real(), -__x.imag()); }
322 template<typename _Tp>
323 inline bool
324 operator==(const complex<_Tp>& __x, const complex<_Tp>& __y)
325 { return __x.real() == __y.real() && __x.imag() == __y.imag(); }
327 template<typename _Tp>
328 inline bool
329 operator==(const complex<_Tp>& __x, const _Tp& __y)
330 { return __x.real() == __y && __x.imag() == _Tp(); }
332 template<typename _Tp>
333 inline bool
334 operator==(const _Tp& __x, const complex<_Tp>& __y)
335 { return __x == __y.real() && _Tp() == __y.imag(); }
337 template<typename _Tp>
338 inline bool
339 operator!=(const complex<_Tp>& __x, const complex<_Tp>& __y)
340 { return __x.real() != __y.real() || __x.imag() != __y.imag(); }
342 template<typename _Tp>
343 inline bool
344 operator!=(const complex<_Tp>& __x, const _Tp& __y)
345 { return __x.real() != __y || __x.imag() != _Tp(); }
347 template<typename _Tp>
348 inline bool
349 operator!=(const _Tp& __x, const complex<_Tp>& __y)
350 { return __x != __y.real() || _Tp() != __y.imag(); }
352 template<typename _Tp, typename _CharT, class _Traits>
353 basic_istream<_CharT, _Traits>&
354 operator>>(basic_istream<_CharT, _Traits>& __is, complex<_Tp>& __x)
356 _Tp __re_x, __im_x;
357 _CharT __ch;
358 __is >> __ch;
359 if (__ch == '(')
361 __is >> __re_x >> __ch;
362 if (__ch == ',')
364 __is >> __im_x >> __ch;
365 if (__ch == ')')
366 __x = complex<_Tp>(__re_x, __im_x);
367 else
368 __is.setstate(ios_base::failbit);
370 else if (__ch == ')')
371 __x = complex<_Tp>(__re_x, _Tp(0));
372 else
373 __is.setstate(ios_base::failbit);
375 else
377 __is.putback(__ch);
378 __is >> __re_x;
379 __x = complex<_Tp>(__re_x, _Tp(0));
381 return __is;
384 template<typename _Tp, typename _CharT, class _Traits>
385 basic_ostream<_CharT, _Traits>&
386 operator<<(basic_ostream<_CharT, _Traits>& __os, const complex<_Tp>& __x)
388 basic_ostringstream<_CharT, _Traits> __s;
389 __s.flags(__os.flags());
390 __s.imbue(__os.getloc());
391 __s.precision(__os.precision());
392 __s << '(' << __x.real() << "," << __x.imag() << ')';
393 return __os << __s.str();
396 // Values
397 template<typename _Tp>
398 inline _Tp
399 real(const complex<_Tp>& __z)
400 { return __z.real(); }
402 template<typename _Tp>
403 inline _Tp
404 imag(const complex<_Tp>& __z)
405 { return __z.imag(); }
407 template<typename _Tp>
408 inline _Tp
409 abs(const complex<_Tp>& __z)
411 _Tp __x = __z.real();
412 _Tp __y = __z.imag();
413 const _Tp __s = max(abs(__x), abs(__y));
414 if (__s == _Tp()) // well ...
415 return __s;
416 __x /= __s;
417 __y /= __s;
418 return __s * sqrt(__x * __x + __y * __y);
421 template<typename _Tp>
422 inline _Tp
423 arg(const complex<_Tp>& __z)
424 { return atan2(__z.imag(), __z.real()); }
426 // 26.2.7/5: norm(__z) returns the squared magintude of __z.
427 // As defined, norm() is -not- a norm is the common mathematical
428 // sens used in numerics. The helper class _Norm_helper<> tries to
429 // distinguish between builtin floating point and the rest, so as
430 // to deliver an answer as close as possible to the real value.
431 template<bool>
432 struct _Norm_helper
434 template<typename _Tp>
435 static inline _Tp _S_do_it(const complex<_Tp>& __z)
437 const _Tp __x = __z.real();
438 const _Tp __y = __z.imag();
439 return __x * __x + __y * __y;
443 template<>
444 struct _Norm_helper<true>
446 template<typename _Tp>
447 static inline _Tp _S_do_it(const complex<_Tp>& __z)
449 _Tp __res = abs(__z);
450 return __res * __res;
454 template<typename _Tp>
455 inline _Tp
456 norm(const complex<_Tp>& __z)
458 return _Norm_helper<__is_floating<_Tp>::_M_type>::_S_do_it(__z);
461 template<typename _Tp>
462 inline complex<_Tp>
463 polar(const _Tp& __rho, const _Tp& __theta)
464 { return complex<_Tp>(__rho * cos(__theta), __rho * sin(__theta)); }
466 template<typename _Tp>
467 inline complex<_Tp>
468 conj(const complex<_Tp>& __z)
469 { return complex<_Tp>(__z.real(), -__z.imag()); }
471 // Transcendentals
472 template<typename _Tp>
473 inline complex<_Tp>
474 cos(const complex<_Tp>& __z)
476 const _Tp __x = __z.real();
477 const _Tp __y = __z.imag();
478 return complex<_Tp>(cos(__x) * cosh(__y), -sin(__x) * sinh(__y));
481 template<typename _Tp>
482 inline complex<_Tp>
483 cosh(const complex<_Tp>& __z)
485 const _Tp __x = __z.real();
486 const _Tp __y = __z.imag();
487 return complex<_Tp>(cosh(__x) * cos(__y), sinh(__x) * sin(__y));
490 template<typename _Tp>
491 inline complex<_Tp>
492 exp(const complex<_Tp>& __z)
493 { return polar(exp(__z.real()), __z.imag()); }
495 template<typename _Tp>
496 inline complex<_Tp>
497 log(const complex<_Tp>& __z)
498 { return complex<_Tp>(log(abs(__z)), arg(__z)); }
500 template<typename _Tp>
501 inline complex<_Tp>
502 log10(const complex<_Tp>& __z)
503 { return log(__z) / log(_Tp(10.0)); }
505 template<typename _Tp>
506 inline complex<_Tp>
507 sin(const complex<_Tp>& __z)
509 const _Tp __x = __z.real();
510 const _Tp __y = __z.imag();
511 return complex<_Tp>(sin(__x) * cosh(__y), cos(__x) * sinh(__y));
514 template<typename _Tp>
515 inline complex<_Tp>
516 sinh(const complex<_Tp>& __z)
518 const _Tp __x = __z.real();
519 const _Tp __y = __z.imag();
520 return complex<_Tp>(sinh(__x) * cos(__y), cosh(__x) * sin(__y));
523 template<typename _Tp>
524 complex<_Tp>
525 sqrt(const complex<_Tp>& __z)
527 _Tp __x = __z.real();
528 _Tp __y = __z.imag();
530 if (__x == _Tp())
532 _Tp __t = sqrt(abs(__y) / 2);
533 return complex<_Tp>(__t, __y < _Tp() ? -__t : __t);
535 else
537 _Tp __t = sqrt(2 * (abs(__z) + abs(__x)));
538 _Tp __u = __t / 2;
539 return __x > _Tp()
540 ? complex<_Tp>(__u, __y / __t)
541 : complex<_Tp>(abs(__y) / __t, __y < _Tp() ? -__u : __u);
545 template<typename _Tp>
546 inline complex<_Tp>
547 tan(const complex<_Tp>& __z)
549 return sin(__z) / cos(__z);
552 template<typename _Tp>
553 inline complex<_Tp>
554 tanh(const complex<_Tp>& __z)
556 return sinh(__z) / cosh(__z);
559 template<typename _Tp>
560 inline complex<_Tp>
561 pow(const complex<_Tp>& __z, int __n)
563 return __pow_helper(__z, __n);
566 template<typename _Tp>
567 inline complex<_Tp>
568 pow(const complex<_Tp>& __x, const _Tp& __y)
570 return exp(__y * log(__x));
573 template<typename _Tp>
574 inline complex<_Tp>
575 pow(const complex<_Tp>& __x, const complex<_Tp>& __y)
577 return exp(__y * log(__x));
580 template<typename _Tp>
581 inline complex<_Tp>
582 pow(const _Tp& __x, const complex<_Tp>& __y)
584 return exp(__y * log(__x));
587 // 26.2.3 complex specializations
588 // complex<float> specialization
589 template<> class complex<float>
591 public:
592 typedef float value_type;
594 complex(float = 0.0f, float = 0.0f);
595 #ifdef _GLIBCPP_BUGGY_COMPLEX
596 complex(const complex& __z) : _M_value(__z._M_value) { }
597 #endif
598 explicit complex(const complex<double>&);
599 explicit complex(const complex<long double>&);
601 float real() const;
602 float imag() const;
604 complex<float>& operator=(float);
605 complex<float>& operator+=(float);
606 complex<float>& operator-=(float);
607 complex<float>& operator*=(float);
608 complex<float>& operator/=(float);
610 // Let's the compiler synthetize the copy and assignment
611 // operator. It always does a pretty good job.
612 // complex& operator= (const complex&);
613 template<typename _Tp>
614 complex<float>&operator=(const complex<_Tp>&);
615 template<typename _Tp>
616 complex<float>& operator+=(const complex<_Tp>&);
617 template<class _Tp>
618 complex<float>& operator-=(const complex<_Tp>&);
619 template<class _Tp>
620 complex<float>& operator*=(const complex<_Tp>&);
621 template<class _Tp>
622 complex<float>&operator/=(const complex<_Tp>&);
624 private:
625 typedef __complex__ float _ComplexT;
626 _ComplexT _M_value;
628 complex(_ComplexT __z) : _M_value(__z) { }
630 friend class complex<double>;
631 friend class complex<long double>;
634 inline float
635 complex<float>::real() const
636 { return __real__ _M_value; }
638 inline float
639 complex<float>::imag() const
640 { return __imag__ _M_value; }
642 inline
643 complex<float>::complex(float r, float i)
645 __real__ _M_value = r;
646 __imag__ _M_value = i;
649 inline complex<float>&
650 complex<float>::operator=(float __f)
652 __real__ _M_value = __f;
653 __imag__ _M_value = 0.0f;
654 return *this;
657 inline complex<float>&
658 complex<float>::operator+=(float __f)
660 __real__ _M_value += __f;
661 return *this;
664 inline complex<float>&
665 complex<float>::operator-=(float __f)
667 __real__ _M_value -= __f;
668 return *this;
671 inline complex<float>&
672 complex<float>::operator*=(float __f)
674 _M_value *= __f;
675 return *this;
678 inline complex<float>&
679 complex<float>::operator/=(float __f)
681 _M_value /= __f;
682 return *this;
685 template<typename _Tp>
686 inline complex<float>&
687 complex<float>::operator=(const complex<_Tp>& __z)
689 __real__ _M_value = __z.real();
690 __imag__ _M_value = __z.imag();
691 return *this;
694 template<typename _Tp>
695 inline complex<float>&
696 complex<float>::operator+=(const complex<_Tp>& __z)
698 __real__ _M_value += __z.real();
699 __imag__ _M_value += __z.imag();
700 return *this;
703 template<typename _Tp>
704 inline complex<float>&
705 complex<float>::operator-=(const complex<_Tp>& __z)
707 __real__ _M_value -= __z.real();
708 __imag__ _M_value -= __z.imag();
709 return *this;
712 template<typename _Tp>
713 inline complex<float>&
714 complex<float>::operator*=(const complex<_Tp>& __z)
716 _ComplexT __t;
717 __real__ __t = __z.real();
718 __imag__ __t = __z.imag();
719 _M_value *= __t;
720 return *this;
723 template<typename _Tp>
724 inline complex<float>&
725 complex<float>::operator/=(const complex<_Tp>& __z)
727 _ComplexT __t;
728 __real__ __t = __z.real();
729 __imag__ __t = __z.imag();
730 _M_value /= __t;
731 return *this;
734 // 26.2.3 complex specializations
735 // complex<double> specialization
736 template<> class complex<double>
738 public:
739 typedef double value_type;
741 complex(double =0.0, double =0.0);
742 #ifdef _GLIBCPP_BUGGY_COMPLEX
743 complex(const complex& __z) : _M_value(__z._M_value) { }
744 #endif
745 complex(const complex<float>&);
746 explicit complex(const complex<long double>&);
748 double real() const;
749 double imag() const;
751 complex<double>& operator=(double);
752 complex<double>& operator+=(double);
753 complex<double>& operator-=(double);
754 complex<double>& operator*=(double);
755 complex<double>& operator/=(double);
757 // The compiler will synthetize this, efficiently.
758 // complex& operator= (const complex&);
759 template<typename _Tp>
760 complex<double>& operator=(const complex<_Tp>&);
761 template<typename _Tp>
762 complex<double>& operator+=(const complex<_Tp>&);
763 template<typename _Tp>
764 complex<double>& operator-=(const complex<_Tp>&);
765 template<typename _Tp>
766 complex<double>& operator*=(const complex<_Tp>&);
767 template<typename _Tp>
768 complex<double>& operator/=(const complex<_Tp>&);
770 private:
771 typedef __complex__ double _ComplexT;
772 _ComplexT _M_value;
774 complex(_ComplexT __z) : _M_value(__z) { }
776 friend class complex<float>;
777 friend class complex<long double>;
780 inline double
781 complex<double>::real() const
782 { return __real__ _M_value; }
784 inline double
785 complex<double>::imag() const
786 { return __imag__ _M_value; }
788 inline
789 complex<double>::complex(double __r, double __i)
791 __real__ _M_value = __r;
792 __imag__ _M_value = __i;
795 inline complex<double>&
796 complex<double>::operator=(double __d)
798 __real__ _M_value = __d;
799 __imag__ _M_value = 0.0;
800 return *this;
803 inline complex<double>&
804 complex<double>::operator+=(double __d)
806 __real__ _M_value += __d;
807 return *this;
810 inline complex<double>&
811 complex<double>::operator-=(double __d)
813 __real__ _M_value -= __d;
814 return *this;
817 inline complex<double>&
818 complex<double>::operator*=(double __d)
820 _M_value *= __d;
821 return *this;
824 inline complex<double>&
825 complex<double>::operator/=(double __d)
827 _M_value /= __d;
828 return *this;
831 template<typename _Tp>
832 inline complex<double>&
833 complex<double>::operator=(const complex<_Tp>& __z)
835 __real__ _M_value = __z.real();
836 __imag__ _M_value = __z.imag();
837 return *this;
840 template<typename _Tp>
841 inline complex<double>&
842 complex<double>::operator+=(const complex<_Tp>& __z)
844 __real__ _M_value += __z.real();
845 __imag__ _M_value += __z.imag();
846 return *this;
849 template<typename _Tp>
850 inline complex<double>&
851 complex<double>::operator-=(const complex<_Tp>& __z)
853 __real__ _M_value -= __z.real();
854 __imag__ _M_value -= __z.imag();
855 return *this;
858 template<typename _Tp>
859 inline complex<double>&
860 complex<double>::operator*=(const complex<_Tp>& __z)
862 _ComplexT __t;
863 __real__ __t = __z.real();
864 __imag__ __t = __z.imag();
865 _M_value *= __t;
866 return *this;
869 template<typename _Tp>
870 inline complex<double>&
871 complex<double>::operator/=(const complex<_Tp>& __z)
873 _ComplexT __t;
874 __real__ __t = __z.real();
875 __imag__ __t = __z.imag();
876 _M_value /= __t;
877 return *this;
880 // 26.2.3 complex specializations
881 // complex<long double> specialization
882 template<> class complex<long double>
884 public:
885 typedef long double value_type;
887 complex(long double = 0.0L, long double = 0.0L);
888 #ifdef _GLIBCPP_BUGGY_COMPLEX
889 complex(const complex& __z) : _M_value(__z._M_value) { }
890 #endif
891 complex(const complex<float>&);
892 complex(const complex<double>&);
894 long double real() const;
895 long double imag() const;
897 complex<long double>& operator= (long double);
898 complex<long double>& operator+= (long double);
899 complex<long double>& operator-= (long double);
900 complex<long double>& operator*= (long double);
901 complex<long double>& operator/= (long double);
903 // The compiler knows how to do this efficiently
904 // complex& operator= (const complex&);
905 template<typename _Tp>
906 complex<long double>& operator=(const complex<_Tp>&);
907 template<typename _Tp>
908 complex<long double>& operator+=(const complex<_Tp>&);
909 template<typename _Tp>
910 complex<long double>& operator-=(const complex<_Tp>&);
911 template<typename _Tp>
912 complex<long double>& operator*=(const complex<_Tp>&);
913 template<typename _Tp>
914 complex<long double>& operator/=(const complex<_Tp>&);
916 private:
917 typedef __complex__ long double _ComplexT;
918 _ComplexT _M_value;
920 complex(_ComplexT __z) : _M_value(__z) { }
922 friend class complex<float>;
923 friend class complex<double>;
926 inline
927 complex<long double>::complex(long double __r, long double __i)
929 __real__ _M_value = __r;
930 __imag__ _M_value = __i;
933 inline long double
934 complex<long double>::real() const
935 { return __real__ _M_value; }
937 inline long double
938 complex<long double>::imag() const
939 { return __imag__ _M_value; }
941 inline complex<long double>&
942 complex<long double>::operator=(long double __r)
944 __real__ _M_value = __r;
945 __imag__ _M_value = 0.0L;
946 return *this;
949 inline complex<long double>&
950 complex<long double>::operator+=(long double __r)
952 __real__ _M_value += __r;
953 return *this;
956 inline complex<long double>&
957 complex<long double>::operator-=(long double __r)
959 __real__ _M_value -= __r;
960 return *this;
963 inline complex<long double>&
964 complex<long double>::operator*=(long double __r)
966 _M_value *= __r;
967 return *this;
970 inline complex<long double>&
971 complex<long double>::operator/=(long double __r)
973 _M_value /= __r;
974 return *this;
977 template<typename _Tp>
978 inline complex<long double>&
979 complex<long double>::operator=(const complex<_Tp>& __z)
981 __real__ _M_value = __z.real();
982 __imag__ _M_value = __z.imag();
983 return *this;
986 template<typename _Tp>
987 inline complex<long double>&
988 complex<long double>::operator+=(const complex<_Tp>& __z)
990 __real__ _M_value += __z.real();
991 __imag__ _M_value += __z.imag();
992 return *this;
995 template<typename _Tp>
996 inline complex<long double>&
997 complex<long double>::operator-=(const complex<_Tp>& __z)
999 __real__ _M_value -= __z.real();
1000 __imag__ _M_value -= __z.imag();
1001 return *this;
1004 template<typename _Tp>
1005 inline complex<long double>&
1006 complex<long double>::operator*=(const complex<_Tp>& __z)
1008 _ComplexT __t;
1009 __real__ __t = __z.real();
1010 __imag__ __t = __z.imag();
1011 _M_value *= __t;
1012 return *this;
1015 template<typename _Tp>
1016 inline complex<long double>&
1017 complex<long double>::operator/=(const complex<_Tp>& __z)
1019 _ComplexT __t;
1020 __real__ __t = __z.real();
1021 __imag__ __t = __z.imag();
1022 _M_value /= __t;
1023 return *this;
1026 // These bits have to be at the end of this file, so that the
1027 // specializations have all been defined.
1028 // ??? No, they have to be there because of compiler limitation at
1029 // inlining. It suffices that class specializations be defined.
1030 inline
1031 complex<float>::complex(const complex<double>& __z)
1032 : _M_value(_ComplexT(__z._M_value)) { }
1034 inline
1035 complex<float>::complex(const complex<long double>& __z)
1036 : _M_value(_ComplexT(__z._M_value)) { }
1038 inline
1039 complex<double>::complex(const complex<float>& __z)
1040 : _M_value(_ComplexT(__z._M_value)) { }
1042 inline
1043 complex<double>::complex(const complex<long double>& __z)
1045 __real__ _M_value = __z.real();
1046 __imag__ _M_value = __z.imag();
1049 inline
1050 complex<long double>::complex(const complex<float>& __z)
1051 : _M_value(_ComplexT(__z._M_value)) { }
1053 inline
1054 complex<long double>::complex(const complex<double>& __z)
1055 : _M_value(_ComplexT(__z._M_value)) { }
1056 } // namespace std
1058 #endif /* _CPP_COMPLEX */