1 // The template and inlines for the -*- C++ -*- complex number classes.
3 // Copyright (C) 1997-2024 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>
51 #pragma clang diagnostic push
52 #pragma clang diagnostic ignored "-Wc99-extensions"
55 #define __glibcxx_want_constexpr_complex
56 #define __glibcxx_want_complex_udls
57 #include <bits/version.h>
59 namespace std _GLIBCXX_VISIBILITY(default)
61 _GLIBCXX_BEGIN_NAMESPACE_VERSION
64 * @defgroup complex_numbers Complex Numbers
67 * Classes and functions for complex numbers.
71 // Forward declarations.
72 template<typename _Tp> class complex;
73 template<> class complex<float>;
74 template<> class complex<double>;
75 template<> class complex<long double>;
77 /// Return magnitude of @a z.
78 template<typename _Tp> _Tp abs(const complex<_Tp>&);
79 /// Return phase angle of @a z.
80 template<typename _Tp> _Tp arg(const complex<_Tp>&);
81 /// Return @a z magnitude squared.
82 template<typename _Tp> _Tp _GLIBCXX20_CONSTEXPR norm(const complex<_Tp>&);
84 /// Return complex conjugate of @a z.
85 template<typename _Tp>
86 _GLIBCXX20_CONSTEXPR complex<_Tp> conj(const complex<_Tp>&);
87 /// Return complex with magnitude @a rho and angle @a theta.
88 template<typename _Tp> complex<_Tp> polar(const _Tp&, const _Tp& = 0);
91 /// Return complex cosine of @a z.
92 template<typename _Tp> complex<_Tp> cos(const complex<_Tp>&);
93 /// Return complex hyperbolic cosine of @a z.
94 template<typename _Tp> complex<_Tp> cosh(const complex<_Tp>&);
95 /// Return complex base e exponential of @a z.
96 template<typename _Tp> complex<_Tp> exp(const complex<_Tp>&);
97 /// Return complex natural logarithm of @a z.
98 template<typename _Tp> complex<_Tp> log(const complex<_Tp>&);
99 /// Return complex base 10 logarithm of @a z.
100 template<typename _Tp> complex<_Tp> log10(const complex<_Tp>&);
101 /// Return @a x to the @a y'th power.
102 template<typename _Tp> complex<_Tp> pow(const complex<_Tp>&, int);
103 /// Return @a x to the @a y'th power.
104 template<typename _Tp> complex<_Tp> pow(const complex<_Tp>&, const _Tp&);
105 /// Return @a x to the @a y'th power.
106 template<typename _Tp> complex<_Tp> pow(const complex<_Tp>&,
107 const complex<_Tp>&);
108 /// Return @a x to the @a y'th power.
109 template<typename _Tp> complex<_Tp> pow(const _Tp&, const complex<_Tp>&);
110 /// Return complex sine of @a z.
111 template<typename _Tp> complex<_Tp> sin(const complex<_Tp>&);
112 /// Return complex hyperbolic sine of @a z.
113 template<typename _Tp> complex<_Tp> sinh(const complex<_Tp>&);
114 /// Return complex square root of @a z.
115 template<typename _Tp> complex<_Tp> sqrt(const complex<_Tp>&);
116 /// Return complex tangent of @a z.
117 template<typename _Tp> complex<_Tp> tan(const complex<_Tp>&);
118 /// Return complex hyperbolic tangent of @a z.
119 template<typename _Tp> complex<_Tp> tanh(const complex<_Tp>&);
122 // 26.2.2 Primary template class complex
124 * Template to represent complex numbers.
126 * Specializations for float, double, and long double are part of the
127 * library. Results with any other type are not guaranteed.
129 * @param Tp Type of real and imaginary values.
131 template<typename _Tp>
136 typedef _Tp value_type;
138 /// Default constructor. First parameter is x, second parameter is y.
139 /// Unspecified parameters default to 0.
140 _GLIBCXX_CONSTEXPR complex(const _Tp& __r = _Tp(), const _Tp& __i = _Tp())
141 : _M_real(__r), _M_imag(__i) { }
143 // Let the compiler synthesize the copy constructor
144 #if __cplusplus >= 201103L
145 constexpr complex(const complex&) = default;
148 /// Converting constructor.
149 template<typename _Up>
150 #if __cplusplus > 202002L
151 explicit(!requires(_Up __u) { _Tp{__u}; })
153 _GLIBCXX_CONSTEXPR complex(const complex<_Up>& __z)
154 : _M_real(_Tp(__z.real())), _M_imag(_Tp(__z.imag())) { }
156 #if __cplusplus >= 201103L
157 // _GLIBCXX_RESOLVE_LIB_DEFECTS
158 // DR 387. std::complex over-encapsulated.
159 _GLIBCXX_ABI_TAG_CXX11
161 real() const { return _M_real; }
163 _GLIBCXX_ABI_TAG_CXX11
165 imag() const { return _M_imag; }
167 /// Return real part of complex number.
169 real() { return _M_real; }
171 /// Return real part of complex number.
173 real() const { return _M_real; }
175 /// Return imaginary part of complex number.
177 imag() { return _M_imag; }
179 /// Return imaginary part of complex number.
181 imag() const { return _M_imag; }
184 // _GLIBCXX_RESOLVE_LIB_DEFECTS
185 // DR 387. std::complex over-encapsulated.
186 _GLIBCXX20_CONSTEXPR void
187 real(_Tp __val) { _M_real = __val; }
189 _GLIBCXX20_CONSTEXPR void
190 imag(_Tp __val) { _M_imag = __val; }
192 /// Assign a scalar to this complex number.
193 _GLIBCXX20_CONSTEXPR complex<_Tp>& operator=(const _Tp&);
195 /// Add a scalar to this complex number.
197 _GLIBCXX20_CONSTEXPR complex<_Tp>&
198 operator+=(const _Tp& __t)
204 /// Subtract a scalar from this complex number.
206 _GLIBCXX20_CONSTEXPR complex<_Tp>&
207 operator-=(const _Tp& __t)
213 /// Multiply this complex number by a scalar.
214 _GLIBCXX20_CONSTEXPR complex<_Tp>& operator*=(const _Tp&);
215 /// Divide this complex number by a scalar.
216 _GLIBCXX20_CONSTEXPR complex<_Tp>& operator/=(const _Tp&);
218 // Let the compiler synthesize the copy assignment operator
219 #if __cplusplus >= 201103L
220 _GLIBCXX20_CONSTEXPR complex& operator=(const complex&) = default;
223 /// Assign another complex number to this one.
224 template<typename _Up>
225 _GLIBCXX20_CONSTEXPR complex<_Tp>& operator=(const complex<_Up>&);
226 /// Add another complex number to this one.
227 template<typename _Up>
228 _GLIBCXX20_CONSTEXPR complex<_Tp>& operator+=(const complex<_Up>&);
229 /// Subtract another complex number from this one.
230 template<typename _Up>
231 _GLIBCXX20_CONSTEXPR complex<_Tp>& operator-=(const complex<_Up>&);
232 /// Multiply this complex number by another.
233 template<typename _Up>
234 _GLIBCXX20_CONSTEXPR complex<_Tp>& operator*=(const complex<_Up>&);
235 /// Divide this complex number by another.
236 template<typename _Up>
237 _GLIBCXX20_CONSTEXPR complex<_Tp>& operator/=(const complex<_Up>&);
239 _GLIBCXX_CONSTEXPR complex __rep() const
247 template<typename _Tp>
248 _GLIBCXX20_CONSTEXPR complex<_Tp>&
249 complex<_Tp>::operator=(const _Tp& __t)
257 template<typename _Tp>
258 _GLIBCXX20_CONSTEXPR complex<_Tp>&
259 complex<_Tp>::operator*=(const _Tp& __t)
267 template<typename _Tp>
268 _GLIBCXX20_CONSTEXPR complex<_Tp>&
269 complex<_Tp>::operator/=(const _Tp& __t)
276 template<typename _Tp>
277 template<typename _Up>
278 _GLIBCXX20_CONSTEXPR complex<_Tp>&
279 complex<_Tp>::operator=(const complex<_Up>& __z)
281 _M_real = __z.real();
282 _M_imag = __z.imag();
287 template<typename _Tp>
288 template<typename _Up>
289 _GLIBCXX20_CONSTEXPR complex<_Tp>&
290 complex<_Tp>::operator+=(const complex<_Up>& __z)
292 _M_real += __z.real();
293 _M_imag += __z.imag();
298 template<typename _Tp>
299 template<typename _Up>
300 _GLIBCXX20_CONSTEXPR complex<_Tp>&
301 complex<_Tp>::operator-=(const complex<_Up>& __z)
303 _M_real -= __z.real();
304 _M_imag -= __z.imag();
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 _M_imag = _M_real * __z.imag() + _M_imag * __z.real();
322 // XXX: This is a grammar school implementation.
323 template<typename _Tp>
324 template<typename _Up>
325 _GLIBCXX20_CONSTEXPR complex<_Tp>&
326 complex<_Tp>::operator/=(const complex<_Up>& __z)
328 const _Tp __r = _M_real * __z.real() + _M_imag * __z.imag();
329 const _Tp __n = std::norm(__z);
330 _M_imag = (_M_imag * __z.real() - _M_real * __z.imag()) / __n;
337 /// Return new complex value @a x plus @a y.
338 template<typename _Tp>
339 inline _GLIBCXX20_CONSTEXPR complex<_Tp>
340 operator+(const complex<_Tp>& __x, const complex<_Tp>& __y)
342 complex<_Tp> __r = __x;
347 template<typename _Tp>
348 inline _GLIBCXX20_CONSTEXPR complex<_Tp>
349 operator+(const complex<_Tp>& __x, const _Tp& __y)
351 complex<_Tp> __r = __x;
356 template<typename _Tp>
357 inline _GLIBCXX20_CONSTEXPR complex<_Tp>
358 operator+(const _Tp& __x, const complex<_Tp>& __y)
360 complex<_Tp> __r = __y;
367 /// Return new complex value @a x minus @a y.
368 template<typename _Tp>
369 inline _GLIBCXX20_CONSTEXPR complex<_Tp>
370 operator-(const complex<_Tp>& __x, const complex<_Tp>& __y)
372 complex<_Tp> __r = __x;
377 template<typename _Tp>
378 inline _GLIBCXX20_CONSTEXPR complex<_Tp>
379 operator-(const complex<_Tp>& __x, const _Tp& __y)
381 complex<_Tp> __r = __x;
386 template<typename _Tp>
387 inline _GLIBCXX20_CONSTEXPR complex<_Tp>
388 operator-(const _Tp& __x, const complex<_Tp>& __y)
390 complex<_Tp> __r = -__y;
397 /// Return new complex value @a x times @a y.
398 template<typename _Tp>
399 inline _GLIBCXX20_CONSTEXPR complex<_Tp>
400 operator*(const complex<_Tp>& __x, const complex<_Tp>& __y)
402 complex<_Tp> __r = __x;
407 template<typename _Tp>
408 inline _GLIBCXX20_CONSTEXPR complex<_Tp>
409 operator*(const complex<_Tp>& __x, const _Tp& __y)
411 complex<_Tp> __r = __x;
416 template<typename _Tp>
417 inline _GLIBCXX20_CONSTEXPR complex<_Tp>
418 operator*(const _Tp& __x, const complex<_Tp>& __y)
420 complex<_Tp> __r = __y;
427 /// Return new complex value @a x divided by @a y.
428 template<typename _Tp>
429 inline _GLIBCXX20_CONSTEXPR complex<_Tp>
430 operator/(const complex<_Tp>& __x, const complex<_Tp>& __y)
432 complex<_Tp> __r = __x;
437 template<typename _Tp>
438 inline _GLIBCXX20_CONSTEXPR complex<_Tp>
439 operator/(const complex<_Tp>& __x, const _Tp& __y)
441 complex<_Tp> __r = __x;
446 template<typename _Tp>
447 inline _GLIBCXX20_CONSTEXPR complex<_Tp>
448 operator/(const _Tp& __x, const complex<_Tp>& __y)
450 complex<_Tp> __r = __x;
457 template<typename _Tp>
458 inline _GLIBCXX20_CONSTEXPR complex<_Tp>
459 operator+(const complex<_Tp>& __x)
462 /// Return complex negation of @a x.
463 template<typename _Tp>
464 inline _GLIBCXX20_CONSTEXPR complex<_Tp>
465 operator-(const complex<_Tp>& __x)
466 { return complex<_Tp>(-__x.real(), -__x.imag()); }
469 /// Return true if @a x is equal to @a y.
470 template<typename _Tp>
471 inline _GLIBCXX_CONSTEXPR bool
472 operator==(const complex<_Tp>& __x, const complex<_Tp>& __y)
473 { return __x.real() == __y.real() && __x.imag() == __y.imag(); }
475 template<typename _Tp>
476 inline _GLIBCXX_CONSTEXPR bool
477 operator==(const complex<_Tp>& __x, const _Tp& __y)
478 { return __x.real() == __y && __x.imag() == _Tp(); }
480 #if !(__cpp_impl_three_way_comparison >= 201907L)
481 template<typename _Tp>
482 inline _GLIBCXX_CONSTEXPR bool
483 operator==(const _Tp& __x, const complex<_Tp>& __y)
484 { return __x == __y.real() && _Tp() == __y.imag(); }
488 /// Return false if @a x is equal to @a y.
489 template<typename _Tp>
490 inline _GLIBCXX_CONSTEXPR bool
491 operator!=(const complex<_Tp>& __x, const complex<_Tp>& __y)
492 { return __x.real() != __y.real() || __x.imag() != __y.imag(); }
494 template<typename _Tp>
495 inline _GLIBCXX_CONSTEXPR bool
496 operator!=(const complex<_Tp>& __x, const _Tp& __y)
497 { return __x.real() != __y || __x.imag() != _Tp(); }
499 template<typename _Tp>
500 inline _GLIBCXX_CONSTEXPR bool
501 operator!=(const _Tp& __x, const complex<_Tp>& __y)
502 { return __x != __y.real() || _Tp() != __y.imag(); }
506 /// Extraction operator for complex values.
507 template<typename _Tp, typename _CharT, class _Traits>
508 basic_istream<_CharT, _Traits>&
509 operator>>(basic_istream<_CharT, _Traits>& __is, complex<_Tp>& __x)
515 if (_Traits::eq(__ch, __is.widen('(')))
518 if (__is >> __u >> __ch)
520 const _CharT __rparen = __is.widen(')');
521 if (_Traits::eq(__ch, __rparen))
526 else if (_Traits::eq(__ch, __is.widen(',')))
529 if (__is >> __v >> __ch)
531 if (_Traits::eq(__ch, __rparen))
533 __x = complex<_Tp>(__u, __v);
556 __is.setstate(ios_base::failbit);
560 /// Insertion operator for complex values.
561 template<typename _Tp, typename _CharT, class _Traits>
562 basic_ostream<_CharT, _Traits>&
563 operator<<(basic_ostream<_CharT, _Traits>& __os, const complex<_Tp>& __x)
565 basic_ostringstream<_CharT, _Traits> __s;
566 __s.flags(__os.flags());
567 __s.imbue(__os.getloc());
568 __s.precision(__os.precision());
569 __s << '(' << __x.real() << ',' << __x.imag() << ')';
570 return __os << __s.str();
574 #if __cplusplus >= 201103L
575 template<typename _Tp>
577 real(const complex<_Tp>& __z)
578 { return __z.real(); }
580 template<typename _Tp>
582 imag(const complex<_Tp>& __z)
583 { return __z.imag(); }
585 template<typename _Tp>
587 real(complex<_Tp>& __z)
588 { return __z.real(); }
590 template<typename _Tp>
592 real(const complex<_Tp>& __z)
593 { return __z.real(); }
595 template<typename _Tp>
597 imag(complex<_Tp>& __z)
598 { return __z.imag(); }
600 template<typename _Tp>
602 imag(const complex<_Tp>& __z)
603 { return __z.imag(); }
606 #if _GLIBCXX_USE_C99_COMPLEX
607 #if defined(__STDCPP_FLOAT16_T__) && defined(_GLIBCXX_FLOAT_IS_IEEE_BINARY32)
609 __complex_abs(__complex__ _Float16 __z)
610 { return _Float16(__builtin_cabsf(__z)); }
613 __complex_arg(__complex__ _Float16 __z)
614 { return _Float16(__builtin_cargf(__z)); }
616 inline __complex__ _Float16
617 __complex_cos(__complex__ _Float16 __z)
618 { return static_cast<__complex__ _Float16>(__builtin_ccosf(__z)); }
620 inline __complex__ _Float16
621 __complex_cosh(__complex__ _Float16 __z)
622 { return static_cast<__complex__ _Float16>(__builtin_ccoshf(__z)); }
624 inline __complex__ _Float16
625 __complex_exp(__complex__ _Float16 __z)
626 { return static_cast<__complex__ _Float16>(__builtin_cexpf(__z)); }
628 inline __complex__ _Float16
629 __complex_log(__complex__ _Float16 __z)
630 { return static_cast<__complex__ _Float16>(__builtin_clogf(__z)); }
632 inline __complex__ _Float16
633 __complex_sin(__complex__ _Float16 __z)
634 { return static_cast<__complex__ _Float16>(__builtin_csinf(__z)); }
636 inline __complex__ _Float16
637 __complex_sinh(__complex__ _Float16 __z)
638 { return static_cast<__complex__ _Float16>(__builtin_csinhf(__z)); }
640 inline __complex__ _Float16
641 __complex_sqrt(__complex__ _Float16 __z)
642 { return static_cast<__complex__ _Float16>(__builtin_csqrtf(__z)); }
644 inline __complex__ _Float16
645 __complex_tan(__complex__ _Float16 __z)
646 { return static_cast<__complex__ _Float16>(__builtin_ctanf(__z)); }
648 inline __complex__ _Float16
649 __complex_tanh(__complex__ _Float16 __z)
650 { return static_cast<__complex__ _Float16>(__builtin_ctanhf(__z)); }
652 inline __complex__ _Float16
653 __complex_pow(__complex__ _Float16 __x, __complex__ _Float16 __y)
654 { return static_cast<__complex__ _Float16>(__builtin_cpowf(__x, __y)); }
657 #if defined(__STDCPP_FLOAT32_T__) && defined(_GLIBCXX_FLOAT_IS_IEEE_BINARY32)
659 __complex_abs(__complex__ _Float32 __z) { return __builtin_cabsf(__z); }
662 __complex_arg(__complex__ _Float32 __z) { return __builtin_cargf(__z); }
664 inline __complex__ _Float32
665 __complex_cos(__complex__ _Float32 __z) { return __builtin_ccosf(__z); }
667 inline __complex__ _Float32
668 __complex_cosh(__complex__ _Float32 __z) { return __builtin_ccoshf(__z); }
670 inline __complex__ _Float32
671 __complex_exp(__complex__ _Float32 __z) { return __builtin_cexpf(__z); }
673 inline __complex__ _Float32
674 __complex_log(__complex__ _Float32 __z) { return __builtin_clogf(__z); }
676 inline __complex__ _Float32
677 __complex_sin(__complex__ _Float32 __z) { return __builtin_csinf(__z); }
679 inline __complex__ _Float32
680 __complex_sinh(__complex__ _Float32 __z) { return __builtin_csinhf(__z); }
682 inline __complex__ _Float32
683 __complex_sqrt(__complex__ _Float32 __z) { return __builtin_csqrtf(__z); }
685 inline __complex__ _Float32
686 __complex_tan(__complex__ _Float32 __z) { return __builtin_ctanf(__z); }
688 inline __complex__ _Float32
689 __complex_tanh(__complex__ _Float32 __z) { return __builtin_ctanhf(__z); }
691 inline __complex__ _Float32
692 __complex_pow(__complex__ _Float32 __x, __complex__ _Float32 __y)
693 { return __builtin_cpowf(__x, __y); }
696 #if defined(__STDCPP_FLOAT64_T__) && defined(_GLIBCXX_DOUBLE_IS_IEEE_BINARY64)
698 __complex_abs(__complex__ _Float64 __z) { return __builtin_cabs(__z); }
701 __complex_arg(__complex__ _Float64 __z) { return __builtin_carg(__z); }
703 inline __complex__ _Float64
704 __complex_cos(__complex__ _Float64 __z) { return __builtin_ccos(__z); }
706 inline __complex__ _Float64
707 __complex_cosh(__complex__ _Float64 __z) { return __builtin_ccosh(__z); }
709 inline __complex__ _Float64
710 __complex_exp(__complex__ _Float64 __z) { return __builtin_cexp(__z); }
712 inline __complex__ _Float64
713 __complex_log(__complex__ _Float64 __z) { return __builtin_clog(__z); }
715 inline __complex__ _Float64
716 __complex_sin(__complex__ _Float64 __z) { return __builtin_csin(__z); }
718 inline __complex__ _Float64
719 __complex_sinh(__complex__ _Float64 __z) { return __builtin_csinh(__z); }
721 inline __complex__ _Float64
722 __complex_sqrt(__complex__ _Float64 __z) { return __builtin_csqrt(__z); }
724 inline __complex__ _Float64
725 __complex_tan(__complex__ _Float64 __z) { return __builtin_ctan(__z); }
727 inline __complex__ _Float64
728 __complex_tanh(__complex__ _Float64 __z) { return __builtin_ctanh(__z); }
730 inline __complex__ _Float64
731 __complex_pow(__complex__ _Float64 __x, __complex__ _Float64 __y)
732 { return __builtin_cpow(__x, __y); }
735 #if defined(__STDCPP_FLOAT128_T__) && defined(_GLIBCXX_LDOUBLE_IS_IEEE_BINARY128)
737 __complex_abs(__complex__ _Float128 __z) { return __builtin_cabsl(__z); }
740 __complex_arg(__complex__ _Float128 __z) { return __builtin_cargl(__z); }
742 inline __complex__ _Float128
743 __complex_cos(__complex__ _Float128 __z) { return __builtin_ccosl(__z); }
745 inline __complex__ _Float128
746 __complex_cosh(__complex__ _Float128 __z) { return __builtin_ccoshl(__z); }
748 inline __complex__ _Float128
749 __complex_exp(__complex__ _Float128 __z) { return __builtin_cexpl(__z); }
751 inline __complex__ _Float128
752 __complex_log(__complex__ _Float128 __z) { return __builtin_clogl(__z); }
754 inline __complex__ _Float128
755 __complex_sin(__complex__ _Float128 __z) { return __builtin_csinl(__z); }
757 inline __complex__ _Float128
758 __complex_sinh(__complex__ _Float128 __z) { return __builtin_csinhl(__z); }
760 inline __complex__ _Float128
761 __complex_sqrt(__complex__ _Float128 __z) { return __builtin_csqrtl(__z); }
763 inline __complex__ _Float128
764 __complex_tan(__complex__ _Float128 __z) { return __builtin_ctanl(__z); }
766 inline __complex__ _Float128
767 __complex_tanh(__complex__ _Float128 __z) { return __builtin_ctanhl(__z); }
769 inline __complex__ _Float128
770 __complex_pow(__complex__ _Float128 __x, __complex__ _Float128 __y)
771 { return __builtin_cpowl(__x, __y); }
772 #elif defined(__STDCPP_FLOAT128_T__) && defined(_GLIBCXX_HAVE_FLOAT128_MATH)
774 __complex_abs(__complex__ _Float128 __z) { return __builtin_cabsf128(__z); }
777 __complex_arg(__complex__ _Float128 __z) { return __builtin_cargf128(__z); }
779 inline __complex__ _Float128
780 __complex_cos(__complex__ _Float128 __z) { return __builtin_ccosf128(__z); }
782 inline __complex__ _Float128
783 __complex_cosh(__complex__ _Float128 __z) { return __builtin_ccoshf128(__z); }
785 inline __complex__ _Float128
786 __complex_exp(__complex__ _Float128 __z) { return __builtin_cexpf128(__z); }
788 inline __complex__ _Float128
789 __complex_log(__complex__ _Float128 __z) { return __builtin_clogf128(__z); }
791 inline __complex__ _Float128
792 __complex_sin(__complex__ _Float128 __z) { return __builtin_csinf128(__z); }
794 inline __complex__ _Float128
795 __complex_sinh(__complex__ _Float128 __z) { return __builtin_csinhf128(__z); }
797 inline __complex__ _Float128
798 __complex_sqrt(__complex__ _Float128 __z) { return __builtin_csqrtf128(__z); }
800 inline __complex__ _Float128
801 __complex_tan(__complex__ _Float128 __z) { return __builtin_ctanf128(__z); }
803 inline __complex__ _Float128
804 __complex_tanh(__complex__ _Float128 __z) { return __builtin_ctanhf128(__z); }
806 inline __complex__ _Float128
807 __complex_pow(__complex__ _Float128 __x, __complex__ _Float128 __y)
808 { return __builtin_cpowf128(__x, __y); }
811 #if defined(__STDCPP_BFLOAT16_T__) && defined(_GLIBCXX_FLOAT_IS_IEEE_BINARY32)
812 inline __gnu_cxx::__bfloat16_t
813 __complex_abs(__complex__ decltype(0.0bf16) __z)
814 { return __gnu_cxx::__bfloat16_t(__builtin_cabsf(__z)); }
816 inline __gnu_cxx::__bfloat16_t
817 __complex_arg(__complex__ decltype(0.0bf16) __z)
818 { return __gnu_cxx::__bfloat16_t(__builtin_cargf(__z)); }
820 inline __complex__ decltype(0.0bf16)
821 __complex_cos(__complex__ decltype(0.0bf16) __z)
822 { return static_cast<__complex__ decltype(0.0bf16)>(__builtin_ccosf(__z)); }
824 inline __complex__ decltype(0.0bf16)
825 __complex_cosh(__complex__ decltype(0.0bf16) __z)
826 { return static_cast<__complex__ decltype(0.0bf16)>(__builtin_ccoshf(__z)); }
828 inline __complex__ decltype(0.0bf16)
829 __complex_exp(__complex__ decltype(0.0bf16) __z)
830 { return static_cast<__complex__ decltype(0.0bf16)>(__builtin_cexpf(__z)); }
832 inline __complex__ decltype(0.0bf16)
833 __complex_log(__complex__ decltype(0.0bf16) __z)
834 { return static_cast<__complex__ decltype(0.0bf16)>(__builtin_clogf(__z)); }
836 inline __complex__ decltype(0.0bf16)
837 __complex_sin(__complex__ decltype(0.0bf16) __z)
838 { return static_cast<__complex__ decltype(0.0bf16)>(__builtin_csinf(__z)); }
840 inline __complex__ decltype(0.0bf16)
841 __complex_sinh(__complex__ decltype(0.0bf16) __z)
842 { return static_cast<__complex__ decltype(0.0bf16)>(__builtin_csinhf(__z)); }
844 inline __complex__ decltype(0.0bf16)
845 __complex_sqrt(__complex__ decltype(0.0bf16) __z)
846 { return static_cast<__complex__ decltype(0.0bf16)>(__builtin_csqrtf(__z)); }
848 inline __complex__ decltype(0.0bf16)
849 __complex_tan(__complex__ decltype(0.0bf16) __z)
850 { return static_cast<__complex__ decltype(0.0bf16)>(__builtin_ctanf(__z)); }
852 inline __complex__ decltype(0.0bf16)
853 __complex_tanh(__complex__ decltype(0.0bf16) __z)
854 { return static_cast<__complex__ decltype(0.0bf16)>(__builtin_ctanhf(__z)); }
856 inline __complex__ decltype(0.0bf16)
857 __complex_pow(__complex__ decltype(0.0bf16) __x,
858 __complex__ decltype(0.0bf16) __y)
859 { return static_cast<__complex__ decltype(0.0bf16)>(__builtin_cpowf(__x,
864 // 26.2.7/3 abs(__z): Returns the magnitude of __z.
865 template<typename _Tp>
867 __complex_abs(const complex<_Tp>& __z)
869 _Tp __x = __z.real();
870 _Tp __y = __z.imag();
871 const _Tp __s = std::max(abs(__x), abs(__y));
872 if (__s == _Tp()) // well ...
876 return __s * sqrt(__x * __x + __y * __y);
879 #if _GLIBCXX_USE_C99_COMPLEX
881 __complex_abs(__complex__ float __z) { return __builtin_cabsf(__z); }
884 __complex_abs(__complex__ double __z) { return __builtin_cabs(__z); }
887 __complex_abs(const __complex__ long double& __z)
888 { return __builtin_cabsl(__z); }
890 template<typename _Tp>
892 abs(const complex<_Tp>& __z) { return __complex_abs(__z.__rep()); }
894 template<typename _Tp>
896 abs(const complex<_Tp>& __z) { return __complex_abs(__z); }
900 // 26.2.7/4: arg(__z): Returns the phase angle of __z.
901 template<typename _Tp>
903 __complex_arg(const complex<_Tp>& __z)
904 { return atan2(__z.imag(), __z.real()); }
906 #if _GLIBCXX_USE_C99_COMPLEX
908 __complex_arg(__complex__ float __z) { return __builtin_cargf(__z); }
911 __complex_arg(__complex__ double __z) { return __builtin_carg(__z); }
914 __complex_arg(const __complex__ long double& __z)
915 { return __builtin_cargl(__z); }
917 template<typename _Tp>
919 arg(const complex<_Tp>& __z) { return __complex_arg(__z.__rep()); }
921 template<typename _Tp>
923 arg(const complex<_Tp>& __z) { return __complex_arg(__z); }
926 // 26.2.7/5: norm(__z) returns the squared magnitude of __z.
927 // As defined, norm() is -not- a norm is the common mathematical
928 // sense used in numerics. The helper class _Norm_helper<> tries to
929 // distinguish between builtin floating point and the rest, so as
930 // to deliver an answer as close as possible to the real value.
934 template<typename _Tp>
935 static inline _GLIBCXX20_CONSTEXPR _Tp _S_do_it(const complex<_Tp>& __z)
937 const _Tp __x = __z.real();
938 const _Tp __y = __z.imag();
939 return __x * __x + __y * __y;
944 struct _Norm_helper<true>
946 template<typename _Tp>
947 static inline _GLIBCXX20_CONSTEXPR _Tp _S_do_it(const complex<_Tp>& __z)
949 //_Tp __res = std::abs(__z);
950 //return __res * __res;
951 const _Tp __x = __z.real();
952 const _Tp __y = __z.imag();
953 return __x * __x + __y * __y;
957 template<typename _Tp>
958 inline _GLIBCXX20_CONSTEXPR _Tp
959 norm(const complex<_Tp>& __z)
961 return _Norm_helper<__is_floating<_Tp>::__value
962 && !_GLIBCXX_FAST_MATH>::_S_do_it(__z);
965 template<typename _Tp>
967 polar(const _Tp& __rho, const _Tp& __theta)
969 __glibcxx_assert( __rho >= 0 );
970 return complex<_Tp>(__rho * cos(__theta), __rho * sin(__theta));
973 template<typename _Tp>
974 inline _GLIBCXX20_CONSTEXPR complex<_Tp>
975 conj(const complex<_Tp>& __z)
976 { return complex<_Tp>(__z.real(), -__z.imag()); }
980 // 26.2.8/1 cos(__z): Returns the cosine of __z.
981 template<typename _Tp>
983 __complex_cos(const complex<_Tp>& __z)
985 const _Tp __x = __z.real();
986 const _Tp __y = __z.imag();
987 return complex<_Tp>(cos(__x) * cosh(__y), -sin(__x) * sinh(__y));
990 #if _GLIBCXX_USE_C99_COMPLEX
991 inline __complex__ float
992 __complex_cos(__complex__ float __z) { return __builtin_ccosf(__z); }
994 inline __complex__ double
995 __complex_cos(__complex__ double __z) { return __builtin_ccos(__z); }
997 inline __complex__ long double
998 __complex_cos(const __complex__ long double& __z)
999 { return __builtin_ccosl(__z); }
1001 template<typename _Tp>
1003 cos(const complex<_Tp>& __z) { return __complex_cos(__z.__rep()); }
1005 template<typename _Tp>
1007 cos(const complex<_Tp>& __z) { return __complex_cos(__z); }
1010 // 26.2.8/2 cosh(__z): Returns the hyperbolic cosine of __z.
1011 template<typename _Tp>
1013 __complex_cosh(const complex<_Tp>& __z)
1015 const _Tp __x = __z.real();
1016 const _Tp __y = __z.imag();
1017 return complex<_Tp>(cosh(__x) * cos(__y), sinh(__x) * sin(__y));
1020 #if _GLIBCXX_USE_C99_COMPLEX
1021 inline __complex__ float
1022 __complex_cosh(__complex__ float __z) { return __builtin_ccoshf(__z); }
1024 inline __complex__ double
1025 __complex_cosh(__complex__ double __z) { return __builtin_ccosh(__z); }
1027 inline __complex__ long double
1028 __complex_cosh(const __complex__ long double& __z)
1029 { return __builtin_ccoshl(__z); }
1031 template<typename _Tp>
1033 cosh(const complex<_Tp>& __z) { return __complex_cosh(__z.__rep()); }
1035 template<typename _Tp>
1037 cosh(const complex<_Tp>& __z) { return __complex_cosh(__z); }
1040 // 26.2.8/3 exp(__z): Returns the complex base e exponential of x
1041 template<typename _Tp>
1043 __complex_exp(const complex<_Tp>& __z)
1044 { return std::polar<_Tp>(exp(__z.real()), __z.imag()); }
1046 #if _GLIBCXX_USE_C99_COMPLEX
1047 inline __complex__ float
1048 __complex_exp(__complex__ float __z) { return __builtin_cexpf(__z); }
1050 inline __complex__ double
1051 __complex_exp(__complex__ double __z) { return __builtin_cexp(__z); }
1053 inline __complex__ long double
1054 __complex_exp(const __complex__ long double& __z)
1055 { return __builtin_cexpl(__z); }
1057 template<typename _Tp>
1059 exp(const complex<_Tp>& __z) { return __complex_exp(__z.__rep()); }
1061 template<typename _Tp>
1063 exp(const complex<_Tp>& __z) { return __complex_exp(__z); }
1066 // 26.2.8/5 log(__z): Returns the natural complex logarithm of __z.
1067 // The branch cut is along the negative axis.
1068 template<typename _Tp>
1070 __complex_log(const complex<_Tp>& __z)
1071 { return complex<_Tp>(log(std::abs(__z)), std::arg(__z)); }
1073 #if _GLIBCXX_USE_C99_COMPLEX
1074 inline __complex__ float
1075 __complex_log(__complex__ float __z) { return __builtin_clogf(__z); }
1077 inline __complex__ double
1078 __complex_log(__complex__ double __z) { return __builtin_clog(__z); }
1080 inline __complex__ long double
1081 __complex_log(const __complex__ long double& __z)
1082 { return __builtin_clogl(__z); }
1084 template<typename _Tp>
1086 log(const complex<_Tp>& __z) { return __complex_log(__z.__rep()); }
1088 template<typename _Tp>
1090 log(const complex<_Tp>& __z) { return __complex_log(__z); }
1093 template<typename _Tp>
1095 log10(const complex<_Tp>& __z)
1096 { return std::log(__z) / log(_Tp(10.0)); }
1098 // 26.2.8/10 sin(__z): Returns the sine of __z.
1099 template<typename _Tp>
1101 __complex_sin(const complex<_Tp>& __z)
1103 const _Tp __x = __z.real();
1104 const _Tp __y = __z.imag();
1105 return complex<_Tp>(sin(__x) * cosh(__y), cos(__x) * sinh(__y));
1108 #if _GLIBCXX_USE_C99_COMPLEX
1109 inline __complex__ float
1110 __complex_sin(__complex__ float __z) { return __builtin_csinf(__z); }
1112 inline __complex__ double
1113 __complex_sin(__complex__ double __z) { return __builtin_csin(__z); }
1115 inline __complex__ long double
1116 __complex_sin(const __complex__ long double& __z)
1117 { return __builtin_csinl(__z); }
1119 template<typename _Tp>
1121 sin(const complex<_Tp>& __z) { return __complex_sin(__z.__rep()); }
1123 template<typename _Tp>
1125 sin(const complex<_Tp>& __z) { return __complex_sin(__z); }
1128 // 26.2.8/11 sinh(__z): Returns the hyperbolic sine of __z.
1129 template<typename _Tp>
1131 __complex_sinh(const complex<_Tp>& __z)
1133 const _Tp __x = __z.real();
1134 const _Tp __y = __z.imag();
1135 return complex<_Tp>(sinh(__x) * cos(__y), cosh(__x) * sin(__y));
1138 #if _GLIBCXX_USE_C99_COMPLEX
1139 inline __complex__ float
1140 __complex_sinh(__complex__ float __z) { return __builtin_csinhf(__z); }
1142 inline __complex__ double
1143 __complex_sinh(__complex__ double __z) { return __builtin_csinh(__z); }
1145 inline __complex__ long double
1146 __complex_sinh(const __complex__ long double& __z)
1147 { return __builtin_csinhl(__z); }
1149 template<typename _Tp>
1151 sinh(const complex<_Tp>& __z) { return __complex_sinh(__z.__rep()); }
1153 template<typename _Tp>
1155 sinh(const complex<_Tp>& __z) { return __complex_sinh(__z); }
1158 // 26.2.8/13 sqrt(__z): Returns the complex square root of __z.
1159 // The branch cut is on the negative axis.
1160 template<typename _Tp>
1162 __complex_sqrt(const complex<_Tp>& __z)
1164 _Tp __x = __z.real();
1165 _Tp __y = __z.imag();
1169 _Tp __t = sqrt(abs(__y) / 2);
1170 return complex<_Tp>(__t, __y < _Tp() ? -__t : __t);
1174 _Tp __t = sqrt(2 * (std::abs(__z) + abs(__x)));
1177 ? complex<_Tp>(__u, __y / __t)
1178 : complex<_Tp>(abs(__y) / __t, __y < _Tp() ? -__u : __u);
1182 #if _GLIBCXX_USE_C99_COMPLEX
1183 inline __complex__ float
1184 __complex_sqrt(__complex__ float __z) { return __builtin_csqrtf(__z); }
1186 inline __complex__ double
1187 __complex_sqrt(__complex__ double __z) { return __builtin_csqrt(__z); }
1189 inline __complex__ long double
1190 __complex_sqrt(const __complex__ long double& __z)
1191 { return __builtin_csqrtl(__z); }
1193 template<typename _Tp>
1195 sqrt(const complex<_Tp>& __z) { return __complex_sqrt(__z.__rep()); }
1197 template<typename _Tp>
1199 sqrt(const complex<_Tp>& __z) { return __complex_sqrt(__z); }
1202 // 26.2.8/14 tan(__z): Return the complex tangent of __z.
1204 template<typename _Tp>
1206 __complex_tan(const complex<_Tp>& __z)
1207 { return std::sin(__z) / std::cos(__z); }
1209 #if _GLIBCXX_USE_C99_COMPLEX
1210 inline __complex__ float
1211 __complex_tan(__complex__ float __z) { return __builtin_ctanf(__z); }
1213 inline __complex__ double
1214 __complex_tan(__complex__ double __z) { return __builtin_ctan(__z); }
1216 inline __complex__ long double
1217 __complex_tan(const __complex__ long double& __z)
1218 { return __builtin_ctanl(__z); }
1220 template<typename _Tp>
1222 tan(const complex<_Tp>& __z) { return __complex_tan(__z.__rep()); }
1224 template<typename _Tp>
1226 tan(const complex<_Tp>& __z) { return __complex_tan(__z); }
1230 // 26.2.8/15 tanh(__z): Returns the hyperbolic tangent of __z.
1232 template<typename _Tp>
1234 __complex_tanh(const complex<_Tp>& __z)
1235 { return std::sinh(__z) / std::cosh(__z); }
1237 #if _GLIBCXX_USE_C99_COMPLEX
1238 inline __complex__ float
1239 __complex_tanh(__complex__ float __z) { return __builtin_ctanhf(__z); }
1241 inline __complex__ double
1242 __complex_tanh(__complex__ double __z) { return __builtin_ctanh(__z); }
1244 inline __complex__ long double
1245 __complex_tanh(const __complex__ long double& __z)
1246 { return __builtin_ctanhl(__z); }
1248 template<typename _Tp>
1250 tanh(const complex<_Tp>& __z) { return __complex_tanh(__z.__rep()); }
1252 template<typename _Tp>
1254 tanh(const complex<_Tp>& __z) { return __complex_tanh(__z); }
1258 // 26.2.8/9 pow(__x, __y): Returns the complex power base of __x
1259 // raised to the __y-th power. The branch
1260 // cut is on the negative axis.
1261 template<typename _Tp>
1263 __complex_pow_unsigned(complex<_Tp> __x, unsigned __n)
1265 complex<_Tp> __y = __n % 2 ? __x : complex<_Tp>(1);
1277 // In C++11 mode we used to implement the resolution of
1278 // DR 844. complex pow return type is ambiguous.
1279 // thus the following overload was disabled in that mode. However, doing
1280 // that causes all sorts of issues, see, for example:
1281 // http://gcc.gnu.org/ml/libstdc++/2013-01/msg00058.html
1282 // and also PR57974.
1283 template<typename _Tp>
1285 pow(const complex<_Tp>& __z, int __n)
1288 ? complex<_Tp>(1) / std::__complex_pow_unsigned(__z, -(unsigned)__n)
1289 : std::__complex_pow_unsigned(__z, __n);
1292 template<typename _Tp>
1294 pow(const complex<_Tp>& __x, const _Tp& __y)
1296 #if ! _GLIBCXX_USE_C99_COMPLEX
1300 if (__x.imag() == _Tp() && __x.real() > _Tp())
1301 return pow(__x.real(), __y);
1303 complex<_Tp> __t = std::log(__x);
1304 return std::polar<_Tp>(exp(__y * __t.real()), __y * __t.imag());
1307 template<typename _Tp>
1309 __complex_pow(const complex<_Tp>& __x, const complex<_Tp>& __y)
1310 { return __x == _Tp() ? _Tp() : std::exp(__y * std::log(__x)); }
1312 #if _GLIBCXX_USE_C99_COMPLEX
1313 inline __complex__ float
1314 __complex_pow(__complex__ float __x, __complex__ float __y)
1315 { return __builtin_cpowf(__x, __y); }
1317 inline __complex__ double
1318 __complex_pow(__complex__ double __x, __complex__ double __y)
1319 { return __builtin_cpow(__x, __y); }
1321 inline __complex__ long double
1322 __complex_pow(const __complex__ long double& __x,
1323 const __complex__ long double& __y)
1324 { return __builtin_cpowl(__x, __y); }
1326 template<typename _Tp>
1328 pow(const complex<_Tp>& __x, const complex<_Tp>& __y)
1329 { return __complex_pow(__x.__rep(), __y.__rep()); }
1331 template<typename _Tp>
1333 pow(const complex<_Tp>& __x, const complex<_Tp>& __y)
1334 { return __complex_pow(__x, __y); }
1337 template<typename _Tp>
1339 pow(const _Tp& __x, const complex<_Tp>& __y)
1341 return __x > _Tp() ? std::polar<_Tp>(pow(__x, __y.real()),
1342 __y.imag() * log(__x))
1343 : std::pow(complex<_Tp>(__x), __y);
1346 /// 26.2.3 complex specializations
1347 /// complex<float> specialization
1349 class complex<float>
1352 typedef float value_type;
1353 typedef __complex__ float _ComplexT;
1355 _GLIBCXX_CONSTEXPR complex(_ComplexT __z) : _M_value(__z) { }
1357 _GLIBCXX_CONSTEXPR complex(float __r = 0.0f, float __i = 0.0f)
1358 #if __cplusplus >= 201103L
1359 : _M_value{ __r, __i } { }
1362 __real__ _M_value = __r;
1363 __imag__ _M_value = __i;
1367 #if __cplusplus >= 201103L
1368 _GLIBCXX14_CONSTEXPR complex(const complex&) = default;
1371 #if __cplusplus > 202002L
1372 template<typename _Up>
1373 explicit(!requires(_Up __u) { value_type{__u}; })
1374 constexpr complex(const complex<_Up>& __z)
1375 : _M_value{ value_type(__z.real()), value_type(__z.imag()) } { }
1377 explicit _GLIBCXX_CONSTEXPR complex(const complex<double>&);
1378 explicit _GLIBCXX_CONSTEXPR complex(const complex<long double>&);
1381 #if __cplusplus >= 201103L
1382 // _GLIBCXX_RESOLVE_LIB_DEFECTS
1383 // DR 387. std::complex over-encapsulated.
1384 __attribute ((__abi_tag__ ("cxx11")))
1386 real() const { return __real__ _M_value; }
1388 __attribute ((__abi_tag__ ("cxx11")))
1390 imag() const { return __imag__ _M_value; }
1393 real() { return __real__ _M_value; }
1396 real() const { return __real__ _M_value; }
1399 imag() { return __imag__ _M_value; }
1402 imag() const { return __imag__ _M_value; }
1405 // _GLIBCXX_RESOLVE_LIB_DEFECTS
1406 // DR 387. std::complex over-encapsulated.
1407 _GLIBCXX20_CONSTEXPR void
1408 real(float __val) { __real__ _M_value = __val; }
1410 _GLIBCXX20_CONSTEXPR void
1411 imag(float __val) { __imag__ _M_value = __val; }
1413 _GLIBCXX20_CONSTEXPR complex&
1414 operator=(float __f)
1420 _GLIBCXX20_CONSTEXPR complex&
1421 operator+=(float __f)
1427 _GLIBCXX20_CONSTEXPR complex&
1428 operator-=(float __f)
1434 _GLIBCXX20_CONSTEXPR complex&
1435 operator*=(float __f)
1441 _GLIBCXX20_CONSTEXPR complex&
1442 operator/=(float __f)
1448 // Let the compiler synthesize the copy and assignment
1449 // operator. It always does a pretty good job.
1450 #if __cplusplus >= 201103L
1451 _GLIBCXX14_CONSTEXPR complex& operator=(const complex&) = default;
1454 template<typename _Tp>
1455 _GLIBCXX20_CONSTEXPR complex&
1456 operator=(const complex<_Tp>& __z)
1458 __real__ _M_value = __z.real();
1459 __imag__ _M_value = __z.imag();
1463 template<typename _Tp>
1464 _GLIBCXX20_CONSTEXPR complex&
1465 operator+=(const complex<_Tp>& __z)
1467 _M_value += __z.__rep();
1472 _GLIBCXX20_CONSTEXPR complex&
1473 operator-=(const complex<_Tp>& __z)
1475 _M_value -= __z.__rep();
1480 _GLIBCXX20_CONSTEXPR complex&
1481 operator*=(const complex<_Tp>& __z)
1483 const _ComplexT __t = __z.__rep();
1489 _GLIBCXX20_CONSTEXPR complex&
1490 operator/=(const complex<_Tp>& __z)
1492 const _ComplexT __t = __z.__rep();
1497 _GLIBCXX_CONSTEXPR _ComplexT __rep() const { return _M_value; }
1503 /// 26.2.3 complex specializations
1504 /// complex<double> specialization
1506 class complex<double>
1509 typedef double value_type;
1510 typedef __complex__ double _ComplexT;
1512 _GLIBCXX_CONSTEXPR complex(_ComplexT __z) : _M_value(__z) { }
1514 _GLIBCXX_CONSTEXPR complex(double __r = 0.0, double __i = 0.0)
1515 #if __cplusplus >= 201103L
1516 : _M_value{ __r, __i } { }
1519 __real__ _M_value = __r;
1520 __imag__ _M_value = __i;
1524 #if __cplusplus >= 201103L
1525 _GLIBCXX14_CONSTEXPR complex(const complex&) = default;
1528 #if __cplusplus > 202002L
1529 template<typename _Up>
1530 explicit(!requires(_Up __u) { value_type{__u}; })
1531 constexpr complex(const complex<_Up>& __z)
1532 : _M_value{ value_type(__z.real()), value_type(__z.imag()) } { }
1534 _GLIBCXX_CONSTEXPR complex(const complex<float>& __z)
1535 : _M_value(__z.__rep()) { }
1537 explicit _GLIBCXX_CONSTEXPR complex(const complex<long double>&);
1540 #if __cplusplus >= 201103L
1541 // _GLIBCXX_RESOLVE_LIB_DEFECTS
1542 // DR 387. std::complex over-encapsulated.
1543 __attribute ((__abi_tag__ ("cxx11")))
1545 real() const { return __real__ _M_value; }
1547 __attribute ((__abi_tag__ ("cxx11")))
1549 imag() const { return __imag__ _M_value; }
1552 real() { return __real__ _M_value; }
1555 real() const { return __real__ _M_value; }
1558 imag() { return __imag__ _M_value; }
1561 imag() const { return __imag__ _M_value; }
1564 // _GLIBCXX_RESOLVE_LIB_DEFECTS
1565 // DR 387. std::complex over-encapsulated.
1566 _GLIBCXX20_CONSTEXPR void
1567 real(double __val) { __real__ _M_value = __val; }
1569 _GLIBCXX20_CONSTEXPR void
1570 imag(double __val) { __imag__ _M_value = __val; }
1572 _GLIBCXX20_CONSTEXPR complex&
1573 operator=(double __d)
1579 _GLIBCXX20_CONSTEXPR complex&
1580 operator+=(double __d)
1586 _GLIBCXX20_CONSTEXPR complex&
1587 operator-=(double __d)
1593 _GLIBCXX20_CONSTEXPR complex&
1594 operator*=(double __d)
1600 _GLIBCXX20_CONSTEXPR complex&
1601 operator/=(double __d)
1607 // The compiler will synthesize this, efficiently.
1608 #if __cplusplus >= 201103L
1609 _GLIBCXX14_CONSTEXPR complex& operator=(const complex&) = default;
1612 template<typename _Tp>
1613 _GLIBCXX20_CONSTEXPR complex&
1614 operator=(const complex<_Tp>& __z)
1616 _M_value = __z.__rep();
1620 template<typename _Tp>
1621 _GLIBCXX20_CONSTEXPR complex&
1622 operator+=(const complex<_Tp>& __z)
1624 _M_value += __z.__rep();
1628 template<typename _Tp>
1629 _GLIBCXX20_CONSTEXPR complex&
1630 operator-=(const complex<_Tp>& __z)
1632 _M_value -= __z.__rep();
1636 template<typename _Tp>
1637 _GLIBCXX20_CONSTEXPR complex&
1638 operator*=(const complex<_Tp>& __z)
1640 const _ComplexT __t = __z.__rep();
1645 template<typename _Tp>
1646 _GLIBCXX20_CONSTEXPR complex&
1647 operator/=(const complex<_Tp>& __z)
1649 const _ComplexT __t = __z.__rep();
1654 _GLIBCXX_CONSTEXPR _ComplexT __rep() const { return _M_value; }
1660 /// 26.2.3 complex specializations
1661 /// complex<long double> specialization
1663 class complex<long double>
1666 typedef long double value_type;
1667 typedef __complex__ long double _ComplexT;
1669 _GLIBCXX_CONSTEXPR complex(_ComplexT __z) : _M_value(__z) { }
1671 _GLIBCXX_CONSTEXPR complex(long double __r = 0.0L,
1672 long double __i = 0.0L)
1673 #if __cplusplus >= 201103L
1674 : _M_value{ __r, __i } { }
1677 __real__ _M_value = __r;
1678 __imag__ _M_value = __i;
1682 #if __cplusplus >= 201103L
1683 _GLIBCXX14_CONSTEXPR complex(const complex&) = default;
1686 #if __cplusplus > 202002L
1687 template<typename _Up>
1688 explicit(!requires(_Up __u) { value_type{__u}; })
1689 constexpr complex(const complex<_Up>& __z)
1690 : _M_value{ value_type(__z.real()), value_type(__z.imag()) } { }
1692 _GLIBCXX_CONSTEXPR complex(const complex<float>& __z)
1693 : _M_value(__z.__rep()) { }
1695 _GLIBCXX_CONSTEXPR complex(const complex<double>& __z)
1696 : _M_value(__z.__rep()) { }
1699 #if __cplusplus >= 201103L
1700 // _GLIBCXX_RESOLVE_LIB_DEFECTS
1701 // DR 387. std::complex over-encapsulated.
1702 __attribute ((__abi_tag__ ("cxx11")))
1703 constexpr long double
1704 real() const { return __real__ _M_value; }
1706 __attribute ((__abi_tag__ ("cxx11")))
1707 constexpr long double
1708 imag() const { return __imag__ _M_value; }
1711 real() { return __real__ _M_value; }
1714 real() const { return __real__ _M_value; }
1717 imag() { return __imag__ _M_value; }
1720 imag() const { return __imag__ _M_value; }
1723 // _GLIBCXX_RESOLVE_LIB_DEFECTS
1724 // DR 387. std::complex over-encapsulated.
1725 _GLIBCXX20_CONSTEXPR void
1726 real(long double __val) { __real__ _M_value = __val; }
1728 _GLIBCXX20_CONSTEXPR void
1729 imag(long double __val) { __imag__ _M_value = __val; }
1731 _GLIBCXX20_CONSTEXPR complex&
1732 operator=(long double __r)
1738 _GLIBCXX20_CONSTEXPR complex&
1739 operator+=(long double __r)
1745 _GLIBCXX20_CONSTEXPR complex&
1746 operator-=(long double __r)
1752 _GLIBCXX20_CONSTEXPR complex&
1753 operator*=(long double __r)
1759 _GLIBCXX20_CONSTEXPR complex&
1760 operator/=(long double __r)
1766 // The compiler knows how to do this efficiently
1767 #if __cplusplus >= 201103L
1768 _GLIBCXX14_CONSTEXPR complex& operator=(const complex&) = default;
1771 template<typename _Tp>
1772 _GLIBCXX20_CONSTEXPR complex&
1773 operator=(const complex<_Tp>& __z)
1775 _M_value = __z.__rep();
1779 template<typename _Tp>
1780 _GLIBCXX20_CONSTEXPR complex&
1781 operator+=(const complex<_Tp>& __z)
1783 _M_value += __z.__rep();
1787 template<typename _Tp>
1788 _GLIBCXX20_CONSTEXPR complex&
1789 operator-=(const complex<_Tp>& __z)
1791 _M_value -= __z.__rep();
1795 template<typename _Tp>
1796 _GLIBCXX20_CONSTEXPR complex&
1797 operator*=(const complex<_Tp>& __z)
1799 const _ComplexT __t = __z.__rep();
1804 template<typename _Tp>
1805 _GLIBCXX20_CONSTEXPR complex&
1806 operator/=(const complex<_Tp>& __z)
1808 const _ComplexT __t = __z.__rep();
1813 _GLIBCXX_CONSTEXPR _ComplexT __rep() const { return _M_value; }
1819 #if __cplusplus > 202002L
1820 template<typename _Tp>
1821 struct __complex_type
1824 #ifdef __STDCPP_FLOAT16_T__
1826 struct __complex_type<_Float16>
1827 { typedef __complex__ _Float16 type; };
1830 #ifdef __STDCPP_FLOAT32_T__
1832 struct __complex_type<_Float32>
1833 { typedef __complex__ _Float32 type; };
1836 #ifdef __STDCPP_FLOAT64_T__
1838 struct __complex_type<_Float64>
1839 { typedef __complex__ _Float64 type; };
1842 #ifdef __STDCPP_FLOAT128_T__
1844 struct __complex_type<_Float128>
1845 { typedef __complex__ _Float128 type; };
1848 #ifdef __STDCPP_BFLOAT16_T__
1850 struct __complex_type<__gnu_cxx::__bfloat16_t>
1851 { typedef __complex__ decltype(0.0bf16) type; };
1854 template<typename _Tp>
1855 requires requires { typename __complex_type<_Tp>::type; }
1859 typedef _Tp value_type;
1860 typedef typename std::__complex_type<_Tp>::type _ComplexT;
1862 constexpr complex(_ComplexT __z) : _M_value(__z) { }
1864 constexpr complex(_Tp __r = _Tp(), _Tp __i = _Tp())
1865 : _M_value{ __r, __i } { }
1867 template<typename _Up>
1868 explicit(!requires(_Up __u) { value_type{__u}; })
1869 constexpr complex(const complex<_Up>& __z)
1870 : _M_value{ value_type(__z.real()), value_type(__z.imag()) } { }
1873 real() const { return __real__ _M_value; }
1876 imag() const { return __imag__ _M_value; }
1879 real(_Tp __val) { __real__ _M_value = __val; }
1882 imag(_Tp __val) { __imag__ _M_value = __val; }
1919 // Let the compiler synthesize the copy and assignment
1920 // operator. It always does a pretty good job.
1921 constexpr complex(const complex&) = default;
1922 constexpr complex& operator=(const complex&) = default;
1924 template<typename _Up>
1926 operator=(const complex<_Up>& __z)
1928 __real__ _M_value = __z.real();
1929 __imag__ _M_value = __z.imag();
1933 template<typename _Up>
1935 operator+=(const complex<_Up>& __z)
1937 _M_value += __z.__rep();
1943 operator-=(const complex<_Up>& __z)
1945 _M_value -= __z.__rep();
1951 operator*=(const complex<_Up>& __z)
1953 const _ComplexT __t = __z.__rep();
1960 operator/=(const complex<_Up>& __z)
1962 const _ComplexT __t = __z.__rep();
1967 constexpr _ComplexT __rep() const { return _M_value; }
1974 #if __cplusplus <= 202002L
1975 // These bits have to be at the end of this file, so that the
1976 // specializations have all been defined.
1977 inline _GLIBCXX_CONSTEXPR
1978 complex<float>::complex(const complex<double>& __z)
1979 : _M_value(__z.__rep()) { }
1981 inline _GLIBCXX_CONSTEXPR
1982 complex<float>::complex(const complex<long double>& __z)
1983 : _M_value(__z.__rep()) { }
1985 inline _GLIBCXX_CONSTEXPR
1986 complex<double>::complex(const complex<long double>& __z)
1987 : _M_value(__z.__rep()) { }
1990 // Inhibit implicit instantiations for required instantiations,
1991 // which are defined via explicit instantiations elsewhere.
1992 // NB: This syntax is a GNU extension.
1993 #if _GLIBCXX_EXTERN_TEMPLATE
1994 extern template istream& operator>>(istream&, complex<float>&);
1995 extern template ostream& operator<<(ostream&, const complex<float>&);
1996 extern template istream& operator>>(istream&, complex<double>&);
1997 extern template ostream& operator<<(ostream&, const complex<double>&);
1998 extern template istream& operator>>(istream&, complex<long double>&);
1999 extern template ostream& operator<<(ostream&, const complex<long double>&);
2001 #ifdef _GLIBCXX_USE_WCHAR_T
2002 extern template wistream& operator>>(wistream&, complex<float>&);
2003 extern template wostream& operator<<(wostream&, const complex<float>&);
2004 extern template wistream& operator>>(wistream&, complex<double>&);
2005 extern template wostream& operator<<(wostream&, const complex<double>&);
2006 extern template wistream& operator>>(wistream&, complex<long double>&);
2007 extern template wostream& operator<<(wostream&, const complex<long double>&);
2011 /// @} group complex_numbers
2013 _GLIBCXX_END_NAMESPACE_VERSION
2016 #if __cplusplus >= 201103L
2018 namespace std _GLIBCXX_VISIBILITY(default)
2020 _GLIBCXX_BEGIN_NAMESPACE_VERSION
2022 // Forward declarations.
2023 template<typename _Tp> std::complex<_Tp> acos(const std::complex<_Tp>&);
2024 template<typename _Tp> std::complex<_Tp> asin(const std::complex<_Tp>&);
2025 template<typename _Tp> std::complex<_Tp> atan(const std::complex<_Tp>&);
2027 template<typename _Tp> std::complex<_Tp> acosh(const std::complex<_Tp>&);
2028 template<typename _Tp> std::complex<_Tp> asinh(const std::complex<_Tp>&);
2029 template<typename _Tp> std::complex<_Tp> atanh(const std::complex<_Tp>&);
2031 template<typename _Tp> _Tp fabs(const std::complex<_Tp>&);
2033 template<typename _Tp>
2034 inline std::complex<_Tp>
2035 __complex_acos(const std::complex<_Tp>& __z)
2037 const std::complex<_Tp> __t = std::asin(__z);
2038 const _Tp __pi_2 = 1.5707963267948966192313216916397514L;
2039 return std::complex<_Tp>(__pi_2 - __t.real(), -__t.imag());
2042 #if _GLIBCXX_USE_C99_COMPLEX_ARC
2043 #if defined(__STDCPP_FLOAT16_T__) && defined(_GLIBCXX_FLOAT_IS_IEEE_BINARY32)
2044 inline __complex__ _Float16
2045 __complex_acos(__complex__ _Float16 __z)
2046 { return static_cast<__complex__ _Float16>(__builtin_cacosf(__z)); }
2048 inline __complex__ _Float16
2049 __complex_asin(__complex__ _Float16 __z)
2050 { return static_cast<__complex__ _Float16>(__builtin_casinf(__z)); }
2052 inline __complex__ _Float16
2053 __complex_atan(__complex__ _Float16 __z)
2054 { return static_cast<__complex__ _Float16>(__builtin_catanf(__z)); }
2056 inline __complex__ _Float16
2057 __complex_acosh(__complex__ _Float16 __z)
2058 { return static_cast<__complex__ _Float16>(__builtin_cacoshf(__z)); }
2060 inline __complex__ _Float16
2061 __complex_asinh(__complex__ _Float16 __z)
2062 { return static_cast<__complex__ _Float16>(__builtin_casinhf(__z)); }
2064 inline __complex__ _Float16
2065 __complex_atanh(__complex__ _Float16 __z)
2066 { return static_cast<__complex__ _Float16>(__builtin_catanhf(__z)); }
2069 #if defined(__STDCPP_FLOAT32_T__) && defined(_GLIBCXX_FLOAT_IS_IEEE_BINARY32)
2070 inline __complex__ _Float32
2071 __complex_acos(__complex__ _Float32 __z)
2072 { return __builtin_cacosf(__z); }
2074 inline __complex__ _Float32
2075 __complex_asin(__complex__ _Float32 __z)
2076 { return __builtin_casinf(__z); }
2078 inline __complex__ _Float32
2079 __complex_atan(__complex__ _Float32 __z)
2080 { return __builtin_catanf(__z); }
2082 inline __complex__ _Float32
2083 __complex_acosh(__complex__ _Float32 __z)
2084 { return __builtin_cacoshf(__z); }
2086 inline __complex__ _Float32
2087 __complex_asinh(__complex__ _Float32 __z)
2088 { return __builtin_casinhf(__z); }
2090 inline __complex__ _Float32
2091 __complex_atanh(__complex__ _Float32 __z)
2092 { return __builtin_catanhf(__z); }
2095 #if defined(__STDCPP_FLOAT64_T__) && defined(_GLIBCXX_DOUBLE_IS_IEEE_BINARY64)
2096 inline __complex__ _Float64
2097 __complex_acos(__complex__ _Float64 __z)
2098 { return __builtin_cacos(__z); }
2100 inline __complex__ _Float64
2101 __complex_asin(__complex__ _Float64 __z)
2102 { return __builtin_casin(__z); }
2104 inline __complex__ _Float64
2105 __complex_atan(__complex__ _Float64 __z)
2106 { return __builtin_catan(__z); }
2108 inline __complex__ _Float64
2109 __complex_acosh(__complex__ _Float64 __z)
2110 { return __builtin_cacosh(__z); }
2112 inline __complex__ _Float64
2113 __complex_asinh(__complex__ _Float64 __z)
2114 { return __builtin_casinh(__z); }
2116 inline __complex__ _Float64
2117 __complex_atanh(__complex__ _Float64 __z)
2118 { return __builtin_catanh(__z); }
2121 #if defined(__STDCPP_FLOAT128_T__) && defined(_GLIBCXX_LDOUBLE_IS_IEEE_BINARY128)
2122 inline __complex__ _Float128
2123 __complex_acos(__complex__ _Float128 __z)
2124 { return __builtin_cacosl(__z); }
2126 inline __complex__ _Float128
2127 __complex_asin(__complex__ _Float128 __z)
2128 { return __builtin_casinl(__z); }
2130 inline __complex__ _Float128
2131 __complex_atan(__complex__ _Float128 __z)
2132 { return __builtin_catanl(__z); }
2134 inline __complex__ _Float128
2135 __complex_acosh(__complex__ _Float128 __z)
2136 { return __builtin_cacoshl(__z); }
2138 inline __complex__ _Float128
2139 __complex_asinh(__complex__ _Float128 __z)
2140 { return __builtin_casinhl(__z); }
2142 inline __complex__ _Float128
2143 __complex_atanh(__complex__ _Float128 __z)
2144 { return __builtin_catanhl(__z); }
2145 #elif defined(__STDCPP_FLOAT128_T__) && defined(_GLIBCXX_HAVE_FLOAT128_MATH)
2146 inline __complex__ _Float128
2147 __complex_acos(__complex__ _Float128 __z)
2148 { return __builtin_cacosf128(__z); }
2150 inline __complex__ _Float128
2151 __complex_asin(__complex__ _Float128 __z)
2152 { return __builtin_casinf128(__z); }
2154 inline __complex__ _Float128
2155 __complex_atan(__complex__ _Float128 __z)
2156 { return __builtin_catanf128(__z); }
2158 inline __complex__ _Float128
2159 __complex_acosh(__complex__ _Float128 __z)
2160 { return __builtin_cacoshf128(__z); }
2162 inline __complex__ _Float128
2163 __complex_asinh(__complex__ _Float128 __z)
2164 { return __builtin_casinhf128(__z); }
2166 inline __complex__ _Float128
2167 __complex_atanh(__complex__ _Float128 __z)
2168 { return __builtin_catanhf128(__z); }
2171 #if defined(__STDCPP_BFLOAT16_T__) && defined(_GLIBCXX_FLOAT_IS_IEEE_BINARY32)
2172 inline __complex__ decltype(0.0bf16)
2173 __complex_acos(__complex__ decltype(0.0bf16) __z)
2174 { return static_cast<__complex__ decltype(0.0bf16)>(__builtin_cacosf(__z)); }
2176 inline __complex__ decltype(0.0bf16)
2177 __complex_asin(__complex__ decltype(0.0bf16) __z)
2178 { return static_cast<__complex__ decltype(0.0bf16)>(__builtin_casinf(__z)); }
2180 inline __complex__ decltype(0.0bf16)
2181 __complex_atan(__complex__ decltype(0.0bf16) __z)
2182 { return static_cast<__complex__ decltype(0.0bf16)>(__builtin_catanf(__z)); }
2184 inline __complex__ decltype(0.0bf16)
2185 __complex_acosh(__complex__ decltype(0.0bf16) __z)
2186 { return static_cast<__complex__ decltype(0.0bf16)>(__builtin_cacoshf(__z)); }
2188 inline __complex__ decltype(0.0bf16)
2189 __complex_asinh(__complex__ decltype(0.0bf16) __z)
2190 { return static_cast<__complex__ decltype(0.0bf16)>(__builtin_casinhf(__z)); }
2192 inline __complex__ decltype(0.0bf16)
2193 __complex_atanh(__complex__ decltype(0.0bf16) __z)
2194 { return static_cast<__complex__ decltype(0.0bf16)>(__builtin_catanhf(__z)); }
2198 #if _GLIBCXX_USE_C99_COMPLEX_ARC
2199 inline __complex__ float
2200 __complex_acos(__complex__ float __z)
2201 { return __builtin_cacosf(__z); }
2203 inline __complex__ double
2204 __complex_acos(__complex__ double __z)
2205 { return __builtin_cacos(__z); }
2207 inline __complex__ long double
2208 __complex_acos(const __complex__ long double& __z)
2209 { return __builtin_cacosl(__z); }
2211 template<typename _Tp>
2212 inline std::complex<_Tp>
2213 acos(const std::complex<_Tp>& __z)
2214 { return __complex_acos(__z.__rep()); }
2216 /// acos(__z) [8.1.2].
2217 // Effects: Behaves the same as C99 function cacos, defined
2218 // in subclause 7.3.5.1.
2219 template<typename _Tp>
2220 inline std::complex<_Tp>
2221 acos(const std::complex<_Tp>& __z)
2222 { return __complex_acos(__z); }
2225 template<typename _Tp>
2226 inline std::complex<_Tp>
2227 __complex_asin(const std::complex<_Tp>& __z)
2229 std::complex<_Tp> __t(-__z.imag(), __z.real());
2230 __t = std::asinh(__t);
2231 return std::complex<_Tp>(__t.imag(), -__t.real());
2234 #if _GLIBCXX_USE_C99_COMPLEX_ARC
2235 inline __complex__ float
2236 __complex_asin(__complex__ float __z)
2237 { return __builtin_casinf(__z); }
2239 inline __complex__ double
2240 __complex_asin(__complex__ double __z)
2241 { return __builtin_casin(__z); }
2243 inline __complex__ long double
2244 __complex_asin(const __complex__ long double& __z)
2245 { return __builtin_casinl(__z); }
2247 template<typename _Tp>
2248 inline std::complex<_Tp>
2249 asin(const std::complex<_Tp>& __z)
2250 { return __complex_asin(__z.__rep()); }
2252 /// asin(__z) [8.1.3].
2253 // Effects: Behaves the same as C99 function casin, defined
2254 // in subclause 7.3.5.2.
2255 template<typename _Tp>
2256 inline std::complex<_Tp>
2257 asin(const std::complex<_Tp>& __z)
2258 { return __complex_asin(__z); }
2261 template<typename _Tp>
2263 __complex_atan(const std::complex<_Tp>& __z)
2265 const _Tp __r2 = __z.real() * __z.real();
2266 const _Tp __x = _Tp(1.0) - __r2 - __z.imag() * __z.imag();
2268 _Tp __num = __z.imag() + _Tp(1.0);
2269 _Tp __den = __z.imag() - _Tp(1.0);
2271 __num = __r2 + __num * __num;
2272 __den = __r2 + __den * __den;
2274 return std::complex<_Tp>(_Tp(0.5) * atan2(_Tp(2.0) * __z.real(), __x),
2275 _Tp(0.25) * log(__num / __den));
2278 #if _GLIBCXX_USE_C99_COMPLEX_ARC
2279 inline __complex__ float
2280 __complex_atan(__complex__ float __z)
2281 { return __builtin_catanf(__z); }
2283 inline __complex__ double
2284 __complex_atan(__complex__ double __z)
2285 { return __builtin_catan(__z); }
2287 inline __complex__ long double
2288 __complex_atan(const __complex__ long double& __z)
2289 { return __builtin_catanl(__z); }
2291 template<typename _Tp>
2292 inline std::complex<_Tp>
2293 atan(const std::complex<_Tp>& __z)
2294 { return __complex_atan(__z.__rep()); }
2296 /// atan(__z) [8.1.4].
2297 // Effects: Behaves the same as C99 function catan, defined
2298 // in subclause 7.3.5.3.
2299 template<typename _Tp>
2300 inline std::complex<_Tp>
2301 atan(const std::complex<_Tp>& __z)
2302 { return __complex_atan(__z); }
2305 template<typename _Tp>
2307 __complex_acosh(const std::complex<_Tp>& __z)
2310 return _Tp(2.0) * std::log(std::sqrt(_Tp(0.5) * (__z + _Tp(1.0)))
2311 + std::sqrt(_Tp(0.5) * (__z - _Tp(1.0))));
2314 #if _GLIBCXX_USE_C99_COMPLEX_ARC
2315 inline __complex__ float
2316 __complex_acosh(__complex__ float __z)
2317 { return __builtin_cacoshf(__z); }
2319 inline __complex__ double
2320 __complex_acosh(__complex__ double __z)
2321 { return __builtin_cacosh(__z); }
2323 inline __complex__ long double
2324 __complex_acosh(const __complex__ long double& __z)
2325 { return __builtin_cacoshl(__z); }
2327 template<typename _Tp>
2328 inline std::complex<_Tp>
2329 acosh(const std::complex<_Tp>& __z)
2330 { return __complex_acosh(__z.__rep()); }
2332 /// acosh(__z) [8.1.5].
2333 // Effects: Behaves the same as C99 function cacosh, defined
2334 // in subclause 7.3.6.1.
2335 template<typename _Tp>
2336 inline std::complex<_Tp>
2337 acosh(const std::complex<_Tp>& __z)
2338 { return __complex_acosh(__z); }
2341 template<typename _Tp>
2343 __complex_asinh(const std::complex<_Tp>& __z)
2345 std::complex<_Tp> __t((__z.real() - __z.imag())
2346 * (__z.real() + __z.imag()) + _Tp(1.0),
2347 _Tp(2.0) * __z.real() * __z.imag());
2348 __t = std::sqrt(__t);
2350 return std::log(__t + __z);
2353 #if _GLIBCXX_USE_C99_COMPLEX_ARC
2354 inline __complex__ float
2355 __complex_asinh(__complex__ float __z)
2356 { return __builtin_casinhf(__z); }
2358 inline __complex__ double
2359 __complex_asinh(__complex__ double __z)
2360 { return __builtin_casinh(__z); }
2362 inline __complex__ long double
2363 __complex_asinh(const __complex__ long double& __z)
2364 { return __builtin_casinhl(__z); }
2366 template<typename _Tp>
2367 inline std::complex<_Tp>
2368 asinh(const std::complex<_Tp>& __z)
2369 { return __complex_asinh(__z.__rep()); }
2371 /// asinh(__z) [8.1.6].
2372 // Effects: Behaves the same as C99 function casin, defined
2373 // in subclause 7.3.6.2.
2374 template<typename _Tp>
2375 inline std::complex<_Tp>
2376 asinh(const std::complex<_Tp>& __z)
2377 { return __complex_asinh(__z); }
2380 template<typename _Tp>
2382 __complex_atanh(const std::complex<_Tp>& __z)
2384 const _Tp __i2 = __z.imag() * __z.imag();
2385 const _Tp __x = _Tp(1.0) - __i2 - __z.real() * __z.real();
2387 _Tp __num = _Tp(1.0) + __z.real();
2388 _Tp __den = _Tp(1.0) - __z.real();
2390 __num = __i2 + __num * __num;
2391 __den = __i2 + __den * __den;
2393 return std::complex<_Tp>(_Tp(0.25) * (log(__num) - log(__den)),
2394 _Tp(0.5) * atan2(_Tp(2.0) * __z.imag(), __x));
2397 #if _GLIBCXX_USE_C99_COMPLEX_ARC
2398 inline __complex__ float
2399 __complex_atanh(__complex__ float __z)
2400 { return __builtin_catanhf(__z); }
2402 inline __complex__ double
2403 __complex_atanh(__complex__ double __z)
2404 { return __builtin_catanh(__z); }
2406 inline __complex__ long double
2407 __complex_atanh(const __complex__ long double& __z)
2408 { return __builtin_catanhl(__z); }
2410 template<typename _Tp>
2411 inline std::complex<_Tp>
2412 atanh(const std::complex<_Tp>& __z)
2413 { return __complex_atanh(__z.__rep()); }
2415 /// atanh(__z) [8.1.7].
2416 // Effects: Behaves the same as C99 function catanh, defined
2417 // in subclause 7.3.6.3.
2418 template<typename _Tp>
2419 inline std::complex<_Tp>
2420 atanh(const std::complex<_Tp>& __z)
2421 { return __complex_atanh(__z); }
2424 template<typename _Tp>
2426 /// fabs(__z) [8.1.8].
2427 // Effects: Behaves the same as C99 function cabs, defined
2428 // in subclause 7.3.8.1.
2429 fabs(const std::complex<_Tp>& __z)
2430 { return std::abs(__z); }
2432 /// Additional overloads [8.1.9].
2433 template<typename _Tp>
2434 inline typename __gnu_cxx::__promote<_Tp>::__type
2437 typedef typename __gnu_cxx::__promote<_Tp>::__type __type;
2438 #if (_GLIBCXX11_USE_C99_MATH && !_GLIBCXX_USE_C99_FP_MACROS_DYNAMIC)
2439 return std::signbit(__x) ? __type(3.1415926535897932384626433832795029L)
2442 return std::arg(std::complex<__type>(__x));
2446 template<typename _Tp>
2447 _GLIBCXX_CONSTEXPR inline typename __gnu_cxx::__promote<_Tp>::__type
2451 template<typename _Tp>
2452 _GLIBCXX20_CONSTEXPR inline typename __gnu_cxx::__promote<_Tp>::__type
2455 typedef typename __gnu_cxx::__promote<_Tp>::__type __type;
2456 return __type(__x) * __type(__x);
2459 template<typename _Tp>
2460 _GLIBCXX_CONSTEXPR inline typename __gnu_cxx::__promote<_Tp>::__type
2464 template<typename _Tp, typename _Up>
2465 inline std::complex<typename __gnu_cxx::__promote_2<_Tp, _Up>::__type>
2466 pow(const std::complex<_Tp>& __x, const _Up& __y)
2468 typedef typename __gnu_cxx::__promote_2<_Tp, _Up>::__type __type;
2469 return std::pow(std::complex<__type>(__x), __type(__y));
2472 template<typename _Tp, typename _Up>
2473 inline std::complex<typename __gnu_cxx::__promote_2<_Tp, _Up>::__type>
2474 pow(const _Tp& __x, const std::complex<_Up>& __y)
2476 typedef typename __gnu_cxx::__promote_2<_Tp, _Up>::__type __type;
2477 return std::pow(__type(__x), std::complex<__type>(__y));
2480 template<typename _Tp, typename _Up>
2481 inline std::complex<typename __gnu_cxx::__promote_2<_Tp, _Up>::__type>
2482 pow(const std::complex<_Tp>& __x, const std::complex<_Up>& __y)
2484 typedef typename __gnu_cxx::__promote_2<_Tp, _Up>::__type __type;
2485 return std::pow(std::complex<__type>(__x),
2486 std::complex<__type>(__y));
2489 // Forward declarations.
2491 template<typename _Tp>
2492 std::complex<_Tp> proj(const std::complex<_Tp>&);
2494 // Generic implementation of std::proj, does not work for infinities.
2495 template<typename _Tp>
2496 inline std::complex<_Tp>
2497 __complex_proj(const std::complex<_Tp>& __z)
2500 #if _GLIBCXX_USE_C99_COMPLEX
2501 inline complex<float>
2502 __complex_proj(const complex<float>& __z)
2503 { return __builtin_cprojf(__z.__rep()); }
2505 inline complex<double>
2506 __complex_proj(const complex<double>& __z)
2507 { return __builtin_cproj(__z.__rep()); }
2509 inline complex<long double>
2510 __complex_proj(const complex<long double>& __z)
2511 { return __builtin_cprojl(__z.__rep()); }
2513 #if __cplusplus > 202002L
2514 #if defined(__STDCPP_FLOAT16_T__) && defined(_GLIBCXX_FLOAT_IS_IEEE_BINARY32)
2515 inline __complex__ _Float16
2516 __complex_proj(__complex__ _Float16 __z)
2517 { return static_cast<__complex__ _Float16>(__builtin_cprojf(__z)); }
2520 #if defined(__STDCPP_FLOAT32_T__) && defined(_GLIBCXX_FLOAT_IS_IEEE_BINARY32)
2521 inline __complex__ _Float32
2522 __complex_proj(__complex__ _Float32 __z)
2523 { return __builtin_cprojf(__z); }
2526 #if defined(__STDCPP_FLOAT64_T__) && defined(_GLIBCXX_DOUBLE_IS_IEEE_BINARY64)
2527 inline __complex__ _Float64
2528 __complex_proj(__complex__ _Float64 __z)
2529 { return __builtin_cproj(__z); }
2532 #if defined(__STDCPP_FLOAT128_T__) && defined(_GLIBCXX_LDOUBLE_IS_IEEE_BINARY128)
2533 inline __complex__ _Float128
2534 __complex_proj(__complex__ _Float128 __z)
2535 { return __builtin_cprojl(__z); }
2536 #elif defined(__STDCPP_FLOAT128_T__) && defined(_GLIBCXX_HAVE_FLOAT128_MATH)
2537 inline __complex__ _Float128
2538 __complex_proj(__complex__ _Float128 __z)
2539 { return __builtin_cprojf128(__z); }
2542 #if defined(__STDCPP_BFLOAT16_T__) && defined(_GLIBCXX_FLOAT_IS_IEEE_BINARY32)
2543 inline __complex__ decltype(0.0bf16)
2544 __complex_proj(__complex__ decltype(0.0bf16) __z)
2545 { return static_cast<__complex__ decltype(0.0bf16)>(__builtin_cprojf(__z)); }
2548 template<typename _Tp>
2549 requires requires { typename __complex_type<_Tp>::type; }
2551 __complex_proj(const complex<_Tp>& __z)
2552 { return __complex_proj(__z.__rep()); }
2555 #elif defined _GLIBCXX_USE_C99_MATH_FUNCS
2556 inline complex<float>
2557 __complex_proj(const complex<float>& __z)
2559 if (__builtin_isinf(__z.real()) || __builtin_isinf(__z.imag()))
2560 return complex<float>(__builtin_inff(),
2561 __builtin_copysignf(0.0f, __z.imag()));
2565 inline complex<double>
2566 __complex_proj(const complex<double>& __z)
2568 if (__builtin_isinf(__z.real()) || __builtin_isinf(__z.imag()))
2569 return complex<double>(__builtin_inf(),
2570 __builtin_copysign(0.0, __z.imag()));
2574 inline complex<long double>
2575 __complex_proj(const complex<long double>& __z)
2577 if (__builtin_isinf(__z.real()) || __builtin_isinf(__z.imag()))
2578 return complex<long double>(__builtin_infl(),
2579 __builtin_copysignl(0.0l, __z.imag()));
2584 template<typename _Tp>
2585 inline std::complex<_Tp>
2586 proj(const std::complex<_Tp>& __z)
2587 { return __complex_proj(__z); }
2589 // Overload for scalars
2590 template<typename _Tp>
2591 inline std::complex<typename __gnu_cxx::__promote<_Tp>::__type>
2594 typedef typename __gnu_cxx::__promote<_Tp>::__type __type;
2595 return std::proj(std::complex<__type>(__x));
2598 template<typename _Tp>
2599 inline _GLIBCXX20_CONSTEXPR
2600 std::complex<typename __gnu_cxx::__promote<_Tp>::__type>
2603 typedef typename __gnu_cxx::__promote<_Tp>::__type __type;
2604 return std::complex<__type>(__x, -__type());
2607 #ifdef __cpp_lib_complex_udls // C++ >= 14
2609 inline namespace literals {
2610 inline namespace complex_literals {
2611 #pragma GCC diagnostic push
2612 #pragma GCC diagnostic ignored "-Wliteral-suffix"
2614 constexpr std::complex<float>
2615 operator""if(long double __num)
2616 { return std::complex<float>{0.0F, static_cast<float>(__num)}; }
2618 constexpr std::complex<float>
2619 operator""if(unsigned long long __num)
2620 { return std::complex<float>{0.0F, static_cast<float>(__num)}; }
2622 constexpr std::complex<double>
2623 operator""i(long double __num)
2624 { return std::complex<double>{0.0, static_cast<double>(__num)}; }
2626 constexpr std::complex<double>
2627 operator""i(unsigned long long __num)
2628 { return std::complex<double>{0.0, static_cast<double>(__num)}; }
2630 constexpr std::complex<long double>
2631 operator""il(long double __num)
2632 { return std::complex<long double>{0.0L, __num}; }
2634 constexpr std::complex<long double>
2635 operator""il(unsigned long long __num)
2636 { return std::complex<long double>{0.0L, static_cast<long double>(__num)}; }
2638 #pragma GCC diagnostic pop
2639 } // inline namespace complex_literals
2640 } // inline namespace literals
2642 #endif // __cpp_lib_complex_udls
2644 _GLIBCXX_END_NAMESPACE_VERSION
2650 #pragma clang diagnostic pop
2653 #endif /* _GLIBCXX_COMPLEX */