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