1 // The template and inlines for the -*- C++ -*- complex number classes.
3 // Copyright (C) 1997-2020 Free Software Foundation, Inc.
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)
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.
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.
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/>.
25 /** @file include/complex
26 * This is a Standard C++ Library header.
30 // ISO C++ 14882: 26.2 Complex Numbers
31 // Note: this is not a conforming implementation.
36 #ifndef _GLIBCXX_COMPLEX
37 #define _GLIBCXX_COMPLEX 1
39 #pragma GCC system_header
41 #include <bits/c++config.h>
42 #include <bits/cpp_type_traits.h>
43 #include <ext/type_traits.h>
47 // Get rid of a macro possibly defined in <complex.h>
50 #if __cplusplus > 201703L
51 # define __cpp_lib_constexpr_complex 201711L
54 namespace std _GLIBCXX_VISIBILITY(default)
56 _GLIBCXX_BEGIN_NAMESPACE_VERSION
59 * @defgroup complex_numbers Complex Numbers
62 * Classes and functions for complex numbers.
66 // Forward declarations.
67 template<typename _Tp> class complex;
68 template<> class complex<float>;
69 template<> class complex<double>;
70 template<> class complex<long double>;
72 /// Return magnitude of @a z.
73 template<typename _Tp> _Tp abs(const complex<_Tp>&);
74 /// Return phase angle of @a z.
75 template<typename _Tp> _Tp arg(const complex<_Tp>&);
76 /// Return @a z magnitude squared.
77 template<typename _Tp> _Tp _GLIBCXX20_CONSTEXPR norm(const complex<_Tp>&);
79 /// Return complex conjugate of @a z.
80 template<typename _Tp>
81 _GLIBCXX20_CONSTEXPR complex<_Tp> conj(const complex<_Tp>&);
82 /// Return complex with magnitude @a rho and angle @a theta.
83 template<typename _Tp> complex<_Tp> polar(const _Tp&, const _Tp& = 0);
86 /// Return complex cosine of @a z.
87 template<typename _Tp> complex<_Tp> cos(const complex<_Tp>&);
88 /// Return complex hyperbolic cosine of @a z.
89 template<typename _Tp> complex<_Tp> cosh(const complex<_Tp>&);
90 /// Return complex base e exponential of @a z.
91 template<typename _Tp> complex<_Tp> exp(const complex<_Tp>&);
92 /// Return complex natural logarithm of @a z.
93 template<typename _Tp> complex<_Tp> log(const complex<_Tp>&);
94 /// Return complex base 10 logarithm of @a z.
95 template<typename _Tp> complex<_Tp> log10(const complex<_Tp>&);
96 /// Return @a x to the @a y'th power.
97 template<typename _Tp> complex<_Tp> pow(const complex<_Tp>&, int);
98 /// Return @a x to the @a y'th power.
99 template<typename _Tp> complex<_Tp> pow(const complex<_Tp>&, const _Tp&);
100 /// Return @a x to the @a y'th power.
101 template<typename _Tp> complex<_Tp> pow(const complex<_Tp>&,
102 const complex<_Tp>&);
103 /// Return @a x to the @a y'th power.
104 template<typename _Tp> complex<_Tp> pow(const _Tp&, const complex<_Tp>&);
105 /// Return complex sine of @a z.
106 template<typename _Tp> complex<_Tp> sin(const complex<_Tp>&);
107 /// Return complex hyperbolic sine of @a z.
108 template<typename _Tp> complex<_Tp> sinh(const complex<_Tp>&);
109 /// Return complex square root of @a z.
110 template<typename _Tp> complex<_Tp> sqrt(const complex<_Tp>&);
111 /// Return complex tangent of @a z.
112 template<typename _Tp> complex<_Tp> tan(const complex<_Tp>&);
113 /// Return complex hyperbolic tangent of @a z.
114 template<typename _Tp> complex<_Tp> tanh(const complex<_Tp>&);
117 // 26.2.2 Primary template class complex
119 * Template to represent complex numbers.
121 * Specializations for float, double, and long double are part of the
122 * library. Results with any other type are not guaranteed.
124 * @param Tp Type of real and imaginary values.
126 template<typename _Tp>
130 typedef _Tp value_type;
132 /// Default constructor. First parameter is x, second parameter is y.
133 /// Unspecified parameters default to 0.
134 _GLIBCXX_CONSTEXPR complex(const _Tp& __r = _Tp(), const _Tp& __i = _Tp())
135 : _M_real(__r), _M_imag(__i) { }
137 // Let the compiler synthesize the copy constructor
138 #if __cplusplus >= 201103L
139 constexpr complex(const complex&) = default;
142 /// Converting constructor.
143 template<typename _Up>
144 _GLIBCXX_CONSTEXPR complex(const complex<_Up>& __z)
145 : _M_real(__z.real()), _M_imag(__z.imag()) { }
147 #if __cplusplus >= 201103L
148 // _GLIBCXX_RESOLVE_LIB_DEFECTS
149 // DR 387. std::complex over-encapsulated.
150 _GLIBCXX_ABI_TAG_CXX11
152 real() const { return _M_real; }
154 _GLIBCXX_ABI_TAG_CXX11
156 imag() const { return _M_imag; }
158 /// Return real part of complex number.
160 real() { return _M_real; }
162 /// Return real part of complex number.
164 real() const { return _M_real; }
166 /// Return imaginary part of complex number.
168 imag() { return _M_imag; }
170 /// Return imaginary part of complex number.
172 imag() const { return _M_imag; }
175 // _GLIBCXX_RESOLVE_LIB_DEFECTS
176 // DR 387. std::complex over-encapsulated.
177 _GLIBCXX20_CONSTEXPR void
178 real(_Tp __val) { _M_real = __val; }
180 _GLIBCXX20_CONSTEXPR void
181 imag(_Tp __val) { _M_imag = __val; }
183 /// Assign a scalar to this complex number.
184 _GLIBCXX20_CONSTEXPR complex<_Tp>& operator=(const _Tp&);
186 /// Add a scalar to this complex number.
188 _GLIBCXX20_CONSTEXPR complex<_Tp>&
189 operator+=(const _Tp& __t)
195 /// Subtract a scalar from this complex number.
197 _GLIBCXX20_CONSTEXPR complex<_Tp>&
198 operator-=(const _Tp& __t)
204 /// Multiply this complex number by a scalar.
205 _GLIBCXX20_CONSTEXPR complex<_Tp>& operator*=(const _Tp&);
206 /// Divide this complex number by a scalar.
207 _GLIBCXX20_CONSTEXPR complex<_Tp>& operator/=(const _Tp&);
209 // Let the compiler synthesize the copy assignment operator
210 #if __cplusplus >= 201103L
211 _GLIBCXX20_CONSTEXPR complex& operator=(const complex&) = default;
214 /// Assign another complex number to this one.
215 template<typename _Up>
216 _GLIBCXX20_CONSTEXPR complex<_Tp>& operator=(const complex<_Up>&);
217 /// Add another complex number to this one.
218 template<typename _Up>
219 _GLIBCXX20_CONSTEXPR complex<_Tp>& operator+=(const complex<_Up>&);
220 /// Subtract another complex number from this one.
221 template<typename _Up>
222 _GLIBCXX20_CONSTEXPR complex<_Tp>& operator-=(const complex<_Up>&);
223 /// Multiply this complex number by another.
224 template<typename _Up>
225 _GLIBCXX20_CONSTEXPR complex<_Tp>& operator*=(const complex<_Up>&);
226 /// Divide this complex number by another.
227 template<typename _Up>
228 _GLIBCXX20_CONSTEXPR complex<_Tp>& operator/=(const complex<_Up>&);
230 _GLIBCXX_CONSTEXPR complex __rep() const
238 template<typename _Tp>
239 _GLIBCXX20_CONSTEXPR complex<_Tp>&
240 complex<_Tp>::operator=(const _Tp& __t)
248 template<typename _Tp>
249 _GLIBCXX20_CONSTEXPR complex<_Tp>&
250 complex<_Tp>::operator*=(const _Tp& __t)
258 template<typename _Tp>
259 _GLIBCXX20_CONSTEXPR complex<_Tp>&
260 complex<_Tp>::operator/=(const _Tp& __t)
267 template<typename _Tp>
268 template<typename _Up>
269 _GLIBCXX20_CONSTEXPR complex<_Tp>&
270 complex<_Tp>::operator=(const complex<_Up>& __z)
272 _M_real = __z.real();
273 _M_imag = __z.imag();
278 template<typename _Tp>
279 template<typename _Up>
280 _GLIBCXX20_CONSTEXPR complex<_Tp>&
281 complex<_Tp>::operator+=(const complex<_Up>& __z)
283 _M_real += __z.real();
284 _M_imag += __z.imag();
289 template<typename _Tp>
290 template<typename _Up>
291 _GLIBCXX20_CONSTEXPR complex<_Tp>&
292 complex<_Tp>::operator-=(const complex<_Up>& __z)
294 _M_real -= __z.real();
295 _M_imag -= __z.imag();
300 // XXX: This is a grammar school implementation.
301 template<typename _Tp>
302 template<typename _Up>
303 _GLIBCXX20_CONSTEXPR complex<_Tp>&
304 complex<_Tp>::operator*=(const complex<_Up>& __z)
306 const _Tp __r = _M_real * __z.real() - _M_imag * __z.imag();
307 _M_imag = _M_real * __z.imag() + _M_imag * __z.real();
313 // XXX: This is a grammar school implementation.
314 template<typename _Tp>
315 template<typename _Up>
316 _GLIBCXX20_CONSTEXPR complex<_Tp>&
317 complex<_Tp>::operator/=(const complex<_Up>& __z)
319 const _Tp __r = _M_real * __z.real() + _M_imag * __z.imag();
320 const _Tp __n = std::norm(__z);
321 _M_imag = (_M_imag * __z.real() - _M_real * __z.imag()) / __n;
328 /// Return new complex value @a x plus @a y.
329 template<typename _Tp>
330 inline _GLIBCXX20_CONSTEXPR complex<_Tp>
331 operator+(const complex<_Tp>& __x, const complex<_Tp>& __y)
333 complex<_Tp> __r = __x;
338 template<typename _Tp>
339 inline _GLIBCXX20_CONSTEXPR complex<_Tp>
340 operator+(const complex<_Tp>& __x, const _Tp& __y)
342 complex<_Tp> __r = __x;
347 template<typename _Tp>
348 inline _GLIBCXX20_CONSTEXPR complex<_Tp>
349 operator+(const _Tp& __x, const complex<_Tp>& __y)
351 complex<_Tp> __r = __y;
358 /// Return new complex value @a x minus @a y.
359 template<typename _Tp>
360 inline _GLIBCXX20_CONSTEXPR complex<_Tp>
361 operator-(const complex<_Tp>& __x, const complex<_Tp>& __y)
363 complex<_Tp> __r = __x;
368 template<typename _Tp>
369 inline _GLIBCXX20_CONSTEXPR complex<_Tp>
370 operator-(const complex<_Tp>& __x, const _Tp& __y)
372 complex<_Tp> __r = __x;
377 template<typename _Tp>
378 inline _GLIBCXX20_CONSTEXPR complex<_Tp>
379 operator-(const _Tp& __x, const complex<_Tp>& __y)
381 complex<_Tp> __r = -__y;
388 /// Return new complex value @a x times @a y.
389 template<typename _Tp>
390 inline _GLIBCXX20_CONSTEXPR complex<_Tp>
391 operator*(const complex<_Tp>& __x, const complex<_Tp>& __y)
393 complex<_Tp> __r = __x;
398 template<typename _Tp>
399 inline _GLIBCXX20_CONSTEXPR complex<_Tp>
400 operator*(const complex<_Tp>& __x, const _Tp& __y)
402 complex<_Tp> __r = __x;
407 template<typename _Tp>
408 inline _GLIBCXX20_CONSTEXPR complex<_Tp>
409 operator*(const _Tp& __x, const complex<_Tp>& __y)
411 complex<_Tp> __r = __y;
418 /// Return new complex value @a x divided by @a y.
419 template<typename _Tp>
420 inline _GLIBCXX20_CONSTEXPR complex<_Tp>
421 operator/(const complex<_Tp>& __x, const complex<_Tp>& __y)
423 complex<_Tp> __r = __x;
428 template<typename _Tp>
429 inline _GLIBCXX20_CONSTEXPR complex<_Tp>
430 operator/(const complex<_Tp>& __x, const _Tp& __y)
432 complex<_Tp> __r = __x;
437 template<typename _Tp>
438 inline _GLIBCXX20_CONSTEXPR complex<_Tp>
439 operator/(const _Tp& __x, const complex<_Tp>& __y)
441 complex<_Tp> __r = __x;
448 template<typename _Tp>
449 inline _GLIBCXX20_CONSTEXPR complex<_Tp>
450 operator+(const complex<_Tp>& __x)
453 /// Return complex negation of @a x.
454 template<typename _Tp>
455 inline _GLIBCXX20_CONSTEXPR complex<_Tp>
456 operator-(const complex<_Tp>& __x)
457 { return complex<_Tp>(-__x.real(), -__x.imag()); }
460 /// Return true if @a x is equal to @a y.
461 template<typename _Tp>
462 inline _GLIBCXX_CONSTEXPR bool
463 operator==(const complex<_Tp>& __x, const complex<_Tp>& __y)
464 { return __x.real() == __y.real() && __x.imag() == __y.imag(); }
466 template<typename _Tp>
467 inline _GLIBCXX_CONSTEXPR bool
468 operator==(const complex<_Tp>& __x, const _Tp& __y)
469 { return __x.real() == __y && __x.imag() == _Tp(); }
471 #if !(__cpp_impl_three_way_comparison >= 201907L)
472 template<typename _Tp>
473 inline _GLIBCXX_CONSTEXPR bool
474 operator==(const _Tp& __x, const complex<_Tp>& __y)
475 { return __x == __y.real() && _Tp() == __y.imag(); }
479 /// Return false if @a x is equal to @a y.
480 template<typename _Tp>
481 inline _GLIBCXX_CONSTEXPR bool
482 operator!=(const complex<_Tp>& __x, const complex<_Tp>& __y)
483 { return __x.real() != __y.real() || __x.imag() != __y.imag(); }
485 template<typename _Tp>
486 inline _GLIBCXX_CONSTEXPR bool
487 operator!=(const complex<_Tp>& __x, const _Tp& __y)
488 { return __x.real() != __y || __x.imag() != _Tp(); }
490 template<typename _Tp>
491 inline _GLIBCXX_CONSTEXPR bool
492 operator!=(const _Tp& __x, const complex<_Tp>& __y)
493 { return __x != __y.real() || _Tp() != __y.imag(); }
497 /// Extraction operator for complex values.
498 template<typename _Tp, typename _CharT, class _Traits>
499 basic_istream<_CharT, _Traits>&
500 operator>>(basic_istream<_CharT, _Traits>& __is, complex<_Tp>& __x)
506 if (_Traits::eq(__ch, __is.widen('(')))
509 if (__is >> __u >> __ch)
511 const _CharT __rparen = __is.widen(')');
512 if (_Traits::eq(__ch, __rparen))
517 else if (_Traits::eq(__ch, __is.widen(',')))
520 if (__is >> __v >> __ch)
522 if (_Traits::eq(__ch, __rparen))
524 __x = complex<_Tp>(__u, __v);
547 __is.setstate(ios_base::failbit);
551 /// Insertion operator for complex values.
552 template<typename _Tp, typename _CharT, class _Traits>
553 basic_ostream<_CharT, _Traits>&
554 operator<<(basic_ostream<_CharT, _Traits>& __os, const complex<_Tp>& __x)
556 basic_ostringstream<_CharT, _Traits> __s;
557 __s.flags(__os.flags());
558 __s.imbue(__os.getloc());
559 __s.precision(__os.precision());
560 __s << '(' << __x.real() << ',' << __x.imag() << ')';
561 return __os << __s.str();
565 #if __cplusplus >= 201103L
566 template<typename _Tp>
568 real(const complex<_Tp>& __z)
569 { return __z.real(); }
571 template<typename _Tp>
573 imag(const complex<_Tp>& __z)
574 { return __z.imag(); }
576 template<typename _Tp>
578 real(complex<_Tp>& __z)
579 { return __z.real(); }
581 template<typename _Tp>
583 real(const complex<_Tp>& __z)
584 { return __z.real(); }
586 template<typename _Tp>
588 imag(complex<_Tp>& __z)
589 { return __z.imag(); }
591 template<typename _Tp>
593 imag(const complex<_Tp>& __z)
594 { return __z.imag(); }
597 // 26.2.7/3 abs(__z): Returns the magnitude of __z.
598 template<typename _Tp>
600 __complex_abs(const complex<_Tp>& __z)
602 _Tp __x = __z.real();
603 _Tp __y = __z.imag();
604 const _Tp __s = std::max(abs(__x), abs(__y));
605 if (__s == _Tp()) // well ...
609 return __s * sqrt(__x * __x + __y * __y);
612 #if _GLIBCXX_USE_C99_COMPLEX
614 __complex_abs(__complex__ float __z) { return __builtin_cabsf(__z); }
617 __complex_abs(__complex__ double __z) { return __builtin_cabs(__z); }
620 __complex_abs(const __complex__ long double& __z)
621 { return __builtin_cabsl(__z); }
623 template<typename _Tp>
625 abs(const complex<_Tp>& __z) { return __complex_abs(__z.__rep()); }
627 template<typename _Tp>
629 abs(const complex<_Tp>& __z) { return __complex_abs(__z); }
633 // 26.2.7/4: arg(__z): Returns the phase angle of __z.
634 template<typename _Tp>
636 __complex_arg(const complex<_Tp>& __z)
637 { return atan2(__z.imag(), __z.real()); }
639 #if _GLIBCXX_USE_C99_COMPLEX
641 __complex_arg(__complex__ float __z) { return __builtin_cargf(__z); }
644 __complex_arg(__complex__ double __z) { return __builtin_carg(__z); }
647 __complex_arg(const __complex__ long double& __z)
648 { return __builtin_cargl(__z); }
650 template<typename _Tp>
652 arg(const complex<_Tp>& __z) { return __complex_arg(__z.__rep()); }
654 template<typename _Tp>
656 arg(const complex<_Tp>& __z) { return __complex_arg(__z); }
659 // 26.2.7/5: norm(__z) returns the squared magnitude of __z.
660 // As defined, norm() is -not- a norm is the common mathematical
661 // sense used in numerics. The helper class _Norm_helper<> tries to
662 // distinguish between builtin floating point and the rest, so as
663 // to deliver an answer as close as possible to the real value.
667 template<typename _Tp>
668 static inline _GLIBCXX20_CONSTEXPR _Tp _S_do_it(const complex<_Tp>& __z)
670 const _Tp __x = __z.real();
671 const _Tp __y = __z.imag();
672 return __x * __x + __y * __y;
677 struct _Norm_helper<true>
679 template<typename _Tp>
680 static inline _GLIBCXX20_CONSTEXPR _Tp _S_do_it(const complex<_Tp>& __z)
682 //_Tp __res = std::abs(__z);
683 //return __res * __res;
684 const _Tp __x = __z.real();
685 const _Tp __y = __z.imag();
686 return __x * __x + __y * __y;
690 template<typename _Tp>
691 inline _GLIBCXX20_CONSTEXPR _Tp
692 norm(const complex<_Tp>& __z)
694 return _Norm_helper<__is_floating<_Tp>::__value
695 && !_GLIBCXX_FAST_MATH>::_S_do_it(__z);
698 template<typename _Tp>
700 polar(const _Tp& __rho, const _Tp& __theta)
702 __glibcxx_assert( __rho >= 0 );
703 return complex<_Tp>(__rho * cos(__theta), __rho * sin(__theta));
706 template<typename _Tp>
707 inline _GLIBCXX20_CONSTEXPR complex<_Tp>
708 conj(const complex<_Tp>& __z)
709 { return complex<_Tp>(__z.real(), -__z.imag()); }
713 // 26.2.8/1 cos(__z): Returns the cosine of __z.
714 template<typename _Tp>
716 __complex_cos(const complex<_Tp>& __z)
718 const _Tp __x = __z.real();
719 const _Tp __y = __z.imag();
720 return complex<_Tp>(cos(__x) * cosh(__y), -sin(__x) * sinh(__y));
723 #if _GLIBCXX_USE_C99_COMPLEX
724 inline __complex__ float
725 __complex_cos(__complex__ float __z) { return __builtin_ccosf(__z); }
727 inline __complex__ double
728 __complex_cos(__complex__ double __z) { return __builtin_ccos(__z); }
730 inline __complex__ long double
731 __complex_cos(const __complex__ long double& __z)
732 { return __builtin_ccosl(__z); }
734 template<typename _Tp>
736 cos(const complex<_Tp>& __z) { return __complex_cos(__z.__rep()); }
738 template<typename _Tp>
740 cos(const complex<_Tp>& __z) { return __complex_cos(__z); }
743 // 26.2.8/2 cosh(__z): Returns the hyperbolic cosine of __z.
744 template<typename _Tp>
746 __complex_cosh(const complex<_Tp>& __z)
748 const _Tp __x = __z.real();
749 const _Tp __y = __z.imag();
750 return complex<_Tp>(cosh(__x) * cos(__y), sinh(__x) * sin(__y));
753 #if _GLIBCXX_USE_C99_COMPLEX
754 inline __complex__ float
755 __complex_cosh(__complex__ float __z) { return __builtin_ccoshf(__z); }
757 inline __complex__ double
758 __complex_cosh(__complex__ double __z) { return __builtin_ccosh(__z); }
760 inline __complex__ long double
761 __complex_cosh(const __complex__ long double& __z)
762 { return __builtin_ccoshl(__z); }
764 template<typename _Tp>
766 cosh(const complex<_Tp>& __z) { return __complex_cosh(__z.__rep()); }
768 template<typename _Tp>
770 cosh(const complex<_Tp>& __z) { return __complex_cosh(__z); }
773 // 26.2.8/3 exp(__z): Returns the complex base e exponential of x
774 template<typename _Tp>
776 __complex_exp(const complex<_Tp>& __z)
777 { return std::polar<_Tp>(exp(__z.real()), __z.imag()); }
779 #if _GLIBCXX_USE_C99_COMPLEX
780 inline __complex__ float
781 __complex_exp(__complex__ float __z) { return __builtin_cexpf(__z); }
783 inline __complex__ double
784 __complex_exp(__complex__ double __z) { return __builtin_cexp(__z); }
786 inline __complex__ long double
787 __complex_exp(const __complex__ long double& __z)
788 { return __builtin_cexpl(__z); }
790 template<typename _Tp>
792 exp(const complex<_Tp>& __z) { return __complex_exp(__z.__rep()); }
794 template<typename _Tp>
796 exp(const complex<_Tp>& __z) { return __complex_exp(__z); }
799 // 26.2.8/5 log(__z): Returns the natural complex logarithm of __z.
800 // The branch cut is along the negative axis.
801 template<typename _Tp>
803 __complex_log(const complex<_Tp>& __z)
804 { return complex<_Tp>(log(std::abs(__z)), std::arg(__z)); }
806 #if _GLIBCXX_USE_C99_COMPLEX
807 inline __complex__ float
808 __complex_log(__complex__ float __z) { return __builtin_clogf(__z); }
810 inline __complex__ double
811 __complex_log(__complex__ double __z) { return __builtin_clog(__z); }
813 inline __complex__ long double
814 __complex_log(const __complex__ long double& __z)
815 { return __builtin_clogl(__z); }
817 template<typename _Tp>
819 log(const complex<_Tp>& __z) { return __complex_log(__z.__rep()); }
821 template<typename _Tp>
823 log(const complex<_Tp>& __z) { return __complex_log(__z); }
826 template<typename _Tp>
828 log10(const complex<_Tp>& __z)
829 { return std::log(__z) / log(_Tp(10.0)); }
831 // 26.2.8/10 sin(__z): Returns the sine of __z.
832 template<typename _Tp>
834 __complex_sin(const complex<_Tp>& __z)
836 const _Tp __x = __z.real();
837 const _Tp __y = __z.imag();
838 return complex<_Tp>(sin(__x) * cosh(__y), cos(__x) * sinh(__y));
841 #if _GLIBCXX_USE_C99_COMPLEX
842 inline __complex__ float
843 __complex_sin(__complex__ float __z) { return __builtin_csinf(__z); }
845 inline __complex__ double
846 __complex_sin(__complex__ double __z) { return __builtin_csin(__z); }
848 inline __complex__ long double
849 __complex_sin(const __complex__ long double& __z)
850 { return __builtin_csinl(__z); }
852 template<typename _Tp>
854 sin(const complex<_Tp>& __z) { return __complex_sin(__z.__rep()); }
856 template<typename _Tp>
858 sin(const complex<_Tp>& __z) { return __complex_sin(__z); }
861 // 26.2.8/11 sinh(__z): Returns the hyperbolic sine of __z.
862 template<typename _Tp>
864 __complex_sinh(const complex<_Tp>& __z)
866 const _Tp __x = __z.real();
867 const _Tp __y = __z.imag();
868 return complex<_Tp>(sinh(__x) * cos(__y), cosh(__x) * sin(__y));
871 #if _GLIBCXX_USE_C99_COMPLEX
872 inline __complex__ float
873 __complex_sinh(__complex__ float __z) { return __builtin_csinhf(__z); }
875 inline __complex__ double
876 __complex_sinh(__complex__ double __z) { return __builtin_csinh(__z); }
878 inline __complex__ long double
879 __complex_sinh(const __complex__ long double& __z)
880 { return __builtin_csinhl(__z); }
882 template<typename _Tp>
884 sinh(const complex<_Tp>& __z) { return __complex_sinh(__z.__rep()); }
886 template<typename _Tp>
888 sinh(const complex<_Tp>& __z) { return __complex_sinh(__z); }
891 // 26.2.8/13 sqrt(__z): Returns the complex square root of __z.
892 // The branch cut is on the negative axis.
893 template<typename _Tp>
895 __complex_sqrt(const complex<_Tp>& __z)
897 _Tp __x = __z.real();
898 _Tp __y = __z.imag();
902 _Tp __t = sqrt(abs(__y) / 2);
903 return complex<_Tp>(__t, __y < _Tp() ? -__t : __t);
907 _Tp __t = sqrt(2 * (std::abs(__z) + abs(__x)));
910 ? complex<_Tp>(__u, __y / __t)
911 : complex<_Tp>(abs(__y) / __t, __y < _Tp() ? -__u : __u);
915 #if _GLIBCXX_USE_C99_COMPLEX
916 inline __complex__ float
917 __complex_sqrt(__complex__ float __z) { return __builtin_csqrtf(__z); }
919 inline __complex__ double
920 __complex_sqrt(__complex__ double __z) { return __builtin_csqrt(__z); }
922 inline __complex__ long double
923 __complex_sqrt(const __complex__ long double& __z)
924 { return __builtin_csqrtl(__z); }
926 template<typename _Tp>
928 sqrt(const complex<_Tp>& __z) { return __complex_sqrt(__z.__rep()); }
930 template<typename _Tp>
932 sqrt(const complex<_Tp>& __z) { return __complex_sqrt(__z); }
935 // 26.2.8/14 tan(__z): Return the complex tangent of __z.
937 template<typename _Tp>
939 __complex_tan(const complex<_Tp>& __z)
940 { return std::sin(__z) / std::cos(__z); }
942 #if _GLIBCXX_USE_C99_COMPLEX
943 inline __complex__ float
944 __complex_tan(__complex__ float __z) { return __builtin_ctanf(__z); }
946 inline __complex__ double
947 __complex_tan(__complex__ double __z) { return __builtin_ctan(__z); }
949 inline __complex__ long double
950 __complex_tan(const __complex__ long double& __z)
951 { return __builtin_ctanl(__z); }
953 template<typename _Tp>
955 tan(const complex<_Tp>& __z) { return __complex_tan(__z.__rep()); }
957 template<typename _Tp>
959 tan(const complex<_Tp>& __z) { return __complex_tan(__z); }
963 // 26.2.8/15 tanh(__z): Returns the hyperbolic tangent of __z.
965 template<typename _Tp>
967 __complex_tanh(const complex<_Tp>& __z)
968 { return std::sinh(__z) / std::cosh(__z); }
970 #if _GLIBCXX_USE_C99_COMPLEX
971 inline __complex__ float
972 __complex_tanh(__complex__ float __z) { return __builtin_ctanhf(__z); }
974 inline __complex__ double
975 __complex_tanh(__complex__ double __z) { return __builtin_ctanh(__z); }
977 inline __complex__ long double
978 __complex_tanh(const __complex__ long double& __z)
979 { return __builtin_ctanhl(__z); }
981 template<typename _Tp>
983 tanh(const complex<_Tp>& __z) { return __complex_tanh(__z.__rep()); }
985 template<typename _Tp>
987 tanh(const complex<_Tp>& __z) { return __complex_tanh(__z); }
991 // 26.2.8/9 pow(__x, __y): Returns the complex power base of __x
992 // raised to the __y-th power. The branch
993 // cut is on the negative axis.
994 template<typename _Tp>
996 __complex_pow_unsigned(complex<_Tp> __x, unsigned __n)
998 complex<_Tp> __y = __n % 2 ? __x : complex<_Tp>(1);
1010 // In C++11 mode we used to implement the resolution of
1011 // DR 844. complex pow return type is ambiguous.
1012 // thus the following overload was disabled in that mode. However, doing
1013 // that causes all sorts of issues, see, for example:
1014 // http://gcc.gnu.org/ml/libstdc++/2013-01/msg00058.html
1015 // and also PR57974.
1016 template<typename _Tp>
1018 pow(const complex<_Tp>& __z, int __n)
1021 ? complex<_Tp>(1) / std::__complex_pow_unsigned(__z, -(unsigned)__n)
1022 : std::__complex_pow_unsigned(__z, __n);
1025 template<typename _Tp>
1027 pow(const complex<_Tp>& __x, const _Tp& __y)
1029 #if ! _GLIBCXX_USE_C99_COMPLEX
1033 if (__x.imag() == _Tp() && __x.real() > _Tp())
1034 return pow(__x.real(), __y);
1036 complex<_Tp> __t = std::log(__x);
1037 return std::polar<_Tp>(exp(__y * __t.real()), __y * __t.imag());
1040 template<typename _Tp>
1042 __complex_pow(const complex<_Tp>& __x, const complex<_Tp>& __y)
1043 { return __x == _Tp() ? _Tp() : std::exp(__y * std::log(__x)); }
1045 #if _GLIBCXX_USE_C99_COMPLEX
1046 inline __complex__ float
1047 __complex_pow(__complex__ float __x, __complex__ float __y)
1048 { return __builtin_cpowf(__x, __y); }
1050 inline __complex__ double
1051 __complex_pow(__complex__ double __x, __complex__ double __y)
1052 { return __builtin_cpow(__x, __y); }
1054 inline __complex__ long double
1055 __complex_pow(const __complex__ long double& __x,
1056 const __complex__ long double& __y)
1057 { return __builtin_cpowl(__x, __y); }
1059 template<typename _Tp>
1061 pow(const complex<_Tp>& __x, const complex<_Tp>& __y)
1062 { return __complex_pow(__x.__rep(), __y.__rep()); }
1064 template<typename _Tp>
1066 pow(const complex<_Tp>& __x, const complex<_Tp>& __y)
1067 { return __complex_pow(__x, __y); }
1070 template<typename _Tp>
1072 pow(const _Tp& __x, const complex<_Tp>& __y)
1074 return __x > _Tp() ? std::polar<_Tp>(pow(__x, __y.real()),
1075 __y.imag() * log(__x))
1076 : std::pow(complex<_Tp>(__x), __y);
1079 /// 26.2.3 complex specializations
1080 /// complex<float> specialization
1082 struct complex<float>
1084 typedef float value_type;
1085 typedef __complex__ float _ComplexT;
1087 _GLIBCXX_CONSTEXPR complex(_ComplexT __z) : _M_value(__z) { }
1089 _GLIBCXX_CONSTEXPR complex(float __r = 0.0f, float __i = 0.0f)
1090 #if __cplusplus >= 201103L
1091 : _M_value{ __r, __i } { }
1094 __real__ _M_value = __r;
1095 __imag__ _M_value = __i;
1099 explicit _GLIBCXX_CONSTEXPR complex(const complex<double>&);
1100 explicit _GLIBCXX_CONSTEXPR complex(const complex<long double>&);
1102 #if __cplusplus >= 201103L
1103 // _GLIBCXX_RESOLVE_LIB_DEFECTS
1104 // DR 387. std::complex over-encapsulated.
1105 __attribute ((__abi_tag__ ("cxx11")))
1107 real() const { return __real__ _M_value; }
1109 __attribute ((__abi_tag__ ("cxx11")))
1111 imag() const { return __imag__ _M_value; }
1114 real() { return __real__ _M_value; }
1117 real() const { return __real__ _M_value; }
1120 imag() { return __imag__ _M_value; }
1123 imag() const { return __imag__ _M_value; }
1126 // _GLIBCXX_RESOLVE_LIB_DEFECTS
1127 // DR 387. std::complex over-encapsulated.
1128 _GLIBCXX20_CONSTEXPR void
1129 real(float __val) { __real__ _M_value = __val; }
1131 _GLIBCXX20_CONSTEXPR void
1132 imag(float __val) { __imag__ _M_value = __val; }
1134 _GLIBCXX20_CONSTEXPR complex&
1135 operator=(float __f)
1141 _GLIBCXX20_CONSTEXPR complex&
1142 operator+=(float __f)
1148 _GLIBCXX20_CONSTEXPR complex&
1149 operator-=(float __f)
1155 _GLIBCXX20_CONSTEXPR complex&
1156 operator*=(float __f)
1162 _GLIBCXX20_CONSTEXPR complex&
1163 operator/=(float __f)
1169 // Let the compiler synthesize the copy and assignment
1170 // operator. It always does a pretty good job.
1171 #if __cplusplus >= 201103L
1172 _GLIBCXX14_CONSTEXPR complex& operator=(const complex&) = default;
1175 template<typename _Tp>
1176 _GLIBCXX20_CONSTEXPR complex&
1177 operator=(const complex<_Tp>& __z)
1179 __real__ _M_value = __z.real();
1180 __imag__ _M_value = __z.imag();
1184 template<typename _Tp>
1185 _GLIBCXX20_CONSTEXPR complex&
1186 operator+=(const complex<_Tp>& __z)
1188 _M_value += __z.__rep();
1193 _GLIBCXX20_CONSTEXPR complex&
1194 operator-=(const complex<_Tp>& __z)
1196 _M_value -= __z.__rep();
1201 _GLIBCXX20_CONSTEXPR complex&
1202 operator*=(const complex<_Tp>& __z)
1204 const _ComplexT __t = __z.__rep();
1210 _GLIBCXX20_CONSTEXPR complex&
1211 operator/=(const complex<_Tp>& __z)
1213 const _ComplexT __t = __z.__rep();
1218 _GLIBCXX_CONSTEXPR _ComplexT __rep() const { return _M_value; }
1224 /// 26.2.3 complex specializations
1225 /// complex<double> specialization
1227 struct complex<double>
1229 typedef double value_type;
1230 typedef __complex__ double _ComplexT;
1232 _GLIBCXX_CONSTEXPR complex(_ComplexT __z) : _M_value(__z) { }
1234 _GLIBCXX_CONSTEXPR complex(double __r = 0.0, double __i = 0.0)
1235 #if __cplusplus >= 201103L
1236 : _M_value{ __r, __i } { }
1239 __real__ _M_value = __r;
1240 __imag__ _M_value = __i;
1244 _GLIBCXX_CONSTEXPR complex(const complex<float>& __z)
1245 : _M_value(__z.__rep()) { }
1247 explicit _GLIBCXX_CONSTEXPR complex(const complex<long double>&);
1249 #if __cplusplus >= 201103L
1250 // _GLIBCXX_RESOLVE_LIB_DEFECTS
1251 // DR 387. std::complex over-encapsulated.
1252 __attribute ((__abi_tag__ ("cxx11")))
1254 real() const { return __real__ _M_value; }
1256 __attribute ((__abi_tag__ ("cxx11")))
1258 imag() const { return __imag__ _M_value; }
1261 real() { return __real__ _M_value; }
1264 real() const { return __real__ _M_value; }
1267 imag() { return __imag__ _M_value; }
1270 imag() const { return __imag__ _M_value; }
1273 // _GLIBCXX_RESOLVE_LIB_DEFECTS
1274 // DR 387. std::complex over-encapsulated.
1275 _GLIBCXX20_CONSTEXPR void
1276 real(double __val) { __real__ _M_value = __val; }
1278 _GLIBCXX20_CONSTEXPR void
1279 imag(double __val) { __imag__ _M_value = __val; }
1281 _GLIBCXX20_CONSTEXPR complex&
1282 operator=(double __d)
1288 _GLIBCXX20_CONSTEXPR complex&
1289 operator+=(double __d)
1295 _GLIBCXX20_CONSTEXPR complex&
1296 operator-=(double __d)
1302 _GLIBCXX20_CONSTEXPR complex&
1303 operator*=(double __d)
1309 _GLIBCXX20_CONSTEXPR complex&
1310 operator/=(double __d)
1316 // The compiler will synthesize this, efficiently.
1317 #if __cplusplus >= 201103L
1318 _GLIBCXX14_CONSTEXPR complex& operator=(const complex&) = default;
1321 template<typename _Tp>
1322 _GLIBCXX20_CONSTEXPR complex&
1323 operator=(const complex<_Tp>& __z)
1325 _M_value = __z.__rep();
1329 template<typename _Tp>
1330 _GLIBCXX20_CONSTEXPR complex&
1331 operator+=(const complex<_Tp>& __z)
1333 _M_value += __z.__rep();
1337 template<typename _Tp>
1338 _GLIBCXX20_CONSTEXPR complex&
1339 operator-=(const complex<_Tp>& __z)
1341 _M_value -= __z.__rep();
1345 template<typename _Tp>
1346 _GLIBCXX20_CONSTEXPR complex&
1347 operator*=(const complex<_Tp>& __z)
1349 const _ComplexT __t = __z.__rep();
1354 template<typename _Tp>
1355 _GLIBCXX20_CONSTEXPR complex&
1356 operator/=(const complex<_Tp>& __z)
1358 const _ComplexT __t = __z.__rep();
1363 _GLIBCXX_CONSTEXPR _ComplexT __rep() const { return _M_value; }
1369 /// 26.2.3 complex specializations
1370 /// complex<long double> specialization
1372 struct complex<long double>
1374 typedef long double value_type;
1375 typedef __complex__ long double _ComplexT;
1377 _GLIBCXX_CONSTEXPR complex(_ComplexT __z) : _M_value(__z) { }
1379 _GLIBCXX_CONSTEXPR complex(long double __r = 0.0L,
1380 long double __i = 0.0L)
1381 #if __cplusplus >= 201103L
1382 : _M_value{ __r, __i } { }
1385 __real__ _M_value = __r;
1386 __imag__ _M_value = __i;
1390 _GLIBCXX_CONSTEXPR complex(const complex<float>& __z)
1391 : _M_value(__z.__rep()) { }
1393 _GLIBCXX_CONSTEXPR complex(const complex<double>& __z)
1394 : _M_value(__z.__rep()) { }
1396 #if __cplusplus >= 201103L
1397 // _GLIBCXX_RESOLVE_LIB_DEFECTS
1398 // DR 387. std::complex over-encapsulated.
1399 __attribute ((__abi_tag__ ("cxx11")))
1400 constexpr long double
1401 real() const { return __real__ _M_value; }
1403 __attribute ((__abi_tag__ ("cxx11")))
1404 constexpr long double
1405 imag() const { return __imag__ _M_value; }
1408 real() { return __real__ _M_value; }
1411 real() const { return __real__ _M_value; }
1414 imag() { return __imag__ _M_value; }
1417 imag() const { return __imag__ _M_value; }
1420 // _GLIBCXX_RESOLVE_LIB_DEFECTS
1421 // DR 387. std::complex over-encapsulated.
1422 _GLIBCXX20_CONSTEXPR void
1423 real(long double __val) { __real__ _M_value = __val; }
1425 _GLIBCXX20_CONSTEXPR void
1426 imag(long double __val) { __imag__ _M_value = __val; }
1428 _GLIBCXX20_CONSTEXPR complex&
1429 operator=(long double __r)
1435 _GLIBCXX20_CONSTEXPR complex&
1436 operator+=(long double __r)
1442 _GLIBCXX20_CONSTEXPR complex&
1443 operator-=(long double __r)
1449 _GLIBCXX20_CONSTEXPR complex&
1450 operator*=(long double __r)
1456 _GLIBCXX20_CONSTEXPR complex&
1457 operator/=(long double __r)
1463 // The compiler knows how to do this efficiently
1464 #if __cplusplus >= 201103L
1465 _GLIBCXX14_CONSTEXPR complex& operator=(const complex&) = default;
1468 template<typename _Tp>
1469 _GLIBCXX20_CONSTEXPR complex&
1470 operator=(const complex<_Tp>& __z)
1472 _M_value = __z.__rep();
1476 template<typename _Tp>
1477 _GLIBCXX20_CONSTEXPR complex&
1478 operator+=(const complex<_Tp>& __z)
1480 _M_value += __z.__rep();
1484 template<typename _Tp>
1485 _GLIBCXX20_CONSTEXPR complex&
1486 operator-=(const complex<_Tp>& __z)
1488 _M_value -= __z.__rep();
1492 template<typename _Tp>
1493 _GLIBCXX20_CONSTEXPR complex&
1494 operator*=(const complex<_Tp>& __z)
1496 const _ComplexT __t = __z.__rep();
1501 template<typename _Tp>
1502 _GLIBCXX20_CONSTEXPR complex&
1503 operator/=(const complex<_Tp>& __z)
1505 const _ComplexT __t = __z.__rep();
1510 _GLIBCXX_CONSTEXPR _ComplexT __rep() const { return _M_value; }
1516 // These bits have to be at the end of this file, so that the
1517 // specializations have all been defined.
1518 inline _GLIBCXX_CONSTEXPR
1519 complex<float>::complex(const complex<double>& __z)
1520 : _M_value(__z.__rep()) { }
1522 inline _GLIBCXX_CONSTEXPR
1523 complex<float>::complex(const complex<long double>& __z)
1524 : _M_value(__z.__rep()) { }
1526 inline _GLIBCXX_CONSTEXPR
1527 complex<double>::complex(const complex<long double>& __z)
1528 : _M_value(__z.__rep()) { }
1530 // Inhibit implicit instantiations for required instantiations,
1531 // which are defined via explicit instantiations elsewhere.
1532 // NB: This syntax is a GNU extension.
1533 #if _GLIBCXX_EXTERN_TEMPLATE
1534 extern template istream& operator>>(istream&, complex<float>&);
1535 extern template ostream& operator<<(ostream&, const complex<float>&);
1536 extern template istream& operator>>(istream&, complex<double>&);
1537 extern template ostream& operator<<(ostream&, const complex<double>&);
1538 extern template istream& operator>>(istream&, complex<long double>&);
1539 extern template ostream& operator<<(ostream&, const complex<long double>&);
1541 #ifdef _GLIBCXX_USE_WCHAR_T
1542 extern template wistream& operator>>(wistream&, complex<float>&);
1543 extern template wostream& operator<<(wostream&, const complex<float>&);
1544 extern template wistream& operator>>(wistream&, complex<double>&);
1545 extern template wostream& operator<<(wostream&, const complex<double>&);
1546 extern template wistream& operator>>(wistream&, complex<long double>&);
1547 extern template wostream& operator<<(wostream&, const complex<long double>&);
1551 /// @} group complex_numbers
1553 _GLIBCXX_END_NAMESPACE_VERSION
1556 namespace __gnu_cxx _GLIBCXX_VISIBILITY(default)
1558 _GLIBCXX_BEGIN_NAMESPACE_VERSION
1560 // See ext/type_traits.h for the primary template.
1561 template<typename _Tp, typename _Up>
1562 struct __promote_2<std::complex<_Tp>, _Up>
1565 typedef std::complex<typename __promote_2<_Tp, _Up>::__type> __type;
1568 template<typename _Tp, typename _Up>
1569 struct __promote_2<_Tp, std::complex<_Up> >
1572 typedef std::complex<typename __promote_2<_Tp, _Up>::__type> __type;
1575 template<typename _Tp, typename _Up>
1576 struct __promote_2<std::complex<_Tp>, std::complex<_Up> >
1579 typedef std::complex<typename __promote_2<_Tp, _Up>::__type> __type;
1582 _GLIBCXX_END_NAMESPACE_VERSION
1585 #if __cplusplus >= 201103L
1587 namespace std _GLIBCXX_VISIBILITY(default)
1589 _GLIBCXX_BEGIN_NAMESPACE_VERSION
1591 // Forward declarations.
1592 template<typename _Tp> std::complex<_Tp> acos(const std::complex<_Tp>&);
1593 template<typename _Tp> std::complex<_Tp> asin(const std::complex<_Tp>&);
1594 template<typename _Tp> std::complex<_Tp> atan(const std::complex<_Tp>&);
1596 template<typename _Tp> std::complex<_Tp> acosh(const std::complex<_Tp>&);
1597 template<typename _Tp> std::complex<_Tp> asinh(const std::complex<_Tp>&);
1598 template<typename _Tp> std::complex<_Tp> atanh(const std::complex<_Tp>&);
1600 template<typename _Tp> _Tp fabs(const std::complex<_Tp>&);
1602 template<typename _Tp>
1603 inline std::complex<_Tp>
1604 __complex_acos(const std::complex<_Tp>& __z)
1606 const std::complex<_Tp> __t = std::asin(__z);
1607 const _Tp __pi_2 = 1.5707963267948966192313216916397514L;
1608 return std::complex<_Tp>(__pi_2 - __t.real(), -__t.imag());
1611 #if _GLIBCXX_USE_C99_COMPLEX_TR1
1612 inline __complex__ float
1613 __complex_acos(__complex__ float __z)
1614 { return __builtin_cacosf(__z); }
1616 inline __complex__ double
1617 __complex_acos(__complex__ double __z)
1618 { return __builtin_cacos(__z); }
1620 inline __complex__ long double
1621 __complex_acos(const __complex__ long double& __z)
1622 { return __builtin_cacosl(__z); }
1624 template<typename _Tp>
1625 inline std::complex<_Tp>
1626 acos(const std::complex<_Tp>& __z)
1627 { return __complex_acos(__z.__rep()); }
1629 /// acos(__z) [8.1.2].
1630 // Effects: Behaves the same as C99 function cacos, defined
1631 // in subclause 7.3.5.1.
1632 template<typename _Tp>
1633 inline std::complex<_Tp>
1634 acos(const std::complex<_Tp>& __z)
1635 { return __complex_acos(__z); }
1638 template<typename _Tp>
1639 inline std::complex<_Tp>
1640 __complex_asin(const std::complex<_Tp>& __z)
1642 std::complex<_Tp> __t(-__z.imag(), __z.real());
1643 __t = std::asinh(__t);
1644 return std::complex<_Tp>(__t.imag(), -__t.real());
1647 #if _GLIBCXX_USE_C99_COMPLEX_TR1
1648 inline __complex__ float
1649 __complex_asin(__complex__ float __z)
1650 { return __builtin_casinf(__z); }
1652 inline __complex__ double
1653 __complex_asin(__complex__ double __z)
1654 { return __builtin_casin(__z); }
1656 inline __complex__ long double
1657 __complex_asin(const __complex__ long double& __z)
1658 { return __builtin_casinl(__z); }
1660 template<typename _Tp>
1661 inline std::complex<_Tp>
1662 asin(const std::complex<_Tp>& __z)
1663 { return __complex_asin(__z.__rep()); }
1665 /// asin(__z) [8.1.3].
1666 // Effects: Behaves the same as C99 function casin, defined
1667 // in subclause 7.3.5.2.
1668 template<typename _Tp>
1669 inline std::complex<_Tp>
1670 asin(const std::complex<_Tp>& __z)
1671 { return __complex_asin(__z); }
1674 template<typename _Tp>
1676 __complex_atan(const std::complex<_Tp>& __z)
1678 const _Tp __r2 = __z.real() * __z.real();
1679 const _Tp __x = _Tp(1.0) - __r2 - __z.imag() * __z.imag();
1681 _Tp __num = __z.imag() + _Tp(1.0);
1682 _Tp __den = __z.imag() - _Tp(1.0);
1684 __num = __r2 + __num * __num;
1685 __den = __r2 + __den * __den;
1687 return std::complex<_Tp>(_Tp(0.5) * atan2(_Tp(2.0) * __z.real(), __x),
1688 _Tp(0.25) * log(__num / __den));
1691 #if _GLIBCXX_USE_C99_COMPLEX_TR1
1692 inline __complex__ float
1693 __complex_atan(__complex__ float __z)
1694 { return __builtin_catanf(__z); }
1696 inline __complex__ double
1697 __complex_atan(__complex__ double __z)
1698 { return __builtin_catan(__z); }
1700 inline __complex__ long double
1701 __complex_atan(const __complex__ long double& __z)
1702 { return __builtin_catanl(__z); }
1704 template<typename _Tp>
1705 inline std::complex<_Tp>
1706 atan(const std::complex<_Tp>& __z)
1707 { return __complex_atan(__z.__rep()); }
1709 /// atan(__z) [8.1.4].
1710 // Effects: Behaves the same as C99 function catan, defined
1711 // in subclause 7.3.5.3.
1712 template<typename _Tp>
1713 inline std::complex<_Tp>
1714 atan(const std::complex<_Tp>& __z)
1715 { return __complex_atan(__z); }
1718 template<typename _Tp>
1720 __complex_acosh(const std::complex<_Tp>& __z)
1723 return _Tp(2.0) * std::log(std::sqrt(_Tp(0.5) * (__z + _Tp(1.0)))
1724 + std::sqrt(_Tp(0.5) * (__z - _Tp(1.0))));
1727 #if _GLIBCXX_USE_C99_COMPLEX_TR1
1728 inline __complex__ float
1729 __complex_acosh(__complex__ float __z)
1730 { return __builtin_cacoshf(__z); }
1732 inline __complex__ double
1733 __complex_acosh(__complex__ double __z)
1734 { return __builtin_cacosh(__z); }
1736 inline __complex__ long double
1737 __complex_acosh(const __complex__ long double& __z)
1738 { return __builtin_cacoshl(__z); }
1740 template<typename _Tp>
1741 inline std::complex<_Tp>
1742 acosh(const std::complex<_Tp>& __z)
1743 { return __complex_acosh(__z.__rep()); }
1745 /// acosh(__z) [8.1.5].
1746 // Effects: Behaves the same as C99 function cacosh, defined
1747 // in subclause 7.3.6.1.
1748 template<typename _Tp>
1749 inline std::complex<_Tp>
1750 acosh(const std::complex<_Tp>& __z)
1751 { return __complex_acosh(__z); }
1754 template<typename _Tp>
1756 __complex_asinh(const std::complex<_Tp>& __z)
1758 std::complex<_Tp> __t((__z.real() - __z.imag())
1759 * (__z.real() + __z.imag()) + _Tp(1.0),
1760 _Tp(2.0) * __z.real() * __z.imag());
1761 __t = std::sqrt(__t);
1763 return std::log(__t + __z);
1766 #if _GLIBCXX_USE_C99_COMPLEX_TR1
1767 inline __complex__ float
1768 __complex_asinh(__complex__ float __z)
1769 { return __builtin_casinhf(__z); }
1771 inline __complex__ double
1772 __complex_asinh(__complex__ double __z)
1773 { return __builtin_casinh(__z); }
1775 inline __complex__ long double
1776 __complex_asinh(const __complex__ long double& __z)
1777 { return __builtin_casinhl(__z); }
1779 template<typename _Tp>
1780 inline std::complex<_Tp>
1781 asinh(const std::complex<_Tp>& __z)
1782 { return __complex_asinh(__z.__rep()); }
1784 /// asinh(__z) [8.1.6].
1785 // Effects: Behaves the same as C99 function casin, defined
1786 // in subclause 7.3.6.2.
1787 template<typename _Tp>
1788 inline std::complex<_Tp>
1789 asinh(const std::complex<_Tp>& __z)
1790 { return __complex_asinh(__z); }
1793 template<typename _Tp>
1795 __complex_atanh(const std::complex<_Tp>& __z)
1797 const _Tp __i2 = __z.imag() * __z.imag();
1798 const _Tp __x = _Tp(1.0) - __i2 - __z.real() * __z.real();
1800 _Tp __num = _Tp(1.0) + __z.real();
1801 _Tp __den = _Tp(1.0) - __z.real();
1803 __num = __i2 + __num * __num;
1804 __den = __i2 + __den * __den;
1806 return std::complex<_Tp>(_Tp(0.25) * (log(__num) - log(__den)),
1807 _Tp(0.5) * atan2(_Tp(2.0) * __z.imag(), __x));
1810 #if _GLIBCXX_USE_C99_COMPLEX_TR1
1811 inline __complex__ float
1812 __complex_atanh(__complex__ float __z)
1813 { return __builtin_catanhf(__z); }
1815 inline __complex__ double
1816 __complex_atanh(__complex__ double __z)
1817 { return __builtin_catanh(__z); }
1819 inline __complex__ long double
1820 __complex_atanh(const __complex__ long double& __z)
1821 { return __builtin_catanhl(__z); }
1823 template<typename _Tp>
1824 inline std::complex<_Tp>
1825 atanh(const std::complex<_Tp>& __z)
1826 { return __complex_atanh(__z.__rep()); }
1828 /// atanh(__z) [8.1.7].
1829 // Effects: Behaves the same as C99 function catanh, defined
1830 // in subclause 7.3.6.3.
1831 template<typename _Tp>
1832 inline std::complex<_Tp>
1833 atanh(const std::complex<_Tp>& __z)
1834 { return __complex_atanh(__z); }
1837 template<typename _Tp>
1839 /// fabs(__z) [8.1.8].
1840 // Effects: Behaves the same as C99 function cabs, defined
1841 // in subclause 7.3.8.1.
1842 fabs(const std::complex<_Tp>& __z)
1843 { return std::abs(__z); }
1845 /// Additional overloads [8.1.9].
1846 template<typename _Tp>
1847 inline typename __gnu_cxx::__promote<_Tp>::__type
1850 typedef typename __gnu_cxx::__promote<_Tp>::__type __type;
1851 #if (_GLIBCXX11_USE_C99_MATH && !_GLIBCXX_USE_C99_FP_MACROS_DYNAMIC)
1852 return std::signbit(__x) ? __type(3.1415926535897932384626433832795029L)
1855 return std::arg(std::complex<__type>(__x));
1859 template<typename _Tp>
1860 _GLIBCXX_CONSTEXPR inline typename __gnu_cxx::__promote<_Tp>::__type
1864 template<typename _Tp>
1865 _GLIBCXX20_CONSTEXPR inline typename __gnu_cxx::__promote<_Tp>::__type
1868 typedef typename __gnu_cxx::__promote<_Tp>::__type __type;
1869 return __type(__x) * __type(__x);
1872 template<typename _Tp>
1873 _GLIBCXX_CONSTEXPR inline typename __gnu_cxx::__promote<_Tp>::__type
1877 template<typename _Tp, typename _Up>
1878 inline std::complex<typename __gnu_cxx::__promote_2<_Tp, _Up>::__type>
1879 pow(const std::complex<_Tp>& __x, const _Up& __y)
1881 typedef typename __gnu_cxx::__promote_2<_Tp, _Up>::__type __type;
1882 return std::pow(std::complex<__type>(__x), __type(__y));
1885 template<typename _Tp, typename _Up>
1886 inline std::complex<typename __gnu_cxx::__promote_2<_Tp, _Up>::__type>
1887 pow(const _Tp& __x, const std::complex<_Up>& __y)
1889 typedef typename __gnu_cxx::__promote_2<_Tp, _Up>::__type __type;
1890 return std::pow(__type(__x), std::complex<__type>(__y));
1893 template<typename _Tp, typename _Up>
1894 inline std::complex<typename __gnu_cxx::__promote_2<_Tp, _Up>::__type>
1895 pow(const std::complex<_Tp>& __x, const std::complex<_Up>& __y)
1897 typedef typename __gnu_cxx::__promote_2<_Tp, _Up>::__type __type;
1898 return std::pow(std::complex<__type>(__x),
1899 std::complex<__type>(__y));
1902 // Forward declarations.
1904 template<typename _Tp>
1905 std::complex<_Tp> proj(const std::complex<_Tp>&);
1907 // Generic implementation of std::proj, does not work for infinities.
1908 template<typename _Tp>
1909 inline std::complex<_Tp>
1910 __complex_proj(const std::complex<_Tp>& __z)
1913 #if _GLIBCXX_USE_C99_COMPLEX
1914 inline complex<float>
1915 __complex_proj(const complex<float>& __z)
1916 { return __builtin_cprojf(__z.__rep()); }
1918 inline complex<double>
1919 __complex_proj(const complex<double>& __z)
1920 { return __builtin_cproj(__z.__rep()); }
1922 inline complex<long double>
1923 __complex_proj(const complex<long double>& __z)
1924 { return __builtin_cprojl(__z.__rep()); }
1925 #elif defined _GLIBCXX_USE_C99_MATH_TR1
1926 inline complex<float>
1927 __complex_proj(const complex<float>& __z)
1929 if (__builtin_isinf(__z.real()) || __builtin_isinf(__z.imag()))
1930 return complex<float>(__builtin_inff(),
1931 __builtin_copysignf(0.0f, __z.imag()));
1935 inline complex<double>
1936 __complex_proj(const complex<double>& __z)
1938 if (__builtin_isinf(__z.real()) || __builtin_isinf(__z.imag()))
1939 return complex<double>(__builtin_inf(),
1940 __builtin_copysign(0.0, __z.imag()));
1944 inline complex<long double>
1945 __complex_proj(const complex<long double>& __z)
1947 if (__builtin_isinf(__z.real()) || __builtin_isinf(__z.imag()))
1948 return complex<long double>(__builtin_infl(),
1949 __builtin_copysignl(0.0l, __z.imag()));
1954 template<typename _Tp>
1955 inline std::complex<_Tp>
1956 proj(const std::complex<_Tp>& __z)
1957 { return __complex_proj(__z); }
1959 // Overload for scalars
1960 template<typename _Tp>
1961 inline std::complex<typename __gnu_cxx::__promote<_Tp>::__type>
1964 typedef typename __gnu_cxx::__promote<_Tp>::__type __type;
1965 return std::proj(std::complex<__type>(__x));
1968 template<typename _Tp>
1969 inline _GLIBCXX20_CONSTEXPR
1970 std::complex<typename __gnu_cxx::__promote<_Tp>::__type>
1973 typedef typename __gnu_cxx::__promote<_Tp>::__type __type;
1974 return std::complex<__type>(__x, -__type());
1977 #if __cplusplus > 201103L
1979 inline namespace literals {
1980 inline namespace complex_literals {
1981 #pragma GCC diagnostic push
1982 #pragma GCC diagnostic ignored "-Wliteral-suffix"
1983 #define __cpp_lib_complex_udls 201309
1985 constexpr std::complex<float>
1986 operator""if(long double __num)
1987 { return std::complex<float>{0.0F, static_cast<float>(__num)}; }
1989 constexpr std::complex<float>
1990 operator""if(unsigned long long __num)
1991 { return std::complex<float>{0.0F, static_cast<float>(__num)}; }
1993 constexpr std::complex<double>
1994 operator""i(long double __num)
1995 { return std::complex<double>{0.0, static_cast<double>(__num)}; }
1997 constexpr std::complex<double>
1998 operator""i(unsigned long long __num)
1999 { return std::complex<double>{0.0, static_cast<double>(__num)}; }
2001 constexpr std::complex<long double>
2002 operator""il(long double __num)
2003 { return std::complex<long double>{0.0L, __num}; }
2005 constexpr std::complex<long double>
2006 operator""il(unsigned long long __num)
2007 { return std::complex<long double>{0.0L, static_cast<long double>(__num)}; }
2009 #pragma GCC diagnostic pop
2010 } // inline namespace complex_literals
2011 } // inline namespace literals
2015 _GLIBCXX_END_NAMESPACE_VERSION
2020 #endif /* _GLIBCXX_COMPLEX */