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