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
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#include <bits/version.h>
64namespace std _GLIBCXX_VISIBILITY(default)
66_GLIBCXX_BEGIN_NAMESPACE_VERSION
69 * @defgroup complex_numbers Complex Numbers
72 * Classes and functions for complex numbers.
76 // Forward declarations.
77 template<typename _Tp> class complex;
78 template<> class complex<float>;
79 template<> class complex<double>;
80 template<> class complex<long double>;
82 /// Return magnitude of @a z.
83 template<typename _Tp> _Tp abs(const complex<_Tp>&);
84 /// Return phase angle of @a z.
85 template<typename _Tp> _Tp arg(const complex<_Tp>&);
86 /// Return @a z magnitude squared.
87 template<typename _Tp> _Tp _GLIBCXX20_CONSTEXPR norm(const complex<_Tp>&);
89 /// Return complex conjugate of @a z.
90 template<typename _Tp>
91 _GLIBCXX20_CONSTEXPR complex<_Tp> conj(const complex<_Tp>&);
92 /// Return complex with magnitude @a rho and angle @a theta.
93 template<typename _Tp> complex<_Tp> polar(const _Tp&, const _Tp& = 0);
96 /// Return complex cosine of @a z.
97 template<typename _Tp> complex<_Tp> cos(const complex<_Tp>&);
98 /// Return complex hyperbolic cosine of @a z.
99 template<typename _Tp> complex<_Tp> cosh(const complex<_Tp>&);
100 /// Return complex base e exponential of @a z.
101 template<typename _Tp> complex<_Tp> exp(const complex<_Tp>&);
102 /// Return complex natural logarithm of @a z.
103 template<typename _Tp> complex<_Tp> log(const complex<_Tp>&);
104 /// Return complex base 10 logarithm of @a z.
105 template<typename _Tp> complex<_Tp> log10(const complex<_Tp>&);
106 /// Return @a x to the @a y'th power.
107 template<typename _Tp> complex<_Tp> pow(const complex<_Tp>&, int);
108 /// Return @a x to the @a y'th power.
109 template<typename _Tp> complex<_Tp> pow(const complex<_Tp>&, const _Tp&);
110 /// Return @a x to the @a y'th power.
111 template<typename _Tp> complex<_Tp> pow(const complex<_Tp>&,
112 const complex<_Tp>&);
113 /// Return @a x to the @a y'th power.
114 template<typename _Tp> complex<_Tp> pow(const _Tp&, const complex<_Tp>&);
115 /// Return complex sine of @a z.
116 template<typename _Tp> complex<_Tp> sin(const complex<_Tp>&);
117 /// Return complex hyperbolic sine of @a z.
118 template<typename _Tp> complex<_Tp> sinh(const complex<_Tp>&);
119 /// Return complex square root of @a z.
120 template<typename _Tp> complex<_Tp> sqrt(const complex<_Tp>&);
121 /// Return complex tangent of @a z.
122 template<typename _Tp> complex<_Tp> tan(const complex<_Tp>&);
123 /// Return complex hyperbolic tangent of @a z.
124 template<typename _Tp> complex<_Tp> tanh(const complex<_Tp>&);
127 // 26.2.2 Primary template class complex
129 * Template to represent complex numbers.
131 * Specializations for float, double, and long double are part of the
132 * library. Results with any other type are not guaranteed.
134 * @param Tp Type of real and imaginary values.
136 template<typename _Tp>
141 typedef _Tp value_type;
143 /// Default constructor. First parameter is x, second parameter is y.
144 /// Unspecified parameters default to 0.
145 _GLIBCXX_CONSTEXPR complex(const _Tp& __r = _Tp(), const _Tp& __i = _Tp())
146 : _M_real(__r), _M_imag(__i) { }
148 // Let the compiler synthesize the copy constructor
149#if __cplusplus >= 201103L
150 constexpr complex(const complex&) = default;
153 /// Converting constructor.
154 template<typename _Up>
155#if __cplusplus > 202002L
156 explicit(!requires(_Up __u) { _Tp{__u}; })
158 _GLIBCXX_CONSTEXPR complex(const complex<_Up>& __z)
159 : _M_real(_Tp(__z.real())), _M_imag(_Tp(__z.imag())) { }
161#if __cplusplus >= 201103L
162 // _GLIBCXX_RESOLVE_LIB_DEFECTS
163 // DR 387. std::complex over-encapsulated.
164 _GLIBCXX_ABI_TAG_CXX11
166 real() const { return _M_real; }
168 _GLIBCXX_ABI_TAG_CXX11
170 imag() const { return _M_imag; }
172 /// Return real part of complex number.
174 real() { return _M_real; }
176 /// Return real part of complex number.
178 real() const { return _M_real; }
180 /// Return imaginary part of complex number.
182 imag() { return _M_imag; }
184 /// Return imaginary part of complex number.
186 imag() const { return _M_imag; }
189 // _GLIBCXX_RESOLVE_LIB_DEFECTS
190 // DR 387. std::complex over-encapsulated.
191 _GLIBCXX20_CONSTEXPR void
192 real(_Tp __val) { _M_real = __val; }
194 _GLIBCXX20_CONSTEXPR void
195 imag(_Tp __val) { _M_imag = __val; }
197 /// Assign a scalar to this complex number.
198 _GLIBCXX20_CONSTEXPR complex<_Tp>& operator=(const _Tp&);
200 /// Add a scalar to this complex number.
202 _GLIBCXX20_CONSTEXPR complex<_Tp>&
203 operator+=(const _Tp& __t)
209 /// Subtract a scalar from this complex number.
211 _GLIBCXX20_CONSTEXPR complex<_Tp>&
212 operator-=(const _Tp& __t)
218 /// Multiply this complex number by a scalar.
219 _GLIBCXX20_CONSTEXPR complex<_Tp>& operator*=(const _Tp&);
220 /// Divide this complex number by a scalar.
221 _GLIBCXX20_CONSTEXPR complex<_Tp>& operator/=(const _Tp&);
223 // Let the compiler synthesize the copy assignment operator
224#if __cplusplus >= 201103L
225 _GLIBCXX20_CONSTEXPR complex& operator=(const complex&) = default;
228 /// Assign another complex number to this one.
229 template<typename _Up>
230 _GLIBCXX20_CONSTEXPR complex<_Tp>& operator=(const complex<_Up>&);
231 /// Add another complex number to this one.
232 template<typename _Up>
233 _GLIBCXX20_CONSTEXPR complex<_Tp>& operator+=(const complex<_Up>&);
234 /// Subtract another complex number from this one.
235 template<typename _Up>
236 _GLIBCXX20_CONSTEXPR complex<_Tp>& operator-=(const complex<_Up>&);
237 /// Multiply this complex number by another.
238 template<typename _Up>
239 _GLIBCXX20_CONSTEXPR complex<_Tp>& operator*=(const complex<_Up>&);
240 /// Divide this complex number by another.
241 template<typename _Up>
242 _GLIBCXX20_CONSTEXPR complex<_Tp>& operator/=(const complex<_Up>&);
244 _GLIBCXX_CONSTEXPR complex __rep() const
252 template<typename _Tp>
253 _GLIBCXX20_CONSTEXPR complex<_Tp>&
254 complex<_Tp>::operator=(const _Tp& __t)
262 template<typename _Tp>
263 _GLIBCXX20_CONSTEXPR complex<_Tp>&
264 complex<_Tp>::operator*=(const _Tp& __t)
272 template<typename _Tp>
273 _GLIBCXX20_CONSTEXPR complex<_Tp>&
274 complex<_Tp>::operator/=(const _Tp& __t)
281 template<typename _Tp>
282 template<typename _Up>
283 _GLIBCXX20_CONSTEXPR complex<_Tp>&
284 complex<_Tp>::operator=(const complex<_Up>& __z)
286 _M_real = __z.real();
287 _M_imag = __z.imag();
292 template<typename _Tp>
293 template<typename _Up>
294 _GLIBCXX20_CONSTEXPR complex<_Tp>&
295 complex<_Tp>::operator+=(const complex<_Up>& __z)
297 _M_real += __z.real();
298 _M_imag += __z.imag();
303 template<typename _Tp>
304 template<typename _Up>
305 _GLIBCXX20_CONSTEXPR complex<_Tp>&
306 complex<_Tp>::operator-=(const complex<_Up>& __z)
308 _M_real -= __z.real();
309 _M_imag -= __z.imag();
314 // XXX: This is a grammar school implementation.
315 template<typename _Tp>
316 template<typename _Up>
317 _GLIBCXX20_CONSTEXPR complex<_Tp>&
318 complex<_Tp>::operator*=(const complex<_Up>& __z)
320 const _Tp __r = _M_real * __z.real() - _M_imag * __z.imag();
321 _M_imag = _M_real * __z.imag() + _M_imag * __z.real();
327 // XXX: This is a grammar school implementation.
328 template<typename _Tp>
329 template<typename _Up>
330 _GLIBCXX20_CONSTEXPR complex<_Tp>&
331 complex<_Tp>::operator/=(const complex<_Up>& __z)
333 const _Tp __r = _M_real * __z.real() + _M_imag * __z.imag();
334 const _Tp __n = std::norm(__z);
335 _M_imag = (_M_imag * __z.real() - _M_real * __z.imag()) / __n;
342 /// Return new complex value @a x plus @a y.
343 template<typename _Tp>
344 inline _GLIBCXX20_CONSTEXPR complex<_Tp>
345 operator+(const complex<_Tp>& __x, const complex<_Tp>& __y)
347 complex<_Tp> __r = __x;
352 template<typename _Tp>
353 inline _GLIBCXX20_CONSTEXPR complex<_Tp>
354 operator+(const complex<_Tp>& __x, const _Tp& __y)
356 complex<_Tp> __r = __x;
361 template<typename _Tp>
362 inline _GLIBCXX20_CONSTEXPR complex<_Tp>
363 operator+(const _Tp& __x, const complex<_Tp>& __y)
365 complex<_Tp> __r = __y;
372 /// Return new complex value @a x minus @a y.
373 template<typename _Tp>
374 inline _GLIBCXX20_CONSTEXPR complex<_Tp>
375 operator-(const complex<_Tp>& __x, const complex<_Tp>& __y)
377 complex<_Tp> __r = __x;
382 template<typename _Tp>
383 inline _GLIBCXX20_CONSTEXPR complex<_Tp>
384 operator-(const complex<_Tp>& __x, const _Tp& __y)
386 complex<_Tp> __r = __x;
391 template<typename _Tp>
392 inline _GLIBCXX20_CONSTEXPR complex<_Tp>
393 operator-(const _Tp& __x, const complex<_Tp>& __y)
395 complex<_Tp> __r = -__y;
402 /// Return new complex value @a x times @a y.
403 template<typename _Tp>
404 inline _GLIBCXX20_CONSTEXPR complex<_Tp>
405 operator*(const complex<_Tp>& __x, const complex<_Tp>& __y)
407 complex<_Tp> __r = __x;
412 template<typename _Tp>
413 inline _GLIBCXX20_CONSTEXPR complex<_Tp>
414 operator*(const complex<_Tp>& __x, const _Tp& __y)
416 complex<_Tp> __r = __x;
421 template<typename _Tp>
422 inline _GLIBCXX20_CONSTEXPR complex<_Tp>
423 operator*(const _Tp& __x, const complex<_Tp>& __y)
425 complex<_Tp> __r = __y;
432 /// Return new complex value @a x divided by @a y.
433 template<typename _Tp>
434 inline _GLIBCXX20_CONSTEXPR complex<_Tp>
435 operator/(const complex<_Tp>& __x, const complex<_Tp>& __y)
437 complex<_Tp> __r = __x;
442 template<typename _Tp>
443 inline _GLIBCXX20_CONSTEXPR complex<_Tp>
444 operator/(const complex<_Tp>& __x, const _Tp& __y)
446 complex<_Tp> __r = __x;
451 template<typename _Tp>
452 inline _GLIBCXX20_CONSTEXPR complex<_Tp>
453 operator/(const _Tp& __x, const complex<_Tp>& __y)
455 complex<_Tp> __r = __x;
462 template<typename _Tp>
463 inline _GLIBCXX20_CONSTEXPR complex<_Tp>
464 operator+(const complex<_Tp>& __x)
467 /// Return complex negation of @a x.
468 template<typename _Tp>
469 inline _GLIBCXX20_CONSTEXPR complex<_Tp>
470 operator-(const complex<_Tp>& __x)
471 { return complex<_Tp>(-__x.real(), -__x.imag()); }
474 /// Return true if @a x is equal to @a y.
475 template<typename _Tp>
476 inline _GLIBCXX_CONSTEXPR bool
477 operator==(const complex<_Tp>& __x, const complex<_Tp>& __y)
478 { return __x.real() == __y.real() && __x.imag() == __y.imag(); }
480 template<typename _Tp>
481 inline _GLIBCXX_CONSTEXPR bool
482 operator==(const complex<_Tp>& __x, const _Tp& __y)
483 { return __x.real() == __y && __x.imag() == _Tp(); }
485#if !(__cpp_impl_three_way_comparison >= 201907L)
486 template<typename _Tp>
487 inline _GLIBCXX_CONSTEXPR bool
488 operator==(const _Tp& __x, const complex<_Tp>& __y)
489 { return __x == __y.real() && _Tp() == __y.imag(); }
493 /// Return false if @a x is equal to @a y.
494 template<typename _Tp>
495 inline _GLIBCXX_CONSTEXPR bool
496 operator!=(const complex<_Tp>& __x, const complex<_Tp>& __y)
497 { return __x.real() != __y.real() || __x.imag() != __y.imag(); }
499 template<typename _Tp>
500 inline _GLIBCXX_CONSTEXPR bool
501 operator!=(const complex<_Tp>& __x, const _Tp& __y)
502 { return __x.real() != __y || __x.imag() != _Tp(); }
504 template<typename _Tp>
505 inline _GLIBCXX_CONSTEXPR bool
506 operator!=(const _Tp& __x, const complex<_Tp>& __y)
507 { return __x != __y.real() || _Tp() != __y.imag(); }
511 /// Extraction operator for complex values.
512 template<typename _Tp, typename _CharT, class _Traits>
513 basic_istream<_CharT, _Traits>&
514 operator>>(basic_istream<_CharT, _Traits>& __is, complex<_Tp>& __x)
520 if (_Traits::eq(__ch, __is.widen('(')))
523 if (__is >> __u >> __ch)
525 const _CharT __rparen = __is.widen(')');
526 if (_Traits::eq(__ch, __rparen))
531 else if (_Traits::eq(__ch, __is.widen(',')))
534 if (__is >> __v >> __ch)
536 if (_Traits::eq(__ch, __rparen))
538 __x = complex<_Tp>(__u, __v);
561 __is.setstate(ios_base::failbit);
565 /// Insertion operator for complex values.
566 template<typename _Tp, typename _CharT, class _Traits>
567 basic_ostream<_CharT, _Traits>&
568 operator<<(basic_ostream<_CharT, _Traits>& __os, const complex<_Tp>& __x)
570 basic_ostringstream<_CharT, _Traits> __s;
571 __s.flags(__os.flags());
572 __s.imbue(__os.getloc());
573 __s.precision(__os.precision());
574 __s << '(' << __x.real() << ',' << __x.imag() << ')';
575 return __os << __s.str();
579#if __cplusplus >= 201103L
580 template<typename _Tp>
582 real(const complex<_Tp>& __z)
583 { return __z.real(); }
585 template<typename _Tp>
587 imag(const complex<_Tp>& __z)
588 { return __z.imag(); }
590 template<typename _Tp>
592 real(complex<_Tp>& __z)
593 { return __z.real(); }
595 template<typename _Tp>
597 real(const complex<_Tp>& __z)
598 { return __z.real(); }
600 template<typename _Tp>
602 imag(complex<_Tp>& __z)
603 { return __z.imag(); }
605 template<typename _Tp>
607 imag(const complex<_Tp>& __z)
608 { return __z.imag(); }
611#if _GLIBCXX_USE_C99_COMPLEX
612#if defined(__STDCPP_FLOAT16_T__) && defined(_GLIBCXX_FLOAT_IS_IEEE_BINARY32)
614 __complex_abs(__complex__ _Float16 __z)
615 { return _Float16(__builtin_cabsf(__z)); }
618 __complex_arg(__complex__ _Float16 __z)
619 { return _Float16(__builtin_cargf(__z)); }
621 inline __complex__ _Float16
622 __complex_cos(__complex__ _Float16 __z)
623 { return static_cast<__complex__ _Float16>(__builtin_ccosf(__z)); }
625 inline __complex__ _Float16
626 __complex_cosh(__complex__ _Float16 __z)
627 { return static_cast<__complex__ _Float16>(__builtin_ccoshf(__z)); }
629 inline __complex__ _Float16
630 __complex_exp(__complex__ _Float16 __z)
631 { return static_cast<__complex__ _Float16>(__builtin_cexpf(__z)); }
633 inline __complex__ _Float16
634 __complex_log(__complex__ _Float16 __z)
635 { return static_cast<__complex__ _Float16>(__builtin_clogf(__z)); }
637 inline __complex__ _Float16
638 __complex_sin(__complex__ _Float16 __z)
639 { return static_cast<__complex__ _Float16>(__builtin_csinf(__z)); }
641 inline __complex__ _Float16
642 __complex_sinh(__complex__ _Float16 __z)
643 { return static_cast<__complex__ _Float16>(__builtin_csinhf(__z)); }
645 inline __complex__ _Float16
646 __complex_sqrt(__complex__ _Float16 __z)
647 { return static_cast<__complex__ _Float16>(__builtin_csqrtf(__z)); }
649 inline __complex__ _Float16
650 __complex_tan(__complex__ _Float16 __z)
651 { return static_cast<__complex__ _Float16>(__builtin_ctanf(__z)); }
653 inline __complex__ _Float16
654 __complex_tanh(__complex__ _Float16 __z)
655 { return static_cast<__complex__ _Float16>(__builtin_ctanhf(__z)); }
657 inline __complex__ _Float16
658 __complex_pow(__complex__ _Float16 __x, __complex__ _Float16 __y)
659 { return static_cast<__complex__ _Float16>(__builtin_cpowf(__x, __y)); }
662#if defined(__STDCPP_FLOAT32_T__) && defined(_GLIBCXX_FLOAT_IS_IEEE_BINARY32)
664 __complex_abs(__complex__ _Float32 __z) { return __builtin_cabsf(__z); }
667 __complex_arg(__complex__ _Float32 __z) { return __builtin_cargf(__z); }
669 inline __complex__ _Float32
670 __complex_cos(__complex__ _Float32 __z) { return __builtin_ccosf(__z); }
672 inline __complex__ _Float32
673 __complex_cosh(__complex__ _Float32 __z) { return __builtin_ccoshf(__z); }
675 inline __complex__ _Float32
676 __complex_exp(__complex__ _Float32 __z) { return __builtin_cexpf(__z); }
678 inline __complex__ _Float32
679 __complex_log(__complex__ _Float32 __z) { return __builtin_clogf(__z); }
681 inline __complex__ _Float32
682 __complex_sin(__complex__ _Float32 __z) { return __builtin_csinf(__z); }
684 inline __complex__ _Float32
685 __complex_sinh(__complex__ _Float32 __z) { return __builtin_csinhf(__z); }
687 inline __complex__ _Float32
688 __complex_sqrt(__complex__ _Float32 __z) { return __builtin_csqrtf(__z); }
690 inline __complex__ _Float32
691 __complex_tan(__complex__ _Float32 __z) { return __builtin_ctanf(__z); }
693 inline __complex__ _Float32
694 __complex_tanh(__complex__ _Float32 __z) { return __builtin_ctanhf(__z); }
696 inline __complex__ _Float32
697 __complex_pow(__complex__ _Float32 __x, __complex__ _Float32 __y)
698 { return __builtin_cpowf(__x, __y); }
701#if defined(__STDCPP_FLOAT64_T__) && defined(_GLIBCXX_DOUBLE_IS_IEEE_BINARY64)
703 __complex_abs(__complex__ _Float64 __z) { return __builtin_cabs(__z); }
706 __complex_arg(__complex__ _Float64 __z) { return __builtin_carg(__z); }
708 inline __complex__ _Float64
709 __complex_cos(__complex__ _Float64 __z) { return __builtin_ccos(__z); }
711 inline __complex__ _Float64
712 __complex_cosh(__complex__ _Float64 __z) { return __builtin_ccosh(__z); }
714 inline __complex__ _Float64
715 __complex_exp(__complex__ _Float64 __z) { return __builtin_cexp(__z); }
717 inline __complex__ _Float64
718 __complex_log(__complex__ _Float64 __z) { return __builtin_clog(__z); }
720 inline __complex__ _Float64
721 __complex_sin(__complex__ _Float64 __z) { return __builtin_csin(__z); }
723 inline __complex__ _Float64
724 __complex_sinh(__complex__ _Float64 __z) { return __builtin_csinh(__z); }
726 inline __complex__ _Float64
727 __complex_sqrt(__complex__ _Float64 __z) { return __builtin_csqrt(__z); }
729 inline __complex__ _Float64
730 __complex_tan(__complex__ _Float64 __z) { return __builtin_ctan(__z); }
732 inline __complex__ _Float64
733 __complex_tanh(__complex__ _Float64 __z) { return __builtin_ctanh(__z); }
735 inline __complex__ _Float64
736 __complex_pow(__complex__ _Float64 __x, __complex__ _Float64 __y)
737 { return __builtin_cpow(__x, __y); }
740#if defined(__STDCPP_FLOAT128_T__) && defined(_GLIBCXX_LDOUBLE_IS_IEEE_BINARY128)
742 __complex_abs(__complex__ _Float128 __z) { return __builtin_cabsl(__z); }
745 __complex_arg(__complex__ _Float128 __z) { return __builtin_cargl(__z); }
747 inline __complex__ _Float128
748 __complex_cos(__complex__ _Float128 __z) { return __builtin_ccosl(__z); }
750 inline __complex__ _Float128
751 __complex_cosh(__complex__ _Float128 __z) { return __builtin_ccoshl(__z); }
753 inline __complex__ _Float128
754 __complex_exp(__complex__ _Float128 __z) { return __builtin_cexpl(__z); }
756 inline __complex__ _Float128
757 __complex_log(__complex__ _Float128 __z) { return __builtin_clogl(__z); }
759 inline __complex__ _Float128
760 __complex_sin(__complex__ _Float128 __z) { return __builtin_csinl(__z); }
762 inline __complex__ _Float128
763 __complex_sinh(__complex__ _Float128 __z) { return __builtin_csinhl(__z); }
765 inline __complex__ _Float128
766 __complex_sqrt(__complex__ _Float128 __z) { return __builtin_csqrtl(__z); }
768 inline __complex__ _Float128
769 __complex_tan(__complex__ _Float128 __z) { return __builtin_ctanl(__z); }
771 inline __complex__ _Float128
772 __complex_tanh(__complex__ _Float128 __z) { return __builtin_ctanhl(__z); }
774 inline __complex__ _Float128
775 __complex_pow(__complex__ _Float128 __x, __complex__ _Float128 __y)
776 { return __builtin_cpowl(__x, __y); }
777#elif defined(__STDCPP_FLOAT128_T__) && defined(_GLIBCXX_HAVE_FLOAT128_MATH)
779 __complex_abs(__complex__ _Float128 __z) { return __builtin_cabsf128(__z); }
782 __complex_arg(__complex__ _Float128 __z) { return __builtin_cargf128(__z); }
784 inline __complex__ _Float128
785 __complex_cos(__complex__ _Float128 __z) { return __builtin_ccosf128(__z); }
787 inline __complex__ _Float128
788 __complex_cosh(__complex__ _Float128 __z) { return __builtin_ccoshf128(__z); }
790 inline __complex__ _Float128
791 __complex_exp(__complex__ _Float128 __z) { return __builtin_cexpf128(__z); }
793 inline __complex__ _Float128
794 __complex_log(__complex__ _Float128 __z) { return __builtin_clogf128(__z); }
796 inline __complex__ _Float128
797 __complex_sin(__complex__ _Float128 __z) { return __builtin_csinf128(__z); }
799 inline __complex__ _Float128
800 __complex_sinh(__complex__ _Float128 __z) { return __builtin_csinhf128(__z); }
802 inline __complex__ _Float128
803 __complex_sqrt(__complex__ _Float128 __z) { return __builtin_csqrtf128(__z); }
805 inline __complex__ _Float128
806 __complex_tan(__complex__ _Float128 __z) { return __builtin_ctanf128(__z); }
808 inline __complex__ _Float128
809 __complex_tanh(__complex__ _Float128 __z) { return __builtin_ctanhf128(__z); }
811 inline __complex__ _Float128
812 __complex_pow(__complex__ _Float128 __x, __complex__ _Float128 __y)
813 { return __builtin_cpowf128(__x, __y); }
816#if defined(__STDCPP_BFLOAT16_T__) && defined(_GLIBCXX_FLOAT_IS_IEEE_BINARY32)
817 inline __gnu_cxx::__bfloat16_t
818 __complex_abs(__complex__ decltype(0.0bf16) __z)
819 { return __gnu_cxx::__bfloat16_t(__builtin_cabsf(__z)); }
821 inline __gnu_cxx::__bfloat16_t
822 __complex_arg(__complex__ decltype(0.0bf16) __z)
823 { return __gnu_cxx::__bfloat16_t(__builtin_cargf(__z)); }
825 inline __complex__ decltype(0.0bf16)
826 __complex_cos(__complex__ decltype(0.0bf16) __z)
827 { return static_cast<__complex__ decltype(0.0bf16)>(__builtin_ccosf(__z)); }
829 inline __complex__ decltype(0.0bf16)
830 __complex_cosh(__complex__ decltype(0.0bf16) __z)
831 { return static_cast<__complex__ decltype(0.0bf16)>(__builtin_ccoshf(__z)); }
833 inline __complex__ decltype(0.0bf16)
834 __complex_exp(__complex__ decltype(0.0bf16) __z)
835 { return static_cast<__complex__ decltype(0.0bf16)>(__builtin_cexpf(__z)); }
837 inline __complex__ decltype(0.0bf16)
838 __complex_log(__complex__ decltype(0.0bf16) __z)
839 { return static_cast<__complex__ decltype(0.0bf16)>(__builtin_clogf(__z)); }
841 inline __complex__ decltype(0.0bf16)
842 __complex_sin(__complex__ decltype(0.0bf16) __z)
843 { return static_cast<__complex__ decltype(0.0bf16)>(__builtin_csinf(__z)); }
845 inline __complex__ decltype(0.0bf16)
846 __complex_sinh(__complex__ decltype(0.0bf16) __z)
847 { return static_cast<__complex__ decltype(0.0bf16)>(__builtin_csinhf(__z)); }
849 inline __complex__ decltype(0.0bf16)
850 __complex_sqrt(__complex__ decltype(0.0bf16) __z)
851 { return static_cast<__complex__ decltype(0.0bf16)>(__builtin_csqrtf(__z)); }
853 inline __complex__ decltype(0.0bf16)
854 __complex_tan(__complex__ decltype(0.0bf16) __z)
855 { return static_cast<__complex__ decltype(0.0bf16)>(__builtin_ctanf(__z)); }
857 inline __complex__ decltype(0.0bf16)
858 __complex_tanh(__complex__ decltype(0.0bf16) __z)
859 { return static_cast<__complex__ decltype(0.0bf16)>(__builtin_ctanhf(__z)); }
861 inline __complex__ decltype(0.0bf16)
862 __complex_pow(__complex__ decltype(0.0bf16) __x,
863 __complex__ decltype(0.0bf16) __y)
864 { return static_cast<__complex__ decltype(0.0bf16)>(__builtin_cpowf(__x,
869 // 26.2.7/3 abs(__z): Returns the magnitude of __z.
870 template<typename _Tp>
872 __complex_abs(const complex<_Tp>& __z)
874 _Tp __x = __z.real();
875 _Tp __y = __z.imag();
876 const _Tp __s = std::max(abs(__x), abs(__y));
877 if (__s == _Tp()) // well ...
881 return __s * sqrt(__x * __x + __y * __y);
884#if _GLIBCXX_USE_C99_COMPLEX
886 __complex_abs(__complex__ float __z) { return __builtin_cabsf(__z); }
889 __complex_abs(__complex__ double __z) { return __builtin_cabs(__z); }
892 __complex_abs(const __complex__ long double& __z)
893 { return __builtin_cabsl(__z); }
895 template<typename _Tp>
897 abs(const complex<_Tp>& __z) { return __complex_abs(__z.__rep()); }
899 template<typename _Tp>
901 abs(const complex<_Tp>& __z) { return __complex_abs(__z); }
905 // 26.2.7/4: arg(__z): Returns the phase angle of __z.
906 template<typename _Tp>
908 __complex_arg(const complex<_Tp>& __z)
909 { return atan2(__z.imag(), __z.real()); }
911#if _GLIBCXX_USE_C99_COMPLEX
913 __complex_arg(__complex__ float __z) { return __builtin_cargf(__z); }
916 __complex_arg(__complex__ double __z) { return __builtin_carg(__z); }
919 __complex_arg(const __complex__ long double& __z)
920 { return __builtin_cargl(__z); }
922 template<typename _Tp>
924 arg(const complex<_Tp>& __z) { return __complex_arg(__z.__rep()); }
926 template<typename _Tp>
928 arg(const complex<_Tp>& __z) { return __complex_arg(__z); }
931 // 26.2.7/5: norm(__z) returns the squared magnitude of __z.
932 // As defined, norm() is -not- a norm is the common mathematical
933 // sense used in numerics. The helper class _Norm_helper<> tries to
934 // distinguish between builtin floating point and the rest, so as
935 // to deliver an answer as close as possible to the real value.
939 template<typename _Tp>
940 static inline _GLIBCXX20_CONSTEXPR _Tp _S_do_it(const complex<_Tp>& __z)
942 const _Tp __x = __z.real();
943 const _Tp __y = __z.imag();
944 return __x * __x + __y * __y;
949 struct _Norm_helper<true>
951 template<typename _Tp>
952 static inline _GLIBCXX20_CONSTEXPR _Tp _S_do_it(const complex<_Tp>& __z)
954 //_Tp __res = std::abs(__z);
955 //return __res * __res;
956 const _Tp __x = __z.real();
957 const _Tp __y = __z.imag();
958 return __x * __x + __y * __y;
962 template<typename _Tp>
963 inline _GLIBCXX20_CONSTEXPR _Tp
964 norm(const complex<_Tp>& __z)
966 return _Norm_helper<__is_floating<_Tp>::__value
967 && !_GLIBCXX_FAST_MATH>::_S_do_it(__z);
970 template<typename _Tp>
972 polar(const _Tp& __rho, const _Tp& __theta)
974 __glibcxx_assert( __rho >= 0 );
975 return complex<_Tp>(__rho * cos(__theta), __rho * sin(__theta));
978 template<typename _Tp>
979 inline _GLIBCXX20_CONSTEXPR complex<_Tp>
980 conj(const complex<_Tp>& __z)
981 { return complex<_Tp>(__z.real(), -__z.imag()); }
985 // 26.2.8/1 cos(__z): Returns the cosine of __z.
986 template<typename _Tp>
988 __complex_cos(const complex<_Tp>& __z)
990 const _Tp __x = __z.real();
991 const _Tp __y = __z.imag();
992 return complex<_Tp>(cos(__x) * cosh(__y), -sin(__x) * sinh(__y));
995#if _GLIBCXX_USE_C99_COMPLEX
996 inline __complex__ float
997 __complex_cos(__complex__ float __z) { return __builtin_ccosf(__z); }
999 inline __complex__ double
1000 __complex_cos(__complex__ double __z) { return __builtin_ccos(__z); }
1002 inline __complex__ long double
1003 __complex_cos(const __complex__ long double& __z)
1004 { return __builtin_ccosl(__z); }
1006 template<typename _Tp>
1008 cos(const complex<_Tp>& __z) { return __complex_cos(__z.__rep()); }
1010 template<typename _Tp>
1012 cos(const complex<_Tp>& __z) { return __complex_cos(__z); }
1015 // 26.2.8/2 cosh(__z): Returns the hyperbolic cosine of __z.
1016 template<typename _Tp>
1018 __complex_cosh(const complex<_Tp>& __z)
1020 const _Tp __x = __z.real();
1021 const _Tp __y = __z.imag();
1022 return complex<_Tp>(cosh(__x) * cos(__y), sinh(__x) * sin(__y));
1025#if _GLIBCXX_USE_C99_COMPLEX
1026 inline __complex__ float
1027 __complex_cosh(__complex__ float __z) { return __builtin_ccoshf(__z); }
1029 inline __complex__ double
1030 __complex_cosh(__complex__ double __z) { return __builtin_ccosh(__z); }
1032 inline __complex__ long double
1033 __complex_cosh(const __complex__ long double& __z)
1034 { return __builtin_ccoshl(__z); }
1036 template<typename _Tp>
1038 cosh(const complex<_Tp>& __z) { return __complex_cosh(__z.__rep()); }
1040 template<typename _Tp>
1042 cosh(const complex<_Tp>& __z) { return __complex_cosh(__z); }
1045 // 26.2.8/3 exp(__z): Returns the complex base e exponential of x
1046 template<typename _Tp>
1048 __complex_exp(const complex<_Tp>& __z)
1049 { return std::polar<_Tp>(exp(__z.real()), __z.imag()); }
1051#if _GLIBCXX_USE_C99_COMPLEX
1052 inline __complex__ float
1053 __complex_exp(__complex__ float __z) { return __builtin_cexpf(__z); }
1055 inline __complex__ double
1056 __complex_exp(__complex__ double __z) { return __builtin_cexp(__z); }
1058 inline __complex__ long double
1059 __complex_exp(const __complex__ long double& __z)
1060 { return __builtin_cexpl(__z); }
1062 template<typename _Tp>
1064 exp(const complex<_Tp>& __z) { return __complex_exp(__z.__rep()); }
1066 template<typename _Tp>
1068 exp(const complex<_Tp>& __z) { return __complex_exp(__z); }
1071 // 26.2.8/5 log(__z): Returns the natural complex logarithm of __z.
1072 // The branch cut is along the negative axis.
1073 template<typename _Tp>
1075 __complex_log(const complex<_Tp>& __z)
1076 { return complex<_Tp>(log(std::abs(__z)), std::arg(__z)); }
1078#if _GLIBCXX_USE_C99_COMPLEX
1079 inline __complex__ float
1080 __complex_log(__complex__ float __z) { return __builtin_clogf(__z); }
1082 inline __complex__ double
1083 __complex_log(__complex__ double __z) { return __builtin_clog(__z); }
1085 inline __complex__ long double
1086 __complex_log(const __complex__ long double& __z)
1087 { return __builtin_clogl(__z); }
1089 template<typename _Tp>
1091 log(const complex<_Tp>& __z) { return __complex_log(__z.__rep()); }
1093 template<typename _Tp>
1095 log(const complex<_Tp>& __z) { return __complex_log(__z); }
1098 template<typename _Tp>
1100 log10(const complex<_Tp>& __z)
1101 { return std::log(__z) / log(_Tp(10.0)); }
1103 // 26.2.8/10 sin(__z): Returns the sine of __z.
1104 template<typename _Tp>
1106 __complex_sin(const complex<_Tp>& __z)
1108 const _Tp __x = __z.real();
1109 const _Tp __y = __z.imag();
1110 return complex<_Tp>(sin(__x) * cosh(__y), cos(__x) * sinh(__y));
1113#if _GLIBCXX_USE_C99_COMPLEX
1114 inline __complex__ float
1115 __complex_sin(__complex__ float __z) { return __builtin_csinf(__z); }
1117 inline __complex__ double
1118 __complex_sin(__complex__ double __z) { return __builtin_csin(__z); }
1120 inline __complex__ long double
1121 __complex_sin(const __complex__ long double& __z)
1122 { return __builtin_csinl(__z); }
1124 template<typename _Tp>
1126 sin(const complex<_Tp>& __z) { return __complex_sin(__z.__rep()); }
1128 template<typename _Tp>
1130 sin(const complex<_Tp>& __z) { return __complex_sin(__z); }
1133 // 26.2.8/11 sinh(__z): Returns the hyperbolic sine of __z.
1134 template<typename _Tp>
1136 __complex_sinh(const complex<_Tp>& __z)
1138 const _Tp __x = __z.real();
1139 const _Tp __y = __z.imag();
1140 return complex<_Tp>(sinh(__x) * cos(__y), cosh(__x) * sin(__y));
1143#if _GLIBCXX_USE_C99_COMPLEX
1144 inline __complex__ float
1145 __complex_sinh(__complex__ float __z) { return __builtin_csinhf(__z); }
1147 inline __complex__ double
1148 __complex_sinh(__complex__ double __z) { return __builtin_csinh(__z); }
1150 inline __complex__ long double
1151 __complex_sinh(const __complex__ long double& __z)
1152 { return __builtin_csinhl(__z); }
1154 template<typename _Tp>
1156 sinh(const complex<_Tp>& __z) { return __complex_sinh(__z.__rep()); }
1158 template<typename _Tp>
1160 sinh(const complex<_Tp>& __z) { return __complex_sinh(__z); }
1163 // 26.2.8/13 sqrt(__z): Returns the complex square root of __z.
1164 // The branch cut is on the negative axis.
1165 template<typename _Tp>
1167 __complex_sqrt(const complex<_Tp>& __z)
1169 _Tp __x = __z.real();
1170 _Tp __y = __z.imag();
1174 _Tp __t = sqrt(abs(__y) / 2);
1175 return complex<_Tp>(__t, __y < _Tp() ? -__t : __t);
1179 _Tp __t = sqrt(2 * (std::abs(__z) + abs(__x)));
1182 ? complex<_Tp>(__u, __y / __t)
1183 : complex<_Tp>(abs(__y) / __t, __y < _Tp() ? -__u : __u);
1187#if _GLIBCXX_USE_C99_COMPLEX
1188 inline __complex__ float
1189 __complex_sqrt(__complex__ float __z) { return __builtin_csqrtf(__z); }
1191 inline __complex__ double
1192 __complex_sqrt(__complex__ double __z) { return __builtin_csqrt(__z); }
1194 inline __complex__ long double
1195 __complex_sqrt(const __complex__ long double& __z)
1196 { return __builtin_csqrtl(__z); }
1198 template<typename _Tp>
1200 sqrt(const complex<_Tp>& __z) { return __complex_sqrt(__z.__rep()); }
1202 template<typename _Tp>
1204 sqrt(const complex<_Tp>& __z) { return __complex_sqrt(__z); }
1207 // 26.2.8/14 tan(__z): Return the complex tangent of __z.
1209 template<typename _Tp>
1211 __complex_tan(const complex<_Tp>& __z)
1212 { return std::sin(__z) / std::cos(__z); }
1214#if _GLIBCXX_USE_C99_COMPLEX
1215 inline __complex__ float
1216 __complex_tan(__complex__ float __z) { return __builtin_ctanf(__z); }
1218 inline __complex__ double
1219 __complex_tan(__complex__ double __z) { return __builtin_ctan(__z); }
1221 inline __complex__ long double
1222 __complex_tan(const __complex__ long double& __z)
1223 { return __builtin_ctanl(__z); }
1225 template<typename _Tp>
1227 tan(const complex<_Tp>& __z) { return __complex_tan(__z.__rep()); }
1229 template<typename _Tp>
1231 tan(const complex<_Tp>& __z) { return __complex_tan(__z); }
1235 // 26.2.8/15 tanh(__z): Returns the hyperbolic tangent of __z.
1237 template<typename _Tp>
1239 __complex_tanh(const complex<_Tp>& __z)
1240 { return std::sinh(__z) / std::cosh(__z); }
1242#if _GLIBCXX_USE_C99_COMPLEX
1243 inline __complex__ float
1244 __complex_tanh(__complex__ float __z) { return __builtin_ctanhf(__z); }
1246 inline __complex__ double
1247 __complex_tanh(__complex__ double __z) { return __builtin_ctanh(__z); }
1249 inline __complex__ long double
1250 __complex_tanh(const __complex__ long double& __z)
1251 { return __builtin_ctanhl(__z); }
1253 template<typename _Tp>
1255 tanh(const complex<_Tp>& __z) { return __complex_tanh(__z.__rep()); }
1257 template<typename _Tp>
1259 tanh(const complex<_Tp>& __z) { return __complex_tanh(__z); }
1263 // 26.2.8/9 pow(__x, __y): Returns the complex power base of __x
1264 // raised to the __y-th power. The branch
1265 // cut is on the negative axis.
1266 template<typename _Tp>
1268 __complex_pow_unsigned(complex<_Tp> __x, unsigned __n)
1270 complex<_Tp> __y = __n % 2 ? __x : complex<_Tp>(1);
1282 // In C++11 mode we used to implement the resolution of
1283 // DR 844. complex pow return type is ambiguous.
1284 // thus the following overload was disabled in that mode. However, doing
1285 // that causes all sorts of issues, see, for example:
1286 // http://gcc.gnu.org/ml/libstdc++/2013-01/msg00058.html
1287 // and also PR57974.
1288 template<typename _Tp>
1290 pow(const complex<_Tp>& __z, int __n)
1293 ? complex<_Tp>(1) / std::__complex_pow_unsigned(__z, -(unsigned)__n)
1294 : std::__complex_pow_unsigned(__z, __n);
1297 template<typename _Tp>
1299 pow(const complex<_Tp>& __x, const _Tp& __y)
1301#if ! _GLIBCXX_USE_C99_COMPLEX
1305 if (__x.imag() == _Tp() && __x.real() > _Tp())
1306 return pow(__x.real(), __y);
1308 complex<_Tp> __t = std::log(__x);
1309 return std::polar<_Tp>(exp(__y * __t.real()), __y * __t.imag());
1312 template<typename _Tp>
1314 __complex_pow(const complex<_Tp>& __x, const complex<_Tp>& __y)
1315 { return __x == _Tp() ? _Tp() : std::exp(__y * std::log(__x)); }
1317#if _GLIBCXX_USE_C99_COMPLEX
1318 inline __complex__ float
1319 __complex_pow(__complex__ float __x, __complex__ float __y)
1320 { return __builtin_cpowf(__x, __y); }
1322 inline __complex__ double
1323 __complex_pow(__complex__ double __x, __complex__ double __y)
1324 { return __builtin_cpow(__x, __y); }
1326 inline __complex__ long double
1327 __complex_pow(const __complex__ long double& __x,
1328 const __complex__ long double& __y)
1329 { return __builtin_cpowl(__x, __y); }
1331 template<typename _Tp>
1333 pow(const complex<_Tp>& __x, const complex<_Tp>& __y)
1334 { return __complex_pow(__x.__rep(), __y.__rep()); }
1336 template<typename _Tp>
1338 pow(const complex<_Tp>& __x, const complex<_Tp>& __y)
1339 { return __complex_pow(__x, __y); }
1342 template<typename _Tp>
1344 pow(const _Tp& __x, const complex<_Tp>& __y)
1346 return __x > _Tp() ? std::polar<_Tp>(pow(__x, __y.real()),
1347 __y.imag() * log(__x))
1348 : std::pow(complex<_Tp>(__x), __y);
1351 /// 26.2.3 complex specializations
1352 /// complex<float> specialization
1354 class complex<float>
1357 typedef float value_type;
1358 typedef __complex__ float _ComplexT;
1360 _GLIBCXX_CONSTEXPR complex(_ComplexT __z) : _M_value(__z) { }
1362 _GLIBCXX_CONSTEXPR complex(float __r = 0.0f, float __i = 0.0f)
1363#if __cplusplus >= 201103L
1364 : _M_value{ __r, __i } { }
1367 __real__ _M_value = __r;
1368 __imag__ _M_value = __i;
1372#if __cplusplus >= 201103L
1373 _GLIBCXX14_CONSTEXPR complex(const complex&) = default;
1376#if __cplusplus > 202002L
1377 template<typename _Up>
1378 explicit(!requires(_Up __u) { value_type{__u}; })
1379 constexpr complex(const complex<_Up>& __z)
1380 : _M_value{ value_type(__z.real()), value_type(__z.imag()) } { }
1382 explicit _GLIBCXX_CONSTEXPR complex(const complex<double>&);
1383 explicit _GLIBCXX_CONSTEXPR complex(const complex<long double>&);
1386#if __cplusplus >= 201103L
1387 // _GLIBCXX_RESOLVE_LIB_DEFECTS
1388 // DR 387. std::complex over-encapsulated.
1389 __attribute ((__abi_tag__ ("cxx11")))
1391 real() const { return __real__ _M_value; }
1393 __attribute ((__abi_tag__ ("cxx11")))
1395 imag() const { return __imag__ _M_value; }
1398 real() { return __real__ _M_value; }
1401 real() const { return __real__ _M_value; }
1404 imag() { return __imag__ _M_value; }
1407 imag() const { return __imag__ _M_value; }
1410 // _GLIBCXX_RESOLVE_LIB_DEFECTS
1411 // DR 387. std::complex over-encapsulated.
1412 _GLIBCXX20_CONSTEXPR void
1413 real(float __val) { __real__ _M_value = __val; }
1415 _GLIBCXX20_CONSTEXPR void
1416 imag(float __val) { __imag__ _M_value = __val; }
1418 _GLIBCXX20_CONSTEXPR complex&
1419 operator=(float __f)
1425 _GLIBCXX20_CONSTEXPR complex&
1426 operator+=(float __f)
1432 _GLIBCXX20_CONSTEXPR complex&
1433 operator-=(float __f)
1439 _GLIBCXX20_CONSTEXPR complex&
1440 operator*=(float __f)
1446 _GLIBCXX20_CONSTEXPR complex&
1447 operator/=(float __f)
1453 // Let the compiler synthesize the copy and assignment
1454 // operator. It always does a pretty good job.
1455#if __cplusplus >= 201103L
1456 _GLIBCXX14_CONSTEXPR complex& operator=(const complex&) = default;
1459 template<typename _Tp>
1460 _GLIBCXX20_CONSTEXPR complex&
1461 operator=(const complex<_Tp>& __z)
1463 __real__ _M_value = __z.real();
1464 __imag__ _M_value = __z.imag();
1468 template<typename _Tp>
1469 _GLIBCXX20_CONSTEXPR complex&
1470 operator+=(const complex<_Tp>& __z)
1472 _M_value += __z.__rep();
1477 _GLIBCXX20_CONSTEXPR complex&
1478 operator-=(const complex<_Tp>& __z)
1480 _M_value -= __z.__rep();
1485 _GLIBCXX20_CONSTEXPR complex&
1486 operator*=(const complex<_Tp>& __z)
1488 const _ComplexT __t = __z.__rep();
1494 _GLIBCXX20_CONSTEXPR complex&
1495 operator/=(const complex<_Tp>& __z)
1497 const _ComplexT __t = __z.__rep();
1502 _GLIBCXX_CONSTEXPR _ComplexT __rep() const { return _M_value; }
1508 /// 26.2.3 complex specializations
1509 /// complex<double> specialization
1511 class complex<double>
1514 typedef double value_type;
1515 typedef __complex__ double _ComplexT;
1517 _GLIBCXX_CONSTEXPR complex(_ComplexT __z) : _M_value(__z) { }
1519 _GLIBCXX_CONSTEXPR complex(double __r = 0.0, double __i = 0.0)
1520#if __cplusplus >= 201103L
1521 : _M_value{ __r, __i } { }
1524 __real__ _M_value = __r;
1525 __imag__ _M_value = __i;
1529#if __cplusplus >= 201103L
1530 _GLIBCXX14_CONSTEXPR complex(const complex&) = default;
1533#if __cplusplus > 202002L
1534 template<typename _Up>
1535 explicit(!requires(_Up __u) { value_type{__u}; })
1536 constexpr complex(const complex<_Up>& __z)
1537 : _M_value{ value_type(__z.real()), value_type(__z.imag()) } { }
1539 _GLIBCXX_CONSTEXPR complex(const complex<float>& __z)
1540 : _M_value(__z.__rep()) { }
1542 explicit _GLIBCXX_CONSTEXPR complex(const complex<long double>&);
1545#if __cplusplus >= 201103L
1546 // _GLIBCXX_RESOLVE_LIB_DEFECTS
1547 // DR 387. std::complex over-encapsulated.
1548 __attribute ((__abi_tag__ ("cxx11")))
1550 real() const { return __real__ _M_value; }
1552 __attribute ((__abi_tag__ ("cxx11")))
1554 imag() const { return __imag__ _M_value; }
1557 real() { return __real__ _M_value; }
1560 real() const { return __real__ _M_value; }
1563 imag() { return __imag__ _M_value; }
1566 imag() const { return __imag__ _M_value; }
1569 // _GLIBCXX_RESOLVE_LIB_DEFECTS
1570 // DR 387. std::complex over-encapsulated.
1571 _GLIBCXX20_CONSTEXPR void
1572 real(double __val) { __real__ _M_value = __val; }
1574 _GLIBCXX20_CONSTEXPR void
1575 imag(double __val) { __imag__ _M_value = __val; }
1577 _GLIBCXX20_CONSTEXPR complex&
1578 operator=(double __d)
1584 _GLIBCXX20_CONSTEXPR complex&
1585 operator+=(double __d)
1591 _GLIBCXX20_CONSTEXPR complex&
1592 operator-=(double __d)
1598 _GLIBCXX20_CONSTEXPR complex&
1599 operator*=(double __d)
1605 _GLIBCXX20_CONSTEXPR complex&
1606 operator/=(double __d)
1612 // The compiler will synthesize this, efficiently.
1613#if __cplusplus >= 201103L
1614 _GLIBCXX14_CONSTEXPR complex& operator=(const complex&) = default;
1617 template<typename _Tp>
1618 _GLIBCXX20_CONSTEXPR complex&
1619 operator=(const complex<_Tp>& __z)
1621 _M_value = __z.__rep();
1625 template<typename _Tp>
1626 _GLIBCXX20_CONSTEXPR complex&
1627 operator+=(const complex<_Tp>& __z)
1629 _M_value += __z.__rep();
1633 template<typename _Tp>
1634 _GLIBCXX20_CONSTEXPR complex&
1635 operator-=(const complex<_Tp>& __z)
1637 _M_value -= __z.__rep();
1641 template<typename _Tp>
1642 _GLIBCXX20_CONSTEXPR complex&
1643 operator*=(const complex<_Tp>& __z)
1645 const _ComplexT __t = __z.__rep();
1650 template<typename _Tp>
1651 _GLIBCXX20_CONSTEXPR complex&
1652 operator/=(const complex<_Tp>& __z)
1654 const _ComplexT __t = __z.__rep();
1659 _GLIBCXX_CONSTEXPR _ComplexT __rep() const { return _M_value; }
1665 /// 26.2.3 complex specializations
1666 /// complex<long double> specialization
1668 class complex<long double>
1671 typedef long double value_type;
1672 typedef __complex__ long double _ComplexT;
1674 _GLIBCXX_CONSTEXPR complex(_ComplexT __z) : _M_value(__z) { }
1676 _GLIBCXX_CONSTEXPR complex(long double __r = 0.0L,
1677 long double __i = 0.0L)
1678#if __cplusplus >= 201103L
1679 : _M_value{ __r, __i } { }
1682 __real__ _M_value = __r;
1683 __imag__ _M_value = __i;
1687#if __cplusplus >= 201103L
1688 _GLIBCXX14_CONSTEXPR complex(const complex&) = default;
1691#if __cplusplus > 202002L
1692 template<typename _Up>
1693 explicit(!requires(_Up __u) { value_type{__u}; })
1694 constexpr complex(const complex<_Up>& __z)
1695 : _M_value{ value_type(__z.real()), value_type(__z.imag()) } { }
1697 _GLIBCXX_CONSTEXPR complex(const complex<float>& __z)
1698 : _M_value(__z.__rep()) { }
1700 _GLIBCXX_CONSTEXPR complex(const complex<double>& __z)
1701 : _M_value(__z.__rep()) { }
1704#if __cplusplus >= 201103L
1705 // _GLIBCXX_RESOLVE_LIB_DEFECTS
1706 // DR 387. std::complex over-encapsulated.
1707 __attribute ((__abi_tag__ ("cxx11")))
1708 constexpr long double
1709 real() const { return __real__ _M_value; }
1711 __attribute ((__abi_tag__ ("cxx11")))
1712 constexpr long double
1713 imag() const { return __imag__ _M_value; }
1716 real() { return __real__ _M_value; }
1719 real() const { return __real__ _M_value; }
1722 imag() { return __imag__ _M_value; }
1725 imag() const { return __imag__ _M_value; }
1728 // _GLIBCXX_RESOLVE_LIB_DEFECTS
1729 // DR 387. std::complex over-encapsulated.
1730 _GLIBCXX20_CONSTEXPR void
1731 real(long double __val) { __real__ _M_value = __val; }
1733 _GLIBCXX20_CONSTEXPR void
1734 imag(long double __val) { __imag__ _M_value = __val; }
1736 _GLIBCXX20_CONSTEXPR complex&
1737 operator=(long double __r)
1743 _GLIBCXX20_CONSTEXPR complex&
1744 operator+=(long double __r)
1750 _GLIBCXX20_CONSTEXPR complex&
1751 operator-=(long double __r)
1757 _GLIBCXX20_CONSTEXPR complex&
1758 operator*=(long double __r)
1764 _GLIBCXX20_CONSTEXPR complex&
1765 operator/=(long double __r)
1771 // The compiler knows how to do this efficiently
1772#if __cplusplus >= 201103L
1773 _GLIBCXX14_CONSTEXPR complex& operator=(const complex&) = default;
1776 template<typename _Tp>
1777 _GLIBCXX20_CONSTEXPR complex&
1778 operator=(const complex<_Tp>& __z)
1780 _M_value = __z.__rep();
1784 template<typename _Tp>
1785 _GLIBCXX20_CONSTEXPR complex&
1786 operator+=(const complex<_Tp>& __z)
1788 _M_value += __z.__rep();
1792 template<typename _Tp>
1793 _GLIBCXX20_CONSTEXPR complex&
1794 operator-=(const complex<_Tp>& __z)
1796 _M_value -= __z.__rep();
1800 template<typename _Tp>
1801 _GLIBCXX20_CONSTEXPR complex&
1802 operator*=(const complex<_Tp>& __z)
1804 const _ComplexT __t = __z.__rep();
1809 template<typename _Tp>
1810 _GLIBCXX20_CONSTEXPR complex&
1811 operator/=(const complex<_Tp>& __z)
1813 const _ComplexT __t = __z.__rep();
1818 _GLIBCXX_CONSTEXPR _ComplexT __rep() const { return _M_value; }
1824#if __cplusplus > 202002L
1825 template<typename _Tp>
1826 struct __complex_type
1829#ifdef __STDCPP_FLOAT16_T__
1831 struct __complex_type<_Float16>
1832 { typedef __complex__ _Float16 type; };
1835#ifdef __STDCPP_FLOAT32_T__
1837 struct __complex_type<_Float32>
1838 { typedef __complex__ _Float32 type; };
1841#ifdef __STDCPP_FLOAT64_T__
1843 struct __complex_type<_Float64>
1844 { typedef __complex__ _Float64 type; };
1847#ifdef __STDCPP_FLOAT128_T__
1849 struct __complex_type<_Float128>
1850 { typedef __complex__ _Float128 type; };
1853#ifdef __STDCPP_BFLOAT16_T__
1855 struct __complex_type<__gnu_cxx::__bfloat16_t>
1856 { typedef __complex__ decltype(0.0bf16) type; };
1859 template<typename _Tp>
1860 requires requires { typename __complex_type<_Tp>::type; }
1864 typedef _Tp value_type;
1865 typedef typename std::__complex_type<_Tp>::type _ComplexT;
1867 constexpr complex(_ComplexT __z) : _M_value(__z) { }
1869 constexpr complex(_Tp __r = _Tp(), _Tp __i = _Tp())
1870 : _M_value{ __r, __i } { }
1872 template<typename _Up>
1873 explicit(!requires(_Up __u) { value_type{__u}; })
1874 constexpr complex(const complex<_Up>& __z)
1875 : _M_value{ value_type(__z.real()), value_type(__z.imag()) } { }
1878 real() const { return __real__ _M_value; }
1881 imag() const { return __imag__ _M_value; }
1884 real(_Tp __val) { __real__ _M_value = __val; }
1887 imag(_Tp __val) { __imag__ _M_value = __val; }
1924 // Let the compiler synthesize the copy and assignment
1925 // operator. It always does a pretty good job.
1926 constexpr complex(const complex&) = default;
1927 constexpr complex& operator=(const complex&) = default;
1929 template<typename _Up>
1931 operator=(const complex<_Up>& __z)
1933 __real__ _M_value = __z.real();
1934 __imag__ _M_value = __z.imag();
1938 template<typename _Up>
1940 operator+=(const complex<_Up>& __z)
1942 _M_value += __z.__rep();
1948 operator-=(const complex<_Up>& __z)
1950 _M_value -= __z.__rep();
1956 operator*=(const complex<_Up>& __z)
1958 const _ComplexT __t = __z.__rep();
1965 operator/=(const complex<_Up>& __z)
1967 const _ComplexT __t = __z.__rep();
1972 constexpr _ComplexT __rep() const { return _M_value; }
1979#if __cplusplus <= 202002L
1980 // These bits have to be at the end of this file, so that the
1981 // specializations have all been defined.
1982 inline _GLIBCXX_CONSTEXPR
1983 complex<float>::complex(const complex<double>& __z)
1984 : _M_value(__z.__rep()) { }
1986 inline _GLIBCXX_CONSTEXPR
1987 complex<float>::complex(const complex<long double>& __z)
1988 : _M_value(__z.__rep()) { }
1990 inline _GLIBCXX_CONSTEXPR
1991 complex<double>::complex(const complex<long double>& __z)
1992 : _M_value(__z.__rep()) { }
1995 // Inhibit implicit instantiations for required instantiations,
1996 // which are defined via explicit instantiations elsewhere.
1997 // NB: This syntax is a GNU extension.
1998#if _GLIBCXX_EXTERN_TEMPLATE
1999 extern template istream& operator>>(istream&, complex<float>&);
2000 extern template ostream& operator<<(ostream&, const complex<float>&);
2001 extern template istream& operator>>(istream&, complex<double>&);
2002 extern template ostream& operator<<(ostream&, const complex<double>&);
2003 extern template istream& operator>>(istream&, complex<long double>&);
2004 extern template ostream& operator<<(ostream&, const complex<long double>&);
2006#ifdef _GLIBCXX_USE_WCHAR_T
2007 extern template wistream& operator>>(wistream&, complex<float>&);
2008 extern template wostream& operator<<(wostream&, const complex<float>&);
2009 extern template wistream& operator>>(wistream&, complex<double>&);
2010 extern template wostream& operator<<(wostream&, const complex<double>&);
2011 extern template wistream& operator>>(wistream&, complex<long double>&);
2012 extern template wostream& operator<<(wostream&, const complex<long double>&);
2016 /// @} group complex_numbers
2018_GLIBCXX_END_NAMESPACE_VERSION
2021#if __cplusplus >= 201103L
2023namespace std _GLIBCXX_VISIBILITY(default)
2025_GLIBCXX_BEGIN_NAMESPACE_VERSION
2027 // Forward declarations.
2028 template<typename _Tp> std::complex<_Tp> acos(const std::complex<_Tp>&);
2029 template<typename _Tp> std::complex<_Tp> asin(const std::complex<_Tp>&);
2030 template<typename _Tp> std::complex<_Tp> atan(const std::complex<_Tp>&);
2032 template<typename _Tp> std::complex<_Tp> acosh(const std::complex<_Tp>&);
2033 template<typename _Tp> std::complex<_Tp> asinh(const std::complex<_Tp>&);
2034 template<typename _Tp> std::complex<_Tp> atanh(const std::complex<_Tp>&);
2036 template<typename _Tp> _Tp fabs(const std::complex<_Tp>&);
2038 template<typename _Tp>
2039 inline std::complex<_Tp>
2040 __complex_acos(const std::complex<_Tp>& __z)
2042 const std::complex<_Tp> __t = std::asin(__z);
2043 const _Tp __pi_2 = (_Tp) 1.5707963267948966192313216916397514L;
2044 return std::complex<_Tp>(__pi_2 - __t.real(), -__t.imag());
2047#if _GLIBCXX_USE_C99_COMPLEX_ARC
2048#if defined(__STDCPP_FLOAT16_T__) && defined(_GLIBCXX_FLOAT_IS_IEEE_BINARY32)
2049 inline __complex__ _Float16
2050 __complex_acos(__complex__ _Float16 __z)
2051 { return static_cast<__complex__ _Float16>(__builtin_cacosf(__z)); }
2053 inline __complex__ _Float16
2054 __complex_asin(__complex__ _Float16 __z)
2055 { return static_cast<__complex__ _Float16>(__builtin_casinf(__z)); }
2057 inline __complex__ _Float16
2058 __complex_atan(__complex__ _Float16 __z)
2059 { return static_cast<__complex__ _Float16>(__builtin_catanf(__z)); }
2061 inline __complex__ _Float16
2062 __complex_acosh(__complex__ _Float16 __z)
2063 { return static_cast<__complex__ _Float16>(__builtin_cacoshf(__z)); }
2065 inline __complex__ _Float16
2066 __complex_asinh(__complex__ _Float16 __z)
2067 { return static_cast<__complex__ _Float16>(__builtin_casinhf(__z)); }
2069 inline __complex__ _Float16
2070 __complex_atanh(__complex__ _Float16 __z)
2071 { return static_cast<__complex__ _Float16>(__builtin_catanhf(__z)); }
2074#if defined(__STDCPP_FLOAT32_T__) && defined(_GLIBCXX_FLOAT_IS_IEEE_BINARY32)
2075 inline __complex__ _Float32
2076 __complex_acos(__complex__ _Float32 __z)
2077 { return __builtin_cacosf(__z); }
2079 inline __complex__ _Float32
2080 __complex_asin(__complex__ _Float32 __z)
2081 { return __builtin_casinf(__z); }
2083 inline __complex__ _Float32
2084 __complex_atan(__complex__ _Float32 __z)
2085 { return __builtin_catanf(__z); }
2087 inline __complex__ _Float32
2088 __complex_acosh(__complex__ _Float32 __z)
2089 { return __builtin_cacoshf(__z); }
2091 inline __complex__ _Float32
2092 __complex_asinh(__complex__ _Float32 __z)
2093 { return __builtin_casinhf(__z); }
2095 inline __complex__ _Float32
2096 __complex_atanh(__complex__ _Float32 __z)
2097 { return __builtin_catanhf(__z); }
2100#if defined(__STDCPP_FLOAT64_T__) && defined(_GLIBCXX_DOUBLE_IS_IEEE_BINARY64)
2101 inline __complex__ _Float64
2102 __complex_acos(__complex__ _Float64 __z)
2103 { return __builtin_cacos(__z); }
2105 inline __complex__ _Float64
2106 __complex_asin(__complex__ _Float64 __z)
2107 { return __builtin_casin(__z); }
2109 inline __complex__ _Float64
2110 __complex_atan(__complex__ _Float64 __z)
2111 { return __builtin_catan(__z); }
2113 inline __complex__ _Float64
2114 __complex_acosh(__complex__ _Float64 __z)
2115 { return __builtin_cacosh(__z); }
2117 inline __complex__ _Float64
2118 __complex_asinh(__complex__ _Float64 __z)
2119 { return __builtin_casinh(__z); }
2121 inline __complex__ _Float64
2122 __complex_atanh(__complex__ _Float64 __z)
2123 { return __builtin_catanh(__z); }
2126#if defined(__STDCPP_FLOAT128_T__) && defined(_GLIBCXX_LDOUBLE_IS_IEEE_BINARY128)
2127 inline __complex__ _Float128
2128 __complex_acos(__complex__ _Float128 __z)
2129 { return __builtin_cacosl(__z); }
2131 inline __complex__ _Float128
2132 __complex_asin(__complex__ _Float128 __z)
2133 { return __builtin_casinl(__z); }
2135 inline __complex__ _Float128
2136 __complex_atan(__complex__ _Float128 __z)
2137 { return __builtin_catanl(__z); }
2139 inline __complex__ _Float128
2140 __complex_acosh(__complex__ _Float128 __z)
2141 { return __builtin_cacoshl(__z); }
2143 inline __complex__ _Float128
2144 __complex_asinh(__complex__ _Float128 __z)
2145 { return __builtin_casinhl(__z); }
2147 inline __complex__ _Float128
2148 __complex_atanh(__complex__ _Float128 __z)
2149 { return __builtin_catanhl(__z); }
2150#elif defined(__STDCPP_FLOAT128_T__) && defined(_GLIBCXX_HAVE_FLOAT128_MATH)
2151 inline __complex__ _Float128
2152 __complex_acos(__complex__ _Float128 __z)
2153 { return __builtin_cacosf128(__z); }
2155 inline __complex__ _Float128
2156 __complex_asin(__complex__ _Float128 __z)
2157 { return __builtin_casinf128(__z); }
2159 inline __complex__ _Float128
2160 __complex_atan(__complex__ _Float128 __z)
2161 { return __builtin_catanf128(__z); }
2163 inline __complex__ _Float128
2164 __complex_acosh(__complex__ _Float128 __z)
2165 { return __builtin_cacoshf128(__z); }
2167 inline __complex__ _Float128
2168 __complex_asinh(__complex__ _Float128 __z)
2169 { return __builtin_casinhf128(__z); }
2171 inline __complex__ _Float128
2172 __complex_atanh(__complex__ _Float128 __z)
2173 { return __builtin_catanhf128(__z); }
2176#if defined(__STDCPP_BFLOAT16_T__) && defined(_GLIBCXX_FLOAT_IS_IEEE_BINARY32)
2177 inline __complex__ decltype(0.0bf16)
2178 __complex_acos(__complex__ decltype(0.0bf16) __z)
2179 { return static_cast<__complex__ decltype(0.0bf16)>(__builtin_cacosf(__z)); }
2181 inline __complex__ decltype(0.0bf16)
2182 __complex_asin(__complex__ decltype(0.0bf16) __z)
2183 { return static_cast<__complex__ decltype(0.0bf16)>(__builtin_casinf(__z)); }
2185 inline __complex__ decltype(0.0bf16)
2186 __complex_atan(__complex__ decltype(0.0bf16) __z)
2187 { return static_cast<__complex__ decltype(0.0bf16)>(__builtin_catanf(__z)); }
2189 inline __complex__ decltype(0.0bf16)
2190 __complex_acosh(__complex__ decltype(0.0bf16) __z)
2191 { return static_cast<__complex__ decltype(0.0bf16)>(__builtin_cacoshf(__z)); }
2193 inline __complex__ decltype(0.0bf16)
2194 __complex_asinh(__complex__ decltype(0.0bf16) __z)
2195 { return static_cast<__complex__ decltype(0.0bf16)>(__builtin_casinhf(__z)); }
2197 inline __complex__ decltype(0.0bf16)
2198 __complex_atanh(__complex__ decltype(0.0bf16) __z)
2199 { return static_cast<__complex__ decltype(0.0bf16)>(__builtin_catanhf(__z)); }
2203#if _GLIBCXX_USE_C99_COMPLEX_ARC
2204 inline __complex__ float
2205 __complex_acos(__complex__ float __z)
2206 { return __builtin_cacosf(__z); }
2208 inline __complex__ double
2209 __complex_acos(__complex__ double __z)
2210 { return __builtin_cacos(__z); }
2212 inline __complex__ long double
2213 __complex_acos(const __complex__ long double& __z)
2214 { return __builtin_cacosl(__z); }
2216 template<typename _Tp>
2217 inline std::complex<_Tp>
2218 acos(const std::complex<_Tp>& __z)
2219 { return __complex_acos(__z.__rep()); }
2221 /// acos(__z) [8.1.2].
2222 // Effects: Behaves the same as C99 function cacos, defined
2223 // in subclause 7.3.5.1.
2224 template<typename _Tp>
2225 inline std::complex<_Tp>
2226 acos(const std::complex<_Tp>& __z)
2227 { return __complex_acos(__z); }
2230 template<typename _Tp>
2231 inline std::complex<_Tp>
2232 __complex_asin(const std::complex<_Tp>& __z)
2234 std::complex<_Tp> __t(-__z.imag(), __z.real());
2235 __t = std::asinh(__t);
2236 return std::complex<_Tp>(__t.imag(), -__t.real());
2239#if _GLIBCXX_USE_C99_COMPLEX_ARC
2240 inline __complex__ float
2241 __complex_asin(__complex__ float __z)
2242 { return __builtin_casinf(__z); }
2244 inline __complex__ double
2245 __complex_asin(__complex__ double __z)
2246 { return __builtin_casin(__z); }
2248 inline __complex__ long double
2249 __complex_asin(const __complex__ long double& __z)
2250 { return __builtin_casinl(__z); }
2252 template<typename _Tp>
2253 inline std::complex<_Tp>
2254 asin(const std::complex<_Tp>& __z)
2255 { return __complex_asin(__z.__rep()); }
2257 /// asin(__z) [8.1.3].
2258 // Effects: Behaves the same as C99 function casin, defined
2259 // in subclause 7.3.5.2.
2260 template<typename _Tp>
2261 inline std::complex<_Tp>
2262 asin(const std::complex<_Tp>& __z)
2263 { return __complex_asin(__z); }
2266 template<typename _Tp>
2268 __complex_atan(const std::complex<_Tp>& __z)
2270 const _Tp __r2 = __z.real() * __z.real();
2271 const _Tp __x = _Tp(1.0) - __r2 - __z.imag() * __z.imag();
2273 _Tp __num = __z.imag() + _Tp(1.0);
2274 _Tp __den = __z.imag() - _Tp(1.0);
2276 __num = __r2 + __num * __num;
2277 __den = __r2 + __den * __den;
2279 return std::complex<_Tp>(_Tp(0.5) * atan2(_Tp(2.0) * __z.real(), __x),
2280 _Tp(0.25) * log(__num / __den));
2283#if _GLIBCXX_USE_C99_COMPLEX_ARC
2284 inline __complex__ float
2285 __complex_atan(__complex__ float __z)
2286 { return __builtin_catanf(__z); }
2288 inline __complex__ double
2289 __complex_atan(__complex__ double __z)
2290 { return __builtin_catan(__z); }
2292 inline __complex__ long double
2293 __complex_atan(const __complex__ long double& __z)
2294 { return __builtin_catanl(__z); }
2296 template<typename _Tp>
2297 inline std::complex<_Tp>
2298 atan(const std::complex<_Tp>& __z)
2299 { return __complex_atan(__z.__rep()); }
2301 /// atan(__z) [8.1.4].
2302 // Effects: Behaves the same as C99 function catan, defined
2303 // in subclause 7.3.5.3.
2304 template<typename _Tp>
2305 inline std::complex<_Tp>
2306 atan(const std::complex<_Tp>& __z)
2307 { return __complex_atan(__z); }
2310 template<typename _Tp>
2312 __complex_acosh(const std::complex<_Tp>& __z)
2315 return _Tp(2.0) * std::log(std::sqrt(_Tp(0.5) * (__z + _Tp(1.0)))
2316 + std::sqrt(_Tp(0.5) * (__z - _Tp(1.0))));
2319#if _GLIBCXX_USE_C99_COMPLEX_ARC
2320 inline __complex__ float
2321 __complex_acosh(__complex__ float __z)
2322 { return __builtin_cacoshf(__z); }
2324 inline __complex__ double
2325 __complex_acosh(__complex__ double __z)
2326 { return __builtin_cacosh(__z); }
2328 inline __complex__ long double
2329 __complex_acosh(const __complex__ long double& __z)
2330 { return __builtin_cacoshl(__z); }
2332 template<typename _Tp>
2333 inline std::complex<_Tp>
2334 acosh(const std::complex<_Tp>& __z)
2335 { return __complex_acosh(__z.__rep()); }
2337 /// acosh(__z) [8.1.5].
2338 // Effects: Behaves the same as C99 function cacosh, defined
2339 // in subclause 7.3.6.1.
2340 template<typename _Tp>
2341 inline std::complex<_Tp>
2342 acosh(const std::complex<_Tp>& __z)
2343 { return __complex_acosh(__z); }
2346 template<typename _Tp>
2348 __complex_asinh(const std::complex<_Tp>& __z)
2350 std::complex<_Tp> __t((__z.real() - __z.imag())
2351 * (__z.real() + __z.imag()) + _Tp(1.0),
2352 _Tp(2.0) * __z.real() * __z.imag());
2353 __t = std::sqrt(__t);
2355 return std::log(__t + __z);
2358#if _GLIBCXX_USE_C99_COMPLEX_ARC
2359 inline __complex__ float
2360 __complex_asinh(__complex__ float __z)
2361 { return __builtin_casinhf(__z); }
2363 inline __complex__ double
2364 __complex_asinh(__complex__ double __z)
2365 { return __builtin_casinh(__z); }
2367 inline __complex__ long double
2368 __complex_asinh(const __complex__ long double& __z)
2369 { return __builtin_casinhl(__z); }
2371 template<typename _Tp>
2372 inline std::complex<_Tp>
2373 asinh(const std::complex<_Tp>& __z)
2374 { return __complex_asinh(__z.__rep()); }
2376 /// asinh(__z) [8.1.6].
2377 // Effects: Behaves the same as C99 function casin, defined
2378 // in subclause 7.3.6.2.
2379 template<typename _Tp>
2380 inline std::complex<_Tp>
2381 asinh(const std::complex<_Tp>& __z)
2382 { return __complex_asinh(__z); }
2385 template<typename _Tp>
2387 __complex_atanh(const std::complex<_Tp>& __z)
2389 const _Tp __i2 = __z.imag() * __z.imag();
2390 const _Tp __x = _Tp(1.0) - __i2 - __z.real() * __z.real();
2392 _Tp __num = _Tp(1.0) + __z.real();
2393 _Tp __den = _Tp(1.0) - __z.real();
2395 __num = __i2 + __num * __num;
2396 __den = __i2 + __den * __den;
2398 return std::complex<_Tp>(_Tp(0.25) * (log(__num) - log(__den)),
2399 _Tp(0.5) * atan2(_Tp(2.0) * __z.imag(), __x));
2402#if _GLIBCXX_USE_C99_COMPLEX_ARC
2403 inline __complex__ float
2404 __complex_atanh(__complex__ float __z)
2405 { return __builtin_catanhf(__z); }
2407 inline __complex__ double
2408 __complex_atanh(__complex__ double __z)
2409 { return __builtin_catanh(__z); }
2411 inline __complex__ long double
2412 __complex_atanh(const __complex__ long double& __z)
2413 { return __builtin_catanhl(__z); }
2415 template<typename _Tp>
2416 inline std::complex<_Tp>
2417 atanh(const std::complex<_Tp>& __z)
2418 { return __complex_atanh(__z.__rep()); }
2420 /// atanh(__z) [8.1.7].
2421 // Effects: Behaves the same as C99 function catanh, defined
2422 // in subclause 7.3.6.3.
2423 template<typename _Tp>
2424 inline std::complex<_Tp>
2425 atanh(const std::complex<_Tp>& __z)
2426 { return __complex_atanh(__z); }
2429 template<typename _Tp>
2431 /// fabs(__z) [8.1.8].
2432 // Effects: Behaves the same as C99 function cabs, defined
2433 // in subclause 7.3.8.1.
2434 fabs(const std::complex<_Tp>& __z)
2435 { return std::abs(__z); }
2437 /// Additional overloads [8.1.9].
2438 template<typename _Tp>
2439 inline typename __gnu_cxx::__promote<_Tp>::__type
2442 typedef typename __gnu_cxx::__promote<_Tp>::__type __type;
2443#if (_GLIBCXX11_USE_C99_MATH && !_GLIBCXX_USE_C99_FP_MACROS_DYNAMIC)
2444 return std::signbit(__x) ? __type(3.1415926535897932384626433832795029L)
2447 return std::arg(std::complex<__type>(__x));
2451 template<typename _Tp>
2452 _GLIBCXX_CONSTEXPR inline typename __gnu_cxx::__promote<_Tp>::__type
2456 template<typename _Tp>
2457 _GLIBCXX20_CONSTEXPR inline typename __gnu_cxx::__promote<_Tp>::__type
2460 typedef typename __gnu_cxx::__promote<_Tp>::__type __type;
2461 return __type(__x) * __type(__x);
2464 template<typename _Tp>
2465 _GLIBCXX_CONSTEXPR inline typename __gnu_cxx::__promote<_Tp>::__type
2469 template<typename _Tp, typename _Up>
2470 inline std::complex<typename __gnu_cxx::__promote_2<_Tp, _Up>::__type>
2471 pow(const std::complex<_Tp>& __x, const _Up& __y)
2473 typedef typename __gnu_cxx::__promote_2<_Tp, _Up>::__type __type;
2474 return std::pow(std::complex<__type>(__x), __type(__y));
2477 template<typename _Tp, typename _Up>
2478 inline std::complex<typename __gnu_cxx::__promote_2<_Tp, _Up>::__type>
2479 pow(const _Tp& __x, const std::complex<_Up>& __y)
2481 typedef typename __gnu_cxx::__promote_2<_Tp, _Up>::__type __type;
2482 return std::pow(__type(__x), std::complex<__type>(__y));
2485 template<typename _Tp, typename _Up>
2486 inline std::complex<typename __gnu_cxx::__promote_2<_Tp, _Up>::__type>
2487 pow(const std::complex<_Tp>& __x, const std::complex<_Up>& __y)
2489 typedef typename __gnu_cxx::__promote_2<_Tp, _Up>::__type __type;
2490 return std::pow(std::complex<__type>(__x),
2491 std::complex<__type>(__y));
2494 // Forward declarations.
2496 template<typename _Tp>
2497 std::complex<_Tp> proj(const std::complex<_Tp>&);
2499 // Generic implementation of std::proj, does not work for infinities.
2500 template<typename _Tp>
2501 inline std::complex<_Tp>
2502 __complex_proj(const std::complex<_Tp>& __z)
2505#if _GLIBCXX_USE_C99_COMPLEX
2506 inline complex<float>
2507 __complex_proj(const complex<float>& __z)
2508 { return __builtin_cprojf(__z.__rep()); }
2510 inline complex<double>
2511 __complex_proj(const complex<double>& __z)
2512 { return __builtin_cproj(__z.__rep()); }
2514 inline complex<long double>
2515 __complex_proj(const complex<long double>& __z)
2516 { return __builtin_cprojl(__z.__rep()); }
2518#if __cplusplus > 202002L
2519#if defined(__STDCPP_FLOAT16_T__) && defined(_GLIBCXX_FLOAT_IS_IEEE_BINARY32)
2520 inline __complex__ _Float16
2521 __complex_proj(__complex__ _Float16 __z)
2522 { return static_cast<__complex__ _Float16>(__builtin_cprojf(__z)); }
2525#if defined(__STDCPP_FLOAT32_T__) && defined(_GLIBCXX_FLOAT_IS_IEEE_BINARY32)
2526 inline __complex__ _Float32
2527 __complex_proj(__complex__ _Float32 __z)
2528 { return __builtin_cprojf(__z); }
2531#if defined(__STDCPP_FLOAT64_T__) && defined(_GLIBCXX_DOUBLE_IS_IEEE_BINARY64)
2532 inline __complex__ _Float64
2533 __complex_proj(__complex__ _Float64 __z)
2534 { return __builtin_cproj(__z); }
2537#if defined(__STDCPP_FLOAT128_T__) && defined(_GLIBCXX_LDOUBLE_IS_IEEE_BINARY128)
2538 inline __complex__ _Float128
2539 __complex_proj(__complex__ _Float128 __z)
2540 { return __builtin_cprojl(__z); }
2541#elif defined(__STDCPP_FLOAT128_T__) && defined(_GLIBCXX_HAVE_FLOAT128_MATH)
2542 inline __complex__ _Float128
2543 __complex_proj(__complex__ _Float128 __z)
2544 { return __builtin_cprojf128(__z); }
2547#if defined(__STDCPP_BFLOAT16_T__) && defined(_GLIBCXX_FLOAT_IS_IEEE_BINARY32)
2548 inline __complex__ decltype(0.0bf16)
2549 __complex_proj(__complex__ decltype(0.0bf16) __z)
2550 { return static_cast<__complex__ decltype(0.0bf16)>(__builtin_cprojf(__z)); }
2553 template<typename _Tp>
2554 requires requires { typename __complex_type<_Tp>::type; }
2556 __complex_proj(const complex<_Tp>& __z)
2557 { return __complex_proj(__z.__rep()); }
2560#elif defined _GLIBCXX_USE_C99_MATH_FUNCS
2561 inline complex<float>
2562 __complex_proj(const complex<float>& __z)
2564 if (__builtin_isinf(__z.real()) || __builtin_isinf(__z.imag()))
2565 return complex<float>(__builtin_inff(),
2566 __builtin_copysignf(0.0f, __z.imag()));
2570 inline complex<double>
2571 __complex_proj(const complex<double>& __z)
2573 if (__builtin_isinf(__z.real()) || __builtin_isinf(__z.imag()))
2574 return complex<double>(__builtin_inf(),
2575 __builtin_copysign(0.0, __z.imag()));
2579 inline complex<long double>
2580 __complex_proj(const complex<long double>& __z)
2582 if (__builtin_isinf(__z.real()) || __builtin_isinf(__z.imag()))
2583 return complex<long double>(__builtin_infl(),
2584 __builtin_copysignl(0.0l, __z.imag()));
2589 template<typename _Tp>
2590 inline std::complex<_Tp>
2591 proj(const std::complex<_Tp>& __z)
2592 { return __complex_proj(__z); }
2594 // Overload for scalars
2595 template<typename _Tp>
2596 inline std::complex<typename __gnu_cxx::__promote<_Tp>::__type>
2599 typedef typename __gnu_cxx::__promote<_Tp>::__type __type;
2600 return std::proj(std::complex<__type>(__x));
2603 template<typename _Tp>
2604 inline _GLIBCXX20_CONSTEXPR
2605 std::complex<typename __gnu_cxx::__promote<_Tp>::__type>
2608 typedef typename __gnu_cxx::__promote<_Tp>::__type __type;
2609 return std::complex<__type>(__x, -__type());
2612#ifdef __cpp_lib_complex_udls // C++ >= 14
2614inline namespace literals {
2615inline namespace complex_literals {
2616#pragma GCC diagnostic push
2617#pragma GCC diagnostic ignored "-Wliteral-suffix"
2619 constexpr std::complex<float>
2620 operator""if(long double __num)
2621 { return std::complex<float>{0.0F, static_cast<float>(__num)}; }
2623 constexpr std::complex<float>
2624 operator""if(unsigned long long __num)
2625 { return std::complex<float>{0.0F, static_cast<float>(__num)}; }
2627 constexpr std::complex<double>
2628 operator""i(long double __num)
2629 { return std::complex<double>{0.0, static_cast<double>(__num)}; }
2631 constexpr std::complex<double>
2632 operator""i(unsigned long long __num)
2633 { return std::complex<double>{0.0, static_cast<double>(__num)}; }
2635 constexpr std::complex<long double>
2636 operator""il(long double __num)
2637 { return std::complex<long double>{0.0L, __num}; }
2639 constexpr std::complex<long double>
2640 operator""il(unsigned long long __num)
2641 { return std::complex<long double>{0.0L, static_cast<long double>(__num)}; }
2643#pragma GCC diagnostic pop
2644} // inline namespace complex_literals
2645} // inline namespace literals
2647#endif // __cpp_lib_complex_udls
2649_GLIBCXX_END_NAMESPACE_VERSION
2654#ifdef _GLIBCXX_CLANG
2655#pragma clang diagnostic pop
2658#pragma GCC diagnostic pop
2659#endif /* _GLIBCXX_COMPLEX */