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.
32 // Initially implemented by Ulrich Drepper <drepper@cygnus.com>
33 // Improved by Gabriel Dos Reis <dosreis@cmla.ens-cachan.fr>
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 template<typename _Tp>
472 inline _GLIBCXX_CONSTEXPR bool
473 operator==(const _Tp& __x, const complex<_Tp>& __y)
474 { return __x == __y.real() && _Tp() == __y.imag(); }
478 /// Return false if @a x is equal to @a y.
479 template<typename _Tp>
480 inline _GLIBCXX_CONSTEXPR bool
481 operator!=(const complex<_Tp>& __x, const complex<_Tp>& __y)
482 { return __x.real() != __y.real() || __x.imag() != __y.imag(); }
484 template<typename _Tp>
485 inline _GLIBCXX_CONSTEXPR bool
486 operator!=(const complex<_Tp>& __x, const _Tp& __y)
487 { return __x.real() != __y || __x.imag() != _Tp(); }
489 template<typename _Tp>
490 inline _GLIBCXX_CONSTEXPR bool
491 operator!=(const _Tp& __x, const complex<_Tp>& __y)
492 { return __x != __y.real() || _Tp() != __y.imag(); }
495 /// Extraction operator for complex values.
496 template<typename _Tp, typename _CharT, class _Traits>
497 basic_istream<_CharT, _Traits>&
498 operator>>(basic_istream<_CharT, _Traits>& __is, complex<_Tp>& __x)
504 if (_Traits::eq(__ch, __is.widen('(')))
507 if (__is >> __u >> __ch)
509 const _CharT __rparen = __is.widen(')');
510 if (_Traits::eq(__ch, __rparen))
515 else if (_Traits::eq(__ch, __is.widen(',')))
518 if (__is >> __v >> __ch)
520 if (_Traits::eq(__ch, __rparen))
522 __x = complex<_Tp>(__u, __v);
545 __is.setstate(ios_base::failbit);
549 /// Insertion operator for complex values.
550 template<typename _Tp, typename _CharT, class _Traits>
551 basic_ostream<_CharT, _Traits>&
552 operator<<(basic_ostream<_CharT, _Traits>& __os, const complex<_Tp>& __x)
554 basic_ostringstream<_CharT, _Traits> __s;
555 __s.flags(__os.flags());
556 __s.imbue(__os.getloc());
557 __s.precision(__os.precision());
558 __s << '(' << __x.real() << ',' << __x.imag() << ')';
559 return __os << __s.str();
563 #if __cplusplus >= 201103L
564 template<typename _Tp>
566 real(const complex<_Tp>& __z)
567 { return __z.real(); }
569 template<typename _Tp>
571 imag(const complex<_Tp>& __z)
572 { return __z.imag(); }
574 template<typename _Tp>
576 real(complex<_Tp>& __z)
577 { return __z.real(); }
579 template<typename _Tp>
581 real(const complex<_Tp>& __z)
582 { return __z.real(); }
584 template<typename _Tp>
586 imag(complex<_Tp>& __z)
587 { return __z.imag(); }
589 template<typename _Tp>
591 imag(const complex<_Tp>& __z)
592 { return __z.imag(); }
595 // 26.2.7/3 abs(__z): Returns the magnitude of __z.
596 template<typename _Tp>
598 __complex_abs(const complex<_Tp>& __z)
600 _Tp __x = __z.real();
601 _Tp __y = __z.imag();
602 const _Tp __s = std::max(abs(__x), abs(__y));
603 if (__s == _Tp()) // well ...
607 return __s * sqrt(__x * __x + __y * __y);
610 #if _GLIBCXX_USE_C99_COMPLEX
612 __complex_abs(__complex__ float __z) { return __builtin_cabsf(__z); }
615 __complex_abs(__complex__ double __z) { return __builtin_cabs(__z); }
618 __complex_abs(const __complex__ long double& __z)
619 { return __builtin_cabsl(__z); }
621 template<typename _Tp>
623 abs(const complex<_Tp>& __z) { return __complex_abs(__z.__rep()); }
625 template<typename _Tp>
627 abs(const complex<_Tp>& __z) { return __complex_abs(__z); }
631 // 26.2.7/4: arg(__z): Returns the phase angle of __z.
632 template<typename _Tp>
634 __complex_arg(const complex<_Tp>& __z)
635 { return atan2(__z.imag(), __z.real()); }
637 #if _GLIBCXX_USE_C99_COMPLEX
639 __complex_arg(__complex__ float __z) { return __builtin_cargf(__z); }
642 __complex_arg(__complex__ double __z) { return __builtin_carg(__z); }
645 __complex_arg(const __complex__ long double& __z)
646 { return __builtin_cargl(__z); }
648 template<typename _Tp>
650 arg(const complex<_Tp>& __z) { return __complex_arg(__z.__rep()); }
652 template<typename _Tp>
654 arg(const complex<_Tp>& __z) { return __complex_arg(__z); }
657 // 26.2.7/5: norm(__z) returns the squared magnitude of __z.
658 // As defined, norm() is -not- a norm is the common mathematical
659 // sense used in numerics. The helper class _Norm_helper<> tries to
660 // distinguish between builtin floating point and the rest, so as
661 // to deliver an answer as close as possible to the real value.
665 template<typename _Tp>
666 static inline _GLIBCXX20_CONSTEXPR _Tp _S_do_it(const complex<_Tp>& __z)
668 const _Tp __x = __z.real();
669 const _Tp __y = __z.imag();
670 return __x * __x + __y * __y;
675 struct _Norm_helper<true>
677 template<typename _Tp>
678 static inline _GLIBCXX20_CONSTEXPR _Tp _S_do_it(const complex<_Tp>& __z)
680 //_Tp __res = std::abs(__z);
681 //return __res * __res;
682 const _Tp __x = __z.real();
683 const _Tp __y = __z.imag();
684 return __x * __x + __y * __y;
688 template<typename _Tp>
689 inline _GLIBCXX20_CONSTEXPR _Tp
690 norm(const complex<_Tp>& __z)
692 return _Norm_helper<__is_floating<_Tp>::__value
693 && !_GLIBCXX_FAST_MATH>::_S_do_it(__z);
696 template<typename _Tp>
698 polar(const _Tp& __rho, const _Tp& __theta)
700 __glibcxx_assert( __rho >= 0 );
701 return complex<_Tp>(__rho * cos(__theta), __rho * sin(__theta));
704 template<typename _Tp>
705 inline _GLIBCXX20_CONSTEXPR complex<_Tp>
706 conj(const complex<_Tp>& __z)
707 { return complex<_Tp>(__z.real(), -__z.imag()); }
711 // 26.2.8/1 cos(__z): Returns the cosine of __z.
712 template<typename _Tp>
714 __complex_cos(const complex<_Tp>& __z)
716 const _Tp __x = __z.real();
717 const _Tp __y = __z.imag();
718 return complex<_Tp>(cos(__x) * cosh(__y), -sin(__x) * sinh(__y));
721 #if _GLIBCXX_USE_C99_COMPLEX
722 inline __complex__ float
723 __complex_cos(__complex__ float __z) { return __builtin_ccosf(__z); }
725 inline __complex__ double
726 __complex_cos(__complex__ double __z) { return __builtin_ccos(__z); }
728 inline __complex__ long double
729 __complex_cos(const __complex__ long double& __z)
730 { return __builtin_ccosl(__z); }
732 template<typename _Tp>
734 cos(const complex<_Tp>& __z) { return __complex_cos(__z.__rep()); }
736 template<typename _Tp>
738 cos(const complex<_Tp>& __z) { return __complex_cos(__z); }
741 // 26.2.8/2 cosh(__z): Returns the hyperbolic cosine of __z.
742 template<typename _Tp>
744 __complex_cosh(const complex<_Tp>& __z)
746 const _Tp __x = __z.real();
747 const _Tp __y = __z.imag();
748 return complex<_Tp>(cosh(__x) * cos(__y), sinh(__x) * sin(__y));
751 #if _GLIBCXX_USE_C99_COMPLEX
752 inline __complex__ float
753 __complex_cosh(__complex__ float __z) { return __builtin_ccoshf(__z); }
755 inline __complex__ double
756 __complex_cosh(__complex__ double __z) { return __builtin_ccosh(__z); }
758 inline __complex__ long double
759 __complex_cosh(const __complex__ long double& __z)
760 { return __builtin_ccoshl(__z); }
762 template<typename _Tp>
764 cosh(const complex<_Tp>& __z) { return __complex_cosh(__z.__rep()); }
766 template<typename _Tp>
768 cosh(const complex<_Tp>& __z) { return __complex_cosh(__z); }
771 // 26.2.8/3 exp(__z): Returns the complex base e exponential of x
772 template<typename _Tp>
774 __complex_exp(const complex<_Tp>& __z)
775 { return std::polar<_Tp>(exp(__z.real()), __z.imag()); }
777 #if _GLIBCXX_USE_C99_COMPLEX
778 inline __complex__ float
779 __complex_exp(__complex__ float __z) { return __builtin_cexpf(__z); }
781 inline __complex__ double
782 __complex_exp(__complex__ double __z) { return __builtin_cexp(__z); }
784 inline __complex__ long double
785 __complex_exp(const __complex__ long double& __z)
786 { return __builtin_cexpl(__z); }
788 template<typename _Tp>
790 exp(const complex<_Tp>& __z) { return __complex_exp(__z.__rep()); }
792 template<typename _Tp>
794 exp(const complex<_Tp>& __z) { return __complex_exp(__z); }
797 // 26.2.8/5 log(__z): Returns the natural complex logarithm of __z.
798 // The branch cut is along the negative axis.
799 template<typename _Tp>
801 __complex_log(const complex<_Tp>& __z)
802 { return complex<_Tp>(log(std::abs(__z)), std::arg(__z)); }
804 #if _GLIBCXX_USE_C99_COMPLEX
805 inline __complex__ float
806 __complex_log(__complex__ float __z) { return __builtin_clogf(__z); }
808 inline __complex__ double
809 __complex_log(__complex__ double __z) { return __builtin_clog(__z); }
811 inline __complex__ long double
812 __complex_log(const __complex__ long double& __z)
813 { return __builtin_clogl(__z); }
815 template<typename _Tp>
817 log(const complex<_Tp>& __z) { return __complex_log(__z.__rep()); }
819 template<typename _Tp>
821 log(const complex<_Tp>& __z) { return __complex_log(__z); }
824 template<typename _Tp>
826 log10(const complex<_Tp>& __z)
827 { return std::log(__z) / log(_Tp(10.0)); }
829 // 26.2.8/10 sin(__z): Returns the sine of __z.
830 template<typename _Tp>
832 __complex_sin(const complex<_Tp>& __z)
834 const _Tp __x = __z.real();
835 const _Tp __y = __z.imag();
836 return complex<_Tp>(sin(__x) * cosh(__y), cos(__x) * sinh(__y));
839 #if _GLIBCXX_USE_C99_COMPLEX
840 inline __complex__ float
841 __complex_sin(__complex__ float __z) { return __builtin_csinf(__z); }
843 inline __complex__ double
844 __complex_sin(__complex__ double __z) { return __builtin_csin(__z); }
846 inline __complex__ long double
847 __complex_sin(const __complex__ long double& __z)
848 { return __builtin_csinl(__z); }
850 template<typename _Tp>
852 sin(const complex<_Tp>& __z) { return __complex_sin(__z.__rep()); }
854 template<typename _Tp>
856 sin(const complex<_Tp>& __z) { return __complex_sin(__z); }
859 // 26.2.8/11 sinh(__z): Returns the hyperbolic sine of __z.
860 template<typename _Tp>
862 __complex_sinh(const complex<_Tp>& __z)
864 const _Tp __x = __z.real();
865 const _Tp __y = __z.imag();
866 return complex<_Tp>(sinh(__x) * cos(__y), cosh(__x) * sin(__y));
869 #if _GLIBCXX_USE_C99_COMPLEX
870 inline __complex__ float
871 __complex_sinh(__complex__ float __z) { return __builtin_csinhf(__z); }
873 inline __complex__ double
874 __complex_sinh(__complex__ double __z) { return __builtin_csinh(__z); }
876 inline __complex__ long double
877 __complex_sinh(const __complex__ long double& __z)
878 { return __builtin_csinhl(__z); }
880 template<typename _Tp>
882 sinh(const complex<_Tp>& __z) { return __complex_sinh(__z.__rep()); }
884 template<typename _Tp>
886 sinh(const complex<_Tp>& __z) { return __complex_sinh(__z); }
889 // 26.2.8/13 sqrt(__z): Returns the complex square root of __z.
890 // The branch cut is on the negative axis.
891 template<typename _Tp>
893 __complex_sqrt(const complex<_Tp>& __z)
895 _Tp __x = __z.real();
896 _Tp __y = __z.imag();
900 _Tp __t = sqrt(abs(__y) / 2);
901 return complex<_Tp>(__t, __y < _Tp() ? -__t : __t);
905 _Tp __t = sqrt(2 * (std::abs(__z) + abs(__x)));
908 ? complex<_Tp>(__u, __y / __t)
909 : complex<_Tp>(abs(__y) / __t, __y < _Tp() ? -__u : __u);
913 #if _GLIBCXX_USE_C99_COMPLEX
914 inline __complex__ float
915 __complex_sqrt(__complex__ float __z) { return __builtin_csqrtf(__z); }
917 inline __complex__ double
918 __complex_sqrt(__complex__ double __z) { return __builtin_csqrt(__z); }
920 inline __complex__ long double
921 __complex_sqrt(const __complex__ long double& __z)
922 { return __builtin_csqrtl(__z); }
924 template<typename _Tp>
926 sqrt(const complex<_Tp>& __z) { return __complex_sqrt(__z.__rep()); }
928 template<typename _Tp>
930 sqrt(const complex<_Tp>& __z) { return __complex_sqrt(__z); }
933 // 26.2.8/14 tan(__z): Return the complex tangent of __z.
935 template<typename _Tp>
937 __complex_tan(const complex<_Tp>& __z)
938 { return std::sin(__z) / std::cos(__z); }
940 #if _GLIBCXX_USE_C99_COMPLEX
941 inline __complex__ float
942 __complex_tan(__complex__ float __z) { return __builtin_ctanf(__z); }
944 inline __complex__ double
945 __complex_tan(__complex__ double __z) { return __builtin_ctan(__z); }
947 inline __complex__ long double
948 __complex_tan(const __complex__ long double& __z)
949 { return __builtin_ctanl(__z); }
951 template<typename _Tp>
953 tan(const complex<_Tp>& __z) { return __complex_tan(__z.__rep()); }
955 template<typename _Tp>
957 tan(const complex<_Tp>& __z) { return __complex_tan(__z); }
961 // 26.2.8/15 tanh(__z): Returns the hyperbolic tangent of __z.
963 template<typename _Tp>
965 __complex_tanh(const complex<_Tp>& __z)
966 { return std::sinh(__z) / std::cosh(__z); }
968 #if _GLIBCXX_USE_C99_COMPLEX
969 inline __complex__ float
970 __complex_tanh(__complex__ float __z) { return __builtin_ctanhf(__z); }
972 inline __complex__ double
973 __complex_tanh(__complex__ double __z) { return __builtin_ctanh(__z); }
975 inline __complex__ long double
976 __complex_tanh(const __complex__ long double& __z)
977 { return __builtin_ctanhl(__z); }
979 template<typename _Tp>
981 tanh(const complex<_Tp>& __z) { return __complex_tanh(__z.__rep()); }
983 template<typename _Tp>
985 tanh(const complex<_Tp>& __z) { return __complex_tanh(__z); }
989 // 26.2.8/9 pow(__x, __y): Returns the complex power base of __x
990 // raised to the __y-th power. The branch
991 // cut is on the negative axis.
992 template<typename _Tp>
994 __complex_pow_unsigned(complex<_Tp> __x, unsigned __n)
996 complex<_Tp> __y = __n % 2 ? __x : complex<_Tp>(1);
1008 // In C++11 mode we used to implement the resolution of
1009 // DR 844. complex pow return type is ambiguous.
1010 // thus the following overload was disabled in that mode. However, doing
1011 // that causes all sorts of issues, see, for example:
1012 // http://gcc.gnu.org/ml/libstdc++/2013-01/msg00058.html
1013 // and also PR57974.
1014 template<typename _Tp>
1016 pow(const complex<_Tp>& __z, int __n)
1019 ? complex<_Tp>(1) / std::__complex_pow_unsigned(__z, -(unsigned)__n)
1020 : std::__complex_pow_unsigned(__z, __n);
1023 template<typename _Tp>
1025 pow(const complex<_Tp>& __x, const _Tp& __y)
1027 #if ! _GLIBCXX_USE_C99_COMPLEX
1031 if (__x.imag() == _Tp() && __x.real() > _Tp())
1032 return pow(__x.real(), __y);
1034 complex<_Tp> __t = std::log(__x);
1035 return std::polar<_Tp>(exp(__y * __t.real()), __y * __t.imag());
1038 template<typename _Tp>
1040 __complex_pow(const complex<_Tp>& __x, const complex<_Tp>& __y)
1041 { return __x == _Tp() ? _Tp() : std::exp(__y * std::log(__x)); }
1043 #if _GLIBCXX_USE_C99_COMPLEX
1044 inline __complex__ float
1045 __complex_pow(__complex__ float __x, __complex__ float __y)
1046 { return __builtin_cpowf(__x, __y); }
1048 inline __complex__ double
1049 __complex_pow(__complex__ double __x, __complex__ double __y)
1050 { return __builtin_cpow(__x, __y); }
1052 inline __complex__ long double
1053 __complex_pow(const __complex__ long double& __x,
1054 const __complex__ long double& __y)
1055 { return __builtin_cpowl(__x, __y); }
1057 template<typename _Tp>
1059 pow(const complex<_Tp>& __x, const complex<_Tp>& __y)
1060 { return __complex_pow(__x.__rep(), __y.__rep()); }
1062 template<typename _Tp>
1064 pow(const complex<_Tp>& __x, const complex<_Tp>& __y)
1065 { return __complex_pow(__x, __y); }
1068 template<typename _Tp>
1070 pow(const _Tp& __x, const complex<_Tp>& __y)
1072 return __x > _Tp() ? std::polar<_Tp>(pow(__x, __y.real()),
1073 __y.imag() * log(__x))
1074 : std::pow(complex<_Tp>(__x), __y);
1077 /// 26.2.3 complex specializations
1078 /// complex<float> specialization
1080 struct complex<float>
1082 typedef float value_type;
1083 typedef __complex__ float _ComplexT;
1085 _GLIBCXX_CONSTEXPR complex(_ComplexT __z) : _M_value(__z) { }
1087 _GLIBCXX_CONSTEXPR complex(float __r = 0.0f, float __i = 0.0f)
1088 #if __cplusplus >= 201103L
1089 : _M_value{ __r, __i } { }
1092 __real__ _M_value = __r;
1093 __imag__ _M_value = __i;
1097 explicit _GLIBCXX_CONSTEXPR complex(const complex<double>&);
1098 explicit _GLIBCXX_CONSTEXPR complex(const complex<long double>&);
1100 #if __cplusplus >= 201103L
1101 // _GLIBCXX_RESOLVE_LIB_DEFECTS
1102 // DR 387. std::complex over-encapsulated.
1103 __attribute ((__abi_tag__ ("cxx11")))
1105 real() const { return __real__ _M_value; }
1107 __attribute ((__abi_tag__ ("cxx11")))
1109 imag() const { return __imag__ _M_value; }
1112 real() { return __real__ _M_value; }
1115 real() const { return __real__ _M_value; }
1118 imag() { return __imag__ _M_value; }
1121 imag() const { return __imag__ _M_value; }
1124 // _GLIBCXX_RESOLVE_LIB_DEFECTS
1125 // DR 387. std::complex over-encapsulated.
1126 _GLIBCXX20_CONSTEXPR void
1127 real(float __val) { __real__ _M_value = __val; }
1129 _GLIBCXX20_CONSTEXPR void
1130 imag(float __val) { __imag__ _M_value = __val; }
1132 _GLIBCXX20_CONSTEXPR complex&
1133 operator=(float __f)
1139 _GLIBCXX20_CONSTEXPR complex&
1140 operator+=(float __f)
1146 _GLIBCXX20_CONSTEXPR complex&
1147 operator-=(float __f)
1153 _GLIBCXX20_CONSTEXPR complex&
1154 operator*=(float __f)
1160 _GLIBCXX20_CONSTEXPR complex&
1161 operator/=(float __f)
1167 // Let the compiler synthesize the copy and assignment
1168 // operator. It always does a pretty good job.
1169 #if __cplusplus >= 201103L
1170 _GLIBCXX14_CONSTEXPR complex& operator=(const complex&) = default;
1173 template<typename _Tp>
1174 _GLIBCXX20_CONSTEXPR complex&
1175 operator=(const complex<_Tp>& __z)
1177 __real__ _M_value = __z.real();
1178 __imag__ _M_value = __z.imag();
1182 template<typename _Tp>
1183 _GLIBCXX20_CONSTEXPR complex&
1184 operator+=(const complex<_Tp>& __z)
1186 _M_value += __z.__rep();
1191 _GLIBCXX20_CONSTEXPR complex&
1192 operator-=(const complex<_Tp>& __z)
1194 _M_value -= __z.__rep();
1199 _GLIBCXX20_CONSTEXPR complex&
1200 operator*=(const complex<_Tp>& __z)
1202 const _ComplexT __t = __z.__rep();
1208 _GLIBCXX20_CONSTEXPR complex&
1209 operator/=(const complex<_Tp>& __z)
1211 const _ComplexT __t = __z.__rep();
1216 _GLIBCXX_CONSTEXPR _ComplexT __rep() const { return _M_value; }
1222 /// 26.2.3 complex specializations
1223 /// complex<double> specialization
1225 struct complex<double>
1227 typedef double value_type;
1228 typedef __complex__ double _ComplexT;
1230 _GLIBCXX_CONSTEXPR complex(_ComplexT __z) : _M_value(__z) { }
1232 _GLIBCXX_CONSTEXPR complex(double __r = 0.0, double __i = 0.0)
1233 #if __cplusplus >= 201103L
1234 : _M_value{ __r, __i } { }
1237 __real__ _M_value = __r;
1238 __imag__ _M_value = __i;
1242 _GLIBCXX_CONSTEXPR complex(const complex<float>& __z)
1243 : _M_value(__z.__rep()) { }
1245 explicit _GLIBCXX_CONSTEXPR complex(const complex<long double>&);
1247 #if __cplusplus >= 201103L
1248 // _GLIBCXX_RESOLVE_LIB_DEFECTS
1249 // DR 387. std::complex over-encapsulated.
1250 __attribute ((__abi_tag__ ("cxx11")))
1252 real() const { return __real__ _M_value; }
1254 __attribute ((__abi_tag__ ("cxx11")))
1256 imag() const { return __imag__ _M_value; }
1259 real() { return __real__ _M_value; }
1262 real() const { return __real__ _M_value; }
1265 imag() { return __imag__ _M_value; }
1268 imag() const { return __imag__ _M_value; }
1271 // _GLIBCXX_RESOLVE_LIB_DEFECTS
1272 // DR 387. std::complex over-encapsulated.
1273 _GLIBCXX20_CONSTEXPR void
1274 real(double __val) { __real__ _M_value = __val; }
1276 _GLIBCXX20_CONSTEXPR void
1277 imag(double __val) { __imag__ _M_value = __val; }
1279 _GLIBCXX20_CONSTEXPR complex&
1280 operator=(double __d)
1286 _GLIBCXX20_CONSTEXPR complex&
1287 operator+=(double __d)
1293 _GLIBCXX20_CONSTEXPR complex&
1294 operator-=(double __d)
1300 _GLIBCXX20_CONSTEXPR complex&
1301 operator*=(double __d)
1307 _GLIBCXX20_CONSTEXPR complex&
1308 operator/=(double __d)
1314 // The compiler will synthesize this, efficiently.
1315 #if __cplusplus >= 201103L
1316 _GLIBCXX14_CONSTEXPR complex& operator=(const complex&) = default;
1319 template<typename _Tp>
1320 _GLIBCXX20_CONSTEXPR complex&
1321 operator=(const complex<_Tp>& __z)
1323 _M_value = __z.__rep();
1327 template<typename _Tp>
1328 _GLIBCXX20_CONSTEXPR complex&
1329 operator+=(const complex<_Tp>& __z)
1331 _M_value += __z.__rep();
1335 template<typename _Tp>
1336 _GLIBCXX20_CONSTEXPR complex&
1337 operator-=(const complex<_Tp>& __z)
1339 _M_value -= __z.__rep();
1343 template<typename _Tp>
1344 _GLIBCXX20_CONSTEXPR complex&
1345 operator*=(const complex<_Tp>& __z)
1347 const _ComplexT __t = __z.__rep();
1352 template<typename _Tp>
1353 _GLIBCXX20_CONSTEXPR complex&
1354 operator/=(const complex<_Tp>& __z)
1356 const _ComplexT __t = __z.__rep();
1361 _GLIBCXX_CONSTEXPR _ComplexT __rep() const { return _M_value; }
1367 /// 26.2.3 complex specializations
1368 /// complex<long double> specialization
1370 struct complex<long double>
1372 typedef long double value_type;
1373 typedef __complex__ long double _ComplexT;
1375 _GLIBCXX_CONSTEXPR complex(_ComplexT __z) : _M_value(__z) { }
1377 _GLIBCXX_CONSTEXPR complex(long double __r = 0.0L,
1378 long double __i = 0.0L)
1379 #if __cplusplus >= 201103L
1380 : _M_value{ __r, __i } { }
1383 __real__ _M_value = __r;
1384 __imag__ _M_value = __i;
1388 _GLIBCXX_CONSTEXPR complex(const complex<float>& __z)
1389 : _M_value(__z.__rep()) { }
1391 _GLIBCXX_CONSTEXPR complex(const complex<double>& __z)
1392 : _M_value(__z.__rep()) { }
1394 #if __cplusplus >= 201103L
1395 // _GLIBCXX_RESOLVE_LIB_DEFECTS
1396 // DR 387. std::complex over-encapsulated.
1397 __attribute ((__abi_tag__ ("cxx11")))
1398 constexpr long double
1399 real() const { return __real__ _M_value; }
1401 __attribute ((__abi_tag__ ("cxx11")))
1402 constexpr long double
1403 imag() const { return __imag__ _M_value; }
1406 real() { return __real__ _M_value; }
1409 real() const { return __real__ _M_value; }
1412 imag() { return __imag__ _M_value; }
1415 imag() const { return __imag__ _M_value; }
1418 // _GLIBCXX_RESOLVE_LIB_DEFECTS
1419 // DR 387. std::complex over-encapsulated.
1420 _GLIBCXX20_CONSTEXPR void
1421 real(long double __val) { __real__ _M_value = __val; }
1423 _GLIBCXX20_CONSTEXPR void
1424 imag(long double __val) { __imag__ _M_value = __val; }
1426 _GLIBCXX20_CONSTEXPR complex&
1427 operator=(long double __r)
1433 _GLIBCXX20_CONSTEXPR complex&
1434 operator+=(long double __r)
1440 _GLIBCXX20_CONSTEXPR complex&
1441 operator-=(long double __r)
1447 _GLIBCXX20_CONSTEXPR complex&
1448 operator*=(long double __r)
1454 _GLIBCXX20_CONSTEXPR complex&
1455 operator/=(long double __r)
1461 // The compiler knows how to do this efficiently
1462 #if __cplusplus >= 201103L
1463 _GLIBCXX14_CONSTEXPR complex& operator=(const complex&) = default;
1466 template<typename _Tp>
1467 _GLIBCXX20_CONSTEXPR complex&
1468 operator=(const complex<_Tp>& __z)
1470 _M_value = __z.__rep();
1474 template<typename _Tp>
1475 _GLIBCXX20_CONSTEXPR complex&
1476 operator+=(const complex<_Tp>& __z)
1478 _M_value += __z.__rep();
1482 template<typename _Tp>
1483 _GLIBCXX20_CONSTEXPR complex&
1484 operator-=(const complex<_Tp>& __z)
1486 _M_value -= __z.__rep();
1490 template<typename _Tp>
1491 _GLIBCXX20_CONSTEXPR complex&
1492 operator*=(const complex<_Tp>& __z)
1494 const _ComplexT __t = __z.__rep();
1499 template<typename _Tp>
1500 _GLIBCXX20_CONSTEXPR complex&
1501 operator/=(const complex<_Tp>& __z)
1503 const _ComplexT __t = __z.__rep();
1508 _GLIBCXX_CONSTEXPR _ComplexT __rep() const { return _M_value; }
1514 // These bits have to be at the end of this file, so that the
1515 // specializations have all been defined.
1516 inline _GLIBCXX_CONSTEXPR
1517 complex<float>::complex(const complex<double>& __z)
1518 : _M_value(__z.__rep()) { }
1520 inline _GLIBCXX_CONSTEXPR
1521 complex<float>::complex(const complex<long double>& __z)
1522 : _M_value(__z.__rep()) { }
1524 inline _GLIBCXX_CONSTEXPR
1525 complex<double>::complex(const complex<long double>& __z)
1526 : _M_value(__z.__rep()) { }
1528 // Inhibit implicit instantiations for required instantiations,
1529 // which are defined via explicit instantiations elsewhere.
1530 // NB: This syntax is a GNU extension.
1531 #if _GLIBCXX_EXTERN_TEMPLATE
1532 extern template istream& operator>>(istream&, complex<float>&);
1533 extern template ostream& operator<<(ostream&, const complex<float>&);
1534 extern template istream& operator>>(istream&, complex<double>&);
1535 extern template ostream& operator<<(ostream&, const complex<double>&);
1536 extern template istream& operator>>(istream&, complex<long double>&);
1537 extern template ostream& operator<<(ostream&, const complex<long double>&);
1539 #ifdef _GLIBCXX_USE_WCHAR_T
1540 extern template wistream& operator>>(wistream&, complex<float>&);
1541 extern template wostream& operator<<(wostream&, const complex<float>&);
1542 extern template wistream& operator>>(wistream&, complex<double>&);
1543 extern template wostream& operator<<(wostream&, const complex<double>&);
1544 extern template wistream& operator>>(wistream&, complex<long double>&);
1545 extern template wostream& operator<<(wostream&, const complex<long double>&);
1549 // @} group complex_numbers
1551 _GLIBCXX_END_NAMESPACE_VERSION
1554 namespace __gnu_cxx _GLIBCXX_VISIBILITY(default)
1556 _GLIBCXX_BEGIN_NAMESPACE_VERSION
1558 // See ext/type_traits.h for the primary template.
1559 template<typename _Tp, typename _Up>
1560 struct __promote_2<std::complex<_Tp>, _Up>
1563 typedef std::complex<typename __promote_2<_Tp, _Up>::__type> __type;
1566 template<typename _Tp, typename _Up>
1567 struct __promote_2<_Tp, std::complex<_Up> >
1570 typedef std::complex<typename __promote_2<_Tp, _Up>::__type> __type;
1573 template<typename _Tp, typename _Up>
1574 struct __promote_2<std::complex<_Tp>, std::complex<_Up> >
1577 typedef std::complex<typename __promote_2<_Tp, _Up>::__type> __type;
1580 _GLIBCXX_END_NAMESPACE_VERSION
1583 #if __cplusplus >= 201103L
1585 namespace std _GLIBCXX_VISIBILITY(default)
1587 _GLIBCXX_BEGIN_NAMESPACE_VERSION
1589 // Forward declarations.
1590 template<typename _Tp> std::complex<_Tp> acos(const std::complex<_Tp>&);
1591 template<typename _Tp> std::complex<_Tp> asin(const std::complex<_Tp>&);
1592 template<typename _Tp> std::complex<_Tp> atan(const std::complex<_Tp>&);
1594 template<typename _Tp> std::complex<_Tp> acosh(const std::complex<_Tp>&);
1595 template<typename _Tp> std::complex<_Tp> asinh(const std::complex<_Tp>&);
1596 template<typename _Tp> std::complex<_Tp> atanh(const std::complex<_Tp>&);
1598 template<typename _Tp> _Tp fabs(const std::complex<_Tp>&);
1600 template<typename _Tp>
1601 inline std::complex<_Tp>
1602 __complex_acos(const std::complex<_Tp>& __z)
1604 const std::complex<_Tp> __t = std::asin(__z);
1605 const _Tp __pi_2 = 1.5707963267948966192313216916397514L;
1606 return std::complex<_Tp>(__pi_2 - __t.real(), -__t.imag());
1609 #if _GLIBCXX_USE_C99_COMPLEX_TR1
1610 inline __complex__ float
1611 __complex_acos(__complex__ float __z)
1612 { return __builtin_cacosf(__z); }
1614 inline __complex__ double
1615 __complex_acos(__complex__ double __z)
1616 { return __builtin_cacos(__z); }
1618 inline __complex__ long double
1619 __complex_acos(const __complex__ long double& __z)
1620 { return __builtin_cacosl(__z); }
1622 template<typename _Tp>
1623 inline std::complex<_Tp>
1624 acos(const std::complex<_Tp>& __z)
1625 { return __complex_acos(__z.__rep()); }
1627 /// acos(__z) [8.1.2].
1628 // Effects: Behaves the same as C99 function cacos, defined
1629 // in subclause 7.3.5.1.
1630 template<typename _Tp>
1631 inline std::complex<_Tp>
1632 acos(const std::complex<_Tp>& __z)
1633 { return __complex_acos(__z); }
1636 template<typename _Tp>
1637 inline std::complex<_Tp>
1638 __complex_asin(const std::complex<_Tp>& __z)
1640 std::complex<_Tp> __t(-__z.imag(), __z.real());
1641 __t = std::asinh(__t);
1642 return std::complex<_Tp>(__t.imag(), -__t.real());
1645 #if _GLIBCXX_USE_C99_COMPLEX_TR1
1646 inline __complex__ float
1647 __complex_asin(__complex__ float __z)
1648 { return __builtin_casinf(__z); }
1650 inline __complex__ double
1651 __complex_asin(__complex__ double __z)
1652 { return __builtin_casin(__z); }
1654 inline __complex__ long double
1655 __complex_asin(const __complex__ long double& __z)
1656 { return __builtin_casinl(__z); }
1658 template<typename _Tp>
1659 inline std::complex<_Tp>
1660 asin(const std::complex<_Tp>& __z)
1661 { return __complex_asin(__z.__rep()); }
1663 /// asin(__z) [8.1.3].
1664 // Effects: Behaves the same as C99 function casin, defined
1665 // in subclause 7.3.5.2.
1666 template<typename _Tp>
1667 inline std::complex<_Tp>
1668 asin(const std::complex<_Tp>& __z)
1669 { return __complex_asin(__z); }
1672 template<typename _Tp>
1674 __complex_atan(const std::complex<_Tp>& __z)
1676 const _Tp __r2 = __z.real() * __z.real();
1677 const _Tp __x = _Tp(1.0) - __r2 - __z.imag() * __z.imag();
1679 _Tp __num = __z.imag() + _Tp(1.0);
1680 _Tp __den = __z.imag() - _Tp(1.0);
1682 __num = __r2 + __num * __num;
1683 __den = __r2 + __den * __den;
1685 return std::complex<_Tp>(_Tp(0.5) * atan2(_Tp(2.0) * __z.real(), __x),
1686 _Tp(0.25) * log(__num / __den));
1689 #if _GLIBCXX_USE_C99_COMPLEX_TR1
1690 inline __complex__ float
1691 __complex_atan(__complex__ float __z)
1692 { return __builtin_catanf(__z); }
1694 inline __complex__ double
1695 __complex_atan(__complex__ double __z)
1696 { return __builtin_catan(__z); }
1698 inline __complex__ long double
1699 __complex_atan(const __complex__ long double& __z)
1700 { return __builtin_catanl(__z); }
1702 template<typename _Tp>
1703 inline std::complex<_Tp>
1704 atan(const std::complex<_Tp>& __z)
1705 { return __complex_atan(__z.__rep()); }
1707 /// atan(__z) [8.1.4].
1708 // Effects: Behaves the same as C99 function catan, defined
1709 // in subclause 7.3.5.3.
1710 template<typename _Tp>
1711 inline std::complex<_Tp>
1712 atan(const std::complex<_Tp>& __z)
1713 { return __complex_atan(__z); }
1716 template<typename _Tp>
1718 __complex_acosh(const std::complex<_Tp>& __z)
1721 return _Tp(2.0) * std::log(std::sqrt(_Tp(0.5) * (__z + _Tp(1.0)))
1722 + std::sqrt(_Tp(0.5) * (__z - _Tp(1.0))));
1725 #if _GLIBCXX_USE_C99_COMPLEX_TR1
1726 inline __complex__ float
1727 __complex_acosh(__complex__ float __z)
1728 { return __builtin_cacoshf(__z); }
1730 inline __complex__ double
1731 __complex_acosh(__complex__ double __z)
1732 { return __builtin_cacosh(__z); }
1734 inline __complex__ long double
1735 __complex_acosh(const __complex__ long double& __z)
1736 { return __builtin_cacoshl(__z); }
1738 template<typename _Tp>
1739 inline std::complex<_Tp>
1740 acosh(const std::complex<_Tp>& __z)
1741 { return __complex_acosh(__z.__rep()); }
1743 /// acosh(__z) [8.1.5].
1744 // Effects: Behaves the same as C99 function cacosh, defined
1745 // in subclause 7.3.6.1.
1746 template<typename _Tp>
1747 inline std::complex<_Tp>
1748 acosh(const std::complex<_Tp>& __z)
1749 { return __complex_acosh(__z); }
1752 template<typename _Tp>
1754 __complex_asinh(const std::complex<_Tp>& __z)
1756 std::complex<_Tp> __t((__z.real() - __z.imag())
1757 * (__z.real() + __z.imag()) + _Tp(1.0),
1758 _Tp(2.0) * __z.real() * __z.imag());
1759 __t = std::sqrt(__t);
1761 return std::log(__t + __z);
1764 #if _GLIBCXX_USE_C99_COMPLEX_TR1
1765 inline __complex__ float
1766 __complex_asinh(__complex__ float __z)
1767 { return __builtin_casinhf(__z); }
1769 inline __complex__ double
1770 __complex_asinh(__complex__ double __z)
1771 { return __builtin_casinh(__z); }
1773 inline __complex__ long double
1774 __complex_asinh(const __complex__ long double& __z)
1775 { return __builtin_casinhl(__z); }
1777 template<typename _Tp>
1778 inline std::complex<_Tp>
1779 asinh(const std::complex<_Tp>& __z)
1780 { return __complex_asinh(__z.__rep()); }
1782 /// asinh(__z) [8.1.6].
1783 // Effects: Behaves the same as C99 function casin, defined
1784 // in subclause 7.3.6.2.
1785 template<typename _Tp>
1786 inline std::complex<_Tp>
1787 asinh(const std::complex<_Tp>& __z)
1788 { return __complex_asinh(__z); }
1791 template<typename _Tp>
1793 __complex_atanh(const std::complex<_Tp>& __z)
1795 const _Tp __i2 = __z.imag() * __z.imag();
1796 const _Tp __x = _Tp(1.0) - __i2 - __z.real() * __z.real();
1798 _Tp __num = _Tp(1.0) + __z.real();
1799 _Tp __den = _Tp(1.0) - __z.real();
1801 __num = __i2 + __num * __num;
1802 __den = __i2 + __den * __den;
1804 return std::complex<_Tp>(_Tp(0.25) * (log(__num) - log(__den)),
1805 _Tp(0.5) * atan2(_Tp(2.0) * __z.imag(), __x));
1808 #if _GLIBCXX_USE_C99_COMPLEX_TR1
1809 inline __complex__ float
1810 __complex_atanh(__complex__ float __z)
1811 { return __builtin_catanhf(__z); }
1813 inline __complex__ double
1814 __complex_atanh(__complex__ double __z)
1815 { return __builtin_catanh(__z); }
1817 inline __complex__ long double
1818 __complex_atanh(const __complex__ long double& __z)
1819 { return __builtin_catanhl(__z); }
1821 template<typename _Tp>
1822 inline std::complex<_Tp>
1823 atanh(const std::complex<_Tp>& __z)
1824 { return __complex_atanh(__z.__rep()); }
1826 /// atanh(__z) [8.1.7].
1827 // Effects: Behaves the same as C99 function catanh, defined
1828 // in subclause 7.3.6.3.
1829 template<typename _Tp>
1830 inline std::complex<_Tp>
1831 atanh(const std::complex<_Tp>& __z)
1832 { return __complex_atanh(__z); }
1835 template<typename _Tp>
1837 /// fabs(__z) [8.1.8].
1838 // Effects: Behaves the same as C99 function cabs, defined
1839 // in subclause 7.3.8.1.
1840 fabs(const std::complex<_Tp>& __z)
1841 { return std::abs(__z); }
1843 /// Additional overloads [8.1.9].
1844 template<typename _Tp>
1845 inline typename __gnu_cxx::__promote<_Tp>::__type
1848 typedef typename __gnu_cxx::__promote<_Tp>::__type __type;
1849 #if (_GLIBCXX11_USE_C99_MATH && !_GLIBCXX_USE_C99_FP_MACROS_DYNAMIC)
1850 return std::signbit(__x) ? __type(3.1415926535897932384626433832795029L)
1853 return std::arg(std::complex<__type>(__x));
1857 template<typename _Tp>
1858 _GLIBCXX_CONSTEXPR inline typename __gnu_cxx::__promote<_Tp>::__type
1862 template<typename _Tp>
1863 _GLIBCXX20_CONSTEXPR inline typename __gnu_cxx::__promote<_Tp>::__type
1866 typedef typename __gnu_cxx::__promote<_Tp>::__type __type;
1867 return __type(__x) * __type(__x);
1870 template<typename _Tp>
1871 _GLIBCXX_CONSTEXPR inline typename __gnu_cxx::__promote<_Tp>::__type
1875 template<typename _Tp, typename _Up>
1876 inline std::complex<typename __gnu_cxx::__promote_2<_Tp, _Up>::__type>
1877 pow(const std::complex<_Tp>& __x, const _Up& __y)
1879 typedef typename __gnu_cxx::__promote_2<_Tp, _Up>::__type __type;
1880 return std::pow(std::complex<__type>(__x), __type(__y));
1883 template<typename _Tp, typename _Up>
1884 inline std::complex<typename __gnu_cxx::__promote_2<_Tp, _Up>::__type>
1885 pow(const _Tp& __x, const std::complex<_Up>& __y)
1887 typedef typename __gnu_cxx::__promote_2<_Tp, _Up>::__type __type;
1888 return std::pow(__type(__x), std::complex<__type>(__y));
1891 template<typename _Tp, typename _Up>
1892 inline std::complex<typename __gnu_cxx::__promote_2<_Tp, _Up>::__type>
1893 pow(const std::complex<_Tp>& __x, const std::complex<_Up>& __y)
1895 typedef typename __gnu_cxx::__promote_2<_Tp, _Up>::__type __type;
1896 return std::pow(std::complex<__type>(__x),
1897 std::complex<__type>(__y));
1900 // Forward declarations.
1902 template<typename _Tp>
1903 std::complex<_Tp> proj(const std::complex<_Tp>&);
1905 // Generic implementation of std::proj, does not work for infinities.
1906 template<typename _Tp>
1907 inline std::complex<_Tp>
1908 __complex_proj(const std::complex<_Tp>& __z)
1911 #if _GLIBCXX_USE_C99_COMPLEX
1912 inline complex<float>
1913 __complex_proj(const complex<float>& __z)
1914 { return __builtin_cprojf(__z.__rep()); }
1916 inline complex<double>
1917 __complex_proj(const complex<double>& __z)
1918 { return __builtin_cproj(__z.__rep()); }
1920 inline complex<long double>
1921 __complex_proj(const complex<long double>& __z)
1922 { return __builtin_cprojl(__z.__rep()); }
1923 #elif defined _GLIBCXX_USE_C99_MATH_TR1
1924 inline complex<float>
1925 __complex_proj(const complex<float>& __z)
1927 if (__builtin_isinf(__z.real()) || __builtin_isinf(__z.imag()))
1928 return complex<float>(__builtin_inff(),
1929 __builtin_copysignf(0.0f, __z.imag()));
1933 inline complex<double>
1934 __complex_proj(const complex<double>& __z)
1936 if (__builtin_isinf(__z.real()) || __builtin_isinf(__z.imag()))
1937 return complex<double>(__builtin_inf(),
1938 __builtin_copysign(0.0, __z.imag()));
1942 inline complex<long double>
1943 __complex_proj(const complex<long double>& __z)
1945 if (__builtin_isinf(__z.real()) || __builtin_isinf(__z.imag()))
1946 return complex<long double>(__builtin_infl(),
1947 __builtin_copysignl(0.0l, __z.imag()));
1952 template<typename _Tp>
1953 inline std::complex<_Tp>
1954 proj(const std::complex<_Tp>& __z)
1955 { return __complex_proj(__z); }
1957 // Overload for scalars
1958 template<typename _Tp>
1959 inline std::complex<typename __gnu_cxx::__promote<_Tp>::__type>
1962 typedef typename __gnu_cxx::__promote<_Tp>::__type __type;
1963 return std::proj(std::complex<__type>(__x));
1966 template<typename _Tp>
1967 inline _GLIBCXX20_CONSTEXPR
1968 std::complex<typename __gnu_cxx::__promote<_Tp>::__type>
1971 typedef typename __gnu_cxx::__promote<_Tp>::__type __type;
1972 return std::complex<__type>(__x, -__type());
1975 #if __cplusplus > 201103L
1977 inline namespace literals {
1978 inline namespace complex_literals {
1979 #pragma GCC diagnostic push
1980 #pragma GCC diagnostic ignored "-Wliteral-suffix"
1981 #define __cpp_lib_complex_udls 201309
1983 constexpr std::complex<float>
1984 operator""if(long double __num)
1985 { return std::complex<float>{0.0F, static_cast<float>(__num)}; }
1987 constexpr std::complex<float>
1988 operator""if(unsigned long long __num)
1989 { return std::complex<float>{0.0F, static_cast<float>(__num)}; }
1991 constexpr std::complex<double>
1992 operator""i(long double __num)
1993 { return std::complex<double>{0.0, static_cast<double>(__num)}; }
1995 constexpr std::complex<double>
1996 operator""i(unsigned long long __num)
1997 { return std::complex<double>{0.0, static_cast<double>(__num)}; }
1999 constexpr std::complex<long double>
2000 operator""il(long double __num)
2001 { return std::complex<long double>{0.0L, __num}; }
2003 constexpr std::complex<long double>
2004 operator""il(unsigned long long __num)
2005 { return std::complex<long double>{0.0L, static_cast<long double>(__num)}; }
2007 #pragma GCC diagnostic pop
2008 } // inline namespace complex_literals
2009 } // inline namespace literals
2013 _GLIBCXX_END_NAMESPACE_VERSION
2018 #endif /* _GLIBCXX_COMPLEX */