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