complex (complex<>::real(_Tp), [...]): Add per DR 387.
[gcc.git] / libstdc++-v3 / include / std / complex
1 // The template and inlines for the -*- C++ -*- complex number classes.
2
3 // Copyright (C) 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005,
4 // 2006, 2007, 2008
5 // Free Software Foundation, Inc.
6 //
7 // This file is part of the GNU ISO C++ Library. This library is free
8 // software; you can redistribute it and/or modify it under the
9 // terms of the GNU General Public License as published by the
10 // Free Software Foundation; either version 2, or (at your option)
11 // any later version.
12
13 // This library is distributed in the hope that it will be useful,
14 // but WITHOUT ANY WARRANTY; without even the implied warranty of
15 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 // GNU General Public License for more details.
17
18 // You should have received a copy of the GNU General Public License
19 // along with this library; see the file COPYING. If not, write to
20 // the Free Software Foundation, 51 Franklin Street, Fifth Floor,
21 // Boston, MA 02110-1301, USA.
22
23 // As a special exception, you may use this file as part of a free software
24 // library without restriction. Specifically, if other files instantiate
25 // templates or use macros or inline functions from this file, or you compile
26 // this file and link it with other files to produce an executable, this
27 // file does not by itself cause the resulting executable to be covered by
28 // the GNU General Public License. This exception does not however
29 // invalidate any other reasons why the executable file might be covered by
30 // the GNU General Public License.
31
32 /** @file complex
33 * This is a Standard C++ Library header.
34 */
35
36 //
37 // ISO C++ 14882: 26.2 Complex Numbers
38 // Note: this is not a conforming implementation.
39 // Initially implemented by Ulrich Drepper <drepper@cygnus.com>
40 // Improved by Gabriel Dos Reis <dosreis@cmla.ens-cachan.fr>
41 //
42
43 #ifndef _GLIBCXX_COMPLEX
44 #define _GLIBCXX_COMPLEX 1
45
46 #pragma GCC system_header
47
48 #include <bits/c++config.h>
49 #include <bits/cpp_type_traits.h>
50 #include <ext/type_traits.h>
51 #include <cmath>
52 #include <sstream>
53
54 _GLIBCXX_BEGIN_NAMESPACE(std)
55
56 // Forward declarations.
57 template<typename _Tp> class complex;
58 template<> class complex<float>;
59 template<> class complex<double>;
60 template<> class complex<long double>;
61
62 /// Return magnitude of @a z.
63 template<typename _Tp> _Tp abs(const complex<_Tp>&);
64 /// Return phase angle of @a z.
65 template<typename _Tp> _Tp arg(const complex<_Tp>&);
66 /// Return @a z magnitude squared.
67 template<typename _Tp> _Tp norm(const complex<_Tp>&);
68
69 /// Return complex conjugate of @a z.
70 template<typename _Tp> complex<_Tp> conj(const complex<_Tp>&);
71 /// Return complex with magnitude @a rho and angle @a theta.
72 template<typename _Tp> complex<_Tp> polar(const _Tp&, const _Tp& = 0);
73
74 // Transcendentals:
75 /// Return complex cosine of @a z.
76 template<typename _Tp> complex<_Tp> cos(const complex<_Tp>&);
77 /// Return complex hyperbolic cosine of @a z.
78 template<typename _Tp> complex<_Tp> cosh(const complex<_Tp>&);
79 /// Return complex base e exponential of @a z.
80 template<typename _Tp> complex<_Tp> exp(const complex<_Tp>&);
81 /// Return complex natural logarithm of @a z.
82 template<typename _Tp> complex<_Tp> log(const complex<_Tp>&);
83 /// Return complex base 10 logarithm of @a z.
84 template<typename _Tp> complex<_Tp> log10(const complex<_Tp>&);
85 /// Return complex cosine of @a z.
86 template<typename _Tp> complex<_Tp> pow(const complex<_Tp>&, int);
87 /// Return @a x to the @a y'th power.
88 template<typename _Tp> complex<_Tp> pow(const complex<_Tp>&, const _Tp&);
89 /// Return @a x to the @a y'th power.
90 template<typename _Tp> complex<_Tp> pow(const complex<_Tp>&,
91 const complex<_Tp>&);
92 /// Return @a x to the @a y'th power.
93 template<typename _Tp> complex<_Tp> pow(const _Tp&, const complex<_Tp>&);
94 /// Return complex sine of @a z.
95 template<typename _Tp> complex<_Tp> sin(const complex<_Tp>&);
96 /// Return complex hyperbolic sine of @a z.
97 template<typename _Tp> complex<_Tp> sinh(const complex<_Tp>&);
98 /// Return complex square root of @a z.
99 template<typename _Tp> complex<_Tp> sqrt(const complex<_Tp>&);
100 /// Return complex tangent of @a z.
101 template<typename _Tp> complex<_Tp> tan(const complex<_Tp>&);
102 /// Return complex hyperbolic tangent of @a z.
103 template<typename _Tp> complex<_Tp> tanh(const complex<_Tp>&);
104 //@}
105
106
107 // 26.2.2 Primary template class complex
108 /**
109 * Template to represent complex numbers.
110 *
111 * Specializations for float, double, and long double are part of the
112 * library. Results with any other type are not guaranteed.
113 *
114 * @param Tp Type of real and imaginary values.
115 */
116 template<typename _Tp>
117 struct complex
118 {
119 /// Value typedef.
120 typedef _Tp value_type;
121
122 /// Default constructor. First parameter is x, second parameter is y.
123 /// Unspecified parameters default to 0.
124 complex(const _Tp& __r = _Tp(), const _Tp& __i = _Tp())
125 : _M_real(__r), _M_imag(__i) { }
126
127 // Lets the compiler synthesize the copy constructor
128 // complex (const complex<_Tp>&);
129 /// Copy constructor.
130 template<typename _Up>
131 complex(const complex<_Up>& __z)
132 : _M_real(__z.real()), _M_imag(__z.imag()) { }
133
134 #ifdef __GXX_EXPERIMENTAL_CXX0X__
135 // _GLIBCXX_RESOLVE_LIB_DEFECTS
136 // DR 387. std::complex over-encapsulated.
137 _Tp real() const
138 { return _M_real; }
139
140 _Tp imag() const
141 { return _M_imag; }
142 #else
143 /// Return real part of complex number.
144 _Tp& real()
145 { return _M_real; }
146
147 /// Return real part of complex number.
148 const _Tp& real() const
149 { return _M_real; }
150
151 /// Return imaginary part of complex number.
152 _Tp& imag()
153 { return _M_imag; }
154
155 /// Return imaginary part of complex number.
156 const _Tp& imag() const
157 { return _M_imag; }
158 #endif
159
160 // _GLIBCXX_RESOLVE_LIB_DEFECTS
161 // DR 387. std::complex over-encapsulated.
162 void real(_Tp __val)
163 { _M_real = __val; }
164
165 void imag(_Tp __val)
166 { _M_imag = __val; }
167
168 /// Assign this complex number to scalar @a t.
169 complex<_Tp>& operator=(const _Tp&);
170
171 /// Add @a t to this complex number.
172 // 26.2.5/1
173 complex<_Tp>&
174 operator+=(const _Tp& __t)
175 {
176 _M_real += __t;
177 return *this;
178 }
179
180 /// Subtract @a t from this complex number.
181 // 26.2.5/3
182 complex<_Tp>&
183 operator-=(const _Tp& __t)
184 {
185 _M_real -= __t;
186 return *this;
187 }
188
189 /// Multiply this complex number by @a t.
190 complex<_Tp>& operator*=(const _Tp&);
191 /// Divide this complex number by @a t.
192 complex<_Tp>& operator/=(const _Tp&);
193
194 // Lets the compiler synthesize the
195 // copy and assignment operator
196 // complex<_Tp>& operator= (const complex<_Tp>&);
197 /// Assign this complex number to complex @a z.
198 template<typename _Up>
199 complex<_Tp>& operator=(const complex<_Up>&);
200 /// Add @a z to this complex number.
201 template<typename _Up>
202 complex<_Tp>& operator+=(const complex<_Up>&);
203 /// Subtract @a z from this complex number.
204 template<typename _Up>
205 complex<_Tp>& operator-=(const complex<_Up>&);
206 /// Multiply this complex number by @a z.
207 template<typename _Up>
208 complex<_Tp>& operator*=(const complex<_Up>&);
209 /// Divide this complex number by @a z.
210 template<typename _Up>
211 complex<_Tp>& operator/=(const complex<_Up>&);
212
213 const complex& __rep() const
214 { return *this; }
215
216 private:
217 _Tp _M_real;
218 _Tp _M_imag;
219 };
220
221 template<typename _Tp>
222 complex<_Tp>&
223 complex<_Tp>::operator=(const _Tp& __t)
224 {
225 _M_real = __t;
226 _M_imag = _Tp();
227 return *this;
228 }
229
230 // 26.2.5/5
231 template<typename _Tp>
232 complex<_Tp>&
233 complex<_Tp>::operator*=(const _Tp& __t)
234 {
235 _M_real *= __t;
236 _M_imag *= __t;
237 return *this;
238 }
239
240 // 26.2.5/7
241 template<typename _Tp>
242 complex<_Tp>&
243 complex<_Tp>::operator/=(const _Tp& __t)
244 {
245 _M_real /= __t;
246 _M_imag /= __t;
247 return *this;
248 }
249
250 template<typename _Tp>
251 template<typename _Up>
252 complex<_Tp>&
253 complex<_Tp>::operator=(const complex<_Up>& __z)
254 {
255 _M_real = __z.real();
256 _M_imag = __z.imag();
257 return *this;
258 }
259
260 // 26.2.5/9
261 template<typename _Tp>
262 template<typename _Up>
263 complex<_Tp>&
264 complex<_Tp>::operator+=(const complex<_Up>& __z)
265 {
266 _M_real += __z.real();
267 _M_imag += __z.imag();
268 return *this;
269 }
270
271 // 26.2.5/11
272 template<typename _Tp>
273 template<typename _Up>
274 complex<_Tp>&
275 complex<_Tp>::operator-=(const complex<_Up>& __z)
276 {
277 _M_real -= __z.real();
278 _M_imag -= __z.imag();
279 return *this;
280 }
281
282 // 26.2.5/13
283 // XXX: This is a grammar school implementation.
284 template<typename _Tp>
285 template<typename _Up>
286 complex<_Tp>&
287 complex<_Tp>::operator*=(const complex<_Up>& __z)
288 {
289 const _Tp __r = _M_real * __z.real() - _M_imag * __z.imag();
290 _M_imag = _M_real * __z.imag() + _M_imag * __z.real();
291 _M_real = __r;
292 return *this;
293 }
294
295 // 26.2.5/15
296 // XXX: This is a grammar school implementation.
297 template<typename _Tp>
298 template<typename _Up>
299 complex<_Tp>&
300 complex<_Tp>::operator/=(const complex<_Up>& __z)
301 {
302 const _Tp __r = _M_real * __z.real() + _M_imag * __z.imag();
303 const _Tp __n = std::norm(__z);
304 _M_imag = (_M_imag * __z.real() - _M_real * __z.imag()) / __n;
305 _M_real = __r / __n;
306 return *this;
307 }
308
309 // Operators:
310 //@{
311 /// Return new complex value @a x plus @a y.
312 template<typename _Tp>
313 inline complex<_Tp>
314 operator+(const complex<_Tp>& __x, const complex<_Tp>& __y)
315 {
316 complex<_Tp> __r = __x;
317 __r += __y;
318 return __r;
319 }
320
321 template<typename _Tp>
322 inline complex<_Tp>
323 operator+(const complex<_Tp>& __x, const _Tp& __y)
324 {
325 complex<_Tp> __r = __x;
326 __r.real() += __y;
327 return __r;
328 }
329
330 template<typename _Tp>
331 inline complex<_Tp>
332 operator+(const _Tp& __x, const complex<_Tp>& __y)
333 {
334 complex<_Tp> __r = __y;
335 __r.real() += __x;
336 return __r;
337 }
338 //@}
339
340 //@{
341 /// Return new complex value @a x minus @a y.
342 template<typename _Tp>
343 inline complex<_Tp>
344 operator-(const complex<_Tp>& __x, const complex<_Tp>& __y)
345 {
346 complex<_Tp> __r = __x;
347 __r -= __y;
348 return __r;
349 }
350
351 template<typename _Tp>
352 inline complex<_Tp>
353 operator-(const complex<_Tp>& __x, const _Tp& __y)
354 {
355 complex<_Tp> __r = __x;
356 __r.real() -= __y;
357 return __r;
358 }
359
360 template<typename _Tp>
361 inline complex<_Tp>
362 operator-(const _Tp& __x, const complex<_Tp>& __y)
363 {
364 complex<_Tp> __r(__x, -__y.imag());
365 __r.real() -= __y.real();
366 return __r;
367 }
368 //@}
369
370 //@{
371 /// Return new complex value @a x times @a y.
372 template<typename _Tp>
373 inline complex<_Tp>
374 operator*(const complex<_Tp>& __x, const complex<_Tp>& __y)
375 {
376 complex<_Tp> __r = __x;
377 __r *= __y;
378 return __r;
379 }
380
381 template<typename _Tp>
382 inline complex<_Tp>
383 operator*(const complex<_Tp>& __x, const _Tp& __y)
384 {
385 complex<_Tp> __r = __x;
386 __r *= __y;
387 return __r;
388 }
389
390 template<typename _Tp>
391 inline complex<_Tp>
392 operator*(const _Tp& __x, const complex<_Tp>& __y)
393 {
394 complex<_Tp> __r = __y;
395 __r *= __x;
396 return __r;
397 }
398 //@}
399
400 //@{
401 /// Return new complex value @a x divided by @a y.
402 template<typename _Tp>
403 inline complex<_Tp>
404 operator/(const complex<_Tp>& __x, const complex<_Tp>& __y)
405 {
406 complex<_Tp> __r = __x;
407 __r /= __y;
408 return __r;
409 }
410
411 template<typename _Tp>
412 inline complex<_Tp>
413 operator/(const complex<_Tp>& __x, const _Tp& __y)
414 {
415 complex<_Tp> __r = __x;
416 __r /= __y;
417 return __r;
418 }
419
420 template<typename _Tp>
421 inline complex<_Tp>
422 operator/(const _Tp& __x, const complex<_Tp>& __y)
423 {
424 complex<_Tp> __r = __x;
425 __r /= __y;
426 return __r;
427 }
428 //@}
429
430 /// Return @a x.
431 template<typename _Tp>
432 inline complex<_Tp>
433 operator+(const complex<_Tp>& __x)
434 { return __x; }
435
436 /// Return complex negation of @a x.
437 template<typename _Tp>
438 inline complex<_Tp>
439 operator-(const complex<_Tp>& __x)
440 { return complex<_Tp>(-__x.real(), -__x.imag()); }
441
442 //@{
443 /// Return true if @a x is equal to @a y.
444 template<typename _Tp>
445 inline bool
446 operator==(const complex<_Tp>& __x, const complex<_Tp>& __y)
447 { return __x.real() == __y.real() && __x.imag() == __y.imag(); }
448
449 template<typename _Tp>
450 inline bool
451 operator==(const complex<_Tp>& __x, const _Tp& __y)
452 { return __x.real() == __y && __x.imag() == _Tp(); }
453
454 template<typename _Tp>
455 inline bool
456 operator==(const _Tp& __x, const complex<_Tp>& __y)
457 { return __x == __y.real() && _Tp() == __y.imag(); }
458 //@}
459
460 //@{
461 /// Return false if @a x is equal to @a y.
462 template<typename _Tp>
463 inline bool
464 operator!=(const complex<_Tp>& __x, const complex<_Tp>& __y)
465 { return __x.real() != __y.real() || __x.imag() != __y.imag(); }
466
467 template<typename _Tp>
468 inline bool
469 operator!=(const complex<_Tp>& __x, const _Tp& __y)
470 { return __x.real() != __y || __x.imag() != _Tp(); }
471
472 template<typename _Tp>
473 inline bool
474 operator!=(const _Tp& __x, const complex<_Tp>& __y)
475 { return __x != __y.real() || _Tp() != __y.imag(); }
476 //@}
477
478 /// Extraction operator for complex values.
479 template<typename _Tp, typename _CharT, class _Traits>
480 basic_istream<_CharT, _Traits>&
481 operator>>(basic_istream<_CharT, _Traits>& __is, complex<_Tp>& __x)
482 {
483 _Tp __re_x, __im_x;
484 _CharT __ch;
485 __is >> __ch;
486 if (__ch == '(')
487 {
488 __is >> __re_x >> __ch;
489 if (__ch == ',')
490 {
491 __is >> __im_x >> __ch;
492 if (__ch == ')')
493 __x = complex<_Tp>(__re_x, __im_x);
494 else
495 __is.setstate(ios_base::failbit);
496 }
497 else if (__ch == ')')
498 __x = __re_x;
499 else
500 __is.setstate(ios_base::failbit);
501 }
502 else
503 {
504 __is.putback(__ch);
505 __is >> __re_x;
506 __x = __re_x;
507 }
508 return __is;
509 }
510
511 /// Insertion operator for complex values.
512 template<typename _Tp, typename _CharT, class _Traits>
513 basic_ostream<_CharT, _Traits>&
514 operator<<(basic_ostream<_CharT, _Traits>& __os, const complex<_Tp>& __x)
515 {
516 basic_ostringstream<_CharT, _Traits> __s;
517 __s.flags(__os.flags());
518 __s.imbue(__os.getloc());
519 __s.precision(__os.precision());
520 __s << '(' << __x.real() << ',' << __x.imag() << ')';
521 return __os << __s.str();
522 }
523
524 // Values
525 #ifdef __GXX_EXPERIMENTAL_CXX0X__
526 template<typename _Tp>
527 inline _Tp
528 real(const complex<_Tp>& __z)
529 { return __z.real(); }
530
531 template<typename _Tp>
532 inline _Tp
533 imag(const complex<_Tp>& __z)
534 { return __z.imag(); }
535 #else
536 template<typename _Tp>
537 inline _Tp&
538 real(complex<_Tp>& __z)
539 { return __z.real(); }
540
541 template<typename _Tp>
542 inline const _Tp&
543 real(const complex<_Tp>& __z)
544 { return __z.real(); }
545
546 template<typename _Tp>
547 inline _Tp&
548 imag(complex<_Tp>& __z)
549 { return __z.imag(); }
550
551 template<typename _Tp>
552 inline const _Tp&
553 imag(const complex<_Tp>& __z)
554 { return __z.imag(); }
555 #endif
556
557 // 26.2.7/3 abs(__z): Returns the magnitude of __z.
558 template<typename _Tp>
559 inline _Tp
560 __complex_abs(const complex<_Tp>& __z)
561 {
562 _Tp __x = __z.real();
563 _Tp __y = __z.imag();
564 const _Tp __s = std::max(abs(__x), abs(__y));
565 if (__s == _Tp()) // well ...
566 return __s;
567 __x /= __s;
568 __y /= __s;
569 return __s * sqrt(__x * __x + __y * __y);
570 }
571
572 #if _GLIBCXX_USE_C99_COMPLEX
573 inline float
574 __complex_abs(__complex__ float __z) { return __builtin_cabsf(__z); }
575
576 inline double
577 __complex_abs(__complex__ double __z) { return __builtin_cabs(__z); }
578
579 inline long double
580 __complex_abs(const __complex__ long double& __z)
581 { return __builtin_cabsl(__z); }
582
583 template<typename _Tp>
584 inline _Tp
585 abs(const complex<_Tp>& __z) { return __complex_abs(__z.__rep()); }
586 #else
587 template<typename _Tp>
588 inline _Tp
589 abs(const complex<_Tp>& __z) { return __complex_abs(__z); }
590 #endif
591
592
593 // 26.2.7/4: arg(__z): Returns the phase angle of __z.
594 template<typename _Tp>
595 inline _Tp
596 __complex_arg(const complex<_Tp>& __z)
597 { return atan2(__z.imag(), __z.real()); }
598
599 #if _GLIBCXX_USE_C99_COMPLEX
600 inline float
601 __complex_arg(__complex__ float __z) { return __builtin_cargf(__z); }
602
603 inline double
604 __complex_arg(__complex__ double __z) { return __builtin_carg(__z); }
605
606 inline long double
607 __complex_arg(const __complex__ long double& __z)
608 { return __builtin_cargl(__z); }
609
610 template<typename _Tp>
611 inline _Tp
612 arg(const complex<_Tp>& __z) { return __complex_arg(__z.__rep()); }
613 #else
614 template<typename _Tp>
615 inline _Tp
616 arg(const complex<_Tp>& __z) { return __complex_arg(__z); }
617 #endif
618
619 // 26.2.7/5: norm(__z) returns the squared magnitude of __z.
620 // As defined, norm() is -not- a norm is the common mathematical
621 // sens used in numerics. The helper class _Norm_helper<> tries to
622 // distinguish between builtin floating point and the rest, so as
623 // to deliver an answer as close as possible to the real value.
624 template<bool>
625 struct _Norm_helper
626 {
627 template<typename _Tp>
628 static inline _Tp _S_do_it(const complex<_Tp>& __z)
629 {
630 const _Tp __x = __z.real();
631 const _Tp __y = __z.imag();
632 return __x * __x + __y * __y;
633 }
634 };
635
636 template<>
637 struct _Norm_helper<true>
638 {
639 template<typename _Tp>
640 static inline _Tp _S_do_it(const complex<_Tp>& __z)
641 {
642 _Tp __res = std::abs(__z);
643 return __res * __res;
644 }
645 };
646
647 template<typename _Tp>
648 inline _Tp
649 norm(const complex<_Tp>& __z)
650 {
651 return _Norm_helper<__is_floating<_Tp>::__value
652 && !_GLIBCXX_FAST_MATH>::_S_do_it(__z);
653 }
654
655 template<typename _Tp>
656 inline complex<_Tp>
657 polar(const _Tp& __rho, const _Tp& __theta)
658 { return complex<_Tp>(__rho * cos(__theta), __rho * sin(__theta)); }
659
660 template<typename _Tp>
661 inline complex<_Tp>
662 conj(const complex<_Tp>& __z)
663 { return complex<_Tp>(__z.real(), -__z.imag()); }
664
665 // Transcendentals
666
667 // 26.2.8/1 cos(__z): Returns the cosine of __z.
668 template<typename _Tp>
669 inline complex<_Tp>
670 __complex_cos(const complex<_Tp>& __z)
671 {
672 const _Tp __x = __z.real();
673 const _Tp __y = __z.imag();
674 return complex<_Tp>(cos(__x) * cosh(__y), -sin(__x) * sinh(__y));
675 }
676
677 #if _GLIBCXX_USE_C99_COMPLEX
678 inline __complex__ float
679 __complex_cos(__complex__ float __z) { return __builtin_ccosf(__z); }
680
681 inline __complex__ double
682 __complex_cos(__complex__ double __z) { return __builtin_ccos(__z); }
683
684 inline __complex__ long double
685 __complex_cos(const __complex__ long double& __z)
686 { return __builtin_ccosl(__z); }
687
688 template<typename _Tp>
689 inline complex<_Tp>
690 cos(const complex<_Tp>& __z) { return __complex_cos(__z.__rep()); }
691 #else
692 template<typename _Tp>
693 inline complex<_Tp>
694 cos(const complex<_Tp>& __z) { return __complex_cos(__z); }
695 #endif
696
697 // 26.2.8/2 cosh(__z): Returns the hyperbolic cosine of __z.
698 template<typename _Tp>
699 inline complex<_Tp>
700 __complex_cosh(const complex<_Tp>& __z)
701 {
702 const _Tp __x = __z.real();
703 const _Tp __y = __z.imag();
704 return complex<_Tp>(cosh(__x) * cos(__y), sinh(__x) * sin(__y));
705 }
706
707 #if _GLIBCXX_USE_C99_COMPLEX
708 inline __complex__ float
709 __complex_cosh(__complex__ float __z) { return __builtin_ccoshf(__z); }
710
711 inline __complex__ double
712 __complex_cosh(__complex__ double __z) { return __builtin_ccosh(__z); }
713
714 inline __complex__ long double
715 __complex_cosh(const __complex__ long double& __z)
716 { return __builtin_ccoshl(__z); }
717
718 template<typename _Tp>
719 inline complex<_Tp>
720 cosh(const complex<_Tp>& __z) { return __complex_cosh(__z.__rep()); }
721 #else
722 template<typename _Tp>
723 inline complex<_Tp>
724 cosh(const complex<_Tp>& __z) { return __complex_cosh(__z); }
725 #endif
726
727 // 26.2.8/3 exp(__z): Returns the complex base e exponential of x
728 template<typename _Tp>
729 inline complex<_Tp>
730 __complex_exp(const complex<_Tp>& __z)
731 { return std::polar(exp(__z.real()), __z.imag()); }
732
733 #if _GLIBCXX_USE_C99_COMPLEX
734 inline __complex__ float
735 __complex_exp(__complex__ float __z) { return __builtin_cexpf(__z); }
736
737 inline __complex__ double
738 __complex_exp(__complex__ double __z) { return __builtin_cexp(__z); }
739
740 inline __complex__ long double
741 __complex_exp(const __complex__ long double& __z)
742 { return __builtin_cexpl(__z); }
743
744 template<typename _Tp>
745 inline complex<_Tp>
746 exp(const complex<_Tp>& __z) { return __complex_exp(__z.__rep()); }
747 #else
748 template<typename _Tp>
749 inline complex<_Tp>
750 exp(const complex<_Tp>& __z) { return __complex_exp(__z); }
751 #endif
752
753 // 26.2.8/5 log(__z): Returns the natural complex logarithm of __z.
754 // The branch cut is along the negative axis.
755 template<typename _Tp>
756 inline complex<_Tp>
757 __complex_log(const complex<_Tp>& __z)
758 { return complex<_Tp>(log(std::abs(__z)), std::arg(__z)); }
759
760 #if _GLIBCXX_USE_C99_COMPLEX
761 inline __complex__ float
762 __complex_log(__complex__ float __z) { return __builtin_clogf(__z); }
763
764 inline __complex__ double
765 __complex_log(__complex__ double __z) { return __builtin_clog(__z); }
766
767 inline __complex__ long double
768 __complex_log(const __complex__ long double& __z)
769 { return __builtin_clogl(__z); }
770
771 template<typename _Tp>
772 inline complex<_Tp>
773 log(const complex<_Tp>& __z) { return __complex_log(__z.__rep()); }
774 #else
775 template<typename _Tp>
776 inline complex<_Tp>
777 log(const complex<_Tp>& __z) { return __complex_log(__z); }
778 #endif
779
780 template<typename _Tp>
781 inline complex<_Tp>
782 log10(const complex<_Tp>& __z)
783 { return std::log(__z) / log(_Tp(10.0)); }
784
785 // 26.2.8/10 sin(__z): Returns the sine of __z.
786 template<typename _Tp>
787 inline complex<_Tp>
788 __complex_sin(const complex<_Tp>& __z)
789 {
790 const _Tp __x = __z.real();
791 const _Tp __y = __z.imag();
792 return complex<_Tp>(sin(__x) * cosh(__y), cos(__x) * sinh(__y));
793 }
794
795 #if _GLIBCXX_USE_C99_COMPLEX
796 inline __complex__ float
797 __complex_sin(__complex__ float __z) { return __builtin_csinf(__z); }
798
799 inline __complex__ double
800 __complex_sin(__complex__ double __z) { return __builtin_csin(__z); }
801
802 inline __complex__ long double
803 __complex_sin(const __complex__ long double& __z)
804 { return __builtin_csinl(__z); }
805
806 template<typename _Tp>
807 inline complex<_Tp>
808 sin(const complex<_Tp>& __z) { return __complex_sin(__z.__rep()); }
809 #else
810 template<typename _Tp>
811 inline complex<_Tp>
812 sin(const complex<_Tp>& __z) { return __complex_sin(__z); }
813 #endif
814
815 // 26.2.8/11 sinh(__z): Returns the hyperbolic sine of __z.
816 template<typename _Tp>
817 inline complex<_Tp>
818 __complex_sinh(const complex<_Tp>& __z)
819 {
820 const _Tp __x = __z.real();
821 const _Tp __y = __z.imag();
822 return complex<_Tp>(sinh(__x) * cos(__y), cosh(__x) * sin(__y));
823 }
824
825 #if _GLIBCXX_USE_C99_COMPLEX
826 inline __complex__ float
827 __complex_sinh(__complex__ float __z) { return __builtin_csinhf(__z); }
828
829 inline __complex__ double
830 __complex_sinh(__complex__ double __z) { return __builtin_csinh(__z); }
831
832 inline __complex__ long double
833 __complex_sinh(const __complex__ long double& __z)
834 { return __builtin_csinhl(__z); }
835
836 template<typename _Tp>
837 inline complex<_Tp>
838 sinh(const complex<_Tp>& __z) { return __complex_sinh(__z.__rep()); }
839 #else
840 template<typename _Tp>
841 inline complex<_Tp>
842 sinh(const complex<_Tp>& __z) { return __complex_sinh(__z); }
843 #endif
844
845 // 26.2.8/13 sqrt(__z): Returns the complex square root of __z.
846 // The branch cut is on the negative axis.
847 template<typename _Tp>
848 complex<_Tp>
849 __complex_sqrt(const complex<_Tp>& __z)
850 {
851 _Tp __x = __z.real();
852 _Tp __y = __z.imag();
853
854 if (__x == _Tp())
855 {
856 _Tp __t = sqrt(abs(__y) / 2);
857 return complex<_Tp>(__t, __y < _Tp() ? -__t : __t);
858 }
859 else
860 {
861 _Tp __t = sqrt(2 * (std::abs(__z) + abs(__x)));
862 _Tp __u = __t / 2;
863 return __x > _Tp()
864 ? complex<_Tp>(__u, __y / __t)
865 : complex<_Tp>(abs(__y) / __t, __y < _Tp() ? -__u : __u);
866 }
867 }
868
869 #if _GLIBCXX_USE_C99_COMPLEX
870 inline __complex__ float
871 __complex_sqrt(__complex__ float __z) { return __builtin_csqrtf(__z); }
872
873 inline __complex__ double
874 __complex_sqrt(__complex__ double __z) { return __builtin_csqrt(__z); }
875
876 inline __complex__ long double
877 __complex_sqrt(const __complex__ long double& __z)
878 { return __builtin_csqrtl(__z); }
879
880 template<typename _Tp>
881 inline complex<_Tp>
882 sqrt(const complex<_Tp>& __z) { return __complex_sqrt(__z.__rep()); }
883 #else
884 template<typename _Tp>
885 inline complex<_Tp>
886 sqrt(const complex<_Tp>& __z) { return __complex_sqrt(__z); }
887 #endif
888
889 // 26.2.8/14 tan(__z): Return the complex tangent of __z.
890
891 template<typename _Tp>
892 inline complex<_Tp>
893 __complex_tan(const complex<_Tp>& __z)
894 { return std::sin(__z) / std::cos(__z); }
895
896 #if _GLIBCXX_USE_C99_COMPLEX
897 inline __complex__ float
898 __complex_tan(__complex__ float __z) { return __builtin_ctanf(__z); }
899
900 inline __complex__ double
901 __complex_tan(__complex__ double __z) { return __builtin_ctan(__z); }
902
903 inline __complex__ long double
904 __complex_tan(const __complex__ long double& __z)
905 { return __builtin_ctanl(__z); }
906
907 template<typename _Tp>
908 inline complex<_Tp>
909 tan(const complex<_Tp>& __z) { return __complex_tan(__z.__rep()); }
910 #else
911 template<typename _Tp>
912 inline complex<_Tp>
913 tan(const complex<_Tp>& __z) { return __complex_tan(__z); }
914 #endif
915
916
917 // 26.2.8/15 tanh(__z): Returns the hyperbolic tangent of __z.
918
919 template<typename _Tp>
920 inline complex<_Tp>
921 __complex_tanh(const complex<_Tp>& __z)
922 { return std::sinh(__z) / std::cosh(__z); }
923
924 #if _GLIBCXX_USE_C99_COMPLEX
925 inline __complex__ float
926 __complex_tanh(__complex__ float __z) { return __builtin_ctanhf(__z); }
927
928 inline __complex__ double
929 __complex_tanh(__complex__ double __z) { return __builtin_ctanh(__z); }
930
931 inline __complex__ long double
932 __complex_tanh(const __complex__ long double& __z)
933 { return __builtin_ctanhl(__z); }
934
935 template<typename _Tp>
936 inline complex<_Tp>
937 tanh(const complex<_Tp>& __z) { return __complex_tanh(__z.__rep()); }
938 #else
939 template<typename _Tp>
940 inline complex<_Tp>
941 tanh(const complex<_Tp>& __z) { return __complex_tanh(__z); }
942 #endif
943
944
945 // 26.2.8/9 pow(__x, __y): Returns the complex power base of __x
946 // raised to the __y-th power. The branch
947 // cut is on the negative axis.
948 template<typename _Tp>
949 inline complex<_Tp>
950 pow(const complex<_Tp>& __z, int __n)
951 { return std::__pow_helper(__z, __n); }
952
953 template<typename _Tp>
954 complex<_Tp>
955 pow(const complex<_Tp>& __x, const _Tp& __y)
956 {
957 #ifndef _GLIBCXX_USE_C99_COMPLEX
958 if (__x == _Tp())
959 return _Tp();
960 #endif
961 if (__x.imag() == _Tp() && __x.real() > _Tp())
962 return pow(__x.real(), __y);
963
964 complex<_Tp> __t = std::log(__x);
965 return std::polar(exp(__y * __t.real()), __y * __t.imag());
966 }
967
968 template<typename _Tp>
969 inline complex<_Tp>
970 __complex_pow(const complex<_Tp>& __x, const complex<_Tp>& __y)
971 { return __x == _Tp() ? _Tp() : std::exp(__y * std::log(__x)); }
972
973 #if _GLIBCXX_USE_C99_COMPLEX
974 inline __complex__ float
975 __complex_pow(__complex__ float __x, __complex__ float __y)
976 { return __builtin_cpowf(__x, __y); }
977
978 inline __complex__ double
979 __complex_pow(__complex__ double __x, __complex__ double __y)
980 { return __builtin_cpow(__x, __y); }
981
982 inline __complex__ long double
983 __complex_pow(const __complex__ long double& __x,
984 const __complex__ long double& __y)
985 { return __builtin_cpowl(__x, __y); }
986
987 template<typename _Tp>
988 inline complex<_Tp>
989 pow(const complex<_Tp>& __x, const complex<_Tp>& __y)
990 { return __complex_pow(__x.__rep(), __y.__rep()); }
991 #else
992 template<typename _Tp>
993 inline complex<_Tp>
994 pow(const complex<_Tp>& __x, const complex<_Tp>& __y)
995 { return __complex_pow(__x, __y); }
996 #endif
997
998 template<typename _Tp>
999 inline complex<_Tp>
1000 pow(const _Tp& __x, const complex<_Tp>& __y)
1001 {
1002 return __x > _Tp() ? std::polar(pow(__x, __y.real()),
1003 __y.imag() * log(__x))
1004 : std::pow(complex<_Tp>(__x), __y);
1005 }
1006
1007 // 26.2.3 complex specializations
1008 // complex<float> specialization
1009 template<>
1010 struct complex<float>
1011 {
1012 typedef float value_type;
1013 typedef __complex__ float _ComplexT;
1014
1015 complex(_ComplexT __z) : _M_value(__z) { }
1016
1017 complex(float __r = 0.0f, float __i = 0.0f)
1018 {
1019 __real__ _M_value = __r;
1020 __imag__ _M_value = __i;
1021 }
1022
1023 explicit complex(const complex<double>&);
1024 explicit complex(const complex<long double>&);
1025
1026 #ifdef __GXX_EXPERIMENTAL_CXX0X__
1027 // _GLIBCXX_RESOLVE_LIB_DEFECTS
1028 // DR 387. std::complex over-encapsulated.
1029 float real() const
1030 { return __real__ _M_value; }
1031
1032 float imag() const
1033 { return __imag__ _M_value; }
1034 #else
1035 float& real()
1036 { return __real__ _M_value; }
1037
1038 const float& real() const
1039 { return __real__ _M_value; }
1040
1041 float& imag()
1042 { return __imag__ _M_value; }
1043
1044 const float& imag() const
1045 { return __imag__ _M_value; }
1046 #endif
1047
1048 // _GLIBCXX_RESOLVE_LIB_DEFECTS
1049 // DR 387. std::complex over-encapsulated.
1050 void real(float __val)
1051 { __real__ _M_value = __val; }
1052
1053 void imag(float __val)
1054 { __imag__ _M_value = __val; }
1055
1056 complex<float>&
1057 operator=(float __f)
1058 {
1059 __real__ _M_value = __f;
1060 __imag__ _M_value = 0.0f;
1061 return *this;
1062 }
1063
1064 complex<float>&
1065 operator+=(float __f)
1066 {
1067 __real__ _M_value += __f;
1068 return *this;
1069 }
1070
1071 complex<float>&
1072 operator-=(float __f)
1073 {
1074 __real__ _M_value -= __f;
1075 return *this;
1076 }
1077
1078 complex<float>&
1079 operator*=(float __f)
1080 {
1081 _M_value *= __f;
1082 return *this;
1083 }
1084
1085 complex<float>&
1086 operator/=(float __f)
1087 {
1088 _M_value /= __f;
1089 return *this;
1090 }
1091
1092 // Let the compiler synthesize the copy and assignment
1093 // operator. It always does a pretty good job.
1094 // complex& operator=(const complex&);
1095
1096 template<typename _Tp>
1097 complex<float>&
1098 operator=(const complex<_Tp>& __z)
1099 {
1100 __real__ _M_value = __z.real();
1101 __imag__ _M_value = __z.imag();
1102 return *this;
1103 }
1104
1105 template<typename _Tp>
1106 complex<float>&
1107 operator+=(const complex<_Tp>& __z)
1108 {
1109 __real__ _M_value += __z.real();
1110 __imag__ _M_value += __z.imag();
1111 return *this;
1112 }
1113
1114 template<class _Tp>
1115 complex<float>&
1116 operator-=(const complex<_Tp>& __z)
1117 {
1118 __real__ _M_value -= __z.real();
1119 __imag__ _M_value -= __z.imag();
1120 return *this;
1121 }
1122
1123 template<class _Tp>
1124 complex<float>&
1125 operator*=(const complex<_Tp>& __z)
1126 {
1127 _ComplexT __t;
1128 __real__ __t = __z.real();
1129 __imag__ __t = __z.imag();
1130 _M_value *= __t;
1131 return *this;
1132 }
1133
1134 template<class _Tp>
1135 complex<float>&
1136 operator/=(const complex<_Tp>& __z)
1137 {
1138 _ComplexT __t;
1139 __real__ __t = __z.real();
1140 __imag__ __t = __z.imag();
1141 _M_value /= __t;
1142 return *this;
1143 }
1144
1145 const _ComplexT& __rep() const { return _M_value; }
1146
1147 private:
1148 _ComplexT _M_value;
1149 };
1150
1151 // 26.2.3 complex specializations
1152 // complex<double> specialization
1153 template<>
1154 struct complex<double>
1155 {
1156 typedef double value_type;
1157 typedef __complex__ double _ComplexT;
1158
1159 complex(_ComplexT __z) : _M_value(__z) { }
1160
1161 complex(double __r = 0.0, double __i = 0.0)
1162 {
1163 __real__ _M_value = __r;
1164 __imag__ _M_value = __i;
1165 }
1166
1167 complex(const complex<float>& __z)
1168 : _M_value(__z.__rep()) { }
1169
1170 explicit complex(const complex<long double>&);
1171
1172 #ifdef __GXX_EXPERIMENTAL_CXX0X__
1173 // _GLIBCXX_RESOLVE_LIB_DEFECTS
1174 // DR 387. std::complex over-encapsulated.
1175 double real() const
1176 { return __real__ _M_value; }
1177
1178 double imag() const
1179 { return __imag__ _M_value; }
1180 #else
1181 double& real()
1182 { return __real__ _M_value; }
1183
1184 const double& real() const
1185 { return __real__ _M_value; }
1186
1187 double& imag()
1188 { return __imag__ _M_value; }
1189
1190 const double& imag() const
1191 { return __imag__ _M_value; }
1192 #endif
1193
1194 // _GLIBCXX_RESOLVE_LIB_DEFECTS
1195 // DR 387. std::complex over-encapsulated.
1196 void real(double __val)
1197 { __real__ _M_value = __val; }
1198
1199 void imag(double __val)
1200 { __imag__ _M_value = __val; }
1201
1202 complex<double>&
1203 operator=(double __d)
1204 {
1205 __real__ _M_value = __d;
1206 __imag__ _M_value = 0.0;
1207 return *this;
1208 }
1209
1210 complex<double>&
1211 operator+=(double __d)
1212 {
1213 __real__ _M_value += __d;
1214 return *this;
1215 }
1216
1217 complex<double>&
1218 operator-=(double __d)
1219 {
1220 __real__ _M_value -= __d;
1221 return *this;
1222 }
1223
1224 complex<double>&
1225 operator*=(double __d)
1226 {
1227 _M_value *= __d;
1228 return *this;
1229 }
1230
1231 complex<double>&
1232 operator/=(double __d)
1233 {
1234 _M_value /= __d;
1235 return *this;
1236 }
1237
1238 // The compiler will synthesize this, efficiently.
1239 // complex& operator=(const complex&);
1240
1241 template<typename _Tp>
1242 complex<double>&
1243 operator=(const complex<_Tp>& __z)
1244 {
1245 __real__ _M_value = __z.real();
1246 __imag__ _M_value = __z.imag();
1247 return *this;
1248 }
1249
1250 template<typename _Tp>
1251 complex<double>&
1252 operator+=(const complex<_Tp>& __z)
1253 {
1254 __real__ _M_value += __z.real();
1255 __imag__ _M_value += __z.imag();
1256 return *this;
1257 }
1258
1259 template<typename _Tp>
1260 complex<double>&
1261 operator-=(const complex<_Tp>& __z)
1262 {
1263 __real__ _M_value -= __z.real();
1264 __imag__ _M_value -= __z.imag();
1265 return *this;
1266 }
1267
1268 template<typename _Tp>
1269 complex<double>&
1270 operator*=(const complex<_Tp>& __z)
1271 {
1272 _ComplexT __t;
1273 __real__ __t = __z.real();
1274 __imag__ __t = __z.imag();
1275 _M_value *= __t;
1276 return *this;
1277 }
1278
1279 template<typename _Tp>
1280 complex<double>&
1281 operator/=(const complex<_Tp>& __z)
1282 {
1283 _ComplexT __t;
1284 __real__ __t = __z.real();
1285 __imag__ __t = __z.imag();
1286 _M_value /= __t;
1287 return *this;
1288 }
1289
1290 const _ComplexT& __rep() const { return _M_value; }
1291
1292 private:
1293 _ComplexT _M_value;
1294 };
1295
1296 // 26.2.3 complex specializations
1297 // complex<long double> specialization
1298 template<>
1299 struct complex<long double>
1300 {
1301 typedef long double value_type;
1302 typedef __complex__ long double _ComplexT;
1303
1304 complex(_ComplexT __z) : _M_value(__z) { }
1305
1306 complex(long double __r = 0.0L, long double __i = 0.0L)
1307 {
1308 __real__ _M_value = __r;
1309 __imag__ _M_value = __i;
1310 }
1311
1312 complex(const complex<float>& __z)
1313 : _M_value(__z.__rep()) { }
1314
1315 complex(const complex<double>& __z)
1316 : _M_value(__z.__rep()) { }
1317
1318 #ifdef __GXX_EXPERIMENTAL_CXX0X__
1319 // _GLIBCXX_RESOLVE_LIB_DEFECTS
1320 // DR 387. std::complex over-encapsulated.
1321 long double real() const
1322 { return __real__ _M_value; }
1323
1324 long double imag() const
1325 { return __imag__ _M_value; }
1326 #else
1327 long double& real()
1328 { return __real__ _M_value; }
1329
1330 const long double& real() const
1331 { return __real__ _M_value; }
1332
1333 long double& imag()
1334 { return __imag__ _M_value; }
1335
1336 const long double& imag() const
1337 { return __imag__ _M_value; }
1338 #endif
1339
1340 // _GLIBCXX_RESOLVE_LIB_DEFECTS
1341 // DR 387. std::complex over-encapsulated.
1342 void real(long double __val)
1343 { __real__ _M_value = __val; }
1344
1345 void imag(long double __val)
1346 { __imag__ _M_value = __val; }
1347
1348 complex<long double>&
1349 operator=(long double __r)
1350 {
1351 __real__ _M_value = __r;
1352 __imag__ _M_value = 0.0L;
1353 return *this;
1354 }
1355
1356 complex<long double>&
1357 operator+=(long double __r)
1358 {
1359 __real__ _M_value += __r;
1360 return *this;
1361 }
1362
1363 complex<long double>&
1364 operator-=(long double __r)
1365 {
1366 __real__ _M_value -= __r;
1367 return *this;
1368 }
1369
1370 complex<long double>&
1371 operator*=(long double __r)
1372 {
1373 _M_value *= __r;
1374 return *this;
1375 }
1376
1377 complex<long double>&
1378 operator/=(long double __r)
1379 {
1380 _M_value /= __r;
1381 return *this;
1382 }
1383
1384 // The compiler knows how to do this efficiently
1385 // complex& operator=(const complex&);
1386
1387 template<typename _Tp>
1388 complex<long double>&
1389 operator=(const complex<_Tp>& __z)
1390 {
1391 __real__ _M_value = __z.real();
1392 __imag__ _M_value = __z.imag();
1393 return *this;
1394 }
1395
1396 template<typename _Tp>
1397 complex<long double>&
1398 operator+=(const complex<_Tp>& __z)
1399 {
1400 __real__ _M_value += __z.real();
1401 __imag__ _M_value += __z.imag();
1402 return *this;
1403 }
1404
1405 template<typename _Tp>
1406 complex<long double>&
1407 operator-=(const complex<_Tp>& __z)
1408 {
1409 __real__ _M_value -= __z.real();
1410 __imag__ _M_value -= __z.imag();
1411 return *this;
1412 }
1413
1414 template<typename _Tp>
1415 complex<long double>&
1416 operator*=(const complex<_Tp>& __z)
1417 {
1418 _ComplexT __t;
1419 __real__ __t = __z.real();
1420 __imag__ __t = __z.imag();
1421 _M_value *= __t;
1422 return *this;
1423 }
1424
1425 template<typename _Tp>
1426 complex<long double>&
1427 operator/=(const complex<_Tp>& __z)
1428 {
1429 _ComplexT __t;
1430 __real__ __t = __z.real();
1431 __imag__ __t = __z.imag();
1432 _M_value /= __t;
1433 return *this;
1434 }
1435
1436 const _ComplexT& __rep() const { return _M_value; }
1437
1438 private:
1439 _ComplexT _M_value;
1440 };
1441
1442 // These bits have to be at the end of this file, so that the
1443 // specializations have all been defined.
1444 // ??? No, they have to be there because of compiler limitation at
1445 // inlining. It suffices that class specializations be defined.
1446 inline
1447 complex<float>::complex(const complex<double>& __z)
1448 : _M_value(__z.__rep()) { }
1449
1450 inline
1451 complex<float>::complex(const complex<long double>& __z)
1452 : _M_value(__z.__rep()) { }
1453
1454 inline
1455 complex<double>::complex(const complex<long double>& __z)
1456 : _M_value(__z.__rep()) { }
1457
1458 // Inhibit implicit instantiations for required instantiations,
1459 // which are defined via explicit instantiations elsewhere.
1460 // NB: This syntax is a GNU extension.
1461 #if _GLIBCXX_EXTERN_TEMPLATE
1462 extern template istream& operator>>(istream&, complex<float>&);
1463 extern template ostream& operator<<(ostream&, const complex<float>&);
1464 extern template istream& operator>>(istream&, complex<double>&);
1465 extern template ostream& operator<<(ostream&, const complex<double>&);
1466 extern template istream& operator>>(istream&, complex<long double>&);
1467 extern template ostream& operator<<(ostream&, const complex<long double>&);
1468
1469 #ifdef _GLIBCXX_USE_WCHAR_T
1470 extern template wistream& operator>>(wistream&, complex<float>&);
1471 extern template wostream& operator<<(wostream&, const complex<float>&);
1472 extern template wistream& operator>>(wistream&, complex<double>&);
1473 extern template wostream& operator<<(wostream&, const complex<double>&);
1474 extern template wistream& operator>>(wistream&, complex<long double>&);
1475 extern template wostream& operator<<(wostream&, const complex<long double>&);
1476 #endif
1477 #endif
1478
1479 _GLIBCXX_END_NAMESPACE
1480
1481 _GLIBCXX_BEGIN_NAMESPACE(__gnu_cxx)
1482
1483 // See ext/type_traits.h for the primary template.
1484 template<typename _Tp, typename _Up>
1485 struct __promote_2<std::complex<_Tp>, _Up>
1486 {
1487 public:
1488 typedef std::complex<typename __promote_2<_Tp, _Up>::__type> __type;
1489 };
1490
1491 template<typename _Tp, typename _Up>
1492 struct __promote_2<_Tp, std::complex<_Up> >
1493 {
1494 public:
1495 typedef std::complex<typename __promote_2<_Tp, _Up>::__type> __type;
1496 };
1497
1498 template<typename _Tp, typename _Up>
1499 struct __promote_2<std::complex<_Tp>, std::complex<_Up> >
1500 {
1501 public:
1502 typedef std::complex<typename __promote_2<_Tp, _Up>::__type> __type;
1503 };
1504
1505 _GLIBCXX_END_NAMESPACE
1506
1507 #ifdef __GXX_EXPERIMENTAL_CXX0X__
1508 # if defined(_GLIBCXX_INCLUDE_AS_TR1)
1509 # error C++0x header cannot be included from TR1 header
1510 # endif
1511 # if defined(_GLIBCXX_INCLUDE_AS_CXX0X)
1512 # include <tr1_impl/complex>
1513 # else
1514 # define _GLIBCXX_INCLUDE_AS_CXX0X
1515 # define _GLIBCXX_BEGIN_NAMESPACE_TR1
1516 # define _GLIBCXX_END_NAMESPACE_TR1
1517 # define _GLIBCXX_TR1
1518 # include <tr1_impl/complex>
1519 # undef _GLIBCXX_TR1
1520 # undef _GLIBCXX_END_NAMESPACE_TR1
1521 # undef _GLIBCXX_BEGIN_NAMESPACE_TR1
1522 # undef _GLIBCXX_INCLUDE_AS_CXX0X
1523 # endif
1524
1525 _GLIBCXX_BEGIN_NAMESPACE(std)
1526
1527 // Forward declarations.
1528 // DR 781.
1529 template<typename _Tp> std::complex<_Tp> proj(const std::complex<_Tp>&);
1530
1531 template<typename _Tp>
1532 std::complex<_Tp>
1533 __complex_proj(const std::complex<_Tp>& __z)
1534 {
1535 const _Tp __den = (__z.real() * __z.real()
1536 + __z.imag() * __z.imag() + _Tp(1.0));
1537
1538 return std::complex<_Tp>((_Tp(2.0) * __z.real()) / __den,
1539 (_Tp(2.0) * __z.imag()) / __den);
1540 }
1541
1542 #if _GLIBCXX_USE_C99_COMPLEX
1543 inline __complex__ float
1544 __complex_proj(__complex__ float __z)
1545 { return __builtin_cprojf(__z); }
1546
1547 inline __complex__ double
1548 __complex_proj(__complex__ double __z)
1549 { return __builtin_cproj(__z); }
1550
1551 inline __complex__ long double
1552 __complex_proj(const __complex__ long double& __z)
1553 { return __builtin_cprojl(__z); }
1554
1555 template<typename _Tp>
1556 inline std::complex<_Tp>
1557 proj(const std::complex<_Tp>& __z)
1558 { return __complex_proj(__z.__rep()); }
1559 #else
1560 template<typename _Tp>
1561 inline std::complex<_Tp>
1562 proj(const std::complex<_Tp>& __z)
1563 { return __complex_proj(__z); }
1564 #endif
1565
1566 template<typename _Tp>
1567 inline std::complex<typename __gnu_cxx::__promote<_Tp>::__type>
1568 proj(_Tp __x)
1569 {
1570 typedef typename __gnu_cxx::__promote<_Tp>::__type __type;
1571 return std::proj(std::complex<__type>(__x));
1572 }
1573
1574 _GLIBCXX_END_NAMESPACE
1575
1576 #endif
1577
1578 #endif /* _GLIBCXX_COMPLEX */