]> git.ipfire.org Git - thirdparty/gcc.git/blob - libstdc++-v3/include/bits/std_complex.h
d07a6b791df9e62afb9a11d104671af7e7803007
[thirdparty/gcc.git] / libstdc++-v3 / include / bits / std_complex.h
1 // The template and inlines for the -*- C++ -*- complex number classes.
2
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.
10
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.
15
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.
20
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.
29
30 //
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>
35 //
36
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.
40 */
41
42 #ifndef _CPP_COMPLEX
43 #define _CPP_COMPLEX 1
44
45 #pragma GCC system_header
46
47 #include <bits/c++config.h>
48 #include <bits/cpp_type_traits.h>
49 #include <bits/std_cmath.h>
50 #include <bits/std_sstream.h>
51
52 namespace std
53 {
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>;
59
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>&);
63
64 template<typename _Tp> complex<_Tp> conj(const complex<_Tp>&);
65 template<typename _Tp> complex<_Tp> polar(const _Tp&, const _Tp& = 0);
66
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>&);
83
84
85 // 26.2.2 Primary template class complex
86 template<typename _Tp>
87 class complex
88 {
89 public:
90 typedef _Tp value_type;
91
92 complex(const _Tp& = _Tp(), const _Tp & = _Tp());
93
94 // Let's the compiler synthetize the copy constructor
95 // complex (const complex<_Tp>&);
96 template<typename _Up>
97 complex(const complex<_Up>&);
98
99 _Tp real() const;
100 _Tp imag() const;
101
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&);
107
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>&);
121
122 private:
123 _Tp _M_real, _M_imag;
124 };
125
126 template<typename _Tp>
127 inline _Tp
128 complex<_Tp>::real() const { return _M_real; }
129
130 template<typename _Tp>
131 inline _Tp
132 complex<_Tp>::imag() const { return _M_imag; }
133
134 template<typename _Tp>
135 inline
136 complex<_Tp>::complex(const _Tp& __r, const _Tp& __i)
137 : _M_real(__r), _M_imag(__i) { }
138
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()) { }
144
145 template<typename _Tp>
146 complex<_Tp>&
147 complex<_Tp>::operator=(const _Tp& __t)
148 {
149 _M_real = __t;
150 _M_imag = _Tp();
151 return *this;
152 }
153
154 // 26.2.5/1
155 template<typename _Tp>
156 inline complex<_Tp>&
157 complex<_Tp>::operator+=(const _Tp& __t)
158 {
159 _M_real += __t;
160 return *this;
161 }
162
163 // 26.2.5/3
164 template<typename _Tp>
165 inline complex<_Tp>&
166 complex<_Tp>::operator-=(const _Tp& __t)
167 {
168 _M_real -= __t;
169 return *this;
170 }
171
172 // 26.2.5/5
173 template<typename _Tp>
174 complex<_Tp>&
175 complex<_Tp>::operator*=(const _Tp& __t)
176 {
177 _M_real *= __t;
178 _M_imag *= __t;
179 return *this;
180 }
181
182 // 26.2.5/7
183 template<typename _Tp>
184 complex<_Tp>&
185 complex<_Tp>::operator/=(const _Tp& __t)
186 {
187 _M_real /= __t;
188 _M_imag /= __t;
189 return *this;
190 }
191
192 template<typename _Tp>
193 template<typename _Up>
194 complex<_Tp>&
195 complex<_Tp>::operator=(const complex<_Up>& __z)
196 {
197 _M_real = __z.real();
198 _M_imag = __z.imag();
199 return *this;
200 }
201
202 // 26.2.5/9
203 template<typename _Tp>
204 template<typename _Up>
205 complex<_Tp>&
206 complex<_Tp>::operator+=(const complex<_Up>& __z)
207 {
208 _M_real += __z.real();
209 _M_imag += __z.imag();
210 return *this;
211 }
212
213 // 26.2.5/11
214 template<typename _Tp>
215 template<typename _Up>
216 complex<_Tp>&
217 complex<_Tp>::operator-=(const complex<_Up>& __z)
218 {
219 _M_real -= __z.real();
220 _M_imag -= __z.imag();
221 return *this;
222 }
223
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)
230 {
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;
235 }
236
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)
243 {
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;
249 }
250
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; }
256
257 template<typename _Tp>
258 inline complex<_Tp>
259 operator+(const complex<_Tp>& __x, const _Tp& __y)
260 { return complex<_Tp> (__x) += __y; }
261
262 template<typename _Tp>
263 inline complex<_Tp>
264 operator+(const _Tp& __x, const complex<_Tp>& __y)
265 { return complex<_Tp> (__y) += __x; }
266
267 template<typename _Tp>
268 inline complex<_Tp>
269 operator-(const complex<_Tp>& __x, const complex<_Tp>& __y)
270 { return complex<_Tp> (__x) -= __y; }
271
272 template<typename _Tp>
273 inline complex<_Tp>
274 operator-(const complex<_Tp>& __x, const _Tp& __y)
275 { return complex<_Tp> (__x) -= __y; }
276
277 template<typename _Tp>
278 inline complex<_Tp>
279 operator-(const _Tp& __x, const complex<_Tp>& __y)
280 { return complex<_Tp> (__x) -= __y; }
281
282 template<typename _Tp>
283 inline complex<_Tp>
284 operator*(const complex<_Tp>& __x, const complex<_Tp>& __y)
285 { return complex<_Tp> (__x) *= __y; }
286
287 template<typename _Tp>
288 inline complex<_Tp>
289 operator*(const complex<_Tp>& __x, const _Tp& __y)
290 { return complex<_Tp> (__x) *= __y; }
291
292 template<typename _Tp>
293 inline complex<_Tp>
294 operator*(const _Tp& __x, const complex<_Tp>& __y)
295 { return complex<_Tp> (__y) *= __x; }
296
297 template<typename _Tp>
298 inline complex<_Tp>
299 operator/(const complex<_Tp>& __x, const complex<_Tp>& __y)
300 { return complex<_Tp> (__x) /= __y; }
301
302 template<typename _Tp>
303 inline complex<_Tp>
304 operator/(const complex<_Tp>& __x, const _Tp& __y)
305 { return complex<_Tp> (__x) /= __y; }
306
307 template<typename _Tp>
308 inline complex<_Tp>
309 operator/(const _Tp& __x, const complex<_Tp>& __y)
310 { return complex<_Tp> (__x) /= __y; }
311
312 template<typename _Tp>
313 inline complex<_Tp>
314 operator+(const complex<_Tp>& __x)
315 { return __x; }
316
317 template<typename _Tp>
318 inline complex<_Tp>
319 operator-(const complex<_Tp>& __x)
320 { return complex<_Tp>(-__x.real(), -__x.imag()); }
321
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(); }
326
327 template<typename _Tp>
328 inline bool
329 operator==(const complex<_Tp>& __x, const _Tp& __y)
330 { return __x.real() == __y && __x.imag() == _Tp(); }
331
332 template<typename _Tp>
333 inline bool
334 operator==(const _Tp& __x, const complex<_Tp>& __y)
335 { return __x == __y.real() && _Tp() == __y.imag(); }
336
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(); }
341
342 template<typename _Tp>
343 inline bool
344 operator!=(const complex<_Tp>& __x, const _Tp& __y)
345 { return __x.real() != __y || __x.imag() != _Tp(); }
346
347 template<typename _Tp>
348 inline bool
349 operator!=(const _Tp& __x, const complex<_Tp>& __y)
350 { return __x != __y.real() || _Tp() != __y.imag(); }
351
352 template<typename _Tp, typename _CharT, class _Traits>
353 basic_istream<_CharT, _Traits>&
354 operator>>(basic_istream<_CharT, _Traits>& __is, complex<_Tp>& __x)
355 {
356 _Tp __re_x, __im_x;
357 _CharT __ch;
358 __is >> __ch;
359 if (__ch == '(')
360 {
361 __is >> __re_x >> __ch;
362 if (__ch == ',')
363 {
364 __is >> __im_x >> __ch;
365 if (__ch == ')')
366 __x = complex<_Tp>(__re_x, __im_x);
367 else
368 __is.setstate(ios_base::failbit);
369 }
370 else if (__ch == ')')
371 __x = complex<_Tp>(__re_x, _Tp(0));
372 else
373 __is.setstate(ios_base::failbit);
374 }
375 else
376 {
377 __is.putback(__ch);
378 __is >> __re_x;
379 __x = complex<_Tp>(__re_x, _Tp(0));
380 }
381 return __is;
382 }
383
384 template<typename _Tp, typename _CharT, class _Traits>
385 basic_ostream<_CharT, _Traits>&
386 operator<<(basic_ostream<_CharT, _Traits>& __os, const complex<_Tp>& __x)
387 {
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();
394 }
395
396 // Values
397 template<typename _Tp>
398 inline _Tp
399 real(const complex<_Tp>& __z)
400 { return __z.real(); }
401
402 template<typename _Tp>
403 inline _Tp
404 imag(const complex<_Tp>& __z)
405 { return __z.imag(); }
406
407 template<typename _Tp>
408 inline _Tp
409 abs(const complex<_Tp>& __z)
410 {
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);
419 }
420
421 template<typename _Tp>
422 inline _Tp
423 arg(const complex<_Tp>& __z)
424 { return atan2(__z.imag(), __z.real()); }
425
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
433 {
434 template<typename _Tp>
435 static inline _Tp _S_do_it(const complex<_Tp>& __z)
436 {
437 const _Tp __x = __z.real();
438 const _Tp __y = __z.imag();
439 return __x * __x + __y * __y;
440 }
441 };
442
443 template<>
444 struct _Norm_helper<true>
445 {
446 template<typename _Tp>
447 static inline _Tp _S_do_it(const complex<_Tp>& __z)
448 {
449 _Tp __res = abs(__z);
450 return __res * __res;
451 }
452 };
453
454 template<typename _Tp>
455 inline _Tp
456 norm(const complex<_Tp>& __z)
457 {
458 return _Norm_helper<__is_floating<_Tp>::_M_type>::_S_do_it(__z);
459 }
460
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)); }
465
466 template<typename _Tp>
467 inline complex<_Tp>
468 conj(const complex<_Tp>& __z)
469 { return complex<_Tp>(__z.real(), -__z.imag()); }
470
471 // Transcendentals
472 template<typename _Tp>
473 inline complex<_Tp>
474 cos(const complex<_Tp>& __z)
475 {
476 const _Tp __x = __z.real();
477 const _Tp __y = __z.imag();
478 return complex<_Tp>(cos(__x) * cosh(__y), -sin(__x) * sinh(__y));
479 }
480
481 template<typename _Tp>
482 inline complex<_Tp>
483 cosh(const complex<_Tp>& __z)
484 {
485 const _Tp __x = __z.real();
486 const _Tp __y = __z.imag();
487 return complex<_Tp>(cosh(__x) * cos(__y), sinh(__x) * sin(__y));
488 }
489
490 template<typename _Tp>
491 inline complex<_Tp>
492 exp(const complex<_Tp>& __z)
493 { return polar(exp(__z.real()), __z.imag()); }
494
495 template<typename _Tp>
496 inline complex<_Tp>
497 log(const complex<_Tp>& __z)
498 { return complex<_Tp>(log(abs(__z)), arg(__z)); }
499
500 template<typename _Tp>
501 inline complex<_Tp>
502 log10(const complex<_Tp>& __z)
503 { return log(__z) / log(_Tp(10.0)); }
504
505 template<typename _Tp>
506 inline complex<_Tp>
507 sin(const complex<_Tp>& __z)
508 {
509 const _Tp __x = __z.real();
510 const _Tp __y = __z.imag();
511 return complex<_Tp>(sin(__x) * cosh(__y), cos(__x) * sinh(__y));
512 }
513
514 template<typename _Tp>
515 inline complex<_Tp>
516 sinh(const complex<_Tp>& __z)
517 {
518 const _Tp __x = __z.real();
519 const _Tp __y = __z.imag();
520 return complex<_Tp>(sinh(__x) * cos(__y), cosh(__x) * sin(__y));
521 }
522
523 template<typename _Tp>
524 complex<_Tp>
525 sqrt(const complex<_Tp>& __z)
526 {
527 _Tp __x = __z.real();
528 _Tp __y = __z.imag();
529
530 if (__x == _Tp())
531 {
532 _Tp __t = sqrt(abs(__y) / 2);
533 return complex<_Tp>(__t, __y < _Tp() ? -__t : __t);
534 }
535 else
536 {
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);
542 }
543 }
544
545 template<typename _Tp>
546 inline complex<_Tp>
547 tan(const complex<_Tp>& __z)
548 {
549 return sin(__z) / cos(__z);
550 }
551
552 template<typename _Tp>
553 inline complex<_Tp>
554 tanh(const complex<_Tp>& __z)
555 {
556 return sinh(__z) / cosh(__z);
557 }
558
559 template<typename _Tp>
560 inline complex<_Tp>
561 pow(const complex<_Tp>& __z, int __n)
562 {
563 return __pow_helper(__z, __n);
564 }
565
566 template<typename _Tp>
567 inline complex<_Tp>
568 pow(const complex<_Tp>& __x, const _Tp& __y)
569 {
570 return exp(__y * log(__x));
571 }
572
573 template<typename _Tp>
574 inline complex<_Tp>
575 pow(const complex<_Tp>& __x, const complex<_Tp>& __y)
576 {
577 return exp(__y * log(__x));
578 }
579
580 template<typename _Tp>
581 inline complex<_Tp>
582 pow(const _Tp& __x, const complex<_Tp>& __y)
583 {
584 return exp(__y * log(__x));
585 }
586
587 // 26.2.3 complex specializations
588 // complex<float> specialization
589 template<> class complex<float>
590 {
591 public:
592 typedef float value_type;
593
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>&);
600
601 float real() const;
602 float imag() const;
603
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);
609
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>&);
623
624 private:
625 typedef __complex__ float _ComplexT;
626 _ComplexT _M_value;
627
628 complex(_ComplexT __z) : _M_value(__z) { }
629
630 friend class complex<double>;
631 friend class complex<long double>;
632 };
633
634 inline float
635 complex<float>::real() const
636 { return __real__ _M_value; }
637
638 inline float
639 complex<float>::imag() const
640 { return __imag__ _M_value; }
641
642 inline
643 complex<float>::complex(float r, float i)
644 {
645 __real__ _M_value = r;
646 __imag__ _M_value = i;
647 }
648
649 inline complex<float>&
650 complex<float>::operator=(float __f)
651 {
652 __real__ _M_value = __f;
653 __imag__ _M_value = 0.0f;
654 return *this;
655 }
656
657 inline complex<float>&
658 complex<float>::operator+=(float __f)
659 {
660 __real__ _M_value += __f;
661 return *this;
662 }
663
664 inline complex<float>&
665 complex<float>::operator-=(float __f)
666 {
667 __real__ _M_value -= __f;
668 return *this;
669 }
670
671 inline complex<float>&
672 complex<float>::operator*=(float __f)
673 {
674 _M_value *= __f;
675 return *this;
676 }
677
678 inline complex<float>&
679 complex<float>::operator/=(float __f)
680 {
681 _M_value /= __f;
682 return *this;
683 }
684
685 template<typename _Tp>
686 inline complex<float>&
687 complex<float>::operator=(const complex<_Tp>& __z)
688 {
689 __real__ _M_value = __z.real();
690 __imag__ _M_value = __z.imag();
691 return *this;
692 }
693
694 template<typename _Tp>
695 inline complex<float>&
696 complex<float>::operator+=(const complex<_Tp>& __z)
697 {
698 __real__ _M_value += __z.real();
699 __imag__ _M_value += __z.imag();
700 return *this;
701 }
702
703 template<typename _Tp>
704 inline complex<float>&
705 complex<float>::operator-=(const complex<_Tp>& __z)
706 {
707 __real__ _M_value -= __z.real();
708 __imag__ _M_value -= __z.imag();
709 return *this;
710 }
711
712 template<typename _Tp>
713 inline complex<float>&
714 complex<float>::operator*=(const complex<_Tp>& __z)
715 {
716 _ComplexT __t;
717 __real__ __t = __z.real();
718 __imag__ __t = __z.imag();
719 _M_value *= __t;
720 return *this;
721 }
722
723 template<typename _Tp>
724 inline complex<float>&
725 complex<float>::operator/=(const complex<_Tp>& __z)
726 {
727 _ComplexT __t;
728 __real__ __t = __z.real();
729 __imag__ __t = __z.imag();
730 _M_value /= __t;
731 return *this;
732 }
733
734 // 26.2.3 complex specializations
735 // complex<double> specialization
736 template<> class complex<double>
737 {
738 public:
739 typedef double value_type;
740
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>&);
747
748 double real() const;
749 double imag() const;
750
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);
756
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>&);
769
770 private:
771 typedef __complex__ double _ComplexT;
772 _ComplexT _M_value;
773
774 complex(_ComplexT __z) : _M_value(__z) { }
775
776 friend class complex<float>;
777 friend class complex<long double>;
778 };
779
780 inline double
781 complex<double>::real() const
782 { return __real__ _M_value; }
783
784 inline double
785 complex<double>::imag() const
786 { return __imag__ _M_value; }
787
788 inline
789 complex<double>::complex(double __r, double __i)
790 {
791 __real__ _M_value = __r;
792 __imag__ _M_value = __i;
793 }
794
795 inline complex<double>&
796 complex<double>::operator=(double __d)
797 {
798 __real__ _M_value = __d;
799 __imag__ _M_value = 0.0;
800 return *this;
801 }
802
803 inline complex<double>&
804 complex<double>::operator+=(double __d)
805 {
806 __real__ _M_value += __d;
807 return *this;
808 }
809
810 inline complex<double>&
811 complex<double>::operator-=(double __d)
812 {
813 __real__ _M_value -= __d;
814 return *this;
815 }
816
817 inline complex<double>&
818 complex<double>::operator*=(double __d)
819 {
820 _M_value *= __d;
821 return *this;
822 }
823
824 inline complex<double>&
825 complex<double>::operator/=(double __d)
826 {
827 _M_value /= __d;
828 return *this;
829 }
830
831 template<typename _Tp>
832 inline complex<double>&
833 complex<double>::operator=(const complex<_Tp>& __z)
834 {
835 __real__ _M_value = __z.real();
836 __imag__ _M_value = __z.imag();
837 return *this;
838 }
839
840 template<typename _Tp>
841 inline complex<double>&
842 complex<double>::operator+=(const complex<_Tp>& __z)
843 {
844 __real__ _M_value += __z.real();
845 __imag__ _M_value += __z.imag();
846 return *this;
847 }
848
849 template<typename _Tp>
850 inline complex<double>&
851 complex<double>::operator-=(const complex<_Tp>& __z)
852 {
853 __real__ _M_value -= __z.real();
854 __imag__ _M_value -= __z.imag();
855 return *this;
856 }
857
858 template<typename _Tp>
859 inline complex<double>&
860 complex<double>::operator*=(const complex<_Tp>& __z)
861 {
862 _ComplexT __t;
863 __real__ __t = __z.real();
864 __imag__ __t = __z.imag();
865 _M_value *= __t;
866 return *this;
867 }
868
869 template<typename _Tp>
870 inline complex<double>&
871 complex<double>::operator/=(const complex<_Tp>& __z)
872 {
873 _ComplexT __t;
874 __real__ __t = __z.real();
875 __imag__ __t = __z.imag();
876 _M_value /= __t;
877 return *this;
878 }
879
880 // 26.2.3 complex specializations
881 // complex<long double> specialization
882 template<> class complex<long double>
883 {
884 public:
885 typedef long double value_type;
886
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>&);
893
894 long double real() const;
895 long double imag() const;
896
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);
902
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>&);
915
916 private:
917 typedef __complex__ long double _ComplexT;
918 _ComplexT _M_value;
919
920 complex(_ComplexT __z) : _M_value(__z) { }
921
922 friend class complex<float>;
923 friend class complex<double>;
924 };
925
926 inline
927 complex<long double>::complex(long double __r, long double __i)
928 {
929 __real__ _M_value = __r;
930 __imag__ _M_value = __i;
931 }
932
933 inline long double
934 complex<long double>::real() const
935 { return __real__ _M_value; }
936
937 inline long double
938 complex<long double>::imag() const
939 { return __imag__ _M_value; }
940
941 inline complex<long double>&
942 complex<long double>::operator=(long double __r)
943 {
944 __real__ _M_value = __r;
945 __imag__ _M_value = 0.0L;
946 return *this;
947 }
948
949 inline complex<long double>&
950 complex<long double>::operator+=(long double __r)
951 {
952 __real__ _M_value += __r;
953 return *this;
954 }
955
956 inline complex<long double>&
957 complex<long double>::operator-=(long double __r)
958 {
959 __real__ _M_value -= __r;
960 return *this;
961 }
962
963 inline complex<long double>&
964 complex<long double>::operator*=(long double __r)
965 {
966 _M_value *= __r;
967 return *this;
968 }
969
970 inline complex<long double>&
971 complex<long double>::operator/=(long double __r)
972 {
973 _M_value /= __r;
974 return *this;
975 }
976
977 template<typename _Tp>
978 inline complex<long double>&
979 complex<long double>::operator=(const complex<_Tp>& __z)
980 {
981 __real__ _M_value = __z.real();
982 __imag__ _M_value = __z.imag();
983 return *this;
984 }
985
986 template<typename _Tp>
987 inline complex<long double>&
988 complex<long double>::operator+=(const complex<_Tp>& __z)
989 {
990 __real__ _M_value += __z.real();
991 __imag__ _M_value += __z.imag();
992 return *this;
993 }
994
995 template<typename _Tp>
996 inline complex<long double>&
997 complex<long double>::operator-=(const complex<_Tp>& __z)
998 {
999 __real__ _M_value -= __z.real();
1000 __imag__ _M_value -= __z.imag();
1001 return *this;
1002 }
1003
1004 template<typename _Tp>
1005 inline complex<long double>&
1006 complex<long double>::operator*=(const complex<_Tp>& __z)
1007 {
1008 _ComplexT __t;
1009 __real__ __t = __z.real();
1010 __imag__ __t = __z.imag();
1011 _M_value *= __t;
1012 return *this;
1013 }
1014
1015 template<typename _Tp>
1016 inline complex<long double>&
1017 complex<long double>::operator/=(const complex<_Tp>& __z)
1018 {
1019 _ComplexT __t;
1020 __real__ __t = __z.real();
1021 __imag__ __t = __z.imag();
1022 _M_value /= __t;
1023 return *this;
1024 }
1025
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)) { }
1033
1034 inline
1035 complex<float>::complex(const complex<long double>& __z)
1036 : _M_value(_ComplexT(__z._M_value)) { }
1037
1038 inline
1039 complex<double>::complex(const complex<float>& __z)
1040 : _M_value(_ComplexT(__z._M_value)) { }
1041
1042 inline
1043 complex<double>::complex(const complex<long double>& __z)
1044 {
1045 __real__ _M_value = __z.real();
1046 __imag__ _M_value = __z.imag();
1047 }
1048
1049 inline
1050 complex<long double>::complex(const complex<float>& __z)
1051 : _M_value(_ComplexT(__z._M_value)) { }
1052
1053 inline
1054 complex<long double>::complex(const complex<double>& __z)
1055 : _M_value(_ComplexT(__z._M_value)) { }
1056 } // namespace std
1057
1058 #endif /* _CPP_COMPLEX */