* Makefile.in: Convert to ./ throughout. Rebuild dependencies
[official-gcc.git] / libstdc++-v3 / include / std / std_complex.h
blob4fe80a557694671140d72b667c6ee5a46383c843
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.
5 //
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)
10 // any later version.
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,
20 // USA.
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>
38 /** @file complex
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>
50 #include <cmath>
51 #include <sstream>
53 namespace std
55 // Forward declarations
56 template<typename _Tp> class complex;
57 template<> class complex<float>;
58 template<> class complex<double>;
59 template<> class complex<long double>;
61 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);
68 // Transcendentals:
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>&,
77 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>
88 class complex
90 public:
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>&);
100 _Tp& real();
101 const _Tp& real() const;
102 _Tp& imag();
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>&);
125 private:
126 _Tp _M_real;
127 _Tp _M_imag;
130 template<typename _Tp>
131 inline _Tp&
132 complex<_Tp>::real() { return _M_real; }
134 template<typename _Tp>
135 inline const _Tp&
136 complex<_Tp>::real() const { return _M_real; }
138 template<typename _Tp>
139 inline _Tp&
140 complex<_Tp>::imag() { return _M_imag; }
142 template<typename _Tp>
143 inline const _Tp&
144 complex<_Tp>::imag() const { return _M_imag; }
146 template<typename _Tp>
147 inline
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>
153 inline
154 complex<_Tp>::complex(const complex<_Up>& __z)
155 : _M_real(__z.real()), _M_imag(__z.imag()) { }
157 template<typename _Tp>
158 complex<_Tp>&
159 complex<_Tp>::operator=(const _Tp& __t)
161 _M_real = __t;
162 _M_imag = _Tp();
163 return *this;
166 // 26.2.5/1
167 template<typename _Tp>
168 inline complex<_Tp>&
169 complex<_Tp>::operator+=(const _Tp& __t)
171 _M_real += __t;
172 return *this;
175 // 26.2.5/3
176 template<typename _Tp>
177 inline complex<_Tp>&
178 complex<_Tp>::operator-=(const _Tp& __t)
180 _M_real -= __t;
181 return *this;
184 // 26.2.5/5
185 template<typename _Tp>
186 complex<_Tp>&
187 complex<_Tp>::operator*=(const _Tp& __t)
189 _M_real *= __t;
190 _M_imag *= __t;
191 return *this;
194 // 26.2.5/7
195 template<typename _Tp>
196 complex<_Tp>&
197 complex<_Tp>::operator/=(const _Tp& __t)
199 _M_real /= __t;
200 _M_imag /= __t;
201 return *this;
204 template<typename _Tp>
205 template<typename _Up>
206 complex<_Tp>&
207 complex<_Tp>::operator=(const complex<_Up>& __z)
209 _M_real = __z.real();
210 _M_imag = __z.imag();
211 return *this;
214 // 26.2.5/9
215 template<typename _Tp>
216 template<typename _Up>
217 complex<_Tp>&
218 complex<_Tp>::operator+=(const complex<_Up>& __z)
220 _M_real += __z.real();
221 _M_imag += __z.imag();
222 return *this;
225 // 26.2.5/11
226 template<typename _Tp>
227 template<typename _Up>
228 complex<_Tp>&
229 complex<_Tp>::operator-=(const complex<_Up>& __z)
231 _M_real -= __z.real();
232 _M_imag -= __z.imag();
233 return *this;
236 // 26.2.5/13
237 // XXX: This is a grammar school implementation.
238 template<typename _Tp>
239 template<typename _Up>
240 complex<_Tp>&
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();
245 _M_real = __r;
246 return *this;
249 // 26.2.5/15
250 // XXX: This is a grammar school implementation.
251 template<typename _Tp>
252 template<typename _Up>
253 complex<_Tp>&
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;
259 _M_real = __r / __n;
260 return *this;
263 // Operators:
264 template<typename _Tp>
265 inline complex<_Tp>
266 operator+(const complex<_Tp>& __x, const complex<_Tp>& __y)
268 complex<_Tp> __r = __x;
269 __r += __y;
270 return __r;
273 template<typename _Tp>
274 inline complex<_Tp>
275 operator+(const complex<_Tp>& __x, const _Tp& __y)
277 complex<_Tp> __r = __x;
278 __r.real() += __y;
279 return __r;
282 template<typename _Tp>
283 inline complex<_Tp>
284 operator+(const _Tp& __x, const complex<_Tp>& __y)
286 complex<_Tp> __r = __y;
287 __r.real() += __x;
288 return __r;
291 template<typename _Tp>
292 inline complex<_Tp>
293 operator-(const complex<_Tp>& __x, const complex<_Tp>& __y)
295 complex<_Tp> __r = __x;
296 __r -= __y;
297 return __r;
300 template<typename _Tp>
301 inline complex<_Tp>
302 operator-(const complex<_Tp>& __x, const _Tp& __y)
304 complex<_Tp> __r = __x;
305 __r.real() -= __y;
306 return __r;
309 template<typename _Tp>
310 inline complex<_Tp>
311 operator-(const _Tp& __x, const complex<_Tp>& __y)
313 complex<_Tp> __r(__x, -__y.imag());
314 __r.real() -= __y.real();
315 return __r;
318 template<typename _Tp>
319 inline complex<_Tp>
320 operator*(const complex<_Tp>& __x, const complex<_Tp>& __y)
322 complex<_Tp> __r = __x;
323 __r *= __y;
324 return __r;
327 template<typename _Tp>
328 inline complex<_Tp>
329 operator*(const complex<_Tp>& __x, const _Tp& __y)
331 complex<_Tp> __r = __x;
332 __r *= __y;
333 return __r;
336 template<typename _Tp>
337 inline complex<_Tp>
338 operator*(const _Tp& __x, const complex<_Tp>& __y)
340 complex<_Tp> __r = __y;
341 __r *= __x;
342 return __r;
345 template<typename _Tp>
346 inline complex<_Tp>
347 operator/(const complex<_Tp>& __x, const complex<_Tp>& __y)
349 complex<_Tp> __r = __x;
350 __r /= __y;
351 return __r;
354 template<typename _Tp>
355 inline complex<_Tp>
356 operator/(const complex<_Tp>& __x, const _Tp& __y)
358 complex<_Tp> __r = __x;
359 __r /= __y;
360 return __r;
363 template<typename _Tp>
364 inline complex<_Tp>
365 operator/(const _Tp& __x, const complex<_Tp>& __y)
367 complex<_Tp> __r = __x;
368 __r /= __y;
369 return __r;
372 template<typename _Tp>
373 inline complex<_Tp>
374 operator+(const complex<_Tp>& __x)
375 { return __x; }
377 template<typename _Tp>
378 inline complex<_Tp>
379 operator-(const complex<_Tp>& __x)
380 { return complex<_Tp>(-__x.real(), -__x.imag()); }
382 template<typename _Tp>
383 inline bool
384 operator==(const complex<_Tp>& __x, const complex<_Tp>& __y)
385 { return __x.real() == __y.real() && __x.imag() == __y.imag(); }
387 template<typename _Tp>
388 inline bool
389 operator==(const complex<_Tp>& __x, const _Tp& __y)
390 { return __x.real() == __y && __x.imag() == _Tp(); }
392 template<typename _Tp>
393 inline bool
394 operator==(const _Tp& __x, const complex<_Tp>& __y)
395 { return __x == __y.real() && _Tp() == __y.imag(); }
397 template<typename _Tp>
398 inline bool
399 operator!=(const complex<_Tp>& __x, const complex<_Tp>& __y)
400 { return __x.real() != __y.real() || __x.imag() != __y.imag(); }
402 template<typename _Tp>
403 inline bool
404 operator!=(const complex<_Tp>& __x, const _Tp& __y)
405 { return __x.real() != __y || __x.imag() != _Tp(); }
407 template<typename _Tp>
408 inline bool
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)
416 _Tp __re_x, __im_x;
417 _CharT __ch;
418 __is >> __ch;
419 if (__ch == '(')
421 __is >> __re_x >> __ch;
422 if (__ch == ',')
424 __is >> __im_x >> __ch;
425 if (__ch == ')')
426 __x = complex<_Tp>(__re_x, __im_x);
427 else
428 __is.setstate(ios_base::failbit);
430 else if (__ch == ')')
431 __x = __re_x;
432 else
433 __is.setstate(ios_base::failbit);
435 else
437 __is.putback(__ch);
438 __is >> __re_x;
439 __x = __re_x;
441 return __is;
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();
456 // Values
457 template<typename _Tp>
458 inline _Tp&
459 real(complex<_Tp>& __z)
460 { return __z.real(); }
462 template<typename _Tp>
463 inline const _Tp&
464 real(const complex<_Tp>& __z)
465 { return __z.real(); }
467 template<typename _Tp>
468 inline _Tp&
469 imag(complex<_Tp>& __z)
470 { return __z.imag(); }
472 template<typename _Tp>
473 inline const _Tp&
474 imag(const complex<_Tp>& __z)
475 { return __z.imag(); }
477 template<typename _Tp>
478 inline _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 ...
485 return __s;
486 __x /= __s;
487 __y /= __s;
488 return __s * sqrt(__x * __x + __y * __y);
491 template<typename _Tp>
492 inline _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.
501 template<bool>
502 struct _Norm_helper
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;
513 template<>
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>
525 inline _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>
532 inline complex<_Tp>
533 polar(const _Tp& __rho, const _Tp& __theta)
534 { return complex<_Tp>(__rho * cos(__theta), __rho * sin(__theta)); }
536 template<typename _Tp>
537 inline complex<_Tp>
538 conj(const complex<_Tp>& __z)
539 { return complex<_Tp>(__z.real(), -__z.imag()); }
541 // Transcendentals
542 template<typename _Tp>
543 inline complex<_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>
552 inline complex<_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>
561 inline complex<_Tp>
562 exp(const complex<_Tp>& __z)
563 { return std::polar(exp(__z.real()), __z.imag()); }
565 template<typename _Tp>
566 inline complex<_Tp>
567 log(const complex<_Tp>& __z)
568 { return complex<_Tp>(log(std::abs(__z)), std::arg(__z)); }
570 template<typename _Tp>
571 inline complex<_Tp>
572 log10(const complex<_Tp>& __z)
573 { return std::log(__z) / log(_Tp(10.0)); }
575 template<typename _Tp>
576 inline complex<_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>
585 inline complex<_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>
594 complex<_Tp>
595 sqrt(const complex<_Tp>& __z)
597 _Tp __x = __z.real();
598 _Tp __y = __z.imag();
600 if (__x == _Tp())
602 _Tp __t = sqrt(abs(__y) / 2);
603 return complex<_Tp>(__t, __y < _Tp() ? -__t : __t);
605 else
607 _Tp __t = sqrt(2 * (std::abs(__z) + abs(__x)));
608 _Tp __u = __t / 2;
609 return __x > _Tp()
610 ? complex<_Tp>(__u, __y / __t)
611 : complex<_Tp>(abs(__y) / __t, __y < _Tp() ? -__u : __u);
615 template<typename _Tp>
616 inline complex<_Tp>
617 tan(const complex<_Tp>& __z)
619 return std::sin(__z) / std::cos(__z);
622 template<typename _Tp>
623 inline complex<_Tp>
624 tanh(const complex<_Tp>& __z)
626 return std::sinh(__z) / std::cosh(__z);
629 template<typename _Tp>
630 inline complex<_Tp>
631 pow(const complex<_Tp>& __z, int __n)
633 return std::__pow_helper(__z, __n);
636 template<typename _Tp>
637 complex<_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>
648 inline complex<_Tp>
649 pow(const complex<_Tp>& __x, const complex<_Tp>& __y)
651 return __x == _Tp() ? _Tp() : exp(__y * log(__x));
654 template<typename _Tp>
655 inline complex<_Tp>
656 pow(const _Tp& __x, const complex<_Tp>& __y)
658 return __x == _Tp()
659 ? _Tp()
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>
667 public:
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) { }
673 #endif
674 explicit complex(const complex<double>&);
675 explicit complex(const complex<long double>&);
677 float& real();
678 const float& real() const;
679 float& imag();
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>&);
695 template<class _Tp>
696 complex<float>& operator-=(const complex<_Tp>&);
697 template<class _Tp>
698 complex<float>& operator*=(const complex<_Tp>&);
699 template<class _Tp>
700 complex<float>&operator/=(const complex<_Tp>&);
702 private:
703 typedef __complex__ float _ComplexT;
704 _ComplexT _M_value;
706 complex(_ComplexT __z) : _M_value(__z) { }
708 friend class complex<double>;
709 friend class complex<long double>;
712 inline float&
713 complex<float>::real()
714 { return __real__ _M_value; }
716 inline const float&
717 complex<float>::real() const
718 { return __real__ _M_value; }
720 inline float&
721 complex<float>::imag()
722 { return __imag__ _M_value; }
724 inline const float&
725 complex<float>::imag() const
726 { return __imag__ _M_value; }
728 inline
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;
740 return *this;
743 inline complex<float>&
744 complex<float>::operator+=(float __f)
746 __real__ _M_value += __f;
747 return *this;
750 inline complex<float>&
751 complex<float>::operator-=(float __f)
753 __real__ _M_value -= __f;
754 return *this;
757 inline complex<float>&
758 complex<float>::operator*=(float __f)
760 _M_value *= __f;
761 return *this;
764 inline complex<float>&
765 complex<float>::operator/=(float __f)
767 _M_value /= __f;
768 return *this;
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();
777 return *this;
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();
786 return *this;
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();
795 return *this;
798 template<typename _Tp>
799 inline complex<float>&
800 complex<float>::operator*=(const complex<_Tp>& __z)
802 _ComplexT __t;
803 __real__ __t = __z.real();
804 __imag__ __t = __z.imag();
805 _M_value *= __t;
806 return *this;
809 template<typename _Tp>
810 inline complex<float>&
811 complex<float>::operator/=(const complex<_Tp>& __z)
813 _ComplexT __t;
814 __real__ __t = __z.real();
815 __imag__ __t = __z.imag();
816 _M_value /= __t;
817 return *this;
820 // 26.2.3 complex specializations
821 // complex<double> specialization
822 template<> class complex<double>
824 public:
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) { }
830 #endif
831 complex(const complex<float>&);
832 explicit complex(const complex<long double>&);
834 double& real();
835 const double& real() const;
836 double& imag();
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>&);
858 private:
859 typedef __complex__ double _ComplexT;
860 _ComplexT _M_value;
862 complex(_ComplexT __z) : _M_value(__z) { }
864 friend class complex<float>;
865 friend class complex<long double>;
868 inline double&
869 complex<double>::real()
870 { return __real__ _M_value; }
872 inline const double&
873 complex<double>::real() const
874 { return __real__ _M_value; }
876 inline double&
877 complex<double>::imag()
878 { return __imag__ _M_value; }
880 inline const double&
881 complex<double>::imag() const
882 { return __imag__ _M_value; }
884 inline
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;
896 return *this;
899 inline complex<double>&
900 complex<double>::operator+=(double __d)
902 __real__ _M_value += __d;
903 return *this;
906 inline complex<double>&
907 complex<double>::operator-=(double __d)
909 __real__ _M_value -= __d;
910 return *this;
913 inline complex<double>&
914 complex<double>::operator*=(double __d)
916 _M_value *= __d;
917 return *this;
920 inline complex<double>&
921 complex<double>::operator/=(double __d)
923 _M_value /= __d;
924 return *this;
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();
933 return *this;
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();
942 return *this;
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();
951 return *this;
954 template<typename _Tp>
955 inline complex<double>&
956 complex<double>::operator*=(const complex<_Tp>& __z)
958 _ComplexT __t;
959 __real__ __t = __z.real();
960 __imag__ __t = __z.imag();
961 _M_value *= __t;
962 return *this;
965 template<typename _Tp>
966 inline complex<double>&
967 complex<double>::operator/=(const complex<_Tp>& __z)
969 _ComplexT __t;
970 __real__ __t = __z.real();
971 __imag__ __t = __z.imag();
972 _M_value /= __t;
973 return *this;
976 // 26.2.3 complex specializations
977 // complex<long double> specialization
978 template<> class complex<long double>
980 public:
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) { }
986 #endif
987 complex(const complex<float>&);
988 complex(const complex<double>&);
990 long double& real();
991 const long double& real() const;
992 long double& imag();
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>&);
1014 private:
1015 typedef __complex__ long double _ComplexT;
1016 _ComplexT _M_value;
1018 complex(_ComplexT __z) : _M_value(__z) { }
1020 friend class complex<float>;
1021 friend class complex<double>;
1024 inline
1025 complex<long double>::complex(long double __r, long double __i)
1027 __real__ _M_value = __r;
1028 __imag__ _M_value = __i;
1031 inline long double&
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; }
1039 inline long double&
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;
1052 return *this;
1055 inline complex<long double>&
1056 complex<long double>::operator+=(long double __r)
1058 __real__ _M_value += __r;
1059 return *this;
1062 inline complex<long double>&
1063 complex<long double>::operator-=(long double __r)
1065 __real__ _M_value -= __r;
1066 return *this;
1069 inline complex<long double>&
1070 complex<long double>::operator*=(long double __r)
1072 _M_value *= __r;
1073 return *this;
1076 inline complex<long double>&
1077 complex<long double>::operator/=(long double __r)
1079 _M_value /= __r;
1080 return *this;
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();
1089 return *this;
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();
1098 return *this;
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();
1107 return *this;
1110 template<typename _Tp>
1111 inline complex<long double>&
1112 complex<long double>::operator*=(const complex<_Tp>& __z)
1114 _ComplexT __t;
1115 __real__ __t = __z.real();
1116 __imag__ __t = __z.imag();
1117 _M_value *= __t;
1118 return *this;
1121 template<typename _Tp>
1122 inline complex<long double>&
1123 complex<long double>::operator/=(const complex<_Tp>& __z)
1125 _ComplexT __t;
1126 __real__ __t = __z.real();
1127 __imag__ __t = __z.imag();
1128 _M_value /= __t;
1129 return *this;
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.
1136 inline
1137 complex<float>::complex(const complex<double>& __z)
1138 : _M_value(_ComplexT(__z._M_value)) { }
1140 inline
1141 complex<float>::complex(const complex<long double>& __z)
1142 : _M_value(_ComplexT(__z._M_value)) { }
1144 inline
1145 complex<double>::complex(const complex<float>& __z)
1146 : _M_value(_ComplexT(__z._M_value)) { }
1148 inline
1149 complex<double>::complex(const complex<long double>& __z)
1151 __real__ _M_value = __z.real();
1152 __imag__ _M_value = __z.imag();
1155 inline
1156 complex<long double>::complex(const complex<float>& __z)
1157 : _M_value(_ComplexT(__z._M_value)) { }
1159 inline
1160 complex<long double>::complex(const complex<double>& __z)
1161 : _M_value(_ComplexT(__z._M_value)) { }
1162 } // namespace std
1164 #endif /* _GLIBCXX_COMPLEX */