libstdc++
complex
Go to the documentation of this file.
1// The template and inlines for the -*- C++ -*- complex number classes.
2
3// Copyright (C) 1997-2026 Free Software Foundation, Inc.
4//
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)
9// any later version.
10
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.
15
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.
19
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/>.
24
25/** @file include/complex
26 * This is a Standard C++ Library header.
27 */
28
29//
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>
34//
35
36#ifndef _GLIBCXX_COMPLEX
37#define _GLIBCXX_COMPLEX 1
39#ifdef _GLIBCXX_SYSHDR
40#pragma GCC system_header
41#endif
42
43#pragma GCC diagnostic push
44#pragma GCC diagnostic ignored "-Wc++11-extensions" // extern template
45
46#include <bits/c++config.h>
48#include <ext/type_traits.h>
49#include <cmath>
50#include <sstream>
51
52// Get rid of a macro possibly defined in <complex.h>
53#undef complex
54
55#ifdef _GLIBCXX_CLANG
56#pragma clang diagnostic push
57#pragma clang diagnostic ignored "-Wc99-extensions"
58#endif
59
60#define __glibcxx_want_constexpr_complex
61#define __glibcxx_want_complex_udls
62#define __glibcxx_want_tuple_like
63#include <bits/version.h>
64
65#if __glibcxx_tuple_like >= 202311 // >= C++26
66# include <bits/utility.h> // for tuple_element_t
67# include <bits/stl_pair.h> // for __is_tuple_like_v
68#endif
69
70namespace std _GLIBCXX_VISIBILITY(default)
71{
72_GLIBCXX_BEGIN_NAMESPACE_VERSION
73
74 /**
75 * @defgroup complex_numbers Complex Numbers
76 * @ingroup numerics
77 *
78 * Classes and functions for complex numbers.
79 * @{
80 */
81
82 // Forward declarations.
83 template<typename _Tp> class complex;
84 template<> class complex<float>;
85 template<> class complex<double>;
86 template<> class complex<long double>;
87
88 /// Return magnitude of @a z.
89 template<typename _Tp> _Tp abs(const complex<_Tp>&);
90 /// Return phase angle of @a z.
91 template<typename _Tp> _Tp arg(const complex<_Tp>&);
92 /// Return @a z magnitude squared.
93 template<typename _Tp> _Tp _GLIBCXX20_CONSTEXPR norm(const complex<_Tp>&);
94
95 /// Return complex conjugate of @a z.
96 template<typename _Tp>
97 _GLIBCXX20_CONSTEXPR complex<_Tp> conj(const complex<_Tp>&);
98 /// Return complex with magnitude @a rho and angle @a theta.
99 template<typename _Tp> complex<_Tp> polar(const _Tp&, const _Tp& = _Tp(0));
100
101 // Transcendentals:
102 /// Return complex cosine of @a z.
103 template<typename _Tp> complex<_Tp> cos(const complex<_Tp>&);
104 /// Return complex hyperbolic cosine of @a z.
105 template<typename _Tp> complex<_Tp> cosh(const complex<_Tp>&);
106 /// Return complex base e exponential of @a z.
107 template<typename _Tp> complex<_Tp> exp(const complex<_Tp>&);
108 /// Return complex natural logarithm of @a z.
109 template<typename _Tp> complex<_Tp> log(const complex<_Tp>&);
110 /// Return complex base 10 logarithm of @a z.
111 template<typename _Tp> complex<_Tp> log10(const complex<_Tp>&);
112 /// Return @a x to the @a y'th power.
113 template<typename _Tp> complex<_Tp> pow(const complex<_Tp>&, int);
114 /// Return @a x to the @a y'th power.
115 template<typename _Tp> complex<_Tp> pow(const complex<_Tp>&, const _Tp&);
116 /// Return @a x to the @a y'th power.
117 template<typename _Tp> complex<_Tp> pow(const complex<_Tp>&,
118 const complex<_Tp>&);
119 /// Return @a x to the @a y'th power.
120 template<typename _Tp> complex<_Tp> pow(const _Tp&, const complex<_Tp>&);
121 /// Return complex sine of @a z.
122 template<typename _Tp> complex<_Tp> sin(const complex<_Tp>&);
123 /// Return complex hyperbolic sine of @a z.
124 template<typename _Tp> complex<_Tp> sinh(const complex<_Tp>&);
125 /// Return complex square root of @a z.
126 template<typename _Tp> complex<_Tp> sqrt(const complex<_Tp>&);
127 /// Return complex tangent of @a z.
128 template<typename _Tp> complex<_Tp> tan(const complex<_Tp>&);
129 /// Return complex hyperbolic tangent of @a z.
130 template<typename _Tp> complex<_Tp> tanh(const complex<_Tp>&);
131
132#if __glibcxx_tuple_like >= 202311L // >= C++26
133 template<typename _Tp>
134 struct tuple_size<complex<_Tp>>
135 : public integral_constant<size_t, 2> { };
136 template<typename _Tp>
137 struct tuple_element<0, complex<_Tp>>
138 { using type = _Tp; };
139 template<typename _Tp>
140 struct tuple_element<1, complex<_Tp>>
141 { using type = _Tp; };
142 template<typename _Tp>
143 inline constexpr bool __is_tuple_like_v<complex<_Tp>> = true;
144#endif // __glibcxx_tuple_like >= 202311
145
146 // 26.2.2 Primary template class complex
147 /**
148 * Template to represent complex numbers.
149 *
150 * Specializations for float, double, and long double are part of the
151 * library. Results with any other type are not guaranteed.
152 *
153 * @param Tp Type of real and imaginary values.
154 */
155 template<typename _Tp>
157 {
158 public:
159 /// Value typedef.
160 typedef _Tp value_type;
161
162 /// Default constructor. First parameter is x, second parameter is y.
163 /// Unspecified parameters default to 0.
164 _GLIBCXX_CONSTEXPR complex(const _Tp& __r = _Tp(), const _Tp& __i = _Tp())
165 : _M_real(__r), _M_imag(__i) { }
166
167 // Let the compiler synthesize the copy constructor
168#if __cplusplus >= 201103L
169 constexpr complex(const complex&) = default;
170#endif
171
172 /// Converting constructor.
173 template<typename _Up>
174#if __cplusplus > 202002L
175 explicit(!requires(_Up __u) { _Tp{__u}; })
176#endif
177 _GLIBCXX_CONSTEXPR complex(const complex<_Up>& __z)
178 : _M_real(_Tp(__z.real())), _M_imag(_Tp(__z.imag())) { }
179
180#if __cplusplus >= 201103L
181 // _GLIBCXX_RESOLVE_LIB_DEFECTS
182 // DR 387. std::complex over-encapsulated.
183 _GLIBCXX_ABI_TAG_CXX11
184 constexpr _Tp
185 real() const { return _M_real; }
186
187 _GLIBCXX_ABI_TAG_CXX11
188 constexpr _Tp
189 imag() const { return _M_imag; }
190#else
191 /// Return real part of complex number.
192 _Tp&
193 real() { return _M_real; }
194
195 /// Return real part of complex number.
196 const _Tp&
197 real() const { return _M_real; }
198
199 /// Return imaginary part of complex number.
200 _Tp&
201 imag() { return _M_imag; }
202
203 /// Return imaginary part of complex number.
204 const _Tp&
205 imag() const { return _M_imag; }
206#endif
207
208 // _GLIBCXX_RESOLVE_LIB_DEFECTS
209 // DR 387. std::complex over-encapsulated.
210 _GLIBCXX20_CONSTEXPR void
211 real(_Tp __val) { _M_real = __val; }
212
213 _GLIBCXX20_CONSTEXPR void
214 imag(_Tp __val) { _M_imag = __val; }
215
216 /// Assign a scalar to this complex number.
217 _GLIBCXX20_CONSTEXPR complex<_Tp>& operator=(const _Tp&);
218
219 /// Add a scalar to this complex number.
220 // 26.2.5/1
221 _GLIBCXX20_CONSTEXPR complex<_Tp>&
222 operator+=(const _Tp& __t)
223 {
224 _M_real += __t;
225 return *this;
226 }
227
228 /// Subtract a scalar from this complex number.
229 // 26.2.5/3
230 _GLIBCXX20_CONSTEXPR complex<_Tp>&
231 operator-=(const _Tp& __t)
232 {
233 _M_real -= __t;
234 return *this;
235 }
236
237 /// Multiply this complex number by a scalar.
238 _GLIBCXX20_CONSTEXPR complex<_Tp>& operator*=(const _Tp&);
239 /// Divide this complex number by a scalar.
240 _GLIBCXX20_CONSTEXPR complex<_Tp>& operator/=(const _Tp&);
241
242 // Let the compiler synthesize the copy assignment operator
243#if __cplusplus >= 201103L
244 _GLIBCXX20_CONSTEXPR complex& operator=(const complex&) = default;
245#endif
246
247 /// Assign another complex number to this one.
248 template<typename _Up>
249 _GLIBCXX20_CONSTEXPR complex<_Tp>& operator=(const complex<_Up>&);
250 /// Add another complex number to this one.
251 template<typename _Up>
252 _GLIBCXX20_CONSTEXPR complex<_Tp>& operator+=(const complex<_Up>&);
253 /// Subtract another complex number from this one.
254 template<typename _Up>
255 _GLIBCXX20_CONSTEXPR complex<_Tp>& operator-=(const complex<_Up>&);
256 /// Multiply this complex number by another.
257 template<typename _Up>
258 _GLIBCXX20_CONSTEXPR complex<_Tp>& operator*=(const complex<_Up>&);
259 /// Divide this complex number by another.
260 template<typename _Up>
261 _GLIBCXX20_CONSTEXPR complex<_Tp>& operator/=(const complex<_Up>&);
262
263 _GLIBCXX_CONSTEXPR complex __rep() const
264 { return *this; }
265
266#if __glibcxx_tuple_like >= 202311L // >= C++26
267 template<typename _Cp>
268 [[__gnu__::__always_inline__]]
269 constexpr auto&
270 __get_part(this _Cp& __z, size_t __i) noexcept
271 {
272 return __i == 0 ? __z._M_real : __z._M_imag;
273 }
274#endif
275
276 private:
277 _Tp _M_real;
278 _Tp _M_imag;
279 };
280
281 template<typename _Tp>
282 _GLIBCXX20_CONSTEXPR complex<_Tp>&
283 complex<_Tp>::operator=(const _Tp& __t)
284 {
285 _M_real = __t;
286 _M_imag = _Tp();
287 return *this;
288 }
289
290 // 26.2.5/5
291 template<typename _Tp>
292 _GLIBCXX20_CONSTEXPR complex<_Tp>&
293 complex<_Tp>::operator*=(const _Tp& __t)
294 {
295 _M_real *= __t;
296 _M_imag *= __t;
297 return *this;
298 }
299
300 // 26.2.5/7
301 template<typename _Tp>
302 _GLIBCXX20_CONSTEXPR complex<_Tp>&
303 complex<_Tp>::operator/=(const _Tp& __t)
304 {
305 _M_real /= __t;
306 _M_imag /= __t;
307 return *this;
308 }
309
310 template<typename _Tp>
311 template<typename _Up>
312 _GLIBCXX20_CONSTEXPR complex<_Tp>&
314 {
315 _M_real = __z.real();
316 _M_imag = __z.imag();
317 return *this;
318 }
319
320 // 26.2.5/9
321 template<typename _Tp>
322 template<typename _Up>
323 _GLIBCXX20_CONSTEXPR complex<_Tp>&
325 {
326 _M_real += __z.real();
327 _M_imag += __z.imag();
328 return *this;
329 }
330
331 // 26.2.5/11
332 template<typename _Tp>
333 template<typename _Up>
334 _GLIBCXX20_CONSTEXPR complex<_Tp>&
336 {
337 _M_real -= __z.real();
338 _M_imag -= __z.imag();
339 return *this;
340 }
341
342 // 26.2.5/13
343 // XXX: This is a grammar school implementation.
344 template<typename _Tp>
345 template<typename _Up>
346 _GLIBCXX20_CONSTEXPR complex<_Tp>&
348 {
349 const _Tp __r = _M_real * __z.real() - _M_imag * __z.imag();
350 _M_imag = _M_real * __z.imag() + _M_imag * __z.real();
351 _M_real = __r;
352 return *this;
353 }
354
355 // 26.2.5/15
356 // XXX: This is a grammar school implementation.
357 template<typename _Tp>
358 template<typename _Up>
359 _GLIBCXX20_CONSTEXPR complex<_Tp>&
361 {
362 const _Tp __r = _M_real * __z.real() + _M_imag * __z.imag();
363 const _Tp __n = std::norm(__z);
364 _M_imag = (_M_imag * __z.real() - _M_real * __z.imag()) / __n;
365 _M_real = __r / __n;
366 return *this;
367 }
368
369 // Operators:
370 ///@{
371 /// Return new complex value @a x plus @a y.
372 template<typename _Tp>
373 inline _GLIBCXX20_CONSTEXPR complex<_Tp>
374 operator+(const complex<_Tp>& __x, const complex<_Tp>& __y)
375 {
376 complex<_Tp> __r = __x;
377 __r += __y;
378 return __r;
379 }
380
381 template<typename _Tp>
382 inline _GLIBCXX20_CONSTEXPR complex<_Tp>
383 operator+(const complex<_Tp>& __x, const _Tp& __y)
384 {
385 complex<_Tp> __r = __x;
386 __r += __y;
387 return __r;
388 }
389
390 template<typename _Tp>
391 inline _GLIBCXX20_CONSTEXPR complex<_Tp>
392 operator+(const _Tp& __x, const complex<_Tp>& __y)
393 {
394 complex<_Tp> __r = __y;
395 __r += __x;
396 return __r;
397 }
398 ///@}
399
400 ///@{
401 /// Return new complex value @a x minus @a y.
402 template<typename _Tp>
403 inline _GLIBCXX20_CONSTEXPR complex<_Tp>
404 operator-(const complex<_Tp>& __x, const complex<_Tp>& __y)
405 {
406 complex<_Tp> __r = __x;
407 __r -= __y;
408 return __r;
409 }
410
411 template<typename _Tp>
412 inline _GLIBCXX20_CONSTEXPR complex<_Tp>
413 operator-(const complex<_Tp>& __x, const _Tp& __y)
414 {
415 complex<_Tp> __r = __x;
416 __r -= __y;
417 return __r;
418 }
419
420 template<typename _Tp>
421 inline _GLIBCXX20_CONSTEXPR complex<_Tp>
422 operator-(const _Tp& __x, const complex<_Tp>& __y)
423 {
424 complex<_Tp> __r = -__y;
425 __r += __x;
426 return __r;
427 }
428 ///@}
429
430 ///@{
431 /// Return new complex value @a x times @a y.
432 template<typename _Tp>
433 inline _GLIBCXX20_CONSTEXPR complex<_Tp>
434 operator*(const complex<_Tp>& __x, const complex<_Tp>& __y)
435 {
436 complex<_Tp> __r = __x;
437 __r *= __y;
438 return __r;
439 }
440
441 template<typename _Tp>
442 inline _GLIBCXX20_CONSTEXPR complex<_Tp>
443 operator*(const complex<_Tp>& __x, const _Tp& __y)
444 {
445 complex<_Tp> __r = __x;
446 __r *= __y;
447 return __r;
448 }
449
450 template<typename _Tp>
451 inline _GLIBCXX20_CONSTEXPR complex<_Tp>
452 operator*(const _Tp& __x, const complex<_Tp>& __y)
453 {
454 complex<_Tp> __r = __y;
455 __r *= __x;
456 return __r;
457 }
458 ///@}
459
460 ///@{
461 /// Return new complex value @a x divided by @a y.
462 template<typename _Tp>
463 inline _GLIBCXX20_CONSTEXPR complex<_Tp>
464 operator/(const complex<_Tp>& __x, const complex<_Tp>& __y)
465 {
466 complex<_Tp> __r = __x;
467 __r /= __y;
468 return __r;
469 }
470
471 template<typename _Tp>
472 inline _GLIBCXX20_CONSTEXPR complex<_Tp>
473 operator/(const complex<_Tp>& __x, const _Tp& __y)
474 {
475 complex<_Tp> __r = __x;
476 __r /= __y;
477 return __r;
478 }
479
480 template<typename _Tp>
481 inline _GLIBCXX20_CONSTEXPR complex<_Tp>
482 operator/(const _Tp& __x, const complex<_Tp>& __y)
483 {
484 complex<_Tp> __r = __x;
485 __r /= __y;
486 return __r;
487 }
488 ///@}
489
490 /// Return @a x.
491 template<typename _Tp>
492 inline _GLIBCXX20_CONSTEXPR complex<_Tp>
494 { return __x; }
495
496 /// Return complex negation of @a x.
497 template<typename _Tp>
498 inline _GLIBCXX20_CONSTEXPR complex<_Tp>
500 { return complex<_Tp>(-__x.real(), -__x.imag()); }
501
502 ///@{
503 /// Return true if @a x is equal to @a y.
504 template<typename _Tp>
505 inline _GLIBCXX_CONSTEXPR bool
506 operator==(const complex<_Tp>& __x, const complex<_Tp>& __y)
507 { return __x.real() == __y.real() && __x.imag() == __y.imag(); }
508
509 template<typename _Tp>
510 inline _GLIBCXX_CONSTEXPR bool
511 operator==(const complex<_Tp>& __x, const _Tp& __y)
512 { return __x.real() == __y && __x.imag() == _Tp(); }
513
514#if !(__cpp_impl_three_way_comparison >= 201907L)
515 template<typename _Tp>
516 inline _GLIBCXX_CONSTEXPR bool
517 operator==(const _Tp& __x, const complex<_Tp>& __y)
518 { return __x == __y.real() && _Tp() == __y.imag(); }
519 ///@}
520
521 ///@{
522 /// Return false if @a x is equal to @a y.
523 template<typename _Tp>
524 inline _GLIBCXX_CONSTEXPR bool
525 operator!=(const complex<_Tp>& __x, const complex<_Tp>& __y)
526 { return __x.real() != __y.real() || __x.imag() != __y.imag(); }
527
528 template<typename _Tp>
529 inline _GLIBCXX_CONSTEXPR bool
530 operator!=(const complex<_Tp>& __x, const _Tp& __y)
531 { return __x.real() != __y || __x.imag() != _Tp(); }
532
533 template<typename _Tp>
534 inline _GLIBCXX_CONSTEXPR bool
535 operator!=(const _Tp& __x, const complex<_Tp>& __y)
536 { return __x != __y.real() || _Tp() != __y.imag(); }
537#endif
538 ///@}
539
540 /// Extraction operator for complex values.
541 template<typename _Tp, typename _CharT, class _Traits>
544 {
545 bool __fail = true;
546 _CharT __ch;
547 if (__is >> __ch)
548 {
549 if (_Traits::eq(__ch, __is.widen('(')))
550 {
551 _Tp __u;
552 if (__is >> __u >> __ch)
553 {
554 const _CharT __rparen = __is.widen(')');
555 if (_Traits::eq(__ch, __rparen))
556 {
557 __x = __u;
558 __fail = false;
559 }
560 else if (_Traits::eq(__ch, __is.widen(',')))
561 {
562 _Tp __v;
563 if (__is >> __v >> __ch)
564 {
565 if (_Traits::eq(__ch, __rparen))
566 {
567 __x = complex<_Tp>(__u, __v);
568 __fail = false;
569 }
570 else
571 __is.putback(__ch);
572 }
573 }
574 else
575 __is.putback(__ch);
576 }
577 }
578 else
579 {
580 __is.putback(__ch);
581 _Tp __u;
582 if (__is >> __u)
583 {
584 __x = __u;
585 __fail = false;
586 }
587 }
588 }
589 if (__fail)
591 return __is;
592 }
593
594 /// Insertion operator for complex values.
595 template<typename _Tp, typename _CharT, class _Traits>
596 basic_ostream<_CharT, _Traits>&
597 operator<<(basic_ostream<_CharT, _Traits>& __os, const complex<_Tp>& __x)
598 {
599 basic_ostringstream<_CharT, _Traits> __s;
600 __s.flags(__os.flags());
601 __s.imbue(__os.getloc());
602 __s.precision(__os.precision());
603 __s << '(' << __x.real() << ',' << __x.imag() << ')';
604 return __os << __s.str();
605 }
606
607 // Values
608#if __cplusplus >= 201103L
609 template<typename _Tp>
610 constexpr _Tp
611 real(const complex<_Tp>& __z)
612 { return __z.real(); }
613
614 template<typename _Tp>
615 constexpr _Tp
616 imag(const complex<_Tp>& __z)
617 { return __z.imag(); }
618#else
619 template<typename _Tp>
620 inline _Tp&
621 real(complex<_Tp>& __z)
622 { return __z.real(); }
623
624 template<typename _Tp>
625 inline const _Tp&
626 real(const complex<_Tp>& __z)
627 { return __z.real(); }
628
629 template<typename _Tp>
630 inline _Tp&
631 imag(complex<_Tp>& __z)
632 { return __z.imag(); }
633
634 template<typename _Tp>
635 inline const _Tp&
636 imag(const complex<_Tp>& __z)
637 { return __z.imag(); }
638#endif
639
640#if __glibcxx_tuple_like >= 202311L // >= C++26
641 // Forward declared in bits/stl_pair.h
642 template<size_t _Int, typename _Tp>
643 [[nodiscard,__gnu__::__always_inline__]]
644 constexpr _Tp&
645 ::std::get(complex<_Tp>& __z) noexcept
646 {
647 static_assert(_Int < 2);
648 return __z.__get_part(_Int);
649 }
650
651 template<size_t _Int, typename _Tp>
652 [[nodiscard,__gnu__::__always_inline__]]
653 constexpr _Tp&&
654 ::std::get(complex<_Tp>&& __z) noexcept
655 {
656 static_assert(_Int < 2);
657 return std::move(__z.__get_part(_Int));
658 }
659
660 template<size_t _Int, typename _Tp>
661 [[nodiscard,__gnu__::__always_inline__]]
662 constexpr const _Tp&
663 ::std::get(const complex<_Tp>& __z) noexcept
664 {
665 static_assert(_Int < 2);
666 return __z.__get_part(_Int);
667 }
668
669 template<size_t _Int, typename _Tp>
670 [[nodiscard,__gnu__::__always_inline__]]
671 constexpr const _Tp&&
672 ::std::get(const complex<_Tp>&& __z) noexcept
673 {
674 static_assert(_Int < 2);
675 return std::move(__z.__get_part(_Int));
676 }
677#endif // __glibcxx_tuple_like >= 202311
678
679#if _GLIBCXX_USE_C99_COMPLEX
680#if defined(__STDCPP_FLOAT16_T__) && defined(_GLIBCXX_FLOAT_IS_IEEE_BINARY32)
681 inline _Float16
682 __complex_abs(__complex__ _Float16 __z)
683 { return _Float16(__builtin_cabsf(__z)); }
684
685 inline _Float16
686 __complex_arg(__complex__ _Float16 __z)
687 { return _Float16(__builtin_cargf(__z)); }
688
689 inline __complex__ _Float16
690 __complex_cos(__complex__ _Float16 __z)
691 { return static_cast<__complex__ _Float16>(__builtin_ccosf(__z)); }
692
693 inline __complex__ _Float16
694 __complex_cosh(__complex__ _Float16 __z)
695 { return static_cast<__complex__ _Float16>(__builtin_ccoshf(__z)); }
696
697 inline __complex__ _Float16
698 __complex_exp(__complex__ _Float16 __z)
699 { return static_cast<__complex__ _Float16>(__builtin_cexpf(__z)); }
700
701 inline __complex__ _Float16
702 __complex_log(__complex__ _Float16 __z)
703 { return static_cast<__complex__ _Float16>(__builtin_clogf(__z)); }
704
705 inline __complex__ _Float16
706 __complex_sin(__complex__ _Float16 __z)
707 { return static_cast<__complex__ _Float16>(__builtin_csinf(__z)); }
708
709 inline __complex__ _Float16
710 __complex_sinh(__complex__ _Float16 __z)
711 { return static_cast<__complex__ _Float16>(__builtin_csinhf(__z)); }
712
713 inline __complex__ _Float16
714 __complex_sqrt(__complex__ _Float16 __z)
715 { return static_cast<__complex__ _Float16>(__builtin_csqrtf(__z)); }
716
717 inline __complex__ _Float16
718 __complex_tan(__complex__ _Float16 __z)
719 { return static_cast<__complex__ _Float16>(__builtin_ctanf(__z)); }
720
721 inline __complex__ _Float16
722 __complex_tanh(__complex__ _Float16 __z)
723 { return static_cast<__complex__ _Float16>(__builtin_ctanhf(__z)); }
724
725 inline __complex__ _Float16
726 __complex_pow(__complex__ _Float16 __x, __complex__ _Float16 __y)
727 { return static_cast<__complex__ _Float16>(__builtin_cpowf(__x, __y)); }
728#endif
729
730#if defined(__STDCPP_FLOAT32_T__) && defined(_GLIBCXX_FLOAT_IS_IEEE_BINARY32)
731 inline _Float32
732 __complex_abs(__complex__ _Float32 __z) { return __builtin_cabsf(__z); }
733
734 inline _Float32
735 __complex_arg(__complex__ _Float32 __z) { return __builtin_cargf(__z); }
736
737 inline __complex__ _Float32
738 __complex_cos(__complex__ _Float32 __z) { return __builtin_ccosf(__z); }
739
740 inline __complex__ _Float32
741 __complex_cosh(__complex__ _Float32 __z) { return __builtin_ccoshf(__z); }
742
743 inline __complex__ _Float32
744 __complex_exp(__complex__ _Float32 __z) { return __builtin_cexpf(__z); }
745
746 inline __complex__ _Float32
747 __complex_log(__complex__ _Float32 __z) { return __builtin_clogf(__z); }
748
749 inline __complex__ _Float32
750 __complex_sin(__complex__ _Float32 __z) { return __builtin_csinf(__z); }
751
752 inline __complex__ _Float32
753 __complex_sinh(__complex__ _Float32 __z) { return __builtin_csinhf(__z); }
754
755 inline __complex__ _Float32
756 __complex_sqrt(__complex__ _Float32 __z) { return __builtin_csqrtf(__z); }
757
758 inline __complex__ _Float32
759 __complex_tan(__complex__ _Float32 __z) { return __builtin_ctanf(__z); }
760
761 inline __complex__ _Float32
762 __complex_tanh(__complex__ _Float32 __z) { return __builtin_ctanhf(__z); }
763
764 inline __complex__ _Float32
765 __complex_pow(__complex__ _Float32 __x, __complex__ _Float32 __y)
766 { return __builtin_cpowf(__x, __y); }
767#endif
768
769#if defined(__STDCPP_FLOAT64_T__) && defined(_GLIBCXX_DOUBLE_IS_IEEE_BINARY64)
770 inline _Float64
771 __complex_abs(__complex__ _Float64 __z) { return __builtin_cabs(__z); }
772
773 inline _Float64
774 __complex_arg(__complex__ _Float64 __z) { return __builtin_carg(__z); }
775
776 inline __complex__ _Float64
777 __complex_cos(__complex__ _Float64 __z) { return __builtin_ccos(__z); }
778
779 inline __complex__ _Float64
780 __complex_cosh(__complex__ _Float64 __z) { return __builtin_ccosh(__z); }
781
782 inline __complex__ _Float64
783 __complex_exp(__complex__ _Float64 __z) { return __builtin_cexp(__z); }
784
785 inline __complex__ _Float64
786 __complex_log(__complex__ _Float64 __z) { return __builtin_clog(__z); }
787
788 inline __complex__ _Float64
789 __complex_sin(__complex__ _Float64 __z) { return __builtin_csin(__z); }
790
791 inline __complex__ _Float64
792 __complex_sinh(__complex__ _Float64 __z) { return __builtin_csinh(__z); }
793
794 inline __complex__ _Float64
795 __complex_sqrt(__complex__ _Float64 __z) { return __builtin_csqrt(__z); }
796
797 inline __complex__ _Float64
798 __complex_tan(__complex__ _Float64 __z) { return __builtin_ctan(__z); }
799
800 inline __complex__ _Float64
801 __complex_tanh(__complex__ _Float64 __z) { return __builtin_ctanh(__z); }
802
803 inline __complex__ _Float64
804 __complex_pow(__complex__ _Float64 __x, __complex__ _Float64 __y)
805 { return __builtin_cpow(__x, __y); }
806#endif
807
808#if defined(__STDCPP_FLOAT128_T__) && defined(_GLIBCXX_LDOUBLE_IS_IEEE_BINARY128)
809 inline _Float128
810 __complex_abs(__complex__ _Float128 __z) { return __builtin_cabsl(__z); }
811
812 inline _Float128
813 __complex_arg(__complex__ _Float128 __z) { return __builtin_cargl(__z); }
814
815 inline __complex__ _Float128
816 __complex_cos(__complex__ _Float128 __z) { return __builtin_ccosl(__z); }
817
818 inline __complex__ _Float128
819 __complex_cosh(__complex__ _Float128 __z) { return __builtin_ccoshl(__z); }
820
821 inline __complex__ _Float128
822 __complex_exp(__complex__ _Float128 __z) { return __builtin_cexpl(__z); }
823
824 inline __complex__ _Float128
825 __complex_log(__complex__ _Float128 __z) { return __builtin_clogl(__z); }
826
827 inline __complex__ _Float128
828 __complex_sin(__complex__ _Float128 __z) { return __builtin_csinl(__z); }
829
830 inline __complex__ _Float128
831 __complex_sinh(__complex__ _Float128 __z) { return __builtin_csinhl(__z); }
832
833 inline __complex__ _Float128
834 __complex_sqrt(__complex__ _Float128 __z) { return __builtin_csqrtl(__z); }
835
836 inline __complex__ _Float128
837 __complex_tan(__complex__ _Float128 __z) { return __builtin_ctanl(__z); }
838
839 inline __complex__ _Float128
840 __complex_tanh(__complex__ _Float128 __z) { return __builtin_ctanhl(__z); }
841
842 inline __complex__ _Float128
843 __complex_pow(__complex__ _Float128 __x, __complex__ _Float128 __y)
844 { return __builtin_cpowl(__x, __y); }
845#elif defined(__STDCPP_FLOAT128_T__) && defined(_GLIBCXX_HAVE_FLOAT128_MATH)
846 inline _Float128
847 __complex_abs(__complex__ _Float128 __z) { return __builtin_cabsf128(__z); }
848
849 inline _Float128
850 __complex_arg(__complex__ _Float128 __z) { return __builtin_cargf128(__z); }
851
852 inline __complex__ _Float128
853 __complex_cos(__complex__ _Float128 __z) { return __builtin_ccosf128(__z); }
854
855 inline __complex__ _Float128
856 __complex_cosh(__complex__ _Float128 __z) { return __builtin_ccoshf128(__z); }
857
858 inline __complex__ _Float128
859 __complex_exp(__complex__ _Float128 __z) { return __builtin_cexpf128(__z); }
860
861 inline __complex__ _Float128
862 __complex_log(__complex__ _Float128 __z) { return __builtin_clogf128(__z); }
863
864 inline __complex__ _Float128
865 __complex_sin(__complex__ _Float128 __z) { return __builtin_csinf128(__z); }
866
867 inline __complex__ _Float128
868 __complex_sinh(__complex__ _Float128 __z) { return __builtin_csinhf128(__z); }
869
870 inline __complex__ _Float128
871 __complex_sqrt(__complex__ _Float128 __z) { return __builtin_csqrtf128(__z); }
872
873 inline __complex__ _Float128
874 __complex_tan(__complex__ _Float128 __z) { return __builtin_ctanf128(__z); }
875
876 inline __complex__ _Float128
877 __complex_tanh(__complex__ _Float128 __z) { return __builtin_ctanhf128(__z); }
878
879 inline __complex__ _Float128
880 __complex_pow(__complex__ _Float128 __x, __complex__ _Float128 __y)
881 { return __builtin_cpowf128(__x, __y); }
882#endif
883
884#if defined(__STDCPP_BFLOAT16_T__) && defined(_GLIBCXX_FLOAT_IS_IEEE_BINARY32)
885 inline __gnu_cxx::__bfloat16_t
886 __complex_abs(__complex__ decltype(0.0bf16) __z)
887 { return __gnu_cxx::__bfloat16_t(__builtin_cabsf(__z)); }
888
889 inline __gnu_cxx::__bfloat16_t
890 __complex_arg(__complex__ decltype(0.0bf16) __z)
891 { return __gnu_cxx::__bfloat16_t(__builtin_cargf(__z)); }
892
893 inline __complex__ decltype(0.0bf16)
894 __complex_cos(__complex__ decltype(0.0bf16) __z)
895 { return static_cast<__complex__ decltype(0.0bf16)>(__builtin_ccosf(__z)); }
896
897 inline __complex__ decltype(0.0bf16)
898 __complex_cosh(__complex__ decltype(0.0bf16) __z)
899 { return static_cast<__complex__ decltype(0.0bf16)>(__builtin_ccoshf(__z)); }
900
901 inline __complex__ decltype(0.0bf16)
902 __complex_exp(__complex__ decltype(0.0bf16) __z)
903 { return static_cast<__complex__ decltype(0.0bf16)>(__builtin_cexpf(__z)); }
904
905 inline __complex__ decltype(0.0bf16)
906 __complex_log(__complex__ decltype(0.0bf16) __z)
907 { return static_cast<__complex__ decltype(0.0bf16)>(__builtin_clogf(__z)); }
908
909 inline __complex__ decltype(0.0bf16)
910 __complex_sin(__complex__ decltype(0.0bf16) __z)
911 { return static_cast<__complex__ decltype(0.0bf16)>(__builtin_csinf(__z)); }
912
913 inline __complex__ decltype(0.0bf16)
914 __complex_sinh(__complex__ decltype(0.0bf16) __z)
915 { return static_cast<__complex__ decltype(0.0bf16)>(__builtin_csinhf(__z)); }
916
917 inline __complex__ decltype(0.0bf16)
918 __complex_sqrt(__complex__ decltype(0.0bf16) __z)
919 { return static_cast<__complex__ decltype(0.0bf16)>(__builtin_csqrtf(__z)); }
920
921 inline __complex__ decltype(0.0bf16)
922 __complex_tan(__complex__ decltype(0.0bf16) __z)
923 { return static_cast<__complex__ decltype(0.0bf16)>(__builtin_ctanf(__z)); }
924
925 inline __complex__ decltype(0.0bf16)
926 __complex_tanh(__complex__ decltype(0.0bf16) __z)
927 { return static_cast<__complex__ decltype(0.0bf16)>(__builtin_ctanhf(__z)); }
928
929 inline __complex__ decltype(0.0bf16)
930 __complex_pow(__complex__ decltype(0.0bf16) __x,
931 __complex__ decltype(0.0bf16) __y)
932 { return static_cast<__complex__ decltype(0.0bf16)>(__builtin_cpowf(__x,
933 __y)); }
934#endif
935#endif
936
937 // 26.2.7/3 abs(__z): Returns the magnitude of __z.
938 template<typename _Tp>
939 inline _Tp
940 __complex_abs(const complex<_Tp>& __z)
941 {
942 _Tp __x = __z.real();
943 _Tp __y = __z.imag();
944 const _Tp __s = std::max(abs(__x), abs(__y));
945 if (__s == _Tp()) // well ...
946 return __s;
947 __x /= __s;
948 __y /= __s;
949 return __s * sqrt(__x * __x + __y * __y);
950 }
951
952#if _GLIBCXX_USE_C99_COMPLEX
953 inline float
954 __complex_abs(__complex__ float __z) { return __builtin_cabsf(__z); }
955
956 inline double
957 __complex_abs(__complex__ double __z) { return __builtin_cabs(__z); }
958
959 inline long double
960 __complex_abs(const __complex__ long double& __z)
961 { return __builtin_cabsl(__z); }
962
963 template<typename _Tp>
964 inline _Tp
965 abs(const complex<_Tp>& __z) { return __complex_abs(__z.__rep()); }
966#else
967 template<typename _Tp>
968 inline _Tp
969 abs(const complex<_Tp>& __z) { return __complex_abs(__z); }
970#endif
971
972
973 // C++11 26.4.7 [complex.value.ops]/4: arg(z): Returns the phase angle of z.
974 template<typename _Tp>
975 inline _Tp
976 __complex_arg(const complex<_Tp>& __z)
977 { return atan2(__z.imag(), __z.real()); }
978
979#if _GLIBCXX_USE_C99_COMPLEX
980 inline float
981 __complex_arg(__complex__ float __z) { return __builtin_cargf(__z); }
982
983 inline double
984 __complex_arg(__complex__ double __z) { return __builtin_carg(__z); }
985
986 inline long double
987 __complex_arg(const __complex__ long double& __z)
988 { return __builtin_cargl(__z); }
989
990 template<typename _Tp>
991 inline _Tp
992 arg(const complex<_Tp>& __z) { return __complex_arg(__z.__rep()); }
993#else
994 template<typename _Tp>
995 inline _Tp
996 arg(const complex<_Tp>& __z) { return __complex_arg(__z); }
997#endif
998
999 // 26.2.7/5: norm(__z) returns the squared magnitude of __z.
1000 // As defined, norm() is -not- a norm is the common mathematical
1001 // sense used in numerics. The helper class _Norm_helper<> tries to
1002 // distinguish between builtin floating point and the rest, so as
1003 // to deliver an answer as close as possible to the real value.
1004 template<bool>
1005 struct _Norm_helper
1006 {
1007 template<typename _Tp>
1008 static inline _GLIBCXX20_CONSTEXPR _Tp _S_do_it(const complex<_Tp>& __z)
1009 {
1010 const _Tp __x = __z.real();
1011 const _Tp __y = __z.imag();
1012 return __x * __x + __y * __y;
1013 }
1014 };
1015
1016 template<>
1017 struct _Norm_helper<true>
1018 {
1019 template<typename _Tp>
1020 static inline _GLIBCXX20_CONSTEXPR _Tp _S_do_it(const complex<_Tp>& __z)
1021 {
1022 //_Tp __res = std::abs(__z);
1023 //return __res * __res;
1024 const _Tp __x = __z.real();
1025 const _Tp __y = __z.imag();
1026 return __x * __x + __y * __y;
1027 }
1028 };
1029
1030 template<typename _Tp>
1031 inline _GLIBCXX20_CONSTEXPR _Tp
1032 norm(const complex<_Tp>& __z)
1033 {
1034 return _Norm_helper<__is_floating<_Tp>::__value
1035 && !_GLIBCXX_FAST_MATH>::_S_do_it(__z);
1036 }
1037
1038 template<typename _Tp>
1039 inline complex<_Tp>
1040 polar(const _Tp& __rho, const _Tp& __theta)
1041 {
1042 __glibcxx_assert( __rho >= _Tp(0) );
1043 return complex<_Tp>(__rho * cos(__theta), __rho * sin(__theta));
1044 }
1045
1046 template<typename _Tp>
1047 inline _GLIBCXX20_CONSTEXPR complex<_Tp>
1048 conj(const complex<_Tp>& __z)
1049 { return complex<_Tp>(__z.real(), -__z.imag()); }
1050
1051 // Transcendentals
1052
1053 // 26.2.8/1 cos(__z): Returns the cosine of __z.
1054 template<typename _Tp>
1055 inline complex<_Tp>
1056 __complex_cos(const complex<_Tp>& __z)
1057 {
1058 const _Tp __x = __z.real();
1059 const _Tp __y = __z.imag();
1060 return complex<_Tp>(cos(__x) * cosh(__y), -sin(__x) * sinh(__y));
1061 }
1062
1063#if _GLIBCXX_USE_C99_COMPLEX
1064 inline __complex__ float
1065 __complex_cos(__complex__ float __z) { return __builtin_ccosf(__z); }
1066
1067 inline __complex__ double
1068 __complex_cos(__complex__ double __z) { return __builtin_ccos(__z); }
1069
1070 inline __complex__ long double
1071 __complex_cos(const __complex__ long double& __z)
1072 { return __builtin_ccosl(__z); }
1073
1074 template<typename _Tp>
1075 inline complex<_Tp>
1076 cos(const complex<_Tp>& __z) { return __complex_cos(__z.__rep()); }
1077#else
1078 template<typename _Tp>
1079 inline complex<_Tp>
1080 cos(const complex<_Tp>& __z) { return __complex_cos(__z); }
1081#endif
1082
1083 // 26.2.8/2 cosh(__z): Returns the hyperbolic cosine of __z.
1084 template<typename _Tp>
1085 inline complex<_Tp>
1086 __complex_cosh(const complex<_Tp>& __z)
1087 {
1088 const _Tp __x = __z.real();
1089 const _Tp __y = __z.imag();
1090 return complex<_Tp>(cosh(__x) * cos(__y), sinh(__x) * sin(__y));
1091 }
1092
1093#if _GLIBCXX_USE_C99_COMPLEX
1094 inline __complex__ float
1095 __complex_cosh(__complex__ float __z) { return __builtin_ccoshf(__z); }
1096
1097 inline __complex__ double
1098 __complex_cosh(__complex__ double __z) { return __builtin_ccosh(__z); }
1099
1100 inline __complex__ long double
1101 __complex_cosh(const __complex__ long double& __z)
1102 { return __builtin_ccoshl(__z); }
1103
1104 template<typename _Tp>
1105 inline complex<_Tp>
1106 cosh(const complex<_Tp>& __z) { return __complex_cosh(__z.__rep()); }
1107#else
1108 template<typename _Tp>
1109 inline complex<_Tp>
1110 cosh(const complex<_Tp>& __z) { return __complex_cosh(__z); }
1111#endif
1112
1113 // 26.2.8/3 exp(__z): Returns the complex base e exponential of x
1114 template<typename _Tp>
1115 inline complex<_Tp>
1116 __complex_exp(const complex<_Tp>& __z)
1117 { return std::polar<_Tp>(exp(__z.real()), __z.imag()); }
1118
1119#if _GLIBCXX_USE_C99_COMPLEX
1120 inline __complex__ float
1121 __complex_exp(__complex__ float __z) { return __builtin_cexpf(__z); }
1122
1123 inline __complex__ double
1124 __complex_exp(__complex__ double __z) { return __builtin_cexp(__z); }
1125
1126 inline __complex__ long double
1127 __complex_exp(const __complex__ long double& __z)
1128 { return __builtin_cexpl(__z); }
1129
1130 template<typename _Tp>
1131 inline complex<_Tp>
1132 exp(const complex<_Tp>& __z) { return __complex_exp(__z.__rep()); }
1133#else
1134 template<typename _Tp>
1135 inline complex<_Tp>
1136 exp(const complex<_Tp>& __z) { return __complex_exp(__z); }
1137#endif
1138
1139 // 26.2.8/5 log(__z): Returns the natural complex logarithm of __z.
1140 // The branch cut is along the negative axis.
1141 template<typename _Tp>
1142 inline complex<_Tp>
1143 __complex_log(const complex<_Tp>& __z)
1144 { return complex<_Tp>(log(std::abs(__z)), std::arg(__z)); }
1145
1146#if _GLIBCXX_USE_C99_COMPLEX
1147 inline __complex__ float
1148 __complex_log(__complex__ float __z) { return __builtin_clogf(__z); }
1149
1150 inline __complex__ double
1151 __complex_log(__complex__ double __z) { return __builtin_clog(__z); }
1152
1153 inline __complex__ long double
1154 __complex_log(const __complex__ long double& __z)
1155 { return __builtin_clogl(__z); }
1156
1157 template<typename _Tp>
1158 inline complex<_Tp>
1159 log(const complex<_Tp>& __z) { return __complex_log(__z.__rep()); }
1160#else
1161 template<typename _Tp>
1162 inline complex<_Tp>
1163 log(const complex<_Tp>& __z) { return __complex_log(__z); }
1164#endif
1165
1166 template<typename _Tp>
1167 inline complex<_Tp>
1169 { return std::log(__z) / log(_Tp(10.0)); }
1170
1171 // 26.2.8/10 sin(__z): Returns the sine of __z.
1172 template<typename _Tp>
1173 inline complex<_Tp>
1174 __complex_sin(const complex<_Tp>& __z)
1175 {
1176 const _Tp __x = __z.real();
1177 const _Tp __y = __z.imag();
1178 return complex<_Tp>(sin(__x) * cosh(__y), cos(__x) * sinh(__y));
1179 }
1180
1181#if _GLIBCXX_USE_C99_COMPLEX
1182 inline __complex__ float
1183 __complex_sin(__complex__ float __z) { return __builtin_csinf(__z); }
1184
1185 inline __complex__ double
1186 __complex_sin(__complex__ double __z) { return __builtin_csin(__z); }
1187
1188 inline __complex__ long double
1189 __complex_sin(const __complex__ long double& __z)
1190 { return __builtin_csinl(__z); }
1191
1192 template<typename _Tp>
1193 inline complex<_Tp>
1194 sin(const complex<_Tp>& __z) { return __complex_sin(__z.__rep()); }
1195#else
1196 template<typename _Tp>
1197 inline complex<_Tp>
1198 sin(const complex<_Tp>& __z) { return __complex_sin(__z); }
1199#endif
1200
1201 // 26.2.8/11 sinh(__z): Returns the hyperbolic sine of __z.
1202 template<typename _Tp>
1203 inline complex<_Tp>
1204 __complex_sinh(const complex<_Tp>& __z)
1205 {
1206 const _Tp __x = __z.real();
1207 const _Tp __y = __z.imag();
1208 return complex<_Tp>(sinh(__x) * cos(__y), cosh(__x) * sin(__y));
1209 }
1210
1211#if _GLIBCXX_USE_C99_COMPLEX
1212 inline __complex__ float
1213 __complex_sinh(__complex__ float __z) { return __builtin_csinhf(__z); }
1214
1215 inline __complex__ double
1216 __complex_sinh(__complex__ double __z) { return __builtin_csinh(__z); }
1217
1218 inline __complex__ long double
1219 __complex_sinh(const __complex__ long double& __z)
1220 { return __builtin_csinhl(__z); }
1221
1222 template<typename _Tp>
1223 inline complex<_Tp>
1224 sinh(const complex<_Tp>& __z) { return __complex_sinh(__z.__rep()); }
1225#else
1226 template<typename _Tp>
1227 inline complex<_Tp>
1228 sinh(const complex<_Tp>& __z) { return __complex_sinh(__z); }
1229#endif
1230
1231 // 26.2.8/13 sqrt(__z): Returns the complex square root of __z.
1232 // The branch cut is on the negative axis.
1233 template<typename _Tp>
1235 __complex_sqrt(const complex<_Tp>& __z)
1236 {
1237 _Tp __x = __z.real();
1238 _Tp __y = __z.imag();
1239
1240 if (__x == _Tp())
1241 {
1242 _Tp __t = sqrt(abs(__y) / _Tp(2));
1243 return complex<_Tp>(__t, __y < _Tp() ? -__t : __t);
1244 }
1245 else
1246 {
1247 _Tp __t = sqrt(_Tp(2) * (std::abs(__z) + abs(__x)));
1248 _Tp __u = __t / _Tp(2);
1249 return __x > _Tp()
1250 ? complex<_Tp>(__u, __y / __t)
1251 : complex<_Tp>(abs(__y) / __t, __y < _Tp() ? -__u : __u);
1252 }
1253 }
1254
1255#if _GLIBCXX_USE_C99_COMPLEX
1256 inline __complex__ float
1257 __complex_sqrt(__complex__ float __z) { return __builtin_csqrtf(__z); }
1258
1259 inline __complex__ double
1260 __complex_sqrt(__complex__ double __z) { return __builtin_csqrt(__z); }
1261
1262 inline __complex__ long double
1263 __complex_sqrt(const __complex__ long double& __z)
1264 { return __builtin_csqrtl(__z); }
1265
1266 template<typename _Tp>
1267 inline complex<_Tp>
1268 sqrt(const complex<_Tp>& __z) { return __complex_sqrt(__z.__rep()); }
1269#else
1270 template<typename _Tp>
1271 inline complex<_Tp>
1272 sqrt(const complex<_Tp>& __z) { return __complex_sqrt(__z); }
1273#endif
1274
1275 // 26.2.8/14 tan(__z): Return the complex tangent of __z.
1276
1277 template<typename _Tp>
1278 inline complex<_Tp>
1279 __complex_tan(const complex<_Tp>& __z)
1280 { return std::sin(__z) / std::cos(__z); }
1281
1282#if _GLIBCXX_USE_C99_COMPLEX
1283 inline __complex__ float
1284 __complex_tan(__complex__ float __z) { return __builtin_ctanf(__z); }
1285
1286 inline __complex__ double
1287 __complex_tan(__complex__ double __z) { return __builtin_ctan(__z); }
1288
1289 inline __complex__ long double
1290 __complex_tan(const __complex__ long double& __z)
1291 { return __builtin_ctanl(__z); }
1292
1293 template<typename _Tp>
1294 inline complex<_Tp>
1295 tan(const complex<_Tp>& __z) { return __complex_tan(__z.__rep()); }
1296#else
1297 template<typename _Tp>
1298 inline complex<_Tp>
1299 tan(const complex<_Tp>& __z) { return __complex_tan(__z); }
1300#endif
1301
1302
1303 // 26.2.8/15 tanh(__z): Returns the hyperbolic tangent of __z.
1304
1305 template<typename _Tp>
1306 inline complex<_Tp>
1307 __complex_tanh(const complex<_Tp>& __z)
1308 { return std::sinh(__z) / std::cosh(__z); }
1309
1310#if _GLIBCXX_USE_C99_COMPLEX
1311 inline __complex__ float
1312 __complex_tanh(__complex__ float __z) { return __builtin_ctanhf(__z); }
1313
1314 inline __complex__ double
1315 __complex_tanh(__complex__ double __z) { return __builtin_ctanh(__z); }
1316
1317 inline __complex__ long double
1318 __complex_tanh(const __complex__ long double& __z)
1319 { return __builtin_ctanhl(__z); }
1320
1321 template<typename _Tp>
1322 inline complex<_Tp>
1323 tanh(const complex<_Tp>& __z) { return __complex_tanh(__z.__rep()); }
1324#else
1325 template<typename _Tp>
1326 inline complex<_Tp>
1327 tanh(const complex<_Tp>& __z) { return __complex_tanh(__z); }
1328#endif
1329
1330
1331 // 26.2.8/9 pow(__x, __y): Returns the complex power base of __x
1332 // raised to the __y-th power. The branch
1333 // cut is on the negative axis.
1334 template<typename _Tp>
1336 __complex_pow_unsigned(complex<_Tp> __x, unsigned __n)
1337 {
1338 complex<_Tp> __y = __n % 2 ? __x : complex<_Tp>(_Tp(1));
1339
1340 while (__n >>= 1)
1341 {
1342 __x *= __x;
1343 if (__n % 2)
1344 __y *= __x;
1345 }
1346
1347 return __y;
1348 }
1349
1350 // In C++11 mode we used to implement the resolution of
1351 // DR 844. complex pow return type is ambiguous.
1352 // thus the following overload was disabled in that mode. However, doing
1353 // that causes all sorts of issues, see, for example:
1354 // http://gcc.gnu.org/ml/libstdc++/2013-01/msg00058.html
1355 // and also PR57974.
1356 template<typename _Tp>
1357 inline complex<_Tp>
1358 pow(const complex<_Tp>& __z, int __n)
1359 {
1360 return __n < 0
1361 ? complex<_Tp>(_Tp(1)) / std::__complex_pow_unsigned(__z,
1362 -(unsigned)__n)
1363 : std::__complex_pow_unsigned(__z, __n);
1364 }
1365
1366 template<typename _Tp>
1367 complex<_Tp>
1368 pow(const complex<_Tp>& __x, const _Tp& __y)
1369 {
1370#if ! _GLIBCXX_USE_C99_COMPLEX
1371 if (__x == _Tp())
1372 return _Tp();
1373#endif
1374 if (__x.imag() == _Tp() && __x.real() > _Tp())
1375 return pow(__x.real(), __y);
1376
1377 complex<_Tp> __t = std::log(__x);
1378 return std::polar<_Tp>(exp(__y * __t.real()), __y * __t.imag());
1379 }
1380
1381 template<typename _Tp>
1382 inline complex<_Tp>
1383 __complex_pow(const complex<_Tp>& __x, const complex<_Tp>& __y)
1384 { return __x == _Tp() ? _Tp() : std::exp(__y * std::log(__x)); }
1385
1386#if _GLIBCXX_USE_C99_COMPLEX
1387 inline __complex__ float
1388 __complex_pow(__complex__ float __x, __complex__ float __y)
1389 { return __builtin_cpowf(__x, __y); }
1390
1391 inline __complex__ double
1392 __complex_pow(__complex__ double __x, __complex__ double __y)
1393 { return __builtin_cpow(__x, __y); }
1394
1395 inline __complex__ long double
1396 __complex_pow(const __complex__ long double& __x,
1397 const __complex__ long double& __y)
1398 { return __builtin_cpowl(__x, __y); }
1399
1400 template<typename _Tp>
1401 inline complex<_Tp>
1402 pow(const complex<_Tp>& __x, const complex<_Tp>& __y)
1403 { return __complex_pow(__x.__rep(), __y.__rep()); }
1404#else
1405 template<typename _Tp>
1406 inline complex<_Tp>
1407 pow(const complex<_Tp>& __x, const complex<_Tp>& __y)
1408 { return __complex_pow(__x, __y); }
1409#endif
1410
1411 template<typename _Tp>
1412 inline complex<_Tp>
1413 pow(const _Tp& __x, const complex<_Tp>& __y)
1414 {
1415 return __x > _Tp() ? std::polar<_Tp>(pow(__x, __y.real()),
1416 __y.imag() * log(__x))
1417 : std::pow(complex<_Tp>(__x), __y);
1418 }
1419
1420#if __glibcxx_tuple_like >= 202311L // >= C++26
1421#define _GLIBCXX26_DECLARE_COMPLEX_TUPLE_HELPER_ACCESSOR \
1422 template<typename _Cp> \
1423 [[__gnu__::__always_inline__]] \
1424 constexpr auto& \
1425 __get_part(this _Cp& __z, size_t __i) noexcept \
1426 { \
1427 return __i == 0 ? __real__ __z._M_value \
1428 : __imag__ __z._M_value; \
1429 }
1430#else
1431#define _GLIBCXX26_DECLARE_COMPLEX_TUPLE_HELPER_ACCESSOR
1432#endif
1433
1434 /// 26.2.3 complex specializations
1435 /// complex<float> specialization
1436 template<>
1437 class complex<float>
1438 {
1439 public:
1440 typedef float value_type;
1441 typedef __complex__ float _ComplexT;
1442
1443 _GLIBCXX_CONSTEXPR complex(_ComplexT __z) : _M_value(__z) { }
1444
1445 _GLIBCXX_CONSTEXPR complex(float __r = 0.0f, float __i = 0.0f)
1446#if __cplusplus >= 201103L
1447 : _M_value{ __r, __i } { }
1448#else
1449 {
1450 __real__ _M_value = __r;
1451 __imag__ _M_value = __i;
1452 }
1453#endif
1454
1455#if __cplusplus >= 201103L
1456 _GLIBCXX14_CONSTEXPR complex(const complex&) = default;
1457#endif
1458
1459#if __cplusplus > 202002L
1460 template<typename _Up>
1461 explicit(!requires(_Up __u) { value_type{__u}; })
1462 constexpr complex(const complex<_Up>& __z)
1463 : _M_value{ value_type(__z.real()), value_type(__z.imag()) } { }
1464#else
1465 explicit _GLIBCXX_CONSTEXPR complex(const complex<double>&);
1466 explicit _GLIBCXX_CONSTEXPR complex(const complex<long double>&);
1467#endif
1468
1469#if __cplusplus >= 201103L
1470 // _GLIBCXX_RESOLVE_LIB_DEFECTS
1471 // DR 387. std::complex over-encapsulated.
1472 __attribute ((__abi_tag__ ("cxx11")))
1473 constexpr float
1474 real() const { return __real__ _M_value; }
1475
1476 __attribute ((__abi_tag__ ("cxx11")))
1477 constexpr float
1478 imag() const { return __imag__ _M_value; }
1479#else
1480 float&
1481 real() { return __real__ _M_value; }
1482
1483 const float&
1484 real() const { return __real__ _M_value; }
1485
1486 float&
1487 imag() { return __imag__ _M_value; }
1488
1489 const float&
1490 imag() const { return __imag__ _M_value; }
1491#endif
1492
1493 // _GLIBCXX_RESOLVE_LIB_DEFECTS
1494 // DR 387. std::complex over-encapsulated.
1495 _GLIBCXX20_CONSTEXPR void
1496 real(float __val) { __real__ _M_value = __val; }
1497
1498 _GLIBCXX20_CONSTEXPR void
1499 imag(float __val) { __imag__ _M_value = __val; }
1500
1501 _GLIBCXX20_CONSTEXPR complex&
1502 operator=(float __f)
1503 {
1504 _M_value = __f;
1505 return *this;
1506 }
1507
1508 _GLIBCXX20_CONSTEXPR complex&
1509 operator+=(float __f)
1510 {
1511 _M_value += __f;
1512 return *this;
1513 }
1514
1515 _GLIBCXX20_CONSTEXPR complex&
1516 operator-=(float __f)
1517 {
1518 _M_value -= __f;
1519 return *this;
1520 }
1521
1522 _GLIBCXX20_CONSTEXPR complex&
1523 operator*=(float __f)
1524 {
1525 _M_value *= __f;
1526 return *this;
1527 }
1528
1529 _GLIBCXX20_CONSTEXPR complex&
1530 operator/=(float __f)
1531 {
1532 _M_value /= __f;
1533 return *this;
1534 }
1535
1536 // Let the compiler synthesize the copy and assignment
1537 // operator. It always does a pretty good job.
1538#if __cplusplus >= 201103L
1539 _GLIBCXX14_CONSTEXPR complex& operator=(const complex&) = default;
1540#endif
1541
1542 template<typename _Tp>
1543 _GLIBCXX20_CONSTEXPR complex&
1544 operator=(const complex<_Tp>& __z)
1545 {
1546 __real__ _M_value = __z.real();
1547 __imag__ _M_value = __z.imag();
1548 return *this;
1549 }
1550
1551 template<typename _Tp>
1552 _GLIBCXX20_CONSTEXPR complex&
1553 operator+=(const complex<_Tp>& __z)
1554 {
1555 _M_value += __z.__rep();
1556 return *this;
1557 }
1558
1559 template<class _Tp>
1560 _GLIBCXX20_CONSTEXPR complex&
1561 operator-=(const complex<_Tp>& __z)
1562 {
1563 _M_value -= __z.__rep();
1564 return *this;
1565 }
1566
1567 template<class _Tp>
1568 _GLIBCXX20_CONSTEXPR complex&
1569 operator*=(const complex<_Tp>& __z)
1570 {
1571 const _ComplexT __t = __z.__rep();
1572 _M_value *= __t;
1573 return *this;
1574 }
1575
1576 template<class _Tp>
1577 _GLIBCXX20_CONSTEXPR complex&
1578 operator/=(const complex<_Tp>& __z)
1579 {
1580 const _ComplexT __t = __z.__rep();
1581 _M_value /= __t;
1582 return *this;
1583 }
1584
1585 _GLIBCXX_CONSTEXPR _ComplexT __rep() const { return _M_value; }
1586
1587 _GLIBCXX26_DECLARE_COMPLEX_TUPLE_HELPER_ACCESSOR
1588
1589 private:
1590 _ComplexT _M_value;
1591 };
1592
1593 /// 26.2.3 complex specializations
1594 /// complex<double> specialization
1595 template<>
1596 class complex<double>
1597 {
1598 public:
1599 typedef double value_type;
1600 typedef __complex__ double _ComplexT;
1601
1602 _GLIBCXX_CONSTEXPR complex(_ComplexT __z) : _M_value(__z) { }
1603
1604 _GLIBCXX_CONSTEXPR complex(double __r = 0.0, double __i = 0.0)
1605#if __cplusplus >= 201103L
1606 : _M_value{ __r, __i } { }
1607#else
1608 {
1609 __real__ _M_value = __r;
1610 __imag__ _M_value = __i;
1611 }
1612#endif
1613
1614#if __cplusplus >= 201103L
1615 _GLIBCXX14_CONSTEXPR complex(const complex&) = default;
1616#endif
1617
1618#if __cplusplus > 202002L
1619 template<typename _Up>
1620 explicit(!requires(_Up __u) { value_type{__u}; })
1621 constexpr complex(const complex<_Up>& __z)
1622 : _M_value{ value_type(__z.real()), value_type(__z.imag()) } { }
1623#else
1624 _GLIBCXX_CONSTEXPR complex(const complex<float>& __z)
1625 : _M_value(__z.__rep()) { }
1626
1627 explicit _GLIBCXX_CONSTEXPR complex(const complex<long double>&);
1628#endif
1629
1630#if __cplusplus >= 201103L
1631 // _GLIBCXX_RESOLVE_LIB_DEFECTS
1632 // DR 387. std::complex over-encapsulated.
1633 __attribute ((__abi_tag__ ("cxx11")))
1634 constexpr double
1635 real() const { return __real__ _M_value; }
1636
1637 __attribute ((__abi_tag__ ("cxx11")))
1638 constexpr double
1639 imag() const { return __imag__ _M_value; }
1640#else
1641 double&
1642 real() { return __real__ _M_value; }
1643
1644 const double&
1645 real() const { return __real__ _M_value; }
1646
1647 double&
1648 imag() { return __imag__ _M_value; }
1649
1650 const double&
1651 imag() const { return __imag__ _M_value; }
1652#endif
1653
1654 // _GLIBCXX_RESOLVE_LIB_DEFECTS
1655 // DR 387. std::complex over-encapsulated.
1656 _GLIBCXX20_CONSTEXPR void
1657 real(double __val) { __real__ _M_value = __val; }
1658
1659 _GLIBCXX20_CONSTEXPR void
1660 imag(double __val) { __imag__ _M_value = __val; }
1661
1662 _GLIBCXX20_CONSTEXPR complex&
1663 operator=(double __d)
1664 {
1665 _M_value = __d;
1666 return *this;
1667 }
1668
1669 _GLIBCXX20_CONSTEXPR complex&
1670 operator+=(double __d)
1671 {
1672 _M_value += __d;
1673 return *this;
1674 }
1675
1676 _GLIBCXX20_CONSTEXPR complex&
1677 operator-=(double __d)
1678 {
1679 _M_value -= __d;
1680 return *this;
1681 }
1682
1683 _GLIBCXX20_CONSTEXPR complex&
1684 operator*=(double __d)
1685 {
1686 _M_value *= __d;
1687 return *this;
1688 }
1689
1690 _GLIBCXX20_CONSTEXPR complex&
1691 operator/=(double __d)
1692 {
1693 _M_value /= __d;
1694 return *this;
1695 }
1696
1697 // The compiler will synthesize this, efficiently.
1698#if __cplusplus >= 201103L
1699 _GLIBCXX14_CONSTEXPR complex& operator=(const complex&) = default;
1700#endif
1701
1702 template<typename _Tp>
1703 _GLIBCXX20_CONSTEXPR complex&
1704 operator=(const complex<_Tp>& __z)
1705 {
1706 _M_value = __z.__rep();
1707 return *this;
1708 }
1709
1710 template<typename _Tp>
1711 _GLIBCXX20_CONSTEXPR complex&
1712 operator+=(const complex<_Tp>& __z)
1713 {
1714 _M_value += __z.__rep();
1715 return *this;
1716 }
1717
1718 template<typename _Tp>
1719 _GLIBCXX20_CONSTEXPR complex&
1720 operator-=(const complex<_Tp>& __z)
1721 {
1722 _M_value -= __z.__rep();
1723 return *this;
1724 }
1725
1726 template<typename _Tp>
1727 _GLIBCXX20_CONSTEXPR complex&
1728 operator*=(const complex<_Tp>& __z)
1729 {
1730 const _ComplexT __t = __z.__rep();
1731 _M_value *= __t;
1732 return *this;
1733 }
1734
1735 template<typename _Tp>
1736 _GLIBCXX20_CONSTEXPR complex&
1737 operator/=(const complex<_Tp>& __z)
1738 {
1739 const _ComplexT __t = __z.__rep();
1740 _M_value /= __t;
1741 return *this;
1742 }
1743
1744 _GLIBCXX_CONSTEXPR _ComplexT __rep() const { return _M_value; }
1745
1746 _GLIBCXX26_DECLARE_COMPLEX_TUPLE_HELPER_ACCESSOR
1747
1748 private:
1749 _ComplexT _M_value;
1750 };
1751
1752 /// 26.2.3 complex specializations
1753 /// complex<long double> specialization
1754 template<>
1755 class complex<long double>
1756 {
1757 public:
1758 typedef long double value_type;
1759 typedef __complex__ long double _ComplexT;
1760
1761 _GLIBCXX_CONSTEXPR complex(_ComplexT __z) : _M_value(__z) { }
1762
1763 _GLIBCXX_CONSTEXPR complex(long double __r = 0.0L,
1764 long double __i = 0.0L)
1765#if __cplusplus >= 201103L
1766 : _M_value{ __r, __i } { }
1767#else
1768 {
1769 __real__ _M_value = __r;
1770 __imag__ _M_value = __i;
1771 }
1772#endif
1773
1774#if __cplusplus >= 201103L
1775 _GLIBCXX14_CONSTEXPR complex(const complex&) = default;
1776#endif
1777
1778#if __cplusplus > 202002L
1779 template<typename _Up>
1780 explicit(!requires(_Up __u) { value_type{__u}; })
1781 constexpr complex(const complex<_Up>& __z)
1782 : _M_value{ value_type(__z.real()), value_type(__z.imag()) } { }
1783#else
1784 _GLIBCXX_CONSTEXPR complex(const complex<float>& __z)
1785 : _M_value(__z.__rep()) { }
1786
1787 _GLIBCXX_CONSTEXPR complex(const complex<double>& __z)
1788 : _M_value(__z.__rep()) { }
1789#endif
1790
1791#if __cplusplus >= 201103L
1792 // _GLIBCXX_RESOLVE_LIB_DEFECTS
1793 // DR 387. std::complex over-encapsulated.
1794 __attribute ((__abi_tag__ ("cxx11")))
1795 constexpr long double
1796 real() const { return __real__ _M_value; }
1797
1798 __attribute ((__abi_tag__ ("cxx11")))
1799 constexpr long double
1800 imag() const { return __imag__ _M_value; }
1801#else
1802 long double&
1803 real() { return __real__ _M_value; }
1804
1805 const long double&
1806 real() const { return __real__ _M_value; }
1807
1808 long double&
1809 imag() { return __imag__ _M_value; }
1810
1811 const long double&
1812 imag() const { return __imag__ _M_value; }
1813#endif
1814
1815 // _GLIBCXX_RESOLVE_LIB_DEFECTS
1816 // DR 387. std::complex over-encapsulated.
1817 _GLIBCXX20_CONSTEXPR void
1818 real(long double __val) { __real__ _M_value = __val; }
1819
1820 _GLIBCXX20_CONSTEXPR void
1821 imag(long double __val) { __imag__ _M_value = __val; }
1822
1823 _GLIBCXX20_CONSTEXPR complex&
1824 operator=(long double __r)
1825 {
1826 _M_value = __r;
1827 return *this;
1828 }
1829
1830 _GLIBCXX20_CONSTEXPR complex&
1831 operator+=(long double __r)
1832 {
1833 _M_value += __r;
1834 return *this;
1835 }
1836
1837 _GLIBCXX20_CONSTEXPR complex&
1838 operator-=(long double __r)
1839 {
1840 _M_value -= __r;
1841 return *this;
1842 }
1843
1844 _GLIBCXX20_CONSTEXPR complex&
1845 operator*=(long double __r)
1846 {
1847 _M_value *= __r;
1848 return *this;
1849 }
1850
1851 _GLIBCXX20_CONSTEXPR complex&
1852 operator/=(long double __r)
1853 {
1854 _M_value /= __r;
1855 return *this;
1856 }
1857
1858 // The compiler knows how to do this efficiently
1859#if __cplusplus >= 201103L
1860 _GLIBCXX14_CONSTEXPR complex& operator=(const complex&) = default;
1861#endif
1862
1863 template<typename _Tp>
1864 _GLIBCXX20_CONSTEXPR complex&
1865 operator=(const complex<_Tp>& __z)
1866 {
1867 _M_value = __z.__rep();
1868 return *this;
1869 }
1870
1871 template<typename _Tp>
1872 _GLIBCXX20_CONSTEXPR complex&
1873 operator+=(const complex<_Tp>& __z)
1874 {
1875 _M_value += __z.__rep();
1876 return *this;
1877 }
1878
1879 template<typename _Tp>
1880 _GLIBCXX20_CONSTEXPR complex&
1881 operator-=(const complex<_Tp>& __z)
1882 {
1883 _M_value -= __z.__rep();
1884 return *this;
1885 }
1886
1887 template<typename _Tp>
1888 _GLIBCXX20_CONSTEXPR complex&
1889 operator*=(const complex<_Tp>& __z)
1890 {
1891 const _ComplexT __t = __z.__rep();
1892 _M_value *= __t;
1893 return *this;
1894 }
1895
1896 template<typename _Tp>
1897 _GLIBCXX20_CONSTEXPR complex&
1898 operator/=(const complex<_Tp>& __z)
1899 {
1900 const _ComplexT __t = __z.__rep();
1901 _M_value /= __t;
1902 return *this;
1903 }
1904
1905 _GLIBCXX_CONSTEXPR _ComplexT __rep() const { return _M_value; }
1906
1907 _GLIBCXX26_DECLARE_COMPLEX_TUPLE_HELPER_ACCESSOR
1908
1909 private:
1910 _ComplexT _M_value;
1911 };
1912
1913#if __cplusplus > 202002L
1914 template<typename _Tp>
1915 struct __complex_type
1916 { };
1917
1918#ifdef __STDCPP_FLOAT16_T__
1919 template<>
1920 struct __complex_type<_Float16>
1921 { typedef __complex__ _Float16 type; };
1922#endif
1923
1924#ifdef __STDCPP_FLOAT32_T__
1925 template<>
1926 struct __complex_type<_Float32>
1927 { typedef __complex__ _Float32 type; };
1928#endif
1929
1930#ifdef __STDCPP_FLOAT64_T__
1931 template<>
1932 struct __complex_type<_Float64>
1933 { typedef __complex__ _Float64 type; };
1934#endif
1935
1936#ifdef __STDCPP_FLOAT128_T__
1937 template<>
1938 struct __complex_type<_Float128>
1939 { typedef __complex__ _Float128 type; };
1940#endif
1941
1942#ifdef __STDCPP_BFLOAT16_T__
1943 template<>
1944 struct __complex_type<__gnu_cxx::__bfloat16_t>
1945 { typedef __complex__ decltype(0.0bf16) type; };
1946#endif
1947
1948 template<typename _Tp>
1949 requires requires { typename __complex_type<_Tp>::type; }
1950 class complex<_Tp>
1951 {
1952 public:
1953 typedef _Tp value_type;
1954 typedef typename std::__complex_type<_Tp>::type _ComplexT;
1955
1956 constexpr complex(_ComplexT __z) : _M_value(__z) { }
1957
1958 constexpr complex(_Tp __r = _Tp(), _Tp __i = _Tp())
1959 : _M_value{ __r, __i } { }
1960
1961 template<typename _Up>
1962 explicit(!requires(_Up __u) { value_type{__u}; })
1963 constexpr complex(const complex<_Up>& __z)
1964 : _M_value{ value_type(__z.real()), value_type(__z.imag()) } { }
1965
1966 constexpr _Tp
1967 real() const { return __real__ _M_value; }
1968
1969 constexpr _Tp
1970 imag() const { return __imag__ _M_value; }
1971
1972 constexpr void
1973 real(_Tp __val) { __real__ _M_value = __val; }
1974
1975 constexpr void
1976 imag(_Tp __val) { __imag__ _M_value = __val; }
1977
1978 constexpr complex&
1979 operator=(_Tp __f)
1980 {
1981 _M_value = __f;
1982 return *this;
1983 }
1984
1985 constexpr complex&
1986 operator+=(_Tp __f)
1987 {
1988 _M_value += __f;
1989 return *this;
1990 }
1991
1992 constexpr complex&
1993 operator-=(_Tp __f)
1994 {
1995 _M_value -= __f;
1996 return *this;
1997 }
1998
1999 constexpr complex&
2000 operator*=(_Tp __f)
2001 {
2002 _M_value *= __f;
2003 return *this;
2004 }
2005
2006 constexpr complex&
2007 operator/=(_Tp __f)
2008 {
2009 _M_value /= __f;
2010 return *this;
2011 }
2012
2013 // Let the compiler synthesize the copy and assignment
2014 // operator. It always does a pretty good job.
2015 constexpr complex(const complex&) = default;
2016 constexpr complex& operator=(const complex&) = default;
2017
2018 template<typename _Up>
2019 constexpr complex&
2020 operator=(const complex<_Up>& __z)
2021 {
2022 __real__ _M_value = __z.real();
2023 __imag__ _M_value = __z.imag();
2024 return *this;
2025 }
2026
2027 template<typename _Up>
2028 constexpr complex&
2029 operator+=(const complex<_Up>& __z)
2030 {
2031 _M_value += __z.__rep();
2032 return *this;
2033 }
2034
2035 template<class _Up>
2036 constexpr complex&
2037 operator-=(const complex<_Up>& __z)
2038 {
2039 _M_value -= __z.__rep();
2040 return *this;
2041 }
2042
2043 template<class _Up>
2044 constexpr complex&
2045 operator*=(const complex<_Up>& __z)
2046 {
2047 const _ComplexT __t = __z.__rep();
2048 _M_value *= __t;
2049 return *this;
2050 }
2051
2052 template<class _Up>
2053 constexpr complex&
2054 operator/=(const complex<_Up>& __z)
2055 {
2056 const _ComplexT __t = __z.__rep();
2057 _M_value /= __t;
2058 return *this;
2059 }
2060
2061 constexpr _ComplexT __rep() const { return _M_value; }
2062
2063 _GLIBCXX26_DECLARE_COMPLEX_TUPLE_HELPER_ACCESSOR
2064
2065 private:
2066 _ComplexT _M_value;
2067 };
2068#endif
2069
2070#undef _GLIBCXX26_DECLARE_COMPLEX_TUPLE_HELPER_ACCESSOR
2071
2072#if __cplusplus <= 202002L
2073 // These bits have to be at the end of this file, so that the
2074 // specializations have all been defined.
2075 inline _GLIBCXX_CONSTEXPR
2077 : _M_value(__z.__rep()) { }
2078
2079 inline _GLIBCXX_CONSTEXPR
2081 : _M_value(__z.__rep()) { }
2082
2083 inline _GLIBCXX_CONSTEXPR
2085 : _M_value(__z.__rep()) { }
2086#endif
2087
2088 // Inhibit implicit instantiations for required instantiations,
2089 // which are defined via explicit instantiations elsewhere.
2090 // NB: This syntax is a GNU extension.
2091#if _GLIBCXX_EXTERN_TEMPLATE
2092 extern template istream& operator>>(istream&, complex<float>&);
2093 extern template ostream& operator<<(ostream&, const complex<float>&);
2094 extern template istream& operator>>(istream&, complex<double>&);
2095 extern template ostream& operator<<(ostream&, const complex<double>&);
2096 extern template istream& operator>>(istream&, complex<long double>&);
2097 extern template ostream& operator<<(ostream&, const complex<long double>&);
2098
2099#ifdef _GLIBCXX_USE_WCHAR_T
2100 extern template wistream& operator>>(wistream&, complex<float>&);
2101 extern template wostream& operator<<(wostream&, const complex<float>&);
2102 extern template wistream& operator>>(wistream&, complex<double>&);
2103 extern template wostream& operator<<(wostream&, const complex<double>&);
2104 extern template wistream& operator>>(wistream&, complex<long double>&);
2105 extern template wostream& operator<<(wostream&, const complex<long double>&);
2106#endif
2107#endif
2108
2109 /// @} group complex_numbers
2110
2111_GLIBCXX_END_NAMESPACE_VERSION
2112} // namespace
2113
2114#if __cplusplus >= 201103L
2115
2116namespace std _GLIBCXX_VISIBILITY(default)
2117{
2118_GLIBCXX_BEGIN_NAMESPACE_VERSION
2119
2120 // Forward declarations.
2121 template<typename _Tp> std::complex<_Tp> acos(const std::complex<_Tp>&);
2122 template<typename _Tp> std::complex<_Tp> asin(const std::complex<_Tp>&);
2123 template<typename _Tp> std::complex<_Tp> atan(const std::complex<_Tp>&);
2124
2125 template<typename _Tp> std::complex<_Tp> acosh(const std::complex<_Tp>&);
2126 template<typename _Tp> std::complex<_Tp> asinh(const std::complex<_Tp>&);
2127 template<typename _Tp> std::complex<_Tp> atanh(const std::complex<_Tp>&);
2128
2129 template<typename _Tp>
2130 inline std::complex<_Tp>
2131 __complex_acos(const std::complex<_Tp>& __z)
2132 {
2133 const std::complex<_Tp> __t = std::asin(__z);
2134 const _Tp __pi_2 = (_Tp) 1.5707963267948966192313216916397514L;
2135 return std::complex<_Tp>(__pi_2 - __t.real(), -__t.imag());
2136 }
2137
2138#if _GLIBCXX_USE_C99_COMPLEX_ARC
2139#if defined(__STDCPP_FLOAT16_T__) && defined(_GLIBCXX_FLOAT_IS_IEEE_BINARY32)
2140 inline __complex__ _Float16
2141 __complex_acos(__complex__ _Float16 __z)
2142 { return static_cast<__complex__ _Float16>(__builtin_cacosf(__z)); }
2143
2144 inline __complex__ _Float16
2145 __complex_asin(__complex__ _Float16 __z)
2146 { return static_cast<__complex__ _Float16>(__builtin_casinf(__z)); }
2147
2148 inline __complex__ _Float16
2149 __complex_atan(__complex__ _Float16 __z)
2150 { return static_cast<__complex__ _Float16>(__builtin_catanf(__z)); }
2151
2152 inline __complex__ _Float16
2153 __complex_acosh(__complex__ _Float16 __z)
2154 { return static_cast<__complex__ _Float16>(__builtin_cacoshf(__z)); }
2155
2156 inline __complex__ _Float16
2157 __complex_asinh(__complex__ _Float16 __z)
2158 { return static_cast<__complex__ _Float16>(__builtin_casinhf(__z)); }
2159
2160 inline __complex__ _Float16
2161 __complex_atanh(__complex__ _Float16 __z)
2162 { return static_cast<__complex__ _Float16>(__builtin_catanhf(__z)); }
2163#endif
2164
2165#if defined(__STDCPP_FLOAT32_T__) && defined(_GLIBCXX_FLOAT_IS_IEEE_BINARY32)
2166 inline __complex__ _Float32
2167 __complex_acos(__complex__ _Float32 __z)
2168 { return __builtin_cacosf(__z); }
2169
2170 inline __complex__ _Float32
2171 __complex_asin(__complex__ _Float32 __z)
2172 { return __builtin_casinf(__z); }
2173
2174 inline __complex__ _Float32
2175 __complex_atan(__complex__ _Float32 __z)
2176 { return __builtin_catanf(__z); }
2177
2178 inline __complex__ _Float32
2179 __complex_acosh(__complex__ _Float32 __z)
2180 { return __builtin_cacoshf(__z); }
2181
2182 inline __complex__ _Float32
2183 __complex_asinh(__complex__ _Float32 __z)
2184 { return __builtin_casinhf(__z); }
2185
2186 inline __complex__ _Float32
2187 __complex_atanh(__complex__ _Float32 __z)
2188 { return __builtin_catanhf(__z); }
2189#endif
2190
2191#if defined(__STDCPP_FLOAT64_T__) && defined(_GLIBCXX_DOUBLE_IS_IEEE_BINARY64)
2192 inline __complex__ _Float64
2193 __complex_acos(__complex__ _Float64 __z)
2194 { return __builtin_cacos(__z); }
2195
2196 inline __complex__ _Float64
2197 __complex_asin(__complex__ _Float64 __z)
2198 { return __builtin_casin(__z); }
2199
2200 inline __complex__ _Float64
2201 __complex_atan(__complex__ _Float64 __z)
2202 { return __builtin_catan(__z); }
2203
2204 inline __complex__ _Float64
2205 __complex_acosh(__complex__ _Float64 __z)
2206 { return __builtin_cacosh(__z); }
2207
2208 inline __complex__ _Float64
2209 __complex_asinh(__complex__ _Float64 __z)
2210 { return __builtin_casinh(__z); }
2211
2212 inline __complex__ _Float64
2213 __complex_atanh(__complex__ _Float64 __z)
2214 { return __builtin_catanh(__z); }
2215#endif
2216
2217#if defined(__STDCPP_FLOAT128_T__) && defined(_GLIBCXX_LDOUBLE_IS_IEEE_BINARY128)
2218 inline __complex__ _Float128
2219 __complex_acos(__complex__ _Float128 __z)
2220 { return __builtin_cacosl(__z); }
2221
2222 inline __complex__ _Float128
2223 __complex_asin(__complex__ _Float128 __z)
2224 { return __builtin_casinl(__z); }
2225
2226 inline __complex__ _Float128
2227 __complex_atan(__complex__ _Float128 __z)
2228 { return __builtin_catanl(__z); }
2229
2230 inline __complex__ _Float128
2231 __complex_acosh(__complex__ _Float128 __z)
2232 { return __builtin_cacoshl(__z); }
2233
2234 inline __complex__ _Float128
2235 __complex_asinh(__complex__ _Float128 __z)
2236 { return __builtin_casinhl(__z); }
2237
2238 inline __complex__ _Float128
2239 __complex_atanh(__complex__ _Float128 __z)
2240 { return __builtin_catanhl(__z); }
2241#elif defined(__STDCPP_FLOAT128_T__) && defined(_GLIBCXX_HAVE_FLOAT128_MATH)
2242 inline __complex__ _Float128
2243 __complex_acos(__complex__ _Float128 __z)
2244 { return __builtin_cacosf128(__z); }
2245
2246 inline __complex__ _Float128
2247 __complex_asin(__complex__ _Float128 __z)
2248 { return __builtin_casinf128(__z); }
2249
2250 inline __complex__ _Float128
2251 __complex_atan(__complex__ _Float128 __z)
2252 { return __builtin_catanf128(__z); }
2253
2254 inline __complex__ _Float128
2255 __complex_acosh(__complex__ _Float128 __z)
2256 { return __builtin_cacoshf128(__z); }
2257
2258 inline __complex__ _Float128
2259 __complex_asinh(__complex__ _Float128 __z)
2260 { return __builtin_casinhf128(__z); }
2261
2262 inline __complex__ _Float128
2263 __complex_atanh(__complex__ _Float128 __z)
2264 { return __builtin_catanhf128(__z); }
2265#endif
2266
2267#if defined(__STDCPP_BFLOAT16_T__) && defined(_GLIBCXX_FLOAT_IS_IEEE_BINARY32)
2268 inline __complex__ decltype(0.0bf16)
2269 __complex_acos(__complex__ decltype(0.0bf16) __z)
2270 { return static_cast<__complex__ decltype(0.0bf16)>(__builtin_cacosf(__z)); }
2271
2272 inline __complex__ decltype(0.0bf16)
2273 __complex_asin(__complex__ decltype(0.0bf16) __z)
2274 { return static_cast<__complex__ decltype(0.0bf16)>(__builtin_casinf(__z)); }
2275
2276 inline __complex__ decltype(0.0bf16)
2277 __complex_atan(__complex__ decltype(0.0bf16) __z)
2278 { return static_cast<__complex__ decltype(0.0bf16)>(__builtin_catanf(__z)); }
2279
2280 inline __complex__ decltype(0.0bf16)
2281 __complex_acosh(__complex__ decltype(0.0bf16) __z)
2282 { return static_cast<__complex__ decltype(0.0bf16)>(__builtin_cacoshf(__z)); }
2283
2284 inline __complex__ decltype(0.0bf16)
2285 __complex_asinh(__complex__ decltype(0.0bf16) __z)
2286 { return static_cast<__complex__ decltype(0.0bf16)>(__builtin_casinhf(__z)); }
2287
2288 inline __complex__ decltype(0.0bf16)
2289 __complex_atanh(__complex__ decltype(0.0bf16) __z)
2290 { return static_cast<__complex__ decltype(0.0bf16)>(__builtin_catanhf(__z)); }
2291#endif
2292#endif
2293
2294#if _GLIBCXX_USE_C99_COMPLEX_ARC
2295 inline __complex__ float
2296 __complex_acos(__complex__ float __z)
2297 { return __builtin_cacosf(__z); }
2298
2299 inline __complex__ double
2300 __complex_acos(__complex__ double __z)
2301 { return __builtin_cacos(__z); }
2302
2303 inline __complex__ long double
2304 __complex_acos(const __complex__ long double& __z)
2305 { return __builtin_cacosl(__z); }
2306
2307 template<typename _Tp>
2308 inline std::complex<_Tp>
2309 acos(const std::complex<_Tp>& __z)
2310 { return __complex_acos(__z.__rep()); }
2311#else
2312 /// acos(__z) C++11 26.4.8 [complex.transcendentals]
2313 // Effects: Behaves the same as C99 function cacos, defined
2314 // in subclause 7.3.5.1.
2315 template<typename _Tp>
2316 inline std::complex<_Tp>
2317 acos(const std::complex<_Tp>& __z)
2318 { return __complex_acos(__z); }
2319#endif
2320
2321 template<typename _Tp>
2322 inline std::complex<_Tp>
2323 __complex_asin(const std::complex<_Tp>& __z)
2324 {
2325 std::complex<_Tp> __t(-__z.imag(), __z.real());
2326 __t = std::asinh(__t);
2327 return std::complex<_Tp>(__t.imag(), -__t.real());
2328 }
2329
2330#if _GLIBCXX_USE_C99_COMPLEX_ARC
2331 inline __complex__ float
2332 __complex_asin(__complex__ float __z)
2333 { return __builtin_casinf(__z); }
2334
2335 inline __complex__ double
2336 __complex_asin(__complex__ double __z)
2337 { return __builtin_casin(__z); }
2338
2339 inline __complex__ long double
2340 __complex_asin(const __complex__ long double& __z)
2341 { return __builtin_casinl(__z); }
2342
2343 template<typename _Tp>
2344 inline std::complex<_Tp>
2345 asin(const std::complex<_Tp>& __z)
2346 { return __complex_asin(__z.__rep()); }
2347#else
2348 /// asin(__z) C++11 26.4.8 [complex.transcendentals]
2349 // Effects: Behaves the same as C99 function casin, defined
2350 // in subclause 7.3.5.2.
2351 template<typename _Tp>
2352 inline std::complex<_Tp>
2353 asin(const std::complex<_Tp>& __z)
2354 { return __complex_asin(__z); }
2355#endif
2356
2357 template<typename _Tp>
2359 __complex_atan(const std::complex<_Tp>& __z)
2360 {
2361 const _Tp __r2 = __z.real() * __z.real();
2362 const _Tp __x = _Tp(1.0) - __r2 - __z.imag() * __z.imag();
2363
2364 _Tp __num = __z.imag() + _Tp(1.0);
2365 _Tp __den = __z.imag() - _Tp(1.0);
2366
2367 __num = __r2 + __num * __num;
2368 __den = __r2 + __den * __den;
2369
2370 return std::complex<_Tp>(_Tp(0.5) * atan2(_Tp(2.0) * __z.real(), __x),
2371 _Tp(0.25) * log(__num / __den));
2372 }
2373
2374#if _GLIBCXX_USE_C99_COMPLEX_ARC
2375 inline __complex__ float
2376 __complex_atan(__complex__ float __z)
2377 { return __builtin_catanf(__z); }
2378
2379 inline __complex__ double
2380 __complex_atan(__complex__ double __z)
2381 { return __builtin_catan(__z); }
2382
2383 inline __complex__ long double
2384 __complex_atan(const __complex__ long double& __z)
2385 { return __builtin_catanl(__z); }
2386
2387 template<typename _Tp>
2388 inline std::complex<_Tp>
2389 atan(const std::complex<_Tp>& __z)
2390 { return __complex_atan(__z.__rep()); }
2391#else
2392 /// atan(__z) C++11 26.4.8 [complex.transcendentals]
2393 // Effects: Behaves the same as C99 function catan, defined
2394 // in subclause 7.3.5.3.
2395 template<typename _Tp>
2396 inline std::complex<_Tp>
2397 atan(const std::complex<_Tp>& __z)
2398 { return __complex_atan(__z); }
2399#endif
2400
2401 template<typename _Tp>
2403 __complex_acosh(const std::complex<_Tp>& __z)
2404 {
2405 // Kahan's formula.
2406 return _Tp(2.0) * std::log(std::sqrt(_Tp(0.5) * (__z + _Tp(1.0)))
2407 + std::sqrt(_Tp(0.5) * (__z - _Tp(1.0))));
2408 }
2409
2410#if _GLIBCXX_USE_C99_COMPLEX_ARC
2411 inline __complex__ float
2412 __complex_acosh(__complex__ float __z)
2413 { return __builtin_cacoshf(__z); }
2414
2415 inline __complex__ double
2416 __complex_acosh(__complex__ double __z)
2417 { return __builtin_cacosh(__z); }
2418
2419 inline __complex__ long double
2420 __complex_acosh(const __complex__ long double& __z)
2421 { return __builtin_cacoshl(__z); }
2422
2423 template<typename _Tp>
2424 inline std::complex<_Tp>
2425 acosh(const std::complex<_Tp>& __z)
2426 { return __complex_acosh(__z.__rep()); }
2427#else
2428 /// acosh(__z) C++11 26.4.8 [complex.transcendentals]
2429 // Effects: Behaves the same as C99 function cacosh, defined
2430 // in subclause 7.3.6.1.
2431 template<typename _Tp>
2432 inline std::complex<_Tp>
2433 acosh(const std::complex<_Tp>& __z)
2434 { return __complex_acosh(__z); }
2435#endif
2436
2437 template<typename _Tp>
2439 __complex_asinh(const std::complex<_Tp>& __z)
2440 {
2441 std::complex<_Tp> __t((__z.real() - __z.imag())
2442 * (__z.real() + __z.imag()) + _Tp(1.0),
2443 _Tp(2.0) * __z.real() * __z.imag());
2444 __t = std::sqrt(__t);
2445
2446 return std::log(__t + __z);
2447 }
2448
2449#if _GLIBCXX_USE_C99_COMPLEX_ARC
2450 inline __complex__ float
2451 __complex_asinh(__complex__ float __z)
2452 { return __builtin_casinhf(__z); }
2453
2454 inline __complex__ double
2455 __complex_asinh(__complex__ double __z)
2456 { return __builtin_casinh(__z); }
2457
2458 inline __complex__ long double
2459 __complex_asinh(const __complex__ long double& __z)
2460 { return __builtin_casinhl(__z); }
2461
2462 template<typename _Tp>
2463 inline std::complex<_Tp>
2464 asinh(const std::complex<_Tp>& __z)
2465 { return __complex_asinh(__z.__rep()); }
2466#else
2467 /// asinh(__z) C++11 26.4.8 [complex.transcendentals]
2468 // Effects: Behaves the same as C99 function casin, defined
2469 // in subclause 7.3.6.2.
2470 template<typename _Tp>
2471 inline std::complex<_Tp>
2472 asinh(const std::complex<_Tp>& __z)
2473 { return __complex_asinh(__z); }
2474#endif
2475
2476 template<typename _Tp>
2478 __complex_atanh(const std::complex<_Tp>& __z)
2479 {
2480 const _Tp __i2 = __z.imag() * __z.imag();
2481 const _Tp __x = _Tp(1.0) - __i2 - __z.real() * __z.real();
2482
2483 _Tp __num = _Tp(1.0) + __z.real();
2484 _Tp __den = _Tp(1.0) - __z.real();
2485
2486 __num = __i2 + __num * __num;
2487 __den = __i2 + __den * __den;
2488
2489 return std::complex<_Tp>(_Tp(0.25) * (log(__num) - log(__den)),
2490 _Tp(0.5) * atan2(_Tp(2.0) * __z.imag(), __x));
2491 }
2492
2493#if _GLIBCXX_USE_C99_COMPLEX_ARC
2494 inline __complex__ float
2495 __complex_atanh(__complex__ float __z)
2496 { return __builtin_catanhf(__z); }
2497
2498 inline __complex__ double
2499 __complex_atanh(__complex__ double __z)
2500 { return __builtin_catanh(__z); }
2501
2502 inline __complex__ long double
2503 __complex_atanh(const __complex__ long double& __z)
2504 { return __builtin_catanhl(__z); }
2505
2506 template<typename _Tp>
2507 inline std::complex<_Tp>
2508 atanh(const std::complex<_Tp>& __z)
2509 { return __complex_atanh(__z.__rep()); }
2510#else
2511 /// atanh(__z) C++11 26.4.8 [complex.transcendentals]
2512 // Effects: Behaves the same as C99 function catanh, defined
2513 // in subclause 7.3.6.3.
2514 template<typename _Tp>
2515 inline std::complex<_Tp>
2516 atanh(const std::complex<_Tp>& __z)
2517 { return __complex_atanh(__z); }
2518#endif
2519
2520 template<typename _Tp>
2521 _GLIBCXX11_DEPRECATED_SUGGEST("std::abs")
2522 inline _Tp
2523 /// fabs(__z) TR1 8.1.8 [tr.c99.cmplx.fabs]
2524 // Effects: Behaves the same as C99 function cabs, defined
2525 // in subclause 7.3.8.1.
2526 fabs(const std::complex<_Tp>& __z)
2527 { return std::abs(__z); }
2528
2529 // Additional overloads C++11 26.4.9 [cmplx.over]
2530
2531 template<typename _Tp>
2532 inline typename __gnu_cxx::__promote<_Tp>::__type
2533 arg(_Tp __x)
2534 {
2535 typedef typename __gnu_cxx::__promote<_Tp>::__type __type;
2536#if (_GLIBCXX11_USE_C99_MATH && !_GLIBCXX_USE_C99_FP_MACROS_DYNAMIC)
2537 return __builtin_signbit(__type(__x))
2538 ? __type(3.1415926535897932384626433832795029L) : __type();
2539#else
2540 return std::arg(std::complex<__type>(__x));
2541#endif
2542 }
2543
2544 template<typename _Tp>
2545 _GLIBCXX_CONSTEXPR inline typename __gnu_cxx::__promote<_Tp>::__type
2546 imag(_Tp)
2547 { return _Tp(); }
2548
2549 template<typename _Tp>
2550 _GLIBCXX20_CONSTEXPR inline typename __gnu_cxx::__promote<_Tp>::__type
2551 norm(_Tp __x)
2552 {
2553 typedef typename __gnu_cxx::__promote<_Tp>::__type __type;
2554 return __type(__x) * __type(__x);
2555 }
2556
2557 template<typename _Tp>
2558 _GLIBCXX_CONSTEXPR inline typename __gnu_cxx::__promote<_Tp>::__type
2559 real(_Tp __x)
2560 { return __x; }
2561
2562 template<typename _Tp, typename _Up>
2564 pow(const std::complex<_Tp>& __x, const _Up& __y)
2565 {
2566 typedef typename __gnu_cxx::__promote_2<_Tp, _Up>::__type __type;
2567 return std::pow(std::complex<__type>(__x), __type(__y));
2568 }
2569
2570 template<typename _Tp, typename _Up>
2572 pow(const _Tp& __x, const std::complex<_Up>& __y)
2573 {
2574 typedef typename __gnu_cxx::__promote_2<_Tp, _Up>::__type __type;
2575 return std::pow(__type(__x), std::complex<__type>(__y));
2576 }
2577
2578 template<typename _Tp, typename _Up>
2580 pow(const std::complex<_Tp>& __x, const std::complex<_Up>& __y)
2581 {
2582 typedef typename __gnu_cxx::__promote_2<_Tp, _Up>::__type __type;
2583 return std::pow(std::complex<__type>(__x),
2585 }
2586
2587 // Forward declarations.
2588 // DR 781.
2589 template<typename _Tp>
2591
2592 // Generic implementation of std::proj, does not work for infinities.
2593 template<typename _Tp>
2594 inline std::complex<_Tp>
2595 __complex_proj(const std::complex<_Tp>& __z)
2596 { return __z; }
2597
2598#if _GLIBCXX_USE_C99_COMPLEX
2599 inline complex<float>
2600 __complex_proj(const complex<float>& __z)
2601 { return __builtin_cprojf(__z.__rep()); }
2602
2603 inline complex<double>
2604 __complex_proj(const complex<double>& __z)
2605 { return __builtin_cproj(__z.__rep()); }
2606
2607 inline complex<long double>
2608 __complex_proj(const complex<long double>& __z)
2609 { return __builtin_cprojl(__z.__rep()); }
2610
2611#if __cplusplus > 202002L
2612#if defined(__STDCPP_FLOAT16_T__) && defined(_GLIBCXX_FLOAT_IS_IEEE_BINARY32)
2613 inline __complex__ _Float16
2614 __complex_proj(__complex__ _Float16 __z)
2615 { return static_cast<__complex__ _Float16>(__builtin_cprojf(__z)); }
2616#endif
2617
2618#if defined(__STDCPP_FLOAT32_T__) && defined(_GLIBCXX_FLOAT_IS_IEEE_BINARY32)
2619 inline __complex__ _Float32
2620 __complex_proj(__complex__ _Float32 __z)
2621 { return __builtin_cprojf(__z); }
2622#endif
2623
2624#if defined(__STDCPP_FLOAT64_T__) && defined(_GLIBCXX_DOUBLE_IS_IEEE_BINARY64)
2625 inline __complex__ _Float64
2626 __complex_proj(__complex__ _Float64 __z)
2627 { return __builtin_cproj(__z); }
2628#endif
2629
2630#if defined(__STDCPP_FLOAT128_T__) && defined(_GLIBCXX_LDOUBLE_IS_IEEE_BINARY128)
2631 inline __complex__ _Float128
2632 __complex_proj(__complex__ _Float128 __z)
2633 { return __builtin_cprojl(__z); }
2634#elif defined(__STDCPP_FLOAT128_T__) && defined(_GLIBCXX_HAVE_FLOAT128_MATH)
2635 inline __complex__ _Float128
2636 __complex_proj(__complex__ _Float128 __z)
2637 { return __builtin_cprojf128(__z); }
2638#endif
2639
2640#if defined(__STDCPP_BFLOAT16_T__) && defined(_GLIBCXX_FLOAT_IS_IEEE_BINARY32)
2641 inline __complex__ decltype(0.0bf16)
2642 __complex_proj(__complex__ decltype(0.0bf16) __z)
2643 { return static_cast<__complex__ decltype(0.0bf16)>(__builtin_cprojf(__z)); }
2644#endif
2645
2646 template<typename _Tp>
2647 requires requires { typename __complex_type<_Tp>::type; }
2648 inline complex<_Tp>
2649 __complex_proj(const complex<_Tp>& __z)
2650 { return __complex_proj(__z.__rep()); }
2651#endif
2652
2653#elif defined _GLIBCXX_USE_C99_MATH_FUNCS
2654 inline complex<float>
2655 __complex_proj(const complex<float>& __z)
2656 {
2657 if (__builtin_isinf(__z.real()) || __builtin_isinf(__z.imag()))
2658 return complex<float>(__builtin_inff(),
2659 __builtin_copysignf(0.0f, __z.imag()));
2660 return __z;
2661 }
2662
2663 inline complex<double>
2664 __complex_proj(const complex<double>& __z)
2665 {
2666 if (__builtin_isinf(__z.real()) || __builtin_isinf(__z.imag()))
2667 return complex<double>(__builtin_inf(),
2668 __builtin_copysign(0.0, __z.imag()));
2669 return __z;
2670 }
2671
2672 inline complex<long double>
2673 __complex_proj(const complex<long double>& __z)
2674 {
2675 if (__builtin_isinf(__z.real()) || __builtin_isinf(__z.imag()))
2676 return complex<long double>(__builtin_infl(),
2677 __builtin_copysignl(0.0l, __z.imag()));
2678 return __z;
2679 }
2680#endif
2681
2682 template<typename _Tp>
2683 inline std::complex<_Tp>
2684 proj(const std::complex<_Tp>& __z)
2685 { return __complex_proj(__z); }
2686
2687 // Overload for scalars
2688 template<typename _Tp>
2690 proj(_Tp __x)
2691 {
2692 typedef typename __gnu_cxx::__promote<_Tp>::__type __type;
2693 return std::proj(std::complex<__type>(__x));
2694 }
2695
2696 template<typename _Tp>
2697 inline _GLIBCXX20_CONSTEXPR
2699 conj(_Tp __x)
2700 {
2701 typedef typename __gnu_cxx::__promote<_Tp>::__type __type;
2702 return std::complex<__type>(__x, -__type());
2703 }
2704
2705#ifdef __cpp_lib_complex_udls // C++ >= 14
2706
2707inline namespace literals {
2708inline namespace complex_literals {
2709#pragma GCC diagnostic push
2710#pragma GCC diagnostic ignored "-Wliteral-suffix"
2711
2712 constexpr std::complex<float>
2713 operator""if(long double __num)
2714 { return std::complex<float>{0.0F, static_cast<float>(__num)}; }
2715
2716 constexpr std::complex<float>
2717 operator""if(unsigned long long __num)
2718 { return std::complex<float>{0.0F, static_cast<float>(__num)}; }
2719
2720 constexpr std::complex<double>
2721 operator""i(long double __num)
2722 { return std::complex<double>{0.0, static_cast<double>(__num)}; }
2723
2724 constexpr std::complex<double>
2725 operator""i(unsigned long long __num)
2726 { return std::complex<double>{0.0, static_cast<double>(__num)}; }
2727
2728 constexpr std::complex<long double>
2729 operator""il(long double __num)
2730 { return std::complex<long double>{0.0L, __num}; }
2731
2732 constexpr std::complex<long double>
2733 operator""il(unsigned long long __num)
2734 { return std::complex<long double>{0.0L, static_cast<long double>(__num)}; }
2735
2736#pragma GCC diagnostic pop
2737} // inline namespace complex_literals
2738} // inline namespace literals
2739
2740#endif // __cpp_lib_complex_udls
2741
2742_GLIBCXX_END_NAMESPACE_VERSION
2743} // namespace
2744
2745#endif // C++11
2746
2747#ifdef _GLIBCXX_CLANG
2748#pragma clang diagnostic pop
2749#endif
2750
2751#pragma GCC diagnostic pop
2752#endif /* _GLIBCXX_COMPLEX */
complex< _Tp > log(const complex< _Tp > &)
Return complex natural logarithm of z.
Definition complex:1159
complex< _Tp > pow(const complex< _Tp > &, int)
Return x to the y'th power.
Definition complex:1358
complex< _Tp > log10(const complex< _Tp > &)
Return complex base 10 logarithm of z.
Definition complex:1168
constexpr complex< _Tp > operator*(const complex< _Tp > &__x, const complex< _Tp > &__y)
Return new complex value x times y.
Definition complex:434
complex< _Tp > sin(const complex< _Tp > &)
Return complex sine of z.
Definition complex:1194
constexpr complex< _Tp > operator-(const complex< _Tp > &__x, const complex< _Tp > &__y)
Return new complex value x minus y.
Definition complex:404
complex< _Tp > log(const complex< _Tp > &)
Return complex natural logarithm of z.
Definition complex:1159
complex< _Tp > tan(const complex< _Tp > &)
Return complex tangent of z.
Definition complex:1295
constexpr complex< _Tp > operator+(const complex< _Tp > &__x, const complex< _Tp > &__y)
Return new complex value x plus y.
Definition complex:374
_Tp abs(const complex< _Tp > &)
Return magnitude of z.
Definition complex:965
complex< _Tp > polar(const _Tp &, const _Tp &=_Tp(0))
Return complex with magnitude rho and angle theta.
Definition complex:1040
complex< _Tp > exp(const complex< _Tp > &)
Return complex base e exponential of z.
Definition complex:1132
complex< _Tp > cosh(const complex< _Tp > &)
Return complex hyperbolic cosine of z.
Definition complex:1106
_Tp arg(const complex< _Tp > &)
Return phase angle of z.
Definition complex:992
complex< _Tp > tanh(const complex< _Tp > &)
Return complex hyperbolic tangent of z.
Definition complex:1323
constexpr complex< _Tp > conj(const complex< _Tp > &)
Return complex conjugate of z.
Definition complex:1048
complex< _Tp > pow(const complex< _Tp > &, int)
Return x to the y'th power.
Definition complex:1358
constexpr complex< _Tp > operator/(const complex< _Tp > &__x, const complex< _Tp > &__y)
Return new complex value x divided by y.
Definition complex:464
complex< _Tp > sinh(const complex< _Tp > &)
Return complex hyperbolic sine of z.
Definition complex:1224
_Tp constexpr norm(const complex< _Tp > &)
Return z magnitude squared.
Definition complex:1032
complex< _Tp > cos(const complex< _Tp > &)
Return complex cosine of z.
Definition complex:1076
complex< _Tp > sqrt(const complex< _Tp > &)
Return complex square root of z.
Definition complex:1268
basic_ostream< char > ostream
Base class for char output streams.
Definition iosfwd.h:84
basic_istream< char > istream
Base class for char input streams.
Definition iosfwd.h:81
basic_ostream< wchar_t > wostream
Base class for wchar_t output streams.
Definition iosfwd.h:101
basic_istream< wchar_t > wistream
Base class for wchar_t input streams.
Definition iosfwd.h:98
constexpr std::remove_reference< _Tp >::type && move(_Tp &&__t) noexcept
Convert a value to an rvalue.
Definition move.h:138
constexpr const _Tp & max(const _Tp &, const _Tp &)
This does what you think it does.
ISO C++ entities toplevel namespace is std.
_Tp fabs(const std::complex< _Tp > &__z)
fabs(__z) TR1 8.1.8 [tr.c99.cmplx.fabs]
Definition complex:2526
std::basic_istream< _CharT, _Traits > & operator>>(std::basic_istream< _CharT, _Traits > &__is, bitset< _Nb > &__x)
Global I/O operators for bitsets.
Definition bitset:1702
std::basic_ostream< _CharT, _Traits > & operator<<(std::basic_ostream< _CharT, _Traits > &__os, const bitset< _Nb > &__x)
Global I/O operators for bitsets.
Definition bitset:1801
constexpr complex< _Tp > & operator-=(const complex< _Up > &)
Subtract another complex number from this one.
_Tp value_type
Value typedef.
Definition complex:160
constexpr complex< _Tp > & operator=(const complex< _Up > &)
Assign another complex number to this one.
constexpr complex< _Tp > & operator*=(const _Tp &)
Multiply this complex number by a scalar.
constexpr complex(const _Tp &__r=_Tp(), const _Tp &__i=_Tp())
Default constructor. First parameter is x, second parameter is y. Unspecified parameters default to 0...
Definition complex:164
constexpr complex< _Tp > & operator+=(const complex< _Up > &)
Add another complex number to this one.
constexpr complex< _Tp > & operator/=(const complex< _Up > &)
Divide this complex number by another.
constexpr complex< _Tp > & operator=(const _Tp &)
Assign a scalar to this complex number.
constexpr complex< _Tp > & operator-=(const _Tp &__t)
Subtract a scalar from this complex number.
Definition complex:231
constexpr complex< _Tp > & operator/=(const _Tp &)
Divide this complex number by a scalar.
constexpr complex< _Tp > & operator+=(const _Tp &__t)
Add a scalar to this complex number.
Definition complex:222
constexpr complex< _Tp > & operator*=(const complex< _Up > &)
Multiply this complex number by another.
Template class basic_istream.
Definition istream:73
__istream_type & putback(char_type __c)
Unextracting a single character.
Definition istream.tcc:771
integral_constant
Definition type_traits:98
void setstate(iostate __state)
Sets additional flags in the error state.
Definition basic_ios.h:167
char_type widen(char __c) const
Widens characters.
Definition basic_ios.h:465
fmtflags flags() const
Access to format flags.
Definition ios_base.h:694
static const iostate failbit
Indicates that an input operation failed to read the expected characters, or that an output operation...
Definition ios_base.h:465
Finds the size of a given tuple type.
Definition utility.h:55
Gives the type of the ith element of a given tuple type.
Definition utility.h:86