1 // The template and inlines for the -*- C++ -*- complex number classes. 2 3 // Copyright (C) 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005 4 // Free Software Foundation, Inc. 5 // 6 // This file is part of the GNU ISO C++ Library. This library is free 7 // software; you can redistribute it and/or modify it under the 8 // terms of the GNU General Public License as published by the 9 // Free Software Foundation; either version 2, or (at your option) 10 // any later version. 11 12 // This library is distributed in the hope that it will be useful, 13 // but WITHOUT ANY WARRANTY; without even the implied warranty of 14 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 15 // GNU General Public License for more details. 16 17 // You should have received a copy of the GNU General Public License along 18 // with this library; see the file COPYING. If not, write to the Free 19 // Software Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, 20 // USA. 21 22 // As a special exception, you may use this file as part of a free software 23 // library without restriction. Specifically, if other files instantiate 24 // templates or use macros or inline functions from this file, or you compile 25 // this file and link it with other files to produce an executable, this 26 // file does not by itself cause the resulting executable to be covered by 27 // the GNU General Public License. This exception does not however 28 // invalidate any other reasons why the executable file might be covered by 29 // the GNU General Public License. 30 31 // 32 // ISO C++ 14882: 26.2 Complex Numbers 33 // Note: this is not a conforming implementation. 34 // Initially implemented by Ulrich Drepper <[email protected]> 35 // Improved by Gabriel Dos Reis <[email protected]> 36 // 37 38 /** @file complex 39 * This is a Standard C++ Library header. You should @c #include this header 40 * in your programs, rather than any of the "st[dl]_*.h" implementation files. 41 */ 42 43 #ifndef _GLIBCXX_COMPLEX 44 #define _GLIBCXX_COMPLEX 1 45 46 #pragma GCC system_header 47 48 #include <bits/c++config.h> 49 #include <bits/cpp_type_traits.h> 50 #include <cmath> 51 #include <sstream> 52 53 namespace std 54 { 55 // Forward declarations 56 template<typename _Tp> class complex; 57 template<> class complex<float>; 58 template<> class complex<double>; 59 template<> class complex<long double>; 60 61 /// Return magnitude of @a z. 62 template<typename _Tp> _Tp abs(const complex<_Tp>&); 63 /// Return phase angle of @a z. 64 template<typename _Tp> _Tp arg(const complex<_Tp>&); 65 /// Return @a z magnitude squared. 66 template<typename _Tp> _Tp norm(const complex<_Tp>&); 67 68 /// Return complex conjugate of @a z. 69 template<typename _Tp> complex<_Tp> conj(const complex<_Tp>&); 70 /// Return complex with magnitude @a rho and angle @a theta. 71 template<typename _Tp> complex<_Tp> polar(const _Tp&, const _Tp& = 0); 72 73 // Transcendentals: 74 /// Return complex cosine of @a z. 75 template<typename _Tp> complex<_Tp> cos(const complex<_Tp>&); 76 /// Return complex hyperbolic cosine of @a z. 77 template<typename _Tp> complex<_Tp> cosh(const complex<_Tp>&); 78 /// Return complex base e exponential of @a z. 79 template<typename _Tp> complex<_Tp> exp(const complex<_Tp>&); 80 /// Return complex natural logarithm of @a z. 81 template<typename _Tp> complex<_Tp> log(const complex<_Tp>&); 82 /// Return complex base 10 logarithm of @a z. 83 template<typename _Tp> complex<_Tp> log10(const complex<_Tp>&); 84 /// Return complex cosine of @a z. 85 template<typename _Tp> complex<_Tp> pow(const complex<_Tp>&, int); 86 /// Return @a x to the @a y'th power. 87 template<typename _Tp> complex<_Tp> pow(const complex<_Tp>&, const _Tp&); 88 /// Return @a x to the @a y'th power. 89 template<typename _Tp> complex<_Tp> pow(const complex<_Tp>&, 90 const complex<_Tp>&); 91 /// Return @a x to the @a y'th power. 92 template<typename _Tp> complex<_Tp> pow(const _Tp&, const complex<_Tp>&); 93 /// Return complex sine of @a z. 94 template<typename _Tp> complex<_Tp> sin(const complex<_Tp>&); 95 /// Return complex hyperbolic sine of @a z. 96 template<typename _Tp> complex<_Tp> sinh(const complex<_Tp>&); 97 /// Return complex square root of @a z. 98 template<typename _Tp> complex<_Tp> sqrt(const complex<_Tp>&); 99 /// Return complex tangent of @a z. 100 template<typename _Tp> complex<_Tp> tan(const complex<_Tp>&); 101 /// Return complex hyperbolic tangent of @a z. 102 template<typename _Tp> complex<_Tp> tanh(const complex<_Tp>&); 103 //@} 104 105 106 // 26.2.2 Primary template class complex 107 /** 108 * Template to represent complex numbers. 109 * 110 * Specializations for float, double, and long double are part of the 111 * library. Results with any other type are not guaranteed. 112 * 113 * @param Tp Type of real and imaginary values. 114 */ 115 template<typename _Tp> 116 class complex 117 { 118 public: 119 /// Value typedef. 120 typedef _Tp value_type; 121 122 /// Default constructor. First parameter is x, second parameter is y. 123 /// Unspecified parameters default to 0. 124 complex(const _Tp& = _Tp(), const _Tp & = _Tp()); 125 126 // Lets the compiler synthesize the copy constructor 127 // complex (const complex<_Tp>&); 128 /// Copy constructor. 129 template<typename _Up> 130 complex(const complex<_Up>&); 131 132 /// Return real part of complex number. 133 _Tp& real(); 134 /// Return real part of complex number. 135 const _Tp& real() const; 136 /// Return imaginary part of complex number. 137 _Tp& imag(); 138 /// Return imaginary part of complex number. 139 const _Tp& imag() const; 140 141 /// Assign this complex number to scalar @a t. 142 complex<_Tp>& operator=(const _Tp&); 143 /// Add @a t to this complex number. 144 complex<_Tp>& operator+=(const _Tp&); 145 /// Subtract @a t from this complex number. 146 complex<_Tp>& operator-=(const _Tp&); 147 /// Multiply this complex number by @a t. 148 complex<_Tp>& operator*=(const _Tp&); 149 /// Divide this complex number by @a t. 150 complex<_Tp>& operator/=(const _Tp&); 151 152 // Lets the compiler synthesize the 153 // copy and assignment operator 154 // complex<_Tp>& operator= (const complex<_Tp>&); 155 /// Assign this complex number to complex @a z. 156 template<typename _Up> 157 complex<_Tp>& operator=(const complex<_Up>&); 158 /// Add @a z to this complex number. 159 template<typename _Up> 160 complex<_Tp>& operator+=(const complex<_Up>&); 161 /// Subtract @a z from this complex number. 162 template<typename _Up> 163 complex<_Tp>& operator-=(const complex<_Up>&); 164 /// Multiply this complex number by @a z. 165 template<typename _Up> 166 complex<_Tp>& operator*=(const complex<_Up>&); 167 /// Divide this complex number by @a z. 168 template<typename _Up> 169 complex<_Tp>& operator/=(const complex<_Up>&); 170 171 private: 172 _Tp _M_real; 173 _Tp _M_imag; 174 }; 175 176 template<typename _Tp> 177 inline _Tp& 178 complex<_Tp>::real() { return _M_real; } 179 180 template<typename _Tp> 181 inline const _Tp& 182 complex<_Tp>::real() const { return _M_real; } 183 184 template<typename _Tp> 185 inline _Tp& 186 complex<_Tp>::imag() { return _M_imag; } 187 188 template<typename _Tp> 189 inline const _Tp& 190 complex<_Tp>::imag() const { return _M_imag; } 191 192 template<typename _Tp> 193 inline 194 complex<_Tp>::complex(const _Tp& __r, const _Tp& __i) 195 : _M_real(__r), _M_imag(__i) { } 196 197 template<typename _Tp> 198 template<typename _Up> 199 inline 200 complex<_Tp>::complex(const complex<_Up>& __z) 201 : _M_real(__z.real()), _M_imag(__z.imag()) { } 202 203 template<typename _Tp> 204 complex<_Tp>& 205 complex<_Tp>::operator=(const _Tp& __t) 206 { 207 _M_real = __t; 208 _M_imag = _Tp(); 209 return *this; 210 } 211 212 // 26.2.5/1 213 template<typename _Tp> 214 inline complex<_Tp>& 215 complex<_Tp>::operator+=(const _Tp& __t) 216 { 217 _M_real += __t; 218 return *this; 219 } 220 221 // 26.2.5/3 222 template<typename _Tp> 223 inline complex<_Tp>& 224 complex<_Tp>::operator-=(const _Tp& __t) 225 { 226 _M_real -= __t; 227 return *this; 228 } 229 230 // 26.2.5/5 231 template<typename _Tp> 232 complex<_Tp>& 233 complex<_Tp>::operator*=(const _Tp& __t) 234 { 235 _M_real *= __t; 236 _M_imag *= __t; 237 return *this; 238 } 239 240 // 26.2.5/7 241 template<typename _Tp> 242 complex<_Tp>& 243 complex<_Tp>::operator/=(const _Tp& __t) 244 { 245 _M_real /= __t; 246 _M_imag /= __t; 247 return *this; 248 } 249 250 template<typename _Tp> 251 template<typename _Up> 252 complex<_Tp>& 253 complex<_Tp>::operator=(const complex<_Up>& __z) 254 { 255 _M_real = __z.real(); 256 _M_imag = __z.imag(); 257 return *this; 258 } 259 260 // 26.2.5/9 261 template<typename _Tp> 262 template<typename _Up> 263 complex<_Tp>& 264 complex<_Tp>::operator+=(const complex<_Up>& __z) 265 { 266 _M_real += __z.real(); 267 _M_imag += __z.imag(); 268 return *this; 269 } 270 271 // 26.2.5/11 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/13 283 // XXX: This is a grammar school implementation. 284 template<typename _Tp> 285 template<typename _Up> 286 complex<_Tp>& 287 complex<_Tp>::operator*=(const complex<_Up>& __z) 288 { 289 const _Tp __r = _M_real * __z.real() - _M_imag * __z.imag(); 290 _M_imag = _M_real * __z.imag() + _M_imag * __z.real(); 291 _M_real = __r; 292 return *this; 293 } 294 295 // 26.2.5/15 296 // XXX: This is a grammar school implementation. 297 template<typename _Tp> 298 template<typename _Up> 299 complex<_Tp>& 300 complex<_Tp>::operator/=(const complex<_Up>& __z) 301 { 302 const _Tp __r = _M_real * __z.real() + _M_imag * __z.imag(); 303 const _Tp __n = std::norm(__z); 304 _M_imag = (_M_imag * __z.real() - _M_real * __z.imag()) / __n; 305 _M_real = __r / __n; 306 return *this; 307 } 308 309 // Operators: 310 //@{ 311 /// Return new complex value @a x plus @a y. 312 template<typename _Tp> 313 inline complex<_Tp> 314 operator+(const complex<_Tp>& __x, const complex<_Tp>& __y) 315 { 316 complex<_Tp> __r = __x; 317 __r += __y; 318 return __r; 319 } 320 321 template<typename _Tp> 322 inline complex<_Tp> 323 operator+(const complex<_Tp>& __x, const _Tp& __y) 324 { 325 complex<_Tp> __r = __x; 326 __r.real() += __y; 327 return __r; 328 } 329 330 template<typename _Tp> 331 inline complex<_Tp> 332 operator+(const _Tp& __x, const complex<_Tp>& __y) 333 { 334 complex<_Tp> __r = __y; 335 __r.real() += __x; 336 return __r; 337 } 338 //@} 339 340 //@{ 341 /// Return new complex value @a x minus @a y. 342 template<typename _Tp> 343 inline complex<_Tp> 344 operator-(const complex<_Tp>& __x, const complex<_Tp>& __y) 345 { 346 complex<_Tp> __r = __x; 347 __r -= __y; 348 return __r; 349 } 350 351 template<typename _Tp> 352 inline complex<_Tp> 353 operator-(const complex<_Tp>& __x, const _Tp& __y) 354 { 355 complex<_Tp> __r = __x; 356 __r.real() -= __y; 357 return __r; 358 } 359 360 template<typename _Tp> 361 inline complex<_Tp> 362 operator-(const _Tp& __x, const complex<_Tp>& __y) 363 { 364 complex<_Tp> __r(__x, -__y.imag()); 365 __r.real() -= __y.real(); 366 return __r; 367 } 368 //@} 369 370 //@{ 371 /// Return new complex value @a x times @a y. 372 template<typename _Tp> 373 inline complex<_Tp> 374 operator*(const complex<_Tp>& __x, const complex<_Tp>& __y) 375 { 376 complex<_Tp> __r = __x; 377 __r *= __y; 378 return __r; 379 } 380 381 template<typename _Tp> 382 inline complex<_Tp> 383 operator*(const complex<_Tp>& __x, const _Tp& __y) 384 { 385 complex<_Tp> __r = __x; 386 __r *= __y; 387 return __r; 388 } 389 390 template<typename _Tp> 391 inline complex<_Tp> 392 operator*(const _Tp& __x, const complex<_Tp>& __y) 393 { 394 complex<_Tp> __r = __y; 395 __r *= __x; 396 return __r; 397 } 398 //@} 399 400 //@{ 401 /// Return new complex value @a x divided by @a y. 402 template<typename _Tp> 403 inline complex<_Tp> 404 operator/(const complex<_Tp>& __x, const complex<_Tp>& __y) 405 { 406 complex<_Tp> __r = __x; 407 __r /= __y; 408 return __r; 409 } 410 411 template<typename _Tp> 412 inline complex<_Tp> 413 operator/(const complex<_Tp>& __x, const _Tp& __y) 414 { 415 complex<_Tp> __r = __x; 416 __r /= __y; 417 return __r; 418 } 419 420 template<typename _Tp> 421 inline complex<_Tp> 422 operator/(const _Tp& __x, const complex<_Tp>& __y) 423 { 424 complex<_Tp> __r = __x; 425 __r /= __y; 426 return __r; 427 } 428 //@} 429 430 /// Return @a x. 431 template<typename _Tp> 432 inline complex<_Tp> 433 operator+(const complex<_Tp>& __x) 434 { return __x; } 435 436 /// Return complex negation of @a x. 437 template<typename _Tp> 438 inline complex<_Tp> 439 operator-(const complex<_Tp>& __x) 440 { return complex<_Tp>(-__x.real(), -__x.imag()); } 441 442 //@{ 443 /// Return true if @a x is equal to @a y. 444 template<typename _Tp> 445 inline bool 446 operator==(const complex<_Tp>& __x, const complex<_Tp>& __y) 447 { return __x.real() == __y.real() && __x.imag() == __y.imag(); } 448 449 template<typename _Tp> 450 inline bool 451 operator==(const complex<_Tp>& __x, const _Tp& __y) 452 { return __x.real() == __y && __x.imag() == _Tp(); } 453 454 template<typename _Tp> 455 inline bool 456 operator==(const _Tp& __x, const complex<_Tp>& __y) 457 { return __x == __y.real() && _Tp() == __y.imag(); } 458 //@} 459 460 //@{ 461 /// Return false if @a x is equal to @a y. 462 template<typename _Tp> 463 inline bool 464 operator!=(const complex<_Tp>& __x, const complex<_Tp>& __y) 465 { return __x.real() != __y.real() || __x.imag() != __y.imag(); } 466 467 template<typename _Tp> 468 inline bool 469 operator!=(const complex<_Tp>& __x, const _Tp& __y) 470 { return __x.real() != __y || __x.imag() != _Tp(); } 471 472 template<typename _Tp> 473 inline bool 474 operator!=(const _Tp& __x, const complex<_Tp>& __y) 475 { return __x != __y.real() || _Tp() != __y.imag(); } 476 //@} 477 478 /// Extraction operator for complex values. 479 template<typename _Tp, typename _CharT, class _Traits> 480 basic_istream<_CharT, _Traits>& 481 operator>>(basic_istream<_CharT, _Traits>& __is, complex<_Tp>& __x) 482 { 483 _Tp __re_x, __im_x; 484 _CharT __ch; 485 __is >> __ch; 486 if (__ch == '(') 487 { 488 __is >> __re_x >> __ch; 489 if (__ch == ',') 490 { 491 __is >> __im_x >> __ch; 492 if (__ch == ')') 493 __x = complex<_Tp>(__re_x, __im_x); 494 else 495 __is.setstate(ios_base::failbit); 496 } 497 else if (__ch == ')') 498 __x = __re_x; 499 else 500 __is.setstate(ios_base::failbit); 501 } 502 else 503 { 504 __is.putback(__ch); 505 __is >> __re_x; 506 __x = __re_x; 507 } 508 return __is; 509 } 510 511 /// Insertion operator for complex values. 512 template<typename _Tp, typename _CharT, class _Traits> 513 basic_ostream<_CharT, _Traits>& 514 operator<<(basic_ostream<_CharT, _Traits>& __os, const complex<_Tp>& __x) 515 { 516 basic_ostringstream<_CharT, _Traits> __s; 517 __s.flags(__os.flags()); 518 __s.imbue(__os.getloc()); 519 __s.precision(__os.precision()); 520 __s << '(' << __x.real() << ',' << __x.imag() << ')'; 521 return __os << __s.str(); 522 } 523 524 // Values 525 template<typename _Tp> 526 inline _Tp& 527 real(complex<_Tp>& __z) 528 { return __z.real(); } 529 530 template<typename _Tp> 531 inline const _Tp& 532 real(const complex<_Tp>& __z) 533 { return __z.real(); } 534 535 template<typename _Tp> 536 inline _Tp& 537 imag(complex<_Tp>& __z) 538 { return __z.imag(); } 539 540 template<typename _Tp> 541 inline const _Tp& 542 imag(const complex<_Tp>& __z) 543 { return __z.imag(); } 544 545 template<typename _Tp> 546 inline _Tp 547 abs(const complex<_Tp>& __z) 548 { 549 _Tp __x = __z.real(); 550 _Tp __y = __z.imag(); 551 const _Tp __s = std::max(abs(__x), abs(__y)); 552 if (__s == _Tp()) // well ... 553 return __s; 554 __x /= __s; 555 __y /= __s; 556 return __s * sqrt(__x * __x + __y * __y); 557 } 558 559 template<typename _Tp> 560 inline _Tp 561 arg(const complex<_Tp>& __z) 562 { return atan2(__z.imag(), __z.real()); } 563 564 // 26.2.7/5: norm(__z) returns the squared magintude of __z. 565 // As defined, norm() is -not- a norm is the common mathematical 566 // sens used in numerics. The helper class _Norm_helper<> tries to 567 // distinguish between builtin floating point and the rest, so as 568 // to deliver an answer as close as possible to the real value. 569 template<bool> 570 struct _Norm_helper 571 { 572 template<typename _Tp> 573 static inline _Tp _S_do_it(const complex<_Tp>& __z) 574 { 575 const _Tp __x = __z.real(); 576 const _Tp __y = __z.imag(); 577 return __x * __x + __y * __y; 578 } 579 }; 580 581 template<> 582 struct _Norm_helper<true> 583 { 584 template<typename _Tp> 585 static inline _Tp _S_do_it(const complex<_Tp>& __z) 586 { 587 _Tp __res = std::abs(__z); 588 return __res * __res; 589 } 590 }; 591 592 template<typename _Tp> 593 inline _Tp 594 norm(const complex<_Tp>& __z) 595 { 596 return _Norm_helper<__is_floating<_Tp>::_M_type && !_GLIBCXX_FAST_MATH>::_S_do_it(__z); 597 } 598 599 template<typename _Tp> 600 inline complex<_Tp> 601 polar(const _Tp& __rho, const _Tp& __theta) 602 { return complex<_Tp>(__rho * cos(__theta), __rho * sin(__theta)); } 603 604 template<typename _Tp> 605 inline complex<_Tp> 606 conj(const complex<_Tp>& __z) 607 { return complex<_Tp>(__z.real(), -__z.imag()); } 608 609 // Transcendentals 610 template<typename _Tp> 611 inline complex<_Tp> 612 cos(const complex<_Tp>& __z) 613 { 614 const _Tp __x = __z.real(); 615 const _Tp __y = __z.imag(); 616 return complex<_Tp>(cos(__x) * cosh(__y), -sin(__x) * sinh(__y)); 617 } 618 619 template<typename _Tp> 620 inline complex<_Tp> 621 cosh(const complex<_Tp>& __z) 622 { 623 const _Tp __x = __z.real(); 624 const _Tp __y = __z.imag(); 625 return complex<_Tp>(cosh(__x) * cos(__y), sinh(__x) * sin(__y)); 626 } 627 628 template<typename _Tp> 629 inline complex<_Tp> 630 exp(const complex<_Tp>& __z) 631 { return std::polar(exp(__z.real()), __z.imag()); } 632 633 template<typename _Tp> 634 inline complex<_Tp> 635 log(const complex<_Tp>& __z) 636 { return complex<_Tp>(log(std::abs(__z)), std::arg(__z)); } 637 638 template<typename _Tp> 639 inline complex<_Tp> 640 log10(const complex<_Tp>& __z) 641 { return std::log(__z) / log(_Tp(10.0)); } 642 643 template<typename _Tp> 644 inline complex<_Tp> 645 sin(const complex<_Tp>& __z) 646 { 647 const _Tp __x = __z.real(); 648 const _Tp __y = __z.imag(); 649 return complex<_Tp>(sin(__x) * cosh(__y), cos(__x) * sinh(__y)); 650 } 651 652 template<typename _Tp> 653 inline complex<_Tp> 654 sinh(const complex<_Tp>& __z) 655 { 656 const _Tp __x = __z.real(); 657 const _Tp __y = __z.imag(); 658 return complex<_Tp>(sinh(__x) * cos(__y), cosh(__x) * sin(__y)); 659 } 660 661 template<typename _Tp> 662 complex<_Tp> 663 sqrt(const complex<_Tp>& __z) 664 { 665 _Tp __x = __z.real(); 666 _Tp __y = __z.imag(); 667 668 if (__x == _Tp()) 669 { 670 _Tp __t = sqrt(abs(__y) / 2); 671 return complex<_Tp>(__t, __y < _Tp() ? -__t : __t); 672 } 673 else 674 { 675 _Tp __t = sqrt(2 * (std::abs(__z) + abs(__x))); 676 _Tp __u = __t / 2; 677 return __x > _Tp() 678 ? complex<_Tp>(__u, __y / __t) 679 : complex<_Tp>(abs(__y) / __t, __y < _Tp() ? -__u : __u); 680 } 681 } 682 683 template<typename _Tp> 684 inline complex<_Tp> 685 tan(const complex<_Tp>& __z) 686 { 687 return std::sin(__z) / std::cos(__z); 688 } 689 690 template<typename _Tp> 691 inline complex<_Tp> 692 tanh(const complex<_Tp>& __z) 693 { 694 return std::sinh(__z) / std::cosh(__z); 695 } 696 697 template<typename _Tp> 698 inline complex<_Tp> 699 pow(const complex<_Tp>& __z, int __n) 700 { 701 return std::__pow_helper(__z, __n); 702 } 703 704 template<typename _Tp> 705 complex<_Tp> 706 pow(const complex<_Tp>& __x, const _Tp& __y) 707 { 708 if (__x.imag() == _Tp() && __x.real() > _Tp()) 709 return pow(__x.real(), __y); 710 711 complex<_Tp> __t = std::log(__x); 712 return std::polar(exp(__y * __t.real()), __y * __t.imag()); 713 } 714 715 template<typename _Tp> 716 inline complex<_Tp> 717 pow(const complex<_Tp>& __x, const complex<_Tp>& __y) 718 { 719 return __x == _Tp() ? _Tp() : std::exp(__y * std::log(__x)); 720 } 721 722 template<typename _Tp> 723 inline complex<_Tp> 724 pow(const _Tp& __x, const complex<_Tp>& __y) 725 { 726 return __x > _Tp() ? std::polar(pow(__x, __y.real()), 727 __y.imag() * log(__x)) 728 : std::pow(complex<_Tp>(__x, _Tp()), __y); 729 } 730 731 // 26.2.3 complex specializations 732 // complex<float> specialization 733 template<> class complex<float> 734 { 735 public: 736 typedef float value_type; 737 738 complex(float = 0.0f, float = 0.0f); 739 740 explicit complex(const complex<double>&); 741 explicit complex(const complex<long double>&); 742 743 float& real(); 744 const float& real() const; 745 float& imag(); 746 const float& imag() const; 747 748 complex<float>& operator=(float); 749 complex<float>& operator+=(float); 750 complex<float>& operator-=(float); 751 complex<float>& operator*=(float); 752 complex<float>& operator/=(float); 753 754 // Let's the compiler synthetize the copy and assignment 755 // operator. It always does a pretty good job. 756 // complex& operator= (const complex&); 757 template<typename _Tp> 758 complex<float>&operator=(const complex<_Tp>&); 759 template<typename _Tp> 760 complex<float>& operator+=(const complex<_Tp>&); 761 template<class _Tp> 762 complex<float>& operator-=(const complex<_Tp>&); 763 template<class _Tp> 764 complex<float>& operator*=(const complex<_Tp>&); 765 template<class _Tp> 766 complex<float>&operator/=(const complex<_Tp>&); 767 768 private: 769 typedef __complex__ float _ComplexT; 770 _ComplexT _M_value; 771 772 complex(_ComplexT __z) : _M_value(__z) { } 773 774 friend class complex<double>; 775 friend class complex<long double>; 776 }; 777 778 inline float& 779 complex<float>::real() 780 { return __real__ _M_value; } 781 782 inline const float& 783 complex<float>::real() const 784 { return __real__ _M_value; } 785 786 inline float& 787 complex<float>::imag() 788 { return __imag__ _M_value; } 789 790 inline const float& 791 complex<float>::imag() const 792 { return __imag__ _M_value; } 793 794 inline 795 complex<float>::complex(float r, float i) 796 { 797 __real__ _M_value = r; 798 __imag__ _M_value = i; 799 } 800 801 inline complex<float>& 802 complex<float>::operator=(float __f) 803 { 804 __real__ _M_value = __f; 805 __imag__ _M_value = 0.0f; 806 return *this; 807 } 808 809 inline complex<float>& 810 complex<float>::operator+=(float __f) 811 { 812 __real__ _M_value += __f; 813 return *this; 814 } 815 816 inline complex<float>& 817 complex<float>::operator-=(float __f) 818 { 819 __real__ _M_value -= __f; 820 return *this; 821 } 822 823 inline complex<float>& 824 complex<float>::operator*=(float __f) 825 { 826 _M_value *= __f; 827 return *this; 828 } 829 830 inline complex<float>& 831 complex<float>::operator/=(float __f) 832 { 833 _M_value /= __f; 834 return *this; 835 } 836 837 template<typename _Tp> 838 inline complex<float>& 839 complex<float>::operator=(const complex<_Tp>& __z) 840 { 841 __real__ _M_value = __z.real(); 842 __imag__ _M_value = __z.imag(); 843 return *this; 844 } 845 846 template<typename _Tp> 847 inline complex<float>& 848 complex<float>::operator+=(const complex<_Tp>& __z) 849 { 850 __real__ _M_value += __z.real(); 851 __imag__ _M_value += __z.imag(); 852 return *this; 853 } 854 855 template<typename _Tp> 856 inline complex<float>& 857 complex<float>::operator-=(const complex<_Tp>& __z) 858 { 859 __real__ _M_value -= __z.real(); 860 __imag__ _M_value -= __z.imag(); 861 return *this; 862 } 863 864 template<typename _Tp> 865 inline complex<float>& 866 complex<float>::operator*=(const complex<_Tp>& __z) 867 { 868 _ComplexT __t; 869 __real__ __t = __z.real(); 870 __imag__ __t = __z.imag(); 871 _M_value *= __t; 872 return *this; 873 } 874 875 template<typename _Tp> 876 inline complex<float>& 877 complex<float>::operator/=(const complex<_Tp>& __z) 878 { 879 _ComplexT __t; 880 __real__ __t = __z.real(); 881 __imag__ __t = __z.imag(); 882 _M_value /= __t; 883 return *this; 884 } 885 886 // 26.2.3 complex specializations 887 // complex<double> specialization 888 template<> class complex<double> 889 { 890 public: 891 typedef double value_type; 892 893 complex(double = 0.0, double = 0.0); 894 895 complex(const complex<float>&); 896 explicit complex(const complex<long double>&); 897 898 double& real(); 899 const double& real() const; 900 double& imag(); 901 const double& imag() const; 902 903 complex<double>& operator=(double); 904 complex<double>& operator+=(double); 905 complex<double>& operator-=(double); 906 complex<double>& operator*=(double); 907 complex<double>& operator/=(double); 908 909 // The compiler will synthetize this, efficiently. 910 // complex& operator= (const complex&); 911 template<typename _Tp> 912 complex<double>& operator=(const complex<_Tp>&); 913 template<typename _Tp> 914 complex<double>& operator+=(const complex<_Tp>&); 915 template<typename _Tp> 916 complex<double>& operator-=(const complex<_Tp>&); 917 template<typename _Tp> 918 complex<double>& operator*=(const complex<_Tp>&); 919 template<typename _Tp> 920 complex<double>& operator/=(const complex<_Tp>&); 921 922 private: 923 typedef __complex__ double _ComplexT; 924 _ComplexT _M_value; 925 926 complex(_ComplexT __z) : _M_value(__z) { } 927 928 friend class complex<float>; 929 friend class complex<long double>; 930 }; 931 932 inline double& 933 complex<double>::real() 934 { return __real__ _M_value; } 935 936 inline const double& 937 complex<double>::real() const 938 { return __real__ _M_value; } 939 940 inline double& 941 complex<double>::imag() 942 { return __imag__ _M_value; } 943 944 inline const double& 945 complex<double>::imag() const 946 { return __imag__ _M_value; } 947 948 inline 949 complex<double>::complex(double __r, double __i) 950 { 951 __real__ _M_value = __r; 952 __imag__ _M_value = __i; 953 } 954 955 inline complex<double>& 956 complex<double>::operator=(double __d) 957 { 958 __real__ _M_value = __d; 959 __imag__ _M_value = 0.0; 960 return *this; 961 } 962 963 inline complex<double>& 964 complex<double>::operator+=(double __d) 965 { 966 __real__ _M_value += __d; 967 return *this; 968 } 969 970 inline complex<double>& 971 complex<double>::operator-=(double __d) 972 { 973 __real__ _M_value -= __d; 974 return *this; 975 } 976 977 inline complex<double>& 978 complex<double>::operator*=(double __d) 979 { 980 _M_value *= __d; 981 return *this; 982 } 983 984 inline complex<double>& 985 complex<double>::operator/=(double __d) 986 { 987 _M_value /= __d; 988 return *this; 989 } 990 991 template<typename _Tp> 992 inline complex<double>& 993 complex<double>::operator=(const complex<_Tp>& __z) 994 { 995 __real__ _M_value = __z.real(); 996 __imag__ _M_value = __z.imag(); 997 return *this; 998 } 999 1000 template<typename _Tp> 1001 inline complex<double>& 1002 complex<double>::operator+=(const complex<_Tp>& __z) 1003 { 1004 __real__ _M_value += __z.real(); 1005 __imag__ _M_value += __z.imag(); 1006 return *this; 1007 } 1008 1009 template<typename _Tp> 1010 inline complex<double>& 1011 complex<double>::operator-=(const complex<_Tp>& __z) 1012 { 1013 __real__ _M_value -= __z.real(); 1014 __imag__ _M_value -= __z.imag(); 1015 return *this; 1016 } 1017 1018 template<typename _Tp> 1019 inline complex<double>& 1020 complex<double>::operator*=(const complex<_Tp>& __z) 1021 { 1022 _ComplexT __t; 1023 __real__ __t = __z.real(); 1024 __imag__ __t = __z.imag(); 1025 _M_value *= __t; 1026 return *this; 1027 } 1028 1029 template<typename _Tp> 1030 inline complex<double>& 1031 complex<double>::operator/=(const complex<_Tp>& __z) 1032 { 1033 _ComplexT __t; 1034 __real__ __t = __z.real(); 1035 __imag__ __t = __z.imag(); 1036 _M_value /= __t; 1037 return *this; 1038 } 1039 1040 // 26.2.3 complex specializations 1041 // complex<long double> specialization 1042 template<> class complex<long double> 1043 { 1044 public: 1045 typedef long double value_type; 1046 1047 complex(long double = 0.0L, long double = 0.0L); 1048 1049 complex(const complex<float>&); 1050 complex(const complex<double>&); 1051 1052 long double& real(); 1053 const long double& real() const; 1054 long double& imag(); 1055 const long double& imag() const; 1056 1057 complex<long double>& operator= (long double); 1058 complex<long double>& operator+= (long double); 1059 complex<long double>& operator-= (long double); 1060 complex<long double>& operator*= (long double); 1061 complex<long double>& operator/= (long double); 1062 1063 // The compiler knows how to do this efficiently 1064 // complex& operator= (const complex&); 1065 template<typename _Tp> 1066 complex<long double>& operator=(const complex<_Tp>&); 1067 template<typename _Tp> 1068 complex<long double>& operator+=(const complex<_Tp>&); 1069 template<typename _Tp> 1070 complex<long double>& operator-=(const complex<_Tp>&); 1071 template<typename _Tp> 1072 complex<long double>& operator*=(const complex<_Tp>&); 1073 template<typename _Tp> 1074 complex<long double>& operator/=(const complex<_Tp>&); 1075 1076 private: 1077 typedef __complex__ long double _ComplexT; 1078 _ComplexT _M_value; 1079 1080 complex(_ComplexT __z) : _M_value(__z) { } 1081 1082 friend class complex<float>; 1083 friend class complex<double>; 1084 }; 1085 1086 inline 1087 complex<long double>::complex(long double __r, long double __i) 1088 { 1089 __real__ _M_value = __r; 1090 __imag__ _M_value = __i; 1091 } 1092 1093 inline long double& 1094 complex<long double>::real() 1095 { return __real__ _M_value; } 1096 1097 inline const long double& 1098 complex<long double>::real() const 1099 { return __real__ _M_value; } 1100 1101 inline long double& 1102 complex<long double>::imag() 1103 { return __imag__ _M_value; } 1104 1105 inline const long double& 1106 complex<long double>::imag() const 1107 { return __imag__ _M_value; } 1108 1109 inline complex<long double>& 1110 complex<long double>::operator=(long double __r) 1111 { 1112 __real__ _M_value = __r; 1113 __imag__ _M_value = 0.0L; 1114 return *this; 1115 } 1116 1117 inline complex<long double>& 1118 complex<long double>::operator+=(long double __r) 1119 { 1120 __real__ _M_value += __r; 1121 return *this; 1122 } 1123 1124 inline complex<long double>& 1125 complex<long double>::operator-=(long double __r) 1126 { 1127 __real__ _M_value -= __r; 1128 return *this; 1129 } 1130 1131 inline complex<long double>& 1132 complex<long double>::operator*=(long double __r) 1133 { 1134 _M_value *= __r; 1135 return *this; 1136 } 1137 1138 inline complex<long double>& 1139 complex<long double>::operator/=(long double __r) 1140 { 1141 _M_value /= __r; 1142 return *this; 1143 } 1144 1145 template<typename _Tp> 1146 inline complex<long double>& 1147 complex<long double>::operator=(const complex<_Tp>& __z) 1148 { 1149 __real__ _M_value = __z.real(); 1150 __imag__ _M_value = __z.imag(); 1151 return *this; 1152 } 1153 1154 template<typename _Tp> 1155 inline complex<long double>& 1156 complex<long double>::operator+=(const complex<_Tp>& __z) 1157 { 1158 __real__ _M_value += __z.real(); 1159 __imag__ _M_value += __z.imag(); 1160 return *this; 1161 } 1162 1163 template<typename _Tp> 1164 inline complex<long double>& 1165 complex<long double>::operator-=(const complex<_Tp>& __z) 1166 { 1167 __real__ _M_value -= __z.real(); 1168 __imag__ _M_value -= __z.imag(); 1169 return *this; 1170 } 1171 1172 template<typename _Tp> 1173 inline complex<long double>& 1174 complex<long double>::operator*=(const complex<_Tp>& __z) 1175 { 1176 _ComplexT __t; 1177 __real__ __t = __z.real(); 1178 __imag__ __t = __z.imag(); 1179 _M_value *= __t; 1180 return *this; 1181 } 1182 1183 template<typename _Tp> 1184 inline complex<long double>& 1185 complex<long double>::operator/=(const complex<_Tp>& __z) 1186 { 1187 _ComplexT __t; 1188 __real__ __t = __z.real(); 1189 __imag__ __t = __z.imag(); 1190 _M_value /= __t; 1191 return *this; 1192 } 1193 1194 // These bits have to be at the end of this file, so that the 1195 // specializations have all been defined. 1196 // ??? No, they have to be there because of compiler limitation at 1197 // inlining. It suffices that class specializations be defined. 1198 inline 1199 complex<float>::complex(const complex<double>& __z) 1200 : _M_value(_ComplexT(__z._M_value)) { } 1201 1202 inline 1203 complex<float>::complex(const complex<long double>& __z) 1204 : _M_value(_ComplexT(__z._M_value)) { } 1205 1206 inline 1207 complex<double>::complex(const complex<float>& __z) 1208 : _M_value(_ComplexT(__z._M_value)) { } 1209 1210 inline 1211 complex<double>::complex(const complex<long double>& __z) 1212 { 1213 __real__ _M_value = __z.real(); 1214 __imag__ _M_value = __z.imag(); 1215 } 1216 1217 inline 1218 complex<long double>::complex(const complex<float>& __z) 1219 : _M_value(_ComplexT(__z._M_value)) { } 1220 1221 inline 1222 complex<long double>::complex(const complex<double>& __z) 1223 : _M_value(_ComplexT(__z._M_value)) { } 1224 } // namespace std 1225 1226 #endif /* _GLIBCXX_COMPLEX */ 1227