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