1// The template and inlines for the -*- C++ -*- complex number classes.
3// Copyright (C) 1997-2025 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
40#pragma GCC system_header
43#pragma GCC diagnostic push
44#pragma GCC diagnostic ignored "-Wc++11-extensions" // extern template
46#include <bits/c++config.h>
47#include <bits/cpp_type_traits.h>
48#include <ext/type_traits.h>
52// Get rid of a macro possibly defined in <complex.h>
56#pragma clang diagnostic push
57#pragma clang diagnostic ignored "-Wc99-extensions"
60#define __glibcxx_want_constexpr_complex
61#define __glibcxx_want_complex_udls
62#define __glibcxx_want_tuple_like
63#include <bits/version.h>
65#if __glibcxx_tuple_like >= 202311 // >= C++26
66# include <bits/utility.h> // for tuple_element_t
67# include <bits/stl_pair.h> // for __is_tuple_like_v
70namespace std _GLIBCXX_VISIBILITY(default)
72_GLIBCXX_BEGIN_NAMESPACE_VERSION
75 * @defgroup complex_numbers Complex Numbers
78 * Classes and functions for complex numbers.
82 // Forward declarations.
83 template<typename _Tp> class complex;
84 template<> class complex<float>;
85 template<> class complex<double>;
86 template<> class complex<long double>;
88 /// Return magnitude of @a z.
89 template<typename _Tp> _Tp abs(const complex<_Tp>&);
90 /// Return phase angle of @a z.
91 template<typename _Tp> _Tp arg(const complex<_Tp>&);
92 /// Return @a z magnitude squared.
93 template<typename _Tp> _Tp _GLIBCXX20_CONSTEXPR norm(const complex<_Tp>&);
95 /// Return complex conjugate of @a z.
96 template<typename _Tp>
97 _GLIBCXX20_CONSTEXPR complex<_Tp> conj(const complex<_Tp>&);
98 /// Return complex with magnitude @a rho and angle @a theta.
99 template<typename _Tp> complex<_Tp> polar(const _Tp&, const _Tp& = _Tp(0));
102 /// Return complex cosine of @a z.
103 template<typename _Tp> complex<_Tp> cos(const complex<_Tp>&);
104 /// Return complex hyperbolic cosine of @a z.
105 template<typename _Tp> complex<_Tp> cosh(const complex<_Tp>&);
106 /// Return complex base e exponential of @a z.
107 template<typename _Tp> complex<_Tp> exp(const complex<_Tp>&);
108 /// Return complex natural logarithm of @a z.
109 template<typename _Tp> complex<_Tp> log(const complex<_Tp>&);
110 /// Return complex base 10 logarithm of @a z.
111 template<typename _Tp> complex<_Tp> log10(const complex<_Tp>&);
112 /// Return @a x to the @a y'th power.
113 template<typename _Tp> complex<_Tp> pow(const complex<_Tp>&, int);
114 /// Return @a x to the @a y'th power.
115 template<typename _Tp> complex<_Tp> pow(const complex<_Tp>&, const _Tp&);
116 /// Return @a x to the @a y'th power.
117 template<typename _Tp> complex<_Tp> pow(const complex<_Tp>&,
118 const complex<_Tp>&);
119 /// Return @a x to the @a y'th power.
120 template<typename _Tp> complex<_Tp> pow(const _Tp&, const complex<_Tp>&);
121 /// Return complex sine of @a z.
122 template<typename _Tp> complex<_Tp> sin(const complex<_Tp>&);
123 /// Return complex hyperbolic sine of @a z.
124 template<typename _Tp> complex<_Tp> sinh(const complex<_Tp>&);
125 /// Return complex square root of @a z.
126 template<typename _Tp> complex<_Tp> sqrt(const complex<_Tp>&);
127 /// Return complex tangent of @a z.
128 template<typename _Tp> complex<_Tp> tan(const complex<_Tp>&);
129 /// Return complex hyperbolic tangent of @a z.
130 template<typename _Tp> complex<_Tp> tanh(const complex<_Tp>&);
132#if __glibcxx_tuple_like >= 202311L // >= C++26
133 template<typename _Tp>
134 struct tuple_size<complex<_Tp>>
135 : public integral_constant<size_t, 2> { };
136 template<typename _Tp>
137 struct tuple_element<0, complex<_Tp>>
138 { using type = _Tp; };
139 template<typename _Tp>
140 struct tuple_element<1, complex<_Tp>>
141 { using type = _Tp; };
142 template<typename _Tp>
143 inline constexpr bool __is_tuple_like_v<complex<_Tp>> = true;
144#endif // __glibcxx_tuple_like >= 202311
146 // 26.2.2 Primary template class complex
148 * Template to represent complex numbers.
150 * Specializations for float, double, and long double are part of the
151 * library. Results with any other type are not guaranteed.
153 * @param Tp Type of real and imaginary values.
155 template<typename _Tp>
160 typedef _Tp value_type;
162 /// Default constructor. First parameter is x, second parameter is y.
163 /// Unspecified parameters default to 0.
164 _GLIBCXX_CONSTEXPR complex(const _Tp& __r = _Tp(), const _Tp& __i = _Tp())
165 : _M_real(__r), _M_imag(__i) { }
167 // Let the compiler synthesize the copy constructor
168#if __cplusplus >= 201103L
169 constexpr complex(const complex&) = default;
172 /// Converting constructor.
173 template<typename _Up>
174#if __cplusplus > 202002L
175 explicit(!requires(_Up __u) { _Tp{__u}; })
177 _GLIBCXX_CONSTEXPR complex(const complex<_Up>& __z)
178 : _M_real(_Tp(__z.real())), _M_imag(_Tp(__z.imag())) { }
180#if __cplusplus >= 201103L
181 // _GLIBCXX_RESOLVE_LIB_DEFECTS
182 // DR 387. std::complex over-encapsulated.
183 _GLIBCXX_ABI_TAG_CXX11
185 real() const { return _M_real; }
187 _GLIBCXX_ABI_TAG_CXX11
189 imag() const { return _M_imag; }
191 /// Return real part of complex number.
193 real() { return _M_real; }
195 /// Return real part of complex number.
197 real() const { return _M_real; }
199 /// Return imaginary part of complex number.
201 imag() { return _M_imag; }
203 /// Return imaginary part of complex number.
205 imag() const { return _M_imag; }
208 // _GLIBCXX_RESOLVE_LIB_DEFECTS
209 // DR 387. std::complex over-encapsulated.
210 _GLIBCXX20_CONSTEXPR void
211 real(_Tp __val) { _M_real = __val; }
213 _GLIBCXX20_CONSTEXPR void
214 imag(_Tp __val) { _M_imag = __val; }
216 /// Assign a scalar to this complex number.
217 _GLIBCXX20_CONSTEXPR complex<_Tp>& operator=(const _Tp&);
219 /// Add a scalar to this complex number.
221 _GLIBCXX20_CONSTEXPR complex<_Tp>&
222 operator+=(const _Tp& __t)
228 /// Subtract a scalar from this complex number.
230 _GLIBCXX20_CONSTEXPR complex<_Tp>&
231 operator-=(const _Tp& __t)
237 /// Multiply this complex number by a scalar.
238 _GLIBCXX20_CONSTEXPR complex<_Tp>& operator*=(const _Tp&);
239 /// Divide this complex number by a scalar.
240 _GLIBCXX20_CONSTEXPR complex<_Tp>& operator/=(const _Tp&);
242 // Let the compiler synthesize the copy assignment operator
243#if __cplusplus >= 201103L
244 _GLIBCXX20_CONSTEXPR complex& operator=(const complex&) = default;
247 /// Assign another complex number to this one.
248 template<typename _Up>
249 _GLIBCXX20_CONSTEXPR complex<_Tp>& operator=(const complex<_Up>&);
250 /// Add another complex number to this one.
251 template<typename _Up>
252 _GLIBCXX20_CONSTEXPR complex<_Tp>& operator+=(const complex<_Up>&);
253 /// Subtract another complex number from this one.
254 template<typename _Up>
255 _GLIBCXX20_CONSTEXPR complex<_Tp>& operator-=(const complex<_Up>&);
256 /// Multiply this complex number by another.
257 template<typename _Up>
258 _GLIBCXX20_CONSTEXPR complex<_Tp>& operator*=(const complex<_Up>&);
259 /// Divide this complex number by another.
260 template<typename _Up>
261 _GLIBCXX20_CONSTEXPR complex<_Tp>& operator/=(const complex<_Up>&);
263 _GLIBCXX_CONSTEXPR complex __rep() const
266#if __glibcxx_tuple_like >= 202311L // >= C++26
267 template<typename _Cp>
268 [[__gnu__::__always_inline__]]
270 __get_part(this _Cp& __z, size_t __i) noexcept
272 return __i == 0 ? __z._M_real : __z._M_imag;
281 template<typename _Tp>
282 _GLIBCXX20_CONSTEXPR complex<_Tp>&
283 complex<_Tp>::operator=(const _Tp& __t)
291 template<typename _Tp>
292 _GLIBCXX20_CONSTEXPR complex<_Tp>&
293 complex<_Tp>::operator*=(const _Tp& __t)
301 template<typename _Tp>
302 _GLIBCXX20_CONSTEXPR complex<_Tp>&
303 complex<_Tp>::operator/=(const _Tp& __t)
310 template<typename _Tp>
311 template<typename _Up>
312 _GLIBCXX20_CONSTEXPR complex<_Tp>&
313 complex<_Tp>::operator=(const complex<_Up>& __z)
315 _M_real = __z.real();
316 _M_imag = __z.imag();
321 template<typename _Tp>
322 template<typename _Up>
323 _GLIBCXX20_CONSTEXPR complex<_Tp>&
324 complex<_Tp>::operator+=(const complex<_Up>& __z)
326 _M_real += __z.real();
327 _M_imag += __z.imag();
332 template<typename _Tp>
333 template<typename _Up>
334 _GLIBCXX20_CONSTEXPR complex<_Tp>&
335 complex<_Tp>::operator-=(const complex<_Up>& __z)
337 _M_real -= __z.real();
338 _M_imag -= __z.imag();
343 // XXX: This is a grammar school implementation.
344 template<typename _Tp>
345 template<typename _Up>
346 _GLIBCXX20_CONSTEXPR complex<_Tp>&
347 complex<_Tp>::operator*=(const complex<_Up>& __z)
349 const _Tp __r = _M_real * __z.real() - _M_imag * __z.imag();
350 _M_imag = _M_real * __z.imag() + _M_imag * __z.real();
356 // XXX: This is a grammar school implementation.
357 template<typename _Tp>
358 template<typename _Up>
359 _GLIBCXX20_CONSTEXPR complex<_Tp>&
360 complex<_Tp>::operator/=(const complex<_Up>& __z)
362 const _Tp __r = _M_real * __z.real() + _M_imag * __z.imag();
363 const _Tp __n = std::norm(__z);
364 _M_imag = (_M_imag * __z.real() - _M_real * __z.imag()) / __n;
371 /// Return new complex value @a x plus @a y.
372 template<typename _Tp>
373 inline _GLIBCXX20_CONSTEXPR complex<_Tp>
374 operator+(const complex<_Tp>& __x, const complex<_Tp>& __y)
376 complex<_Tp> __r = __x;
381 template<typename _Tp>
382 inline _GLIBCXX20_CONSTEXPR complex<_Tp>
383 operator+(const complex<_Tp>& __x, const _Tp& __y)
385 complex<_Tp> __r = __x;
390 template<typename _Tp>
391 inline _GLIBCXX20_CONSTEXPR complex<_Tp>
392 operator+(const _Tp& __x, const complex<_Tp>& __y)
394 complex<_Tp> __r = __y;
401 /// Return new complex value @a x minus @a y.
402 template<typename _Tp>
403 inline _GLIBCXX20_CONSTEXPR complex<_Tp>
404 operator-(const complex<_Tp>& __x, const complex<_Tp>& __y)
406 complex<_Tp> __r = __x;
411 template<typename _Tp>
412 inline _GLIBCXX20_CONSTEXPR complex<_Tp>
413 operator-(const complex<_Tp>& __x, const _Tp& __y)
415 complex<_Tp> __r = __x;
420 template<typename _Tp>
421 inline _GLIBCXX20_CONSTEXPR complex<_Tp>
422 operator-(const _Tp& __x, const complex<_Tp>& __y)
424 complex<_Tp> __r = -__y;
431 /// Return new complex value @a x times @a y.
432 template<typename _Tp>
433 inline _GLIBCXX20_CONSTEXPR complex<_Tp>
434 operator*(const complex<_Tp>& __x, const complex<_Tp>& __y)
436 complex<_Tp> __r = __x;
441 template<typename _Tp>
442 inline _GLIBCXX20_CONSTEXPR complex<_Tp>
443 operator*(const complex<_Tp>& __x, const _Tp& __y)
445 complex<_Tp> __r = __x;
450 template<typename _Tp>
451 inline _GLIBCXX20_CONSTEXPR complex<_Tp>
452 operator*(const _Tp& __x, const complex<_Tp>& __y)
454 complex<_Tp> __r = __y;
461 /// Return new complex value @a x divided by @a y.
462 template<typename _Tp>
463 inline _GLIBCXX20_CONSTEXPR complex<_Tp>
464 operator/(const complex<_Tp>& __x, const complex<_Tp>& __y)
466 complex<_Tp> __r = __x;
471 template<typename _Tp>
472 inline _GLIBCXX20_CONSTEXPR complex<_Tp>
473 operator/(const complex<_Tp>& __x, const _Tp& __y)
475 complex<_Tp> __r = __x;
480 template<typename _Tp>
481 inline _GLIBCXX20_CONSTEXPR complex<_Tp>
482 operator/(const _Tp& __x, const complex<_Tp>& __y)
484 complex<_Tp> __r = __x;
491 template<typename _Tp>
492 inline _GLIBCXX20_CONSTEXPR complex<_Tp>
493 operator+(const complex<_Tp>& __x)
496 /// Return complex negation of @a x.
497 template<typename _Tp>
498 inline _GLIBCXX20_CONSTEXPR complex<_Tp>
499 operator-(const complex<_Tp>& __x)
500 { return complex<_Tp>(-__x.real(), -__x.imag()); }
503 /// Return true if @a x is equal to @a y.
504 template<typename _Tp>
505 inline _GLIBCXX_CONSTEXPR bool
506 operator==(const complex<_Tp>& __x, const complex<_Tp>& __y)
507 { return __x.real() == __y.real() && __x.imag() == __y.imag(); }
509 template<typename _Tp>
510 inline _GLIBCXX_CONSTEXPR bool
511 operator==(const complex<_Tp>& __x, const _Tp& __y)
512 { return __x.real() == __y && __x.imag() == _Tp(); }
514#if !(__cpp_impl_three_way_comparison >= 201907L)
515 template<typename _Tp>
516 inline _GLIBCXX_CONSTEXPR bool
517 operator==(const _Tp& __x, const complex<_Tp>& __y)
518 { return __x == __y.real() && _Tp() == __y.imag(); }
522 /// Return false if @a x is equal to @a y.
523 template<typename _Tp>
524 inline _GLIBCXX_CONSTEXPR bool
525 operator!=(const complex<_Tp>& __x, const complex<_Tp>& __y)
526 { return __x.real() != __y.real() || __x.imag() != __y.imag(); }
528 template<typename _Tp>
529 inline _GLIBCXX_CONSTEXPR bool
530 operator!=(const complex<_Tp>& __x, const _Tp& __y)
531 { return __x.real() != __y || __x.imag() != _Tp(); }
533 template<typename _Tp>
534 inline _GLIBCXX_CONSTEXPR bool
535 operator!=(const _Tp& __x, const complex<_Tp>& __y)
536 { return __x != __y.real() || _Tp() != __y.imag(); }
540 /// Extraction operator for complex values.
541 template<typename _Tp, typename _CharT, class _Traits>
542 basic_istream<_CharT, _Traits>&
543 operator>>(basic_istream<_CharT, _Traits>& __is, complex<_Tp>& __x)
549 if (_Traits::eq(__ch, __is.widen('(')))
552 if (__is >> __u >> __ch)
554 const _CharT __rparen = __is.widen(')');
555 if (_Traits::eq(__ch, __rparen))
560 else if (_Traits::eq(__ch, __is.widen(',')))
563 if (__is >> __v >> __ch)
565 if (_Traits::eq(__ch, __rparen))
567 __x = complex<_Tp>(__u, __v);
590 __is.setstate(ios_base::failbit);
594 /// Insertion operator for complex values.
595 template<typename _Tp, typename _CharT, class _Traits>
596 basic_ostream<_CharT, _Traits>&
597 operator<<(basic_ostream<_CharT, _Traits>& __os, const complex<_Tp>& __x)
599 basic_ostringstream<_CharT, _Traits> __s;
600 __s.flags(__os.flags());
601 __s.imbue(__os.getloc());
602 __s.precision(__os.precision());
603 __s << '(' << __x.real() << ',' << __x.imag() << ')';
604 return __os << __s.str();
608#if __cplusplus >= 201103L
609 template<typename _Tp>
611 real(const complex<_Tp>& __z)
612 { return __z.real(); }
614 template<typename _Tp>
616 imag(const complex<_Tp>& __z)
617 { return __z.imag(); }
619 template<typename _Tp>
621 real(complex<_Tp>& __z)
622 { return __z.real(); }
624 template<typename _Tp>
626 real(const complex<_Tp>& __z)
627 { return __z.real(); }
629 template<typename _Tp>
631 imag(complex<_Tp>& __z)
632 { return __z.imag(); }
634 template<typename _Tp>
636 imag(const complex<_Tp>& __z)
637 { return __z.imag(); }
640#if __glibcxx_tuple_like >= 202311L // >= C++26
641 template<size_t _Int, typename _Tp>
642 [[nodiscard,__gnu__::__always_inline__]]
644 get(complex<_Tp>& __z) noexcept
646 static_assert(_Int < 2);
647 return __z.__get_part(_Int);
650 template<size_t _Int, typename _Tp>
651 [[nodiscard,__gnu__::__always_inline__]]
653 get(complex<_Tp>&& __z) noexcept
655 static_assert(_Int < 2);
656 return std::move(__z.__get_part(_Int));
659 template<size_t _Int, typename _Tp>
660 [[nodiscard,__gnu__::__always_inline__]]
662 get(const complex<_Tp>& __z) noexcept
664 static_assert(_Int < 2);
665 return __z.__get_part(_Int);
668 template<size_t _Int, typename _Tp>
669 [[nodiscard,__gnu__::__always_inline__]]
670 constexpr const _Tp&&
671 get(const complex<_Tp>&& __z) noexcept
673 static_assert(_Int < 2);
674 return std::move(__z.__get_part(_Int));
676#endif // __glibcxx_tuple_like >= 202311
678#if _GLIBCXX_USE_C99_COMPLEX
679#if defined(__STDCPP_FLOAT16_T__) && defined(_GLIBCXX_FLOAT_IS_IEEE_BINARY32)
681 __complex_abs(__complex__ _Float16 __z)
682 { return _Float16(__builtin_cabsf(__z)); }
685 __complex_arg(__complex__ _Float16 __z)
686 { return _Float16(__builtin_cargf(__z)); }
688 inline __complex__ _Float16
689 __complex_cos(__complex__ _Float16 __z)
690 { return static_cast<__complex__ _Float16>(__builtin_ccosf(__z)); }
692 inline __complex__ _Float16
693 __complex_cosh(__complex__ _Float16 __z)
694 { return static_cast<__complex__ _Float16>(__builtin_ccoshf(__z)); }
696 inline __complex__ _Float16
697 __complex_exp(__complex__ _Float16 __z)
698 { return static_cast<__complex__ _Float16>(__builtin_cexpf(__z)); }
700 inline __complex__ _Float16
701 __complex_log(__complex__ _Float16 __z)
702 { return static_cast<__complex__ _Float16>(__builtin_clogf(__z)); }
704 inline __complex__ _Float16
705 __complex_sin(__complex__ _Float16 __z)
706 { return static_cast<__complex__ _Float16>(__builtin_csinf(__z)); }
708 inline __complex__ _Float16
709 __complex_sinh(__complex__ _Float16 __z)
710 { return static_cast<__complex__ _Float16>(__builtin_csinhf(__z)); }
712 inline __complex__ _Float16
713 __complex_sqrt(__complex__ _Float16 __z)
714 { return static_cast<__complex__ _Float16>(__builtin_csqrtf(__z)); }
716 inline __complex__ _Float16
717 __complex_tan(__complex__ _Float16 __z)
718 { return static_cast<__complex__ _Float16>(__builtin_ctanf(__z)); }
720 inline __complex__ _Float16
721 __complex_tanh(__complex__ _Float16 __z)
722 { return static_cast<__complex__ _Float16>(__builtin_ctanhf(__z)); }
724 inline __complex__ _Float16
725 __complex_pow(__complex__ _Float16 __x, __complex__ _Float16 __y)
726 { return static_cast<__complex__ _Float16>(__builtin_cpowf(__x, __y)); }
729#if defined(__STDCPP_FLOAT32_T__) && defined(_GLIBCXX_FLOAT_IS_IEEE_BINARY32)
731 __complex_abs(__complex__ _Float32 __z) { return __builtin_cabsf(__z); }
734 __complex_arg(__complex__ _Float32 __z) { return __builtin_cargf(__z); }
736 inline __complex__ _Float32
737 __complex_cos(__complex__ _Float32 __z) { return __builtin_ccosf(__z); }
739 inline __complex__ _Float32
740 __complex_cosh(__complex__ _Float32 __z) { return __builtin_ccoshf(__z); }
742 inline __complex__ _Float32
743 __complex_exp(__complex__ _Float32 __z) { return __builtin_cexpf(__z); }
745 inline __complex__ _Float32
746 __complex_log(__complex__ _Float32 __z) { return __builtin_clogf(__z); }
748 inline __complex__ _Float32
749 __complex_sin(__complex__ _Float32 __z) { return __builtin_csinf(__z); }
751 inline __complex__ _Float32
752 __complex_sinh(__complex__ _Float32 __z) { return __builtin_csinhf(__z); }
754 inline __complex__ _Float32
755 __complex_sqrt(__complex__ _Float32 __z) { return __builtin_csqrtf(__z); }
757 inline __complex__ _Float32
758 __complex_tan(__complex__ _Float32 __z) { return __builtin_ctanf(__z); }
760 inline __complex__ _Float32
761 __complex_tanh(__complex__ _Float32 __z) { return __builtin_ctanhf(__z); }
763 inline __complex__ _Float32
764 __complex_pow(__complex__ _Float32 __x, __complex__ _Float32 __y)
765 { return __builtin_cpowf(__x, __y); }
768#if defined(__STDCPP_FLOAT64_T__) && defined(_GLIBCXX_DOUBLE_IS_IEEE_BINARY64)
770 __complex_abs(__complex__ _Float64 __z) { return __builtin_cabs(__z); }
773 __complex_arg(__complex__ _Float64 __z) { return __builtin_carg(__z); }
775 inline __complex__ _Float64
776 __complex_cos(__complex__ _Float64 __z) { return __builtin_ccos(__z); }
778 inline __complex__ _Float64
779 __complex_cosh(__complex__ _Float64 __z) { return __builtin_ccosh(__z); }
781 inline __complex__ _Float64
782 __complex_exp(__complex__ _Float64 __z) { return __builtin_cexp(__z); }
784 inline __complex__ _Float64
785 __complex_log(__complex__ _Float64 __z) { return __builtin_clog(__z); }
787 inline __complex__ _Float64
788 __complex_sin(__complex__ _Float64 __z) { return __builtin_csin(__z); }
790 inline __complex__ _Float64
791 __complex_sinh(__complex__ _Float64 __z) { return __builtin_csinh(__z); }
793 inline __complex__ _Float64
794 __complex_sqrt(__complex__ _Float64 __z) { return __builtin_csqrt(__z); }
796 inline __complex__ _Float64
797 __complex_tan(__complex__ _Float64 __z) { return __builtin_ctan(__z); }
799 inline __complex__ _Float64
800 __complex_tanh(__complex__ _Float64 __z) { return __builtin_ctanh(__z); }
802 inline __complex__ _Float64
803 __complex_pow(__complex__ _Float64 __x, __complex__ _Float64 __y)
804 { return __builtin_cpow(__x, __y); }
807#if defined(__STDCPP_FLOAT128_T__) && defined(_GLIBCXX_LDOUBLE_IS_IEEE_BINARY128)
809 __complex_abs(__complex__ _Float128 __z) { return __builtin_cabsl(__z); }
812 __complex_arg(__complex__ _Float128 __z) { return __builtin_cargl(__z); }
814 inline __complex__ _Float128
815 __complex_cos(__complex__ _Float128 __z) { return __builtin_ccosl(__z); }
817 inline __complex__ _Float128
818 __complex_cosh(__complex__ _Float128 __z) { return __builtin_ccoshl(__z); }
820 inline __complex__ _Float128
821 __complex_exp(__complex__ _Float128 __z) { return __builtin_cexpl(__z); }
823 inline __complex__ _Float128
824 __complex_log(__complex__ _Float128 __z) { return __builtin_clogl(__z); }
826 inline __complex__ _Float128
827 __complex_sin(__complex__ _Float128 __z) { return __builtin_csinl(__z); }
829 inline __complex__ _Float128
830 __complex_sinh(__complex__ _Float128 __z) { return __builtin_csinhl(__z); }
832 inline __complex__ _Float128
833 __complex_sqrt(__complex__ _Float128 __z) { return __builtin_csqrtl(__z); }
835 inline __complex__ _Float128
836 __complex_tan(__complex__ _Float128 __z) { return __builtin_ctanl(__z); }
838 inline __complex__ _Float128
839 __complex_tanh(__complex__ _Float128 __z) { return __builtin_ctanhl(__z); }
841 inline __complex__ _Float128
842 __complex_pow(__complex__ _Float128 __x, __complex__ _Float128 __y)
843 { return __builtin_cpowl(__x, __y); }
844#elif defined(__STDCPP_FLOAT128_T__) && defined(_GLIBCXX_HAVE_FLOAT128_MATH)
846 __complex_abs(__complex__ _Float128 __z) { return __builtin_cabsf128(__z); }
849 __complex_arg(__complex__ _Float128 __z) { return __builtin_cargf128(__z); }
851 inline __complex__ _Float128
852 __complex_cos(__complex__ _Float128 __z) { return __builtin_ccosf128(__z); }
854 inline __complex__ _Float128
855 __complex_cosh(__complex__ _Float128 __z) { return __builtin_ccoshf128(__z); }
857 inline __complex__ _Float128
858 __complex_exp(__complex__ _Float128 __z) { return __builtin_cexpf128(__z); }
860 inline __complex__ _Float128
861 __complex_log(__complex__ _Float128 __z) { return __builtin_clogf128(__z); }
863 inline __complex__ _Float128
864 __complex_sin(__complex__ _Float128 __z) { return __builtin_csinf128(__z); }
866 inline __complex__ _Float128
867 __complex_sinh(__complex__ _Float128 __z) { return __builtin_csinhf128(__z); }
869 inline __complex__ _Float128
870 __complex_sqrt(__complex__ _Float128 __z) { return __builtin_csqrtf128(__z); }
872 inline __complex__ _Float128
873 __complex_tan(__complex__ _Float128 __z) { return __builtin_ctanf128(__z); }
875 inline __complex__ _Float128
876 __complex_tanh(__complex__ _Float128 __z) { return __builtin_ctanhf128(__z); }
878 inline __complex__ _Float128
879 __complex_pow(__complex__ _Float128 __x, __complex__ _Float128 __y)
880 { return __builtin_cpowf128(__x, __y); }
883#if defined(__STDCPP_BFLOAT16_T__) && defined(_GLIBCXX_FLOAT_IS_IEEE_BINARY32)
884 inline __gnu_cxx::__bfloat16_t
885 __complex_abs(__complex__ decltype(0.0bf16) __z)
886 { return __gnu_cxx::__bfloat16_t(__builtin_cabsf(__z)); }
888 inline __gnu_cxx::__bfloat16_t
889 __complex_arg(__complex__ decltype(0.0bf16) __z)
890 { return __gnu_cxx::__bfloat16_t(__builtin_cargf(__z)); }
892 inline __complex__ decltype(0.0bf16)
893 __complex_cos(__complex__ decltype(0.0bf16) __z)
894 { return static_cast<__complex__ decltype(0.0bf16)>(__builtin_ccosf(__z)); }
896 inline __complex__ decltype(0.0bf16)
897 __complex_cosh(__complex__ decltype(0.0bf16) __z)
898 { return static_cast<__complex__ decltype(0.0bf16)>(__builtin_ccoshf(__z)); }
900 inline __complex__ decltype(0.0bf16)
901 __complex_exp(__complex__ decltype(0.0bf16) __z)
902 { return static_cast<__complex__ decltype(0.0bf16)>(__builtin_cexpf(__z)); }
904 inline __complex__ decltype(0.0bf16)
905 __complex_log(__complex__ decltype(0.0bf16) __z)
906 { return static_cast<__complex__ decltype(0.0bf16)>(__builtin_clogf(__z)); }
908 inline __complex__ decltype(0.0bf16)
909 __complex_sin(__complex__ decltype(0.0bf16) __z)
910 { return static_cast<__complex__ decltype(0.0bf16)>(__builtin_csinf(__z)); }
912 inline __complex__ decltype(0.0bf16)
913 __complex_sinh(__complex__ decltype(0.0bf16) __z)
914 { return static_cast<__complex__ decltype(0.0bf16)>(__builtin_csinhf(__z)); }
916 inline __complex__ decltype(0.0bf16)
917 __complex_sqrt(__complex__ decltype(0.0bf16) __z)
918 { return static_cast<__complex__ decltype(0.0bf16)>(__builtin_csqrtf(__z)); }
920 inline __complex__ decltype(0.0bf16)
921 __complex_tan(__complex__ decltype(0.0bf16) __z)
922 { return static_cast<__complex__ decltype(0.0bf16)>(__builtin_ctanf(__z)); }
924 inline __complex__ decltype(0.0bf16)
925 __complex_tanh(__complex__ decltype(0.0bf16) __z)
926 { return static_cast<__complex__ decltype(0.0bf16)>(__builtin_ctanhf(__z)); }
928 inline __complex__ decltype(0.0bf16)
929 __complex_pow(__complex__ decltype(0.0bf16) __x,
930 __complex__ decltype(0.0bf16) __y)
931 { return static_cast<__complex__ decltype(0.0bf16)>(__builtin_cpowf(__x,
936 // 26.2.7/3 abs(__z): Returns the magnitude of __z.
937 template<typename _Tp>
939 __complex_abs(const complex<_Tp>& __z)
941 _Tp __x = __z.real();
942 _Tp __y = __z.imag();
943 const _Tp __s = std::max(abs(__x), abs(__y));
944 if (__s == _Tp()) // well ...
948 return __s * sqrt(__x * __x + __y * __y);
951#if _GLIBCXX_USE_C99_COMPLEX
953 __complex_abs(__complex__ float __z) { return __builtin_cabsf(__z); }
956 __complex_abs(__complex__ double __z) { return __builtin_cabs(__z); }
959 __complex_abs(const __complex__ long double& __z)
960 { return __builtin_cabsl(__z); }
962 template<typename _Tp>
964 abs(const complex<_Tp>& __z) { return __complex_abs(__z.__rep()); }
966 template<typename _Tp>
968 abs(const complex<_Tp>& __z) { return __complex_abs(__z); }
972 // C++11 26.4.7 [complex.value.ops]/4: arg(z): Returns the phase angle of z.
973 template<typename _Tp>
975 __complex_arg(const complex<_Tp>& __z)
976 { return atan2(__z.imag(), __z.real()); }
978#if _GLIBCXX_USE_C99_COMPLEX
980 __complex_arg(__complex__ float __z) { return __builtin_cargf(__z); }
983 __complex_arg(__complex__ double __z) { return __builtin_carg(__z); }
986 __complex_arg(const __complex__ long double& __z)
987 { return __builtin_cargl(__z); }
989 template<typename _Tp>
991 arg(const complex<_Tp>& __z) { return __complex_arg(__z.__rep()); }
993 template<typename _Tp>
995 arg(const complex<_Tp>& __z) { return __complex_arg(__z); }
998 // 26.2.7/5: norm(__z) returns the squared magnitude of __z.
999 // As defined, norm() is -not- a norm is the common mathematical
1000 // sense used in numerics. The helper class _Norm_helper<> tries to
1001 // distinguish between builtin floating point and the rest, so as
1002 // to deliver an answer as close as possible to the real value.
1006 template<typename _Tp>
1007 static inline _GLIBCXX20_CONSTEXPR _Tp _S_do_it(const complex<_Tp>& __z)
1009 const _Tp __x = __z.real();
1010 const _Tp __y = __z.imag();
1011 return __x * __x + __y * __y;
1016 struct _Norm_helper<true>
1018 template<typename _Tp>
1019 static inline _GLIBCXX20_CONSTEXPR _Tp _S_do_it(const complex<_Tp>& __z)
1021 //_Tp __res = std::abs(__z);
1022 //return __res * __res;
1023 const _Tp __x = __z.real();
1024 const _Tp __y = __z.imag();
1025 return __x * __x + __y * __y;
1029 template<typename _Tp>
1030 inline _GLIBCXX20_CONSTEXPR _Tp
1031 norm(const complex<_Tp>& __z)
1033 return _Norm_helper<__is_floating<_Tp>::__value
1034 && !_GLIBCXX_FAST_MATH>::_S_do_it(__z);
1037 template<typename _Tp>
1039 polar(const _Tp& __rho, const _Tp& __theta)
1041 __glibcxx_assert( __rho >= _Tp(0) );
1042 return complex<_Tp>(__rho * cos(__theta), __rho * sin(__theta));
1045 template<typename _Tp>
1046 inline _GLIBCXX20_CONSTEXPR complex<_Tp>
1047 conj(const complex<_Tp>& __z)
1048 { return complex<_Tp>(__z.real(), -__z.imag()); }
1052 // 26.2.8/1 cos(__z): Returns the cosine of __z.
1053 template<typename _Tp>
1055 __complex_cos(const complex<_Tp>& __z)
1057 const _Tp __x = __z.real();
1058 const _Tp __y = __z.imag();
1059 return complex<_Tp>(cos(__x) * cosh(__y), -sin(__x) * sinh(__y));
1062#if _GLIBCXX_USE_C99_COMPLEX
1063 inline __complex__ float
1064 __complex_cos(__complex__ float __z) { return __builtin_ccosf(__z); }
1066 inline __complex__ double
1067 __complex_cos(__complex__ double __z) { return __builtin_ccos(__z); }
1069 inline __complex__ long double
1070 __complex_cos(const __complex__ long double& __z)
1071 { return __builtin_ccosl(__z); }
1073 template<typename _Tp>
1075 cos(const complex<_Tp>& __z) { return __complex_cos(__z.__rep()); }
1077 template<typename _Tp>
1079 cos(const complex<_Tp>& __z) { return __complex_cos(__z); }
1082 // 26.2.8/2 cosh(__z): Returns the hyperbolic cosine of __z.
1083 template<typename _Tp>
1085 __complex_cosh(const complex<_Tp>& __z)
1087 const _Tp __x = __z.real();
1088 const _Tp __y = __z.imag();
1089 return complex<_Tp>(cosh(__x) * cos(__y), sinh(__x) * sin(__y));
1092#if _GLIBCXX_USE_C99_COMPLEX
1093 inline __complex__ float
1094 __complex_cosh(__complex__ float __z) { return __builtin_ccoshf(__z); }
1096 inline __complex__ double
1097 __complex_cosh(__complex__ double __z) { return __builtin_ccosh(__z); }
1099 inline __complex__ long double
1100 __complex_cosh(const __complex__ long double& __z)
1101 { return __builtin_ccoshl(__z); }
1103 template<typename _Tp>
1105 cosh(const complex<_Tp>& __z) { return __complex_cosh(__z.__rep()); }
1107 template<typename _Tp>
1109 cosh(const complex<_Tp>& __z) { return __complex_cosh(__z); }
1112 // 26.2.8/3 exp(__z): Returns the complex base e exponential of x
1113 template<typename _Tp>
1115 __complex_exp(const complex<_Tp>& __z)
1116 { return std::polar<_Tp>(exp(__z.real()), __z.imag()); }
1118#if _GLIBCXX_USE_C99_COMPLEX
1119 inline __complex__ float
1120 __complex_exp(__complex__ float __z) { return __builtin_cexpf(__z); }
1122 inline __complex__ double
1123 __complex_exp(__complex__ double __z) { return __builtin_cexp(__z); }
1125 inline __complex__ long double
1126 __complex_exp(const __complex__ long double& __z)
1127 { return __builtin_cexpl(__z); }
1129 template<typename _Tp>
1131 exp(const complex<_Tp>& __z) { return __complex_exp(__z.__rep()); }
1133 template<typename _Tp>
1135 exp(const complex<_Tp>& __z) { return __complex_exp(__z); }
1138 // 26.2.8/5 log(__z): Returns the natural complex logarithm of __z.
1139 // The branch cut is along the negative axis.
1140 template<typename _Tp>
1142 __complex_log(const complex<_Tp>& __z)
1143 { return complex<_Tp>(log(std::abs(__z)), std::arg(__z)); }
1145#if _GLIBCXX_USE_C99_COMPLEX
1146 inline __complex__ float
1147 __complex_log(__complex__ float __z) { return __builtin_clogf(__z); }
1149 inline __complex__ double
1150 __complex_log(__complex__ double __z) { return __builtin_clog(__z); }
1152 inline __complex__ long double
1153 __complex_log(const __complex__ long double& __z)
1154 { return __builtin_clogl(__z); }
1156 template<typename _Tp>
1158 log(const complex<_Tp>& __z) { return __complex_log(__z.__rep()); }
1160 template<typename _Tp>
1162 log(const complex<_Tp>& __z) { return __complex_log(__z); }
1165 template<typename _Tp>
1167 log10(const complex<_Tp>& __z)
1168 { return std::log(__z) / log(_Tp(10.0)); }
1170 // 26.2.8/10 sin(__z): Returns the sine of __z.
1171 template<typename _Tp>
1173 __complex_sin(const complex<_Tp>& __z)
1175 const _Tp __x = __z.real();
1176 const _Tp __y = __z.imag();
1177 return complex<_Tp>(sin(__x) * cosh(__y), cos(__x) * sinh(__y));
1180#if _GLIBCXX_USE_C99_COMPLEX
1181 inline __complex__ float
1182 __complex_sin(__complex__ float __z) { return __builtin_csinf(__z); }
1184 inline __complex__ double
1185 __complex_sin(__complex__ double __z) { return __builtin_csin(__z); }
1187 inline __complex__ long double
1188 __complex_sin(const __complex__ long double& __z)
1189 { return __builtin_csinl(__z); }
1191 template<typename _Tp>
1193 sin(const complex<_Tp>& __z) { return __complex_sin(__z.__rep()); }
1195 template<typename _Tp>
1197 sin(const complex<_Tp>& __z) { return __complex_sin(__z); }
1200 // 26.2.8/11 sinh(__z): Returns the hyperbolic sine of __z.
1201 template<typename _Tp>
1203 __complex_sinh(const complex<_Tp>& __z)
1205 const _Tp __x = __z.real();
1206 const _Tp __y = __z.imag();
1207 return complex<_Tp>(sinh(__x) * cos(__y), cosh(__x) * sin(__y));
1210#if _GLIBCXX_USE_C99_COMPLEX
1211 inline __complex__ float
1212 __complex_sinh(__complex__ float __z) { return __builtin_csinhf(__z); }
1214 inline __complex__ double
1215 __complex_sinh(__complex__ double __z) { return __builtin_csinh(__z); }
1217 inline __complex__ long double
1218 __complex_sinh(const __complex__ long double& __z)
1219 { return __builtin_csinhl(__z); }
1221 template<typename _Tp>
1223 sinh(const complex<_Tp>& __z) { return __complex_sinh(__z.__rep()); }
1225 template<typename _Tp>
1227 sinh(const complex<_Tp>& __z) { return __complex_sinh(__z); }
1230 // 26.2.8/13 sqrt(__z): Returns the complex square root of __z.
1231 // The branch cut is on the negative axis.
1232 template<typename _Tp>
1234 __complex_sqrt(const complex<_Tp>& __z)
1236 _Tp __x = __z.real();
1237 _Tp __y = __z.imag();
1241 _Tp __t = sqrt(abs(__y) / _Tp(2));
1242 return complex<_Tp>(__t, __y < _Tp() ? -__t : __t);
1246 _Tp __t = sqrt(_Tp(2) * (std::abs(__z) + abs(__x)));
1247 _Tp __u = __t / _Tp(2);
1249 ? complex<_Tp>(__u, __y / __t)
1250 : complex<_Tp>(abs(__y) / __t, __y < _Tp() ? -__u : __u);
1254#if _GLIBCXX_USE_C99_COMPLEX
1255 inline __complex__ float
1256 __complex_sqrt(__complex__ float __z) { return __builtin_csqrtf(__z); }
1258 inline __complex__ double
1259 __complex_sqrt(__complex__ double __z) { return __builtin_csqrt(__z); }
1261 inline __complex__ long double
1262 __complex_sqrt(const __complex__ long double& __z)
1263 { return __builtin_csqrtl(__z); }
1265 template<typename _Tp>
1267 sqrt(const complex<_Tp>& __z) { return __complex_sqrt(__z.__rep()); }
1269 template<typename _Tp>
1271 sqrt(const complex<_Tp>& __z) { return __complex_sqrt(__z); }
1274 // 26.2.8/14 tan(__z): Return the complex tangent of __z.
1276 template<typename _Tp>
1278 __complex_tan(const complex<_Tp>& __z)
1279 { return std::sin(__z) / std::cos(__z); }
1281#if _GLIBCXX_USE_C99_COMPLEX
1282 inline __complex__ float
1283 __complex_tan(__complex__ float __z) { return __builtin_ctanf(__z); }
1285 inline __complex__ double
1286 __complex_tan(__complex__ double __z) { return __builtin_ctan(__z); }
1288 inline __complex__ long double
1289 __complex_tan(const __complex__ long double& __z)
1290 { return __builtin_ctanl(__z); }
1292 template<typename _Tp>
1294 tan(const complex<_Tp>& __z) { return __complex_tan(__z.__rep()); }
1296 template<typename _Tp>
1298 tan(const complex<_Tp>& __z) { return __complex_tan(__z); }
1302 // 26.2.8/15 tanh(__z): Returns the hyperbolic tangent of __z.
1304 template<typename _Tp>
1306 __complex_tanh(const complex<_Tp>& __z)
1307 { return std::sinh(__z) / std::cosh(__z); }
1309#if _GLIBCXX_USE_C99_COMPLEX
1310 inline __complex__ float
1311 __complex_tanh(__complex__ float __z) { return __builtin_ctanhf(__z); }
1313 inline __complex__ double
1314 __complex_tanh(__complex__ double __z) { return __builtin_ctanh(__z); }
1316 inline __complex__ long double
1317 __complex_tanh(const __complex__ long double& __z)
1318 { return __builtin_ctanhl(__z); }
1320 template<typename _Tp>
1322 tanh(const complex<_Tp>& __z) { return __complex_tanh(__z.__rep()); }
1324 template<typename _Tp>
1326 tanh(const complex<_Tp>& __z) { return __complex_tanh(__z); }
1330 // 26.2.8/9 pow(__x, __y): Returns the complex power base of __x
1331 // raised to the __y-th power. The branch
1332 // cut is on the negative axis.
1333 template<typename _Tp>
1335 __complex_pow_unsigned(complex<_Tp> __x, unsigned __n)
1337 complex<_Tp> __y = __n % 2 ? __x : complex<_Tp>(_Tp(1));
1349 // In C++11 mode we used to implement the resolution of
1350 // DR 844. complex pow return type is ambiguous.
1351 // thus the following overload was disabled in that mode. However, doing
1352 // that causes all sorts of issues, see, for example:
1353 // http://gcc.gnu.org/ml/libstdc++/2013-01/msg00058.html
1354 // and also PR57974.
1355 template<typename _Tp>
1357 pow(const complex<_Tp>& __z, int __n)
1360 ? complex<_Tp>(_Tp(1)) / std::__complex_pow_unsigned(__z,
1362 : std::__complex_pow_unsigned(__z, __n);
1365 template<typename _Tp>
1367 pow(const complex<_Tp>& __x, const _Tp& __y)
1369#if ! _GLIBCXX_USE_C99_COMPLEX
1373 if (__x.imag() == _Tp() && __x.real() > _Tp())
1374 return pow(__x.real(), __y);
1376 complex<_Tp> __t = std::log(__x);
1377 return std::polar<_Tp>(exp(__y * __t.real()), __y * __t.imag());
1380 template<typename _Tp>
1382 __complex_pow(const complex<_Tp>& __x, const complex<_Tp>& __y)
1383 { return __x == _Tp() ? _Tp() : std::exp(__y * std::log(__x)); }
1385#if _GLIBCXX_USE_C99_COMPLEX
1386 inline __complex__ float
1387 __complex_pow(__complex__ float __x, __complex__ float __y)
1388 { return __builtin_cpowf(__x, __y); }
1390 inline __complex__ double
1391 __complex_pow(__complex__ double __x, __complex__ double __y)
1392 { return __builtin_cpow(__x, __y); }
1394 inline __complex__ long double
1395 __complex_pow(const __complex__ long double& __x,
1396 const __complex__ long double& __y)
1397 { return __builtin_cpowl(__x, __y); }
1399 template<typename _Tp>
1401 pow(const complex<_Tp>& __x, const complex<_Tp>& __y)
1402 { return __complex_pow(__x.__rep(), __y.__rep()); }
1404 template<typename _Tp>
1406 pow(const complex<_Tp>& __x, const complex<_Tp>& __y)
1407 { return __complex_pow(__x, __y); }
1410 template<typename _Tp>
1412 pow(const _Tp& __x, const complex<_Tp>& __y)
1414 return __x > _Tp() ? std::polar<_Tp>(pow(__x, __y.real()),
1415 __y.imag() * log(__x))
1416 : std::pow(complex<_Tp>(__x), __y);
1419#if __glibcxx_tuple_like >= 202311L // >= C++26
1420#define _GLIBCXX26_DECLARE_COMPLEX_TUPLE_HELPER_ACCESSOR \
1421 template<typename _Cp> \
1422 [[__gnu__::__always_inline__]] \
1424 __get_part(this _Cp& __z, size_t __i) noexcept \
1426 return __i == 0 ? __real__ __z._M_value \
1427 : __imag__ __z._M_value; \
1430#define _GLIBCXX26_DECLARE_COMPLEX_TUPLE_HELPER_ACCESSOR
1433 /// 26.2.3 complex specializations
1434 /// complex<float> specialization
1436 class complex<float>
1439 typedef float value_type;
1440 typedef __complex__ float _ComplexT;
1442 _GLIBCXX_CONSTEXPR complex(_ComplexT __z) : _M_value(__z) { }
1444 _GLIBCXX_CONSTEXPR complex(float __r = 0.0f, float __i = 0.0f)
1445#if __cplusplus >= 201103L
1446 : _M_value{ __r, __i } { }
1449 __real__ _M_value = __r;
1450 __imag__ _M_value = __i;
1454#if __cplusplus >= 201103L
1455 _GLIBCXX14_CONSTEXPR complex(const complex&) = default;
1458#if __cplusplus > 202002L
1459 template<typename _Up>
1460 explicit(!requires(_Up __u) { value_type{__u}; })
1461 constexpr complex(const complex<_Up>& __z)
1462 : _M_value{ value_type(__z.real()), value_type(__z.imag()) } { }
1464 explicit _GLIBCXX_CONSTEXPR complex(const complex<double>&);
1465 explicit _GLIBCXX_CONSTEXPR complex(const complex<long double>&);
1468#if __cplusplus >= 201103L
1469 // _GLIBCXX_RESOLVE_LIB_DEFECTS
1470 // DR 387. std::complex over-encapsulated.
1471 __attribute ((__abi_tag__ ("cxx11")))
1473 real() const { return __real__ _M_value; }
1475 __attribute ((__abi_tag__ ("cxx11")))
1477 imag() const { return __imag__ _M_value; }
1480 real() { return __real__ _M_value; }
1483 real() const { return __real__ _M_value; }
1486 imag() { return __imag__ _M_value; }
1489 imag() const { return __imag__ _M_value; }
1492 // _GLIBCXX_RESOLVE_LIB_DEFECTS
1493 // DR 387. std::complex over-encapsulated.
1494 _GLIBCXX20_CONSTEXPR void
1495 real(float __val) { __real__ _M_value = __val; }
1497 _GLIBCXX20_CONSTEXPR void
1498 imag(float __val) { __imag__ _M_value = __val; }
1500 _GLIBCXX20_CONSTEXPR complex&
1501 operator=(float __f)
1507 _GLIBCXX20_CONSTEXPR complex&
1508 operator+=(float __f)
1514 _GLIBCXX20_CONSTEXPR complex&
1515 operator-=(float __f)
1521 _GLIBCXX20_CONSTEXPR complex&
1522 operator*=(float __f)
1528 _GLIBCXX20_CONSTEXPR complex&
1529 operator/=(float __f)
1535 // Let the compiler synthesize the copy and assignment
1536 // operator. It always does a pretty good job.
1537#if __cplusplus >= 201103L
1538 _GLIBCXX14_CONSTEXPR complex& operator=(const complex&) = default;
1541 template<typename _Tp>
1542 _GLIBCXX20_CONSTEXPR complex&
1543 operator=(const complex<_Tp>& __z)
1545 __real__ _M_value = __z.real();
1546 __imag__ _M_value = __z.imag();
1550 template<typename _Tp>
1551 _GLIBCXX20_CONSTEXPR complex&
1552 operator+=(const complex<_Tp>& __z)
1554 _M_value += __z.__rep();
1559 _GLIBCXX20_CONSTEXPR complex&
1560 operator-=(const complex<_Tp>& __z)
1562 _M_value -= __z.__rep();
1567 _GLIBCXX20_CONSTEXPR complex&
1568 operator*=(const complex<_Tp>& __z)
1570 const _ComplexT __t = __z.__rep();
1576 _GLIBCXX20_CONSTEXPR complex&
1577 operator/=(const complex<_Tp>& __z)
1579 const _ComplexT __t = __z.__rep();
1584 _GLIBCXX_CONSTEXPR _ComplexT __rep() const { return _M_value; }
1586 _GLIBCXX26_DECLARE_COMPLEX_TUPLE_HELPER_ACCESSOR
1592 /// 26.2.3 complex specializations
1593 /// complex<double> specialization
1595 class complex<double>
1598 typedef double value_type;
1599 typedef __complex__ double _ComplexT;
1601 _GLIBCXX_CONSTEXPR complex(_ComplexT __z) : _M_value(__z) { }
1603 _GLIBCXX_CONSTEXPR complex(double __r = 0.0, double __i = 0.0)
1604#if __cplusplus >= 201103L
1605 : _M_value{ __r, __i } { }
1608 __real__ _M_value = __r;
1609 __imag__ _M_value = __i;
1613#if __cplusplus >= 201103L
1614 _GLIBCXX14_CONSTEXPR complex(const complex&) = default;
1617#if __cplusplus > 202002L
1618 template<typename _Up>
1619 explicit(!requires(_Up __u) { value_type{__u}; })
1620 constexpr complex(const complex<_Up>& __z)
1621 : _M_value{ value_type(__z.real()), value_type(__z.imag()) } { }
1623 _GLIBCXX_CONSTEXPR complex(const complex<float>& __z)
1624 : _M_value(__z.__rep()) { }
1626 explicit _GLIBCXX_CONSTEXPR complex(const complex<long double>&);
1629#if __cplusplus >= 201103L
1630 // _GLIBCXX_RESOLVE_LIB_DEFECTS
1631 // DR 387. std::complex over-encapsulated.
1632 __attribute ((__abi_tag__ ("cxx11")))
1634 real() const { return __real__ _M_value; }
1636 __attribute ((__abi_tag__ ("cxx11")))
1638 imag() const { return __imag__ _M_value; }
1641 real() { return __real__ _M_value; }
1644 real() const { return __real__ _M_value; }
1647 imag() { return __imag__ _M_value; }
1650 imag() const { return __imag__ _M_value; }
1653 // _GLIBCXX_RESOLVE_LIB_DEFECTS
1654 // DR 387. std::complex over-encapsulated.
1655 _GLIBCXX20_CONSTEXPR void
1656 real(double __val) { __real__ _M_value = __val; }
1658 _GLIBCXX20_CONSTEXPR void
1659 imag(double __val) { __imag__ _M_value = __val; }
1661 _GLIBCXX20_CONSTEXPR complex&
1662 operator=(double __d)
1668 _GLIBCXX20_CONSTEXPR complex&
1669 operator+=(double __d)
1675 _GLIBCXX20_CONSTEXPR complex&
1676 operator-=(double __d)
1682 _GLIBCXX20_CONSTEXPR complex&
1683 operator*=(double __d)
1689 _GLIBCXX20_CONSTEXPR complex&
1690 operator/=(double __d)
1696 // The compiler will synthesize this, efficiently.
1697#if __cplusplus >= 201103L
1698 _GLIBCXX14_CONSTEXPR complex& operator=(const complex&) = default;
1701 template<typename _Tp>
1702 _GLIBCXX20_CONSTEXPR complex&
1703 operator=(const complex<_Tp>& __z)
1705 _M_value = __z.__rep();
1709 template<typename _Tp>
1710 _GLIBCXX20_CONSTEXPR complex&
1711 operator+=(const complex<_Tp>& __z)
1713 _M_value += __z.__rep();
1717 template<typename _Tp>
1718 _GLIBCXX20_CONSTEXPR complex&
1719 operator-=(const complex<_Tp>& __z)
1721 _M_value -= __z.__rep();
1725 template<typename _Tp>
1726 _GLIBCXX20_CONSTEXPR complex&
1727 operator*=(const complex<_Tp>& __z)
1729 const _ComplexT __t = __z.__rep();
1734 template<typename _Tp>
1735 _GLIBCXX20_CONSTEXPR complex&
1736 operator/=(const complex<_Tp>& __z)
1738 const _ComplexT __t = __z.__rep();
1743 _GLIBCXX_CONSTEXPR _ComplexT __rep() const { return _M_value; }
1745 _GLIBCXX26_DECLARE_COMPLEX_TUPLE_HELPER_ACCESSOR
1751 /// 26.2.3 complex specializations
1752 /// complex<long double> specialization
1754 class complex<long double>
1757 typedef long double value_type;
1758 typedef __complex__ long double _ComplexT;
1760 _GLIBCXX_CONSTEXPR complex(_ComplexT __z) : _M_value(__z) { }
1762 _GLIBCXX_CONSTEXPR complex(long double __r = 0.0L,
1763 long double __i = 0.0L)
1764#if __cplusplus >= 201103L
1765 : _M_value{ __r, __i } { }
1768 __real__ _M_value = __r;
1769 __imag__ _M_value = __i;
1773#if __cplusplus >= 201103L
1774 _GLIBCXX14_CONSTEXPR complex(const complex&) = default;
1777#if __cplusplus > 202002L
1778 template<typename _Up>
1779 explicit(!requires(_Up __u) { value_type{__u}; })
1780 constexpr complex(const complex<_Up>& __z)
1781 : _M_value{ value_type(__z.real()), value_type(__z.imag()) } { }
1783 _GLIBCXX_CONSTEXPR complex(const complex<float>& __z)
1784 : _M_value(__z.__rep()) { }
1786 _GLIBCXX_CONSTEXPR complex(const complex<double>& __z)
1787 : _M_value(__z.__rep()) { }
1790#if __cplusplus >= 201103L
1791 // _GLIBCXX_RESOLVE_LIB_DEFECTS
1792 // DR 387. std::complex over-encapsulated.
1793 __attribute ((__abi_tag__ ("cxx11")))
1794 constexpr long double
1795 real() const { return __real__ _M_value; }
1797 __attribute ((__abi_tag__ ("cxx11")))
1798 constexpr long double
1799 imag() const { return __imag__ _M_value; }
1802 real() { return __real__ _M_value; }
1805 real() const { return __real__ _M_value; }
1808 imag() { return __imag__ _M_value; }
1811 imag() const { return __imag__ _M_value; }
1814 // _GLIBCXX_RESOLVE_LIB_DEFECTS
1815 // DR 387. std::complex over-encapsulated.
1816 _GLIBCXX20_CONSTEXPR void
1817 real(long double __val) { __real__ _M_value = __val; }
1819 _GLIBCXX20_CONSTEXPR void
1820 imag(long double __val) { __imag__ _M_value = __val; }
1822 _GLIBCXX20_CONSTEXPR complex&
1823 operator=(long double __r)
1829 _GLIBCXX20_CONSTEXPR complex&
1830 operator+=(long double __r)
1836 _GLIBCXX20_CONSTEXPR complex&
1837 operator-=(long double __r)
1843 _GLIBCXX20_CONSTEXPR complex&
1844 operator*=(long double __r)
1850 _GLIBCXX20_CONSTEXPR complex&
1851 operator/=(long double __r)
1857 // The compiler knows how to do this efficiently
1858#if __cplusplus >= 201103L
1859 _GLIBCXX14_CONSTEXPR complex& operator=(const complex&) = default;
1862 template<typename _Tp>
1863 _GLIBCXX20_CONSTEXPR complex&
1864 operator=(const complex<_Tp>& __z)
1866 _M_value = __z.__rep();
1870 template<typename _Tp>
1871 _GLIBCXX20_CONSTEXPR complex&
1872 operator+=(const complex<_Tp>& __z)
1874 _M_value += __z.__rep();
1878 template<typename _Tp>
1879 _GLIBCXX20_CONSTEXPR complex&
1880 operator-=(const complex<_Tp>& __z)
1882 _M_value -= __z.__rep();
1886 template<typename _Tp>
1887 _GLIBCXX20_CONSTEXPR complex&
1888 operator*=(const complex<_Tp>& __z)
1890 const _ComplexT __t = __z.__rep();
1895 template<typename _Tp>
1896 _GLIBCXX20_CONSTEXPR complex&
1897 operator/=(const complex<_Tp>& __z)
1899 const _ComplexT __t = __z.__rep();
1904 _GLIBCXX_CONSTEXPR _ComplexT __rep() const { return _M_value; }
1906 _GLIBCXX26_DECLARE_COMPLEX_TUPLE_HELPER_ACCESSOR
1912#if __cplusplus > 202002L
1913 template<typename _Tp>
1914 struct __complex_type
1917#ifdef __STDCPP_FLOAT16_T__
1919 struct __complex_type<_Float16>
1920 { typedef __complex__ _Float16 type; };
1923#ifdef __STDCPP_FLOAT32_T__
1925 struct __complex_type<_Float32>
1926 { typedef __complex__ _Float32 type; };
1929#ifdef __STDCPP_FLOAT64_T__
1931 struct __complex_type<_Float64>
1932 { typedef __complex__ _Float64 type; };
1935#ifdef __STDCPP_FLOAT128_T__
1937 struct __complex_type<_Float128>
1938 { typedef __complex__ _Float128 type; };
1941#ifdef __STDCPP_BFLOAT16_T__
1943 struct __complex_type<__gnu_cxx::__bfloat16_t>
1944 { typedef __complex__ decltype(0.0bf16) type; };
1947 template<typename _Tp>
1948 requires requires { typename __complex_type<_Tp>::type; }
1952 typedef _Tp value_type;
1953 typedef typename std::__complex_type<_Tp>::type _ComplexT;
1955 constexpr complex(_ComplexT __z) : _M_value(__z) { }
1957 constexpr complex(_Tp __r = _Tp(), _Tp __i = _Tp())
1958 : _M_value{ __r, __i } { }
1960 template<typename _Up>
1961 explicit(!requires(_Up __u) { value_type{__u}; })
1962 constexpr complex(const complex<_Up>& __z)
1963 : _M_value{ value_type(__z.real()), value_type(__z.imag()) } { }
1966 real() const { return __real__ _M_value; }
1969 imag() const { return __imag__ _M_value; }
1972 real(_Tp __val) { __real__ _M_value = __val; }
1975 imag(_Tp __val) { __imag__ _M_value = __val; }
2012 // Let the compiler synthesize the copy and assignment
2013 // operator. It always does a pretty good job.
2014 constexpr complex(const complex&) = default;
2015 constexpr complex& operator=(const complex&) = default;
2017 template<typename _Up>
2019 operator=(const complex<_Up>& __z)
2021 __real__ _M_value = __z.real();
2022 __imag__ _M_value = __z.imag();
2026 template<typename _Up>
2028 operator+=(const complex<_Up>& __z)
2030 _M_value += __z.__rep();
2036 operator-=(const complex<_Up>& __z)
2038 _M_value -= __z.__rep();
2044 operator*=(const complex<_Up>& __z)
2046 const _ComplexT __t = __z.__rep();
2053 operator/=(const complex<_Up>& __z)
2055 const _ComplexT __t = __z.__rep();
2060 constexpr _ComplexT __rep() const { return _M_value; }
2062 _GLIBCXX26_DECLARE_COMPLEX_TUPLE_HELPER_ACCESSOR
2069#undef _GLIBCXX26_DECLARE_COMPLEX_TUPLE_HELPER_ACCESSOR
2071#if __cplusplus <= 202002L
2072 // These bits have to be at the end of this file, so that the
2073 // specializations have all been defined.
2074 inline _GLIBCXX_CONSTEXPR
2075 complex<float>::complex(const complex<double>& __z)
2076 : _M_value(__z.__rep()) { }
2078 inline _GLIBCXX_CONSTEXPR
2079 complex<float>::complex(const complex<long double>& __z)
2080 : _M_value(__z.__rep()) { }
2082 inline _GLIBCXX_CONSTEXPR
2083 complex<double>::complex(const complex<long double>& __z)
2084 : _M_value(__z.__rep()) { }
2087 // Inhibit implicit instantiations for required instantiations,
2088 // which are defined via explicit instantiations elsewhere.
2089 // NB: This syntax is a GNU extension.
2090#if _GLIBCXX_EXTERN_TEMPLATE
2091 extern template istream& operator>>(istream&, complex<float>&);
2092 extern template ostream& operator<<(ostream&, const complex<float>&);
2093 extern template istream& operator>>(istream&, complex<double>&);
2094 extern template ostream& operator<<(ostream&, const complex<double>&);
2095 extern template istream& operator>>(istream&, complex<long double>&);
2096 extern template ostream& operator<<(ostream&, const complex<long double>&);
2098#ifdef _GLIBCXX_USE_WCHAR_T
2099 extern template wistream& operator>>(wistream&, complex<float>&);
2100 extern template wostream& operator<<(wostream&, const complex<float>&);
2101 extern template wistream& operator>>(wistream&, complex<double>&);
2102 extern template wostream& operator<<(wostream&, const complex<double>&);
2103 extern template wistream& operator>>(wistream&, complex<long double>&);
2104 extern template wostream& operator<<(wostream&, const complex<long double>&);
2108 /// @} group complex_numbers
2110_GLIBCXX_END_NAMESPACE_VERSION
2113#if __cplusplus >= 201103L
2115namespace std _GLIBCXX_VISIBILITY(default)
2117_GLIBCXX_BEGIN_NAMESPACE_VERSION
2119 // Forward declarations.
2120 template<typename _Tp> std::complex<_Tp> acos(const std::complex<_Tp>&);
2121 template<typename _Tp> std::complex<_Tp> asin(const std::complex<_Tp>&);
2122 template<typename _Tp> std::complex<_Tp> atan(const std::complex<_Tp>&);
2124 template<typename _Tp> std::complex<_Tp> acosh(const std::complex<_Tp>&);
2125 template<typename _Tp> std::complex<_Tp> asinh(const std::complex<_Tp>&);
2126 template<typename _Tp> std::complex<_Tp> atanh(const std::complex<_Tp>&);
2128 template<typename _Tp>
2129 inline std::complex<_Tp>
2130 __complex_acos(const std::complex<_Tp>& __z)
2132 const std::complex<_Tp> __t = std::asin(__z);
2133 const _Tp __pi_2 = (_Tp) 1.5707963267948966192313216916397514L;
2134 return std::complex<_Tp>(__pi_2 - __t.real(), -__t.imag());
2137#if _GLIBCXX_USE_C99_COMPLEX_ARC
2138#if defined(__STDCPP_FLOAT16_T__) && defined(_GLIBCXX_FLOAT_IS_IEEE_BINARY32)
2139 inline __complex__ _Float16
2140 __complex_acos(__complex__ _Float16 __z)
2141 { return static_cast<__complex__ _Float16>(__builtin_cacosf(__z)); }
2143 inline __complex__ _Float16
2144 __complex_asin(__complex__ _Float16 __z)
2145 { return static_cast<__complex__ _Float16>(__builtin_casinf(__z)); }
2147 inline __complex__ _Float16
2148 __complex_atan(__complex__ _Float16 __z)
2149 { return static_cast<__complex__ _Float16>(__builtin_catanf(__z)); }
2151 inline __complex__ _Float16
2152 __complex_acosh(__complex__ _Float16 __z)
2153 { return static_cast<__complex__ _Float16>(__builtin_cacoshf(__z)); }
2155 inline __complex__ _Float16
2156 __complex_asinh(__complex__ _Float16 __z)
2157 { return static_cast<__complex__ _Float16>(__builtin_casinhf(__z)); }
2159 inline __complex__ _Float16
2160 __complex_atanh(__complex__ _Float16 __z)
2161 { return static_cast<__complex__ _Float16>(__builtin_catanhf(__z)); }
2164#if defined(__STDCPP_FLOAT32_T__) && defined(_GLIBCXX_FLOAT_IS_IEEE_BINARY32)
2165 inline __complex__ _Float32
2166 __complex_acos(__complex__ _Float32 __z)
2167 { return __builtin_cacosf(__z); }
2169 inline __complex__ _Float32
2170 __complex_asin(__complex__ _Float32 __z)
2171 { return __builtin_casinf(__z); }
2173 inline __complex__ _Float32
2174 __complex_atan(__complex__ _Float32 __z)
2175 { return __builtin_catanf(__z); }
2177 inline __complex__ _Float32
2178 __complex_acosh(__complex__ _Float32 __z)
2179 { return __builtin_cacoshf(__z); }
2181 inline __complex__ _Float32
2182 __complex_asinh(__complex__ _Float32 __z)
2183 { return __builtin_casinhf(__z); }
2185 inline __complex__ _Float32
2186 __complex_atanh(__complex__ _Float32 __z)
2187 { return __builtin_catanhf(__z); }
2190#if defined(__STDCPP_FLOAT64_T__) && defined(_GLIBCXX_DOUBLE_IS_IEEE_BINARY64)
2191 inline __complex__ _Float64
2192 __complex_acos(__complex__ _Float64 __z)
2193 { return __builtin_cacos(__z); }
2195 inline __complex__ _Float64
2196 __complex_asin(__complex__ _Float64 __z)
2197 { return __builtin_casin(__z); }
2199 inline __complex__ _Float64
2200 __complex_atan(__complex__ _Float64 __z)
2201 { return __builtin_catan(__z); }
2203 inline __complex__ _Float64
2204 __complex_acosh(__complex__ _Float64 __z)
2205 { return __builtin_cacosh(__z); }
2207 inline __complex__ _Float64
2208 __complex_asinh(__complex__ _Float64 __z)
2209 { return __builtin_casinh(__z); }
2211 inline __complex__ _Float64
2212 __complex_atanh(__complex__ _Float64 __z)
2213 { return __builtin_catanh(__z); }
2216#if defined(__STDCPP_FLOAT128_T__) && defined(_GLIBCXX_LDOUBLE_IS_IEEE_BINARY128)
2217 inline __complex__ _Float128
2218 __complex_acos(__complex__ _Float128 __z)
2219 { return __builtin_cacosl(__z); }
2221 inline __complex__ _Float128
2222 __complex_asin(__complex__ _Float128 __z)
2223 { return __builtin_casinl(__z); }
2225 inline __complex__ _Float128
2226 __complex_atan(__complex__ _Float128 __z)
2227 { return __builtin_catanl(__z); }
2229 inline __complex__ _Float128
2230 __complex_acosh(__complex__ _Float128 __z)
2231 { return __builtin_cacoshl(__z); }
2233 inline __complex__ _Float128
2234 __complex_asinh(__complex__ _Float128 __z)
2235 { return __builtin_casinhl(__z); }
2237 inline __complex__ _Float128
2238 __complex_atanh(__complex__ _Float128 __z)
2239 { return __builtin_catanhl(__z); }
2240#elif defined(__STDCPP_FLOAT128_T__) && defined(_GLIBCXX_HAVE_FLOAT128_MATH)
2241 inline __complex__ _Float128
2242 __complex_acos(__complex__ _Float128 __z)
2243 { return __builtin_cacosf128(__z); }
2245 inline __complex__ _Float128
2246 __complex_asin(__complex__ _Float128 __z)
2247 { return __builtin_casinf128(__z); }
2249 inline __complex__ _Float128
2250 __complex_atan(__complex__ _Float128 __z)
2251 { return __builtin_catanf128(__z); }
2253 inline __complex__ _Float128
2254 __complex_acosh(__complex__ _Float128 __z)
2255 { return __builtin_cacoshf128(__z); }
2257 inline __complex__ _Float128
2258 __complex_asinh(__complex__ _Float128 __z)
2259 { return __builtin_casinhf128(__z); }
2261 inline __complex__ _Float128
2262 __complex_atanh(__complex__ _Float128 __z)
2263 { return __builtin_catanhf128(__z); }
2266#if defined(__STDCPP_BFLOAT16_T__) && defined(_GLIBCXX_FLOAT_IS_IEEE_BINARY32)
2267 inline __complex__ decltype(0.0bf16)
2268 __complex_acos(__complex__ decltype(0.0bf16) __z)
2269 { return static_cast<__complex__ decltype(0.0bf16)>(__builtin_cacosf(__z)); }
2271 inline __complex__ decltype(0.0bf16)
2272 __complex_asin(__complex__ decltype(0.0bf16) __z)
2273 { return static_cast<__complex__ decltype(0.0bf16)>(__builtin_casinf(__z)); }
2275 inline __complex__ decltype(0.0bf16)
2276 __complex_atan(__complex__ decltype(0.0bf16) __z)
2277 { return static_cast<__complex__ decltype(0.0bf16)>(__builtin_catanf(__z)); }
2279 inline __complex__ decltype(0.0bf16)
2280 __complex_acosh(__complex__ decltype(0.0bf16) __z)
2281 { return static_cast<__complex__ decltype(0.0bf16)>(__builtin_cacoshf(__z)); }
2283 inline __complex__ decltype(0.0bf16)
2284 __complex_asinh(__complex__ decltype(0.0bf16) __z)
2285 { return static_cast<__complex__ decltype(0.0bf16)>(__builtin_casinhf(__z)); }
2287 inline __complex__ decltype(0.0bf16)
2288 __complex_atanh(__complex__ decltype(0.0bf16) __z)
2289 { return static_cast<__complex__ decltype(0.0bf16)>(__builtin_catanhf(__z)); }
2293#if _GLIBCXX_USE_C99_COMPLEX_ARC
2294 inline __complex__ float
2295 __complex_acos(__complex__ float __z)
2296 { return __builtin_cacosf(__z); }
2298 inline __complex__ double
2299 __complex_acos(__complex__ double __z)
2300 { return __builtin_cacos(__z); }
2302 inline __complex__ long double
2303 __complex_acos(const __complex__ long double& __z)
2304 { return __builtin_cacosl(__z); }
2306 template<typename _Tp>
2307 inline std::complex<_Tp>
2308 acos(const std::complex<_Tp>& __z)
2309 { return __complex_acos(__z.__rep()); }
2311 /// acos(__z) C++11 26.4.8 [complex.transcendentals]
2312 // Effects: Behaves the same as C99 function cacos, defined
2313 // in subclause 7.3.5.1.
2314 template<typename _Tp>
2315 inline std::complex<_Tp>
2316 acos(const std::complex<_Tp>& __z)
2317 { return __complex_acos(__z); }
2320 template<typename _Tp>
2321 inline std::complex<_Tp>
2322 __complex_asin(const std::complex<_Tp>& __z)
2324 std::complex<_Tp> __t(-__z.imag(), __z.real());
2325 __t = std::asinh(__t);
2326 return std::complex<_Tp>(__t.imag(), -__t.real());
2329#if _GLIBCXX_USE_C99_COMPLEX_ARC
2330 inline __complex__ float
2331 __complex_asin(__complex__ float __z)
2332 { return __builtin_casinf(__z); }
2334 inline __complex__ double
2335 __complex_asin(__complex__ double __z)
2336 { return __builtin_casin(__z); }
2338 inline __complex__ long double
2339 __complex_asin(const __complex__ long double& __z)
2340 { return __builtin_casinl(__z); }
2342 template<typename _Tp>
2343 inline std::complex<_Tp>
2344 asin(const std::complex<_Tp>& __z)
2345 { return __complex_asin(__z.__rep()); }
2347 /// asin(__z) C++11 26.4.8 [complex.transcendentals]
2348 // Effects: Behaves the same as C99 function casin, defined
2349 // in subclause 7.3.5.2.
2350 template<typename _Tp>
2351 inline std::complex<_Tp>
2352 asin(const std::complex<_Tp>& __z)
2353 { return __complex_asin(__z); }
2356 template<typename _Tp>
2358 __complex_atan(const std::complex<_Tp>& __z)
2360 const _Tp __r2 = __z.real() * __z.real();
2361 const _Tp __x = _Tp(1.0) - __r2 - __z.imag() * __z.imag();
2363 _Tp __num = __z.imag() + _Tp(1.0);
2364 _Tp __den = __z.imag() - _Tp(1.0);
2366 __num = __r2 + __num * __num;
2367 __den = __r2 + __den * __den;
2369 return std::complex<_Tp>(_Tp(0.5) * atan2(_Tp(2.0) * __z.real(), __x),
2370 _Tp(0.25) * log(__num / __den));
2373#if _GLIBCXX_USE_C99_COMPLEX_ARC
2374 inline __complex__ float
2375 __complex_atan(__complex__ float __z)
2376 { return __builtin_catanf(__z); }
2378 inline __complex__ double
2379 __complex_atan(__complex__ double __z)
2380 { return __builtin_catan(__z); }
2382 inline __complex__ long double
2383 __complex_atan(const __complex__ long double& __z)
2384 { return __builtin_catanl(__z); }
2386 template<typename _Tp>
2387 inline std::complex<_Tp>
2388 atan(const std::complex<_Tp>& __z)
2389 { return __complex_atan(__z.__rep()); }
2391 /// atan(__z) C++11 26.4.8 [complex.transcendentals]
2392 // Effects: Behaves the same as C99 function catan, defined
2393 // in subclause 7.3.5.3.
2394 template<typename _Tp>
2395 inline std::complex<_Tp>
2396 atan(const std::complex<_Tp>& __z)
2397 { return __complex_atan(__z); }
2400 template<typename _Tp>
2402 __complex_acosh(const std::complex<_Tp>& __z)
2405 return _Tp(2.0) * std::log(std::sqrt(_Tp(0.5) * (__z + _Tp(1.0)))
2406 + std::sqrt(_Tp(0.5) * (__z - _Tp(1.0))));
2409#if _GLIBCXX_USE_C99_COMPLEX_ARC
2410 inline __complex__ float
2411 __complex_acosh(__complex__ float __z)
2412 { return __builtin_cacoshf(__z); }
2414 inline __complex__ double
2415 __complex_acosh(__complex__ double __z)
2416 { return __builtin_cacosh(__z); }
2418 inline __complex__ long double
2419 __complex_acosh(const __complex__ long double& __z)
2420 { return __builtin_cacoshl(__z); }
2422 template<typename _Tp>
2423 inline std::complex<_Tp>
2424 acosh(const std::complex<_Tp>& __z)
2425 { return __complex_acosh(__z.__rep()); }
2427 /// acosh(__z) C++11 26.4.8 [complex.transcendentals]
2428 // Effects: Behaves the same as C99 function cacosh, defined
2429 // in subclause 7.3.6.1.
2430 template<typename _Tp>
2431 inline std::complex<_Tp>
2432 acosh(const std::complex<_Tp>& __z)
2433 { return __complex_acosh(__z); }
2436 template<typename _Tp>
2438 __complex_asinh(const std::complex<_Tp>& __z)
2440 std::complex<_Tp> __t((__z.real() - __z.imag())
2441 * (__z.real() + __z.imag()) + _Tp(1.0),
2442 _Tp(2.0) * __z.real() * __z.imag());
2443 __t = std::sqrt(__t);
2445 return std::log(__t + __z);
2448#if _GLIBCXX_USE_C99_COMPLEX_ARC
2449 inline __complex__ float
2450 __complex_asinh(__complex__ float __z)
2451 { return __builtin_casinhf(__z); }
2453 inline __complex__ double
2454 __complex_asinh(__complex__ double __z)
2455 { return __builtin_casinh(__z); }
2457 inline __complex__ long double
2458 __complex_asinh(const __complex__ long double& __z)
2459 { return __builtin_casinhl(__z); }
2461 template<typename _Tp>
2462 inline std::complex<_Tp>
2463 asinh(const std::complex<_Tp>& __z)
2464 { return __complex_asinh(__z.__rep()); }
2466 /// asinh(__z) C++11 26.4.8 [complex.transcendentals]
2467 // Effects: Behaves the same as C99 function casin, defined
2468 // in subclause 7.3.6.2.
2469 template<typename _Tp>
2470 inline std::complex<_Tp>
2471 asinh(const std::complex<_Tp>& __z)
2472 { return __complex_asinh(__z); }
2475 template<typename _Tp>
2477 __complex_atanh(const std::complex<_Tp>& __z)
2479 const _Tp __i2 = __z.imag() * __z.imag();
2480 const _Tp __x = _Tp(1.0) - __i2 - __z.real() * __z.real();
2482 _Tp __num = _Tp(1.0) + __z.real();
2483 _Tp __den = _Tp(1.0) - __z.real();
2485 __num = __i2 + __num * __num;
2486 __den = __i2 + __den * __den;
2488 return std::complex<_Tp>(_Tp(0.25) * (log(__num) - log(__den)),
2489 _Tp(0.5) * atan2(_Tp(2.0) * __z.imag(), __x));
2492#if _GLIBCXX_USE_C99_COMPLEX_ARC
2493 inline __complex__ float
2494 __complex_atanh(__complex__ float __z)
2495 { return __builtin_catanhf(__z); }
2497 inline __complex__ double
2498 __complex_atanh(__complex__ double __z)
2499 { return __builtin_catanh(__z); }
2501 inline __complex__ long double
2502 __complex_atanh(const __complex__ long double& __z)
2503 { return __builtin_catanhl(__z); }
2505 template<typename _Tp>
2506 inline std::complex<_Tp>
2507 atanh(const std::complex<_Tp>& __z)
2508 { return __complex_atanh(__z.__rep()); }
2510 /// atanh(__z) C++11 26.4.8 [complex.transcendentals]
2511 // Effects: Behaves the same as C99 function catanh, defined
2512 // in subclause 7.3.6.3.
2513 template<typename _Tp>
2514 inline std::complex<_Tp>
2515 atanh(const std::complex<_Tp>& __z)
2516 { return __complex_atanh(__z); }
2519 template<typename _Tp>
2520 _GLIBCXX11_DEPRECATED_SUGGEST("std::abs")
2522 /// fabs(__z) TR1 8.1.8 [tr.c99.cmplx.fabs]
2523 // Effects: Behaves the same as C99 function cabs, defined
2524 // in subclause 7.3.8.1.
2525 fabs(const std::complex<_Tp>& __z)
2526 { return std::abs(__z); }
2528 // Additional overloads C++11 26.4.9 [cmplx.over]
2530 template<typename _Tp>
2531 inline typename __gnu_cxx::__promote<_Tp>::__type
2534 typedef typename __gnu_cxx::__promote<_Tp>::__type __type;
2535#if (_GLIBCXX11_USE_C99_MATH && !_GLIBCXX_USE_C99_FP_MACROS_DYNAMIC)
2536 return __builtin_signbit(__type(__x))
2537 ? __type(3.1415926535897932384626433832795029L) : __type();
2539 return std::arg(std::complex<__type>(__x));
2543 template<typename _Tp>
2544 _GLIBCXX_CONSTEXPR inline typename __gnu_cxx::__promote<_Tp>::__type
2548 template<typename _Tp>
2549 _GLIBCXX20_CONSTEXPR inline typename __gnu_cxx::__promote<_Tp>::__type
2552 typedef typename __gnu_cxx::__promote<_Tp>::__type __type;
2553 return __type(__x) * __type(__x);
2556 template<typename _Tp>
2557 _GLIBCXX_CONSTEXPR inline typename __gnu_cxx::__promote<_Tp>::__type
2561 template<typename _Tp, typename _Up>
2562 inline std::complex<typename __gnu_cxx::__promote_2<_Tp, _Up>::__type>
2563 pow(const std::complex<_Tp>& __x, const _Up& __y)
2565 typedef typename __gnu_cxx::__promote_2<_Tp, _Up>::__type __type;
2566 return std::pow(std::complex<__type>(__x), __type(__y));
2569 template<typename _Tp, typename _Up>
2570 inline std::complex<typename __gnu_cxx::__promote_2<_Tp, _Up>::__type>
2571 pow(const _Tp& __x, const std::complex<_Up>& __y)
2573 typedef typename __gnu_cxx::__promote_2<_Tp, _Up>::__type __type;
2574 return std::pow(__type(__x), std::complex<__type>(__y));
2577 template<typename _Tp, typename _Up>
2578 inline std::complex<typename __gnu_cxx::__promote_2<_Tp, _Up>::__type>
2579 pow(const std::complex<_Tp>& __x, const std::complex<_Up>& __y)
2581 typedef typename __gnu_cxx::__promote_2<_Tp, _Up>::__type __type;
2582 return std::pow(std::complex<__type>(__x),
2583 std::complex<__type>(__y));
2586 // Forward declarations.
2588 template<typename _Tp>
2589 std::complex<_Tp> proj(const std::complex<_Tp>&);
2591 // Generic implementation of std::proj, does not work for infinities.
2592 template<typename _Tp>
2593 inline std::complex<_Tp>
2594 __complex_proj(const std::complex<_Tp>& __z)
2597#if _GLIBCXX_USE_C99_COMPLEX
2598 inline complex<float>
2599 __complex_proj(const complex<float>& __z)
2600 { return __builtin_cprojf(__z.__rep()); }
2602 inline complex<double>
2603 __complex_proj(const complex<double>& __z)
2604 { return __builtin_cproj(__z.__rep()); }
2606 inline complex<long double>
2607 __complex_proj(const complex<long double>& __z)
2608 { return __builtin_cprojl(__z.__rep()); }
2610#if __cplusplus > 202002L
2611#if defined(__STDCPP_FLOAT16_T__) && defined(_GLIBCXX_FLOAT_IS_IEEE_BINARY32)
2612 inline __complex__ _Float16
2613 __complex_proj(__complex__ _Float16 __z)
2614 { return static_cast<__complex__ _Float16>(__builtin_cprojf(__z)); }
2617#if defined(__STDCPP_FLOAT32_T__) && defined(_GLIBCXX_FLOAT_IS_IEEE_BINARY32)
2618 inline __complex__ _Float32
2619 __complex_proj(__complex__ _Float32 __z)
2620 { return __builtin_cprojf(__z); }
2623#if defined(__STDCPP_FLOAT64_T__) && defined(_GLIBCXX_DOUBLE_IS_IEEE_BINARY64)
2624 inline __complex__ _Float64
2625 __complex_proj(__complex__ _Float64 __z)
2626 { return __builtin_cproj(__z); }
2629#if defined(__STDCPP_FLOAT128_T__) && defined(_GLIBCXX_LDOUBLE_IS_IEEE_BINARY128)
2630 inline __complex__ _Float128
2631 __complex_proj(__complex__ _Float128 __z)
2632 { return __builtin_cprojl(__z); }
2633#elif defined(__STDCPP_FLOAT128_T__) && defined(_GLIBCXX_HAVE_FLOAT128_MATH)
2634 inline __complex__ _Float128
2635 __complex_proj(__complex__ _Float128 __z)
2636 { return __builtin_cprojf128(__z); }
2639#if defined(__STDCPP_BFLOAT16_T__) && defined(_GLIBCXX_FLOAT_IS_IEEE_BINARY32)
2640 inline __complex__ decltype(0.0bf16)
2641 __complex_proj(__complex__ decltype(0.0bf16) __z)
2642 { return static_cast<__complex__ decltype(0.0bf16)>(__builtin_cprojf(__z)); }
2645 template<typename _Tp>
2646 requires requires { typename __complex_type<_Tp>::type; }
2648 __complex_proj(const complex<_Tp>& __z)
2649 { return __complex_proj(__z.__rep()); }
2652#elif defined _GLIBCXX_USE_C99_MATH_FUNCS
2653 inline complex<float>
2654 __complex_proj(const complex<float>& __z)
2656 if (__builtin_isinf(__z.real()) || __builtin_isinf(__z.imag()))
2657 return complex<float>(__builtin_inff(),
2658 __builtin_copysignf(0.0f, __z.imag()));
2662 inline complex<double>
2663 __complex_proj(const complex<double>& __z)
2665 if (__builtin_isinf(__z.real()) || __builtin_isinf(__z.imag()))
2666 return complex<double>(__builtin_inf(),
2667 __builtin_copysign(0.0, __z.imag()));
2671 inline complex<long double>
2672 __complex_proj(const complex<long double>& __z)
2674 if (__builtin_isinf(__z.real()) || __builtin_isinf(__z.imag()))
2675 return complex<long double>(__builtin_infl(),
2676 __builtin_copysignl(0.0l, __z.imag()));
2681 template<typename _Tp>
2682 inline std::complex<_Tp>
2683 proj(const std::complex<_Tp>& __z)
2684 { return __complex_proj(__z); }
2686 // Overload for scalars
2687 template<typename _Tp>
2688 inline std::complex<typename __gnu_cxx::__promote<_Tp>::__type>
2691 typedef typename __gnu_cxx::__promote<_Tp>::__type __type;
2692 return std::proj(std::complex<__type>(__x));
2695 template<typename _Tp>
2696 inline _GLIBCXX20_CONSTEXPR
2697 std::complex<typename __gnu_cxx::__promote<_Tp>::__type>
2700 typedef typename __gnu_cxx::__promote<_Tp>::__type __type;
2701 return std::complex<__type>(__x, -__type());
2704#ifdef __cpp_lib_complex_udls // C++ >= 14
2706inline namespace literals {
2707inline namespace complex_literals {
2708#pragma GCC diagnostic push
2709#pragma GCC diagnostic ignored "-Wliteral-suffix"
2711 constexpr std::complex<float>
2712 operator""if(long double __num)
2713 { return std::complex<float>{0.0F, static_cast<float>(__num)}; }
2715 constexpr std::complex<float>
2716 operator""if(unsigned long long __num)
2717 { return std::complex<float>{0.0F, static_cast<float>(__num)}; }
2719 constexpr std::complex<double>
2720 operator""i(long double __num)
2721 { return std::complex<double>{0.0, static_cast<double>(__num)}; }
2723 constexpr std::complex<double>
2724 operator""i(unsigned long long __num)
2725 { return std::complex<double>{0.0, static_cast<double>(__num)}; }
2727 constexpr std::complex<long double>
2728 operator""il(long double __num)
2729 { return std::complex<long double>{0.0L, __num}; }
2731 constexpr std::complex<long double>
2732 operator""il(unsigned long long __num)
2733 { return std::complex<long double>{0.0L, static_cast<long double>(__num)}; }
2735#pragma GCC diagnostic pop
2736} // inline namespace complex_literals
2737} // inline namespace literals
2739#endif // __cpp_lib_complex_udls
2741_GLIBCXX_END_NAMESPACE_VERSION
2746#ifdef _GLIBCXX_CLANG
2747#pragma clang diagnostic pop
2750#pragma GCC diagnostic pop
2751#endif /* _GLIBCXX_COMPLEX */