1 // The template and inlines for the -*- C++ -*- complex number classes. 2 3 // Copyright (C) 1997-2014 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 // The C++ <complex> header is incompatible with the C99 <complex.h> header, 48 // they cannot be included into a single translation unit portably. Notably, 49 // C++11's <ccomplex> does not include C99's <complex.h> and in C++11's 50 // <complex.h> is defined to provide only what C++11's <ccomplex> does in a 51 // different namespace. 52 #ifdef _GLIBCXX_C99_COMPLEX_H 53 #error "Cannot include both <complex> and C99 <complex.h>" 54 #endif 55 56 namespace std _GLIBCXX_VISIBILITY(default) 57 { 58 _GLIBCXX_BEGIN_NAMESPACE_VERSION 59 60 /** 61 * @defgroup complex_numbers Complex Numbers 62 * @ingroup numerics 63 * 64 * Classes and functions for complex numbers. 65 * @{ 66 */ 67 68 // Forward declarations. 69 template<typename _Tp> class complex; 70 template<> class complex<float>; 71 template<> class complex<double>; 72 template<> class complex<long double>; 73 74 /// Return magnitude of @a z. 75 template<typename _Tp> _Tp abs(const complex<_Tp>&); 76 /// Return phase angle of @a z. 77 template<typename _Tp> _Tp arg(const complex<_Tp>&); 78 /// Return @a z magnitude squared. 79 template<typename _Tp> _Tp norm(const complex<_Tp>&); 80 81 /// Return complex conjugate of @a z. 82 template<typename _Tp> complex<_Tp> conj(const complex<_Tp>&); 83 /// Return complex with magnitude @a rho and angle @a theta. 84 template<typename _Tp> complex<_Tp> polar(const _Tp&, const _Tp& = 0); 85 86 // Transcendentals: 87 /// Return complex cosine of @a z. 88 template<typename _Tp> complex<_Tp> cos(const complex<_Tp>&); 89 /// Return complex hyperbolic cosine of @a z. 90 template<typename _Tp> complex<_Tp> cosh(const complex<_Tp>&); 91 /// Return complex base e exponential of @a z. 92 template<typename _Tp> complex<_Tp> exp(const complex<_Tp>&); 93 /// Return complex natural logarithm of @a z. 94 template<typename _Tp> complex<_Tp> log(const complex<_Tp>&); 95 /// Return complex base 10 logarithm of @a z. 96 template<typename _Tp> complex<_Tp> log10(const complex<_Tp>&); 97 /// Return @a x to the @a y'th power. 98 template<typename _Tp> complex<_Tp> pow(const complex<_Tp>&, int); 99 /// Return @a x to the @a y'th power. 100 template<typename _Tp> complex<_Tp> pow(const complex<_Tp>&, const _Tp&); 101 /// Return @a x to the @a y'th power. 102 template<typename _Tp> complex<_Tp> pow(const complex<_Tp>&, 103 const complex<_Tp>&); 104 /// Return @a x to the @a y'th power. 105 template<typename _Tp> complex<_Tp> pow(const _Tp&, const complex<_Tp>&); 106 /// Return complex sine of @a z. 107 template<typename _Tp> complex<_Tp> sin(const complex<_Tp>&); 108 /// Return complex hyperbolic sine of @a z. 109 template<typename _Tp> complex<_Tp> sinh(const complex<_Tp>&); 110 /// Return complex square root of @a z. 111 template<typename _Tp> complex<_Tp> sqrt(const complex<_Tp>&); 112 /// Return complex tangent of @a z. 113 template<typename _Tp> complex<_Tp> tan(const complex<_Tp>&); 114 /// Return complex hyperbolic tangent of @a z. 115 template<typename _Tp> complex<_Tp> tanh(const complex<_Tp>&); 116 117 118 // 26.2.2 Primary template class complex 119 /** 120 * Template to represent complex numbers. 121 * 122 * Specializations for float, double, and long double are part of the 123 * library. Results with any other type are not guaranteed. 124 * 125 * @param Tp Type of real and imaginary values. 126 */ 127 template<typename _Tp> 128 struct complex 129 { 130 /// Value typedef. 131 typedef _Tp value_type; 132 133 /// Default constructor. First parameter is x, second parameter is y. 134 /// Unspecified parameters default to 0. 135 _GLIBCXX_CONSTEXPR complex(const _Tp& __r = _Tp(), const _Tp& __i = _Tp()) 136 : _M_real(__r), _M_imag(__i) { } 137 138 // Lets the compiler synthesize the copy constructor 139 // complex (const complex<_Tp>&); 140 /// Copy constructor. 141 template<typename _Up> 142 _GLIBCXX_CONSTEXPR complex(const complex<_Up>& __z) 143 : _M_real(__z.real()), _M_imag(__z.imag()) { } 144 145 #if __cplusplus >= 201103L 146 // _GLIBCXX_RESOLVE_LIB_DEFECTS 147 // DR 387. std::complex over-encapsulated. 148 _GLIBCXX_ABI_TAG_CXX11 149 constexpr _Tp 150 real() const { return _M_real; } 151 152 _GLIBCXX_ABI_TAG_CXX11 153 constexpr _Tp 154 imag() const { return _M_imag; } 155 #else 156 /// Return real part of complex number. 157 _Tp& 158 real() { return _M_real; } 159 160 /// Return real part of complex number. 161 const _Tp& 162 real() const { return _M_real; } 163 164 /// Return imaginary part of complex number. 165 _Tp& 166 imag() { return _M_imag; } 167 168 /// Return imaginary part of complex number. 169 const _Tp& 170 imag() const { return _M_imag; } 171 #endif 172 173 // _GLIBCXX_RESOLVE_LIB_DEFECTS 174 // DR 387. std::complex over-encapsulated. 175 void 176 real(_Tp __val) { _M_real = __val; } 177 178 void 179 imag(_Tp __val) { _M_imag = __val; } 180 181 /// Assign this complex number to scalar @a t. 182 complex<_Tp>& operator=(const _Tp&); 183 184 /// Add @a t to this complex number. 185 // 26.2.5/1 186 complex<_Tp>& 187 operator+=(const _Tp& __t) 188 { 189 _M_real += __t; 190 return *this; 191 } 192 193 /// Subtract @a t from this complex number. 194 // 26.2.5/3 195 complex<_Tp>& 196 operator-=(const _Tp& __t) 197 { 198 _M_real -= __t; 199 return *this; 200 } 201 202 /// Multiply this complex number by @a t. 203 complex<_Tp>& operator*=(const _Tp&); 204 /// Divide this complex number by @a t. 205 complex<_Tp>& operator/=(const _Tp&); 206 207 // Lets the compiler synthesize the 208 // copy and assignment operator 209 // complex<_Tp>& operator= (const complex<_Tp>&); 210 /// Assign this complex number to complex @a z. 211 template<typename _Up> 212 complex<_Tp>& operator=(const complex<_Up>&); 213 /// Add @a z to this complex number. 214 template<typename _Up> 215 complex<_Tp>& operator+=(const complex<_Up>&); 216 /// Subtract @a z from this complex number. 217 template<typename _Up> 218 complex<_Tp>& operator-=(const complex<_Up>&); 219 /// Multiply this complex number by @a z. 220 template<typename _Up> 221 complex<_Tp>& operator*=(const complex<_Up>&); 222 /// Divide this complex number by @a z. 223 template<typename _Up> 224 complex<_Tp>& operator/=(const complex<_Up>&); 225 226 _GLIBCXX_USE_CONSTEXPR complex __rep() const 227 { return *this; } 228 229 private: 230 _Tp _M_real; 231 _Tp _M_imag; 232 }; 233 234 template<typename _Tp> 235 complex<_Tp>& 236 complex<_Tp>::operator=(const _Tp& __t) 237 { 238 _M_real = __t; 239 _M_imag = _Tp(); 240 return *this; 241 } 242 243 // 26.2.5/5 244 template<typename _Tp> 245 complex<_Tp>& 246 complex<_Tp>::operator*=(const _Tp& __t) 247 { 248 _M_real *= __t; 249 _M_imag *= __t; 250 return *this; 251 } 252 253 // 26.2.5/7 254 template<typename _Tp> 255 complex<_Tp>& 256 complex<_Tp>::operator/=(const _Tp& __t) 257 { 258 _M_real /= __t; 259 _M_imag /= __t; 260 return *this; 261 } 262 263 template<typename _Tp> 264 template<typename _Up> 265 complex<_Tp>& 266 complex<_Tp>::operator=(const complex<_Up>& __z) 267 { 268 _M_real = __z.real(); 269 _M_imag = __z.imag(); 270 return *this; 271 } 272 273 // 26.2.5/9 274 template<typename _Tp> 275 template<typename _Up> 276 complex<_Tp>& 277 complex<_Tp>::operator+=(const complex<_Up>& __z) 278 { 279 _M_real += __z.real(); 280 _M_imag += __z.imag(); 281 return *this; 282 } 283 284 // 26.2.5/11 285 template<typename _Tp> 286 template<typename _Up> 287 complex<_Tp>& 288 complex<_Tp>::operator-=(const complex<_Up>& __z) 289 { 290 _M_real -= __z.real(); 291 _M_imag -= __z.imag(); 292 return *this; 293 } 294 295 // 26.2.5/13 296 // XXX: This is a grammar school implementation. 297 template<typename _Tp> 298 template<typename _Up> 299 complex<_Tp>& 300 complex<_Tp>::operator*=(const complex<_Up>& __z) 301 { 302 const _Tp __r = _M_real * __z.real() - _M_imag * __z.imag(); 303 _M_imag = _M_real * __z.imag() + _M_imag * __z.real(); 304 _M_real = __r; 305 return *this; 306 } 307 308 // 26.2.5/15 309 // XXX: This is a grammar school implementation. 310 template<typename _Tp> 311 template<typename _Up> 312 complex<_Tp>& 313 complex<_Tp>::operator/=(const complex<_Up>& __z) 314 { 315 const _Tp __r = _M_real * __z.real() + _M_imag * __z.imag(); 316 const _Tp __n = std::norm(__z); 317 _M_imag = (_M_imag * __z.real() - _M_real * __z.imag()) / __n; 318 _M_real = __r / __n; 319 return *this; 320 } 321 322 // Operators: 323 //@{ 324 /// Return new complex value @a x plus @a y. 325 template<typename _Tp> 326 inline complex<_Tp> 327 operator+(const complex<_Tp>& __x, const complex<_Tp>& __y) 328 { 329 complex<_Tp> __r = __x; 330 __r += __y; 331 return __r; 332 } 333 334 template<typename _Tp> 335 inline complex<_Tp> 336 operator+(const complex<_Tp>& __x, const _Tp& __y) 337 { 338 complex<_Tp> __r = __x; 339 __r += __y; 340 return __r; 341 } 342 343 template<typename _Tp> 344 inline complex<_Tp> 345 operator+(const _Tp& __x, const complex<_Tp>& __y) 346 { 347 complex<_Tp> __r = __y; 348 __r += __x; 349 return __r; 350 } 351 //@} 352 353 //@{ 354 /// Return new complex value @a x minus @a y. 355 template<typename _Tp> 356 inline complex<_Tp> 357 operator-(const complex<_Tp>& __x, const complex<_Tp>& __y) 358 { 359 complex<_Tp> __r = __x; 360 __r -= __y; 361 return __r; 362 } 363 364 template<typename _Tp> 365 inline complex<_Tp> 366 operator-(const complex<_Tp>& __x, const _Tp& __y) 367 { 368 complex<_Tp> __r = __x; 369 __r -= __y; 370 return __r; 371 } 372 373 template<typename _Tp> 374 inline complex<_Tp> 375 operator-(const _Tp& __x, const complex<_Tp>& __y) 376 { 377 complex<_Tp> __r(__x, -__y.imag()); 378 __r -= __y.real(); 379 return __r; 380 } 381 //@} 382 383 //@{ 384 /// Return new complex value @a x times @a y. 385 template<typename _Tp> 386 inline complex<_Tp> 387 operator*(const complex<_Tp>& __x, const complex<_Tp>& __y) 388 { 389 complex<_Tp> __r = __x; 390 __r *= __y; 391 return __r; 392 } 393 394 template<typename _Tp> 395 inline complex<_Tp> 396 operator*(const complex<_Tp>& __x, const _Tp& __y) 397 { 398 complex<_Tp> __r = __x; 399 __r *= __y; 400 return __r; 401 } 402 403 template<typename _Tp> 404 inline complex<_Tp> 405 operator*(const _Tp& __x, const complex<_Tp>& __y) 406 { 407 complex<_Tp> __r = __y; 408 __r *= __x; 409 return __r; 410 } 411 //@} 412 413 //@{ 414 /// Return new complex value @a x divided by @a y. 415 template<typename _Tp> 416 inline complex<_Tp> 417 operator/(const complex<_Tp>& __x, const complex<_Tp>& __y) 418 { 419 complex<_Tp> __r = __x; 420 __r /= __y; 421 return __r; 422 } 423 424 template<typename _Tp> 425 inline complex<_Tp> 426 operator/(const complex<_Tp>& __x, const _Tp& __y) 427 { 428 complex<_Tp> __r = __x; 429 __r /= __y; 430 return __r; 431 } 432 433 template<typename _Tp> 434 inline complex<_Tp> 435 operator/(const _Tp& __x, const complex<_Tp>& __y) 436 { 437 complex<_Tp> __r = __x; 438 __r /= __y; 439 return __r; 440 } 441 //@} 442 443 /// Return @a x. 444 template<typename _Tp> 445 inline complex<_Tp> 446 operator+(const complex<_Tp>& __x) 447 { return __x; } 448 449 /// Return complex negation of @a x. 450 template<typename _Tp> 451 inline complex<_Tp> 452 operator-(const complex<_Tp>& __x) 453 { return complex<_Tp>(-__x.real(), -__x.imag()); } 454 455 //@{ 456 /// Return true if @a x is equal to @a y. 457 template<typename _Tp> 458 inline _GLIBCXX_CONSTEXPR bool 459 operator==(const complex<_Tp>& __x, const complex<_Tp>& __y) 460 { return __x.real() == __y.real() && __x.imag() == __y.imag(); } 461 462 template<typename _Tp> 463 inline _GLIBCXX_CONSTEXPR bool 464 operator==(const complex<_Tp>& __x, const _Tp& __y) 465 { return __x.real() == __y && __x.imag() == _Tp(); } 466 467 template<typename _Tp> 468 inline _GLIBCXX_CONSTEXPR bool 469 operator==(const _Tp& __x, const complex<_Tp>& __y) 470 { return __x == __y.real() && _Tp() == __y.imag(); } 471 //@} 472 473 //@{ 474 /// Return false 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 template<typename _Tp> 486 inline _GLIBCXX_CONSTEXPR bool 487 operator!=(const _Tp& __x, const complex<_Tp>& __y) 488 { return __x != __y.real() || _Tp() != __y.imag(); } 489 //@} 490 491 /// Extraction operator for complex values. 492 template<typename _Tp, typename _CharT, class _Traits> 493 basic_istream<_CharT, _Traits>& 494 operator>>(basic_istream<_CharT, _Traits>& __is, complex<_Tp>& __x) 495 { 496 _Tp __re_x, __im_x; 497 _CharT __ch; 498 __is >> __ch; 499 if (__ch == '(') 500 { 501 __is >> __re_x >> __ch; 502 if (__ch == ',') 503 { 504 __is >> __im_x >> __ch; 505 if (__ch == ')') 506 __x = complex<_Tp>(__re_x, __im_x); 507 else 508 __is.setstate(ios_base::failbit); 509 } 510 else if (__ch == ')') 511 __x = __re_x; 512 else 513 __is.setstate(ios_base::failbit); 514 } 515 else 516 { 517 __is.putback(__ch); 518 __is >> __re_x; 519 __x = __re_x; 520 } 521 return __is; 522 } 523 524 /// Insertion operator for complex values. 525 template<typename _Tp, typename _CharT, class _Traits> 526 basic_ostream<_CharT, _Traits>& 527 operator<<(basic_ostream<_CharT, _Traits>& __os, const complex<_Tp>& __x) 528 { 529 basic_ostringstream<_CharT, _Traits> __s; 530 __s.flags(__os.flags()); 531 __s.imbue(__os.getloc()); 532 __s.precision(__os.precision()); 533 __s << '(' << __x.real() << ',' << __x.imag() << ')'; 534 return __os << __s.str(); 535 } 536 537 // Values 538 #if __cplusplus >= 201103L 539 template<typename _Tp> 540 constexpr _Tp 541 real(const complex<_Tp>& __z) 542 { return __z.real(); } 543 544 template<typename _Tp> 545 constexpr _Tp 546 imag(const complex<_Tp>& __z) 547 { return __z.imag(); } 548 #else 549 template<typename _Tp> 550 inline _Tp& 551 real(complex<_Tp>& __z) 552 { return __z.real(); } 553 554 template<typename _Tp> 555 inline const _Tp& 556 real(const complex<_Tp>& __z) 557 { return __z.real(); } 558 559 template<typename _Tp> 560 inline _Tp& 561 imag(complex<_Tp>& __z) 562 { return __z.imag(); } 563 564 template<typename _Tp> 565 inline const _Tp& 566 imag(const complex<_Tp>& __z) 567 { return __z.imag(); } 568 #endif 569 570 // 26.2.7/3 abs(__z): Returns the magnitude of __z. 571 template<typename _Tp> 572 inline _Tp 573 __complex_abs(const complex<_Tp>& __z) 574 { 575 _Tp __x = __z.real(); 576 _Tp __y = __z.imag(); 577 const _Tp __s = std::max(abs(__x), abs(__y)); 578 if (__s == _Tp()) // well ... 579 return __s; 580 __x /= __s; 581 __y /= __s; 582 return __s * sqrt(__x * __x + __y * __y); 583 } 584 585 #if _GLIBCXX_USE_C99_COMPLEX 586 inline float 587 __complex_abs(__complex__ float __z) { return __builtin_cabsf(__z); } 588 589 inline double 590 __complex_abs(__complex__ double __z) { return __builtin_cabs(__z); } 591 592 inline long double 593 __complex_abs(const __complex__ long double& __z) 594 { return __builtin_cabsl(__z); } 595 596 template<typename _Tp> 597 inline _Tp 598 abs(const complex<_Tp>& __z) { return __complex_abs(__z.__rep()); } 599 #else 600 template<typename _Tp> 601 inline _Tp 602 abs(const complex<_Tp>& __z) { return __complex_abs(__z); } 603 #endif 604 605 606 // 26.2.7/4: arg(__z): Returns the phase angle of __z. 607 template<typename _Tp> 608 inline _Tp 609 __complex_arg(const complex<_Tp>& __z) 610 { return atan2(__z.imag(), __z.real()); } 611 612 #if _GLIBCXX_USE_C99_COMPLEX 613 inline float 614 __complex_arg(__complex__ float __z) { return __builtin_cargf(__z); } 615 616 inline double 617 __complex_arg(__complex__ double __z) { return __builtin_carg(__z); } 618 619 inline long double 620 __complex_arg(const __complex__ long double& __z) 621 { return __builtin_cargl(__z); } 622 623 template<typename _Tp> 624 inline _Tp 625 arg(const complex<_Tp>& __z) { return __complex_arg(__z.__rep()); } 626 #else 627 template<typename _Tp> 628 inline _Tp 629 arg(const complex<_Tp>& __z) { return __complex_arg(__z); } 630 #endif 631 632 // 26.2.7/5: norm(__z) returns the squared magnitude of __z. 633 // As defined, norm() is -not- a norm is the common mathematical 634 // sens used in numerics. The helper class _Norm_helper<> tries to 635 // distinguish between builtin floating point and the rest, so as 636 // to deliver an answer as close as possible to the real value. 637 template<bool> 638 struct _Norm_helper 639 { 640 template<typename _Tp> 641 static inline _Tp _S_do_it(const complex<_Tp>& __z) 642 { 643 const _Tp __x = __z.real(); 644 const _Tp __y = __z.imag(); 645 return __x * __x + __y * __y; 646 } 647 }; 648 649 template<> 650 struct _Norm_helper<true> 651 { 652 template<typename _Tp> 653 static inline _Tp _S_do_it(const complex<_Tp>& __z) 654 { 655 _Tp __res = std::abs(__z); 656 return __res * __res; 657 } 658 }; 659 660 template<typename _Tp> 661 inline _Tp 662 norm(const complex<_Tp>& __z) 663 { 664 return _Norm_helper<__is_floating<_Tp>::__value 665 && !_GLIBCXX_FAST_MATH>::_S_do_it(__z); 666 } 667 668 template<typename _Tp> 669 inline complex<_Tp> 670 polar(const _Tp& __rho, const _Tp& __theta) 671 { return complex<_Tp>(__rho * cos(__theta), __rho * sin(__theta)); } 672 673 template<typename _Tp> 674 inline complex<_Tp> 675 conj(const complex<_Tp>& __z) 676 { return complex<_Tp>(__z.real(), -__z.imag()); } 677 678 // Transcendentals 679 680 // 26.2.8/1 cos(__z): Returns the cosine of __z. 681 template<typename _Tp> 682 inline complex<_Tp> 683 __complex_cos(const complex<_Tp>& __z) 684 { 685 const _Tp __x = __z.real(); 686 const _Tp __y = __z.imag(); 687 return complex<_Tp>(cos(__x) * cosh(__y), -sin(__x) * sinh(__y)); 688 } 689 690 #if _GLIBCXX_USE_C99_COMPLEX 691 inline __complex__ float 692 __complex_cos(__complex__ float __z) { return __builtin_ccosf(__z); } 693 694 inline __complex__ double 695 __complex_cos(__complex__ double __z) { return __builtin_ccos(__z); } 696 697 inline __complex__ long double 698 __complex_cos(const __complex__ long double& __z) 699 { return __builtin_ccosl(__z); } 700 701 template<typename _Tp> 702 inline complex<_Tp> 703 cos(const complex<_Tp>& __z) { return __complex_cos(__z.__rep()); } 704 #else 705 template<typename _Tp> 706 inline complex<_Tp> 707 cos(const complex<_Tp>& __z) { return __complex_cos(__z); } 708 #endif 709 710 // 26.2.8/2 cosh(__z): Returns the hyperbolic cosine of __z. 711 template<typename _Tp> 712 inline complex<_Tp> 713 __complex_cosh(const complex<_Tp>& __z) 714 { 715 const _Tp __x = __z.real(); 716 const _Tp __y = __z.imag(); 717 return complex<_Tp>(cosh(__x) * cos(__y), sinh(__x) * sin(__y)); 718 } 719 720 #if _GLIBCXX_USE_C99_COMPLEX 721 inline __complex__ float 722 __complex_cosh(__complex__ float __z) { return __builtin_ccoshf(__z); } 723 724 inline __complex__ double 725 __complex_cosh(__complex__ double __z) { return __builtin_ccosh(__z); } 726 727 inline __complex__ long double 728 __complex_cosh(const __complex__ long double& __z) 729 { return __builtin_ccoshl(__z); } 730 731 template<typename _Tp> 732 inline complex<_Tp> 733 cosh(const complex<_Tp>& __z) { return __complex_cosh(__z.__rep()); } 734 #else 735 template<typename _Tp> 736 inline complex<_Tp> 737 cosh(const complex<_Tp>& __z) { return __complex_cosh(__z); } 738 #endif 739 740 // 26.2.8/3 exp(__z): Returns the complex base e exponential of x 741 template<typename _Tp> 742 inline complex<_Tp> 743 __complex_exp(const complex<_Tp>& __z) 744 { return std::polar(exp(__z.real()), __z.imag()); } 745 746 #if _GLIBCXX_USE_C99_COMPLEX 747 inline __complex__ float 748 __complex_exp(__complex__ float __z) { return __builtin_cexpf(__z); } 749 750 inline __complex__ double 751 __complex_exp(__complex__ double __z) { return __builtin_cexp(__z); } 752 753 inline __complex__ long double 754 __complex_exp(const __complex__ long double& __z) 755 { return __builtin_cexpl(__z); } 756 757 template<typename _Tp> 758 inline complex<_Tp> 759 exp(const complex<_Tp>& __z) { return __complex_exp(__z.__rep()); } 760 #else 761 template<typename _Tp> 762 inline complex<_Tp> 763 exp(const complex<_Tp>& __z) { return __complex_exp(__z); } 764 #endif 765 766 // 26.2.8/5 log(__z): Returns the natural complex logarithm of __z. 767 // The branch cut is along the negative axis. 768 template<typename _Tp> 769 inline complex<_Tp> 770 __complex_log(const complex<_Tp>& __z) 771 { return complex<_Tp>(log(std::abs(__z)), std::arg(__z)); } 772 773 #if _GLIBCXX_USE_C99_COMPLEX 774 inline __complex__ float 775 __complex_log(__complex__ float __z) { return __builtin_clogf(__z); } 776 777 inline __complex__ double 778 __complex_log(__complex__ double __z) { return __builtin_clog(__z); } 779 780 inline __complex__ long double 781 __complex_log(const __complex__ long double& __z) 782 { return __builtin_clogl(__z); } 783 784 template<typename _Tp> 785 inline complex<_Tp> 786 log(const complex<_Tp>& __z) { return __complex_log(__z.__rep()); } 787 #else 788 template<typename _Tp> 789 inline complex<_Tp> 790 log(const complex<_Tp>& __z) { return __complex_log(__z); } 791 #endif 792 793 template<typename _Tp> 794 inline complex<_Tp> 795 log10(const complex<_Tp>& __z) 796 { return std::log(__z) / log(_Tp(10.0)); } 797 798 // 26.2.8/10 sin(__z): Returns the sine of __z. 799 template<typename _Tp> 800 inline complex<_Tp> 801 __complex_sin(const complex<_Tp>& __z) 802 { 803 const _Tp __x = __z.real(); 804 const _Tp __y = __z.imag(); 805 return complex<_Tp>(sin(__x) * cosh(__y), cos(__x) * sinh(__y)); 806 } 807 808 #if _GLIBCXX_USE_C99_COMPLEX 809 inline __complex__ float 810 __complex_sin(__complex__ float __z) { return __builtin_csinf(__z); } 811 812 inline __complex__ double 813 __complex_sin(__complex__ double __z) { return __builtin_csin(__z); } 814 815 inline __complex__ long double 816 __complex_sin(const __complex__ long double& __z) 817 { return __builtin_csinl(__z); } 818 819 template<typename _Tp> 820 inline complex<_Tp> 821 sin(const complex<_Tp>& __z) { return __complex_sin(__z.__rep()); } 822 #else 823 template<typename _Tp> 824 inline complex<_Tp> 825 sin(const complex<_Tp>& __z) { return __complex_sin(__z); } 826 #endif 827 828 // 26.2.8/11 sinh(__z): Returns the hyperbolic sine of __z. 829 template<typename _Tp> 830 inline complex<_Tp> 831 __complex_sinh(const complex<_Tp>& __z) 832 { 833 const _Tp __x = __z.real(); 834 const _Tp __y = __z.imag(); 835 return complex<_Tp>(sinh(__x) * cos(__y), cosh(__x) * sin(__y)); 836 } 837 838 #if _GLIBCXX_USE_C99_COMPLEX 839 inline __complex__ float 840 __complex_sinh(__complex__ float __z) { return __builtin_csinhf(__z); } 841 842 inline __complex__ double 843 __complex_sinh(__complex__ double __z) { return __builtin_csinh(__z); } 844 845 inline __complex__ long double 846 __complex_sinh(const __complex__ long double& __z) 847 { return __builtin_csinhl(__z); } 848 849 template<typename _Tp> 850 inline complex<_Tp> 851 sinh(const complex<_Tp>& __z) { return __complex_sinh(__z.__rep()); } 852 #else 853 template<typename _Tp> 854 inline complex<_Tp> 855 sinh(const complex<_Tp>& __z) { return __complex_sinh(__z); } 856 #endif 857 858 // 26.2.8/13 sqrt(__z): Returns the complex square root of __z. 859 // The branch cut is on the negative axis. 860 template<typename _Tp> 861 complex<_Tp> 862 __complex_sqrt(const complex<_Tp>& __z) 863 { 864 _Tp __x = __z.real(); 865 _Tp __y = __z.imag(); 866 867 if (__x == _Tp()) 868 { 869 _Tp __t = sqrt(abs(__y) / 2); 870 return complex<_Tp>(__t, __y < _Tp() ? -__t : __t); 871 } 872 else 873 { 874 _Tp __t = sqrt(2 * (std::abs(__z) + abs(__x))); 875 _Tp __u = __t / 2; 876 return __x > _Tp() 877 ? complex<_Tp>(__u, __y / __t) 878 : complex<_Tp>(abs(__y) / __t, __y < _Tp() ? -__u : __u); 879 } 880 } 881 882 #if _GLIBCXX_USE_C99_COMPLEX 883 inline __complex__ float 884 __complex_sqrt(__complex__ float __z) { return __builtin_csqrtf(__z); } 885 886 inline __complex__ double 887 __complex_sqrt(__complex__ double __z) { return __builtin_csqrt(__z); } 888 889 inline __complex__ long double 890 __complex_sqrt(const __complex__ long double& __z) 891 { return __builtin_csqrtl(__z); } 892 893 template<typename _Tp> 894 inline complex<_Tp> 895 sqrt(const complex<_Tp>& __z) { return __complex_sqrt(__z.__rep()); } 896 #else 897 template<typename _Tp> 898 inline complex<_Tp> 899 sqrt(const complex<_Tp>& __z) { return __complex_sqrt(__z); } 900 #endif 901 902 // 26.2.8/14 tan(__z): Return the complex tangent of __z. 903 904 template<typename _Tp> 905 inline complex<_Tp> 906 __complex_tan(const complex<_Tp>& __z) 907 { return std::sin(__z) / std::cos(__z); } 908 909 #if _GLIBCXX_USE_C99_COMPLEX 910 inline __complex__ float 911 __complex_tan(__complex__ float __z) { return __builtin_ctanf(__z); } 912 913 inline __complex__ double 914 __complex_tan(__complex__ double __z) { return __builtin_ctan(__z); } 915 916 inline __complex__ long double 917 __complex_tan(const __complex__ long double& __z) 918 { return __builtin_ctanl(__z); } 919 920 template<typename _Tp> 921 inline complex<_Tp> 922 tan(const complex<_Tp>& __z) { return __complex_tan(__z.__rep()); } 923 #else 924 template<typename _Tp> 925 inline complex<_Tp> 926 tan(const complex<_Tp>& __z) { return __complex_tan(__z); } 927 #endif 928 929 930 // 26.2.8/15 tanh(__z): Returns the hyperbolic tangent of __z. 931 932 template<typename _Tp> 933 inline complex<_Tp> 934 __complex_tanh(const complex<_Tp>& __z) 935 { return std::sinh(__z) / std::cosh(__z); } 936 937 #if _GLIBCXX_USE_C99_COMPLEX 938 inline __complex__ float 939 __complex_tanh(__complex__ float __z) { return __builtin_ctanhf(__z); } 940 941 inline __complex__ double 942 __complex_tanh(__complex__ double __z) { return __builtin_ctanh(__z); } 943 944 inline __complex__ long double 945 __complex_tanh(const __complex__ long double& __z) 946 { return __builtin_ctanhl(__z); } 947 948 template<typename _Tp> 949 inline complex<_Tp> 950 tanh(const complex<_Tp>& __z) { return __complex_tanh(__z.__rep()); } 951 #else 952 template<typename _Tp> 953 inline complex<_Tp> 954 tanh(const complex<_Tp>& __z) { return __complex_tanh(__z); } 955 #endif 956 957 958 // 26.2.8/9 pow(__x, __y): Returns the complex power base of __x 959 // raised to the __y-th power. The branch 960 // cut is on the negative axis. 961 template<typename _Tp> 962 complex<_Tp> 963 __complex_pow_unsigned(complex<_Tp> __x, unsigned __n) 964 { 965 complex<_Tp> __y = __n % 2 ? __x : complex<_Tp>(1); 966 967 while (__n >>= 1) 968 { 969 __x *= __x; 970 if (__n % 2) 971 __y *= __x; 972 } 973 974 return __y; 975 } 976 977 // In C++11 mode we used to implement the resolution of 978 // DR 844. complex pow return type is ambiguous. 979 // thus the following overload was disabled in that mode. However, doing 980 // that causes all sorts of issues, see, for example: 981 // http://gcc.gnu.org/ml/libstdc++/2013-01/msg00058.html 982 // and also PR57974. 983 template<typename _Tp> 984 inline complex<_Tp> 985 pow(const complex<_Tp>& __z, int __n) 986 { 987 return __n < 0 988 ? complex<_Tp>(1) / std::__complex_pow_unsigned(__z, -(unsigned)__n) 989 : std::__complex_pow_unsigned(__z, __n); 990 } 991 992 template<typename _Tp> 993 complex<_Tp> 994 pow(const complex<_Tp>& __x, const _Tp& __y) 995 { 996 #ifndef _GLIBCXX_USE_C99_COMPLEX 997 if (__x == _Tp()) 998 return _Tp(); 999 #endif 1000 if (__x.imag() == _Tp() && __x.real() > _Tp()) 1001 return pow(__x.real(), __y); 1002 1003 complex<_Tp> __t = std::log(__x); 1004 return std::polar(exp(__y * __t.real()), __y * __t.imag()); 1005 } 1006 1007 template<typename _Tp> 1008 inline complex<_Tp> 1009 __complex_pow(const complex<_Tp>& __x, const complex<_Tp>& __y) 1010 { return __x == _Tp() ? _Tp() : std::exp(__y * std::log(__x)); } 1011 1012 #if _GLIBCXX_USE_C99_COMPLEX 1013 inline __complex__ float 1014 __complex_pow(__complex__ float __x, __complex__ float __y) 1015 { return __builtin_cpowf(__x, __y); } 1016 1017 inline __complex__ double 1018 __complex_pow(__complex__ double __x, __complex__ double __y) 1019 { return __builtin_cpow(__x, __y); } 1020 1021 inline __complex__ long double 1022 __complex_pow(const __complex__ long double& __x, 1023 const __complex__ long double& __y) 1024 { return __builtin_cpowl(__x, __y); } 1025 1026 template<typename _Tp> 1027 inline complex<_Tp> 1028 pow(const complex<_Tp>& __x, const complex<_Tp>& __y) 1029 { return __complex_pow(__x.__rep(), __y.__rep()); } 1030 #else 1031 template<typename _Tp> 1032 inline complex<_Tp> 1033 pow(const complex<_Tp>& __x, const complex<_Tp>& __y) 1034 { return __complex_pow(__x, __y); } 1035 #endif 1036 1037 template<typename _Tp> 1038 inline complex<_Tp> 1039 pow(const _Tp& __x, const complex<_Tp>& __y) 1040 { 1041 return __x > _Tp() ? std::polar(pow(__x, __y.real()), 1042 __y.imag() * log(__x)) 1043 : std::pow(complex<_Tp>(__x), __y); 1044 } 1045 1046 /// 26.2.3 complex specializations 1047 /// complex<float> specialization 1048 template<> 1049 struct complex<float> 1050 { 1051 typedef float value_type; 1052 typedef __complex__ float _ComplexT; 1053 1054 _GLIBCXX_CONSTEXPR complex(_ComplexT __z) : _M_value(__z) { } 1055 1056 _GLIBCXX_CONSTEXPR complex(float __r = 0.0f, float __i = 0.0f) 1057 #if __cplusplus >= 201103L 1058 : _M_value{ __r, __i } { } 1059 #else 1060 { 1061 __real__ _M_value = __r; 1062 __imag__ _M_value = __i; 1063 } 1064 #endif 1065 1066 explicit _GLIBCXX_CONSTEXPR complex(const complex<double>&); 1067 explicit _GLIBCXX_CONSTEXPR complex(const complex<long double>&); 1068 1069 #if __cplusplus >= 201103L 1070 // _GLIBCXX_RESOLVE_LIB_DEFECTS 1071 // DR 387. std::complex over-encapsulated. 1072 __attribute ((__abi_tag__ ("cxx11"))) 1073 constexpr float 1074 real() const { return __real__ _M_value; } 1075 1076 __attribute ((__abi_tag__ ("cxx11"))) 1077 constexpr float 1078 imag() const { return __imag__ _M_value; } 1079 #else 1080 float& 1081 real() { return __real__ _M_value; } 1082 1083 const float& 1084 real() const { return __real__ _M_value; } 1085 1086 float& 1087 imag() { return __imag__ _M_value; } 1088 1089 const float& 1090 imag() const { return __imag__ _M_value; } 1091 #endif 1092 1093 // _GLIBCXX_RESOLVE_LIB_DEFECTS 1094 // DR 387. std::complex over-encapsulated. 1095 void 1096 real(float __val) { __real__ _M_value = __val; } 1097 1098 void 1099 imag(float __val) { __imag__ _M_value = __val; } 1100 1101 complex& 1102 operator=(float __f) 1103 { 1104 _M_value = __f; 1105 return *this; 1106 } 1107 1108 complex& 1109 operator+=(float __f) 1110 { 1111 _M_value += __f; 1112 return *this; 1113 } 1114 1115 complex& 1116 operator-=(float __f) 1117 { 1118 _M_value -= __f; 1119 return *this; 1120 } 1121 1122 complex& 1123 operator*=(float __f) 1124 { 1125 _M_value *= __f; 1126 return *this; 1127 } 1128 1129 complex& 1130 operator/=(float __f) 1131 { 1132 _M_value /= __f; 1133 return *this; 1134 } 1135 1136 // Let the compiler synthesize the copy and assignment 1137 // operator. It always does a pretty good job. 1138 // complex& operator=(const complex&); 1139 1140 template<typename _Tp> 1141 complex& 1142 operator=(const complex<_Tp>& __z) 1143 { 1144 __real__ _M_value = __z.real(); 1145 __imag__ _M_value = __z.imag(); 1146 return *this; 1147 } 1148 1149 template<typename _Tp> 1150 complex& 1151 operator+=(const complex<_Tp>& __z) 1152 { 1153 __real__ _M_value += __z.real(); 1154 __imag__ _M_value += __z.imag(); 1155 return *this; 1156 } 1157 1158 template<class _Tp> 1159 complex& 1160 operator-=(const complex<_Tp>& __z) 1161 { 1162 __real__ _M_value -= __z.real(); 1163 __imag__ _M_value -= __z.imag(); 1164 return *this; 1165 } 1166 1167 template<class _Tp> 1168 complex& 1169 operator*=(const complex<_Tp>& __z) 1170 { 1171 _ComplexT __t; 1172 __real__ __t = __z.real(); 1173 __imag__ __t = __z.imag(); 1174 _M_value *= __t; 1175 return *this; 1176 } 1177 1178 template<class _Tp> 1179 complex& 1180 operator/=(const complex<_Tp>& __z) 1181 { 1182 _ComplexT __t; 1183 __real__ __t = __z.real(); 1184 __imag__ __t = __z.imag(); 1185 _M_value /= __t; 1186 return *this; 1187 } 1188 1189 _GLIBCXX_USE_CONSTEXPR _ComplexT __rep() const { return _M_value; } 1190 1191 private: 1192 _ComplexT _M_value; 1193 }; 1194 1195 /// 26.2.3 complex specializations 1196 /// complex<double> specialization 1197 template<> 1198 struct complex<double> 1199 { 1200 typedef double value_type; 1201 typedef __complex__ double _ComplexT; 1202 1203 _GLIBCXX_CONSTEXPR complex(_ComplexT __z) : _M_value(__z) { } 1204 1205 _GLIBCXX_CONSTEXPR complex(double __r = 0.0, double __i = 0.0) 1206 #if __cplusplus >= 201103L 1207 : _M_value{ __r, __i } { } 1208 #else 1209 { 1210 __real__ _M_value = __r; 1211 __imag__ _M_value = __i; 1212 } 1213 #endif 1214 1215 _GLIBCXX_CONSTEXPR complex(const complex<float>& __z) 1216 : _M_value(__z.__rep()) { } 1217 1218 explicit _GLIBCXX_CONSTEXPR complex(const complex<long double>&); 1219 1220 #if __cplusplus >= 201103L 1221 // _GLIBCXX_RESOLVE_LIB_DEFECTS 1222 // DR 387. std::complex over-encapsulated. 1223 __attribute ((__abi_tag__ ("cxx11"))) 1224 constexpr double 1225 real() const { return __real__ _M_value; } 1226 1227 __attribute ((__abi_tag__ ("cxx11"))) 1228 constexpr double 1229 imag() const { return __imag__ _M_value; } 1230 #else 1231 double& 1232 real() { return __real__ _M_value; } 1233 1234 const double& 1235 real() const { return __real__ _M_value; } 1236 1237 double& 1238 imag() { return __imag__ _M_value; } 1239 1240 const double& 1241 imag() const { return __imag__ _M_value; } 1242 #endif 1243 1244 // _GLIBCXX_RESOLVE_LIB_DEFECTS 1245 // DR 387. std::complex over-encapsulated. 1246 void 1247 real(double __val) { __real__ _M_value = __val; } 1248 1249 void 1250 imag(double __val) { __imag__ _M_value = __val; } 1251 1252 complex& 1253 operator=(double __d) 1254 { 1255 _M_value = __d; 1256 return *this; 1257 } 1258 1259 complex& 1260 operator+=(double __d) 1261 { 1262 _M_value += __d; 1263 return *this; 1264 } 1265 1266 complex& 1267 operator-=(double __d) 1268 { 1269 _M_value -= __d; 1270 return *this; 1271 } 1272 1273 complex& 1274 operator*=(double __d) 1275 { 1276 _M_value *= __d; 1277 return *this; 1278 } 1279 1280 complex& 1281 operator/=(double __d) 1282 { 1283 _M_value /= __d; 1284 return *this; 1285 } 1286 1287 // The compiler will synthesize this, efficiently. 1288 // complex& operator=(const complex&); 1289 1290 template<typename _Tp> 1291 complex& 1292 operator=(const complex<_Tp>& __z) 1293 { 1294 __real__ _M_value = __z.real(); 1295 __imag__ _M_value = __z.imag(); 1296 return *this; 1297 } 1298 1299 template<typename _Tp> 1300 complex& 1301 operator+=(const complex<_Tp>& __z) 1302 { 1303 __real__ _M_value += __z.real(); 1304 __imag__ _M_value += __z.imag(); 1305 return *this; 1306 } 1307 1308 template<typename _Tp> 1309 complex& 1310 operator-=(const complex<_Tp>& __z) 1311 { 1312 __real__ _M_value -= __z.real(); 1313 __imag__ _M_value -= __z.imag(); 1314 return *this; 1315 } 1316 1317 template<typename _Tp> 1318 complex& 1319 operator*=(const complex<_Tp>& __z) 1320 { 1321 _ComplexT __t; 1322 __real__ __t = __z.real(); 1323 __imag__ __t = __z.imag(); 1324 _M_value *= __t; 1325 return *this; 1326 } 1327 1328 template<typename _Tp> 1329 complex& 1330 operator/=(const complex<_Tp>& __z) 1331 { 1332 _ComplexT __t; 1333 __real__ __t = __z.real(); 1334 __imag__ __t = __z.imag(); 1335 _M_value /= __t; 1336 return *this; 1337 } 1338 1339 _GLIBCXX_USE_CONSTEXPR _ComplexT __rep() const { return _M_value; } 1340 1341 private: 1342 _ComplexT _M_value; 1343 }; 1344 1345 /// 26.2.3 complex specializations 1346 /// complex<long double> specialization 1347 template<> 1348 struct complex<long double> 1349 { 1350 typedef long double value_type; 1351 typedef __complex__ long double _ComplexT; 1352 1353 _GLIBCXX_CONSTEXPR complex(_ComplexT __z) : _M_value(__z) { } 1354 1355 _GLIBCXX_CONSTEXPR complex(long double __r = 0.0L, 1356 long double __i = 0.0L) 1357 #if __cplusplus >= 201103L 1358 : _M_value{ __r, __i } { } 1359 #else 1360 { 1361 __real__ _M_value = __r; 1362 __imag__ _M_value = __i; 1363 } 1364 #endif 1365 1366 _GLIBCXX_CONSTEXPR complex(const complex<float>& __z) 1367 : _M_value(__z.__rep()) { } 1368 1369 _GLIBCXX_CONSTEXPR complex(const complex<double>& __z) 1370 : _M_value(__z.__rep()) { } 1371 1372 #if __cplusplus >= 201103L 1373 // _GLIBCXX_RESOLVE_LIB_DEFECTS 1374 // DR 387. std::complex over-encapsulated. 1375 __attribute ((__abi_tag__ ("cxx11"))) 1376 constexpr long double 1377 real() const { return __real__ _M_value; } 1378 1379 __attribute ((__abi_tag__ ("cxx11"))) 1380 constexpr long double 1381 imag() const { return __imag__ _M_value; } 1382 #else 1383 long double& 1384 real() { return __real__ _M_value; } 1385 1386 const long double& 1387 real() const { return __real__ _M_value; } 1388 1389 long double& 1390 imag() { return __imag__ _M_value; } 1391 1392 const long double& 1393 imag() const { return __imag__ _M_value; } 1394 #endif 1395 1396 // _GLIBCXX_RESOLVE_LIB_DEFECTS 1397 // DR 387. std::complex over-encapsulated. 1398 void 1399 real(long double __val) { __real__ _M_value = __val; } 1400 1401 void 1402 imag(long double __val) { __imag__ _M_value = __val; } 1403 1404 complex& 1405 operator=(long double __r) 1406 { 1407 _M_value = __r; 1408 return *this; 1409 } 1410 1411 complex& 1412 operator+=(long double __r) 1413 { 1414 _M_value += __r; 1415 return *this; 1416 } 1417 1418 complex& 1419 operator-=(long double __r) 1420 { 1421 _M_value -= __r; 1422 return *this; 1423 } 1424 1425 complex& 1426 operator*=(long double __r) 1427 { 1428 _M_value *= __r; 1429 return *this; 1430 } 1431 1432 complex& 1433 operator/=(long double __r) 1434 { 1435 _M_value /= __r; 1436 return *this; 1437 } 1438 1439 // The compiler knows how to do this efficiently 1440 // complex& operator=(const complex&); 1441 1442 template<typename _Tp> 1443 complex& 1444 operator=(const complex<_Tp>& __z) 1445 { 1446 __real__ _M_value = __z.real(); 1447 __imag__ _M_value = __z.imag(); 1448 return *this; 1449 } 1450 1451 template<typename _Tp> 1452 complex& 1453 operator+=(const complex<_Tp>& __z) 1454 { 1455 __real__ _M_value += __z.real(); 1456 __imag__ _M_value += __z.imag(); 1457 return *this; 1458 } 1459 1460 template<typename _Tp> 1461 complex& 1462 operator-=(const complex<_Tp>& __z) 1463 { 1464 __real__ _M_value -= __z.real(); 1465 __imag__ _M_value -= __z.imag(); 1466 return *this; 1467 } 1468 1469 template<typename _Tp> 1470 complex& 1471 operator*=(const complex<_Tp>& __z) 1472 { 1473 _ComplexT __t; 1474 __real__ __t = __z.real(); 1475 __imag__ __t = __z.imag(); 1476 _M_value *= __t; 1477 return *this; 1478 } 1479 1480 template<typename _Tp> 1481 complex& 1482 operator/=(const complex<_Tp>& __z) 1483 { 1484 _ComplexT __t; 1485 __real__ __t = __z.real(); 1486 __imag__ __t = __z.imag(); 1487 _M_value /= __t; 1488 return *this; 1489 } 1490 1491 _GLIBCXX_USE_CONSTEXPR _ComplexT __rep() const { return _M_value; } 1492 1493 private: 1494 _ComplexT _M_value; 1495 }; 1496 1497 // These bits have to be at the end of this file, so that the 1498 // specializations have all been defined. 1499 inline _GLIBCXX_CONSTEXPR 1500 complex<float>::complex(const complex<double>& __z) 1501 : _M_value(__z.__rep()) { } 1502 1503 inline _GLIBCXX_CONSTEXPR 1504 complex<float>::complex(const complex<long double>& __z) 1505 : _M_value(__z.__rep()) { } 1506 1507 inline _GLIBCXX_CONSTEXPR 1508 complex<double>::complex(const complex<long double>& __z) 1509 : _M_value(__z.__rep()) { } 1510 1511 // Inhibit implicit instantiations for required instantiations, 1512 // which are defined via explicit instantiations elsewhere. 1513 // NB: This syntax is a GNU extension. 1514 #if _GLIBCXX_EXTERN_TEMPLATE 1515 extern template istream& operator>>(istream&, complex<float>&); 1516 extern template ostream& operator<<(ostream&, const complex<float>&); 1517 extern template istream& operator>>(istream&, complex<double>&); 1518 extern template ostream& operator<<(ostream&, const complex<double>&); 1519 extern template istream& operator>>(istream&, complex<long double>&); 1520 extern template ostream& operator<<(ostream&, const complex<long double>&); 1521 1522 #ifdef _GLIBCXX_USE_WCHAR_T 1523 extern template wistream& operator>>(wistream&, complex<float>&); 1524 extern template wostream& operator<<(wostream&, const complex<float>&); 1525 extern template wistream& operator>>(wistream&, complex<double>&); 1526 extern template wostream& operator<<(wostream&, const complex<double>&); 1527 extern template wistream& operator>>(wistream&, complex<long double>&); 1528 extern template wostream& operator<<(wostream&, const complex<long double>&); 1529 #endif 1530 #endif 1531 1532 // @} group complex_numbers 1533 1534 _GLIBCXX_END_NAMESPACE_VERSION 1535 } // namespace 1536 1537 namespace __gnu_cxx _GLIBCXX_VISIBILITY(default) 1538 { 1539 _GLIBCXX_BEGIN_NAMESPACE_VERSION 1540 1541 // See ext/type_traits.h for the primary template. 1542 template<typename _Tp, typename _Up> 1543 struct __promote_2<std::complex<_Tp>, _Up> 1544 { 1545 public: 1546 typedef std::complex<typename __promote_2<_Tp, _Up>::__type> __type; 1547 }; 1548 1549 template<typename _Tp, typename _Up> 1550 struct __promote_2<_Tp, std::complex<_Up> > 1551 { 1552 public: 1553 typedef std::complex<typename __promote_2<_Tp, _Up>::__type> __type; 1554 }; 1555 1556 template<typename _Tp, typename _Up> 1557 struct __promote_2<std::complex<_Tp>, std::complex<_Up> > 1558 { 1559 public: 1560 typedef std::complex<typename __promote_2<_Tp, _Up>::__type> __type; 1561 }; 1562 1563 _GLIBCXX_END_NAMESPACE_VERSION 1564 } // namespace 1565 1566 #if __cplusplus >= 201103L 1567 1568 namespace std _GLIBCXX_VISIBILITY(default) 1569 { 1570 _GLIBCXX_BEGIN_NAMESPACE_VERSION 1571 1572 // Forward declarations. 1573 template<typename _Tp> std::complex<_Tp> acos(const std::complex<_Tp>&); 1574 template<typename _Tp> std::complex<_Tp> asin(const std::complex<_Tp>&); 1575 template<typename _Tp> std::complex<_Tp> atan(const std::complex<_Tp>&); 1576 1577 template<typename _Tp> std::complex<_Tp> acosh(const std::complex<_Tp>&); 1578 template<typename _Tp> std::complex<_Tp> asinh(const std::complex<_Tp>&); 1579 template<typename _Tp> std::complex<_Tp> atanh(const std::complex<_Tp>&); 1580 // DR 595. 1581 template<typename _Tp> _Tp fabs(const std::complex<_Tp>&); 1582 1583 template<typename _Tp> 1584 inline std::complex<_Tp> 1585 __complex_acos(const std::complex<_Tp>& __z) 1586 { 1587 const std::complex<_Tp> __t = std::asin(__z); 1588 const _Tp __pi_2 = 1.5707963267948966192313216916397514L; 1589 return std::complex<_Tp>(__pi_2 - __t.real(), -__t.imag()); 1590 } 1591 1592 #if _GLIBCXX_USE_C99_COMPLEX_TR1 1593 inline __complex__ float 1594 __complex_acos(__complex__ float __z) 1595 { return __builtin_cacosf(__z); } 1596 1597 inline __complex__ double 1598 __complex_acos(__complex__ double __z) 1599 { return __builtin_cacos(__z); } 1600 1601 inline __complex__ long double 1602 __complex_acos(const __complex__ long double& __z) 1603 { return __builtin_cacosl(__z); } 1604 1605 template<typename _Tp> 1606 inline std::complex<_Tp> 1607 acos(const std::complex<_Tp>& __z) 1608 { return __complex_acos(__z.__rep()); } 1609 #else 1610 /// acos(__z) [8.1.2]. 1611 // Effects: Behaves the same as C99 function cacos, defined 1612 // in subclause 7.3.5.1. 1613 template<typename _Tp> 1614 inline std::complex<_Tp> 1615 acos(const std::complex<_Tp>& __z) 1616 { return __complex_acos(__z); } 1617 #endif 1618 1619 template<typename _Tp> 1620 inline std::complex<_Tp> 1621 __complex_asin(const std::complex<_Tp>& __z) 1622 { 1623 std::complex<_Tp> __t(-__z.imag(), __z.real()); 1624 __t = std::asinh(__t); 1625 return std::complex<_Tp>(__t.imag(), -__t.real()); 1626 } 1627 1628 #if _GLIBCXX_USE_C99_COMPLEX_TR1 1629 inline __complex__ float 1630 __complex_asin(__complex__ float __z) 1631 { return __builtin_casinf(__z); } 1632 1633 inline __complex__ double 1634 __complex_asin(__complex__ double __z) 1635 { return __builtin_casin(__z); } 1636 1637 inline __complex__ long double 1638 __complex_asin(const __complex__ long double& __z) 1639 { return __builtin_casinl(__z); } 1640 1641 template<typename _Tp> 1642 inline std::complex<_Tp> 1643 asin(const std::complex<_Tp>& __z) 1644 { return __complex_asin(__z.__rep()); } 1645 #else 1646 /// asin(__z) [8.1.3]. 1647 // Effects: Behaves the same as C99 function casin, defined 1648 // in subclause 7.3.5.2. 1649 template<typename _Tp> 1650 inline std::complex<_Tp> 1651 asin(const std::complex<_Tp>& __z) 1652 { return __complex_asin(__z); } 1653 #endif 1654 1655 template<typename _Tp> 1656 std::complex<_Tp> 1657 __complex_atan(const std::complex<_Tp>& __z) 1658 { 1659 const _Tp __r2 = __z.real() * __z.real(); 1660 const _Tp __x = _Tp(1.0) - __r2 - __z.imag() * __z.imag(); 1661 1662 _Tp __num = __z.imag() + _Tp(1.0); 1663 _Tp __den = __z.imag() - _Tp(1.0); 1664 1665 __num = __r2 + __num * __num; 1666 __den = __r2 + __den * __den; 1667 1668 return std::complex<_Tp>(_Tp(0.5) * atan2(_Tp(2.0) * __z.real(), __x), 1669 _Tp(0.25) * log(__num / __den)); 1670 } 1671 1672 #if _GLIBCXX_USE_C99_COMPLEX_TR1 1673 inline __complex__ float 1674 __complex_atan(__complex__ float __z) 1675 { return __builtin_catanf(__z); } 1676 1677 inline __complex__ double 1678 __complex_atan(__complex__ double __z) 1679 { return __builtin_catan(__z); } 1680 1681 inline __complex__ long double 1682 __complex_atan(const __complex__ long double& __z) 1683 { return __builtin_catanl(__z); } 1684 1685 template<typename _Tp> 1686 inline std::complex<_Tp> 1687 atan(const std::complex<_Tp>& __z) 1688 { return __complex_atan(__z.__rep()); } 1689 #else 1690 /// atan(__z) [8.1.4]. 1691 // Effects: Behaves the same as C99 function catan, defined 1692 // in subclause 7.3.5.3. 1693 template<typename _Tp> 1694 inline std::complex<_Tp> 1695 atan(const std::complex<_Tp>& __z) 1696 { return __complex_atan(__z); } 1697 #endif 1698 1699 template<typename _Tp> 1700 std::complex<_Tp> 1701 __complex_acosh(const std::complex<_Tp>& __z) 1702 { 1703 // Kahan's formula. 1704 return _Tp(2.0) * std::log(std::sqrt(_Tp(0.5) * (__z + _Tp(1.0))) 1705 + std::sqrt(_Tp(0.5) * (__z - _Tp(1.0)))); 1706 } 1707 1708 #if _GLIBCXX_USE_C99_COMPLEX_TR1 1709 inline __complex__ float 1710 __complex_acosh(__complex__ float __z) 1711 { return __builtin_cacoshf(__z); } 1712 1713 inline __complex__ double 1714 __complex_acosh(__complex__ double __z) 1715 { return __builtin_cacosh(__z); } 1716 1717 inline __complex__ long double 1718 __complex_acosh(const __complex__ long double& __z) 1719 { return __builtin_cacoshl(__z); } 1720 1721 template<typename _Tp> 1722 inline std::complex<_Tp> 1723 acosh(const std::complex<_Tp>& __z) 1724 { return __complex_acosh(__z.__rep()); } 1725 #else 1726 /// acosh(__z) [8.1.5]. 1727 // Effects: Behaves the same as C99 function cacosh, defined 1728 // in subclause 7.3.6.1. 1729 template<typename _Tp> 1730 inline std::complex<_Tp> 1731 acosh(const std::complex<_Tp>& __z) 1732 { return __complex_acosh(__z); } 1733 #endif 1734 1735 template<typename _Tp> 1736 std::complex<_Tp> 1737 __complex_asinh(const std::complex<_Tp>& __z) 1738 { 1739 std::complex<_Tp> __t((__z.real() - __z.imag()) 1740 * (__z.real() + __z.imag()) + _Tp(1.0), 1741 _Tp(2.0) * __z.real() * __z.imag()); 1742 __t = std::sqrt(__t); 1743 1744 return std::log(__t + __z); 1745 } 1746 1747 #if _GLIBCXX_USE_C99_COMPLEX_TR1 1748 inline __complex__ float 1749 __complex_asinh(__complex__ float __z) 1750 { return __builtin_casinhf(__z); } 1751 1752 inline __complex__ double 1753 __complex_asinh(__complex__ double __z) 1754 { return __builtin_casinh(__z); } 1755 1756 inline __complex__ long double 1757 __complex_asinh(const __complex__ long double& __z) 1758 { return __builtin_casinhl(__z); } 1759 1760 template<typename _Tp> 1761 inline std::complex<_Tp> 1762 asinh(const std::complex<_Tp>& __z) 1763 { return __complex_asinh(__z.__rep()); } 1764 #else 1765 /// asinh(__z) [8.1.6]. 1766 // Effects: Behaves the same as C99 function casin, defined 1767 // in subclause 7.3.6.2. 1768 template<typename _Tp> 1769 inline std::complex<_Tp> 1770 asinh(const std::complex<_Tp>& __z) 1771 { return __complex_asinh(__z); } 1772 #endif 1773 1774 template<typename _Tp> 1775 std::complex<_Tp> 1776 __complex_atanh(const std::complex<_Tp>& __z) 1777 { 1778 const _Tp __i2 = __z.imag() * __z.imag(); 1779 const _Tp __x = _Tp(1.0) - __i2 - __z.real() * __z.real(); 1780 1781 _Tp __num = _Tp(1.0) + __z.real(); 1782 _Tp __den = _Tp(1.0) - __z.real(); 1783 1784 __num = __i2 + __num * __num; 1785 __den = __i2 + __den * __den; 1786 1787 return std::complex<_Tp>(_Tp(0.25) * (log(__num) - log(__den)), 1788 _Tp(0.5) * atan2(_Tp(2.0) * __z.imag(), __x)); 1789 } 1790 1791 #if _GLIBCXX_USE_C99_COMPLEX_TR1 1792 inline __complex__ float 1793 __complex_atanh(__complex__ float __z) 1794 { return __builtin_catanhf(__z); } 1795 1796 inline __complex__ double 1797 __complex_atanh(__complex__ double __z) 1798 { return __builtin_catanh(__z); } 1799 1800 inline __complex__ long double 1801 __complex_atanh(const __complex__ long double& __z) 1802 { return __builtin_catanhl(__z); } 1803 1804 template<typename _Tp> 1805 inline std::complex<_Tp> 1806 atanh(const std::complex<_Tp>& __z) 1807 { return __complex_atanh(__z.__rep()); } 1808 #else 1809 /// atanh(__z) [8.1.7]. 1810 // Effects: Behaves the same as C99 function catanh, defined 1811 // in subclause 7.3.6.3. 1812 template<typename _Tp> 1813 inline std::complex<_Tp> 1814 atanh(const std::complex<_Tp>& __z) 1815 { return __complex_atanh(__z); } 1816 #endif 1817 1818 template<typename _Tp> 1819 inline _Tp 1820 /// fabs(__z) [8.1.8]. 1821 // Effects: Behaves the same as C99 function cabs, defined 1822 // in subclause 7.3.8.1. 1823 fabs(const std::complex<_Tp>& __z) 1824 { return std::abs(__z); } 1825 1826 /// Additional overloads [8.1.9]. 1827 template<typename _Tp> 1828 inline typename __gnu_cxx::__promote<_Tp>::__type 1829 arg(_Tp __x) 1830 { 1831 typedef typename __gnu_cxx::__promote<_Tp>::__type __type; 1832 #if (_GLIBCXX_USE_C99_MATH && !_GLIBCXX_USE_C99_FP_MACROS_DYNAMIC) 1833 return std::signbit(__x) ? __type(3.1415926535897932384626433832795029L) 1834 : __type(); 1835 #else 1836 return std::arg(std::complex<__type>(__x)); 1837 #endif 1838 } 1839 1840 template<typename _Tp> 1841 inline typename __gnu_cxx::__promote<_Tp>::__type 1842 imag(_Tp) 1843 { return _Tp(); } 1844 1845 template<typename _Tp> 1846 inline typename __gnu_cxx::__promote<_Tp>::__type 1847 norm(_Tp __x) 1848 { 1849 typedef typename __gnu_cxx::__promote<_Tp>::__type __type; 1850 return __type(__x) * __type(__x); 1851 } 1852 1853 template<typename _Tp> 1854 inline typename __gnu_cxx::__promote<_Tp>::__type 1855 real(_Tp __x) 1856 { return __x; } 1857 1858 template<typename _Tp, typename _Up> 1859 inline std::complex<typename __gnu_cxx::__promote_2<_Tp, _Up>::__type> 1860 pow(const std::complex<_Tp>& __x, const _Up& __y) 1861 { 1862 typedef typename __gnu_cxx::__promote_2<_Tp, _Up>::__type __type; 1863 return std::pow(std::complex<__type>(__x), __type(__y)); 1864 } 1865 1866 template<typename _Tp, typename _Up> 1867 inline std::complex<typename __gnu_cxx::__promote_2<_Tp, _Up>::__type> 1868 pow(const _Tp& __x, const std::complex<_Up>& __y) 1869 { 1870 typedef typename __gnu_cxx::__promote_2<_Tp, _Up>::__type __type; 1871 return std::pow(__type(__x), std::complex<__type>(__y)); 1872 } 1873 1874 template<typename _Tp, typename _Up> 1875 inline std::complex<typename __gnu_cxx::__promote_2<_Tp, _Up>::__type> 1876 pow(const std::complex<_Tp>& __x, const std::complex<_Up>& __y) 1877 { 1878 typedef typename __gnu_cxx::__promote_2<_Tp, _Up>::__type __type; 1879 return std::pow(std::complex<__type>(__x), 1880 std::complex<__type>(__y)); 1881 } 1882 1883 // Forward declarations. 1884 // DR 781. 1885 template<typename _Tp> std::complex<_Tp> proj(const std::complex<_Tp>&); 1886 1887 template<typename _Tp> 1888 std::complex<_Tp> 1889 __complex_proj(const std::complex<_Tp>& __z) 1890 { 1891 const _Tp __den = (__z.real() * __z.real() 1892 + __z.imag() * __z.imag() + _Tp(1.0)); 1893 1894 return std::complex<_Tp>((_Tp(2.0) * __z.real()) / __den, 1895 (_Tp(2.0) * __z.imag()) / __den); 1896 } 1897 1898 #if _GLIBCXX_USE_C99_COMPLEX 1899 inline __complex__ float 1900 __complex_proj(__complex__ float __z) 1901 { return __builtin_cprojf(__z); } 1902 1903 inline __complex__ double 1904 __complex_proj(__complex__ double __z) 1905 { return __builtin_cproj(__z); } 1906 1907 inline __complex__ long double 1908 __complex_proj(const __complex__ long double& __z) 1909 { return __builtin_cprojl(__z); } 1910 1911 template<typename _Tp> 1912 inline std::complex<_Tp> 1913 proj(const std::complex<_Tp>& __z) 1914 { return __complex_proj(__z.__rep()); } 1915 #else 1916 template<typename _Tp> 1917 inline std::complex<_Tp> 1918 proj(const std::complex<_Tp>& __z) 1919 { return __complex_proj(__z); } 1920 #endif 1921 1922 // DR 1137. 1923 template<typename _Tp> 1924 inline typename __gnu_cxx::__promote<_Tp>::__type 1925 proj(_Tp __x) 1926 { return __x; } 1927 1928 template<typename _Tp> 1929 inline typename __gnu_cxx::__promote<_Tp>::__type 1930 conj(_Tp __x) 1931 { return __x; } 1932 1933 #if __cplusplus > 201103L 1934 1935 inline namespace literals { 1936 inline namespace complex_literals { 1937 1938 #define __cpp_lib_complex_udls 201309 1939 1940 constexpr std::complex<float> 1941 operator""if(long double __num) 1942 { return std::complex<float>{0.0F, static_cast<float>(__num)}; } 1943 1944 constexpr std::complex<float> 1945 operator""if(unsigned long long __num) 1946 { return std::complex<float>{0.0F, static_cast<float>(__num)}; } 1947 1948 constexpr std::complex<double> 1949 operator""i(long double __num) 1950 { return std::complex<double>{0.0, static_cast<double>(__num)}; } 1951 1952 constexpr std::complex<double> 1953 operator""i(unsigned long long __num) 1954 { return std::complex<double>{0.0, static_cast<double>(__num)}; } 1955 1956 constexpr std::complex<long double> 1957 operator""il(long double __num) 1958 { return std::complex<long double>{0.0L, __num}; } 1959 1960 constexpr std::complex<long double> 1961 operator""il(unsigned long long __num) 1962 { return std::complex<long double>{0.0L, static_cast<long double>(__num)}; } 1963 1964 } // inline namespace complex_literals 1965 } // inline namespace literals 1966 1967 #endif // C++14 1968 1969 _GLIBCXX_END_NAMESPACE_VERSION 1970 } // namespace 1971 1972 #endif // C++11 1973 1974 #endif /* _GLIBCXX_COMPLEX */ 1975