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