re PR libstdc++/56085 (Unsafe negation in C++03 pow(complex,int))
[gcc.git] / libstdc++-v3 / include / std / complex
1 // The template and inlines for the -*- C++ -*- complex number classes.
2
3 // Copyright (C) 1997-2013 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 3, 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 // Under Section 7 of GPL version 3, you are granted additional
17 // permissions described in the GCC Runtime Library Exception, version
18 // 3.1, as published by the Free Software Foundation.
19
20 // You should have received a copy of the GNU General Public License and
21 // a copy of the GCC Runtime Library Exception along with this program;
22 // see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
23 // <http://www.gnu.org/licenses/>.
24
25 /** @file include/complex
26 * This is a Standard C++ Library header.
27 */
28
29 //
30 // ISO C++ 14882: 26.2 Complex Numbers
31 // Note: this is not a conforming implementation.
32 // Initially implemented by Ulrich Drepper <drepper@cygnus.com>
33 // Improved by Gabriel Dos Reis <dosreis@cmla.ens-cachan.fr>
34 //
35
36 #ifndef _GLIBCXX_COMPLEX
37 #define _GLIBCXX_COMPLEX 1
38
39 #pragma GCC system_header
40
41 #include <bits/c++config.h>
42 #include <bits/cpp_type_traits.h>
43 #include <ext/type_traits.h>
44 #include <cmath>
45 #include <sstream>
46
47 namespace std _GLIBCXX_VISIBILITY(default)
48 {
49 _GLIBCXX_BEGIN_NAMESPACE_VERSION
50
51 /**
52 * @defgroup complex_numbers Complex Numbers
53 * @ingroup numerics
54 *
55 * Classes and functions for complex numbers.
56 * @{
57 */
58
59 // Forward declarations.
60 template<typename _Tp> class complex;
61 template<> class complex<float>;
62 template<> class complex<double>;
63 template<> class complex<long double>;
64
65 /// Return magnitude of @a z.
66 template<typename _Tp> _Tp abs(const complex<_Tp>&);
67 /// Return phase angle of @a z.
68 template<typename _Tp> _Tp arg(const complex<_Tp>&);
69 /// Return @a z magnitude squared.
70 template<typename _Tp> _Tp norm(const complex<_Tp>&);
71
72 /// Return complex conjugate of @a z.
73 template<typename _Tp> complex<_Tp> conj(const complex<_Tp>&);
74 /// Return complex with magnitude @a rho and angle @a theta.
75 template<typename _Tp> complex<_Tp> polar(const _Tp&, const _Tp& = 0);
76
77 // Transcendentals:
78 /// Return complex cosine of @a z.
79 template<typename _Tp> complex<_Tp> cos(const complex<_Tp>&);
80 /// Return complex hyperbolic cosine of @a z.
81 template<typename _Tp> complex<_Tp> cosh(const complex<_Tp>&);
82 /// Return complex base e exponential of @a z.
83 template<typename _Tp> complex<_Tp> exp(const complex<_Tp>&);
84 /// Return complex natural logarithm of @a z.
85 template<typename _Tp> complex<_Tp> log(const complex<_Tp>&);
86 /// Return complex base 10 logarithm of @a z.
87 template<typename _Tp> complex<_Tp> log10(const complex<_Tp>&);
88 #if __cplusplus < 201103L
89 // DR 844.
90 /// Return @a x to the @a y'th power.
91 template<typename _Tp> complex<_Tp> pow(const complex<_Tp>&, int);
92 #endif
93 /// Return @a x to the @a y'th power.
94 template<typename _Tp> complex<_Tp> pow(const complex<_Tp>&, const _Tp&);
95 /// Return @a x to the @a y'th power.
96 template<typename _Tp> complex<_Tp> pow(const complex<_Tp>&,
97 const complex<_Tp>&);
98 /// Return @a x to the @a y'th power.
99 template<typename _Tp> complex<_Tp> pow(const _Tp&, const complex<_Tp>&);
100 /// Return complex sine of @a z.
101 template<typename _Tp> complex<_Tp> sin(const complex<_Tp>&);
102 /// Return complex hyperbolic sine of @a z.
103 template<typename _Tp> complex<_Tp> sinh(const complex<_Tp>&);
104 /// Return complex square root of @a z.
105 template<typename _Tp> complex<_Tp> sqrt(const complex<_Tp>&);
106 /// Return complex tangent of @a z.
107 template<typename _Tp> complex<_Tp> tan(const complex<_Tp>&);
108 /// Return complex hyperbolic tangent of @a z.
109 template<typename _Tp> complex<_Tp> tanh(const complex<_Tp>&);
110
111
112 // 26.2.2 Primary template class complex
113 /**
114 * Template to represent complex numbers.
115 *
116 * Specializations for float, double, and long double are part of the
117 * library. Results with any other type are not guaranteed.
118 *
119 * @param Tp Type of real and imaginary values.
120 */
121 template<typename _Tp>
122 struct complex
123 {
124 /// Value typedef.
125 typedef _Tp value_type;
126
127 /// Default constructor. First parameter is x, second parameter is y.
128 /// Unspecified parameters default to 0.
129 _GLIBCXX_CONSTEXPR complex(const _Tp& __r = _Tp(), const _Tp& __i = _Tp())
130 : _M_real(__r), _M_imag(__i) { }
131
132 // Lets the compiler synthesize the copy constructor
133 // complex (const complex<_Tp>&);
134 /// Copy constructor.
135 template<typename _Up>
136 _GLIBCXX_CONSTEXPR complex(const complex<_Up>& __z)
137 : _M_real(__z.real()), _M_imag(__z.imag()) { }
138
139 #if __cplusplus >= 201103L
140 // _GLIBCXX_RESOLVE_LIB_DEFECTS
141 // DR 387. std::complex over-encapsulated.
142 __attribute ((__abi_tag__ ("cxx11")))
143 constexpr _Tp
144 real() { return _M_real; }
145
146 __attribute ((__abi_tag__ ("cxx11")))
147 constexpr _Tp
148 imag() { return _M_imag; }
149 #else
150 /// Return real part of complex number.
151 _Tp&
152 real() { return _M_real; }
153
154 /// Return real part of complex number.
155 const _Tp&
156 real() const { return _M_real; }
157
158 /// Return imaginary part of complex number.
159 _Tp&
160 imag() { return _M_imag; }
161
162 /// Return imaginary part of complex number.
163 const _Tp&
164 imag() const { return _M_imag; }
165 #endif
166
167 // _GLIBCXX_RESOLVE_LIB_DEFECTS
168 // DR 387. std::complex over-encapsulated.
169 void
170 real(_Tp __val) { _M_real = __val; }
171
172 void
173 imag(_Tp __val) { _M_imag = __val; }
174
175 /// Assign this complex number to scalar @a t.
176 complex<_Tp>& operator=(const _Tp&);
177
178 /// Add @a t to this complex number.
179 // 26.2.5/1
180 complex<_Tp>&
181 operator+=(const _Tp& __t)
182 {
183 _M_real += __t;
184 return *this;
185 }
186
187 /// Subtract @a t from this complex number.
188 // 26.2.5/3
189 complex<_Tp>&
190 operator-=(const _Tp& __t)
191 {
192 _M_real -= __t;
193 return *this;
194 }
195
196 /// Multiply this complex number by @a t.
197 complex<_Tp>& operator*=(const _Tp&);
198 /// Divide this complex number by @a t.
199 complex<_Tp>& operator/=(const _Tp&);
200
201 // Lets the compiler synthesize the
202 // copy and assignment operator
203 // complex<_Tp>& operator= (const complex<_Tp>&);
204 /// Assign this complex number to complex @a z.
205 template<typename _Up>
206 complex<_Tp>& operator=(const complex<_Up>&);
207 /// Add @a z to this complex number.
208 template<typename _Up>
209 complex<_Tp>& operator+=(const complex<_Up>&);
210 /// Subtract @a z from this complex number.
211 template<typename _Up>
212 complex<_Tp>& operator-=(const complex<_Up>&);
213 /// Multiply this complex number by @a z.
214 template<typename _Up>
215 complex<_Tp>& operator*=(const complex<_Up>&);
216 /// Divide this complex number by @a z.
217 template<typename _Up>
218 complex<_Tp>& operator/=(const complex<_Up>&);
219
220 _GLIBCXX_USE_CONSTEXPR complex __rep() const
221 { return *this; }
222
223 private:
224 _Tp _M_real;
225 _Tp _M_imag;
226 };
227
228 template<typename _Tp>
229 complex<_Tp>&
230 complex<_Tp>::operator=(const _Tp& __t)
231 {
232 _M_real = __t;
233 _M_imag = _Tp();
234 return *this;
235 }
236
237 // 26.2.5/5
238 template<typename _Tp>
239 complex<_Tp>&
240 complex<_Tp>::operator*=(const _Tp& __t)
241 {
242 _M_real *= __t;
243 _M_imag *= __t;
244 return *this;
245 }
246
247 // 26.2.5/7
248 template<typename _Tp>
249 complex<_Tp>&
250 complex<_Tp>::operator/=(const _Tp& __t)
251 {
252 _M_real /= __t;
253 _M_imag /= __t;
254 return *this;
255 }
256
257 template<typename _Tp>
258 template<typename _Up>
259 complex<_Tp>&
260 complex<_Tp>::operator=(const complex<_Up>& __z)
261 {
262 _M_real = __z.real();
263 _M_imag = __z.imag();
264 return *this;
265 }
266
267 // 26.2.5/9
268 template<typename _Tp>
269 template<typename _Up>
270 complex<_Tp>&
271 complex<_Tp>::operator+=(const complex<_Up>& __z)
272 {
273 _M_real += __z.real();
274 _M_imag += __z.imag();
275 return *this;
276 }
277
278 // 26.2.5/11
279 template<typename _Tp>
280 template<typename _Up>
281 complex<_Tp>&
282 complex<_Tp>::operator-=(const complex<_Up>& __z)
283 {
284 _M_real -= __z.real();
285 _M_imag -= __z.imag();
286 return *this;
287 }
288
289 // 26.2.5/13
290 // XXX: This is a grammar school implementation.
291 template<typename _Tp>
292 template<typename _Up>
293 complex<_Tp>&
294 complex<_Tp>::operator*=(const complex<_Up>& __z)
295 {
296 const _Tp __r = _M_real * __z.real() - _M_imag * __z.imag();
297 _M_imag = _M_real * __z.imag() + _M_imag * __z.real();
298 _M_real = __r;
299 return *this;
300 }
301
302 // 26.2.5/15
303 // XXX: This is a grammar school implementation.
304 template<typename _Tp>
305 template<typename _Up>
306 complex<_Tp>&
307 complex<_Tp>::operator/=(const complex<_Up>& __z)
308 {
309 const _Tp __r = _M_real * __z.real() + _M_imag * __z.imag();
310 const _Tp __n = std::norm(__z);
311 _M_imag = (_M_imag * __z.real() - _M_real * __z.imag()) / __n;
312 _M_real = __r / __n;
313 return *this;
314 }
315
316 // Operators:
317 //@{
318 /// Return new complex value @a x plus @a y.
319 template<typename _Tp>
320 inline complex<_Tp>
321 operator+(const complex<_Tp>& __x, const complex<_Tp>& __y)
322 {
323 complex<_Tp> __r = __x;
324 __r += __y;
325 return __r;
326 }
327
328 template<typename _Tp>
329 inline complex<_Tp>
330 operator+(const complex<_Tp>& __x, const _Tp& __y)
331 {
332 complex<_Tp> __r = __x;
333 __r += __y;
334 return __r;
335 }
336
337 template<typename _Tp>
338 inline complex<_Tp>
339 operator+(const _Tp& __x, const complex<_Tp>& __y)
340 {
341 complex<_Tp> __r = __y;
342 __r += __x;
343 return __r;
344 }
345 //@}
346
347 //@{
348 /// Return new complex value @a x minus @a y.
349 template<typename _Tp>
350 inline complex<_Tp>
351 operator-(const complex<_Tp>& __x, const complex<_Tp>& __y)
352 {
353 complex<_Tp> __r = __x;
354 __r -= __y;
355 return __r;
356 }
357
358 template<typename _Tp>
359 inline complex<_Tp>
360 operator-(const complex<_Tp>& __x, const _Tp& __y)
361 {
362 complex<_Tp> __r = __x;
363 __r -= __y;
364 return __r;
365 }
366
367 template<typename _Tp>
368 inline complex<_Tp>
369 operator-(const _Tp& __x, const complex<_Tp>& __y)
370 {
371 complex<_Tp> __r(__x, -__y.imag());
372 __r -= __y.real();
373 return __r;
374 }
375 //@}
376
377 //@{
378 /// Return new complex value @a x times @a y.
379 template<typename _Tp>
380 inline complex<_Tp>
381 operator*(const complex<_Tp>& __x, const complex<_Tp>& __y)
382 {
383 complex<_Tp> __r = __x;
384 __r *= __y;
385 return __r;
386 }
387
388 template<typename _Tp>
389 inline complex<_Tp>
390 operator*(const complex<_Tp>& __x, const _Tp& __y)
391 {
392 complex<_Tp> __r = __x;
393 __r *= __y;
394 return __r;
395 }
396
397 template<typename _Tp>
398 inline complex<_Tp>
399 operator*(const _Tp& __x, const complex<_Tp>& __y)
400 {
401 complex<_Tp> __r = __y;
402 __r *= __x;
403 return __r;
404 }
405 //@}
406
407 //@{
408 /// Return new complex value @a x divided by @a y.
409 template<typename _Tp>
410 inline complex<_Tp>
411 operator/(const complex<_Tp>& __x, const complex<_Tp>& __y)
412 {
413 complex<_Tp> __r = __x;
414 __r /= __y;
415 return __r;
416 }
417
418 template<typename _Tp>
419 inline complex<_Tp>
420 operator/(const complex<_Tp>& __x, const _Tp& __y)
421 {
422 complex<_Tp> __r = __x;
423 __r /= __y;
424 return __r;
425 }
426
427 template<typename _Tp>
428 inline complex<_Tp>
429 operator/(const _Tp& __x, const complex<_Tp>& __y)
430 {
431 complex<_Tp> __r = __x;
432 __r /= __y;
433 return __r;
434 }
435 //@}
436
437 /// Return @a x.
438 template<typename _Tp>
439 inline complex<_Tp>
440 operator+(const complex<_Tp>& __x)
441 { return __x; }
442
443 /// Return complex negation of @a x.
444 template<typename _Tp>
445 inline complex<_Tp>
446 operator-(const complex<_Tp>& __x)
447 { return complex<_Tp>(-__x.real(), -__x.imag()); }
448
449 //@{
450 /// Return true if @a x is equal to @a y.
451 template<typename _Tp>
452 inline _GLIBCXX_CONSTEXPR bool
453 operator==(const complex<_Tp>& __x, const complex<_Tp>& __y)
454 { return __x.real() == __y.real() && __x.imag() == __y.imag(); }
455
456 template<typename _Tp>
457 inline _GLIBCXX_CONSTEXPR bool
458 operator==(const complex<_Tp>& __x, const _Tp& __y)
459 { return __x.real() == __y && __x.imag() == _Tp(); }
460
461 template<typename _Tp>
462 inline _GLIBCXX_CONSTEXPR bool
463 operator==(const _Tp& __x, const complex<_Tp>& __y)
464 { return __x == __y.real() && _Tp() == __y.imag(); }
465 //@}
466
467 //@{
468 /// Return false if @a x is equal to @a y.
469 template<typename _Tp>
470 inline _GLIBCXX_CONSTEXPR bool
471 operator!=(const complex<_Tp>& __x, const complex<_Tp>& __y)
472 { return __x.real() != __y.real() || __x.imag() != __y.imag(); }
473
474 template<typename _Tp>
475 inline _GLIBCXX_CONSTEXPR bool
476 operator!=(const complex<_Tp>& __x, const _Tp& __y)
477 { return __x.real() != __y || __x.imag() != _Tp(); }
478
479 template<typename _Tp>
480 inline _GLIBCXX_CONSTEXPR bool
481 operator!=(const _Tp& __x, const complex<_Tp>& __y)
482 { return __x != __y.real() || _Tp() != __y.imag(); }
483 //@}
484
485 /// Extraction operator for complex values.
486 template<typename _Tp, typename _CharT, class _Traits>
487 basic_istream<_CharT, _Traits>&
488 operator>>(basic_istream<_CharT, _Traits>& __is, complex<_Tp>& __x)
489 {
490 _Tp __re_x, __im_x;
491 _CharT __ch;
492 __is >> __ch;
493 if (__ch == '(')
494 {
495 __is >> __re_x >> __ch;
496 if (__ch == ',')
497 {
498 __is >> __im_x >> __ch;
499 if (__ch == ')')
500 __x = complex<_Tp>(__re_x, __im_x);
501 else
502 __is.setstate(ios_base::failbit);
503 }
504 else if (__ch == ')')
505 __x = __re_x;
506 else
507 __is.setstate(ios_base::failbit);
508 }
509 else
510 {
511 __is.putback(__ch);
512 __is >> __re_x;
513 __x = __re_x;
514 }
515 return __is;
516 }
517
518 /// Insertion operator for complex values.
519 template<typename _Tp, typename _CharT, class _Traits>
520 basic_ostream<_CharT, _Traits>&
521 operator<<(basic_ostream<_CharT, _Traits>& __os, const complex<_Tp>& __x)
522 {
523 basic_ostringstream<_CharT, _Traits> __s;
524 __s.flags(__os.flags());
525 __s.imbue(__os.getloc());
526 __s.precision(__os.precision());
527 __s << '(' << __x.real() << ',' << __x.imag() << ')';
528 return __os << __s.str();
529 }
530
531 // Values
532 #if __cplusplus >= 201103L
533 template<typename _Tp>
534 constexpr _Tp
535 real(const complex<_Tp>& __z)
536 { return __z.real(); }
537
538 template<typename _Tp>
539 constexpr _Tp
540 imag(const complex<_Tp>& __z)
541 { return __z.imag(); }
542 #else
543 template<typename _Tp>
544 inline _Tp&
545 real(complex<_Tp>& __z)
546 { return __z.real(); }
547
548 template<typename _Tp>
549 inline const _Tp&
550 real(const complex<_Tp>& __z)
551 { return __z.real(); }
552
553 template<typename _Tp>
554 inline _Tp&
555 imag(complex<_Tp>& __z)
556 { return __z.imag(); }
557
558 template<typename _Tp>
559 inline const _Tp&
560 imag(const complex<_Tp>& __z)
561 { return __z.imag(); }
562 #endif
563
564 // 26.2.7/3 abs(__z): Returns the magnitude of __z.
565 template<typename _Tp>
566 inline _Tp
567 __complex_abs(const complex<_Tp>& __z)
568 {
569 _Tp __x = __z.real();
570 _Tp __y = __z.imag();
571 const _Tp __s = std::max(abs(__x), abs(__y));
572 if (__s == _Tp()) // well ...
573 return __s;
574 __x /= __s;
575 __y /= __s;
576 return __s * sqrt(__x * __x + __y * __y);
577 }
578
579 #if _GLIBCXX_USE_C99_COMPLEX
580 inline float
581 __complex_abs(__complex__ float __z) { return __builtin_cabsf(__z); }
582
583 inline double
584 __complex_abs(__complex__ double __z) { return __builtin_cabs(__z); }
585
586 inline long double
587 __complex_abs(const __complex__ long double& __z)
588 { return __builtin_cabsl(__z); }
589
590 template<typename _Tp>
591 inline _Tp
592 abs(const complex<_Tp>& __z) { return __complex_abs(__z.__rep()); }
593 #else
594 template<typename _Tp>
595 inline _Tp
596 abs(const complex<_Tp>& __z) { return __complex_abs(__z); }
597 #endif
598
599
600 // 26.2.7/4: arg(__z): Returns the phase angle of __z.
601 template<typename _Tp>
602 inline _Tp
603 __complex_arg(const complex<_Tp>& __z)
604 { return atan2(__z.imag(), __z.real()); }
605
606 #if _GLIBCXX_USE_C99_COMPLEX
607 inline float
608 __complex_arg(__complex__ float __z) { return __builtin_cargf(__z); }
609
610 inline double
611 __complex_arg(__complex__ double __z) { return __builtin_carg(__z); }
612
613 inline long double
614 __complex_arg(const __complex__ long double& __z)
615 { return __builtin_cargl(__z); }
616
617 template<typename _Tp>
618 inline _Tp
619 arg(const complex<_Tp>& __z) { return __complex_arg(__z.__rep()); }
620 #else
621 template<typename _Tp>
622 inline _Tp
623 arg(const complex<_Tp>& __z) { return __complex_arg(__z); }
624 #endif
625
626 // 26.2.7/5: norm(__z) returns the squared magnitude of __z.
627 // As defined, norm() is -not- a norm is the common mathematical
628 // sens used in numerics. The helper class _Norm_helper<> tries to
629 // distinguish between builtin floating point and the rest, so as
630 // to deliver an answer as close as possible to the real value.
631 template<bool>
632 struct _Norm_helper
633 {
634 template<typename _Tp>
635 static inline _Tp _S_do_it(const complex<_Tp>& __z)
636 {
637 const _Tp __x = __z.real();
638 const _Tp __y = __z.imag();
639 return __x * __x + __y * __y;
640 }
641 };
642
643 template<>
644 struct _Norm_helper<true>
645 {
646 template<typename _Tp>
647 static inline _Tp _S_do_it(const complex<_Tp>& __z)
648 {
649 _Tp __res = std::abs(__z);
650 return __res * __res;
651 }
652 };
653
654 template<typename _Tp>
655 inline _Tp
656 norm(const complex<_Tp>& __z)
657 {
658 return _Norm_helper<__is_floating<_Tp>::__value
659 && !_GLIBCXX_FAST_MATH>::_S_do_it(__z);
660 }
661
662 template<typename _Tp>
663 inline complex<_Tp>
664 polar(const _Tp& __rho, const _Tp& __theta)
665 { return complex<_Tp>(__rho * cos(__theta), __rho * sin(__theta)); }
666
667 template<typename _Tp>
668 inline complex<_Tp>
669 conj(const complex<_Tp>& __z)
670 { return complex<_Tp>(__z.real(), -__z.imag()); }
671
672 // Transcendentals
673
674 // 26.2.8/1 cos(__z): Returns the cosine of __z.
675 template<typename _Tp>
676 inline complex<_Tp>
677 __complex_cos(const complex<_Tp>& __z)
678 {
679 const _Tp __x = __z.real();
680 const _Tp __y = __z.imag();
681 return complex<_Tp>(cos(__x) * cosh(__y), -sin(__x) * sinh(__y));
682 }
683
684 #if _GLIBCXX_USE_C99_COMPLEX
685 inline __complex__ float
686 __complex_cos(__complex__ float __z) { return __builtin_ccosf(__z); }
687
688 inline __complex__ double
689 __complex_cos(__complex__ double __z) { return __builtin_ccos(__z); }
690
691 inline __complex__ long double
692 __complex_cos(const __complex__ long double& __z)
693 { return __builtin_ccosl(__z); }
694
695 template<typename _Tp>
696 inline complex<_Tp>
697 cos(const complex<_Tp>& __z) { return __complex_cos(__z.__rep()); }
698 #else
699 template<typename _Tp>
700 inline complex<_Tp>
701 cos(const complex<_Tp>& __z) { return __complex_cos(__z); }
702 #endif
703
704 // 26.2.8/2 cosh(__z): Returns the hyperbolic cosine of __z.
705 template<typename _Tp>
706 inline complex<_Tp>
707 __complex_cosh(const complex<_Tp>& __z)
708 {
709 const _Tp __x = __z.real();
710 const _Tp __y = __z.imag();
711 return complex<_Tp>(cosh(__x) * cos(__y), sinh(__x) * sin(__y));
712 }
713
714 #if _GLIBCXX_USE_C99_COMPLEX
715 inline __complex__ float
716 __complex_cosh(__complex__ float __z) { return __builtin_ccoshf(__z); }
717
718 inline __complex__ double
719 __complex_cosh(__complex__ double __z) { return __builtin_ccosh(__z); }
720
721 inline __complex__ long double
722 __complex_cosh(const __complex__ long double& __z)
723 { return __builtin_ccoshl(__z); }
724
725 template<typename _Tp>
726 inline complex<_Tp>
727 cosh(const complex<_Tp>& __z) { return __complex_cosh(__z.__rep()); }
728 #else
729 template<typename _Tp>
730 inline complex<_Tp>
731 cosh(const complex<_Tp>& __z) { return __complex_cosh(__z); }
732 #endif
733
734 // 26.2.8/3 exp(__z): Returns the complex base e exponential of x
735 template<typename _Tp>
736 inline complex<_Tp>
737 __complex_exp(const complex<_Tp>& __z)
738 { return std::polar(exp(__z.real()), __z.imag()); }
739
740 #if _GLIBCXX_USE_C99_COMPLEX
741 inline __complex__ float
742 __complex_exp(__complex__ float __z) { return __builtin_cexpf(__z); }
743
744 inline __complex__ double
745 __complex_exp(__complex__ double __z) { return __builtin_cexp(__z); }
746
747 inline __complex__ long double
748 __complex_exp(const __complex__ long double& __z)
749 { return __builtin_cexpl(__z); }
750
751 template<typename _Tp>
752 inline complex<_Tp>
753 exp(const complex<_Tp>& __z) { return __complex_exp(__z.__rep()); }
754 #else
755 template<typename _Tp>
756 inline complex<_Tp>
757 exp(const complex<_Tp>& __z) { return __complex_exp(__z); }
758 #endif
759
760 // 26.2.8/5 log(__z): Returns the natural complex logarithm of __z.
761 // The branch cut is along the negative axis.
762 template<typename _Tp>
763 inline complex<_Tp>
764 __complex_log(const complex<_Tp>& __z)
765 { return complex<_Tp>(log(std::abs(__z)), std::arg(__z)); }
766
767 #if _GLIBCXX_USE_C99_COMPLEX
768 inline __complex__ float
769 __complex_log(__complex__ float __z) { return __builtin_clogf(__z); }
770
771 inline __complex__ double
772 __complex_log(__complex__ double __z) { return __builtin_clog(__z); }
773
774 inline __complex__ long double
775 __complex_log(const __complex__ long double& __z)
776 { return __builtin_clogl(__z); }
777
778 template<typename _Tp>
779 inline complex<_Tp>
780 log(const complex<_Tp>& __z) { return __complex_log(__z.__rep()); }
781 #else
782 template<typename _Tp>
783 inline complex<_Tp>
784 log(const complex<_Tp>& __z) { return __complex_log(__z); }
785 #endif
786
787 template<typename _Tp>
788 inline complex<_Tp>
789 log10(const complex<_Tp>& __z)
790 { return std::log(__z) / log(_Tp(10.0)); }
791
792 // 26.2.8/10 sin(__z): Returns the sine of __z.
793 template<typename _Tp>
794 inline complex<_Tp>
795 __complex_sin(const complex<_Tp>& __z)
796 {
797 const _Tp __x = __z.real();
798 const _Tp __y = __z.imag();
799 return complex<_Tp>(sin(__x) * cosh(__y), cos(__x) * sinh(__y));
800 }
801
802 #if _GLIBCXX_USE_C99_COMPLEX
803 inline __complex__ float
804 __complex_sin(__complex__ float __z) { return __builtin_csinf(__z); }
805
806 inline __complex__ double
807 __complex_sin(__complex__ double __z) { return __builtin_csin(__z); }
808
809 inline __complex__ long double
810 __complex_sin(const __complex__ long double& __z)
811 { return __builtin_csinl(__z); }
812
813 template<typename _Tp>
814 inline complex<_Tp>
815 sin(const complex<_Tp>& __z) { return __complex_sin(__z.__rep()); }
816 #else
817 template<typename _Tp>
818 inline complex<_Tp>
819 sin(const complex<_Tp>& __z) { return __complex_sin(__z); }
820 #endif
821
822 // 26.2.8/11 sinh(__z): Returns the hyperbolic sine of __z.
823 template<typename _Tp>
824 inline complex<_Tp>
825 __complex_sinh(const complex<_Tp>& __z)
826 {
827 const _Tp __x = __z.real();
828 const _Tp __y = __z.imag();
829 return complex<_Tp>(sinh(__x) * cos(__y), cosh(__x) * sin(__y));
830 }
831
832 #if _GLIBCXX_USE_C99_COMPLEX
833 inline __complex__ float
834 __complex_sinh(__complex__ float __z) { return __builtin_csinhf(__z); }
835
836 inline __complex__ double
837 __complex_sinh(__complex__ double __z) { return __builtin_csinh(__z); }
838
839 inline __complex__ long double
840 __complex_sinh(const __complex__ long double& __z)
841 { return __builtin_csinhl(__z); }
842
843 template<typename _Tp>
844 inline complex<_Tp>
845 sinh(const complex<_Tp>& __z) { return __complex_sinh(__z.__rep()); }
846 #else
847 template<typename _Tp>
848 inline complex<_Tp>
849 sinh(const complex<_Tp>& __z) { return __complex_sinh(__z); }
850 #endif
851
852 // 26.2.8/13 sqrt(__z): Returns the complex square root of __z.
853 // The branch cut is on the negative axis.
854 template<typename _Tp>
855 complex<_Tp>
856 __complex_sqrt(const complex<_Tp>& __z)
857 {
858 _Tp __x = __z.real();
859 _Tp __y = __z.imag();
860
861 if (__x == _Tp())
862 {
863 _Tp __t = sqrt(abs(__y) / 2);
864 return complex<_Tp>(__t, __y < _Tp() ? -__t : __t);
865 }
866 else
867 {
868 _Tp __t = sqrt(2 * (std::abs(__z) + abs(__x)));
869 _Tp __u = __t / 2;
870 return __x > _Tp()
871 ? complex<_Tp>(__u, __y / __t)
872 : complex<_Tp>(abs(__y) / __t, __y < _Tp() ? -__u : __u);
873 }
874 }
875
876 #if _GLIBCXX_USE_C99_COMPLEX
877 inline __complex__ float
878 __complex_sqrt(__complex__ float __z) { return __builtin_csqrtf(__z); }
879
880 inline __complex__ double
881 __complex_sqrt(__complex__ double __z) { return __builtin_csqrt(__z); }
882
883 inline __complex__ long double
884 __complex_sqrt(const __complex__ long double& __z)
885 { return __builtin_csqrtl(__z); }
886
887 template<typename _Tp>
888 inline complex<_Tp>
889 sqrt(const complex<_Tp>& __z) { return __complex_sqrt(__z.__rep()); }
890 #else
891 template<typename _Tp>
892 inline complex<_Tp>
893 sqrt(const complex<_Tp>& __z) { return __complex_sqrt(__z); }
894 #endif
895
896 // 26.2.8/14 tan(__z): Return the complex tangent of __z.
897
898 template<typename _Tp>
899 inline complex<_Tp>
900 __complex_tan(const complex<_Tp>& __z)
901 { return std::sin(__z) / std::cos(__z); }
902
903 #if _GLIBCXX_USE_C99_COMPLEX
904 inline __complex__ float
905 __complex_tan(__complex__ float __z) { return __builtin_ctanf(__z); }
906
907 inline __complex__ double
908 __complex_tan(__complex__ double __z) { return __builtin_ctan(__z); }
909
910 inline __complex__ long double
911 __complex_tan(const __complex__ long double& __z)
912 { return __builtin_ctanl(__z); }
913
914 template<typename _Tp>
915 inline complex<_Tp>
916 tan(const complex<_Tp>& __z) { return __complex_tan(__z.__rep()); }
917 #else
918 template<typename _Tp>
919 inline complex<_Tp>
920 tan(const complex<_Tp>& __z) { return __complex_tan(__z); }
921 #endif
922
923
924 // 26.2.8/15 tanh(__z): Returns the hyperbolic tangent of __z.
925
926 template<typename _Tp>
927 inline complex<_Tp>
928 __complex_tanh(const complex<_Tp>& __z)
929 { return std::sinh(__z) / std::cosh(__z); }
930
931 #if _GLIBCXX_USE_C99_COMPLEX
932 inline __complex__ float
933 __complex_tanh(__complex__ float __z) { return __builtin_ctanhf(__z); }
934
935 inline __complex__ double
936 __complex_tanh(__complex__ double __z) { return __builtin_ctanh(__z); }
937
938 inline __complex__ long double
939 __complex_tanh(const __complex__ long double& __z)
940 { return __builtin_ctanhl(__z); }
941
942 template<typename _Tp>
943 inline complex<_Tp>
944 tanh(const complex<_Tp>& __z) { return __complex_tanh(__z.__rep()); }
945 #else
946 template<typename _Tp>
947 inline complex<_Tp>
948 tanh(const complex<_Tp>& __z) { return __complex_tanh(__z); }
949 #endif
950
951
952 // 26.2.8/9 pow(__x, __y): Returns the complex power base of __x
953 // raised to the __y-th power. The branch
954 // cut is on the negative axis.
955 #if __cplusplus < 201103L
956 template<typename _Tp>
957 complex<_Tp>
958 __complex_pow_unsigned(complex<_Tp> __x, unsigned __n)
959 {
960 complex<_Tp> __y = __n % 2 ? __x : complex<_Tp>(1);
961
962 while (__n >>= 1)
963 {
964 __x *= __x;
965 if (__n % 2)
966 __y *= __x;
967 }
968
969 return __y;
970 }
971
972 // _GLIBCXX_RESOLVE_LIB_DEFECTS
973 // DR 844. complex pow return type is ambiguous.
974 template<typename _Tp>
975 inline complex<_Tp>
976 pow(const complex<_Tp>& __z, int __n)
977 {
978 return __n < 0
979 ? complex<_Tp>(1) / std::__complex_pow_unsigned(__z, -(unsigned)__n)
980 : std::__complex_pow_unsigned(__z, __n);
981 }
982 #endif
983
984 template<typename _Tp>
985 complex<_Tp>
986 pow(const complex<_Tp>& __x, const _Tp& __y)
987 {
988 #ifndef _GLIBCXX_USE_C99_COMPLEX
989 if (__x == _Tp())
990 return _Tp();
991 #endif
992 if (__x.imag() == _Tp() && __x.real() > _Tp())
993 return pow(__x.real(), __y);
994
995 complex<_Tp> __t = std::log(__x);
996 return std::polar(exp(__y * __t.real()), __y * __t.imag());
997 }
998
999 template<typename _Tp>
1000 inline complex<_Tp>
1001 __complex_pow(const complex<_Tp>& __x, const complex<_Tp>& __y)
1002 { return __x == _Tp() ? _Tp() : std::exp(__y * std::log(__x)); }
1003
1004 #if _GLIBCXX_USE_C99_COMPLEX
1005 inline __complex__ float
1006 __complex_pow(__complex__ float __x, __complex__ float __y)
1007 { return __builtin_cpowf(__x, __y); }
1008
1009 inline __complex__ double
1010 __complex_pow(__complex__ double __x, __complex__ double __y)
1011 { return __builtin_cpow(__x, __y); }
1012
1013 inline __complex__ long double
1014 __complex_pow(const __complex__ long double& __x,
1015 const __complex__ long double& __y)
1016 { return __builtin_cpowl(__x, __y); }
1017
1018 template<typename _Tp>
1019 inline complex<_Tp>
1020 pow(const complex<_Tp>& __x, const complex<_Tp>& __y)
1021 { return __complex_pow(__x.__rep(), __y.__rep()); }
1022 #else
1023 template<typename _Tp>
1024 inline complex<_Tp>
1025 pow(const complex<_Tp>& __x, const complex<_Tp>& __y)
1026 { return __complex_pow(__x, __y); }
1027 #endif
1028
1029 template<typename _Tp>
1030 inline complex<_Tp>
1031 pow(const _Tp& __x, const complex<_Tp>& __y)
1032 {
1033 return __x > _Tp() ? std::polar(pow(__x, __y.real()),
1034 __y.imag() * log(__x))
1035 : std::pow(complex<_Tp>(__x), __y);
1036 }
1037
1038 /// 26.2.3 complex specializations
1039 /// complex<float> specialization
1040 template<>
1041 struct complex<float>
1042 {
1043 typedef float value_type;
1044 typedef __complex__ float _ComplexT;
1045
1046 _GLIBCXX_CONSTEXPR complex(_ComplexT __z) : _M_value(__z) { }
1047
1048 _GLIBCXX_CONSTEXPR complex(float __r = 0.0f, float __i = 0.0f)
1049 #if __cplusplus >= 201103L
1050 : _M_value{ __r, __i } { }
1051 #else
1052 {
1053 __real__ _M_value = __r;
1054 __imag__ _M_value = __i;
1055 }
1056 #endif
1057
1058 explicit _GLIBCXX_CONSTEXPR complex(const complex<double>&);
1059 explicit _GLIBCXX_CONSTEXPR complex(const complex<long double>&);
1060
1061 #if __cplusplus >= 201103L
1062 // _GLIBCXX_RESOLVE_LIB_DEFECTS
1063 // DR 387. std::complex over-encapsulated.
1064 __attribute ((__abi_tag__ ("cxx11")))
1065 constexpr float
1066 real() { return __real__ _M_value; }
1067
1068 __attribute ((__abi_tag__ ("cxx11")))
1069 constexpr float
1070 imag() { return __imag__ _M_value; }
1071 #else
1072 float&
1073 real() { return __real__ _M_value; }
1074
1075 const float&
1076 real() const { return __real__ _M_value; }
1077
1078 float&
1079 imag() { return __imag__ _M_value; }
1080
1081 const float&
1082 imag() const { return __imag__ _M_value; }
1083 #endif
1084
1085 // _GLIBCXX_RESOLVE_LIB_DEFECTS
1086 // DR 387. std::complex over-encapsulated.
1087 void
1088 real(float __val) { __real__ _M_value = __val; }
1089
1090 void
1091 imag(float __val) { __imag__ _M_value = __val; }
1092
1093 complex&
1094 operator=(float __f)
1095 {
1096 _M_value = __f;
1097 return *this;
1098 }
1099
1100 complex&
1101 operator+=(float __f)
1102 {
1103 _M_value += __f;
1104 return *this;
1105 }
1106
1107 complex&
1108 operator-=(float __f)
1109 {
1110 _M_value -= __f;
1111 return *this;
1112 }
1113
1114 complex&
1115 operator*=(float __f)
1116 {
1117 _M_value *= __f;
1118 return *this;
1119 }
1120
1121 complex&
1122 operator/=(float __f)
1123 {
1124 _M_value /= __f;
1125 return *this;
1126 }
1127
1128 // Let the compiler synthesize the copy and assignment
1129 // operator. It always does a pretty good job.
1130 // complex& operator=(const complex&);
1131
1132 template<typename _Tp>
1133 complex&
1134 operator=(const complex<_Tp>& __z)
1135 {
1136 __real__ _M_value = __z.real();
1137 __imag__ _M_value = __z.imag();
1138 return *this;
1139 }
1140
1141 template<typename _Tp>
1142 complex&
1143 operator+=(const complex<_Tp>& __z)
1144 {
1145 __real__ _M_value += __z.real();
1146 __imag__ _M_value += __z.imag();
1147 return *this;
1148 }
1149
1150 template<class _Tp>
1151 complex&
1152 operator-=(const complex<_Tp>& __z)
1153 {
1154 __real__ _M_value -= __z.real();
1155 __imag__ _M_value -= __z.imag();
1156 return *this;
1157 }
1158
1159 template<class _Tp>
1160 complex&
1161 operator*=(const complex<_Tp>& __z)
1162 {
1163 _ComplexT __t;
1164 __real__ __t = __z.real();
1165 __imag__ __t = __z.imag();
1166 _M_value *= __t;
1167 return *this;
1168 }
1169
1170 template<class _Tp>
1171 complex&
1172 operator/=(const complex<_Tp>& __z)
1173 {
1174 _ComplexT __t;
1175 __real__ __t = __z.real();
1176 __imag__ __t = __z.imag();
1177 _M_value /= __t;
1178 return *this;
1179 }
1180
1181 _GLIBCXX_USE_CONSTEXPR _ComplexT __rep() const { return _M_value; }
1182
1183 private:
1184 _ComplexT _M_value;
1185 };
1186
1187 /// 26.2.3 complex specializations
1188 /// complex<double> specialization
1189 template<>
1190 struct complex<double>
1191 {
1192 typedef double value_type;
1193 typedef __complex__ double _ComplexT;
1194
1195 _GLIBCXX_CONSTEXPR complex(_ComplexT __z) : _M_value(__z) { }
1196
1197 _GLIBCXX_CONSTEXPR complex(double __r = 0.0, double __i = 0.0)
1198 #if __cplusplus >= 201103L
1199 : _M_value{ __r, __i } { }
1200 #else
1201 {
1202 __real__ _M_value = __r;
1203 __imag__ _M_value = __i;
1204 }
1205 #endif
1206
1207 _GLIBCXX_CONSTEXPR complex(const complex<float>& __z)
1208 : _M_value(__z.__rep()) { }
1209
1210 explicit _GLIBCXX_CONSTEXPR complex(const complex<long double>&);
1211
1212 #if __cplusplus >= 201103L
1213 // _GLIBCXX_RESOLVE_LIB_DEFECTS
1214 // DR 387. std::complex over-encapsulated.
1215 __attribute ((__abi_tag__ ("cxx11")))
1216 constexpr double
1217 real() { return __real__ _M_value; }
1218
1219 __attribute ((__abi_tag__ ("cxx11")))
1220 constexpr double
1221 imag() { return __imag__ _M_value; }
1222 #else
1223 double&
1224 real() { return __real__ _M_value; }
1225
1226 const double&
1227 real() const { return __real__ _M_value; }
1228
1229 double&
1230 imag() { return __imag__ _M_value; }
1231
1232 const double&
1233 imag() const { return __imag__ _M_value; }
1234 #endif
1235
1236 // _GLIBCXX_RESOLVE_LIB_DEFECTS
1237 // DR 387. std::complex over-encapsulated.
1238 void
1239 real(double __val) { __real__ _M_value = __val; }
1240
1241 void
1242 imag(double __val) { __imag__ _M_value = __val; }
1243
1244 complex&
1245 operator=(double __d)
1246 {
1247 _M_value = __d;
1248 return *this;
1249 }
1250
1251 complex&
1252 operator+=(double __d)
1253 {
1254 _M_value += __d;
1255 return *this;
1256 }
1257
1258 complex&
1259 operator-=(double __d)
1260 {
1261 _M_value -= __d;
1262 return *this;
1263 }
1264
1265 complex&
1266 operator*=(double __d)
1267 {
1268 _M_value *= __d;
1269 return *this;
1270 }
1271
1272 complex&
1273 operator/=(double __d)
1274 {
1275 _M_value /= __d;
1276 return *this;
1277 }
1278
1279 // The compiler will synthesize this, efficiently.
1280 // complex& operator=(const complex&);
1281
1282 template<typename _Tp>
1283 complex&
1284 operator=(const complex<_Tp>& __z)
1285 {
1286 __real__ _M_value = __z.real();
1287 __imag__ _M_value = __z.imag();
1288 return *this;
1289 }
1290
1291 template<typename _Tp>
1292 complex&
1293 operator+=(const complex<_Tp>& __z)
1294 {
1295 __real__ _M_value += __z.real();
1296 __imag__ _M_value += __z.imag();
1297 return *this;
1298 }
1299
1300 template<typename _Tp>
1301 complex&
1302 operator-=(const complex<_Tp>& __z)
1303 {
1304 __real__ _M_value -= __z.real();
1305 __imag__ _M_value -= __z.imag();
1306 return *this;
1307 }
1308
1309 template<typename _Tp>
1310 complex&
1311 operator*=(const complex<_Tp>& __z)
1312 {
1313 _ComplexT __t;
1314 __real__ __t = __z.real();
1315 __imag__ __t = __z.imag();
1316 _M_value *= __t;
1317 return *this;
1318 }
1319
1320 template<typename _Tp>
1321 complex&
1322 operator/=(const complex<_Tp>& __z)
1323 {
1324 _ComplexT __t;
1325 __real__ __t = __z.real();
1326 __imag__ __t = __z.imag();
1327 _M_value /= __t;
1328 return *this;
1329 }
1330
1331 _GLIBCXX_USE_CONSTEXPR _ComplexT __rep() const { return _M_value; }
1332
1333 private:
1334 _ComplexT _M_value;
1335 };
1336
1337 /// 26.2.3 complex specializations
1338 /// complex<long double> specialization
1339 template<>
1340 struct complex<long double>
1341 {
1342 typedef long double value_type;
1343 typedef __complex__ long double _ComplexT;
1344
1345 _GLIBCXX_CONSTEXPR complex(_ComplexT __z) : _M_value(__z) { }
1346
1347 _GLIBCXX_CONSTEXPR complex(long double __r = 0.0L,
1348 long double __i = 0.0L)
1349 #if __cplusplus >= 201103L
1350 : _M_value{ __r, __i } { }
1351 #else
1352 {
1353 __real__ _M_value = __r;
1354 __imag__ _M_value = __i;
1355 }
1356 #endif
1357
1358 _GLIBCXX_CONSTEXPR complex(const complex<float>& __z)
1359 : _M_value(__z.__rep()) { }
1360
1361 _GLIBCXX_CONSTEXPR complex(const complex<double>& __z)
1362 : _M_value(__z.__rep()) { }
1363
1364 #if __cplusplus >= 201103L
1365 // _GLIBCXX_RESOLVE_LIB_DEFECTS
1366 // DR 387. std::complex over-encapsulated.
1367 __attribute ((__abi_tag__ ("cxx11")))
1368 constexpr long double
1369 real() { return __real__ _M_value; }
1370
1371 __attribute ((__abi_tag__ ("cxx11")))
1372 constexpr long double
1373 imag() { return __imag__ _M_value; }
1374 #else
1375 long double&
1376 real() { return __real__ _M_value; }
1377
1378 const long double&
1379 real() const { return __real__ _M_value; }
1380
1381 long double&
1382 imag() { return __imag__ _M_value; }
1383
1384 const long double&
1385 imag() const { return __imag__ _M_value; }
1386 #endif
1387
1388 // _GLIBCXX_RESOLVE_LIB_DEFECTS
1389 // DR 387. std::complex over-encapsulated.
1390 void
1391 real(long double __val) { __real__ _M_value = __val; }
1392
1393 void
1394 imag(long double __val) { __imag__ _M_value = __val; }
1395
1396 complex&
1397 operator=(long double __r)
1398 {
1399 _M_value = __r;
1400 return *this;
1401 }
1402
1403 complex&
1404 operator+=(long double __r)
1405 {
1406 _M_value += __r;
1407 return *this;
1408 }
1409
1410 complex&
1411 operator-=(long double __r)
1412 {
1413 _M_value -= __r;
1414 return *this;
1415 }
1416
1417 complex&
1418 operator*=(long double __r)
1419 {
1420 _M_value *= __r;
1421 return *this;
1422 }
1423
1424 complex&
1425 operator/=(long double __r)
1426 {
1427 _M_value /= __r;
1428 return *this;
1429 }
1430
1431 // The compiler knows how to do this efficiently
1432 // complex& operator=(const complex&);
1433
1434 template<typename _Tp>
1435 complex&
1436 operator=(const complex<_Tp>& __z)
1437 {
1438 __real__ _M_value = __z.real();
1439 __imag__ _M_value = __z.imag();
1440 return *this;
1441 }
1442
1443 template<typename _Tp>
1444 complex&
1445 operator+=(const complex<_Tp>& __z)
1446 {
1447 __real__ _M_value += __z.real();
1448 __imag__ _M_value += __z.imag();
1449 return *this;
1450 }
1451
1452 template<typename _Tp>
1453 complex&
1454 operator-=(const complex<_Tp>& __z)
1455 {
1456 __real__ _M_value -= __z.real();
1457 __imag__ _M_value -= __z.imag();
1458 return *this;
1459 }
1460
1461 template<typename _Tp>
1462 complex&
1463 operator*=(const complex<_Tp>& __z)
1464 {
1465 _ComplexT __t;
1466 __real__ __t = __z.real();
1467 __imag__ __t = __z.imag();
1468 _M_value *= __t;
1469 return *this;
1470 }
1471
1472 template<typename _Tp>
1473 complex&
1474 operator/=(const complex<_Tp>& __z)
1475 {
1476 _ComplexT __t;
1477 __real__ __t = __z.real();
1478 __imag__ __t = __z.imag();
1479 _M_value /= __t;
1480 return *this;
1481 }
1482
1483 _GLIBCXX_USE_CONSTEXPR _ComplexT __rep() const { return _M_value; }
1484
1485 private:
1486 _ComplexT _M_value;
1487 };
1488
1489 // These bits have to be at the end of this file, so that the
1490 // specializations have all been defined.
1491 inline _GLIBCXX_CONSTEXPR
1492 complex<float>::complex(const complex<double>& __z)
1493 : _M_value(__z.__rep()) { }
1494
1495 inline _GLIBCXX_CONSTEXPR
1496 complex<float>::complex(const complex<long double>& __z)
1497 : _M_value(__z.__rep()) { }
1498
1499 inline _GLIBCXX_CONSTEXPR
1500 complex<double>::complex(const complex<long double>& __z)
1501 : _M_value(__z.__rep()) { }
1502
1503 // Inhibit implicit instantiations for required instantiations,
1504 // which are defined via explicit instantiations elsewhere.
1505 // NB: This syntax is a GNU extension.
1506 #if _GLIBCXX_EXTERN_TEMPLATE
1507 extern template istream& operator>>(istream&, complex<float>&);
1508 extern template ostream& operator<<(ostream&, const complex<float>&);
1509 extern template istream& operator>>(istream&, complex<double>&);
1510 extern template ostream& operator<<(ostream&, const complex<double>&);
1511 extern template istream& operator>>(istream&, complex<long double>&);
1512 extern template ostream& operator<<(ostream&, const complex<long double>&);
1513
1514 #ifdef _GLIBCXX_USE_WCHAR_T
1515 extern template wistream& operator>>(wistream&, complex<float>&);
1516 extern template wostream& operator<<(wostream&, const complex<float>&);
1517 extern template wistream& operator>>(wistream&, complex<double>&);
1518 extern template wostream& operator<<(wostream&, const complex<double>&);
1519 extern template wistream& operator>>(wistream&, complex<long double>&);
1520 extern template wostream& operator<<(wostream&, const complex<long double>&);
1521 #endif
1522 #endif
1523
1524 // @} group complex_numbers
1525
1526 _GLIBCXX_END_NAMESPACE_VERSION
1527 } // namespace
1528
1529 namespace __gnu_cxx _GLIBCXX_VISIBILITY(default)
1530 {
1531 _GLIBCXX_BEGIN_NAMESPACE_VERSION
1532
1533 // See ext/type_traits.h for the primary template.
1534 template<typename _Tp, typename _Up>
1535 struct __promote_2<std::complex<_Tp>, _Up>
1536 {
1537 public:
1538 typedef std::complex<typename __promote_2<_Tp, _Up>::__type> __type;
1539 };
1540
1541 template<typename _Tp, typename _Up>
1542 struct __promote_2<_Tp, std::complex<_Up> >
1543 {
1544 public:
1545 typedef std::complex<typename __promote_2<_Tp, _Up>::__type> __type;
1546 };
1547
1548 template<typename _Tp, typename _Up>
1549 struct __promote_2<std::complex<_Tp>, std::complex<_Up> >
1550 {
1551 public:
1552 typedef std::complex<typename __promote_2<_Tp, _Up>::__type> __type;
1553 };
1554
1555 _GLIBCXX_END_NAMESPACE_VERSION
1556 } // namespace
1557
1558 #if __cplusplus >= 201103L
1559
1560 namespace std _GLIBCXX_VISIBILITY(default)
1561 {
1562 _GLIBCXX_BEGIN_NAMESPACE_VERSION
1563
1564 // Forward declarations.
1565 template<typename _Tp> std::complex<_Tp> acos(const std::complex<_Tp>&);
1566 template<typename _Tp> std::complex<_Tp> asin(const std::complex<_Tp>&);
1567 template<typename _Tp> std::complex<_Tp> atan(const std::complex<_Tp>&);
1568
1569 template<typename _Tp> std::complex<_Tp> acosh(const std::complex<_Tp>&);
1570 template<typename _Tp> std::complex<_Tp> asinh(const std::complex<_Tp>&);
1571 template<typename _Tp> std::complex<_Tp> atanh(const std::complex<_Tp>&);
1572 // DR 595.
1573 template<typename _Tp> _Tp fabs(const std::complex<_Tp>&);
1574
1575 template<typename _Tp>
1576 inline std::complex<_Tp>
1577 __complex_acos(const std::complex<_Tp>& __z)
1578 {
1579 const std::complex<_Tp> __t = std::asin(__z);
1580 const _Tp __pi_2 = 1.5707963267948966192313216916397514L;
1581 return std::complex<_Tp>(__pi_2 - __t.real(), -__t.imag());
1582 }
1583
1584 #if _GLIBCXX_USE_C99_COMPLEX_TR1
1585 inline __complex__ float
1586 __complex_acos(__complex__ float __z)
1587 { return __builtin_cacosf(__z); }
1588
1589 inline __complex__ double
1590 __complex_acos(__complex__ double __z)
1591 { return __builtin_cacos(__z); }
1592
1593 inline __complex__ long double
1594 __complex_acos(const __complex__ long double& __z)
1595 { return __builtin_cacosl(__z); }
1596
1597 template<typename _Tp>
1598 inline std::complex<_Tp>
1599 acos(const std::complex<_Tp>& __z)
1600 { return __complex_acos(__z.__rep()); }
1601 #else
1602 /// acos(__z) [8.1.2].
1603 // Effects: Behaves the same as C99 function cacos, defined
1604 // in subclause 7.3.5.1.
1605 template<typename _Tp>
1606 inline std::complex<_Tp>
1607 acos(const std::complex<_Tp>& __z)
1608 { return __complex_acos(__z); }
1609 #endif
1610
1611 template<typename _Tp>
1612 inline std::complex<_Tp>
1613 __complex_asin(const std::complex<_Tp>& __z)
1614 {
1615 std::complex<_Tp> __t(-__z.imag(), __z.real());
1616 __t = std::asinh(__t);
1617 return std::complex<_Tp>(__t.imag(), -__t.real());
1618 }
1619
1620 #if _GLIBCXX_USE_C99_COMPLEX_TR1
1621 inline __complex__ float
1622 __complex_asin(__complex__ float __z)
1623 { return __builtin_casinf(__z); }
1624
1625 inline __complex__ double
1626 __complex_asin(__complex__ double __z)
1627 { return __builtin_casin(__z); }
1628
1629 inline __complex__ long double
1630 __complex_asin(const __complex__ long double& __z)
1631 { return __builtin_casinl(__z); }
1632
1633 template<typename _Tp>
1634 inline std::complex<_Tp>
1635 asin(const std::complex<_Tp>& __z)
1636 { return __complex_asin(__z.__rep()); }
1637 #else
1638 /// asin(__z) [8.1.3].
1639 // Effects: Behaves the same as C99 function casin, defined
1640 // in subclause 7.3.5.2.
1641 template<typename _Tp>
1642 inline std::complex<_Tp>
1643 asin(const std::complex<_Tp>& __z)
1644 { return __complex_asin(__z); }
1645 #endif
1646
1647 template<typename _Tp>
1648 std::complex<_Tp>
1649 __complex_atan(const std::complex<_Tp>& __z)
1650 {
1651 const _Tp __r2 = __z.real() * __z.real();
1652 const _Tp __x = _Tp(1.0) - __r2 - __z.imag() * __z.imag();
1653
1654 _Tp __num = __z.imag() + _Tp(1.0);
1655 _Tp __den = __z.imag() - _Tp(1.0);
1656
1657 __num = __r2 + __num * __num;
1658 __den = __r2 + __den * __den;
1659
1660 return std::complex<_Tp>(_Tp(0.5) * atan2(_Tp(2.0) * __z.real(), __x),
1661 _Tp(0.25) * log(__num / __den));
1662 }
1663
1664 #if _GLIBCXX_USE_C99_COMPLEX_TR1
1665 inline __complex__ float
1666 __complex_atan(__complex__ float __z)
1667 { return __builtin_catanf(__z); }
1668
1669 inline __complex__ double
1670 __complex_atan(__complex__ double __z)
1671 { return __builtin_catan(__z); }
1672
1673 inline __complex__ long double
1674 __complex_atan(const __complex__ long double& __z)
1675 { return __builtin_catanl(__z); }
1676
1677 template<typename _Tp>
1678 inline std::complex<_Tp>
1679 atan(const std::complex<_Tp>& __z)
1680 { return __complex_atan(__z.__rep()); }
1681 #else
1682 /// atan(__z) [8.1.4].
1683 // Effects: Behaves the same as C99 function catan, defined
1684 // in subclause 7.3.5.3.
1685 template<typename _Tp>
1686 inline std::complex<_Tp>
1687 atan(const std::complex<_Tp>& __z)
1688 { return __complex_atan(__z); }
1689 #endif
1690
1691 template<typename _Tp>
1692 std::complex<_Tp>
1693 __complex_acosh(const std::complex<_Tp>& __z)
1694 {
1695 // Kahan's formula.
1696 return _Tp(2.0) * std::log(std::sqrt(_Tp(0.5) * (__z + _Tp(1.0)))
1697 + std::sqrt(_Tp(0.5) * (__z - _Tp(1.0))));
1698 }
1699
1700 #if _GLIBCXX_USE_C99_COMPLEX_TR1
1701 inline __complex__ float
1702 __complex_acosh(__complex__ float __z)
1703 { return __builtin_cacoshf(__z); }
1704
1705 inline __complex__ double
1706 __complex_acosh(__complex__ double __z)
1707 { return __builtin_cacosh(__z); }
1708
1709 inline __complex__ long double
1710 __complex_acosh(const __complex__ long double& __z)
1711 { return __builtin_cacoshl(__z); }
1712
1713 template<typename _Tp>
1714 inline std::complex<_Tp>
1715 acosh(const std::complex<_Tp>& __z)
1716 { return __complex_acosh(__z.__rep()); }
1717 #else
1718 /// acosh(__z) [8.1.5].
1719 // Effects: Behaves the same as C99 function cacosh, defined
1720 // in subclause 7.3.6.1.
1721 template<typename _Tp>
1722 inline std::complex<_Tp>
1723 acosh(const std::complex<_Tp>& __z)
1724 { return __complex_acosh(__z); }
1725 #endif
1726
1727 template<typename _Tp>
1728 std::complex<_Tp>
1729 __complex_asinh(const std::complex<_Tp>& __z)
1730 {
1731 std::complex<_Tp> __t((__z.real() - __z.imag())
1732 * (__z.real() + __z.imag()) + _Tp(1.0),
1733 _Tp(2.0) * __z.real() * __z.imag());
1734 __t = std::sqrt(__t);
1735
1736 return std::log(__t + __z);
1737 }
1738
1739 #if _GLIBCXX_USE_C99_COMPLEX_TR1
1740 inline __complex__ float
1741 __complex_asinh(__complex__ float __z)
1742 { return __builtin_casinhf(__z); }
1743
1744 inline __complex__ double
1745 __complex_asinh(__complex__ double __z)
1746 { return __builtin_casinh(__z); }
1747
1748 inline __complex__ long double
1749 __complex_asinh(const __complex__ long double& __z)
1750 { return __builtin_casinhl(__z); }
1751
1752 template<typename _Tp>
1753 inline std::complex<_Tp>
1754 asinh(const std::complex<_Tp>& __z)
1755 { return __complex_asinh(__z.__rep()); }
1756 #else
1757 /// asinh(__z) [8.1.6].
1758 // Effects: Behaves the same as C99 function casin, defined
1759 // in subclause 7.3.6.2.
1760 template<typename _Tp>
1761 inline std::complex<_Tp>
1762 asinh(const std::complex<_Tp>& __z)
1763 { return __complex_asinh(__z); }
1764 #endif
1765
1766 template<typename _Tp>
1767 std::complex<_Tp>
1768 __complex_atanh(const std::complex<_Tp>& __z)
1769 {
1770 const _Tp __i2 = __z.imag() * __z.imag();
1771 const _Tp __x = _Tp(1.0) - __i2 - __z.real() * __z.real();
1772
1773 _Tp __num = _Tp(1.0) + __z.real();
1774 _Tp __den = _Tp(1.0) - __z.real();
1775
1776 __num = __i2 + __num * __num;
1777 __den = __i2 + __den * __den;
1778
1779 return std::complex<_Tp>(_Tp(0.25) * (log(__num) - log(__den)),
1780 _Tp(0.5) * atan2(_Tp(2.0) * __z.imag(), __x));
1781 }
1782
1783 #if _GLIBCXX_USE_C99_COMPLEX_TR1
1784 inline __complex__ float
1785 __complex_atanh(__complex__ float __z)
1786 { return __builtin_catanhf(__z); }
1787
1788 inline __complex__ double
1789 __complex_atanh(__complex__ double __z)
1790 { return __builtin_catanh(__z); }
1791
1792 inline __complex__ long double
1793 __complex_atanh(const __complex__ long double& __z)
1794 { return __builtin_catanhl(__z); }
1795
1796 template<typename _Tp>
1797 inline std::complex<_Tp>
1798 atanh(const std::complex<_Tp>& __z)
1799 { return __complex_atanh(__z.__rep()); }
1800 #else
1801 /// atanh(__z) [8.1.7].
1802 // Effects: Behaves the same as C99 function catanh, defined
1803 // in subclause 7.3.6.3.
1804 template<typename _Tp>
1805 inline std::complex<_Tp>
1806 atanh(const std::complex<_Tp>& __z)
1807 { return __complex_atanh(__z); }
1808 #endif
1809
1810 template<typename _Tp>
1811 inline _Tp
1812 /// fabs(__z) [8.1.8].
1813 // Effects: Behaves the same as C99 function cabs, defined
1814 // in subclause 7.3.8.1.
1815 fabs(const std::complex<_Tp>& __z)
1816 { return std::abs(__z); }
1817
1818 /// Additional overloads [8.1.9].
1819 template<typename _Tp>
1820 inline typename __gnu_cxx::__promote<_Tp>::__type
1821 arg(_Tp __x)
1822 {
1823 typedef typename __gnu_cxx::__promote<_Tp>::__type __type;
1824 #if (_GLIBCXX_USE_C99_MATH && !_GLIBCXX_USE_C99_FP_MACROS_DYNAMIC)
1825 return std::signbit(__x) ? __type(3.1415926535897932384626433832795029L)
1826 : __type();
1827 #else
1828 return std::arg(std::complex<__type>(__x));
1829 #endif
1830 }
1831
1832 template<typename _Tp>
1833 inline typename __gnu_cxx::__promote<_Tp>::__type
1834 imag(_Tp)
1835 { return _Tp(); }
1836
1837 template<typename _Tp>
1838 inline typename __gnu_cxx::__promote<_Tp>::__type
1839 norm(_Tp __x)
1840 {
1841 typedef typename __gnu_cxx::__promote<_Tp>::__type __type;
1842 return __type(__x) * __type(__x);
1843 }
1844
1845 template<typename _Tp>
1846 inline typename __gnu_cxx::__promote<_Tp>::__type
1847 real(_Tp __x)
1848 { return __x; }
1849
1850 template<typename _Tp, typename _Up>
1851 inline std::complex<typename __gnu_cxx::__promote_2<_Tp, _Up>::__type>
1852 pow(const std::complex<_Tp>& __x, const _Up& __y)
1853 {
1854 typedef typename __gnu_cxx::__promote_2<_Tp, _Up>::__type __type;
1855 return std::pow(std::complex<__type>(__x), __type(__y));
1856 }
1857
1858 template<typename _Tp, typename _Up>
1859 inline std::complex<typename __gnu_cxx::__promote_2<_Tp, _Up>::__type>
1860 pow(const _Tp& __x, const std::complex<_Up>& __y)
1861 {
1862 typedef typename __gnu_cxx::__promote_2<_Tp, _Up>::__type __type;
1863 return std::pow(__type(__x), std::complex<__type>(__y));
1864 }
1865
1866 template<typename _Tp, typename _Up>
1867 inline std::complex<typename __gnu_cxx::__promote_2<_Tp, _Up>::__type>
1868 pow(const std::complex<_Tp>& __x, const std::complex<_Up>& __y)
1869 {
1870 typedef typename __gnu_cxx::__promote_2<_Tp, _Up>::__type __type;
1871 return std::pow(std::complex<__type>(__x),
1872 std::complex<__type>(__y));
1873 }
1874
1875 // Forward declarations.
1876 // DR 781.
1877 template<typename _Tp> std::complex<_Tp> proj(const std::complex<_Tp>&);
1878
1879 template<typename _Tp>
1880 std::complex<_Tp>
1881 __complex_proj(const std::complex<_Tp>& __z)
1882 {
1883 const _Tp __den = (__z.real() * __z.real()
1884 + __z.imag() * __z.imag() + _Tp(1.0));
1885
1886 return std::complex<_Tp>((_Tp(2.0) * __z.real()) / __den,
1887 (_Tp(2.0) * __z.imag()) / __den);
1888 }
1889
1890 #if _GLIBCXX_USE_C99_COMPLEX
1891 inline __complex__ float
1892 __complex_proj(__complex__ float __z)
1893 { return __builtin_cprojf(__z); }
1894
1895 inline __complex__ double
1896 __complex_proj(__complex__ double __z)
1897 { return __builtin_cproj(__z); }
1898
1899 inline __complex__ long double
1900 __complex_proj(const __complex__ long double& __z)
1901 { return __builtin_cprojl(__z); }
1902
1903 template<typename _Tp>
1904 inline std::complex<_Tp>
1905 proj(const std::complex<_Tp>& __z)
1906 { return __complex_proj(__z.__rep()); }
1907 #else
1908 template<typename _Tp>
1909 inline std::complex<_Tp>
1910 proj(const std::complex<_Tp>& __z)
1911 { return __complex_proj(__z); }
1912 #endif
1913
1914 // DR 1137.
1915 template<typename _Tp>
1916 inline typename __gnu_cxx::__promote<_Tp>::__type
1917 proj(_Tp __x)
1918 { return __x; }
1919
1920 template<typename _Tp>
1921 inline typename __gnu_cxx::__promote<_Tp>::__type
1922 conj(_Tp __x)
1923 { return __x; }
1924
1925 _GLIBCXX_END_NAMESPACE_VERSION
1926 } // namespace
1927
1928 #endif // C++11
1929
1930 #endif /* _GLIBCXX_COMPLEX */