1 // The template and inlines for the -*- C++ -*- complex number classes.
3 // Copyright (C) 1997-2019 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 namespace std _GLIBCXX_VISIBILITY(default)
52 _GLIBCXX_BEGIN_NAMESPACE_VERSION
55 * @defgroup complex_numbers Complex Numbers
58 * Classes and functions for complex numbers.
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>;
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 _GLIBCXX20_CONSTEXPR norm(const complex<_Tp>&);
75 /// Return complex conjugate of @a z.
76 template<typename _Tp>
77 _GLIBCXX20_CONSTEXPR complex<_Tp> conj(const complex<_Tp>&);
78 /// Return complex with magnitude @a rho and angle @a theta.
79 template<typename _Tp> complex<_Tp> polar(const _Tp&, const _Tp& = 0);
82 /// Return complex cosine of @a z.
83 template<typename _Tp> complex<_Tp> cos(const complex<_Tp>&);
84 /// Return complex hyperbolic cosine of @a z.
85 template<typename _Tp> complex<_Tp> cosh(const complex<_Tp>&);
86 /// Return complex base e exponential of @a z.
87 template<typename _Tp> complex<_Tp> exp(const complex<_Tp>&);
88 /// Return complex natural logarithm of @a z.
89 template<typename _Tp> complex<_Tp> log(const complex<_Tp>&);
90 /// Return complex base 10 logarithm of @a z.
91 template<typename _Tp> complex<_Tp> log10(const complex<_Tp>&);
92 /// Return @a x to the @a y'th power.
93 template<typename _Tp> complex<_Tp> pow(const complex<_Tp>&, int);
94 /// Return @a x to the @a y'th power.
95 template<typename _Tp> complex<_Tp> pow(const complex<_Tp>&, const _Tp&);
96 /// Return @a x to the @a y'th power.
97 template<typename _Tp> complex<_Tp> pow(const complex<_Tp>&,
99 /// Return @a x to the @a y'th power.
100 template<typename _Tp> complex<_Tp> pow(const _Tp&, const complex<_Tp>&);
101 /// Return complex sine of @a z.
102 template<typename _Tp> complex<_Tp> sin(const complex<_Tp>&);
103 /// Return complex hyperbolic sine of @a z.
104 template<typename _Tp> complex<_Tp> sinh(const complex<_Tp>&);
105 /// Return complex square root of @a z.
106 template<typename _Tp> complex<_Tp> sqrt(const complex<_Tp>&);
107 /// Return complex tangent of @a z.
108 template<typename _Tp> complex<_Tp> tan(const complex<_Tp>&);
109 /// Return complex hyperbolic tangent of @a z.
110 template<typename _Tp> complex<_Tp> tanh(const complex<_Tp>&);
113 // 26.2.2 Primary template class complex
115 * Template to represent complex numbers.
117 * Specializations for float, double, and long double are part of the
118 * library. Results with any other type are not guaranteed.
120 * @param Tp Type of real and imaginary values.
122 template<typename _Tp>
126 typedef _Tp value_type;
128 /// Default constructor. First parameter is x, second parameter is y.
129 /// Unspecified parameters default to 0.
130 _GLIBCXX_CONSTEXPR complex(const _Tp& __r = _Tp(), const _Tp& __i = _Tp())
131 : _M_real(__r), _M_imag(__i) { }
133 // Let the compiler synthesize the copy constructor
134 #if __cplusplus >= 201103L
135 constexpr complex(const complex&) = default;
138 /// Converting constructor.
139 template<typename _Up>
140 _GLIBCXX_CONSTEXPR complex(const complex<_Up>& __z)
141 : _M_real(__z.real()), _M_imag(__z.imag()) { }
143 #if __cplusplus >= 201103L
144 // _GLIBCXX_RESOLVE_LIB_DEFECTS
145 // DR 387. std::complex over-encapsulated.
146 _GLIBCXX_ABI_TAG_CXX11
148 real() const { return _M_real; }
150 _GLIBCXX_ABI_TAG_CXX11
152 imag() const { return _M_imag; }
154 /// Return real part of complex number.
156 real() { return _M_real; }
158 /// Return real part of complex number.
160 real() const { return _M_real; }
162 /// Return imaginary part of complex number.
164 imag() { return _M_imag; }
166 /// Return imaginary part of complex number.
168 imag() const { return _M_imag; }
171 // _GLIBCXX_RESOLVE_LIB_DEFECTS
172 // DR 387. std::complex over-encapsulated.
173 _GLIBCXX20_CONSTEXPR void
174 real(_Tp __val) { _M_real = __val; }
176 _GLIBCXX20_CONSTEXPR void
177 imag(_Tp __val) { _M_imag = __val; }
179 /// Assign a scalar to this complex number.
180 _GLIBCXX20_CONSTEXPR complex<_Tp>& operator=(const _Tp&);
182 /// Add a scalar to this complex number.
184 _GLIBCXX20_CONSTEXPR complex<_Tp>&
185 operator+=(const _Tp& __t)
191 /// Subtract a scalar from this complex number.
193 _GLIBCXX20_CONSTEXPR complex<_Tp>&
194 operator-=(const _Tp& __t)
200 /// Multiply this complex number by a scalar.
201 _GLIBCXX20_CONSTEXPR complex<_Tp>& operator*=(const _Tp&);
202 /// Divide this complex number by a scalar.
203 _GLIBCXX20_CONSTEXPR complex<_Tp>& operator/=(const _Tp&);
205 // Let the compiler synthesize the copy assignment operator
206 #if __cplusplus >= 201103L
207 _GLIBCXX20_CONSTEXPR complex& operator=(const complex&) = default;
210 /// Assign another complex number to this one.
211 template<typename _Up>
212 _GLIBCXX20_CONSTEXPR complex<_Tp>& operator=(const complex<_Up>&);
213 /// Add another complex number to this one.
214 template<typename _Up>
215 _GLIBCXX20_CONSTEXPR complex<_Tp>& operator+=(const complex<_Up>&);
216 /// Subtract another complex number from this one.
217 template<typename _Up>
218 _GLIBCXX20_CONSTEXPR complex<_Tp>& operator-=(const complex<_Up>&);
219 /// Multiply this complex number by another.
220 template<typename _Up>
221 _GLIBCXX20_CONSTEXPR complex<_Tp>& operator*=(const complex<_Up>&);
222 /// Divide this complex number by another.
223 template<typename _Up>
224 _GLIBCXX20_CONSTEXPR complex<_Tp>& operator/=(const complex<_Up>&);
226 _GLIBCXX_CONSTEXPR complex __rep() const
234 template<typename _Tp>
235 _GLIBCXX20_CONSTEXPR complex<_Tp>&
236 complex<_Tp>::operator=(const _Tp& __t)
244 template<typename _Tp>
245 _GLIBCXX20_CONSTEXPR complex<_Tp>&
246 complex<_Tp>::operator*=(const _Tp& __t)
254 template<typename _Tp>
255 _GLIBCXX20_CONSTEXPR complex<_Tp>&
256 complex<_Tp>::operator/=(const _Tp& __t)
263 template<typename _Tp>
264 template<typename _Up>
265 _GLIBCXX20_CONSTEXPR complex<_Tp>&
266 complex<_Tp>::operator=(const complex<_Up>& __z)
268 _M_real = __z.real();
269 _M_imag = __z.imag();
274 template<typename _Tp>
275 template<typename _Up>
276 _GLIBCXX20_CONSTEXPR complex<_Tp>&
277 complex<_Tp>::operator+=(const complex<_Up>& __z)
279 _M_real += __z.real();
280 _M_imag += __z.imag();
285 template<typename _Tp>
286 template<typename _Up>
287 _GLIBCXX20_CONSTEXPR complex<_Tp>&
288 complex<_Tp>::operator-=(const complex<_Up>& __z)
290 _M_real -= __z.real();
291 _M_imag -= __z.imag();
296 // XXX: This is a grammar school implementation.
297 template<typename _Tp>
298 template<typename _Up>
299 _GLIBCXX20_CONSTEXPR complex<_Tp>&
300 complex<_Tp>::operator*=(const complex<_Up>& __z)
302 const _Tp __r = _M_real * __z.real() - _M_imag * __z.imag();
303 _M_imag = _M_real * __z.imag() + _M_imag * __z.real();
309 // XXX: This is a grammar school implementation.
310 template<typename _Tp>
311 template<typename _Up>
312 _GLIBCXX20_CONSTEXPR complex<_Tp>&
313 complex<_Tp>::operator/=(const complex<_Up>& __z)
315 const _Tp __r = _M_real * __z.real() + _M_imag * __z.imag();
316 const _Tp __n = std::norm(__z);
317 _M_imag = (_M_imag * __z.real() - _M_real * __z.imag()) / __n;
324 /// Return new complex value @a x plus @a y.
325 template<typename _Tp>
326 inline _GLIBCXX20_CONSTEXPR complex<_Tp>
327 operator+(const complex<_Tp>& __x, const complex<_Tp>& __y)
329 complex<_Tp> __r = __x;
334 template<typename _Tp>
335 inline _GLIBCXX20_CONSTEXPR complex<_Tp>
336 operator+(const complex<_Tp>& __x, const _Tp& __y)
338 complex<_Tp> __r = __x;
343 template<typename _Tp>
344 inline _GLIBCXX20_CONSTEXPR complex<_Tp>
345 operator+(const _Tp& __x, const complex<_Tp>& __y)
347 complex<_Tp> __r = __y;
354 /// Return new complex value @a x minus @a y.
355 template<typename _Tp>
356 inline _GLIBCXX20_CONSTEXPR complex<_Tp>
357 operator-(const complex<_Tp>& __x, const complex<_Tp>& __y)
359 complex<_Tp> __r = __x;
364 template<typename _Tp>
365 inline _GLIBCXX20_CONSTEXPR complex<_Tp>
366 operator-(const complex<_Tp>& __x, const _Tp& __y)
368 complex<_Tp> __r = __x;
373 template<typename _Tp>
374 inline _GLIBCXX20_CONSTEXPR complex<_Tp>
375 operator-(const _Tp& __x, const complex<_Tp>& __y)
377 complex<_Tp> __r = -__y;
384 /// Return new complex value @a x times @a y.
385 template<typename _Tp>
386 inline _GLIBCXX20_CONSTEXPR complex<_Tp>
387 operator*(const complex<_Tp>& __x, const complex<_Tp>& __y)
389 complex<_Tp> __r = __x;
394 template<typename _Tp>
395 inline _GLIBCXX20_CONSTEXPR complex<_Tp>
396 operator*(const complex<_Tp>& __x, const _Tp& __y)
398 complex<_Tp> __r = __x;
403 template<typename _Tp>
404 inline _GLIBCXX20_CONSTEXPR complex<_Tp>
405 operator*(const _Tp& __x, const complex<_Tp>& __y)
407 complex<_Tp> __r = __y;
414 /// Return new complex value @a x divided by @a y.
415 template<typename _Tp>
416 inline _GLIBCXX20_CONSTEXPR complex<_Tp>
417 operator/(const complex<_Tp>& __x, const complex<_Tp>& __y)
419 complex<_Tp> __r = __x;
424 template<typename _Tp>
425 inline _GLIBCXX20_CONSTEXPR complex<_Tp>
426 operator/(const complex<_Tp>& __x, const _Tp& __y)
428 complex<_Tp> __r = __x;
433 template<typename _Tp>
434 inline _GLIBCXX20_CONSTEXPR complex<_Tp>
435 operator/(const _Tp& __x, const complex<_Tp>& __y)
437 complex<_Tp> __r = __x;
444 template<typename _Tp>
445 inline _GLIBCXX20_CONSTEXPR complex<_Tp>
446 operator+(const complex<_Tp>& __x)
449 /// Return complex negation of @a x.
450 template<typename _Tp>
451 inline _GLIBCXX20_CONSTEXPR complex<_Tp>
452 operator-(const complex<_Tp>& __x)
453 { return complex<_Tp>(-__x.real(), -__x.imag()); }
456 /// Return true if @a x is equal to @a y.
457 template<typename _Tp>
458 inline _GLIBCXX_CONSTEXPR bool
459 operator==(const complex<_Tp>& __x, const complex<_Tp>& __y)
460 { return __x.real() == __y.real() && __x.imag() == __y.imag(); }
462 template<typename _Tp>
463 inline _GLIBCXX_CONSTEXPR bool
464 operator==(const complex<_Tp>& __x, const _Tp& __y)
465 { return __x.real() == __y && __x.imag() == _Tp(); }
467 template<typename _Tp>
468 inline _GLIBCXX_CONSTEXPR bool
469 operator==(const _Tp& __x, const complex<_Tp>& __y)
470 { return __x == __y.real() && _Tp() == __y.imag(); }
474 /// Return false if @a x is equal to @a y.
475 template<typename _Tp>
476 inline _GLIBCXX_CONSTEXPR bool
477 operator!=(const complex<_Tp>& __x, const complex<_Tp>& __y)
478 { return __x.real() != __y.real() || __x.imag() != __y.imag(); }
480 template<typename _Tp>
481 inline _GLIBCXX_CONSTEXPR bool
482 operator!=(const complex<_Tp>& __x, const _Tp& __y)
483 { return __x.real() != __y || __x.imag() != _Tp(); }
485 template<typename _Tp>
486 inline _GLIBCXX_CONSTEXPR bool
487 operator!=(const _Tp& __x, const complex<_Tp>& __y)
488 { return __x != __y.real() || _Tp() != __y.imag(); }
491 /// Extraction operator for complex values.
492 template<typename _Tp, typename _CharT, class _Traits>
493 basic_istream<_CharT, _Traits>&
494 operator>>(basic_istream<_CharT, _Traits>& __is, complex<_Tp>& __x)
500 if (_Traits::eq(__ch, __is.widen('(')))
503 if (__is >> __u >> __ch)
505 const _CharT __rparen = __is.widen(')');
506 if (_Traits::eq(__ch, __rparen))
511 else if (_Traits::eq(__ch, __is.widen(',')))
514 if (__is >> __v >> __ch)
516 if (_Traits::eq(__ch, __rparen))
518 __x = complex<_Tp>(__u, __v);
541 __is.setstate(ios_base::failbit);
545 /// Insertion operator for complex values.
546 template<typename _Tp, typename _CharT, class _Traits>
547 basic_ostream<_CharT, _Traits>&
548 operator<<(basic_ostream<_CharT, _Traits>& __os, const complex<_Tp>& __x)
550 basic_ostringstream<_CharT, _Traits> __s;
551 __s.flags(__os.flags());
552 __s.imbue(__os.getloc());
553 __s.precision(__os.precision());
554 __s << '(' << __x.real() << ',' << __x.imag() << ')';
555 return __os << __s.str();
559 #if __cplusplus >= 201103L
560 template<typename _Tp>
562 real(const complex<_Tp>& __z)
563 { return __z.real(); }
565 template<typename _Tp>
567 imag(const complex<_Tp>& __z)
568 { return __z.imag(); }
570 template<typename _Tp>
572 real(complex<_Tp>& __z)
573 { return __z.real(); }
575 template<typename _Tp>
577 real(const complex<_Tp>& __z)
578 { return __z.real(); }
580 template<typename _Tp>
582 imag(complex<_Tp>& __z)
583 { return __z.imag(); }
585 template<typename _Tp>
587 imag(const complex<_Tp>& __z)
588 { return __z.imag(); }
591 // 26.2.7/3 abs(__z): Returns the magnitude of __z.
592 template<typename _Tp>
594 __complex_abs(const complex<_Tp>& __z)
596 _Tp __x = __z.real();
597 _Tp __y = __z.imag();
598 const _Tp __s = std::max(abs(__x), abs(__y));
599 if (__s == _Tp()) // well ...
603 return __s * sqrt(__x * __x + __y * __y);
606 #if _GLIBCXX_USE_C99_COMPLEX
608 __complex_abs(__complex__ float __z) { return __builtin_cabsf(__z); }
611 __complex_abs(__complex__ double __z) { return __builtin_cabs(__z); }
614 __complex_abs(const __complex__ long double& __z)
615 { return __builtin_cabsl(__z); }
617 template<typename _Tp>
619 abs(const complex<_Tp>& __z) { return __complex_abs(__z.__rep()); }
621 template<typename _Tp>
623 abs(const complex<_Tp>& __z) { return __complex_abs(__z); }
627 // 26.2.7/4: arg(__z): Returns the phase angle of __z.
628 template<typename _Tp>
630 __complex_arg(const complex<_Tp>& __z)
631 { return atan2(__z.imag(), __z.real()); }
633 #if _GLIBCXX_USE_C99_COMPLEX
635 __complex_arg(__complex__ float __z) { return __builtin_cargf(__z); }
638 __complex_arg(__complex__ double __z) { return __builtin_carg(__z); }
641 __complex_arg(const __complex__ long double& __z)
642 { return __builtin_cargl(__z); }
644 template<typename _Tp>
646 arg(const complex<_Tp>& __z) { return __complex_arg(__z.__rep()); }
648 template<typename _Tp>
650 arg(const complex<_Tp>& __z) { return __complex_arg(__z); }
653 // 26.2.7/5: norm(__z) returns the squared magnitude of __z.
654 // As defined, norm() is -not- a norm is the common mathematical
655 // sense used in numerics. The helper class _Norm_helper<> tries to
656 // distinguish between builtin floating point and the rest, so as
657 // to deliver an answer as close as possible to the real value.
661 template<typename _Tp>
662 static inline _GLIBCXX20_CONSTEXPR _Tp _S_do_it(const complex<_Tp>& __z)
664 const _Tp __x = __z.real();
665 const _Tp __y = __z.imag();
666 return __x * __x + __y * __y;
671 struct _Norm_helper<true>
673 template<typename _Tp>
674 static inline _GLIBCXX20_CONSTEXPR _Tp _S_do_it(const complex<_Tp>& __z)
676 //_Tp __res = std::abs(__z);
677 //return __res * __res;
678 const _Tp __x = __z.real();
679 const _Tp __y = __z.imag();
680 return __x * __x + __y * __y;
684 template<typename _Tp>
685 inline _GLIBCXX20_CONSTEXPR _Tp
686 norm(const complex<_Tp>& __z)
688 return _Norm_helper<__is_floating<_Tp>::__value
689 && !_GLIBCXX_FAST_MATH>::_S_do_it(__z);
692 template<typename _Tp>
694 polar(const _Tp& __rho, const _Tp& __theta)
696 __glibcxx_assert( __rho >= 0 );
697 return complex<_Tp>(__rho * cos(__theta), __rho * sin(__theta));
700 template<typename _Tp>
701 inline _GLIBCXX20_CONSTEXPR complex<_Tp>
702 conj(const complex<_Tp>& __z)
703 { return complex<_Tp>(__z.real(), -__z.imag()); }
707 // 26.2.8/1 cos(__z): Returns the cosine of __z.
708 template<typename _Tp>
710 __complex_cos(const complex<_Tp>& __z)
712 const _Tp __x = __z.real();
713 const _Tp __y = __z.imag();
714 return complex<_Tp>(cos(__x) * cosh(__y), -sin(__x) * sinh(__y));
717 #if _GLIBCXX_USE_C99_COMPLEX
718 inline __complex__ float
719 __complex_cos(__complex__ float __z) { return __builtin_ccosf(__z); }
721 inline __complex__ double
722 __complex_cos(__complex__ double __z) { return __builtin_ccos(__z); }
724 inline __complex__ long double
725 __complex_cos(const __complex__ long double& __z)
726 { return __builtin_ccosl(__z); }
728 template<typename _Tp>
730 cos(const complex<_Tp>& __z) { return __complex_cos(__z.__rep()); }
732 template<typename _Tp>
734 cos(const complex<_Tp>& __z) { return __complex_cos(__z); }
737 // 26.2.8/2 cosh(__z): Returns the hyperbolic cosine of __z.
738 template<typename _Tp>
740 __complex_cosh(const complex<_Tp>& __z)
742 const _Tp __x = __z.real();
743 const _Tp __y = __z.imag();
744 return complex<_Tp>(cosh(__x) * cos(__y), sinh(__x) * sin(__y));
747 #if _GLIBCXX_USE_C99_COMPLEX
748 inline __complex__ float
749 __complex_cosh(__complex__ float __z) { return __builtin_ccoshf(__z); }
751 inline __complex__ double
752 __complex_cosh(__complex__ double __z) { return __builtin_ccosh(__z); }
754 inline __complex__ long double
755 __complex_cosh(const __complex__ long double& __z)
756 { return __builtin_ccoshl(__z); }
758 template<typename _Tp>
760 cosh(const complex<_Tp>& __z) { return __complex_cosh(__z.__rep()); }
762 template<typename _Tp>
764 cosh(const complex<_Tp>& __z) { return __complex_cosh(__z); }
767 // 26.2.8/3 exp(__z): Returns the complex base e exponential of x
768 template<typename _Tp>
770 __complex_exp(const complex<_Tp>& __z)
771 { return std::polar<_Tp>(exp(__z.real()), __z.imag()); }
773 #if _GLIBCXX_USE_C99_COMPLEX
774 inline __complex__ float
775 __complex_exp(__complex__ float __z) { return __builtin_cexpf(__z); }
777 inline __complex__ double
778 __complex_exp(__complex__ double __z) { return __builtin_cexp(__z); }
780 inline __complex__ long double
781 __complex_exp(const __complex__ long double& __z)
782 { return __builtin_cexpl(__z); }
784 template<typename _Tp>
786 exp(const complex<_Tp>& __z) { return __complex_exp(__z.__rep()); }
788 template<typename _Tp>
790 exp(const complex<_Tp>& __z) { return __complex_exp(__z); }
793 // 26.2.8/5 log(__z): Returns the natural complex logarithm of __z.
794 // The branch cut is along the negative axis.
795 template<typename _Tp>
797 __complex_log(const complex<_Tp>& __z)
798 { return complex<_Tp>(log(std::abs(__z)), std::arg(__z)); }
800 #if _GLIBCXX_USE_C99_COMPLEX
801 inline __complex__ float
802 __complex_log(__complex__ float __z) { return __builtin_clogf(__z); }
804 inline __complex__ double
805 __complex_log(__complex__ double __z) { return __builtin_clog(__z); }
807 inline __complex__ long double
808 __complex_log(const __complex__ long double& __z)
809 { return __builtin_clogl(__z); }
811 template<typename _Tp>
813 log(const complex<_Tp>& __z) { return __complex_log(__z.__rep()); }
815 template<typename _Tp>
817 log(const complex<_Tp>& __z) { return __complex_log(__z); }
820 template<typename _Tp>
822 log10(const complex<_Tp>& __z)
823 { return std::log(__z) / log(_Tp(10.0)); }
825 // 26.2.8/10 sin(__z): Returns the sine of __z.
826 template<typename _Tp>
828 __complex_sin(const complex<_Tp>& __z)
830 const _Tp __x = __z.real();
831 const _Tp __y = __z.imag();
832 return complex<_Tp>(sin(__x) * cosh(__y), cos(__x) * sinh(__y));
835 #if _GLIBCXX_USE_C99_COMPLEX
836 inline __complex__ float
837 __complex_sin(__complex__ float __z) { return __builtin_csinf(__z); }
839 inline __complex__ double
840 __complex_sin(__complex__ double __z) { return __builtin_csin(__z); }
842 inline __complex__ long double
843 __complex_sin(const __complex__ long double& __z)
844 { return __builtin_csinl(__z); }
846 template<typename _Tp>
848 sin(const complex<_Tp>& __z) { return __complex_sin(__z.__rep()); }
850 template<typename _Tp>
852 sin(const complex<_Tp>& __z) { return __complex_sin(__z); }
855 // 26.2.8/11 sinh(__z): Returns the hyperbolic sine of __z.
856 template<typename _Tp>
858 __complex_sinh(const complex<_Tp>& __z)
860 const _Tp __x = __z.real();
861 const _Tp __y = __z.imag();
862 return complex<_Tp>(sinh(__x) * cos(__y), cosh(__x) * sin(__y));
865 #if _GLIBCXX_USE_C99_COMPLEX
866 inline __complex__ float
867 __complex_sinh(__complex__ float __z) { return __builtin_csinhf(__z); }
869 inline __complex__ double
870 __complex_sinh(__complex__ double __z) { return __builtin_csinh(__z); }
872 inline __complex__ long double
873 __complex_sinh(const __complex__ long double& __z)
874 { return __builtin_csinhl(__z); }
876 template<typename _Tp>
878 sinh(const complex<_Tp>& __z) { return __complex_sinh(__z.__rep()); }
880 template<typename _Tp>
882 sinh(const complex<_Tp>& __z) { return __complex_sinh(__z); }
885 // 26.2.8/13 sqrt(__z): Returns the complex square root of __z.
886 // The branch cut is on the negative axis.
887 template<typename _Tp>
889 __complex_sqrt(const complex<_Tp>& __z)
891 _Tp __x = __z.real();
892 _Tp __y = __z.imag();
896 _Tp __t = sqrt(abs(__y) / 2);
897 return complex<_Tp>(__t, __y < _Tp() ? -__t : __t);
901 _Tp __t = sqrt(2 * (std::abs(__z) + abs(__x)));
904 ? complex<_Tp>(__u, __y / __t)
905 : complex<_Tp>(abs(__y) / __t, __y < _Tp() ? -__u : __u);
909 #if _GLIBCXX_USE_C99_COMPLEX
910 inline __complex__ float
911 __complex_sqrt(__complex__ float __z) { return __builtin_csqrtf(__z); }
913 inline __complex__ double
914 __complex_sqrt(__complex__ double __z) { return __builtin_csqrt(__z); }
916 inline __complex__ long double
917 __complex_sqrt(const __complex__ long double& __z)
918 { return __builtin_csqrtl(__z); }
920 template<typename _Tp>
922 sqrt(const complex<_Tp>& __z) { return __complex_sqrt(__z.__rep()); }
924 template<typename _Tp>
926 sqrt(const complex<_Tp>& __z) { return __complex_sqrt(__z); }
929 // 26.2.8/14 tan(__z): Return the complex tangent of __z.
931 template<typename _Tp>
933 __complex_tan(const complex<_Tp>& __z)
934 { return std::sin(__z) / std::cos(__z); }
936 #if _GLIBCXX_USE_C99_COMPLEX
937 inline __complex__ float
938 __complex_tan(__complex__ float __z) { return __builtin_ctanf(__z); }
940 inline __complex__ double
941 __complex_tan(__complex__ double __z) { return __builtin_ctan(__z); }
943 inline __complex__ long double
944 __complex_tan(const __complex__ long double& __z)
945 { return __builtin_ctanl(__z); }
947 template<typename _Tp>
949 tan(const complex<_Tp>& __z) { return __complex_tan(__z.__rep()); }
951 template<typename _Tp>
953 tan(const complex<_Tp>& __z) { return __complex_tan(__z); }
957 // 26.2.8/15 tanh(__z): Returns the hyperbolic tangent of __z.
959 template<typename _Tp>
961 __complex_tanh(const complex<_Tp>& __z)
962 { return std::sinh(__z) / std::cosh(__z); }
964 #if _GLIBCXX_USE_C99_COMPLEX
965 inline __complex__ float
966 __complex_tanh(__complex__ float __z) { return __builtin_ctanhf(__z); }
968 inline __complex__ double
969 __complex_tanh(__complex__ double __z) { return __builtin_ctanh(__z); }
971 inline __complex__ long double
972 __complex_tanh(const __complex__ long double& __z)
973 { return __builtin_ctanhl(__z); }
975 template<typename _Tp>
977 tanh(const complex<_Tp>& __z) { return __complex_tanh(__z.__rep()); }
979 template<typename _Tp>
981 tanh(const complex<_Tp>& __z) { return __complex_tanh(__z); }
985 // 26.2.8/9 pow(__x, __y): Returns the complex power base of __x
986 // raised to the __y-th power. The branch
987 // cut is on the negative axis.
988 template<typename _Tp>
990 __complex_pow_unsigned(complex<_Tp> __x, unsigned __n)
992 complex<_Tp> __y = __n % 2 ? __x : complex<_Tp>(1);
1004 // In C++11 mode we used to implement the resolution of
1005 // DR 844. complex pow return type is ambiguous.
1006 // thus the following overload was disabled in that mode. However, doing
1007 // that causes all sorts of issues, see, for example:
1008 // http://gcc.gnu.org/ml/libstdc++/2013-01/msg00058.html
1009 // and also PR57974.
1010 template<typename _Tp>
1012 pow(const complex<_Tp>& __z, int __n)
1015 ? complex<_Tp>(1) / std::__complex_pow_unsigned(__z, -(unsigned)__n)
1016 : std::__complex_pow_unsigned(__z, __n);
1019 template<typename _Tp>
1021 pow(const complex<_Tp>& __x, const _Tp& __y)
1023 #if ! _GLIBCXX_USE_C99_COMPLEX
1027 if (__x.imag() == _Tp() && __x.real() > _Tp())
1028 return pow(__x.real(), __y);
1030 complex<_Tp> __t = std::log(__x);
1031 return std::polar<_Tp>(exp(__y * __t.real()), __y * __t.imag());
1034 template<typename _Tp>
1036 __complex_pow(const complex<_Tp>& __x, const complex<_Tp>& __y)
1037 { return __x == _Tp() ? _Tp() : std::exp(__y * std::log(__x)); }
1039 #if _GLIBCXX_USE_C99_COMPLEX
1040 inline __complex__ float
1041 __complex_pow(__complex__ float __x, __complex__ float __y)
1042 { return __builtin_cpowf(__x, __y); }
1044 inline __complex__ double
1045 __complex_pow(__complex__ double __x, __complex__ double __y)
1046 { return __builtin_cpow(__x, __y); }
1048 inline __complex__ long double
1049 __complex_pow(const __complex__ long double& __x,
1050 const __complex__ long double& __y)
1051 { return __builtin_cpowl(__x, __y); }
1053 template<typename _Tp>
1055 pow(const complex<_Tp>& __x, const complex<_Tp>& __y)
1056 { return __complex_pow(__x.__rep(), __y.__rep()); }
1058 template<typename _Tp>
1060 pow(const complex<_Tp>& __x, const complex<_Tp>& __y)
1061 { return __complex_pow(__x, __y); }
1064 template<typename _Tp>
1066 pow(const _Tp& __x, const complex<_Tp>& __y)
1068 return __x > _Tp() ? std::polar<_Tp>(pow(__x, __y.real()),
1069 __y.imag() * log(__x))
1070 : std::pow(complex<_Tp>(__x), __y);
1073 /// 26.2.3 complex specializations
1074 /// complex<float> specialization
1076 struct complex<float>
1078 typedef float value_type;
1079 typedef __complex__ float _ComplexT;
1081 _GLIBCXX_CONSTEXPR complex(_ComplexT __z) : _M_value(__z) { }
1083 _GLIBCXX_CONSTEXPR complex(float __r = 0.0f, float __i = 0.0f)
1084 #if __cplusplus >= 201103L
1085 : _M_value{ __r, __i } { }
1088 __real__ _M_value = __r;
1089 __imag__ _M_value = __i;
1093 explicit _GLIBCXX_CONSTEXPR complex(const complex<double>&);
1094 explicit _GLIBCXX_CONSTEXPR complex(const complex<long double>&);
1096 #if __cplusplus >= 201103L
1097 // _GLIBCXX_RESOLVE_LIB_DEFECTS
1098 // DR 387. std::complex over-encapsulated.
1099 __attribute ((__abi_tag__ ("cxx11")))
1101 real() const { return __real__ _M_value; }
1103 __attribute ((__abi_tag__ ("cxx11")))
1105 imag() const { return __imag__ _M_value; }
1108 real() { return __real__ _M_value; }
1111 real() const { return __real__ _M_value; }
1114 imag() { return __imag__ _M_value; }
1117 imag() const { return __imag__ _M_value; }
1120 // _GLIBCXX_RESOLVE_LIB_DEFECTS
1121 // DR 387. std::complex over-encapsulated.
1122 _GLIBCXX20_CONSTEXPR void
1123 real(float __val) { __real__ _M_value = __val; }
1125 _GLIBCXX20_CONSTEXPR void
1126 imag(float __val) { __imag__ _M_value = __val; }
1128 _GLIBCXX20_CONSTEXPR complex&
1129 operator=(float __f)
1135 _GLIBCXX20_CONSTEXPR complex&
1136 operator+=(float __f)
1142 _GLIBCXX20_CONSTEXPR complex&
1143 operator-=(float __f)
1149 _GLIBCXX20_CONSTEXPR complex&
1150 operator*=(float __f)
1156 _GLIBCXX20_CONSTEXPR complex&
1157 operator/=(float __f)
1163 // Let the compiler synthesize the copy and assignment
1164 // operator. It always does a pretty good job.
1165 #if __cplusplus >= 201103L
1166 _GLIBCXX14_CONSTEXPR complex& operator=(const complex&) = default;
1169 template<typename _Tp>
1170 _GLIBCXX20_CONSTEXPR complex&
1171 operator=(const complex<_Tp>& __z)
1173 __real__ _M_value = __z.real();
1174 __imag__ _M_value = __z.imag();
1178 template<typename _Tp>
1179 _GLIBCXX20_CONSTEXPR complex&
1180 operator+=(const complex<_Tp>& __z)
1182 _M_value += __z.__rep();
1187 _GLIBCXX20_CONSTEXPR complex&
1188 operator-=(const complex<_Tp>& __z)
1190 _M_value -= __z.__rep();
1195 _GLIBCXX20_CONSTEXPR complex&
1196 operator*=(const complex<_Tp>& __z)
1198 const _ComplexT __t = __z.__rep();
1204 _GLIBCXX20_CONSTEXPR complex&
1205 operator/=(const complex<_Tp>& __z)
1207 const _ComplexT __t = __z.__rep();
1212 _GLIBCXX_CONSTEXPR _ComplexT __rep() const { return _M_value; }
1218 /// 26.2.3 complex specializations
1219 /// complex<double> specialization
1221 struct complex<double>
1223 typedef double value_type;
1224 typedef __complex__ double _ComplexT;
1226 _GLIBCXX_CONSTEXPR complex(_ComplexT __z) : _M_value(__z) { }
1228 _GLIBCXX_CONSTEXPR complex(double __r = 0.0, double __i = 0.0)
1229 #if __cplusplus >= 201103L
1230 : _M_value{ __r, __i } { }
1233 __real__ _M_value = __r;
1234 __imag__ _M_value = __i;
1238 _GLIBCXX_CONSTEXPR complex(const complex<float>& __z)
1239 : _M_value(__z.__rep()) { }
1241 explicit _GLIBCXX_CONSTEXPR complex(const complex<long double>&);
1243 #if __cplusplus >= 201103L
1244 // _GLIBCXX_RESOLVE_LIB_DEFECTS
1245 // DR 387. std::complex over-encapsulated.
1246 __attribute ((__abi_tag__ ("cxx11")))
1248 real() const { return __real__ _M_value; }
1250 __attribute ((__abi_tag__ ("cxx11")))
1252 imag() const { return __imag__ _M_value; }
1255 real() { return __real__ _M_value; }
1258 real() const { return __real__ _M_value; }
1261 imag() { return __imag__ _M_value; }
1264 imag() const { return __imag__ _M_value; }
1267 // _GLIBCXX_RESOLVE_LIB_DEFECTS
1268 // DR 387. std::complex over-encapsulated.
1269 _GLIBCXX20_CONSTEXPR void
1270 real(double __val) { __real__ _M_value = __val; }
1272 _GLIBCXX20_CONSTEXPR void
1273 imag(double __val) { __imag__ _M_value = __val; }
1275 _GLIBCXX20_CONSTEXPR complex&
1276 operator=(double __d)
1282 _GLIBCXX20_CONSTEXPR complex&
1283 operator+=(double __d)
1289 _GLIBCXX20_CONSTEXPR complex&
1290 operator-=(double __d)
1296 _GLIBCXX20_CONSTEXPR complex&
1297 operator*=(double __d)
1303 _GLIBCXX20_CONSTEXPR complex&
1304 operator/=(double __d)
1310 // The compiler will synthesize this, efficiently.
1311 #if __cplusplus >= 201103L
1312 _GLIBCXX14_CONSTEXPR complex& operator=(const complex&) = default;
1315 template<typename _Tp>
1316 _GLIBCXX20_CONSTEXPR complex&
1317 operator=(const complex<_Tp>& __z)
1319 _M_value = __z.__rep();
1323 template<typename _Tp>
1324 _GLIBCXX20_CONSTEXPR complex&
1325 operator+=(const complex<_Tp>& __z)
1327 _M_value += __z.__rep();
1331 template<typename _Tp>
1332 _GLIBCXX20_CONSTEXPR complex&
1333 operator-=(const complex<_Tp>& __z)
1335 _M_value -= __z.__rep();
1339 template<typename _Tp>
1340 _GLIBCXX20_CONSTEXPR complex&
1341 operator*=(const complex<_Tp>& __z)
1343 const _ComplexT __t = __z.__rep();
1348 template<typename _Tp>
1349 _GLIBCXX20_CONSTEXPR complex&
1350 operator/=(const complex<_Tp>& __z)
1352 const _ComplexT __t = __z.__rep();
1357 _GLIBCXX_CONSTEXPR _ComplexT __rep() const { return _M_value; }
1363 /// 26.2.3 complex specializations
1364 /// complex<long double> specialization
1366 struct complex<long double>
1368 typedef long double value_type;
1369 typedef __complex__ long double _ComplexT;
1371 _GLIBCXX_CONSTEXPR complex(_ComplexT __z) : _M_value(__z) { }
1373 _GLIBCXX_CONSTEXPR complex(long double __r = 0.0L,
1374 long double __i = 0.0L)
1375 #if __cplusplus >= 201103L
1376 : _M_value{ __r, __i } { }
1379 __real__ _M_value = __r;
1380 __imag__ _M_value = __i;
1384 _GLIBCXX_CONSTEXPR complex(const complex<float>& __z)
1385 : _M_value(__z.__rep()) { }
1387 _GLIBCXX_CONSTEXPR complex(const complex<double>& __z)
1388 : _M_value(__z.__rep()) { }
1390 #if __cplusplus >= 201103L
1391 // _GLIBCXX_RESOLVE_LIB_DEFECTS
1392 // DR 387. std::complex over-encapsulated.
1393 __attribute ((__abi_tag__ ("cxx11")))
1394 constexpr long double
1395 real() const { return __real__ _M_value; }
1397 __attribute ((__abi_tag__ ("cxx11")))
1398 constexpr long double
1399 imag() const { return __imag__ _M_value; }
1402 real() { return __real__ _M_value; }
1405 real() const { return __real__ _M_value; }
1408 imag() { return __imag__ _M_value; }
1411 imag() const { return __imag__ _M_value; }
1414 // _GLIBCXX_RESOLVE_LIB_DEFECTS
1415 // DR 387. std::complex over-encapsulated.
1416 _GLIBCXX20_CONSTEXPR void
1417 real(long double __val) { __real__ _M_value = __val; }
1419 _GLIBCXX20_CONSTEXPR void
1420 imag(long double __val) { __imag__ _M_value = __val; }
1422 _GLIBCXX20_CONSTEXPR complex&
1423 operator=(long double __r)
1429 _GLIBCXX20_CONSTEXPR complex&
1430 operator+=(long double __r)
1436 _GLIBCXX20_CONSTEXPR complex&
1437 operator-=(long double __r)
1443 _GLIBCXX20_CONSTEXPR complex&
1444 operator*=(long double __r)
1450 _GLIBCXX20_CONSTEXPR complex&
1451 operator/=(long double __r)
1457 // The compiler knows how to do this efficiently
1458 #if __cplusplus >= 201103L
1459 _GLIBCXX14_CONSTEXPR complex& operator=(const complex&) = default;
1462 template<typename _Tp>
1463 _GLIBCXX20_CONSTEXPR complex&
1464 operator=(const complex<_Tp>& __z)
1466 _M_value = __z.__rep();
1470 template<typename _Tp>
1471 _GLIBCXX20_CONSTEXPR complex&
1472 operator+=(const complex<_Tp>& __z)
1474 _M_value += __z.__rep();
1478 template<typename _Tp>
1479 _GLIBCXX20_CONSTEXPR complex&
1480 operator-=(const complex<_Tp>& __z)
1482 _M_value -= __z.__rep();
1486 template<typename _Tp>
1487 _GLIBCXX20_CONSTEXPR complex&
1488 operator*=(const complex<_Tp>& __z)
1490 const _ComplexT __t = __z.__rep();
1495 template<typename _Tp>
1496 _GLIBCXX20_CONSTEXPR complex&
1497 operator/=(const complex<_Tp>& __z)
1499 const _ComplexT __t = __z.__rep();
1504 _GLIBCXX_CONSTEXPR _ComplexT __rep() const { return _M_value; }
1510 // These bits have to be at the end of this file, so that the
1511 // specializations have all been defined.
1512 inline _GLIBCXX_CONSTEXPR
1513 complex<float>::complex(const complex<double>& __z)
1514 : _M_value(__z.__rep()) { }
1516 inline _GLIBCXX_CONSTEXPR
1517 complex<float>::complex(const complex<long double>& __z)
1518 : _M_value(__z.__rep()) { }
1520 inline _GLIBCXX_CONSTEXPR
1521 complex<double>::complex(const complex<long double>& __z)
1522 : _M_value(__z.__rep()) { }
1524 // Inhibit implicit instantiations for required instantiations,
1525 // which are defined via explicit instantiations elsewhere.
1526 // NB: This syntax is a GNU extension.
1527 #if _GLIBCXX_EXTERN_TEMPLATE
1528 extern template istream& operator>>(istream&, complex<float>&);
1529 extern template ostream& operator<<(ostream&, const complex<float>&);
1530 extern template istream& operator>>(istream&, complex<double>&);
1531 extern template ostream& operator<<(ostream&, const complex<double>&);
1532 extern template istream& operator>>(istream&, complex<long double>&);
1533 extern template ostream& operator<<(ostream&, const complex<long double>&);
1535 #ifdef _GLIBCXX_USE_WCHAR_T
1536 extern template wistream& operator>>(wistream&, complex<float>&);
1537 extern template wostream& operator<<(wostream&, const complex<float>&);
1538 extern template wistream& operator>>(wistream&, complex<double>&);
1539 extern template wostream& operator<<(wostream&, const complex<double>&);
1540 extern template wistream& operator>>(wistream&, complex<long double>&);
1541 extern template wostream& operator<<(wostream&, const complex<long double>&);
1545 // @} group complex_numbers
1547 _GLIBCXX_END_NAMESPACE_VERSION
1550 namespace __gnu_cxx _GLIBCXX_VISIBILITY(default)
1552 _GLIBCXX_BEGIN_NAMESPACE_VERSION
1554 // See ext/type_traits.h for the primary template.
1555 template<typename _Tp, typename _Up>
1556 struct __promote_2<std::complex<_Tp>, _Up>
1559 typedef std::complex<typename __promote_2<_Tp, _Up>::__type> __type;
1562 template<typename _Tp, typename _Up>
1563 struct __promote_2<_Tp, std::complex<_Up> >
1566 typedef std::complex<typename __promote_2<_Tp, _Up>::__type> __type;
1569 template<typename _Tp, typename _Up>
1570 struct __promote_2<std::complex<_Tp>, std::complex<_Up> >
1573 typedef std::complex<typename __promote_2<_Tp, _Up>::__type> __type;
1576 _GLIBCXX_END_NAMESPACE_VERSION
1579 #if __cplusplus >= 201103L
1581 namespace std _GLIBCXX_VISIBILITY(default)
1583 _GLIBCXX_BEGIN_NAMESPACE_VERSION
1585 // Forward declarations.
1586 template<typename _Tp> std::complex<_Tp> acos(const std::complex<_Tp>&);
1587 template<typename _Tp> std::complex<_Tp> asin(const std::complex<_Tp>&);
1588 template<typename _Tp> std::complex<_Tp> atan(const std::complex<_Tp>&);
1590 template<typename _Tp> std::complex<_Tp> acosh(const std::complex<_Tp>&);
1591 template<typename _Tp> std::complex<_Tp> asinh(const std::complex<_Tp>&);
1592 template<typename _Tp> std::complex<_Tp> atanh(const std::complex<_Tp>&);
1594 template<typename _Tp> _Tp fabs(const std::complex<_Tp>&);
1596 template<typename _Tp>
1597 inline std::complex<_Tp>
1598 __complex_acos(const std::complex<_Tp>& __z)
1600 const std::complex<_Tp> __t = std::asin(__z);
1601 const _Tp __pi_2 = 1.5707963267948966192313216916397514L;
1602 return std::complex<_Tp>(__pi_2 - __t.real(), -__t.imag());
1605 #if _GLIBCXX_USE_C99_COMPLEX_TR1
1606 inline __complex__ float
1607 __complex_acos(__complex__ float __z)
1608 { return __builtin_cacosf(__z); }
1610 inline __complex__ double
1611 __complex_acos(__complex__ double __z)
1612 { return __builtin_cacos(__z); }
1614 inline __complex__ long double
1615 __complex_acos(const __complex__ long double& __z)
1616 { return __builtin_cacosl(__z); }
1618 template<typename _Tp>
1619 inline std::complex<_Tp>
1620 acos(const std::complex<_Tp>& __z)
1621 { return __complex_acos(__z.__rep()); }
1623 /// acos(__z) [8.1.2].
1624 // Effects: Behaves the same as C99 function cacos, defined
1625 // in subclause 7.3.5.1.
1626 template<typename _Tp>
1627 inline std::complex<_Tp>
1628 acos(const std::complex<_Tp>& __z)
1629 { return __complex_acos(__z); }
1632 template<typename _Tp>
1633 inline std::complex<_Tp>
1634 __complex_asin(const std::complex<_Tp>& __z)
1636 std::complex<_Tp> __t(-__z.imag(), __z.real());
1637 __t = std::asinh(__t);
1638 return std::complex<_Tp>(__t.imag(), -__t.real());
1641 #if _GLIBCXX_USE_C99_COMPLEX_TR1
1642 inline __complex__ float
1643 __complex_asin(__complex__ float __z)
1644 { return __builtin_casinf(__z); }
1646 inline __complex__ double
1647 __complex_asin(__complex__ double __z)
1648 { return __builtin_casin(__z); }
1650 inline __complex__ long double
1651 __complex_asin(const __complex__ long double& __z)
1652 { return __builtin_casinl(__z); }
1654 template<typename _Tp>
1655 inline std::complex<_Tp>
1656 asin(const std::complex<_Tp>& __z)
1657 { return __complex_asin(__z.__rep()); }
1659 /// asin(__z) [8.1.3].
1660 // Effects: Behaves the same as C99 function casin, defined
1661 // in subclause 7.3.5.2.
1662 template<typename _Tp>
1663 inline std::complex<_Tp>
1664 asin(const std::complex<_Tp>& __z)
1665 { return __complex_asin(__z); }
1668 template<typename _Tp>
1670 __complex_atan(const std::complex<_Tp>& __z)
1672 const _Tp __r2 = __z.real() * __z.real();
1673 const _Tp __x = _Tp(1.0) - __r2 - __z.imag() * __z.imag();
1675 _Tp __num = __z.imag() + _Tp(1.0);
1676 _Tp __den = __z.imag() - _Tp(1.0);
1678 __num = __r2 + __num * __num;
1679 __den = __r2 + __den * __den;
1681 return std::complex<_Tp>(_Tp(0.5) * atan2(_Tp(2.0) * __z.real(), __x),
1682 _Tp(0.25) * log(__num / __den));
1685 #if _GLIBCXX_USE_C99_COMPLEX_TR1
1686 inline __complex__ float
1687 __complex_atan(__complex__ float __z)
1688 { return __builtin_catanf(__z); }
1690 inline __complex__ double
1691 __complex_atan(__complex__ double __z)
1692 { return __builtin_catan(__z); }
1694 inline __complex__ long double
1695 __complex_atan(const __complex__ long double& __z)
1696 { return __builtin_catanl(__z); }
1698 template<typename _Tp>
1699 inline std::complex<_Tp>
1700 atan(const std::complex<_Tp>& __z)
1701 { return __complex_atan(__z.__rep()); }
1703 /// atan(__z) [8.1.4].
1704 // Effects: Behaves the same as C99 function catan, defined
1705 // in subclause 7.3.5.3.
1706 template<typename _Tp>
1707 inline std::complex<_Tp>
1708 atan(const std::complex<_Tp>& __z)
1709 { return __complex_atan(__z); }
1712 template<typename _Tp>
1714 __complex_acosh(const std::complex<_Tp>& __z)
1717 return _Tp(2.0) * std::log(std::sqrt(_Tp(0.5) * (__z + _Tp(1.0)))
1718 + std::sqrt(_Tp(0.5) * (__z - _Tp(1.0))));
1721 #if _GLIBCXX_USE_C99_COMPLEX_TR1
1722 inline __complex__ float
1723 __complex_acosh(__complex__ float __z)
1724 { return __builtin_cacoshf(__z); }
1726 inline __complex__ double
1727 __complex_acosh(__complex__ double __z)
1728 { return __builtin_cacosh(__z); }
1730 inline __complex__ long double
1731 __complex_acosh(const __complex__ long double& __z)
1732 { return __builtin_cacoshl(__z); }
1734 template<typename _Tp>
1735 inline std::complex<_Tp>
1736 acosh(const std::complex<_Tp>& __z)
1737 { return __complex_acosh(__z.__rep()); }
1739 /// acosh(__z) [8.1.5].
1740 // Effects: Behaves the same as C99 function cacosh, defined
1741 // in subclause 7.3.6.1.
1742 template<typename _Tp>
1743 inline std::complex<_Tp>
1744 acosh(const std::complex<_Tp>& __z)
1745 { return __complex_acosh(__z); }
1748 template<typename _Tp>
1750 __complex_asinh(const std::complex<_Tp>& __z)
1752 std::complex<_Tp> __t((__z.real() - __z.imag())
1753 * (__z.real() + __z.imag()) + _Tp(1.0),
1754 _Tp(2.0) * __z.real() * __z.imag());
1755 __t = std::sqrt(__t);
1757 return std::log(__t + __z);
1760 #if _GLIBCXX_USE_C99_COMPLEX_TR1
1761 inline __complex__ float
1762 __complex_asinh(__complex__ float __z)
1763 { return __builtin_casinhf(__z); }
1765 inline __complex__ double
1766 __complex_asinh(__complex__ double __z)
1767 { return __builtin_casinh(__z); }
1769 inline __complex__ long double
1770 __complex_asinh(const __complex__ long double& __z)
1771 { return __builtin_casinhl(__z); }
1773 template<typename _Tp>
1774 inline std::complex<_Tp>
1775 asinh(const std::complex<_Tp>& __z)
1776 { return __complex_asinh(__z.__rep()); }
1778 /// asinh(__z) [8.1.6].
1779 // Effects: Behaves the same as C99 function casin, defined
1780 // in subclause 7.3.6.2.
1781 template<typename _Tp>
1782 inline std::complex<_Tp>
1783 asinh(const std::complex<_Tp>& __z)
1784 { return __complex_asinh(__z); }
1787 template<typename _Tp>
1789 __complex_atanh(const std::complex<_Tp>& __z)
1791 const _Tp __i2 = __z.imag() * __z.imag();
1792 const _Tp __x = _Tp(1.0) - __i2 - __z.real() * __z.real();
1794 _Tp __num = _Tp(1.0) + __z.real();
1795 _Tp __den = _Tp(1.0) - __z.real();
1797 __num = __i2 + __num * __num;
1798 __den = __i2 + __den * __den;
1800 return std::complex<_Tp>(_Tp(0.25) * (log(__num) - log(__den)),
1801 _Tp(0.5) * atan2(_Tp(2.0) * __z.imag(), __x));
1804 #if _GLIBCXX_USE_C99_COMPLEX_TR1
1805 inline __complex__ float
1806 __complex_atanh(__complex__ float __z)
1807 { return __builtin_catanhf(__z); }
1809 inline __complex__ double
1810 __complex_atanh(__complex__ double __z)
1811 { return __builtin_catanh(__z); }
1813 inline __complex__ long double
1814 __complex_atanh(const __complex__ long double& __z)
1815 { return __builtin_catanhl(__z); }
1817 template<typename _Tp>
1818 inline std::complex<_Tp>
1819 atanh(const std::complex<_Tp>& __z)
1820 { return __complex_atanh(__z.__rep()); }
1822 /// atanh(__z) [8.1.7].
1823 // Effects: Behaves the same as C99 function catanh, defined
1824 // in subclause 7.3.6.3.
1825 template<typename _Tp>
1826 inline std::complex<_Tp>
1827 atanh(const std::complex<_Tp>& __z)
1828 { return __complex_atanh(__z); }
1831 template<typename _Tp>
1833 /// fabs(__z) [8.1.8].
1834 // Effects: Behaves the same as C99 function cabs, defined
1835 // in subclause 7.3.8.1.
1836 fabs(const std::complex<_Tp>& __z)
1837 { return std::abs(__z); }
1839 /// Additional overloads [8.1.9].
1840 template<typename _Tp>
1841 inline typename __gnu_cxx::__promote<_Tp>::__type
1844 typedef typename __gnu_cxx::__promote<_Tp>::__type __type;
1845 #if (_GLIBCXX11_USE_C99_MATH && !_GLIBCXX_USE_C99_FP_MACROS_DYNAMIC)
1846 return std::signbit(__x) ? __type(3.1415926535897932384626433832795029L)
1849 return std::arg(std::complex<__type>(__x));
1853 template<typename _Tp>
1854 _GLIBCXX_CONSTEXPR inline typename __gnu_cxx::__promote<_Tp>::__type
1858 template<typename _Tp>
1859 _GLIBCXX20_CONSTEXPR inline typename __gnu_cxx::__promote<_Tp>::__type
1862 typedef typename __gnu_cxx::__promote<_Tp>::__type __type;
1863 return __type(__x) * __type(__x);
1866 template<typename _Tp>
1867 _GLIBCXX_CONSTEXPR inline typename __gnu_cxx::__promote<_Tp>::__type
1871 template<typename _Tp, typename _Up>
1872 inline std::complex<typename __gnu_cxx::__promote_2<_Tp, _Up>::__type>
1873 pow(const std::complex<_Tp>& __x, const _Up& __y)
1875 typedef typename __gnu_cxx::__promote_2<_Tp, _Up>::__type __type;
1876 return std::pow(std::complex<__type>(__x), __type(__y));
1879 template<typename _Tp, typename _Up>
1880 inline std::complex<typename __gnu_cxx::__promote_2<_Tp, _Up>::__type>
1881 pow(const _Tp& __x, const std::complex<_Up>& __y)
1883 typedef typename __gnu_cxx::__promote_2<_Tp, _Up>::__type __type;
1884 return std::pow(__type(__x), std::complex<__type>(__y));
1887 template<typename _Tp, typename _Up>
1888 inline std::complex<typename __gnu_cxx::__promote_2<_Tp, _Up>::__type>
1889 pow(const std::complex<_Tp>& __x, const std::complex<_Up>& __y)
1891 typedef typename __gnu_cxx::__promote_2<_Tp, _Up>::__type __type;
1892 return std::pow(std::complex<__type>(__x),
1893 std::complex<__type>(__y));
1896 // Forward declarations.
1898 template<typename _Tp>
1899 std::complex<_Tp> proj(const std::complex<_Tp>&);
1901 // Generic implementation of std::proj, does not work for infinities.
1902 template<typename _Tp>
1903 inline std::complex<_Tp>
1904 __complex_proj(const std::complex<_Tp>& __z)
1907 #if _GLIBCXX_USE_C99_COMPLEX
1908 inline complex<float>
1909 __complex_proj(const complex<float>& __z)
1910 { return __builtin_cprojf(__z.__rep()); }
1912 inline complex<double>
1913 __complex_proj(const complex<double>& __z)
1914 { return __builtin_cproj(__z.__rep()); }
1916 inline complex<long double>
1917 __complex_proj(const complex<long double>& __z)
1918 { return __builtin_cprojl(__z.__rep()); }
1919 #elif defined _GLIBCXX_USE_C99_MATH_TR1
1920 inline complex<float>
1921 __complex_proj(const complex<float>& __z)
1923 if (__builtin_isinf(__z.real()) || __builtin_isinf(__z.imag()))
1924 return complex<float>(__builtin_inff(),
1925 __builtin_copysignf(0.0f, __z.imag()));
1929 inline complex<double>
1930 __complex_proj(const complex<double>& __z)
1932 if (__builtin_isinf(__z.real()) || __builtin_isinf(__z.imag()))
1933 return complex<double>(__builtin_inf(),
1934 __builtin_copysign(0.0, __z.imag()));
1938 inline complex<long double>
1939 __complex_proj(const complex<long double>& __z)
1941 if (__builtin_isinf(__z.real()) || __builtin_isinf(__z.imag()))
1942 return complex<long double>(__builtin_infl(),
1943 __builtin_copysignl(0.0l, __z.imag()));
1948 template<typename _Tp>
1949 inline std::complex<_Tp>
1950 proj(const std::complex<_Tp>& __z)
1951 { return __complex_proj(__z); }
1953 // Overload for scalars
1954 template<typename _Tp>
1955 inline std::complex<typename __gnu_cxx::__promote<_Tp>::__type>
1958 typedef typename __gnu_cxx::__promote<_Tp>::__type __type;
1959 return std::proj(std::complex<__type>(__x));
1962 template<typename _Tp>
1963 inline _GLIBCXX20_CONSTEXPR
1964 std::complex<typename __gnu_cxx::__promote<_Tp>::__type>
1967 typedef typename __gnu_cxx::__promote<_Tp>::__type __type;
1968 return std::complex<__type>(__x, -__type());
1971 #if __cplusplus > 201103L
1973 inline namespace literals {
1974 inline namespace complex_literals {
1975 #pragma GCC diagnostic push
1976 #pragma GCC diagnostic ignored "-Wliteral-suffix"
1977 #define __cpp_lib_complex_udls 201309
1979 constexpr std::complex<float>
1980 operator""if(long double __num)
1981 { return std::complex<float>{0.0F, static_cast<float>(__num)}; }
1983 constexpr std::complex<float>
1984 operator""if(unsigned long long __num)
1985 { return std::complex<float>{0.0F, static_cast<float>(__num)}; }
1987 constexpr std::complex<double>
1988 operator""i(long double __num)
1989 { return std::complex<double>{0.0, static_cast<double>(__num)}; }
1991 constexpr std::complex<double>
1992 operator""i(unsigned long long __num)
1993 { return std::complex<double>{0.0, static_cast<double>(__num)}; }
1995 constexpr std::complex<long double>
1996 operator""il(long double __num)
1997 { return std::complex<long double>{0.0L, __num}; }
1999 constexpr std::complex<long double>
2000 operator""il(unsigned long long __num)
2001 { return std::complex<long double>{0.0L, static_cast<long double>(__num)}; }
2003 #pragma GCC diagnostic pop
2004 } // inline namespace complex_literals
2005 } // inline namespace literals
2009 _GLIBCXX_END_NAMESPACE_VERSION
2014 #endif /* _GLIBCXX_COMPLEX */