1// The template and inlines for the -*- C++ -*- complex number classes.
3// Copyright (C) 1997-2023 Free Software Foundation, Inc.
5// This file is part of the GNU ISO C++ Library. This library is free
6// software; you can redistribute it and/or modify it under the
7// terms of the GNU General Public License as published by the
8// Free Software Foundation; either version 3, or (at your option)
11// This library is distributed in the hope that it will be useful,
12// but WITHOUT ANY WARRANTY; without even the implied warranty of
13// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14// GNU General Public License for more details.
16// Under Section 7 of GPL version 3, you are granted additional
17// permissions described in the GCC Runtime Library Exception, version
18// 3.1, as published by the Free Software Foundation.
20// You should have received a copy of the GNU General Public License and
21// a copy of the GCC Runtime Library Exception along with this program;
22// see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
23// <http://www.gnu.org/licenses/>.
25/** @file include/complex
26 * This is a Standard C++ Library header.
30// ISO C++ 14882: 26.2 Complex Numbers
31// Note: this is not a conforming implementation.
36#ifndef _GLIBCXX_COMPLEX
37#define _GLIBCXX_COMPLEX 1
39#pragma GCC system_header
41#include <bits/c++config.h>
42#include <bits/cpp_type_traits.h>
43#include <ext/type_traits.h>
47// Get rid of a macro possibly defined in <complex.h>
50#if __cplusplus > 201703L
51# define __cpp_lib_constexpr_complex 201711L
54namespace std _GLIBCXX_VISIBILITY(default)
56_GLIBCXX_BEGIN_NAMESPACE_VERSION
59 * @defgroup complex_numbers Complex Numbers
62 * Classes and functions for complex numbers.
66 // Forward declarations.
67 template<typename _Tp> class complex;
68 template<> class complex<float>;
69 template<> class complex<double>;
70 template<> class complex<long double>;
72 /// Return magnitude of @a z.
73 template<typename _Tp> _Tp abs(const complex<_Tp>&);
74 /// Return phase angle of @a z.
75 template<typename _Tp> _Tp arg(const complex<_Tp>&);
76 /// Return @a z magnitude squared.
77 template<typename _Tp> _Tp _GLIBCXX20_CONSTEXPR norm(const complex<_Tp>&);
79 /// Return complex conjugate of @a z.
80 template<typename _Tp>
81 _GLIBCXX20_CONSTEXPR complex<_Tp> conj(const complex<_Tp>&);
82 /// Return complex with magnitude @a rho and angle @a theta.
83 template<typename _Tp> complex<_Tp> polar(const _Tp&, const _Tp& = 0);
86 /// Return complex cosine of @a z.
87 template<typename _Tp> complex<_Tp> cos(const complex<_Tp>&);
88 /// Return complex hyperbolic cosine of @a z.
89 template<typename _Tp> complex<_Tp> cosh(const complex<_Tp>&);
90 /// Return complex base e exponential of @a z.
91 template<typename _Tp> complex<_Tp> exp(const complex<_Tp>&);
92 /// Return complex natural logarithm of @a z.
93 template<typename _Tp> complex<_Tp> log(const complex<_Tp>&);
94 /// Return complex base 10 logarithm of @a z.
95 template<typename _Tp> complex<_Tp> log10(const complex<_Tp>&);
96 /// Return @a x to the @a y'th power.
97 template<typename _Tp> complex<_Tp> pow(const complex<_Tp>&, int);
98 /// Return @a x to the @a y'th power.
99 template<typename _Tp> complex<_Tp> pow(const complex<_Tp>&, const _Tp&);
100 /// Return @a x to the @a y'th power.
101 template<typename _Tp> complex<_Tp> pow(const complex<_Tp>&,
102 const complex<_Tp>&);
103 /// Return @a x to the @a y'th power.
104 template<typename _Tp> complex<_Tp> pow(const _Tp&, const complex<_Tp>&);
105 /// Return complex sine of @a z.
106 template<typename _Tp> complex<_Tp> sin(const complex<_Tp>&);
107 /// Return complex hyperbolic sine of @a z.
108 template<typename _Tp> complex<_Tp> sinh(const complex<_Tp>&);
109 /// Return complex square root of @a z.
110 template<typename _Tp> complex<_Tp> sqrt(const complex<_Tp>&);
111 /// Return complex tangent of @a z.
112 template<typename _Tp> complex<_Tp> tan(const complex<_Tp>&);
113 /// Return complex hyperbolic tangent of @a z.
114 template<typename _Tp> complex<_Tp> tanh(const complex<_Tp>&);
117 // 26.2.2 Primary template class complex
119 * Template to represent complex numbers.
121 * Specializations for float, double, and long double are part of the
122 * library. Results with any other type are not guaranteed.
124 * @param Tp Type of real and imaginary values.
126 template<typename _Tp>
131 typedef _Tp value_type;
133 /// Default constructor. First parameter is x, second parameter is y.
134 /// Unspecified parameters default to 0.
135 _GLIBCXX_CONSTEXPR complex(const _Tp& __r = _Tp(), const _Tp& __i = _Tp())
136 : _M_real(__r), _M_imag(__i) { }
138 // Let the compiler synthesize the copy constructor
139#if __cplusplus >= 201103L
140 constexpr complex(const complex&) = default;
143 /// Converting constructor.
144 template<typename _Up>
145#if __cplusplus > 202002L
146 explicit(!requires(_Up __u) { _Tp{__u}; })
148 _GLIBCXX_CONSTEXPR complex(const complex<_Up>& __z)
149 : _M_real(_Tp(__z.real())), _M_imag(_Tp(__z.imag())) { }
151#if __cplusplus >= 201103L
152 // _GLIBCXX_RESOLVE_LIB_DEFECTS
153 // DR 387. std::complex over-encapsulated.
154 _GLIBCXX_ABI_TAG_CXX11
156 real() const { return _M_real; }
158 _GLIBCXX_ABI_TAG_CXX11
160 imag() const { return _M_imag; }
162 /// Return real part of complex number.
164 real() { return _M_real; }
166 /// Return real part of complex number.
168 real() const { return _M_real; }
170 /// Return imaginary part of complex number.
172 imag() { return _M_imag; }
174 /// Return imaginary part of complex number.
176 imag() const { return _M_imag; }
179 // _GLIBCXX_RESOLVE_LIB_DEFECTS
180 // DR 387. std::complex over-encapsulated.
181 _GLIBCXX20_CONSTEXPR void
182 real(_Tp __val) { _M_real = __val; }
184 _GLIBCXX20_CONSTEXPR void
185 imag(_Tp __val) { _M_imag = __val; }
187 /// Assign a scalar to this complex number.
188 _GLIBCXX20_CONSTEXPR complex<_Tp>& operator=(const _Tp&);
190 /// Add a scalar to this complex number.
192 _GLIBCXX20_CONSTEXPR complex<_Tp>&
193 operator+=(const _Tp& __t)
199 /// Subtract a scalar from this complex number.
201 _GLIBCXX20_CONSTEXPR complex<_Tp>&
202 operator-=(const _Tp& __t)
208 /// Multiply this complex number by a scalar.
209 _GLIBCXX20_CONSTEXPR complex<_Tp>& operator*=(const _Tp&);
210 /// Divide this complex number by a scalar.
211 _GLIBCXX20_CONSTEXPR complex<_Tp>& operator/=(const _Tp&);
213 // Let the compiler synthesize the copy assignment operator
214#if __cplusplus >= 201103L
215 _GLIBCXX20_CONSTEXPR complex& operator=(const complex&) = default;
218 /// Assign another complex number to this one.
219 template<typename _Up>
220 _GLIBCXX20_CONSTEXPR complex<_Tp>& operator=(const complex<_Up>&);
221 /// Add another complex number to this one.
222 template<typename _Up>
223 _GLIBCXX20_CONSTEXPR complex<_Tp>& operator+=(const complex<_Up>&);
224 /// Subtract another complex number from this one.
225 template<typename _Up>
226 _GLIBCXX20_CONSTEXPR complex<_Tp>& operator-=(const complex<_Up>&);
227 /// Multiply this complex number by another.
228 template<typename _Up>
229 _GLIBCXX20_CONSTEXPR complex<_Tp>& operator*=(const complex<_Up>&);
230 /// Divide this complex number by another.
231 template<typename _Up>
232 _GLIBCXX20_CONSTEXPR complex<_Tp>& operator/=(const complex<_Up>&);
234 _GLIBCXX_CONSTEXPR complex __rep() const
242 template<typename _Tp>
243 _GLIBCXX20_CONSTEXPR complex<_Tp>&
244 complex<_Tp>::operator=(const _Tp& __t)
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)
271 template<typename _Tp>
272 template<typename _Up>
273 _GLIBCXX20_CONSTEXPR complex<_Tp>&
274 complex<_Tp>::operator=(const complex<_Up>& __z)
276 _M_real = __z.real();
277 _M_imag = __z.imag();
282 template<typename _Tp>
283 template<typename _Up>
284 _GLIBCXX20_CONSTEXPR complex<_Tp>&
285 complex<_Tp>::operator+=(const complex<_Up>& __z)
287 _M_real += __z.real();
288 _M_imag += __z.imag();
293 template<typename _Tp>
294 template<typename _Up>
295 _GLIBCXX20_CONSTEXPR complex<_Tp>&
296 complex<_Tp>::operator-=(const complex<_Up>& __z)
298 _M_real -= __z.real();
299 _M_imag -= __z.imag();
304 // XXX: This is a grammar school implementation.
305 template<typename _Tp>
306 template<typename _Up>
307 _GLIBCXX20_CONSTEXPR complex<_Tp>&
308 complex<_Tp>::operator*=(const complex<_Up>& __z)
310 const _Tp __r = _M_real * __z.real() - _M_imag * __z.imag();
311 _M_imag = _M_real * __z.imag() + _M_imag * __z.real();
317 // XXX: This is a grammar school implementation.
318 template<typename _Tp>
319 template<typename _Up>
320 _GLIBCXX20_CONSTEXPR complex<_Tp>&
321 complex<_Tp>::operator/=(const complex<_Up>& __z)
323 const _Tp __r = _M_real * __z.real() + _M_imag * __z.imag();
324 const _Tp __n = std::norm(__z);
325 _M_imag = (_M_imag * __z.real() - _M_real * __z.imag()) / __n;
332 /// Return new complex value @a x plus @a y.
333 template<typename _Tp>
334 inline _GLIBCXX20_CONSTEXPR complex<_Tp>
335 operator+(const complex<_Tp>& __x, const complex<_Tp>& __y)
337 complex<_Tp> __r = __x;
342 template<typename _Tp>
343 inline _GLIBCXX20_CONSTEXPR complex<_Tp>
344 operator+(const complex<_Tp>& __x, const _Tp& __y)
346 complex<_Tp> __r = __x;
351 template<typename _Tp>
352 inline _GLIBCXX20_CONSTEXPR complex<_Tp>
353 operator+(const _Tp& __x, const complex<_Tp>& __y)
355 complex<_Tp> __r = __y;
362 /// Return new complex value @a x minus @a y.
363 template<typename _Tp>
364 inline _GLIBCXX20_CONSTEXPR complex<_Tp>
365 operator-(const complex<_Tp>& __x, const complex<_Tp>& __y)
367 complex<_Tp> __r = __x;
372 template<typename _Tp>
373 inline _GLIBCXX20_CONSTEXPR complex<_Tp>
374 operator-(const complex<_Tp>& __x, const _Tp& __y)
376 complex<_Tp> __r = __x;
381 template<typename _Tp>
382 inline _GLIBCXX20_CONSTEXPR complex<_Tp>
383 operator-(const _Tp& __x, const complex<_Tp>& __y)
385 complex<_Tp> __r = -__y;
392 /// Return new complex value @a x times @a y.
393 template<typename _Tp>
394 inline _GLIBCXX20_CONSTEXPR complex<_Tp>
395 operator*(const complex<_Tp>& __x, const complex<_Tp>& __y)
397 complex<_Tp> __r = __x;
402 template<typename _Tp>
403 inline _GLIBCXX20_CONSTEXPR complex<_Tp>
404 operator*(const complex<_Tp>& __x, const _Tp& __y)
406 complex<_Tp> __r = __x;
411 template<typename _Tp>
412 inline _GLIBCXX20_CONSTEXPR complex<_Tp>
413 operator*(const _Tp& __x, const complex<_Tp>& __y)
415 complex<_Tp> __r = __y;
422 /// Return new complex value @a x divided by @a y.
423 template<typename _Tp>
424 inline _GLIBCXX20_CONSTEXPR complex<_Tp>
425 operator/(const complex<_Tp>& __x, const complex<_Tp>& __y)
427 complex<_Tp> __r = __x;
432 template<typename _Tp>
433 inline _GLIBCXX20_CONSTEXPR complex<_Tp>
434 operator/(const complex<_Tp>& __x, const _Tp& __y)
436 complex<_Tp> __r = __x;
441 template<typename _Tp>
442 inline _GLIBCXX20_CONSTEXPR complex<_Tp>
443 operator/(const _Tp& __x, const complex<_Tp>& __y)
445 complex<_Tp> __r = __x;
452 template<typename _Tp>
453 inline _GLIBCXX20_CONSTEXPR complex<_Tp>
454 operator+(const complex<_Tp>& __x)
457 /// Return complex negation of @a x.
458 template<typename _Tp>
459 inline _GLIBCXX20_CONSTEXPR complex<_Tp>
460 operator-(const complex<_Tp>& __x)
461 { return complex<_Tp>(-__x.real(), -__x.imag()); }
464 /// Return true if @a x is equal to @a y.
465 template<typename _Tp>
466 inline _GLIBCXX_CONSTEXPR bool
467 operator==(const complex<_Tp>& __x, const complex<_Tp>& __y)
468 { return __x.real() == __y.real() && __x.imag() == __y.imag(); }
470 template<typename _Tp>
471 inline _GLIBCXX_CONSTEXPR bool
472 operator==(const complex<_Tp>& __x, const _Tp& __y)
473 { return __x.real() == __y && __x.imag() == _Tp(); }
475#if !(__cpp_impl_three_way_comparison >= 201907L)
476 template<typename _Tp>
477 inline _GLIBCXX_CONSTEXPR bool
478 operator==(const _Tp& __x, const complex<_Tp>& __y)
479 { return __x == __y.real() && _Tp() == __y.imag(); }
483 /// Return false if @a x is equal to @a y.
484 template<typename _Tp>
485 inline _GLIBCXX_CONSTEXPR bool
486 operator!=(const complex<_Tp>& __x, const complex<_Tp>& __y)
487 { return __x.real() != __y.real() || __x.imag() != __y.imag(); }
489 template<typename _Tp>
490 inline _GLIBCXX_CONSTEXPR bool
491 operator!=(const complex<_Tp>& __x, const _Tp& __y)
492 { return __x.real() != __y || __x.imag() != _Tp(); }
494 template<typename _Tp>
495 inline _GLIBCXX_CONSTEXPR bool
496 operator!=(const _Tp& __x, const complex<_Tp>& __y)
497 { return __x != __y.real() || _Tp() != __y.imag(); }
501 /// Extraction operator for complex values.
502 template<typename _Tp, typename _CharT, class _Traits>
503 basic_istream<_CharT, _Traits>&
504 operator>>(basic_istream<_CharT, _Traits>& __is, complex<_Tp>& __x)
510 if (_Traits::eq(__ch, __is.widen('(')))
513 if (__is >> __u >> __ch)
515 const _CharT __rparen = __is.widen(')');
516 if (_Traits::eq(__ch, __rparen))
521 else if (_Traits::eq(__ch, __is.widen(',')))
524 if (__is >> __v >> __ch)
526 if (_Traits::eq(__ch, __rparen))
528 __x = complex<_Tp>(__u, __v);
551 __is.setstate(ios_base::failbit);
555 /// Insertion operator for complex values.
556 template<typename _Tp, typename _CharT, class _Traits>
557 basic_ostream<_CharT, _Traits>&
558 operator<<(basic_ostream<_CharT, _Traits>& __os, const complex<_Tp>& __x)
560 basic_ostringstream<_CharT, _Traits> __s;
561 __s.flags(__os.flags());
562 __s.imbue(__os.getloc());
563 __s.precision(__os.precision());
564 __s << '(' << __x.real() << ',' << __x.imag() << ')';
565 return __os << __s.str();
569#if __cplusplus >= 201103L
570 template<typename _Tp>
572 real(const complex<_Tp>& __z)
573 { return __z.real(); }
575 template<typename _Tp>
577 imag(const complex<_Tp>& __z)
578 { return __z.imag(); }
580 template<typename _Tp>
582 real(complex<_Tp>& __z)
583 { return __z.real(); }
585 template<typename _Tp>
587 real(const complex<_Tp>& __z)
588 { return __z.real(); }
590 template<typename _Tp>
592 imag(complex<_Tp>& __z)
593 { return __z.imag(); }
595 template<typename _Tp>
597 imag(const complex<_Tp>& __z)
598 { return __z.imag(); }
601#if _GLIBCXX_USE_C99_COMPLEX
602#if defined(__STDCPP_FLOAT16_T__) && defined(_GLIBCXX_FLOAT_IS_IEEE_BINARY32)
604 __complex_abs(__complex__ _Float16 __z)
605 { return _Float16(__builtin_cabsf(__z)); }
608 __complex_arg(__complex__ _Float16 __z)
609 { return _Float16(__builtin_cargf(__z)); }
611 inline __complex__ _Float16
612 __complex_cos(__complex__ _Float16 __z)
613 { return static_cast<__complex__ _Float16>(__builtin_ccosf(__z)); }
615 inline __complex__ _Float16
616 __complex_cosh(__complex__ _Float16 __z)
617 { return static_cast<__complex__ _Float16>(__builtin_ccoshf(__z)); }
619 inline __complex__ _Float16
620 __complex_exp(__complex__ _Float16 __z)
621 { return static_cast<__complex__ _Float16>(__builtin_cexpf(__z)); }
623 inline __complex__ _Float16
624 __complex_log(__complex__ _Float16 __z)
625 { return static_cast<__complex__ _Float16>(__builtin_clogf(__z)); }
627 inline __complex__ _Float16
628 __complex_sin(__complex__ _Float16 __z)
629 { return static_cast<__complex__ _Float16>(__builtin_csinf(__z)); }
631 inline __complex__ _Float16
632 __complex_sinh(__complex__ _Float16 __z)
633 { return static_cast<__complex__ _Float16>(__builtin_csinhf(__z)); }
635 inline __complex__ _Float16
636 __complex_sqrt(__complex__ _Float16 __z)
637 { return static_cast<__complex__ _Float16>(__builtin_csqrtf(__z)); }
639 inline __complex__ _Float16
640 __complex_tan(__complex__ _Float16 __z)
641 { return static_cast<__complex__ _Float16>(__builtin_ctanf(__z)); }
643 inline __complex__ _Float16
644 __complex_tanh(__complex__ _Float16 __z)
645 { return static_cast<__complex__ _Float16>(__builtin_ctanhf(__z)); }
647 inline __complex__ _Float16
648 __complex_pow(__complex__ _Float16 __x, __complex__ _Float16 __y)
649 { return static_cast<__complex__ _Float16>(__builtin_cpowf(__x, __y)); }
652#if defined(__STDCPP_FLOAT32_T__) && defined(_GLIBCXX_FLOAT_IS_IEEE_BINARY32)
654 __complex_abs(__complex__ _Float32 __z) { return __builtin_cabsf(__z); }
657 __complex_arg(__complex__ _Float32 __z) { return __builtin_cargf(__z); }
659 inline __complex__ _Float32
660 __complex_cos(__complex__ _Float32 __z) { return __builtin_ccosf(__z); }
662 inline __complex__ _Float32
663 __complex_cosh(__complex__ _Float32 __z) { return __builtin_ccoshf(__z); }
665 inline __complex__ _Float32
666 __complex_exp(__complex__ _Float32 __z) { return __builtin_cexpf(__z); }
668 inline __complex__ _Float32
669 __complex_log(__complex__ _Float32 __z) { return __builtin_clogf(__z); }
671 inline __complex__ _Float32
672 __complex_sin(__complex__ _Float32 __z) { return __builtin_csinf(__z); }
674 inline __complex__ _Float32
675 __complex_sinh(__complex__ _Float32 __z) { return __builtin_csinhf(__z); }
677 inline __complex__ _Float32
678 __complex_sqrt(__complex__ _Float32 __z) { return __builtin_csqrtf(__z); }
680 inline __complex__ _Float32
681 __complex_tan(__complex__ _Float32 __z) { return __builtin_ctanf(__z); }
683 inline __complex__ _Float32
684 __complex_tanh(__complex__ _Float32 __z) { return __builtin_ctanhf(__z); }
686 inline __complex__ _Float32
687 __complex_pow(__complex__ _Float32 __x, __complex__ _Float32 __y)
688 { return __builtin_cpowf(__x, __y); }
691#if defined(__STDCPP_FLOAT64_T__) && defined(_GLIBCXX_DOUBLE_IS_IEEE_BINARY64)
693 __complex_abs(__complex__ _Float64 __z) { return __builtin_cabs(__z); }
696 __complex_arg(__complex__ _Float64 __z) { return __builtin_carg(__z); }
698 inline __complex__ _Float64
699 __complex_cos(__complex__ _Float64 __z) { return __builtin_ccos(__z); }
701 inline __complex__ _Float64
702 __complex_cosh(__complex__ _Float64 __z) { return __builtin_ccosh(__z); }
704 inline __complex__ _Float64
705 __complex_exp(__complex__ _Float64 __z) { return __builtin_cexp(__z); }
707 inline __complex__ _Float64
708 __complex_log(__complex__ _Float64 __z) { return __builtin_clog(__z); }
710 inline __complex__ _Float64
711 __complex_sin(__complex__ _Float64 __z) { return __builtin_csin(__z); }
713 inline __complex__ _Float64
714 __complex_sinh(__complex__ _Float64 __z) { return __builtin_csinh(__z); }
716 inline __complex__ _Float64
717 __complex_sqrt(__complex__ _Float64 __z) { return __builtin_csqrt(__z); }
719 inline __complex__ _Float64
720 __complex_tan(__complex__ _Float64 __z) { return __builtin_ctan(__z); }
722 inline __complex__ _Float64
723 __complex_tanh(__complex__ _Float64 __z) { return __builtin_ctanh(__z); }
725 inline __complex__ _Float64
726 __complex_pow(__complex__ _Float64 __x, __complex__ _Float64 __y)
727 { return __builtin_cpow(__x, __y); }
730#if defined(__STDCPP_FLOAT128_T__) && defined(_GLIBCXX_LDOUBLE_IS_IEEE_BINARY128)
732 __complex_abs(__complex__ _Float128 __z) { return __builtin_cabsl(__z); }
735 __complex_arg(__complex__ _Float128 __z) { return __builtin_cargl(__z); }
737 inline __complex__ _Float128
738 __complex_cos(__complex__ _Float128 __z) { return __builtin_ccosl(__z); }
740 inline __complex__ _Float128
741 __complex_cosh(__complex__ _Float128 __z) { return __builtin_ccoshl(__z); }
743 inline __complex__ _Float128
744 __complex_exp(__complex__ _Float128 __z) { return __builtin_cexpl(__z); }
746 inline __complex__ _Float128
747 __complex_log(__complex__ _Float128 __z) { return __builtin_clogl(__z); }
749 inline __complex__ _Float128
750 __complex_sin(__complex__ _Float128 __z) { return __builtin_csinl(__z); }
752 inline __complex__ _Float128
753 __complex_sinh(__complex__ _Float128 __z) { return __builtin_csinhl(__z); }
755 inline __complex__ _Float128
756 __complex_sqrt(__complex__ _Float128 __z) { return __builtin_csqrtl(__z); }
758 inline __complex__ _Float128
759 __complex_tan(__complex__ _Float128 __z) { return __builtin_ctanl(__z); }
761 inline __complex__ _Float128
762 __complex_tanh(__complex__ _Float128 __z) { return __builtin_ctanhl(__z); }
764 inline __complex__ _Float128
765 __complex_pow(__complex__ _Float128 __x, __complex__ _Float128 __y)
766 { return __builtin_cpowl(__x, __y); }
767#elif defined(__STDCPP_FLOAT128_T__) && defined(_GLIBCXX_HAVE_FLOAT128_MATH)
769 __complex_abs(__complex__ _Float128 __z) { return __builtin_cabsf128(__z); }
772 __complex_arg(__complex__ _Float128 __z) { return __builtin_cargf128(__z); }
774 inline __complex__ _Float128
775 __complex_cos(__complex__ _Float128 __z) { return __builtin_ccosf128(__z); }
777 inline __complex__ _Float128
778 __complex_cosh(__complex__ _Float128 __z) { return __builtin_ccoshf128(__z); }
780 inline __complex__ _Float128
781 __complex_exp(__complex__ _Float128 __z) { return __builtin_cexpf128(__z); }
783 inline __complex__ _Float128
784 __complex_log(__complex__ _Float128 __z) { return __builtin_clogf128(__z); }
786 inline __complex__ _Float128
787 __complex_sin(__complex__ _Float128 __z) { return __builtin_csinf128(__z); }
789 inline __complex__ _Float128
790 __complex_sinh(__complex__ _Float128 __z) { return __builtin_csinhf128(__z); }
792 inline __complex__ _Float128
793 __complex_sqrt(__complex__ _Float128 __z) { return __builtin_csqrtf128(__z); }
795 inline __complex__ _Float128
796 __complex_tan(__complex__ _Float128 __z) { return __builtin_ctanf128(__z); }
798 inline __complex__ _Float128
799 __complex_tanh(__complex__ _Float128 __z) { return __builtin_ctanhf128(__z); }
801 inline __complex__ _Float128
802 __complex_pow(__complex__ _Float128 __x, __complex__ _Float128 __y)
803 { return __builtin_cpowf128(__x, __y); }
806#if defined(__STDCPP_BFLOAT16_T__) && defined(_GLIBCXX_FLOAT_IS_IEEE_BINARY32)
807 inline __gnu_cxx::__bfloat16_t
808 __complex_abs(__complex__ decltype(0.0bf16) __z)
809 { return __gnu_cxx::__bfloat16_t(__builtin_cabsf(__z)); }
811 inline __gnu_cxx::__bfloat16_t
812 __complex_arg(__complex__ decltype(0.0bf16) __z)
813 { return __gnu_cxx::__bfloat16_t(__builtin_cargf(__z)); }
815 inline __complex__ decltype(0.0bf16)
816 __complex_cos(__complex__ decltype(0.0bf16) __z)
817 { return static_cast<__complex__ decltype(0.0bf16)>(__builtin_ccosf(__z)); }
819 inline __complex__ decltype(0.0bf16)
820 __complex_cosh(__complex__ decltype(0.0bf16) __z)
821 { return static_cast<__complex__ decltype(0.0bf16)>(__builtin_ccoshf(__z)); }
823 inline __complex__ decltype(0.0bf16)
824 __complex_exp(__complex__ decltype(0.0bf16) __z)
825 { return static_cast<__complex__ decltype(0.0bf16)>(__builtin_cexpf(__z)); }
827 inline __complex__ decltype(0.0bf16)
828 __complex_log(__complex__ decltype(0.0bf16) __z)
829 { return static_cast<__complex__ decltype(0.0bf16)>(__builtin_clogf(__z)); }
831 inline __complex__ decltype(0.0bf16)
832 __complex_sin(__complex__ decltype(0.0bf16) __z)
833 { return static_cast<__complex__ decltype(0.0bf16)>(__builtin_csinf(__z)); }
835 inline __complex__ decltype(0.0bf16)
836 __complex_sinh(__complex__ decltype(0.0bf16) __z)
837 { return static_cast<__complex__ decltype(0.0bf16)>(__builtin_csinhf(__z)); }
839 inline __complex__ decltype(0.0bf16)
840 __complex_sqrt(__complex__ decltype(0.0bf16) __z)
841 { return static_cast<__complex__ decltype(0.0bf16)>(__builtin_csqrtf(__z)); }
843 inline __complex__ decltype(0.0bf16)
844 __complex_tan(__complex__ decltype(0.0bf16) __z)
845 { return static_cast<__complex__ decltype(0.0bf16)>(__builtin_ctanf(__z)); }
847 inline __complex__ decltype(0.0bf16)
848 __complex_tanh(__complex__ decltype(0.0bf16) __z)
849 { return static_cast<__complex__ decltype(0.0bf16)>(__builtin_ctanhf(__z)); }
851 inline __complex__ decltype(0.0bf16)
852 __complex_pow(__complex__ decltype(0.0bf16) __x,
853 __complex__ decltype(0.0bf16) __y)
854 { return static_cast<__complex__ decltype(0.0bf16)>(__builtin_cpowf(__x,
859 // 26.2.7/3 abs(__z): Returns the magnitude of __z.
860 template<typename _Tp>
862 __complex_abs(const complex<_Tp>& __z)
864 _Tp __x = __z.real();
865 _Tp __y = __z.imag();
866 const _Tp __s = std::max(abs(__x), abs(__y));
867 if (__s == _Tp()) // well ...
871 return __s * sqrt(__x * __x + __y * __y);
874#if _GLIBCXX_USE_C99_COMPLEX
876 __complex_abs(__complex__ float __z) { return __builtin_cabsf(__z); }
879 __complex_abs(__complex__ double __z) { return __builtin_cabs(__z); }
882 __complex_abs(const __complex__ long double& __z)
883 { return __builtin_cabsl(__z); }
885 template<typename _Tp>
887 abs(const complex<_Tp>& __z) { return __complex_abs(__z.__rep()); }
889 template<typename _Tp>
891 abs(const complex<_Tp>& __z) { return __complex_abs(__z); }
895 // 26.2.7/4: arg(__z): Returns the phase angle of __z.
896 template<typename _Tp>
898 __complex_arg(const complex<_Tp>& __z)
899 { return atan2(__z.imag(), __z.real()); }
901#if _GLIBCXX_USE_C99_COMPLEX
903 __complex_arg(__complex__ float __z) { return __builtin_cargf(__z); }
906 __complex_arg(__complex__ double __z) { return __builtin_carg(__z); }
909 __complex_arg(const __complex__ long double& __z)
910 { return __builtin_cargl(__z); }
912 template<typename _Tp>
914 arg(const complex<_Tp>& __z) { return __complex_arg(__z.__rep()); }
916 template<typename _Tp>
918 arg(const complex<_Tp>& __z) { return __complex_arg(__z); }
921 // 26.2.7/5: norm(__z) returns the squared magnitude of __z.
922 // As defined, norm() is -not- a norm is the common mathematical
923 // sense used in numerics. The helper class _Norm_helper<> tries to
924 // distinguish between builtin floating point and the rest, so as
925 // to deliver an answer as close as possible to the real value.
929 template<typename _Tp>
930 static inline _GLIBCXX20_CONSTEXPR _Tp _S_do_it(const complex<_Tp>& __z)
932 const _Tp __x = __z.real();
933 const _Tp __y = __z.imag();
934 return __x * __x + __y * __y;
939 struct _Norm_helper<true>
941 template<typename _Tp>
942 static inline _GLIBCXX20_CONSTEXPR _Tp _S_do_it(const complex<_Tp>& __z)
944 //_Tp __res = std::abs(__z);
945 //return __res * __res;
946 const _Tp __x = __z.real();
947 const _Tp __y = __z.imag();
948 return __x * __x + __y * __y;
952 template<typename _Tp>
953 inline _GLIBCXX20_CONSTEXPR _Tp
954 norm(const complex<_Tp>& __z)
956 return _Norm_helper<__is_floating<_Tp>::__value
957 && !_GLIBCXX_FAST_MATH>::_S_do_it(__z);
960 template<typename _Tp>
962 polar(const _Tp& __rho, const _Tp& __theta)
964 __glibcxx_assert( __rho >= 0 );
965 return complex<_Tp>(__rho * cos(__theta), __rho * sin(__theta));
968 template<typename _Tp>
969 inline _GLIBCXX20_CONSTEXPR complex<_Tp>
970 conj(const complex<_Tp>& __z)
971 { return complex<_Tp>(__z.real(), -__z.imag()); }
975 // 26.2.8/1 cos(__z): Returns the cosine of __z.
976 template<typename _Tp>
978 __complex_cos(const complex<_Tp>& __z)
980 const _Tp __x = __z.real();
981 const _Tp __y = __z.imag();
982 return complex<_Tp>(cos(__x) * cosh(__y), -sin(__x) * sinh(__y));
985#if _GLIBCXX_USE_C99_COMPLEX
986 inline __complex__ float
987 __complex_cos(__complex__ float __z) { return __builtin_ccosf(__z); }
989 inline __complex__ double
990 __complex_cos(__complex__ double __z) { return __builtin_ccos(__z); }
992 inline __complex__ long double
993 __complex_cos(const __complex__ long double& __z)
994 { return __builtin_ccosl(__z); }
996 template<typename _Tp>
998 cos(const complex<_Tp>& __z) { return __complex_cos(__z.__rep()); }
1000 template<typename _Tp>
1002 cos(const complex<_Tp>& __z) { return __complex_cos(__z); }
1005 // 26.2.8/2 cosh(__z): Returns the hyperbolic cosine of __z.
1006 template<typename _Tp>
1008 __complex_cosh(const complex<_Tp>& __z)
1010 const _Tp __x = __z.real();
1011 const _Tp __y = __z.imag();
1012 return complex<_Tp>(cosh(__x) * cos(__y), sinh(__x) * sin(__y));
1015#if _GLIBCXX_USE_C99_COMPLEX
1016 inline __complex__ float
1017 __complex_cosh(__complex__ float __z) { return __builtin_ccoshf(__z); }
1019 inline __complex__ double
1020 __complex_cosh(__complex__ double __z) { return __builtin_ccosh(__z); }
1022 inline __complex__ long double
1023 __complex_cosh(const __complex__ long double& __z)
1024 { return __builtin_ccoshl(__z); }
1026 template<typename _Tp>
1028 cosh(const complex<_Tp>& __z) { return __complex_cosh(__z.__rep()); }
1030 template<typename _Tp>
1032 cosh(const complex<_Tp>& __z) { return __complex_cosh(__z); }
1035 // 26.2.8/3 exp(__z): Returns the complex base e exponential of x
1036 template<typename _Tp>
1038 __complex_exp(const complex<_Tp>& __z)
1039 { return std::polar<_Tp>(exp(__z.real()), __z.imag()); }
1041#if _GLIBCXX_USE_C99_COMPLEX
1042 inline __complex__ float
1043 __complex_exp(__complex__ float __z) { return __builtin_cexpf(__z); }
1045 inline __complex__ double
1046 __complex_exp(__complex__ double __z) { return __builtin_cexp(__z); }
1048 inline __complex__ long double
1049 __complex_exp(const __complex__ long double& __z)
1050 { return __builtin_cexpl(__z); }
1052 template<typename _Tp>
1054 exp(const complex<_Tp>& __z) { return __complex_exp(__z.__rep()); }
1056 template<typename _Tp>
1058 exp(const complex<_Tp>& __z) { return __complex_exp(__z); }
1061 // 26.2.8/5 log(__z): Returns the natural complex logarithm of __z.
1062 // The branch cut is along the negative axis.
1063 template<typename _Tp>
1065 __complex_log(const complex<_Tp>& __z)
1066 { return complex<_Tp>(log(std::abs(__z)), std::arg(__z)); }
1068#if _GLIBCXX_USE_C99_COMPLEX
1069 inline __complex__ float
1070 __complex_log(__complex__ float __z) { return __builtin_clogf(__z); }
1072 inline __complex__ double
1073 __complex_log(__complex__ double __z) { return __builtin_clog(__z); }
1075 inline __complex__ long double
1076 __complex_log(const __complex__ long double& __z)
1077 { return __builtin_clogl(__z); }
1079 template<typename _Tp>
1081 log(const complex<_Tp>& __z) { return __complex_log(__z.__rep()); }
1083 template<typename _Tp>
1085 log(const complex<_Tp>& __z) { return __complex_log(__z); }
1088 template<typename _Tp>
1090 log10(const complex<_Tp>& __z)
1091 { return std::log(__z) / log(_Tp(10.0)); }
1093 // 26.2.8/10 sin(__z): Returns the sine of __z.
1094 template<typename _Tp>
1096 __complex_sin(const complex<_Tp>& __z)
1098 const _Tp __x = __z.real();
1099 const _Tp __y = __z.imag();
1100 return complex<_Tp>(sin(__x) * cosh(__y), cos(__x) * sinh(__y));
1103#if _GLIBCXX_USE_C99_COMPLEX
1104 inline __complex__ float
1105 __complex_sin(__complex__ float __z) { return __builtin_csinf(__z); }
1107 inline __complex__ double
1108 __complex_sin(__complex__ double __z) { return __builtin_csin(__z); }
1110 inline __complex__ long double
1111 __complex_sin(const __complex__ long double& __z)
1112 { return __builtin_csinl(__z); }
1114 template<typename _Tp>
1116 sin(const complex<_Tp>& __z) { return __complex_sin(__z.__rep()); }
1118 template<typename _Tp>
1120 sin(const complex<_Tp>& __z) { return __complex_sin(__z); }
1123 // 26.2.8/11 sinh(__z): Returns the hyperbolic sine of __z.
1124 template<typename _Tp>
1126 __complex_sinh(const complex<_Tp>& __z)
1128 const _Tp __x = __z.real();
1129 const _Tp __y = __z.imag();
1130 return complex<_Tp>(sinh(__x) * cos(__y), cosh(__x) * sin(__y));
1133#if _GLIBCXX_USE_C99_COMPLEX
1134 inline __complex__ float
1135 __complex_sinh(__complex__ float __z) { return __builtin_csinhf(__z); }
1137 inline __complex__ double
1138 __complex_sinh(__complex__ double __z) { return __builtin_csinh(__z); }
1140 inline __complex__ long double
1141 __complex_sinh(const __complex__ long double& __z)
1142 { return __builtin_csinhl(__z); }
1144 template<typename _Tp>
1146 sinh(const complex<_Tp>& __z) { return __complex_sinh(__z.__rep()); }
1148 template<typename _Tp>
1150 sinh(const complex<_Tp>& __z) { return __complex_sinh(__z); }
1153 // 26.2.8/13 sqrt(__z): Returns the complex square root of __z.
1154 // The branch cut is on the negative axis.
1155 template<typename _Tp>
1157 __complex_sqrt(const complex<_Tp>& __z)
1159 _Tp __x = __z.real();
1160 _Tp __y = __z.imag();
1164 _Tp __t = sqrt(abs(__y) / 2);
1165 return complex<_Tp>(__t, __y < _Tp() ? -__t : __t);
1169 _Tp __t = sqrt(2 * (std::abs(__z) + abs(__x)));
1172 ? complex<_Tp>(__u, __y / __t)
1173 : complex<_Tp>(abs(__y) / __t, __y < _Tp() ? -__u : __u);
1177#if _GLIBCXX_USE_C99_COMPLEX
1178 inline __complex__ float
1179 __complex_sqrt(__complex__ float __z) { return __builtin_csqrtf(__z); }
1181 inline __complex__ double
1182 __complex_sqrt(__complex__ double __z) { return __builtin_csqrt(__z); }
1184 inline __complex__ long double
1185 __complex_sqrt(const __complex__ long double& __z)
1186 { return __builtin_csqrtl(__z); }
1188 template<typename _Tp>
1190 sqrt(const complex<_Tp>& __z) { return __complex_sqrt(__z.__rep()); }
1192 template<typename _Tp>
1194 sqrt(const complex<_Tp>& __z) { return __complex_sqrt(__z); }
1197 // 26.2.8/14 tan(__z): Return the complex tangent of __z.
1199 template<typename _Tp>
1201 __complex_tan(const complex<_Tp>& __z)
1202 { return std::sin(__z) / std::cos(__z); }
1204#if _GLIBCXX_USE_C99_COMPLEX
1205 inline __complex__ float
1206 __complex_tan(__complex__ float __z) { return __builtin_ctanf(__z); }
1208 inline __complex__ double
1209 __complex_tan(__complex__ double __z) { return __builtin_ctan(__z); }
1211 inline __complex__ long double
1212 __complex_tan(const __complex__ long double& __z)
1213 { return __builtin_ctanl(__z); }
1215 template<typename _Tp>
1217 tan(const complex<_Tp>& __z) { return __complex_tan(__z.__rep()); }
1219 template<typename _Tp>
1221 tan(const complex<_Tp>& __z) { return __complex_tan(__z); }
1225 // 26.2.8/15 tanh(__z): Returns the hyperbolic tangent of __z.
1227 template<typename _Tp>
1229 __complex_tanh(const complex<_Tp>& __z)
1230 { return std::sinh(__z) / std::cosh(__z); }
1232#if _GLIBCXX_USE_C99_COMPLEX
1233 inline __complex__ float
1234 __complex_tanh(__complex__ float __z) { return __builtin_ctanhf(__z); }
1236 inline __complex__ double
1237 __complex_tanh(__complex__ double __z) { return __builtin_ctanh(__z); }
1239 inline __complex__ long double
1240 __complex_tanh(const __complex__ long double& __z)
1241 { return __builtin_ctanhl(__z); }
1243 template<typename _Tp>
1245 tanh(const complex<_Tp>& __z) { return __complex_tanh(__z.__rep()); }
1247 template<typename _Tp>
1249 tanh(const complex<_Tp>& __z) { return __complex_tanh(__z); }
1253 // 26.2.8/9 pow(__x, __y): Returns the complex power base of __x
1254 // raised to the __y-th power. The branch
1255 // cut is on the negative axis.
1256 template<typename _Tp>
1258 __complex_pow_unsigned(complex<_Tp> __x, unsigned __n)
1260 complex<_Tp> __y = __n % 2 ? __x : complex<_Tp>(1);
1272 // In C++11 mode we used to implement the resolution of
1273 // DR 844. complex pow return type is ambiguous.
1274 // thus the following overload was disabled in that mode. However, doing
1275 // that causes all sorts of issues, see, for example:
1276 // http://gcc.gnu.org/ml/libstdc++/2013-01/msg00058.html
1277 // and also PR57974.
1278 template<typename _Tp>
1280 pow(const complex<_Tp>& __z, int __n)
1283 ? complex<_Tp>(1) / std::__complex_pow_unsigned(__z, -(unsigned)__n)
1284 : std::__complex_pow_unsigned(__z, __n);
1287 template<typename _Tp>
1289 pow(const complex<_Tp>& __x, const _Tp& __y)
1291#if ! _GLIBCXX_USE_C99_COMPLEX
1295 if (__x.imag() == _Tp() && __x.real() > _Tp())
1296 return pow(__x.real(), __y);
1298 complex<_Tp> __t = std::log(__x);
1299 return std::polar<_Tp>(exp(__y * __t.real()), __y * __t.imag());
1302 template<typename _Tp>
1304 __complex_pow(const complex<_Tp>& __x, const complex<_Tp>& __y)
1305 { return __x == _Tp() ? _Tp() : std::exp(__y * std::log(__x)); }
1307#if _GLIBCXX_USE_C99_COMPLEX
1308 inline __complex__ float
1309 __complex_pow(__complex__ float __x, __complex__ float __y)
1310 { return __builtin_cpowf(__x, __y); }
1312 inline __complex__ double
1313 __complex_pow(__complex__ double __x, __complex__ double __y)
1314 { return __builtin_cpow(__x, __y); }
1316 inline __complex__ long double
1317 __complex_pow(const __complex__ long double& __x,
1318 const __complex__ long double& __y)
1319 { return __builtin_cpowl(__x, __y); }
1321 template<typename _Tp>
1323 pow(const complex<_Tp>& __x, const complex<_Tp>& __y)
1324 { return __complex_pow(__x.__rep(), __y.__rep()); }
1326 template<typename _Tp>
1328 pow(const complex<_Tp>& __x, const complex<_Tp>& __y)
1329 { return __complex_pow(__x, __y); }
1332 template<typename _Tp>
1334 pow(const _Tp& __x, const complex<_Tp>& __y)
1336 return __x > _Tp() ? std::polar<_Tp>(pow(__x, __y.real()),
1337 __y.imag() * log(__x))
1338 : std::pow(complex<_Tp>(__x), __y);
1341 /// 26.2.3 complex specializations
1342 /// complex<float> specialization
1344 class complex<float>
1347 typedef float value_type;
1348 typedef __complex__ float _ComplexT;
1350 _GLIBCXX_CONSTEXPR complex(_ComplexT __z) : _M_value(__z) { }
1352 _GLIBCXX_CONSTEXPR complex(float __r = 0.0f, float __i = 0.0f)
1353#if __cplusplus >= 201103L
1354 : _M_value{ __r, __i } { }
1357 __real__ _M_value = __r;
1358 __imag__ _M_value = __i;
1362#if __cplusplus > 202002L
1363 template<typename _Up>
1364 explicit(!requires(_Up __u) { value_type{__u}; })
1365 constexpr complex(const complex<_Up>& __z)
1366 : _M_value{ value_type(__z.real()), value_type(__z.imag()) } { }
1368 explicit _GLIBCXX_CONSTEXPR complex(const complex<double>&);
1369 explicit _GLIBCXX_CONSTEXPR complex(const complex<long double>&);
1372#if __cplusplus >= 201103L
1373 // _GLIBCXX_RESOLVE_LIB_DEFECTS
1374 // DR 387. std::complex over-encapsulated.
1375 __attribute ((__abi_tag__ ("cxx11")))
1377 real() const { return __real__ _M_value; }
1379 __attribute ((__abi_tag__ ("cxx11")))
1381 imag() const { return __imag__ _M_value; }
1384 real() { return __real__ _M_value; }
1387 real() const { return __real__ _M_value; }
1390 imag() { return __imag__ _M_value; }
1393 imag() const { return __imag__ _M_value; }
1396 // _GLIBCXX_RESOLVE_LIB_DEFECTS
1397 // DR 387. std::complex over-encapsulated.
1398 _GLIBCXX20_CONSTEXPR void
1399 real(float __val) { __real__ _M_value = __val; }
1401 _GLIBCXX20_CONSTEXPR void
1402 imag(float __val) { __imag__ _M_value = __val; }
1404 _GLIBCXX20_CONSTEXPR complex&
1405 operator=(float __f)
1411 _GLIBCXX20_CONSTEXPR complex&
1412 operator+=(float __f)
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 // Let the compiler synthesize the copy and assignment
1440 // operator. It always does a pretty good job.
1441#if __cplusplus >= 201103L
1442 _GLIBCXX14_CONSTEXPR complex& operator=(const complex&) = default;
1445 template<typename _Tp>
1446 _GLIBCXX20_CONSTEXPR complex&
1447 operator=(const complex<_Tp>& __z)
1449 __real__ _M_value = __z.real();
1450 __imag__ _M_value = __z.imag();
1454 template<typename _Tp>
1455 _GLIBCXX20_CONSTEXPR complex&
1456 operator+=(const complex<_Tp>& __z)
1458 _M_value += __z.__rep();
1463 _GLIBCXX20_CONSTEXPR complex&
1464 operator-=(const complex<_Tp>& __z)
1466 _M_value -= __z.__rep();
1471 _GLIBCXX20_CONSTEXPR complex&
1472 operator*=(const complex<_Tp>& __z)
1474 const _ComplexT __t = __z.__rep();
1480 _GLIBCXX20_CONSTEXPR complex&
1481 operator/=(const complex<_Tp>& __z)
1483 const _ComplexT __t = __z.__rep();
1488 _GLIBCXX_CONSTEXPR _ComplexT __rep() const { return _M_value; }
1494 /// 26.2.3 complex specializations
1495 /// complex<double> specialization
1497 class complex<double>
1500 typedef double value_type;
1501 typedef __complex__ double _ComplexT;
1503 _GLIBCXX_CONSTEXPR complex(_ComplexT __z) : _M_value(__z) { }
1505 _GLIBCXX_CONSTEXPR complex(double __r = 0.0, double __i = 0.0)
1506#if __cplusplus >= 201103L
1507 : _M_value{ __r, __i } { }
1510 __real__ _M_value = __r;
1511 __imag__ _M_value = __i;
1515#if __cplusplus > 202002L
1516 template<typename _Up>
1517 explicit(!requires(_Up __u) { value_type{__u}; })
1518 constexpr complex(const complex<_Up>& __z)
1519 : _M_value{ value_type(__z.real()), value_type(__z.imag()) } { }
1521 _GLIBCXX_CONSTEXPR complex(const complex<float>& __z)
1522 : _M_value(__z.__rep()) { }
1524 explicit _GLIBCXX_CONSTEXPR complex(const complex<long double>&);
1527#if __cplusplus >= 201103L
1528 // _GLIBCXX_RESOLVE_LIB_DEFECTS
1529 // DR 387. std::complex over-encapsulated.
1530 __attribute ((__abi_tag__ ("cxx11")))
1532 real() const { return __real__ _M_value; }
1534 __attribute ((__abi_tag__ ("cxx11")))
1536 imag() const { return __imag__ _M_value; }
1539 real() { return __real__ _M_value; }
1542 real() const { return __real__ _M_value; }
1545 imag() { return __imag__ _M_value; }
1548 imag() const { return __imag__ _M_value; }
1551 // _GLIBCXX_RESOLVE_LIB_DEFECTS
1552 // DR 387. std::complex over-encapsulated.
1553 _GLIBCXX20_CONSTEXPR void
1554 real(double __val) { __real__ _M_value = __val; }
1556 _GLIBCXX20_CONSTEXPR void
1557 imag(double __val) { __imag__ _M_value = __val; }
1559 _GLIBCXX20_CONSTEXPR complex&
1560 operator=(double __d)
1566 _GLIBCXX20_CONSTEXPR complex&
1567 operator+=(double __d)
1573 _GLIBCXX20_CONSTEXPR complex&
1574 operator-=(double __d)
1580 _GLIBCXX20_CONSTEXPR complex&
1581 operator*=(double __d)
1587 _GLIBCXX20_CONSTEXPR complex&
1588 operator/=(double __d)
1594 // The compiler will synthesize this, efficiently.
1595#if __cplusplus >= 201103L
1596 _GLIBCXX14_CONSTEXPR complex& operator=(const complex&) = default;
1599 template<typename _Tp>
1600 _GLIBCXX20_CONSTEXPR complex&
1601 operator=(const complex<_Tp>& __z)
1603 _M_value = __z.__rep();
1607 template<typename _Tp>
1608 _GLIBCXX20_CONSTEXPR complex&
1609 operator+=(const complex<_Tp>& __z)
1611 _M_value += __z.__rep();
1615 template<typename _Tp>
1616 _GLIBCXX20_CONSTEXPR complex&
1617 operator-=(const complex<_Tp>& __z)
1619 _M_value -= __z.__rep();
1623 template<typename _Tp>
1624 _GLIBCXX20_CONSTEXPR complex&
1625 operator*=(const complex<_Tp>& __z)
1627 const _ComplexT __t = __z.__rep();
1632 template<typename _Tp>
1633 _GLIBCXX20_CONSTEXPR complex&
1634 operator/=(const complex<_Tp>& __z)
1636 const _ComplexT __t = __z.__rep();
1641 _GLIBCXX_CONSTEXPR _ComplexT __rep() const { return _M_value; }
1647 /// 26.2.3 complex specializations
1648 /// complex<long double> specialization
1650 class complex<long double>
1653 typedef long double value_type;
1654 typedef __complex__ long double _ComplexT;
1656 _GLIBCXX_CONSTEXPR complex(_ComplexT __z) : _M_value(__z) { }
1658 _GLIBCXX_CONSTEXPR complex(long double __r = 0.0L,
1659 long double __i = 0.0L)
1660#if __cplusplus >= 201103L
1661 : _M_value{ __r, __i } { }
1664 __real__ _M_value = __r;
1665 __imag__ _M_value = __i;
1669#if __cplusplus > 202002L
1670 template<typename _Up>
1671 explicit(!requires(_Up __u) { value_type{__u}; })
1672 constexpr complex(const complex<_Up>& __z)
1673 : _M_value{ value_type(__z.real()), value_type(__z.imag()) } { }
1675 _GLIBCXX_CONSTEXPR complex(const complex<float>& __z)
1676 : _M_value(__z.__rep()) { }
1678 _GLIBCXX_CONSTEXPR complex(const complex<double>& __z)
1679 : _M_value(__z.__rep()) { }
1682#if __cplusplus >= 201103L
1683 // _GLIBCXX_RESOLVE_LIB_DEFECTS
1684 // DR 387. std::complex over-encapsulated.
1685 __attribute ((__abi_tag__ ("cxx11")))
1686 constexpr long double
1687 real() const { return __real__ _M_value; }
1689 __attribute ((__abi_tag__ ("cxx11")))
1690 constexpr long double
1691 imag() const { return __imag__ _M_value; }
1694 real() { return __real__ _M_value; }
1697 real() const { return __real__ _M_value; }
1700 imag() { return __imag__ _M_value; }
1703 imag() const { return __imag__ _M_value; }
1706 // _GLIBCXX_RESOLVE_LIB_DEFECTS
1707 // DR 387. std::complex over-encapsulated.
1708 _GLIBCXX20_CONSTEXPR void
1709 real(long double __val) { __real__ _M_value = __val; }
1711 _GLIBCXX20_CONSTEXPR void
1712 imag(long double __val) { __imag__ _M_value = __val; }
1714 _GLIBCXX20_CONSTEXPR complex&
1715 operator=(long double __r)
1721 _GLIBCXX20_CONSTEXPR complex&
1722 operator+=(long double __r)
1728 _GLIBCXX20_CONSTEXPR complex&
1729 operator-=(long double __r)
1735 _GLIBCXX20_CONSTEXPR complex&
1736 operator*=(long double __r)
1742 _GLIBCXX20_CONSTEXPR complex&
1743 operator/=(long double __r)
1749 // The compiler knows how to do this efficiently
1750#if __cplusplus >= 201103L
1751 _GLIBCXX14_CONSTEXPR complex& operator=(const complex&) = default;
1754 template<typename _Tp>
1755 _GLIBCXX20_CONSTEXPR complex&
1756 operator=(const complex<_Tp>& __z)
1758 _M_value = __z.__rep();
1762 template<typename _Tp>
1763 _GLIBCXX20_CONSTEXPR complex&
1764 operator+=(const complex<_Tp>& __z)
1766 _M_value += __z.__rep();
1770 template<typename _Tp>
1771 _GLIBCXX20_CONSTEXPR complex&
1772 operator-=(const complex<_Tp>& __z)
1774 _M_value -= __z.__rep();
1778 template<typename _Tp>
1779 _GLIBCXX20_CONSTEXPR complex&
1780 operator*=(const complex<_Tp>& __z)
1782 const _ComplexT __t = __z.__rep();
1787 template<typename _Tp>
1788 _GLIBCXX20_CONSTEXPR complex&
1789 operator/=(const complex<_Tp>& __z)
1791 const _ComplexT __t = __z.__rep();
1796 _GLIBCXX_CONSTEXPR _ComplexT __rep() const { return _M_value; }
1802#if __cplusplus > 202002L
1803 template<typename _Tp>
1804 struct __complex_type
1807#ifdef __STDCPP_FLOAT16_T__
1809 struct __complex_type<_Float16>
1810 { typedef __complex__ _Float16 type; };
1813#ifdef __STDCPP_FLOAT32_T__
1815 struct __complex_type<_Float32>
1816 { typedef __complex__ _Float32 type; };
1819#ifdef __STDCPP_FLOAT64_T__
1821 struct __complex_type<_Float64>
1822 { typedef __complex__ _Float64 type; };
1825#ifdef __STDCPP_FLOAT128_T__
1827 struct __complex_type<_Float128>
1828 { typedef __complex__ _Float128 type; };
1831#ifdef __STDCPP_BFLOAT16_T__
1833 struct __complex_type<__gnu_cxx::__bfloat16_t>
1834 { typedef __complex__ decltype(0.0bf16) type; };
1837 template<typename _Tp>
1838 requires requires { typename __complex_type<_Tp>::type; }
1842 typedef _Tp value_type;
1843 typedef typename std::__complex_type<_Tp>::type _ComplexT;
1845 constexpr complex(_ComplexT __z) : _M_value(__z) { }
1847 constexpr complex(_Tp __r = _Tp(), _Tp __i = _Tp())
1848 : _M_value{ __r, __i } { }
1850 template<typename _Up>
1851 explicit(!requires(_Up __u) { value_type{__u}; })
1852 constexpr complex(const complex<_Up>& __z)
1853 : _M_value{ value_type(__z.real()), value_type(__z.imag()) } { }
1856 real() const { return __real__ _M_value; }
1859 imag() const { return __imag__ _M_value; }
1862 real(_Tp __val) { __real__ _M_value = __val; }
1865 imag(_Tp __val) { __imag__ _M_value = __val; }
1902 // Let the compiler synthesize the copy and assignment
1903 // operator. It always does a pretty good job.
1904 constexpr complex& operator=(const complex&) = default;
1906 template<typename _Up>
1908 operator=(const complex<_Up>& __z)
1910 __real__ _M_value = __z.real();
1911 __imag__ _M_value = __z.imag();
1915 template<typename _Up>
1917 operator+=(const complex<_Up>& __z)
1919 _M_value += __z.__rep();
1925 operator-=(const complex<_Up>& __z)
1927 _M_value -= __z.__rep();
1933 operator*=(const complex<_Up>& __z)
1935 const _ComplexT __t = __z.__rep();
1942 operator/=(const complex<_Up>& __z)
1944 const _ComplexT __t = __z.__rep();
1949 constexpr _ComplexT __rep() const { return _M_value; }
1956#if __cplusplus <= 202002L
1957 // These bits have to be at the end of this file, so that the
1958 // specializations have all been defined.
1959 inline _GLIBCXX_CONSTEXPR
1960 complex<float>::complex(const complex<double>& __z)
1961 : _M_value(__z.__rep()) { }
1963 inline _GLIBCXX_CONSTEXPR
1964 complex<float>::complex(const complex<long double>& __z)
1965 : _M_value(__z.__rep()) { }
1967 inline _GLIBCXX_CONSTEXPR
1968 complex<double>::complex(const complex<long double>& __z)
1969 : _M_value(__z.__rep()) { }
1972 // Inhibit implicit instantiations for required instantiations,
1973 // which are defined via explicit instantiations elsewhere.
1974 // NB: This syntax is a GNU extension.
1975#if _GLIBCXX_EXTERN_TEMPLATE
1976 extern template istream& operator>>(istream&, complex<float>&);
1977 extern template ostream& operator<<(ostream&, const complex<float>&);
1978 extern template istream& operator>>(istream&, complex<double>&);
1979 extern template ostream& operator<<(ostream&, const complex<double>&);
1980 extern template istream& operator>>(istream&, complex<long double>&);
1981 extern template ostream& operator<<(ostream&, const complex<long double>&);
1983#ifdef _GLIBCXX_USE_WCHAR_T
1984 extern template wistream& operator>>(wistream&, complex<float>&);
1985 extern template wostream& operator<<(wostream&, const complex<float>&);
1986 extern template wistream& operator>>(wistream&, complex<double>&);
1987 extern template wostream& operator<<(wostream&, const complex<double>&);
1988 extern template wistream& operator>>(wistream&, complex<long double>&);
1989 extern template wostream& operator<<(wostream&, const complex<long double>&);
1993 /// @} group complex_numbers
1995_GLIBCXX_END_NAMESPACE_VERSION
1998#if __cplusplus >= 201103L
2000namespace std _GLIBCXX_VISIBILITY(default)
2002_GLIBCXX_BEGIN_NAMESPACE_VERSION
2004 // Forward declarations.
2005 template<typename _Tp> std::complex<_Tp> acos(const std::complex<_Tp>&);
2006 template<typename _Tp> std::complex<_Tp> asin(const std::complex<_Tp>&);
2007 template<typename _Tp> std::complex<_Tp> atan(const std::complex<_Tp>&);
2009 template<typename _Tp> std::complex<_Tp> acosh(const std::complex<_Tp>&);
2010 template<typename _Tp> std::complex<_Tp> asinh(const std::complex<_Tp>&);
2011 template<typename _Tp> std::complex<_Tp> atanh(const std::complex<_Tp>&);
2013 template<typename _Tp> _Tp fabs(const std::complex<_Tp>&);
2015 template<typename _Tp>
2016 inline std::complex<_Tp>
2017 __complex_acos(const std::complex<_Tp>& __z)
2019 const std::complex<_Tp> __t = std::asin(__z);
2020 const _Tp __pi_2 = 1.5707963267948966192313216916397514L;
2021 return std::complex<_Tp>(__pi_2 - __t.real(), -__t.imag());
2024#if _GLIBCXX_USE_C99_COMPLEX_TR1
2025#if defined(__STDCPP_FLOAT16_T__) && defined(_GLIBCXX_FLOAT_IS_IEEE_BINARY32)
2026 inline __complex__ _Float16
2027 __complex_acos(__complex__ _Float16 __z)
2028 { return static_cast<__complex__ _Float16>(__builtin_cacosf(__z)); }
2030 inline __complex__ _Float16
2031 __complex_asin(__complex__ _Float16 __z)
2032 { return static_cast<__complex__ _Float16>(__builtin_casinf(__z)); }
2034 inline __complex__ _Float16
2035 __complex_atan(__complex__ _Float16 __z)
2036 { return static_cast<__complex__ _Float16>(__builtin_catanf(__z)); }
2038 inline __complex__ _Float16
2039 __complex_acosh(__complex__ _Float16 __z)
2040 { return static_cast<__complex__ _Float16>(__builtin_cacoshf(__z)); }
2042 inline __complex__ _Float16
2043 __complex_asinh(__complex__ _Float16 __z)
2044 { return static_cast<__complex__ _Float16>(__builtin_casinhf(__z)); }
2046 inline __complex__ _Float16
2047 __complex_atanh(__complex__ _Float16 __z)
2048 { return static_cast<__complex__ _Float16>(__builtin_catanhf(__z)); }
2051#if defined(__STDCPP_FLOAT32_T__) && defined(_GLIBCXX_FLOAT_IS_IEEE_BINARY32)
2052 inline __complex__ _Float32
2053 __complex_acos(__complex__ _Float32 __z)
2054 { return __builtin_cacosf(__z); }
2056 inline __complex__ _Float32
2057 __complex_asin(__complex__ _Float32 __z)
2058 { return __builtin_casinf(__z); }
2060 inline __complex__ _Float32
2061 __complex_atan(__complex__ _Float32 __z)
2062 { return __builtin_catanf(__z); }
2064 inline __complex__ _Float32
2065 __complex_acosh(__complex__ _Float32 __z)
2066 { return __builtin_cacoshf(__z); }
2068 inline __complex__ _Float32
2069 __complex_asinh(__complex__ _Float32 __z)
2070 { return __builtin_casinhf(__z); }
2072 inline __complex__ _Float32
2073 __complex_atanh(__complex__ _Float32 __z)
2074 { return __builtin_catanhf(__z); }
2077#if defined(__STDCPP_FLOAT64_T__) && defined(_GLIBCXX_DOUBLE_IS_IEEE_BINARY64)
2078 inline __complex__ _Float64
2079 __complex_acos(__complex__ _Float64 __z)
2080 { return __builtin_cacos(__z); }
2082 inline __complex__ _Float64
2083 __complex_asin(__complex__ _Float64 __z)
2084 { return __builtin_casin(__z); }
2086 inline __complex__ _Float64
2087 __complex_atan(__complex__ _Float64 __z)
2088 { return __builtin_catan(__z); }
2090 inline __complex__ _Float64
2091 __complex_acosh(__complex__ _Float64 __z)
2092 { return __builtin_cacosh(__z); }
2094 inline __complex__ _Float64
2095 __complex_asinh(__complex__ _Float64 __z)
2096 { return __builtin_casinh(__z); }
2098 inline __complex__ _Float64
2099 __complex_atanh(__complex__ _Float64 __z)
2100 { return __builtin_catanh(__z); }
2103#if defined(__STDCPP_FLOAT128_T__) && defined(_GLIBCXX_LDOUBLE_IS_IEEE_BINARY128)
2104 inline __complex__ _Float128
2105 __complex_acos(__complex__ _Float128 __z)
2106 { return __builtin_cacosl(__z); }
2108 inline __complex__ _Float128
2109 __complex_asin(__complex__ _Float128 __z)
2110 { return __builtin_casinl(__z); }
2112 inline __complex__ _Float128
2113 __complex_atan(__complex__ _Float128 __z)
2114 { return __builtin_catanl(__z); }
2116 inline __complex__ _Float128
2117 __complex_acosh(__complex__ _Float128 __z)
2118 { return __builtin_cacoshl(__z); }
2120 inline __complex__ _Float128
2121 __complex_asinh(__complex__ _Float128 __z)
2122 { return __builtin_casinhl(__z); }
2124 inline __complex__ _Float128
2125 __complex_atanh(__complex__ _Float128 __z)
2126 { return __builtin_catanhl(__z); }
2127#elif defined(__STDCPP_FLOAT128_T__) && defined(_GLIBCXX_HAVE_FLOAT128_MATH)
2128 inline __complex__ _Float128
2129 __complex_acos(__complex__ _Float128 __z)
2130 { return __builtin_cacosf128(__z); }
2132 inline __complex__ _Float128
2133 __complex_asin(__complex__ _Float128 __z)
2134 { return __builtin_casinf128(__z); }
2136 inline __complex__ _Float128
2137 __complex_atan(__complex__ _Float128 __z)
2138 { return __builtin_catanf128(__z); }
2140 inline __complex__ _Float128
2141 __complex_acosh(__complex__ _Float128 __z)
2142 { return __builtin_cacoshf128(__z); }
2144 inline __complex__ _Float128
2145 __complex_asinh(__complex__ _Float128 __z)
2146 { return __builtin_casinhf128(__z); }
2148 inline __complex__ _Float128
2149 __complex_atanh(__complex__ _Float128 __z)
2150 { return __builtin_catanhf128(__z); }
2153#if defined(__STDCPP_BFLOAT16_T__) && defined(_GLIBCXX_FLOAT_IS_IEEE_BINARY32)
2154 inline __complex__ decltype(0.0bf16)
2155 __complex_acos(__complex__ decltype(0.0bf16) __z)
2156 { return static_cast<__complex__ decltype(0.0bf16)>(__builtin_cacosf(__z)); }
2158 inline __complex__ decltype(0.0bf16)
2159 __complex_asin(__complex__ decltype(0.0bf16) __z)
2160 { return static_cast<__complex__ decltype(0.0bf16)>(__builtin_casinf(__z)); }
2162 inline __complex__ decltype(0.0bf16)
2163 __complex_atan(__complex__ decltype(0.0bf16) __z)
2164 { return static_cast<__complex__ decltype(0.0bf16)>(__builtin_catanf(__z)); }
2166 inline __complex__ decltype(0.0bf16)
2167 __complex_acosh(__complex__ decltype(0.0bf16) __z)
2168 { return static_cast<__complex__ decltype(0.0bf16)>(__builtin_cacoshf(__z)); }
2170 inline __complex__ decltype(0.0bf16)
2171 __complex_asinh(__complex__ decltype(0.0bf16) __z)
2172 { return static_cast<__complex__ decltype(0.0bf16)>(__builtin_casinhf(__z)); }
2174 inline __complex__ decltype(0.0bf16)
2175 __complex_atanh(__complex__ decltype(0.0bf16) __z)
2176 { return static_cast<__complex__ decltype(0.0bf16)>(__builtin_catanhf(__z)); }
2180#if _GLIBCXX_USE_C99_COMPLEX_TR1
2181 inline __complex__ float
2182 __complex_acos(__complex__ float __z)
2183 { return __builtin_cacosf(__z); }
2185 inline __complex__ double
2186 __complex_acos(__complex__ double __z)
2187 { return __builtin_cacos(__z); }
2189 inline __complex__ long double
2190 __complex_acos(const __complex__ long double& __z)
2191 { return __builtin_cacosl(__z); }
2193 template<typename _Tp>
2194 inline std::complex<_Tp>
2195 acos(const std::complex<_Tp>& __z)
2196 { return __complex_acos(__z.__rep()); }
2198 /// acos(__z) [8.1.2].
2199 // Effects: Behaves the same as C99 function cacos, defined
2200 // in subclause 7.3.5.1.
2201 template<typename _Tp>
2202 inline std::complex<_Tp>
2203 acos(const std::complex<_Tp>& __z)
2204 { return __complex_acos(__z); }
2207 template<typename _Tp>
2208 inline std::complex<_Tp>
2209 __complex_asin(const std::complex<_Tp>& __z)
2211 std::complex<_Tp> __t(-__z.imag(), __z.real());
2212 __t = std::asinh(__t);
2213 return std::complex<_Tp>(__t.imag(), -__t.real());
2216#if _GLIBCXX_USE_C99_COMPLEX_TR1
2217 inline __complex__ float
2218 __complex_asin(__complex__ float __z)
2219 { return __builtin_casinf(__z); }
2221 inline __complex__ double
2222 __complex_asin(__complex__ double __z)
2223 { return __builtin_casin(__z); }
2225 inline __complex__ long double
2226 __complex_asin(const __complex__ long double& __z)
2227 { return __builtin_casinl(__z); }
2229 template<typename _Tp>
2230 inline std::complex<_Tp>
2231 asin(const std::complex<_Tp>& __z)
2232 { return __complex_asin(__z.__rep()); }
2234 /// asin(__z) [8.1.3].
2235 // Effects: Behaves the same as C99 function casin, defined
2236 // in subclause 7.3.5.2.
2237 template<typename _Tp>
2238 inline std::complex<_Tp>
2239 asin(const std::complex<_Tp>& __z)
2240 { return __complex_asin(__z); }
2243 template<typename _Tp>
2245 __complex_atan(const std::complex<_Tp>& __z)
2247 const _Tp __r2 = __z.real() * __z.real();
2248 const _Tp __x = _Tp(1.0) - __r2 - __z.imag() * __z.imag();
2250 _Tp __num = __z.imag() + _Tp(1.0);
2251 _Tp __den = __z.imag() - _Tp(1.0);
2253 __num = __r2 + __num * __num;
2254 __den = __r2 + __den * __den;
2256 return std::complex<_Tp>(_Tp(0.5) * atan2(_Tp(2.0) * __z.real(), __x),
2257 _Tp(0.25) * log(__num / __den));
2260#if _GLIBCXX_USE_C99_COMPLEX_TR1
2261 inline __complex__ float
2262 __complex_atan(__complex__ float __z)
2263 { return __builtin_catanf(__z); }
2265 inline __complex__ double
2266 __complex_atan(__complex__ double __z)
2267 { return __builtin_catan(__z); }
2269 inline __complex__ long double
2270 __complex_atan(const __complex__ long double& __z)
2271 { return __builtin_catanl(__z); }
2273 template<typename _Tp>
2274 inline std::complex<_Tp>
2275 atan(const std::complex<_Tp>& __z)
2276 { return __complex_atan(__z.__rep()); }
2278 /// atan(__z) [8.1.4].
2279 // Effects: Behaves the same as C99 function catan, defined
2280 // in subclause 7.3.5.3.
2281 template<typename _Tp>
2282 inline std::complex<_Tp>
2283 atan(const std::complex<_Tp>& __z)
2284 { return __complex_atan(__z); }
2287 template<typename _Tp>
2289 __complex_acosh(const std::complex<_Tp>& __z)
2292 return _Tp(2.0) * std::log(std::sqrt(_Tp(0.5) * (__z + _Tp(1.0)))
2293 + std::sqrt(_Tp(0.5) * (__z - _Tp(1.0))));
2296#if _GLIBCXX_USE_C99_COMPLEX_TR1
2297 inline __complex__ float
2298 __complex_acosh(__complex__ float __z)
2299 { return __builtin_cacoshf(__z); }
2301 inline __complex__ double
2302 __complex_acosh(__complex__ double __z)
2303 { return __builtin_cacosh(__z); }
2305 inline __complex__ long double
2306 __complex_acosh(const __complex__ long double& __z)
2307 { return __builtin_cacoshl(__z); }
2309 template<typename _Tp>
2310 inline std::complex<_Tp>
2311 acosh(const std::complex<_Tp>& __z)
2312 { return __complex_acosh(__z.__rep()); }
2314 /// acosh(__z) [8.1.5].
2315 // Effects: Behaves the same as C99 function cacosh, defined
2316 // in subclause 7.3.6.1.
2317 template<typename _Tp>
2318 inline std::complex<_Tp>
2319 acosh(const std::complex<_Tp>& __z)
2320 { return __complex_acosh(__z); }
2323 template<typename _Tp>
2325 __complex_asinh(const std::complex<_Tp>& __z)
2327 std::complex<_Tp> __t((__z.real() - __z.imag())
2328 * (__z.real() + __z.imag()) + _Tp(1.0),
2329 _Tp(2.0) * __z.real() * __z.imag());
2330 __t = std::sqrt(__t);
2332 return std::log(__t + __z);
2335#if _GLIBCXX_USE_C99_COMPLEX_TR1
2336 inline __complex__ float
2337 __complex_asinh(__complex__ float __z)
2338 { return __builtin_casinhf(__z); }
2340 inline __complex__ double
2341 __complex_asinh(__complex__ double __z)
2342 { return __builtin_casinh(__z); }
2344 inline __complex__ long double
2345 __complex_asinh(const __complex__ long double& __z)
2346 { return __builtin_casinhl(__z); }
2348 template<typename _Tp>
2349 inline std::complex<_Tp>
2350 asinh(const std::complex<_Tp>& __z)
2351 { return __complex_asinh(__z.__rep()); }
2353 /// asinh(__z) [8.1.6].
2354 // Effects: Behaves the same as C99 function casin, defined
2355 // in subclause 7.3.6.2.
2356 template<typename _Tp>
2357 inline std::complex<_Tp>
2358 asinh(const std::complex<_Tp>& __z)
2359 { return __complex_asinh(__z); }
2362 template<typename _Tp>
2364 __complex_atanh(const std::complex<_Tp>& __z)
2366 const _Tp __i2 = __z.imag() * __z.imag();
2367 const _Tp __x = _Tp(1.0) - __i2 - __z.real() * __z.real();
2369 _Tp __num = _Tp(1.0) + __z.real();
2370 _Tp __den = _Tp(1.0) - __z.real();
2372 __num = __i2 + __num * __num;
2373 __den = __i2 + __den * __den;
2375 return std::complex<_Tp>(_Tp(0.25) * (log(__num) - log(__den)),
2376 _Tp(0.5) * atan2(_Tp(2.0) * __z.imag(), __x));
2379#if _GLIBCXX_USE_C99_COMPLEX_TR1
2380 inline __complex__ float
2381 __complex_atanh(__complex__ float __z)
2382 { return __builtin_catanhf(__z); }
2384 inline __complex__ double
2385 __complex_atanh(__complex__ double __z)
2386 { return __builtin_catanh(__z); }
2388 inline __complex__ long double
2389 __complex_atanh(const __complex__ long double& __z)
2390 { return __builtin_catanhl(__z); }
2392 template<typename _Tp>
2393 inline std::complex<_Tp>
2394 atanh(const std::complex<_Tp>& __z)
2395 { return __complex_atanh(__z.__rep()); }
2397 /// atanh(__z) [8.1.7].
2398 // Effects: Behaves the same as C99 function catanh, defined
2399 // in subclause 7.3.6.3.
2400 template<typename _Tp>
2401 inline std::complex<_Tp>
2402 atanh(const std::complex<_Tp>& __z)
2403 { return __complex_atanh(__z); }
2406 template<typename _Tp>
2408 /// fabs(__z) [8.1.8].
2409 // Effects: Behaves the same as C99 function cabs, defined
2410 // in subclause 7.3.8.1.
2411 fabs(const std::complex<_Tp>& __z)
2412 { return std::abs(__z); }
2414 /// Additional overloads [8.1.9].
2415 template<typename _Tp>
2416 inline typename __gnu_cxx::__promote<_Tp>::__type
2419 typedef typename __gnu_cxx::__promote<_Tp>::__type __type;
2420#if (_GLIBCXX11_USE_C99_MATH && !_GLIBCXX_USE_C99_FP_MACROS_DYNAMIC)
2421 return std::signbit(__x) ? __type(3.1415926535897932384626433832795029L)
2424 return std::arg(std::complex<__type>(__x));
2428 template<typename _Tp>
2429 _GLIBCXX_CONSTEXPR inline typename __gnu_cxx::__promote<_Tp>::__type
2433 template<typename _Tp>
2434 _GLIBCXX20_CONSTEXPR inline typename __gnu_cxx::__promote<_Tp>::__type
2437 typedef typename __gnu_cxx::__promote<_Tp>::__type __type;
2438 return __type(__x) * __type(__x);
2441 template<typename _Tp>
2442 _GLIBCXX_CONSTEXPR inline typename __gnu_cxx::__promote<_Tp>::__type
2446 template<typename _Tp, typename _Up>
2447 inline std::complex<typename __gnu_cxx::__promote_2<_Tp, _Up>::__type>
2448 pow(const std::complex<_Tp>& __x, const _Up& __y)
2450 typedef typename __gnu_cxx::__promote_2<_Tp, _Up>::__type __type;
2451 return std::pow(std::complex<__type>(__x), __type(__y));
2454 template<typename _Tp, typename _Up>
2455 inline std::complex<typename __gnu_cxx::__promote_2<_Tp, _Up>::__type>
2456 pow(const _Tp& __x, const std::complex<_Up>& __y)
2458 typedef typename __gnu_cxx::__promote_2<_Tp, _Up>::__type __type;
2459 return std::pow(__type(__x), std::complex<__type>(__y));
2462 template<typename _Tp, typename _Up>
2463 inline std::complex<typename __gnu_cxx::__promote_2<_Tp, _Up>::__type>
2464 pow(const std::complex<_Tp>& __x, const std::complex<_Up>& __y)
2466 typedef typename __gnu_cxx::__promote_2<_Tp, _Up>::__type __type;
2467 return std::pow(std::complex<__type>(__x),
2468 std::complex<__type>(__y));
2471 // Forward declarations.
2473 template<typename _Tp>
2474 std::complex<_Tp> proj(const std::complex<_Tp>&);
2476 // Generic implementation of std::proj, does not work for infinities.
2477 template<typename _Tp>
2478 inline std::complex<_Tp>
2479 __complex_proj(const std::complex<_Tp>& __z)
2482#if _GLIBCXX_USE_C99_COMPLEX
2483 inline complex<float>
2484 __complex_proj(const complex<float>& __z)
2485 { return __builtin_cprojf(__z.__rep()); }
2487 inline complex<double>
2488 __complex_proj(const complex<double>& __z)
2489 { return __builtin_cproj(__z.__rep()); }
2491 inline complex<long double>
2492 __complex_proj(const complex<long double>& __z)
2493 { return __builtin_cprojl(__z.__rep()); }
2495#if __cplusplus > 202002L
2496#if defined(__STDCPP_FLOAT16_T__) && defined(_GLIBCXX_FLOAT_IS_IEEE_BINARY32)
2497 inline __complex__ _Float16
2498 __complex_proj(__complex__ _Float16 __z)
2499 { return static_cast<__complex__ _Float16>(__builtin_cprojf(__z)); }
2502#if defined(__STDCPP_FLOAT32_T__) && defined(_GLIBCXX_FLOAT_IS_IEEE_BINARY32)
2503 inline __complex__ _Float32
2504 __complex_proj(__complex__ _Float32 __z)
2505 { return __builtin_cprojf(__z); }
2508#if defined(__STDCPP_FLOAT64_T__) && defined(_GLIBCXX_DOUBLE_IS_IEEE_BINARY64)
2509 inline __complex__ _Float64
2510 __complex_proj(__complex__ _Float64 __z)
2511 { return __builtin_cproj(__z); }
2514#if defined(__STDCPP_FLOAT128_T__) && defined(_GLIBCXX_LDOUBLE_IS_IEEE_BINARY128)
2515 inline __complex__ _Float128
2516 __complex_proj(__complex__ _Float128 __z)
2517 { return __builtin_cprojl(__z); }
2518#elif defined(__STDCPP_FLOAT128_T__) && defined(_GLIBCXX_HAVE_FLOAT128_MATH)
2519 inline __complex__ _Float128
2520 __complex_proj(__complex__ _Float128 __z)
2521 { return __builtin_cprojf128(__z); }
2524#if defined(__STDCPP_BFLOAT16_T__) && defined(_GLIBCXX_FLOAT_IS_IEEE_BINARY32)
2525 inline __complex__ decltype(0.0bf16)
2526 __complex_proj(__complex__ decltype(0.0bf16) __z)
2527 { return static_cast<__complex__ decltype(0.0bf16)>(__builtin_cprojf(__z)); }
2530 template<typename _Tp>
2531 requires requires { typename __complex_type<_Tp>::type; }
2533 __complex_proj(const complex<_Tp>& __z)
2534 { return __complex_proj(__z.__rep()); }
2537#elif defined _GLIBCXX_USE_C99_MATH_TR1
2538 inline complex<float>
2539 __complex_proj(const complex<float>& __z)
2541 if (__builtin_isinf(__z.real()) || __builtin_isinf(__z.imag()))
2542 return complex<float>(__builtin_inff(),
2543 __builtin_copysignf(0.0f, __z.imag()));
2547 inline complex<double>
2548 __complex_proj(const complex<double>& __z)
2550 if (__builtin_isinf(__z.real()) || __builtin_isinf(__z.imag()))
2551 return complex<double>(__builtin_inf(),
2552 __builtin_copysign(0.0, __z.imag()));
2556 inline complex<long double>
2557 __complex_proj(const complex<long double>& __z)
2559 if (__builtin_isinf(__z.real()) || __builtin_isinf(__z.imag()))
2560 return complex<long double>(__builtin_infl(),
2561 __builtin_copysignl(0.0l, __z.imag()));
2566 template<typename _Tp>
2567 inline std::complex<_Tp>
2568 proj(const std::complex<_Tp>& __z)
2569 { return __complex_proj(__z); }
2571 // Overload for scalars
2572 template<typename _Tp>
2573 inline std::complex<typename __gnu_cxx::__promote<_Tp>::__type>
2576 typedef typename __gnu_cxx::__promote<_Tp>::__type __type;
2577 return std::proj(std::complex<__type>(__x));
2580 template<typename _Tp>
2581 inline _GLIBCXX20_CONSTEXPR
2582 std::complex<typename __gnu_cxx::__promote<_Tp>::__type>
2585 typedef typename __gnu_cxx::__promote<_Tp>::__type __type;
2586 return std::complex<__type>(__x, -__type());
2589#if __cplusplus > 201103L
2591inline namespace literals {
2592inline namespace complex_literals {
2593#pragma GCC diagnostic push
2594#pragma GCC diagnostic ignored "-Wliteral-suffix"
2595#define __cpp_lib_complex_udls 201309L
2597 constexpr std::complex<float>
2598 operator""if(long double __num)
2599 { return std::complex<float>{0.0F, static_cast<float>(__num)}; }
2601 constexpr std::complex<float>
2602 operator""if(unsigned long long __num)
2603 { return std::complex<float>{0.0F, static_cast<float>(__num)}; }
2605 constexpr std::complex<double>
2606 operator""i(long double __num)
2607 { return std::complex<double>{0.0, static_cast<double>(__num)}; }
2609 constexpr std::complex<double>
2610 operator""i(unsigned long long __num)
2611 { return std::complex<double>{0.0, static_cast<double>(__num)}; }
2613 constexpr std::complex<long double>
2614 operator""il(long double __num)
2615 { return std::complex<long double>{0.0L, __num}; }
2617 constexpr std::complex<long double>
2618 operator""il(unsigned long long __num)
2619 { return std::complex<long double>{0.0L, static_cast<long double>(__num)}; }
2621#pragma GCC diagnostic pop
2622} // inline namespace complex_literals
2623} // inline namespace literals
2627_GLIBCXX_END_NAMESPACE_VERSION
2632#endif /* _GLIBCXX_COMPLEX */