complex

Go to the documentation of this file.
00001 // The template and inlines for the -*- C++ -*- complex number classes. 00002 00003 // Copyright (C) 1997, 1998, 1999, 2000, 2001, 2002 00004 // Free Software Foundation, Inc. 00005 // 00006 // This file is part of the GNU ISO C++ Library. This library is free 00007 // software; you can redistribute it and/or modify it under the 00008 // terms of the GNU General Public License as published by the 00009 // Free Software Foundation; either version 2, or (at your option) 00010 // any later version. 00011 00012 // This library is distributed in the hope that it will be useful, 00013 // but WITHOUT ANY WARRANTY; without even the implied warranty of 00014 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00015 // GNU General Public License for more details. 00016 00017 // You should have received a copy of the GNU General Public License along 00018 // with this library; see the file COPYING. If not, write to the Free 00019 // Software Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, 00020 // USA. 00021 00022 // As a special exception, you may use this file as part of a free software 00023 // library without restriction. Specifically, if other files instantiate 00024 // templates or use macros or inline functions from this file, or you compile 00025 // this file and link it with other files to produce an executable, this 00026 // file does not by itself cause the resulting executable to be covered by 00027 // the GNU General Public License. This exception does not however 00028 // invalidate any other reasons why the executable file might be covered by 00029 // the GNU General Public License. 00030 00031 // 00032 // ISO C++ 14882: 26.2 Complex Numbers 00033 // Note: this is not a conforming implementation. 00034 // Initially implemented by Ulrich Drepper <drepper@cygnus.com> 00035 // Improved by Gabriel Dos Reis <dosreis@cmla.ens-cachan.fr> 00036 // 00037 00038 /** @file complex 00039 * This is a Standard C++ Library header. You should @c #include this header 00040 * in your programs, rather than any of the "st[dl]_*.h" implementation files. 00041 */ 00042 00043 #ifndef _CPP_COMPLEX 00044 #define _CPP_COMPLEX 1 00045 00046 #pragma GCC system_header 00047 00048 #include <bits/c++config.h> 00049 #include <bits/cpp_type_traits.h> 00050 #include <cmath> 00051 #include <sstream> 00052 00053 namespace std 00054 { 00055 // Forward declarations 00056 template<typename _Tp> class complex; 00057 template<> class complex<float>; 00058 template<> class complex<double>; 00059 template<> class complex<long double>; 00060 00061 template<typename _Tp> _Tp abs(const complex<_Tp>&); 00062 template<typename _Tp> _Tp arg(const complex<_Tp>&); 00063 template<typename _Tp> _Tp norm(const complex<_Tp>&); 00064 00065 template<typename _Tp> complex<_Tp> conj(const complex<_Tp>&); 00066 template<typename _Tp> complex<_Tp> polar(const _Tp&, const _Tp& = 0); 00067 00068 // Transcendentals: 00069 template<typename _Tp> complex<_Tp> cos(const complex<_Tp>&); 00070 template<typename _Tp> complex<_Tp> cosh(const complex<_Tp>&); 00071 template<typename _Tp> complex<_Tp> exp(const complex<_Tp>&); 00072 template<typename _Tp> complex<_Tp> log(const complex<_Tp>&); 00073 template<typename _Tp> complex<_Tp> log10(const complex<_Tp>&); 00074 template<typename _Tp> complex<_Tp> pow(const complex<_Tp>&, int); 00075 template<typename _Tp> complex<_Tp> pow(const complex<_Tp>&, const _Tp&); 00076 template<typename _Tp> complex<_Tp> pow(const complex<_Tp>&, 00077 const complex<_Tp>&); 00078 template<typename _Tp> complex<_Tp> pow(const _Tp&, const complex<_Tp>&); 00079 template<typename _Tp> complex<_Tp> sin(const complex<_Tp>&); 00080 template<typename _Tp> complex<_Tp> sinh(const complex<_Tp>&); 00081 template<typename _Tp> complex<_Tp> sqrt(const complex<_Tp>&); 00082 template<typename _Tp> complex<_Tp> tan(const complex<_Tp>&); 00083 template<typename _Tp> complex<_Tp> tanh(const complex<_Tp>&); 00084 00085 00086 // 26.2.2 Primary template class complex 00087 template<typename _Tp> 00088 class complex 00089 { 00090 public: 00091 typedef _Tp value_type; 00092 00093 complex(const _Tp& = _Tp(), const _Tp & = _Tp()); 00094 00095 // Let's the compiler synthetize the copy constructor 00096 // complex (const complex<_Tp>&); 00097 template<typename _Up> 00098 complex(const complex<_Up>&); 00099 00100 _Tp real() const; 00101 _Tp imag() const; 00102 00103 complex<_Tp>& operator=(const _Tp&); 00104 complex<_Tp>& operator+=(const _Tp&); 00105 complex<_Tp>& operator-=(const _Tp&); 00106 complex<_Tp>& operator*=(const _Tp&); 00107 complex<_Tp>& operator/=(const _Tp&); 00108 00109 // Let's the compiler synthetize the 00110 // copy and assignment operator 00111 // complex<_Tp>& operator= (const complex<_Tp>&); 00112 template<typename _Up> 00113 complex<_Tp>& operator=(const complex<_Up>&); 00114 template<typename _Up> 00115 complex<_Tp>& operator+=(const complex<_Up>&); 00116 template<typename _Up> 00117 complex<_Tp>& operator-=(const complex<_Up>&); 00118 template<typename _Up> 00119 complex<_Tp>& operator*=(const complex<_Up>&); 00120 template<typename _Up> 00121 complex<_Tp>& operator/=(const complex<_Up>&); 00122 00123 private: 00124 _Tp _M_real, _M_imag; 00125 }; 00126 00127 template<typename _Tp> 00128 inline _Tp 00129 complex<_Tp>::real() const { return _M_real; } 00130 00131 template<typename _Tp> 00132 inline _Tp 00133 complex<_Tp>::imag() const { return _M_imag; } 00134 00135 template<typename _Tp> 00136 inline 00137 complex<_Tp>::complex(const _Tp& __r, const _Tp& __i) 00138 : _M_real(__r), _M_imag(__i) { } 00139 00140 template<typename _Tp> 00141 template<typename _Up> 00142 inline 00143 complex<_Tp>::complex(const complex<_Up>& __z) 00144 : _M_real(__z.real()), _M_imag(__z.imag()) { } 00145 00146 template<typename _Tp> 00147 complex<_Tp>& 00148 complex<_Tp>::operator=(const _Tp& __t) 00149 { 00150 _M_real = __t; 00151 _M_imag = _Tp(); 00152 return *this; 00153 } 00154 00155 // 26.2.5/1 00156 template<typename _Tp> 00157 inline complex<_Tp>& 00158 complex<_Tp>::operator+=(const _Tp& __t) 00159 { 00160 _M_real += __t; 00161 return *this; 00162 } 00163 00164 // 26.2.5/3 00165 template<typename _Tp> 00166 inline complex<_Tp>& 00167 complex<_Tp>::operator-=(const _Tp& __t) 00168 { 00169 _M_real -= __t; 00170 return *this; 00171 } 00172 00173 // 26.2.5/5 00174 template<typename _Tp> 00175 complex<_Tp>& 00176 complex<_Tp>::operator*=(const _Tp& __t) 00177 { 00178 _M_real *= __t; 00179 _M_imag *= __t; 00180 return *this; 00181 } 00182 00183 // 26.2.5/7 00184 template<typename _Tp> 00185 complex<_Tp>& 00186 complex<_Tp>::operator/=(const _Tp& __t) 00187 { 00188 _M_real /= __t; 00189 _M_imag /= __t; 00190 return *this; 00191 } 00192 00193 template<typename _Tp> 00194 template<typename _Up> 00195 complex<_Tp>& 00196 complex<_Tp>::operator=(const complex<_Up>& __z) 00197 { 00198 _M_real = __z.real(); 00199 _M_imag = __z.imag(); 00200 return *this; 00201 } 00202 00203 // 26.2.5/9 00204 template<typename _Tp> 00205 template<typename _Up> 00206 complex<_Tp>& 00207 complex<_Tp>::operator+=(const complex<_Up>& __z) 00208 { 00209 _M_real += __z.real(); 00210 _M_imag += __z.imag(); 00211 return *this; 00212 } 00213 00214 // 26.2.5/11 00215 template<typename _Tp> 00216 template<typename _Up> 00217 complex<_Tp>& 00218 complex<_Tp>::operator-=(const complex<_Up>& __z) 00219 { 00220 _M_real -= __z.real(); 00221 _M_imag -= __z.imag(); 00222 return *this; 00223 } 00224 00225 // 26.2.5/13 00226 // XXX: This is a grammar school implementation. 00227 template<typename _Tp> 00228 template<typename _Up> 00229 complex<_Tp>& 00230 complex<_Tp>::operator*=(const complex<_Up>& __z) 00231 { 00232 const _Tp __r = _M_real * __z.real() - _M_imag * __z.imag(); 00233 _M_imag = _M_real * __z.imag() + _M_imag * __z.real(); 00234 _M_real = __r; 00235 return *this; 00236 } 00237 00238 // 26.2.5/15 00239 // XXX: This is a grammar school implementation. 00240 template<typename _Tp> 00241 template<typename _Up> 00242 complex<_Tp>& 00243 complex<_Tp>::operator/=(const complex<_Up>& __z) 00244 { 00245 const _Tp __r = _M_real * __z.real() + _M_imag * __z.imag(); 00246 const _Tp __n = norm(__z); 00247 _M_imag = (_M_imag * __z.real() - _M_real * __z.imag()) / __n; 00248 _M_real = __r / __n; 00249 return *this; 00250 } 00251 00252 // Operators: 00253 template<typename _Tp> 00254 inline complex<_Tp> 00255 operator+(const complex<_Tp>& __x, const complex<_Tp>& __y) 00256 { return complex<_Tp> (__x) += __y; } 00257 00258 template<typename _Tp> 00259 inline complex<_Tp> 00260 operator+(const complex<_Tp>& __x, const _Tp& __y) 00261 { return complex<_Tp> (__x) += __y; } 00262 00263 template<typename _Tp> 00264 inline complex<_Tp> 00265 operator+(const _Tp& __x, const complex<_Tp>& __y) 00266 { return complex<_Tp> (__y) += __x; } 00267 00268 template<typename _Tp> 00269 inline complex<_Tp> 00270 operator-(const complex<_Tp>& __x, const complex<_Tp>& __y) 00271 { return complex<_Tp> (__x) -= __y; } 00272 00273 template<typename _Tp> 00274 inline complex<_Tp> 00275 operator-(const complex<_Tp>& __x, const _Tp& __y) 00276 { return complex<_Tp> (__x) -= __y; } 00277 00278 template<typename _Tp> 00279 inline complex<_Tp> 00280 operator-(const _Tp& __x, const complex<_Tp>& __y) 00281 { return complex<_Tp> (__x) -= __y; } 00282 00283 template<typename _Tp> 00284 inline complex<_Tp> 00285 operator*(const complex<_Tp>& __x, const complex<_Tp>& __y) 00286 { return complex<_Tp> (__x) *= __y; } 00287 00288 template<typename _Tp> 00289 inline complex<_Tp> 00290 operator*(const complex<_Tp>& __x, const _Tp& __y) 00291 { return complex<_Tp> (__x) *= __y; } 00292 00293 template<typename _Tp> 00294 inline complex<_Tp> 00295 operator*(const _Tp& __x, const complex<_Tp>& __y) 00296 { return complex<_Tp> (__y) *= __x; } 00297 00298 template<typename _Tp> 00299 inline complex<_Tp> 00300 operator/(const complex<_Tp>& __x, const complex<_Tp>& __y) 00301 { return complex<_Tp> (__x) /= __y; } 00302 00303 template<typename _Tp> 00304 inline complex<_Tp> 00305 operator/(const complex<_Tp>& __x, const _Tp& __y) 00306 { return complex<_Tp> (__x) /= __y; } 00307 00308 template<typename _Tp> 00309 inline complex<_Tp> 00310 operator/(const _Tp& __x, const complex<_Tp>& __y) 00311 { return complex<_Tp> (__x) /= __y; } 00312 00313 template<typename _Tp> 00314 inline complex<_Tp> 00315 operator+(const complex<_Tp>& __x) 00316 { return __x; } 00317 00318 template<typename _Tp> 00319 inline complex<_Tp> 00320 operator-(const complex<_Tp>& __x) 00321 { return complex<_Tp>(-__x.real(), -__x.imag()); } 00322 00323 template<typename _Tp> 00324 inline bool 00325 operator==(const complex<_Tp>& __x, const complex<_Tp>& __y) 00326 { return __x.real() == __y.real() && __x.imag() == __y.imag(); } 00327 00328 template<typename _Tp> 00329 inline bool 00330 operator==(const complex<_Tp>& __x, const _Tp& __y) 00331 { return __x.real() == __y && __x.imag() == _Tp(); } 00332 00333 template<typename _Tp> 00334 inline bool 00335 operator==(const _Tp& __x, const complex<_Tp>& __y) 00336 { return __x == __y.real() && _Tp() == __y.imag(); } 00337 00338 template<typename _Tp> 00339 inline bool 00340 operator!=(const complex<_Tp>& __x, const complex<_Tp>& __y) 00341 { return __x.real() != __y.real() || __x.imag() != __y.imag(); } 00342 00343 template<typename _Tp> 00344 inline bool 00345 operator!=(const complex<_Tp>& __x, const _Tp& __y) 00346 { return __x.real() != __y || __x.imag() != _Tp(); } 00347 00348 template<typename _Tp> 00349 inline bool 00350 operator!=(const _Tp& __x, const complex<_Tp>& __y) 00351 { return __x != __y.real() || _Tp() != __y.imag(); } 00352 00353 template<typename _Tp, typename _CharT, class _Traits> 00354 basic_istream<_CharT, _Traits>& 00355 operator>>(basic_istream<_CharT, _Traits>& __is, complex<_Tp>& __x) 00356 { 00357 _Tp __re_x, __im_x; 00358 _CharT __ch; 00359 __is >> __ch; 00360 if (__ch == '(') 00361 { 00362 __is >> __re_x >> __ch; 00363 if (__ch == ',') 00364 { 00365 __is >> __im_x >> __ch; 00366 if (__ch == ')') 00367 __x = complex<_Tp>(__re_x, __im_x); 00368 else 00369 __is.setstate(ios_base::failbit); 00370 } 00371 else if (__ch == ')') 00372 __x = complex<_Tp>(__re_x, _Tp(0)); 00373 else 00374 __is.setstate(ios_base::failbit); 00375 } 00376 else 00377 { 00378 __is.putback(__ch); 00379 __is >> __re_x; 00380 __x = complex<_Tp>(__re_x, _Tp(0)); 00381 } 00382 return __is; 00383 } 00384 00385 template<typename _Tp, typename _CharT, class _Traits> 00386 basic_ostream<_CharT, _Traits>& 00387 operator<<(basic_ostream<_CharT, _Traits>& __os, const complex<_Tp>& __x) 00388 { 00389 basic_ostringstream<_CharT, _Traits> __s; 00390 __s.flags(__os.flags()); 00391 __s.imbue(__os.getloc()); 00392 __s.precision(__os.precision()); 00393 __s << '(' << __x.real() << ',' << __x.imag() << ')'; 00394 return __os << __s.str(); 00395 } 00396 00397 // Values 00398 template<typename _Tp> 00399 inline _Tp 00400 real(const complex<_Tp>& __z) 00401 { return __z.real(); } 00402 00403 template<typename _Tp> 00404 inline _Tp 00405 imag(const complex<_Tp>& __z) 00406 { return __z.imag(); } 00407 00408 template<typename _Tp> 00409 inline _Tp 00410 abs(const complex<_Tp>& __z) 00411 { 00412 _Tp __x = __z.real(); 00413 _Tp __y = __z.imag(); 00414 const _Tp __s = max(abs(__x), abs(__y)); 00415 if (__s == _Tp()) // well ... 00416 return __s; 00417 __x /= __s; 00418 __y /= __s; 00419 return __s * sqrt(__x * __x + __y * __y); 00420 } 00421 00422 template<typename _Tp> 00423 inline _Tp 00424 arg(const complex<_Tp>& __z) 00425 { return atan2(__z.imag(), __z.real()); } 00426 00427 // 26.2.7/5: norm(__z) returns the squared magintude of __z. 00428 // As defined, norm() is -not- a norm is the common mathematical 00429 // sens used in numerics. The helper class _Norm_helper<> tries to 00430 // distinguish between builtin floating point and the rest, so as 00431 // to deliver an answer as close as possible to the real value. 00432 template<bool> 00433 struct _Norm_helper 00434 { 00435 template<typename _Tp> 00436 static inline _Tp _S_do_it(const complex<_Tp>& __z) 00437 { 00438 const _Tp __x = __z.real(); 00439 const _Tp __y = __z.imag(); 00440 return __x * __x + __y * __y; 00441 } 00442 }; 00443 00444 template<> 00445 struct _Norm_helper<true> 00446 { 00447 template<typename _Tp> 00448 static inline _Tp _S_do_it(const complex<_Tp>& __z) 00449 { 00450 _Tp __res = abs(__z); 00451 return __res * __res; 00452 } 00453 }; 00454 00455 template<typename _Tp> 00456 inline _Tp 00457 norm(const complex<_Tp>& __z) 00458 { 00459 return _Norm_helper<__is_floating<_Tp>::_M_type && !_GLIBCPP_FAST_MATH>::_S_do_it(__z); 00460 } 00461 00462 template<typename _Tp> 00463 inline complex<_Tp> 00464 polar(const _Tp& __rho, const _Tp& __theta) 00465 { return complex<_Tp>(__rho * cos(__theta), __rho * sin(__theta)); } 00466 00467 template<typename _Tp> 00468 inline complex<_Tp> 00469 conj(const complex<_Tp>& __z) 00470 { return complex<_Tp>(__z.real(), -__z.imag()); } 00471 00472 // Transcendentals 00473 template<typename _Tp> 00474 inline complex<_Tp> 00475 cos(const complex<_Tp>& __z) 00476 { 00477 const _Tp __x = __z.real(); 00478 const _Tp __y = __z.imag(); 00479 return complex<_Tp>(cos(__x) * cosh(__y), -sin(__x) * sinh(__y)); 00480 } 00481 00482 template<typename _Tp> 00483 inline complex<_Tp> 00484 cosh(const complex<_Tp>& __z) 00485 { 00486 const _Tp __x = __z.real(); 00487 const _Tp __y = __z.imag(); 00488 return complex<_Tp>(cosh(__x) * cos(__y), sinh(__x) * sin(__y)); 00489 } 00490 00491 template<typename _Tp> 00492 inline complex<_Tp> 00493 exp(const complex<_Tp>& __z) 00494 { return polar(exp(__z.real()), __z.imag()); } 00495 00496 template<typename _Tp> 00497 inline complex<_Tp> 00498 log(const complex<_Tp>& __z) 00499 { return complex<_Tp>(log(abs(__z)), arg(__z)); } 00500 00501 template<typename _Tp> 00502 inline complex<_Tp> 00503 log10(const complex<_Tp>& __z) 00504 { return log(__z) / log(_Tp(10.0)); } 00505 00506 template<typename _Tp> 00507 inline complex<_Tp> 00508 sin(const complex<_Tp>& __z) 00509 { 00510 const _Tp __x = __z.real(); 00511 const _Tp __y = __z.imag(); 00512 return complex<_Tp>(sin(__x) * cosh(__y), cos(__x) * sinh(__y)); 00513 } 00514 00515 template<typename _Tp> 00516 inline complex<_Tp> 00517 sinh(const complex<_Tp>& __z) 00518 { 00519 const _Tp __x = __z.real(); 00520 const _Tp __y = __z.imag(); 00521 return complex<_Tp>(sinh(__x) * cos(__y), cosh(__x) * sin(__y)); 00522 } 00523 00524 template<typename _Tp> 00525 complex<_Tp> 00526 sqrt(const complex<_Tp>& __z) 00527 { 00528 _Tp __x = __z.real(); 00529 _Tp __y = __z.imag(); 00530 00531 if (__x == _Tp()) 00532 { 00533 _Tp __t = sqrt(abs(__y) / 2); 00534 return complex<_Tp>(__t, __y < _Tp() ? -__t : __t); 00535 } 00536 else 00537 { 00538 _Tp __t = sqrt(2 * (abs(__z) + abs(__x))); 00539 _Tp __u = __t / 2; 00540 return __x > _Tp() 00541 ? complex<_Tp>(__u, __y / __t) 00542 : complex<_Tp>(abs(__y) / __t, __y < _Tp() ? -__u : __u); 00543 } 00544 } 00545 00546 template<typename _Tp> 00547 inline complex<_Tp> 00548 tan(const complex<_Tp>& __z) 00549 { 00550 return sin(__z) / cos(__z); 00551 } 00552 00553 template<typename _Tp> 00554 inline complex<_Tp> 00555 tanh(const complex<_Tp>& __z) 00556 { 00557 return sinh(__z) / cosh(__z); 00558 } 00559 00560 template<typename _Tp> 00561 inline complex<_Tp> 00562 pow(const complex<_Tp>& __z, int __n) 00563 { 00564 return __pow_helper(__z, __n); 00565 } 00566 00567 template<typename _Tp> 00568 complex<_Tp> 00569 pow(const complex<_Tp>& __x, const _Tp& __y) 00570 { 00571 if (__x.imag() == _Tp()) 00572 return pow(__x.real(), __y); 00573 00574 complex<_Tp> __t = log(__x); 00575 return polar(exp(__y * __t.real()), __y * __t.imag()); 00576 } 00577 00578 template<typename _Tp> 00579 inline complex<_Tp> 00580 pow(const complex<_Tp>& __x, const complex<_Tp>& __y) 00581 { 00582 return __x == _Tp() ? _Tp() : exp(__y * log(__x)); 00583 } 00584 00585 template<typename _Tp> 00586 inline complex<_Tp> 00587 pow(const _Tp& __x, const complex<_Tp>& __y) 00588 { 00589 return __x == _Tp() 00590 ? _Tp() 00591 : polar(pow(__x, __y.real()), __y.imag() * log(__x)); 00592 } 00593 00594 // 26.2.3 complex specializations 00595 // complex<float> specialization 00596 template<> class complex<float> 00597 { 00598 public: 00599 typedef float value_type; 00600 00601 complex(float = 0.0f, float = 0.0f); 00602 #ifdef _GLIBCPP_BUGGY_COMPLEX 00603 complex(const complex& __z) : _M_value(__z._M_value) { } 00604 #endif 00605 explicit complex(const complex<double>&); 00606 explicit complex(const complex<long double>&); 00607 00608 float real() const; 00609 float imag() const; 00610 00611 complex<float>& operator=(float); 00612 complex<float>& operator+=(float); 00613 complex<float>& operator-=(float); 00614 complex<float>& operator*=(float); 00615 complex<float>& operator/=(float); 00616 00617 // Let's the compiler synthetize the copy and assignment 00618 // operator. It always does a pretty good job. 00619 // complex& operator= (const complex&); 00620 template<typename _Tp> 00621 complex<float>&operator=(const complex<_Tp>&); 00622 template<typename _Tp> 00623 complex<float>& operator+=(const complex<_Tp>&); 00624 template<class _Tp> 00625 complex<float>& operator-=(const complex<_Tp>&); 00626 template<class _Tp> 00627 complex<float>& operator*=(const complex<_Tp>&); 00628 template<class _Tp> 00629 complex<float>&operator/=(const complex<_Tp>&); 00630 00631 private: 00632 typedef __complex__ float _ComplexT; 00633 _ComplexT _M_value; 00634 00635 complex(_ComplexT __z) : _M_value(__z) { } 00636 00637 friend class complex<double>; 00638 friend class complex<long double>; 00639 }; 00640 00641 inline float 00642 complex<float>::real() const 00643 { return __real__ _M_value; } 00644 00645 inline float 00646 complex<float>::imag() const 00647 { return __imag__ _M_value; } 00648 00649 inline 00650 complex<float>::complex(float r, float i) 00651 { 00652 __real__ _M_value = r; 00653 __imag__ _M_value = i; 00654 } 00655 00656 inline complex<float>& 00657 complex<float>::operator=(float __f) 00658 { 00659 __real__ _M_value = __f; 00660 __imag__ _M_value = 0.0f; 00661 return *this; 00662 } 00663 00664 inline complex<float>& 00665 complex<float>::operator+=(float __f) 00666 { 00667 __real__ _M_value += __f; 00668 return *this; 00669 } 00670 00671 inline complex<float>& 00672 complex<float>::operator-=(float __f) 00673 { 00674 __real__ _M_value -= __f; 00675 return *this; 00676 } 00677 00678 inline complex<float>& 00679 complex<float>::operator*=(float __f) 00680 { 00681 _M_value *= __f; 00682 return *this; 00683 } 00684 00685 inline complex<float>& 00686 complex<float>::operator/=(float __f) 00687 { 00688 _M_value /= __f; 00689 return *this; 00690 } 00691 00692 template<typename _Tp> 00693 inline complex<float>& 00694 complex<float>::operator=(const complex<_Tp>& __z) 00695 { 00696 __real__ _M_value = __z.real(); 00697 __imag__ _M_value = __z.imag(); 00698 return *this; 00699 } 00700 00701 template<typename _Tp> 00702 inline complex<float>& 00703 complex<float>::operator+=(const complex<_Tp>& __z) 00704 { 00705 __real__ _M_value += __z.real(); 00706 __imag__ _M_value += __z.imag(); 00707 return *this; 00708 } 00709 00710 template<typename _Tp> 00711 inline complex<float>& 00712 complex<float>::operator-=(const complex<_Tp>& __z) 00713 { 00714 __real__ _M_value -= __z.real(); 00715 __imag__ _M_value -= __z.imag(); 00716 return *this; 00717 } 00718 00719 template<typename _Tp> 00720 inline complex<float>& 00721 complex<float>::operator*=(const complex<_Tp>& __z) 00722 { 00723 _ComplexT __t; 00724 __real__ __t = __z.real(); 00725 __imag__ __t = __z.imag(); 00726 _M_value *= __t; 00727 return *this; 00728 } 00729 00730 template<typename _Tp> 00731 inline complex<float>& 00732 complex<float>::operator/=(const complex<_Tp>& __z) 00733 { 00734 _ComplexT __t; 00735 __real__ __t = __z.real(); 00736 __imag__ __t = __z.imag(); 00737 _M_value /= __t; 00738 return *this; 00739 } 00740 00741 // 26.2.3 complex specializations 00742 // complex<double> specialization 00743 template<> class complex<double> 00744 { 00745 public: 00746 typedef double value_type; 00747 00748 complex(double =0.0, double =0.0); 00749 #ifdef _GLIBCPP_BUGGY_COMPLEX 00750 complex(const complex& __z) : _M_value(__z._M_value) { } 00751 #endif 00752 complex(const complex<float>&); 00753 explicit complex(const complex<long double>&); 00754 00755 double real() const; 00756 double imag() const; 00757 00758 complex<double>& operator=(double); 00759 complex<double>& operator+=(double); 00760 complex<double>& operator-=(double); 00761 complex<double>& operator*=(double); 00762 complex<double>& operator/=(double); 00763 00764 // The compiler will synthetize this, efficiently. 00765 // complex& operator= (const complex&); 00766 template<typename _Tp> 00767 complex<double>& operator=(const complex<_Tp>&); 00768 template<typename _Tp> 00769 complex<double>& operator+=(const complex<_Tp>&); 00770 template<typename _Tp> 00771 complex<double>& operator-=(const complex<_Tp>&); 00772 template<typename _Tp> 00773 complex<double>& operator*=(const complex<_Tp>&); 00774 template<typename _Tp> 00775 complex<double>& operator/=(const complex<_Tp>&); 00776 00777 private: 00778 typedef __complex__ double _ComplexT; 00779 _ComplexT _M_value; 00780 00781 complex(_ComplexT __z) : _M_value(__z) { } 00782 00783 friend class complex<float>; 00784 friend class complex<long double>; 00785 }; 00786 00787 inline double 00788 complex<double>::real() const 00789 { return __real__ _M_value; } 00790 00791 inline double 00792 complex<double>::imag() const 00793 { return __imag__ _M_value; } 00794 00795 inline 00796 complex<double>::complex(double __r, double __i) 00797 { 00798 __real__ _M_value = __r; 00799 __imag__ _M_value = __i; 00800 } 00801 00802 inline complex<double>& 00803 complex<double>::operator=(double __d) 00804 { 00805 __real__ _M_value = __d; 00806 __imag__ _M_value = 0.0; 00807 return *this; 00808 } 00809 00810 inline complex<double>& 00811 complex<double>::operator+=(double __d) 00812 { 00813 __real__ _M_value += __d; 00814 return *this; 00815 } 00816 00817 inline complex<double>& 00818 complex<double>::operator-=(double __d) 00819 { 00820 __real__ _M_value -= __d; 00821 return *this; 00822 } 00823 00824 inline complex<double>& 00825 complex<double>::operator*=(double __d) 00826 { 00827 _M_value *= __d; 00828 return *this; 00829 } 00830 00831 inline complex<double>& 00832 complex<double>::operator/=(double __d) 00833 { 00834 _M_value /= __d; 00835 return *this; 00836 } 00837 00838 template<typename _Tp> 00839 inline complex<double>& 00840 complex<double>::operator=(const complex<_Tp>& __z) 00841 { 00842 __real__ _M_value = __z.real(); 00843 __imag__ _M_value = __z.imag(); 00844 return *this; 00845 } 00846 00847 template<typename _Tp> 00848 inline complex<double>& 00849 complex<double>::operator+=(const complex<_Tp>& __z) 00850 { 00851 __real__ _M_value += __z.real(); 00852 __imag__ _M_value += __z.imag(); 00853 return *this; 00854 } 00855 00856 template<typename _Tp> 00857 inline complex<double>& 00858 complex<double>::operator-=(const complex<_Tp>& __z) 00859 { 00860 __real__ _M_value -= __z.real(); 00861 __imag__ _M_value -= __z.imag(); 00862 return *this; 00863 } 00864 00865 template<typename _Tp> 00866 inline complex<double>& 00867 complex<double>::operator*=(const complex<_Tp>& __z) 00868 { 00869 _ComplexT __t; 00870 __real__ __t = __z.real(); 00871 __imag__ __t = __z.imag(); 00872 _M_value *= __t; 00873 return *this; 00874 } 00875 00876 template<typename _Tp> 00877 inline complex<double>& 00878 complex<double>::operator/=(const complex<_Tp>& __z) 00879 { 00880 _ComplexT __t; 00881 __real__ __t = __z.real(); 00882 __imag__ __t = __z.imag(); 00883 _M_value /= __t; 00884 return *this; 00885 } 00886 00887 // 26.2.3 complex specializations 00888 // complex<long double> specialization 00889 template<> class complex<long double> 00890 { 00891 public: 00892 typedef long double value_type; 00893 00894 complex(long double = 0.0L, long double = 0.0L); 00895 #ifdef _GLIBCPP_BUGGY_COMPLEX 00896 complex(const complex& __z) : _M_value(__z._M_value) { } 00897 #endif 00898 complex(const complex<float>&); 00899 complex(const complex<double>&); 00900 00901 long double real() const; 00902 long double imag() const; 00903 00904 complex<long double>& operator= (long double); 00905 complex<long double>& operator+= (long double); 00906 complex<long double>& operator-= (long double); 00907 complex<long double>& operator*= (long double); 00908 complex<long double>& operator/= (long double); 00909 00910 // The compiler knows how to do this efficiently 00911 // complex& operator= (const complex&); 00912 template<typename _Tp> 00913 complex<long double>& operator=(const complex<_Tp>&); 00914 template<typename _Tp> 00915 complex<long double>& operator+=(const complex<_Tp>&); 00916 template<typename _Tp> 00917 complex<long double>& operator-=(const complex<_Tp>&); 00918 template<typename _Tp> 00919 complex<long double>& operator*=(const complex<_Tp>&); 00920 template<typename _Tp> 00921 complex<long double>& operator/=(const complex<_Tp>&); 00922 00923 private: 00924 typedef __complex__ long double _ComplexT; 00925 _ComplexT _M_value; 00926 00927 complex(_ComplexT __z) : _M_value(__z) { } 00928 00929 friend class complex<float>; 00930 friend class complex<double>; 00931 }; 00932 00933 inline 00934 complex<long double>::complex(long double __r, long double __i) 00935 { 00936 __real__ _M_value = __r; 00937 __imag__ _M_value = __i; 00938 } 00939 00940 inline long double 00941 complex<long double>::real() const 00942 { return __real__ _M_value; } 00943 00944 inline long double 00945 complex<long double>::imag() const 00946 { return __imag__ _M_value; } 00947 00948 inline complex<long double>& 00949 complex<long double>::operator=(long double __r) 00950 { 00951 __real__ _M_value = __r; 00952 __imag__ _M_value = 0.0L; 00953 return *this; 00954 } 00955 00956 inline complex<long double>& 00957 complex<long double>::operator+=(long double __r) 00958 { 00959 __real__ _M_value += __r; 00960 return *this; 00961 } 00962 00963 inline complex<long double>& 00964 complex<long double>::operator-=(long double __r) 00965 { 00966 __real__ _M_value -= __r; 00967 return *this; 00968 } 00969 00970 inline complex<long double>& 00971 complex<long double>::operator*=(long double __r) 00972 { 00973 _M_value *= __r; 00974 return *this; 00975 } 00976 00977 inline complex<long double>& 00978 complex<long double>::operator/=(long double __r) 00979 { 00980 _M_value /= __r; 00981 return *this; 00982 } 00983 00984 template<typename _Tp> 00985 inline complex<long double>& 00986 complex<long double>::operator=(const complex<_Tp>& __z) 00987 { 00988 __real__ _M_value = __z.real(); 00989 __imag__ _M_value = __z.imag(); 00990 return *this; 00991 } 00992 00993 template<typename _Tp> 00994 inline complex<long double>& 00995 complex<long double>::operator+=(const complex<_Tp>& __z) 00996 { 00997 __real__ _M_value += __z.real(); 00998 __imag__ _M_value += __z.imag(); 00999 return *this; 01000 } 01001 01002 template<typename _Tp> 01003 inline complex<long double>& 01004 complex<long double>::operator-=(const complex<_Tp>& __z) 01005 { 01006 __real__ _M_value -= __z.real(); 01007 __imag__ _M_value -= __z.imag(); 01008 return *this; 01009 } 01010 01011 template<typename _Tp> 01012 inline complex<long double>& 01013 complex<long double>::operator*=(const complex<_Tp>& __z) 01014 { 01015 _ComplexT __t; 01016 __real__ __t = __z.real(); 01017 __imag__ __t = __z.imag(); 01018 _M_value *= __t; 01019 return *this; 01020 } 01021 01022 template<typename _Tp> 01023 inline complex<long double>& 01024 complex<long double>::operator/=(const complex<_Tp>& __z) 01025 { 01026 _ComplexT __t; 01027 __real__ __t = __z.real(); 01028 __imag__ __t = __z.imag(); 01029 _M_value /= __t; 01030 return *this; 01031 } 01032 01033 // These bits have to be at the end of this file, so that the 01034 // specializations have all been defined. 01035 // ??? No, they have to be there because of compiler limitation at 01036 // inlining. It suffices that class specializations be defined. 01037 inline 01038 complex<float>::complex(const complex<double>& __z) 01039 : _M_value(_ComplexT(__z._M_value)) { } 01040 01041 inline 01042 complex<float>::complex(const complex<long double>& __z) 01043 : _M_value(_ComplexT(__z._M_value)) { } 01044 01045 inline 01046 complex<double>::complex(const complex<float>& __z) 01047 : _M_value(_ComplexT(__z._M_value)) { } 01048 01049 inline 01050 complex<double>::complex(const complex<long double>& __z) 01051 { 01052 __real__ _M_value = __z.real(); 01053 __imag__ _M_value = __z.imag(); 01054 } 01055 01056 inline 01057 complex<long double>::complex(const complex<float>& __z) 01058 : _M_value(_ComplexT(__z._M_value)) { } 01059 01060 inline 01061 complex<long double>::complex(const complex<double>& __z) 01062 : _M_value(_ComplexT(__z._M_value)) { } 01063 } // namespace std 01064 01065 #endif /* _CPP_COMPLEX */

Generated on Sun Sep 19 16:33:45 2004 for libstdc++-v3 Source by doxygen 1.3.8