1// The template and inlines for the -*- C++ -*- complex number classes. 2 3// Copyright (C) 1997-2017 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 = _CharT(); 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 if (__is) 525 { 526 __is.putback(__ch); 527 if (__is >> __re_x) 528 __x = __re_x; 529 else 530 __is.setstate(ios_base::failbit); 531 } 532 return __is; 533 } 534 535 /// Insertion operator for complex values. 536 template<typename _Tp, typename _CharT, class _Traits> 537 basic_ostream<_CharT, _Traits>& 538 operator<<(basic_ostream<_CharT, _Traits>& __os, const complex<_Tp>& __x) 539 { 540 basic_ostringstream<_CharT, _Traits> __s; 541 __s.flags(__os.flags()); 542 __s.imbue(__os.getloc()); 543 __s.precision(__os.precision()); 544 __s << '(' << __x.real() << ',' << __x.imag() << ')'; 545 return __os << __s.str(); 546 } 547 548 // Values 549#if __cplusplus >= 201103L 550 template<typename _Tp> 551 constexpr _Tp 552 real(const complex<_Tp>& __z) 553 { return __z.real(); } 554 555 template<typename _Tp> 556 constexpr _Tp 557 imag(const complex<_Tp>& __z) 558 { return __z.imag(); } 559#else 560 template<typename _Tp> 561 inline _Tp& 562 real(complex<_Tp>& __z) 563 { return __z.real(); } 564 565 template<typename _Tp> 566 inline const _Tp& 567 real(const complex<_Tp>& __z) 568 { return __z.real(); } 569 570 template<typename _Tp> 571 inline _Tp& 572 imag(complex<_Tp>& __z) 573 { return __z.imag(); } 574 575 template<typename _Tp> 576 inline const _Tp& 577 imag(const complex<_Tp>& __z) 578 { return __z.imag(); } 579#endif 580 581 // 26.2.7/3 abs(__z): Returns the magnitude of __z. 582 template<typename _Tp> 583 inline _Tp 584 __complex_abs(const complex<_Tp>& __z) 585 { 586 _Tp __x = __z.real(); 587 _Tp __y = __z.imag(); 588 const _Tp __s = std::max(abs(__x), abs(__y)); 589 if (__s == _Tp()) // well ... 590 return __s; 591 __x /= __s; 592 __y /= __s; 593 return __s * sqrt(__x * __x + __y * __y); 594 } 595 596#if _GLIBCXX_USE_C99_COMPLEX 597 // XXX: We can't use __builtin_cabs* because they are broken 598 inline float 599 __complex_abs(__complex__ float __z) { return __c99_cabsf(__z); } 600 601 inline double 602 __complex_abs(__complex__ double __z) { return __c99_cabs(__z); } 603 604 inline long double 605 __complex_abs(const __complex__ long double& __z) 606 { return __c99_cabsl(__z); } 607 608 template<typename _Tp> 609 inline _Tp 610 abs(const complex<_Tp>& __z) { return __complex_abs(__z.__rep()); } 611#else 612 template<typename _Tp> 613 inline _Tp 614 abs(const complex<_Tp>& __z) { return __complex_abs(__z); } 615#endif 616 617 618 // 26.2.7/4: arg(__z): Returns the phase angle of __z. 619 template<typename _Tp> 620 inline _Tp 621 __complex_arg(const complex<_Tp>& __z) 622 { return atan2(__z.imag(), __z.real()); } 623 624#if _GLIBCXX_USE_C99_COMPLEX 625 inline float 626 __complex_arg(__complex__ float __z) { return __builtin_cargf(__z); } 627 628 inline double 629 __complex_arg(__complex__ double __z) { return __builtin_carg(__z); } 630 631 inline long double 632 __complex_arg(const __complex__ long double& __z) 633 { return __builtin_cargl(__z); } 634 635 template<typename _Tp> 636 inline _Tp 637 arg(const complex<_Tp>& __z) { return __complex_arg(__z.__rep()); } 638#else 639 template<typename _Tp> 640 inline _Tp 641 arg(const complex<_Tp>& __z) { return __complex_arg(__z); } 642#endif 643 644 // 26.2.7/5: norm(__z) returns the squared magnitude of __z. 645 // As defined, norm() is -not- a norm is the common mathematical 646 // sense used in numerics. The helper class _Norm_helper<> tries to 647 // distinguish between builtin floating point and the rest, so as 648 // to deliver an answer as close as possible to the real value. 649 template<bool> 650 struct _Norm_helper 651 { 652 template<typename _Tp> 653 static inline _Tp _S_do_it(const complex<_Tp>& __z) 654 { 655 const _Tp __x = __z.real(); 656 const _Tp __y = __z.imag(); 657 return __x * __x + __y * __y; 658 } 659 }; 660 661 template<> 662 struct _Norm_helper<true> 663 { 664 template<typename _Tp> 665 static inline _Tp _S_do_it(const complex<_Tp>& __z) 666 { 667 _Tp __res = std::abs(__z); 668 return __res * __res; 669 } 670 }; 671 672 template<typename _Tp> 673 inline _Tp 674 norm(const complex<_Tp>& __z) 675 { 676 return _Norm_helper<__is_floating<_Tp>::__value 677 && !_GLIBCXX_FAST_MATH>::_S_do_it(__z); 678 } 679 680 template<typename _Tp> 681 inline complex<_Tp> 682 polar(const _Tp& __rho, const _Tp& __theta) 683 { 684 __glibcxx_assert( __rho >= 0 ); 685 return complex<_Tp>(__rho * cos(__theta), __rho * sin(__theta)); 686 } 687 688 template<typename _Tp> 689 inline complex<_Tp> 690 conj(const complex<_Tp>& __z) 691 { return complex<_Tp>(__z.real(), -__z.imag()); } 692 693 // Transcendentals 694 695 // 26.2.8/1 cos(__z): Returns the cosine of __z. 696 template<typename _Tp> 697 inline complex<_Tp> 698 __complex_cos(const complex<_Tp>& __z) 699 { 700 const _Tp __x = __z.real(); 701 const _Tp __y = __z.imag(); 702 return complex<_Tp>(cos(__x) * cosh(__y), -sin(__x) * sinh(__y)); 703 } 704 705#if _GLIBCXX_USE_C99_COMPLEX 706 inline __complex__ float 707 __complex_cos(__complex__ float __z) { return __builtin_ccosf(__z); } 708 709 inline __complex__ double 710 __complex_cos(__complex__ double __z) { return __builtin_ccos(__z); } 711 712 inline __complex__ long double 713 __complex_cos(const __complex__ long double& __z) 714 { return __builtin_ccosl(__z); } 715 716 template<typename _Tp> 717 inline complex<_Tp> 718 cos(const complex<_Tp>& __z) { return __complex_cos(__z.__rep()); } 719#else 720 template<typename _Tp> 721 inline complex<_Tp> 722 cos(const complex<_Tp>& __z) { return __complex_cos(__z); } 723#endif 724 725 // 26.2.8/2 cosh(__z): Returns the hyperbolic cosine of __z. 726 template<typename _Tp> 727 inline complex<_Tp> 728 __complex_cosh(const complex<_Tp>& __z) 729 { 730 const _Tp __x = __z.real(); 731 const _Tp __y = __z.imag(); 732 return complex<_Tp>(cosh(__x) * cos(__y), sinh(__x) * sin(__y)); 733 } 734 735#if _GLIBCXX_USE_C99_COMPLEX 736 inline __complex__ float 737 __complex_cosh(__complex__ float __z) { return __builtin_ccoshf(__z); } 738 739 inline __complex__ double 740 __complex_cosh(__complex__ double __z) { return __builtin_ccosh(__z); } 741 742 inline __complex__ long double 743 __complex_cosh(const __complex__ long double& __z) 744 { return __builtin_ccoshl(__z); } 745 746 template<typename _Tp> 747 inline complex<_Tp> 748 cosh(const complex<_Tp>& __z) { return __complex_cosh(__z.__rep()); } 749#else 750 template<typename _Tp> 751 inline complex<_Tp> 752 cosh(const complex<_Tp>& __z) { return __complex_cosh(__z); } 753#endif 754 755 // 26.2.8/3 exp(__z): Returns the complex base e exponential of x 756 template<typename _Tp> 757 inline complex<_Tp> 758 __complex_exp(const complex<_Tp>& __z) 759 { return std::polar<_Tp>(exp(__z.real()), __z.imag()); } 760 761#if _GLIBCXX_USE_C99_COMPLEX 762 inline __complex__ float 763 __complex_exp(__complex__ float __z) { return __builtin_cexpf(__z); } 764 765 inline __complex__ double 766 __complex_exp(__complex__ double __z) { return __builtin_cexp(__z); } 767 768 inline __complex__ long double 769 __complex_exp(const __complex__ long double& __z) 770 { return __builtin_cexpl(__z); } 771 772 template<typename _Tp> 773 inline complex<_Tp> 774 exp(const complex<_Tp>& __z) { return __complex_exp(__z.__rep()); } 775#else 776 template<typename _Tp> 777 inline complex<_Tp> 778 exp(const complex<_Tp>& __z) { return __complex_exp(__z); } 779#endif 780 781 // 26.2.8/5 log(__z): Returns the natural complex logarithm of __z. 782 // The branch cut is along the negative axis. 783 template<typename _Tp> 784 inline complex<_Tp> 785 __complex_log(const complex<_Tp>& __z) 786 { return complex<_Tp>(log(std::abs(__z)), std::arg(__z)); } 787 788#if _GLIBCXX_USE_C99_COMPLEX 789 inline __complex__ float 790 __complex_log(__complex__ float __z) { return __builtin_clogf(__z); } 791 792 inline __complex__ double 793 __complex_log(__complex__ double __z) { return __builtin_clog(__z); } 794 795 inline __complex__ long double 796 __complex_log(const __complex__ long double& __z) 797 { return __builtin_clogl(__z); } 798 799 template<typename _Tp> 800 inline complex<_Tp> 801 log(const complex<_Tp>& __z) { return __complex_log(__z.__rep()); } 802#else 803 template<typename _Tp> 804 inline complex<_Tp> 805 log(const complex<_Tp>& __z) { return __complex_log(__z); } 806#endif 807 808 template<typename _Tp> 809 inline complex<_Tp> 810 log10(const complex<_Tp>& __z) 811 { return std::log(__z) / log(_Tp(10.0)); } 812 813 // 26.2.8/10 sin(__z): Returns the sine of __z. 814 template<typename _Tp> 815 inline complex<_Tp> 816 __complex_sin(const complex<_Tp>& __z) 817 { 818 const _Tp __x = __z.real(); 819 const _Tp __y = __z.imag(); 820 return complex<_Tp>(sin(__x) * cosh(__y), cos(__x) * sinh(__y)); 821 } 822 823#if _GLIBCXX_USE_C99_COMPLEX 824 inline __complex__ float 825 __complex_sin(__complex__ float __z) { return __builtin_csinf(__z); } 826 827 inline __complex__ double 828 __complex_sin(__complex__ double __z) { return __builtin_csin(__z); } 829 830 inline __complex__ long double 831 __complex_sin(const __complex__ long double& __z) 832 { return __builtin_csinl(__z); } 833 834 template<typename _Tp> 835 inline complex<_Tp> 836 sin(const complex<_Tp>& __z) { return __complex_sin(__z.__rep()); } 837#else 838 template<typename _Tp> 839 inline complex<_Tp> 840 sin(const complex<_Tp>& __z) { return __complex_sin(__z); } 841#endif 842 843 // 26.2.8/11 sinh(__z): Returns the hyperbolic sine of __z. 844 template<typename _Tp> 845 inline complex<_Tp> 846 __complex_sinh(const complex<_Tp>& __z) 847 { 848 const _Tp __x = __z.real(); 849 const _Tp __y = __z.imag(); 850 return complex<_Tp>(sinh(__x) * cos(__y), cosh(__x) * sin(__y)); 851 } 852 853#if _GLIBCXX_USE_C99_COMPLEX 854 inline __complex__ float 855 __complex_sinh(__complex__ float __z) { return __builtin_csinhf(__z); } 856 857 inline __complex__ double 858 __complex_sinh(__complex__ double __z) { return __builtin_csinh(__z); } 859 860 inline __complex__ long double 861 __complex_sinh(const __complex__ long double& __z) 862 { return __builtin_csinhl(__z); } 863 864 template<typename _Tp> 865 inline complex<_Tp> 866 sinh(const complex<_Tp>& __z) { return __complex_sinh(__z.__rep()); } 867#else 868 template<typename _Tp> 869 inline complex<_Tp> 870 sinh(const complex<_Tp>& __z) { return __complex_sinh(__z); } 871#endif 872 873 // 26.2.8/13 sqrt(__z): Returns the complex square root of __z. 874 // The branch cut is on the negative axis. 875 template<typename _Tp> 876 complex<_Tp> 877 __complex_sqrt(const complex<_Tp>& __z) 878 { 879 _Tp __x = __z.real(); 880 _Tp __y = __z.imag(); 881 882 if (__x == _Tp()) 883 { 884 _Tp __t = sqrt(abs(__y) / 2); 885 return complex<_Tp>(__t, __y < _Tp() ? -__t : __t); 886 } 887 else 888 { 889 _Tp __t = sqrt(2 * (std::abs(__z) + abs(__x))); 890 _Tp __u = __t / 2; 891 return __x > _Tp() 892 ? complex<_Tp>(__u, __y / __t) 893 : complex<_Tp>(abs(__y) / __t, __y < _Tp() ? -__u : __u); 894 } 895 } 896 897#if _GLIBCXX_USE_C99_COMPLEX 898 inline __complex__ float 899 __complex_sqrt(__complex__ float __z) { return __builtin_csqrtf(__z); } 900 901 inline __complex__ double 902 __complex_sqrt(__complex__ double __z) { return __builtin_csqrt(__z); } 903 904 inline __complex__ long double 905 __complex_sqrt(const __complex__ long double& __z) 906 { return __builtin_csqrtl(__z); } 907 908 template<typename _Tp> 909 inline complex<_Tp> 910 sqrt(const complex<_Tp>& __z) { return __complex_sqrt(__z.__rep()); } 911#else 912 template<typename _Tp> 913 inline complex<_Tp> 914 sqrt(const complex<_Tp>& __z) { return __complex_sqrt(__z); } 915#endif 916 917 // 26.2.8/14 tan(__z): Return the complex tangent of __z. 918 919 template<typename _Tp> 920 inline complex<_Tp> 921 __complex_tan(const complex<_Tp>& __z) 922 { return std::sin(__z) / std::cos(__z); } 923 924#if _GLIBCXX_USE_C99_COMPLEX 925 inline __complex__ float 926 __complex_tan(__complex__ float __z) { return __builtin_ctanf(__z); } 927 928 inline __complex__ double 929 __complex_tan(__complex__ double __z) { return __builtin_ctan(__z); } 930 931 inline __complex__ long double 932 __complex_tan(const __complex__ long double& __z) 933 { return __builtin_ctanl(__z); } 934 935 template<typename _Tp> 936 inline complex<_Tp> 937 tan(const complex<_Tp>& __z) { return __complex_tan(__z.__rep()); } 938#else 939 template<typename _Tp> 940 inline complex<_Tp> 941 tan(const complex<_Tp>& __z) { return __complex_tan(__z); } 942#endif 943 944 945 // 26.2.8/15 tanh(__z): Returns the hyperbolic tangent of __z. 946 947 template<typename _Tp> 948 inline complex<_Tp> 949 __complex_tanh(const complex<_Tp>& __z) 950 { return std::sinh(__z) / std::cosh(__z); } 951 952#if _GLIBCXX_USE_C99_COMPLEX 953 inline __complex__ float 954 __complex_tanh(__complex__ float __z) { return __builtin_ctanhf(__z); } 955 956 inline __complex__ double 957 __complex_tanh(__complex__ double __z) { return __builtin_ctanh(__z); } 958 959 inline __complex__ long double 960 __complex_tanh(const __complex__ long double& __z) 961 { return __builtin_ctanhl(__z); } 962 963 template<typename _Tp> 964 inline complex<_Tp> 965 tanh(const complex<_Tp>& __z) { return __complex_tanh(__z.__rep()); } 966#else 967 template<typename _Tp> 968 inline complex<_Tp> 969 tanh(const complex<_Tp>& __z) { return __complex_tanh(__z); } 970#endif 971 972 973 // 26.2.8/9 pow(__x, __y): Returns the complex power base of __x 974 // raised to the __y-th power. The branch 975 // cut is on the negative axis. 976 template<typename _Tp> 977 complex<_Tp> 978 __complex_pow_unsigned(complex<_Tp> __x, unsigned __n) 979 { 980 complex<_Tp> __y = __n % 2 ? __x : complex<_Tp>(1); 981 982 while (__n >>= 1) 983 { 984 __x *= __x; 985 if (__n % 2) 986 __y *= __x; 987 } 988 989 return __y; 990 } 991 992 // In C++11 mode we used to implement the resolution of 993 // DR 844. complex pow return type is ambiguous. 994 // thus the following overload was disabled in that mode. However, doing 995 // that causes all sorts of issues, see, for example: 996 // http://gcc.gnu.org/ml/libstdc++/2013-01/msg00058.html 997 // and also PR57974. 998 template<typename _Tp> 999 inline complex<_Tp> 1000 pow(const complex<_Tp>& __z, int __n) 1001 { 1002 return __n < 0 1003 ? complex<_Tp>(1) / std::__complex_pow_unsigned(__z, -(unsigned)__n) 1004 : std::__complex_pow_unsigned(__z, __n); 1005 } 1006 1007 template<typename _Tp> 1008 complex<_Tp> 1009 pow(const complex<_Tp>& __x, const _Tp& __y) 1010 { 1011#if ! _GLIBCXX_USE_C99_COMPLEX 1012 if (__x == _Tp()) 1013 return _Tp(); 1014#endif 1015 if (__x.imag() == _Tp() && __x.real() > _Tp()) 1016 return pow(__x.real(), __y); 1017 1018 complex<_Tp> __t = std::log(__x); 1019 return std::polar<_Tp>(exp(__y * __t.real()), __y * __t.imag()); 1020 } 1021 1022 template<typename _Tp> 1023 inline complex<_Tp> 1024 __complex_pow(const complex<_Tp>& __x, const complex<_Tp>& __y) 1025 { return __x == _Tp() ? _Tp() : std::exp(__y * std::log(__x)); } 1026 1027#if _GLIBCXX_USE_C99_COMPLEX 1028 inline __complex__ float 1029 __complex_pow(__complex__ float __x, __complex__ float __y) 1030 { return __builtin_cpowf(__x, __y); } 1031 1032 inline __complex__ double 1033 __complex_pow(__complex__ double __x, __complex__ double __y) 1034 { return __builtin_cpow(__x, __y); } 1035 1036 inline __complex__ long double 1037 __complex_pow(const __complex__ long double& __x, 1038 const __complex__ long double& __y) 1039 { return __builtin_cpowl(__x, __y); } 1040 1041 template<typename _Tp> 1042 inline complex<_Tp> 1043 pow(const complex<_Tp>& __x, const complex<_Tp>& __y) 1044 { return __complex_pow(__x.__rep(), __y.__rep()); } 1045#else 1046 template<typename _Tp> 1047 inline complex<_Tp> 1048 pow(const complex<_Tp>& __x, const complex<_Tp>& __y) 1049 { return __complex_pow(__x, __y); } 1050#endif 1051 1052 template<typename _Tp> 1053 inline complex<_Tp> 1054 pow(const _Tp& __x, const complex<_Tp>& __y) 1055 { 1056 return __x > _Tp() ? std::polar<_Tp>(pow(__x, __y.real()), 1057 __y.imag() * log(__x)) 1058 : std::pow(complex<_Tp>(__x), __y); 1059 } 1060 1061 /// 26.2.3 complex specializations 1062 /// complex<float> specialization 1063 template<> 1064 struct complex<float> 1065 { 1066 typedef float value_type; 1067 typedef __complex__ float _ComplexT; 1068 1069 _GLIBCXX_CONSTEXPR complex(_ComplexT __z) : _M_value(__z) { } 1070 1071 _GLIBCXX_CONSTEXPR complex(float __r = 0.0f, float __i = 0.0f) 1072#if __cplusplus >= 201103L 1073 : _M_value{ __r, __i } { } 1074#else 1075 { 1076 __real__ _M_value = __r; 1077 __imag__ _M_value = __i; 1078 } 1079#endif 1080 1081 explicit _GLIBCXX_CONSTEXPR complex(const complex<double>&); 1082 explicit _GLIBCXX_CONSTEXPR complex(const complex<long double>&); 1083 1084#if __cplusplus >= 201103L 1085 // _GLIBCXX_RESOLVE_LIB_DEFECTS 1086 // DR 387. std::complex over-encapsulated. 1087 __attribute ((__abi_tag__ ("cxx11"))) 1088 constexpr float 1089 real() const { return __real__ _M_value; } 1090 1091 __attribute ((__abi_tag__ ("cxx11"))) 1092 constexpr float 1093 imag() const { return __imag__ _M_value; } 1094#else 1095 float& 1096 real() { return __real__ _M_value; } 1097 1098 const float& 1099 real() const { return __real__ _M_value; } 1100 1101 float& 1102 imag() { return __imag__ _M_value; } 1103 1104 const float& 1105 imag() const { return __imag__ _M_value; } 1106#endif 1107 1108 // _GLIBCXX_RESOLVE_LIB_DEFECTS 1109 // DR 387. std::complex over-encapsulated. 1110 void 1111 real(float __val) { __real__ _M_value = __val; } 1112 1113 void 1114 imag(float __val) { __imag__ _M_value = __val; } 1115 1116 complex& 1117 operator=(float __f) 1118 { 1119 _M_value = __f; 1120 return *this; 1121 } 1122 1123 complex& 1124 operator+=(float __f) 1125 { 1126 _M_value += __f; 1127 return *this; 1128 } 1129 1130 complex& 1131 operator-=(float __f) 1132 { 1133 _M_value -= __f; 1134 return *this; 1135 } 1136 1137 complex& 1138 operator*=(float __f) 1139 { 1140 _M_value *= __f; 1141 return *this; 1142 } 1143 1144 complex& 1145 operator/=(float __f) 1146 { 1147 _M_value /= __f; 1148 return *this; 1149 } 1150 1151 // Let the compiler synthesize the copy and assignment 1152 // operator. It always does a pretty good job. 1153 // complex& operator=(const complex&); 1154 1155 template<typename _Tp> 1156 complex& 1157 operator=(const complex<_Tp>& __z) 1158 { 1159 __real__ _M_value = __z.real(); 1160 __imag__ _M_value = __z.imag(); 1161 return *this; 1162 } 1163 1164 template<typename _Tp> 1165 complex& 1166 operator+=(const complex<_Tp>& __z) 1167 { 1168 __real__ _M_value += __z.real(); 1169 __imag__ _M_value += __z.imag(); 1170 return *this; 1171 } 1172 1173 template<class _Tp> 1174 complex& 1175 operator-=(const complex<_Tp>& __z) 1176 { 1177 __real__ _M_value -= __z.real(); 1178 __imag__ _M_value -= __z.imag(); 1179 return *this; 1180 } 1181 1182 template<class _Tp> 1183 complex& 1184 operator*=(const complex<_Tp>& __z) 1185 { 1186 _ComplexT __t; 1187 __real__ __t = __z.real(); 1188 __imag__ __t = __z.imag(); 1189 _M_value *= __t; 1190 return *this; 1191 } 1192 1193 template<class _Tp> 1194 complex& 1195 operator/=(const complex<_Tp>& __z) 1196 { 1197 _ComplexT __t; 1198 __real__ __t = __z.real(); 1199 __imag__ __t = __z.imag(); 1200 _M_value /= __t; 1201 return *this; 1202 } 1203 1204 _GLIBCXX_CONSTEXPR _ComplexT __rep() const { return _M_value; } 1205 1206 private: 1207 _ComplexT _M_value; 1208 }; 1209 1210 /// 26.2.3 complex specializations 1211 /// complex<double> specialization 1212 template<> 1213 struct complex<double> 1214 { 1215 typedef double value_type; 1216 typedef __complex__ double _ComplexT; 1217 1218 _GLIBCXX_CONSTEXPR complex(_ComplexT __z) : _M_value(__z) { } 1219 1220 _GLIBCXX_CONSTEXPR complex(double __r = 0.0, double __i = 0.0) 1221#if __cplusplus >= 201103L 1222 : _M_value{ __r, __i } { } 1223#else 1224 { 1225 __real__ _M_value = __r; 1226 __imag__ _M_value = __i; 1227 } 1228#endif 1229 1230 _GLIBCXX_CONSTEXPR complex(const complex<float>& __z) 1231 : _M_value(__z.__rep()) { } 1232 1233 explicit _GLIBCXX_CONSTEXPR complex(const complex<long double>&); 1234 1235#if __cplusplus >= 201103L 1236 // _GLIBCXX_RESOLVE_LIB_DEFECTS 1237 // DR 387. std::complex over-encapsulated. 1238 __attribute ((__abi_tag__ ("cxx11"))) 1239 constexpr double 1240 real() const { return __real__ _M_value; } 1241 1242 __attribute ((__abi_tag__ ("cxx11"))) 1243 constexpr double 1244 imag() const { return __imag__ _M_value; } 1245#else 1246 double& 1247 real() { return __real__ _M_value; } 1248 1249 const double& 1250 real() const { return __real__ _M_value; } 1251 1252 double& 1253 imag() { return __imag__ _M_value; } 1254 1255 const double& 1256 imag() const { return __imag__ _M_value; } 1257#endif 1258 1259 // _GLIBCXX_RESOLVE_LIB_DEFECTS 1260 // DR 387. std::complex over-encapsulated. 1261 void 1262 real(double __val) { __real__ _M_value = __val; } 1263 1264 void 1265 imag(double __val) { __imag__ _M_value = __val; } 1266 1267 complex& 1268 operator=(double __d) 1269 { 1270 _M_value = __d; 1271 return *this; 1272 } 1273 1274 complex& 1275 operator+=(double __d) 1276 { 1277 _M_value += __d; 1278 return *this; 1279 } 1280 1281 complex& 1282 operator-=(double __d) 1283 { 1284 _M_value -= __d; 1285 return *this; 1286 } 1287 1288 complex& 1289 operator*=(double __d) 1290 { 1291 _M_value *= __d; 1292 return *this; 1293 } 1294 1295 complex& 1296 operator/=(double __d) 1297 { 1298 _M_value /= __d; 1299 return *this; 1300 } 1301 1302 // The compiler will synthesize this, efficiently. 1303 // complex& operator=(const complex&); 1304 1305 template<typename _Tp> 1306 complex& 1307 operator=(const complex<_Tp>& __z) 1308 { 1309 __real__ _M_value = __z.real(); 1310 __imag__ _M_value = __z.imag(); 1311 return *this; 1312 } 1313 1314 template<typename _Tp> 1315 complex& 1316 operator+=(const complex<_Tp>& __z) 1317 { 1318 __real__ _M_value += __z.real(); 1319 __imag__ _M_value += __z.imag(); 1320 return *this; 1321 } 1322 1323 template<typename _Tp> 1324 complex& 1325 operator-=(const complex<_Tp>& __z) 1326 { 1327 __real__ _M_value -= __z.real(); 1328 __imag__ _M_value -= __z.imag(); 1329 return *this; 1330 } 1331 1332 template<typename _Tp> 1333 complex& 1334 operator*=(const complex<_Tp>& __z) 1335 { 1336 _ComplexT __t; 1337 __real__ __t = __z.real(); 1338 __imag__ __t = __z.imag(); 1339 _M_value *= __t; 1340 return *this; 1341 } 1342 1343 template<typename _Tp> 1344 complex& 1345 operator/=(const complex<_Tp>& __z) 1346 { 1347 _ComplexT __t; 1348 __real__ __t = __z.real(); 1349 __imag__ __t = __z.imag(); 1350 _M_value /= __t; 1351 return *this; 1352 } 1353 1354 _GLIBCXX_CONSTEXPR _ComplexT __rep() const { return _M_value; } 1355 1356 private: 1357 _ComplexT _M_value; 1358 }; 1359 1360 /// 26.2.3 complex specializations 1361 /// complex<long double> specialization 1362 template<> 1363 struct complex<long double> 1364 { 1365 typedef long double value_type; 1366 typedef __complex__ long double _ComplexT; 1367 1368 _GLIBCXX_CONSTEXPR complex(_ComplexT __z) : _M_value(__z) { } 1369 1370 _GLIBCXX_CONSTEXPR complex(long double __r = 0.0L, 1371 long double __i = 0.0L) 1372#if __cplusplus >= 201103L 1373 : _M_value{ __r, __i } { } 1374#else 1375 { 1376 __real__ _M_value = __r; 1377 __imag__ _M_value = __i; 1378 } 1379#endif 1380 1381 _GLIBCXX_CONSTEXPR complex(const complex<float>& __z) 1382 : _M_value(__z.__rep()) { } 1383 1384 _GLIBCXX_CONSTEXPR complex(const complex<double>& __z) 1385 : _M_value(__z.__rep()) { } 1386 1387#if __cplusplus >= 201103L 1388 // _GLIBCXX_RESOLVE_LIB_DEFECTS 1389 // DR 387. std::complex over-encapsulated. 1390 __attribute ((__abi_tag__ ("cxx11"))) 1391 constexpr long double 1392 real() const { return __real__ _M_value; } 1393 1394 __attribute ((__abi_tag__ ("cxx11"))) 1395 constexpr long double 1396 imag() const { return __imag__ _M_value; } 1397#else 1398 long double& 1399 real() { return __real__ _M_value; } 1400 1401 const long double& 1402 real() const { return __real__ _M_value; } 1403 1404 long double& 1405 imag() { return __imag__ _M_value; } 1406 1407 const long double& 1408 imag() const { return __imag__ _M_value; } 1409#endif 1410 1411 // _GLIBCXX_RESOLVE_LIB_DEFECTS 1412 // DR 387. std::complex over-encapsulated. 1413 void 1414 real(long double __val) { __real__ _M_value = __val; } 1415 1416 void 1417 imag(long double __val) { __imag__ _M_value = __val; } 1418 1419 complex& 1420 operator=(long double __r) 1421 { 1422 _M_value = __r; 1423 return *this; 1424 } 1425 1426 complex& 1427 operator+=(long double __r) 1428 { 1429 _M_value += __r; 1430 return *this; 1431 } 1432 1433 complex& 1434 operator-=(long double __r) 1435 { 1436 _M_value -= __r; 1437 return *this; 1438 } 1439 1440 complex& 1441 operator*=(long double __r) 1442 { 1443 _M_value *= __r; 1444 return *this; 1445 } 1446 1447 complex& 1448 operator/=(long double __r) 1449 { 1450 _M_value /= __r; 1451 return *this; 1452 } 1453 1454 // The compiler knows how to do this efficiently 1455 // complex& operator=(const complex&); 1456 1457 template<typename _Tp> 1458 complex& 1459 operator=(const complex<_Tp>& __z) 1460 { 1461 __real__ _M_value = __z.real(); 1462 __imag__ _M_value = __z.imag(); 1463 return *this; 1464 } 1465 1466 template<typename _Tp> 1467 complex& 1468 operator+=(const complex<_Tp>& __z) 1469 { 1470 __real__ _M_value += __z.real(); 1471 __imag__ _M_value += __z.imag(); 1472 return *this; 1473 } 1474 1475 template<typename _Tp> 1476 complex& 1477 operator-=(const complex<_Tp>& __z) 1478 { 1479 __real__ _M_value -= __z.real(); 1480 __imag__ _M_value -= __z.imag(); 1481 return *this; 1482 } 1483 1484 template<typename _Tp> 1485 complex& 1486 operator*=(const complex<_Tp>& __z) 1487 { 1488 _ComplexT __t; 1489 __real__ __t = __z.real(); 1490 __imag__ __t = __z.imag(); 1491 _M_value *= __t; 1492 return *this; 1493 } 1494 1495 template<typename _Tp> 1496 complex& 1497 operator/=(const complex<_Tp>& __z) 1498 { 1499 _ComplexT __t; 1500 __real__ __t = __z.real(); 1501 __imag__ __t = __z.imag(); 1502 _M_value /= __t; 1503 return *this; 1504 } 1505 1506 _GLIBCXX_CONSTEXPR _ComplexT __rep() const { return _M_value; } 1507 1508 private: 1509 _ComplexT _M_value; 1510 }; 1511 1512 // These bits have to be at the end of this file, so that the 1513 // specializations have all been defined. 1514 inline _GLIBCXX_CONSTEXPR 1515 complex<float>::complex(const complex<double>& __z) 1516 : _M_value(__z.__rep()) { } 1517 1518 inline _GLIBCXX_CONSTEXPR 1519 complex<float>::complex(const complex<long double>& __z) 1520 : _M_value(__z.__rep()) { } 1521 1522 inline _GLIBCXX_CONSTEXPR 1523 complex<double>::complex(const complex<long double>& __z) 1524 : _M_value(__z.__rep()) { } 1525 1526 // Inhibit implicit instantiations for required instantiations, 1527 // which are defined via explicit instantiations elsewhere. 1528 // NB: This syntax is a GNU extension. 1529#if _GLIBCXX_EXTERN_TEMPLATE 1530 extern template istream& operator>>(istream&, complex<float>&); 1531 extern template ostream& operator<<(ostream&, const complex<float>&); 1532 extern template istream& operator>>(istream&, complex<double>&); 1533 extern template ostream& operator<<(ostream&, const complex<double>&); 1534 extern template istream& operator>>(istream&, complex<long double>&); 1535 extern template ostream& operator<<(ostream&, const complex<long double>&); 1536 1537#ifdef _GLIBCXX_USE_WCHAR_T 1538 extern template wistream& operator>>(wistream&, complex<float>&); 1539 extern template wostream& operator<<(wostream&, const complex<float>&); 1540 extern template wistream& operator>>(wistream&, complex<double>&); 1541 extern template wostream& operator<<(wostream&, const complex<double>&); 1542 extern template wistream& operator>>(wistream&, complex<long double>&); 1543 extern template wostream& operator<<(wostream&, const complex<long double>&); 1544#endif 1545#endif 1546 1547 // @} group complex_numbers 1548 1549_GLIBCXX_END_NAMESPACE_VERSION 1550} // namespace 1551 1552namespace __gnu_cxx _GLIBCXX_VISIBILITY(default) 1553{ 1554_GLIBCXX_BEGIN_NAMESPACE_VERSION 1555 1556 // See ext/type_traits.h for the primary template. 1557 template<typename _Tp, typename _Up> 1558 struct __promote_2<std::complex<_Tp>, _Up> 1559 { 1560 public: 1561 typedef std::complex<typename __promote_2<_Tp, _Up>::__type> __type; 1562 }; 1563 1564 template<typename _Tp, typename _Up> 1565 struct __promote_2<_Tp, std::complex<_Up> > 1566 { 1567 public: 1568 typedef std::complex<typename __promote_2<_Tp, _Up>::__type> __type; 1569 }; 1570 1571 template<typename _Tp, typename _Up> 1572 struct __promote_2<std::complex<_Tp>, std::complex<_Up> > 1573 { 1574 public: 1575 typedef std::complex<typename __promote_2<_Tp, _Up>::__type> __type; 1576 }; 1577 1578_GLIBCXX_END_NAMESPACE_VERSION 1579} // namespace 1580 1581#if __cplusplus >= 201103L 1582 1583namespace std _GLIBCXX_VISIBILITY(default) 1584{ 1585_GLIBCXX_BEGIN_NAMESPACE_VERSION 1586 1587 // Forward declarations. 1588 template<typename _Tp> std::complex<_Tp> acos(const std::complex<_Tp>&); 1589 template<typename _Tp> std::complex<_Tp> asin(const std::complex<_Tp>&); 1590 template<typename _Tp> std::complex<_Tp> atan(const std::complex<_Tp>&); 1591 1592 template<typename _Tp> std::complex<_Tp> acosh(const std::complex<_Tp>&); 1593 template<typename _Tp> std::complex<_Tp> asinh(const std::complex<_Tp>&); 1594 template<typename _Tp> std::complex<_Tp> atanh(const std::complex<_Tp>&); 1595 // DR 595. 1596 template<typename _Tp> _Tp fabs(const std::complex<_Tp>&); 1597 1598 template<typename _Tp> 1599 inline std::complex<_Tp> 1600 __complex_acos(const std::complex<_Tp>& __z) 1601 { 1602 const std::complex<_Tp> __t = std::asin(__z); 1603 const _Tp __pi_2 = 1.5707963267948966192313216916397514L; 1604 return std::complex<_Tp>(__pi_2 - __t.real(), -__t.imag()); 1605 } 1606 1607#if _GLIBCXX_USE_C99_COMPLEX_TR1 1608 inline __complex__ float 1609 __complex_acos(__complex__ float __z) 1610 { return __builtin_cacosf(__z); } 1611 1612 inline __complex__ double 1613 __complex_acos(__complex__ double __z) 1614 { return __builtin_cacos(__z); } 1615 1616 inline __complex__ long double 1617 __complex_acos(const __complex__ long double& __z) 1618 { return __builtin_cacosl(__z); } 1619 1620 template<typename _Tp> 1621 inline std::complex<_Tp> 1622 acos(const std::complex<_Tp>& __z) 1623 { return __complex_acos(__z.__rep()); } 1624#else 1625 /// acos(__z) [8.1.2]. 1626 // Effects: Behaves the same as C99 function cacos, defined 1627 // in subclause 7.3.5.1. 1628 template<typename _Tp> 1629 inline std::complex<_Tp> 1630 acos(const std::complex<_Tp>& __z) 1631 { return __complex_acos(__z); } 1632#endif 1633 1634 template<typename _Tp> 1635 inline std::complex<_Tp> 1636 __complex_asin(const std::complex<_Tp>& __z) 1637 { 1638 std::complex<_Tp> __t(-__z.imag(), __z.real()); 1639 __t = std::asinh(__t); 1640 return std::complex<_Tp>(__t.imag(), -__t.real()); 1641 } 1642 1643#if _GLIBCXX_USE_C99_COMPLEX_TR1 1644 inline __complex__ float 1645 __complex_asin(__complex__ float __z) 1646 { return __builtin_casinf(__z); } 1647 1648 inline __complex__ double 1649 __complex_asin(__complex__ double __z) 1650 { return __builtin_casin(__z); } 1651 1652 inline __complex__ long double 1653 __complex_asin(const __complex__ long double& __z) 1654 { return __builtin_casinl(__z); } 1655 1656 template<typename _Tp> 1657 inline std::complex<_Tp> 1658 asin(const std::complex<_Tp>& __z) 1659 { return __complex_asin(__z.__rep()); } 1660#else 1661 /// asin(__z) [8.1.3]. 1662 // Effects: Behaves the same as C99 function casin, defined 1663 // in subclause 7.3.5.2. 1664 template<typename _Tp> 1665 inline std::complex<_Tp> 1666 asin(const std::complex<_Tp>& __z) 1667 { return __complex_asin(__z); } 1668#endif 1669 1670 template<typename _Tp> 1671 std::complex<_Tp> 1672 __complex_atan(const std::complex<_Tp>& __z) 1673 { 1674 const _Tp __r2 = __z.real() * __z.real(); 1675 const _Tp __x = _Tp(1.0) - __r2 - __z.imag() * __z.imag(); 1676 1677 _Tp __num = __z.imag() + _Tp(1.0); 1678 _Tp __den = __z.imag() - _Tp(1.0); 1679 1680 __num = __r2 + __num * __num; 1681 __den = __r2 + __den * __den; 1682 1683 return std::complex<_Tp>(_Tp(0.5) * atan2(_Tp(2.0) * __z.real(), __x), 1684 _Tp(0.25) * log(__num / __den)); 1685 } 1686 1687#if _GLIBCXX_USE_C99_COMPLEX_TR1 1688 inline __complex__ float 1689 __complex_atan(__complex__ float __z) 1690 { return __builtin_catanf(__z); } 1691 1692 inline __complex__ double 1693 __complex_atan(__complex__ double __z) 1694 { return __builtin_catan(__z); } 1695 1696 inline __complex__ long double 1697 __complex_atan(const __complex__ long double& __z) 1698 { return __builtin_catanl(__z); } 1699 1700 template<typename _Tp> 1701 inline std::complex<_Tp> 1702 atan(const std::complex<_Tp>& __z) 1703 { return __complex_atan(__z.__rep()); } 1704#else 1705 /// atan(__z) [8.1.4]. 1706 // Effects: Behaves the same as C99 function catan, defined 1707 // in subclause 7.3.5.3. 1708 template<typename _Tp> 1709 inline std::complex<_Tp> 1710 atan(const std::complex<_Tp>& __z) 1711 { return __complex_atan(__z); } 1712#endif 1713 1714 template<typename _Tp> 1715 std::complex<_Tp> 1716 __complex_acosh(const std::complex<_Tp>& __z) 1717 { 1718 // Kahan's formula. 1719 return _Tp(2.0) * std::log(std::sqrt(_Tp(0.5) * (__z + _Tp(1.0))) 1720 + std::sqrt(_Tp(0.5) * (__z - _Tp(1.0)))); 1721 } 1722 1723#if _GLIBCXX_USE_C99_COMPLEX_TR1 1724 inline __complex__ float 1725 __complex_acosh(__complex__ float __z) 1726 { return __builtin_cacoshf(__z); } 1727 1728 inline __complex__ double 1729 __complex_acosh(__complex__ double __z) 1730 { return __builtin_cacosh(__z); } 1731 1732 inline __complex__ long double 1733 __complex_acosh(const __complex__ long double& __z) 1734 { return __builtin_cacoshl(__z); } 1735 1736 template<typename _Tp> 1737 inline std::complex<_Tp> 1738 acosh(const std::complex<_Tp>& __z) 1739 { return __complex_acosh(__z.__rep()); } 1740#else 1741 /// acosh(__z) [8.1.5]. 1742 // Effects: Behaves the same as C99 function cacosh, defined 1743 // in subclause 7.3.6.1. 1744 template<typename _Tp> 1745 inline std::complex<_Tp> 1746 acosh(const std::complex<_Tp>& __z) 1747 { return __complex_acosh(__z); } 1748#endif 1749 1750 template<typename _Tp> 1751 std::complex<_Tp> 1752 __complex_asinh(const std::complex<_Tp>& __z) 1753 { 1754 std::complex<_Tp> __t((__z.real() - __z.imag()) 1755 * (__z.real() + __z.imag()) + _Tp(1.0), 1756 _Tp(2.0) * __z.real() * __z.imag()); 1757 __t = std::sqrt(__t); 1758 1759 return std::log(__t + __z); 1760 } 1761 1762#if _GLIBCXX_USE_C99_COMPLEX_TR1 1763 inline __complex__ float 1764 __complex_asinh(__complex__ float __z) 1765 { return __builtin_casinhf(__z); } 1766 1767 inline __complex__ double 1768 __complex_asinh(__complex__ double __z) 1769 { return __builtin_casinh(__z); } 1770 1771 inline __complex__ long double 1772 __complex_asinh(const __complex__ long double& __z) 1773 { return __builtin_casinhl(__z); } 1774 1775 template<typename _Tp> 1776 inline std::complex<_Tp> 1777 asinh(const std::complex<_Tp>& __z) 1778 { return __complex_asinh(__z.__rep()); } 1779#else 1780 /// asinh(__z) [8.1.6]. 1781 // Effects: Behaves the same as C99 function casin, defined 1782 // in subclause 7.3.6.2. 1783 template<typename _Tp> 1784 inline std::complex<_Tp> 1785 asinh(const std::complex<_Tp>& __z) 1786 { return __complex_asinh(__z); } 1787#endif 1788 1789 template<typename _Tp> 1790 std::complex<_Tp> 1791 __complex_atanh(const std::complex<_Tp>& __z) 1792 { 1793 const _Tp __i2 = __z.imag() * __z.imag(); 1794 const _Tp __x = _Tp(1.0) - __i2 - __z.real() * __z.real(); 1795 1796 _Tp __num = _Tp(1.0) + __z.real(); 1797 _Tp __den = _Tp(1.0) - __z.real(); 1798 1799 __num = __i2 + __num * __num; 1800 __den = __i2 + __den * __den; 1801 1802 return std::complex<_Tp>(_Tp(0.25) * (log(__num) - log(__den)), 1803 _Tp(0.5) * atan2(_Tp(2.0) * __z.imag(), __x)); 1804 } 1805 1806#if _GLIBCXX_USE_C99_COMPLEX_TR1 1807 inline __complex__ float 1808 __complex_atanh(__complex__ float __z) 1809 { return __builtin_catanhf(__z); } 1810 1811 inline __complex__ double 1812 __complex_atanh(__complex__ double __z) 1813 { return __builtin_catanh(__z); } 1814 1815 inline __complex__ long double 1816 __complex_atanh(const __complex__ long double& __z) 1817 { return __builtin_catanhl(__z); } 1818 1819 template<typename _Tp> 1820 inline std::complex<_Tp> 1821 atanh(const std::complex<_Tp>& __z) 1822 { return __complex_atanh(__z.__rep()); } 1823#else 1824 /// atanh(__z) [8.1.7]. 1825 // Effects: Behaves the same as C99 function catanh, defined 1826 // in subclause 7.3.6.3. 1827 template<typename _Tp> 1828 inline std::complex<_Tp> 1829 atanh(const std::complex<_Tp>& __z) 1830 { return __complex_atanh(__z); } 1831#endif 1832 1833 template<typename _Tp> 1834 inline _Tp 1835 /// fabs(__z) [8.1.8]. 1836 // Effects: Behaves the same as C99 function cabs, defined 1837 // in subclause 7.3.8.1. 1838 fabs(const std::complex<_Tp>& __z) 1839 { return std::abs(__z); } 1840 1841 /// Additional overloads [8.1.9]. 1842 template<typename _Tp> 1843 inline typename __gnu_cxx::__promote<_Tp>::__type 1844 arg(_Tp __x) 1845 { 1846 typedef typename __gnu_cxx::__promote<_Tp>::__type __type; 1847#if (_GLIBCXX11_USE_C99_MATH && !_GLIBCXX_USE_C99_FP_MACROS_DYNAMIC) 1848 return std::signbit(__x) ? __type(3.1415926535897932384626433832795029L) 1849 : __type(); 1850#else 1851 return std::arg(std::complex<__type>(__x)); 1852#endif 1853 } 1854 1855 template<typename _Tp> 1856 _GLIBCXX_CONSTEXPR inline typename __gnu_cxx::__promote<_Tp>::__type 1857 imag(_Tp) 1858 { return _Tp(); } 1859 1860 template<typename _Tp> 1861 inline typename __gnu_cxx::__promote<_Tp>::__type 1862 norm(_Tp __x) 1863 { 1864 typedef typename __gnu_cxx::__promote<_Tp>::__type __type; 1865 return __type(__x) * __type(__x); 1866 } 1867 1868 template<typename _Tp> 1869 _GLIBCXX_CONSTEXPR inline typename __gnu_cxx::__promote<_Tp>::__type 1870 real(_Tp __x) 1871 { return __x; } 1872 1873 template<typename _Tp, typename _Up> 1874 inline std::complex<typename __gnu_cxx::__promote_2<_Tp, _Up>::__type> 1875 pow(const std::complex<_Tp>& __x, const _Up& __y) 1876 { 1877 typedef typename __gnu_cxx::__promote_2<_Tp, _Up>::__type __type; 1878 return std::pow(std::complex<__type>(__x), __type(__y)); 1879 } 1880 1881 template<typename _Tp, typename _Up> 1882 inline std::complex<typename __gnu_cxx::__promote_2<_Tp, _Up>::__type> 1883 pow(const _Tp& __x, const std::complex<_Up>& __y) 1884 { 1885 typedef typename __gnu_cxx::__promote_2<_Tp, _Up>::__type __type; 1886 return std::pow(__type(__x), std::complex<__type>(__y)); 1887 } 1888 1889 template<typename _Tp, typename _Up> 1890 inline std::complex<typename __gnu_cxx::__promote_2<_Tp, _Up>::__type> 1891 pow(const std::complex<_Tp>& __x, const std::complex<_Up>& __y) 1892 { 1893 typedef typename __gnu_cxx::__promote_2<_Tp, _Up>::__type __type; 1894 return std::pow(std::complex<__type>(__x), 1895 std::complex<__type>(__y)); 1896 } 1897 1898 // Forward declarations. 1899 // DR 781. 1900 template<typename _Tp> std::complex<_Tp> proj(const std::complex<_Tp>&); 1901 1902 template<typename _Tp> 1903 std::complex<_Tp> 1904 __complex_proj(const std::complex<_Tp>& __z) 1905 { 1906 const _Tp __den = (__z.real() * __z.real() 1907 + __z.imag() * __z.imag() + _Tp(1.0)); 1908 1909 return std::complex<_Tp>((_Tp(2.0) * __z.real()) / __den, 1910 (_Tp(2.0) * __z.imag()) / __den); 1911 } 1912 1913#if _GLIBCXX_USE_C99_COMPLEX 1914 inline __complex__ float 1915 __complex_proj(__complex__ float __z) 1916 { return __builtin_cprojf(__z); } 1917 1918 inline __complex__ double 1919 __complex_proj(__complex__ double __z) 1920 { return __builtin_cproj(__z); } 1921 1922 inline __complex__ long double 1923 __complex_proj(const __complex__ long double& __z) 1924 { return __builtin_cprojl(__z); } 1925 1926 template<typename _Tp> 1927 inline std::complex<_Tp> 1928 proj(const std::complex<_Tp>& __z) 1929 { return __complex_proj(__z.__rep()); } 1930#else 1931 template<typename _Tp> 1932 inline std::complex<_Tp> 1933 proj(const std::complex<_Tp>& __z) 1934 { return __complex_proj(__z); } 1935#endif 1936 1937 template<typename _Tp> 1938 inline std::complex<typename __gnu_cxx::__promote<_Tp>::__type> 1939 proj(_Tp __x) 1940 { 1941 typedef typename __gnu_cxx::__promote<_Tp>::__type __type; 1942 return std::proj(std::complex<__type>(__x)); 1943 } 1944 1945 template<typename _Tp> 1946 inline std::complex<typename __gnu_cxx::__promote<_Tp>::__type> 1947 conj(_Tp __x) 1948 { 1949 typedef typename __gnu_cxx::__promote<_Tp>::__type __type; 1950 return std::complex<__type>(__x, -__type()); 1951 } 1952 1953_GLIBCXX_END_NAMESPACE_VERSION 1954 1955#if __cplusplus > 201103L 1956 1957inline namespace literals { 1958inline namespace complex_literals { 1959_GLIBCXX_BEGIN_NAMESPACE_VERSION 1960 1961#define __cpp_lib_complex_udls 201309 1962 1963 constexpr std::complex<float> 1964 operator""if(long double __num) 1965 { return std::complex<float>{0.0F, static_cast<float>(__num)}; } 1966 1967 constexpr std::complex<float> 1968 operator""if(unsigned long long __num) 1969 { return std::complex<float>{0.0F, static_cast<float>(__num)}; } 1970 1971 constexpr std::complex<double> 1972 operator""i(long double __num) 1973 { return std::complex<double>{0.0, static_cast<double>(__num)}; } 1974 1975 constexpr std::complex<double> 1976 operator""i(unsigned long long __num) 1977 { return std::complex<double>{0.0, static_cast<double>(__num)}; } 1978 1979 constexpr std::complex<long double> 1980 operator""il(long double __num) 1981 { return std::complex<long double>{0.0L, __num}; } 1982 1983 constexpr std::complex<long double> 1984 operator""il(unsigned long long __num) 1985 { return std::complex<long double>{0.0L, static_cast<long double>(__num)}; } 1986 1987_GLIBCXX_END_NAMESPACE_VERSION 1988} // inline namespace complex_literals 1989} // inline namespace literals 1990 1991#endif // C++14 1992 1993} // namespace 1994 1995#endif // C++11 1996 1997#endif /* _GLIBCXX_COMPLEX */ 1998