_complex.h

来自「stl的源码」· C头文件 代码 · 共 936 行 · 第 1/2 页

H
936
字号
/* * Copyright (c) 1999 * Silicon Graphics Computer Systems, Inc. * * Copyright (c) 1999 * Boris Fomitchev * * This material is provided "as is", with absolutely no warranty expressed * or implied. Any use is at your own risk. * * Permission to use or copy this software for any purpose is hereby granted * without fee, provided the above notices are retained on all copies. * Permission to modify the code and to distribute modified code is granted, * provided the above notices are retained, and a notice that the code was * modified is included with the above copyright notice. * */#ifndef _STLP_INTERNAL_COMPLEX#define _STLP_INTERNAL_COMPLEX// This header declares the template class complex, as described in// in the draft C++ standard.  Single-precision complex numbers// are complex<float>, double-precision are complex<double>, and// quad precision are complex<long double>.// Note that the template class complex is declared within namespace// std, as called for by the draft C++ standard.#ifndef _STLP_INTERNAL_CMATH#  include <stl/_cmath.h>#endif_STLP_BEGIN_NAMESPACEtemplate <class _Tp>struct complex {  typedef _Tp value_type;  typedef complex<_Tp> _Self;  // Constructors, destructor, assignment operator.  complex() : _M_re(0), _M_im(0) {}  complex(const value_type& __x)    : _M_re(__x), _M_im(0) {}  complex(const value_type& __x, const value_type& __y)    : _M_re(__x), _M_im(__y) {}  complex(const _Self& __z)    : _M_re(__z._M_re), _M_im(__z._M_im) {}  _Self& operator=(const _Self& __z) {    _M_re = __z._M_re;    _M_im = __z._M_im;    return *this;  }#if defined (_STLP_MEMBER_TEMPLATES) && defined (_STLP_FUNCTION_TMPL_PARTIAL_ORDER)  template <class _Tp2>  explicit complex(const complex<_Tp2>& __z)    : _M_re(__z._M_re), _M_im(__z._M_im) {}  template <class _Tp2>  _Self& operator=(const complex<_Tp2>& __z) {    _M_re = __z._M_re;    _M_im = __z._M_im;    return *this;  }#endif /* _STLP_MEMBER_TEMPLATES */  // Element access.  value_type real() const { return _M_re; }  value_type imag() const { return _M_im; }  // Arithmetic op= operations involving one real argument.  _Self& operator= (const value_type& __x) {    _M_re = __x;    _M_im = 0;    return *this;  }  _Self& operator+= (const value_type& __x) {    _M_re += __x;    return *this;  }  _Self& operator-= (const value_type& __x) {    _M_re -= __x;    return *this;  }  _Self& operator*= (const value_type& __x) {    _M_re *= __x;    _M_im *= __x;    return *this;  }  _Self& operator/= (const value_type& __x) {    _M_re /= __x;    _M_im /= __x;    return *this;  }  // Arithmetic op= operations involving two complex arguments.  static void  _STLP_CALL _div(const value_type& __z1_r, const value_type& __z1_i,                               const value_type& __z2_r, const value_type& __z2_i,                               value_type& __res_r, value_type& __res_i);  static void _STLP_CALL _div(const value_type& __z1_r,                              const value_type& __z2_r, const value_type& __z2_i,                              value_type& __res_r, value_type& __res_i);#if defined (_STLP_MEMBER_TEMPLATES) // && defined (_STLP_FUNCTION_TMPL_PARTIAL_ORDER)  template <class _Tp2> _Self& operator+= (const complex<_Tp2>& __z) {    _M_re += __z._M_re;    _M_im += __z._M_im;    return *this;  }  template <class _Tp2> _Self& operator-= (const complex<_Tp2>& __z) {    _M_re -= __z._M_re;    _M_im -= __z._M_im;    return *this;  }  template <class _Tp2> _Self& operator*= (const complex<_Tp2>& __z) {    value_type __r = _M_re * __z._M_re - _M_im * __z._M_im;    value_type __i = _M_re * __z._M_im + _M_im * __z._M_re;    _M_re = __r;    _M_im = __i;    return *this;  }  template <class _Tp2> _Self& operator/= (const complex<_Tp2>& __z) {    value_type __r;    value_type __i;    _div(_M_re, _M_im, __z._M_re, __z._M_im, __r, __i);    _M_re = __r;    _M_im = __i;    return *this;  }#endif /* _STLP_MEMBER_TEMPLATES */  _Self& operator+= (const _Self& __z) {    _M_re += __z._M_re;    _M_im += __z._M_im;    return *this;  }  _Self& operator-= (const _Self& __z) {    _M_re -= __z._M_re;    _M_im -= __z._M_im;    return *this;  }  _Self& operator*= (const _Self& __z) {    value_type __r = _M_re * __z._M_re - _M_im * __z._M_im;    value_type __i = _M_re * __z._M_im + _M_im * __z._M_re;    _M_re = __r;    _M_im = __i;    return *this;  }  _Self& operator/= (const _Self& __z) {    value_type __r;    value_type __i;    _div(_M_re, _M_im, __z._M_re, __z._M_im, __r, __i);    _M_re = __r;    _M_im = __i;    return *this;  }  // Data members.  value_type _M_re;  value_type _M_im;};// Explicit specializations for float, double, long double.  The only// reason for these specializations is to enable automatic conversions// from complex<float> to complex<double>, and complex<double> to// complex<long double>._STLP_TEMPLATE_NULLstruct _STLP_CLASS_DECLSPEC complex<float> {  typedef float value_type;  typedef complex<float> _Self;  // Constructors, destructor, assignment operator.  complex(value_type __x = 0.0f, value_type __y = 0.0f)    : _M_re(__x), _M_im(__y) {}  complex(const complex<float>& __z)    : _M_re(__z._M_re), _M_im(__z._M_im) {}  inline explicit complex(const complex<double>& __z);#ifndef _STLP_NO_LONG_DOUBLE  inline explicit complex(const complex<long double>& __z);#endif  // Element access.  value_type real() const { return _M_re; }  value_type imag() const { return _M_im; }  // Arithmetic op= operations involving one real argument.  _Self& operator= (value_type __x) {    _M_re = __x;    _M_im = 0.0f;    return *this;  }  _Self& operator+= (value_type __x) {    _M_re += __x;    return *this;  }  _Self& operator-= (value_type __x) {    _M_re -= __x;    return *this;  }  _Self& operator*= (value_type __x) {    _M_re *= __x;    _M_im *= __x;    return *this;  }  _Self& operator/= (value_type __x) {    _M_re /= __x;    _M_im /= __x;    return *this;  }  // Arithmetic op= operations involving two complex arguments.  static void _STLP_CALL _div(const float& __z1_r, const float& __z1_i,                              const float& __z2_r, const float& __z2_i,                              float& __res_r, float& __res_i);  static void _STLP_CALL _div(const float& __z1_r,                              const float& __z2_r, const float& __z2_i,                              float& __res_r, float& __res_i);#if defined (_STLP_MEMBER_TEMPLATES)  template <class _Tp2>  complex<float>& operator=(const complex<_Tp2>& __z) {    _M_re = __z._M_re;    _M_im = __z._M_im;    return *this;  }  template <class _Tp2>  complex<float>& operator+= (const complex<_Tp2>& __z) {    _M_re += __z._M_re;    _M_im += __z._M_im;    return *this;  }  template <class _Tp2>  complex<float>& operator-= (const complex<_Tp2>& __z) {    _M_re -= __z._M_re;    _M_im -= __z._M_im;    return *this;  }  template <class _Tp2>  complex<float>& operator*= (const complex<_Tp2>& __z) {    float __r = _M_re * __z._M_re - _M_im * __z._M_im;    float __i = _M_re * __z._M_im + _M_im * __z._M_re;    _M_re = __r;    _M_im = __i;    return *this;  }  template <class _Tp2>  complex<float>& operator/= (const complex<_Tp2>& __z) {    float __r;    float __i;    _div(_M_re, _M_im, __z._M_re, __z._M_im, __r, __i);    _M_re = __r;    _M_im = __i;    return *this;  }#endif /* _STLP_MEMBER_TEMPLATES */  _Self& operator=(const _Self& __z) {    _M_re = __z._M_re;    _M_im = __z._M_im;    return *this;  }  _Self& operator+= (const _Self& __z) {    _M_re += __z._M_re;    _M_im += __z._M_im;    return *this;  }  _Self& operator-= (const _Self& __z) {    _M_re -= __z._M_re;    _M_im -= __z._M_im;    return *this;  }  _Self& operator*= (const _Self& __z) {    value_type __r = _M_re * __z._M_re - _M_im * __z._M_im;    value_type __i = _M_re * __z._M_im + _M_im * __z._M_re;    _M_re = __r;    _M_im = __i;    return *this;  }  _Self& operator/= (const _Self& __z) {    value_type __r;    value_type __i;    _div(_M_re, _M_im, __z._M_re, __z._M_im, __r, __i);    _M_re = __r;    _M_im = __i;    return *this;  }  // Data members.  value_type _M_re;  value_type _M_im;};_STLP_TEMPLATE_NULLstruct _STLP_CLASS_DECLSPEC complex<double> {  typedef double value_type;  typedef complex<double> _Self;  // Constructors, destructor, assignment operator.  complex(value_type __x = 0.0, value_type __y = 0.0)    : _M_re(__x), _M_im(__y) {}  complex(const complex<double>& __z)    : _M_re(__z._M_re), _M_im(__z._M_im) {}  inline complex(const complex<float>& __z);#if !defined (_STLP_NO_LONG_DOUBLE)  explicit inline complex(const complex<long double>& __z);#endif  // Element access.  value_type real() const { return _M_re; }  value_type imag() const { return _M_im; }  // Arithmetic op= operations involving one real argument.  _Self& operator= (value_type __x) {    _M_re = __x;    _M_im = 0.0;    return *this;  }  _Self& operator+= (value_type __x) {    _M_re += __x;    return *this;  }  _Self& operator-= (value_type __x) {    _M_re -= __x;    return *this;  }  _Self& operator*= (value_type __x) {    _M_re *= __x;    _M_im *= __x;    return *this;  }  _Self& operator/= (value_type __x) {    _M_re /= __x;    _M_im /= __x;    return *this;  }  // Arithmetic op= operations involving two complex arguments.  static void _STLP_CALL _div(const double& __z1_r, const double& __z1_i,                              const double& __z2_r, const double& __z2_i,                              double& __res_r, double& __res_i);  static void _STLP_CALL _div(const double& __z1_r,                              const double& __z2_r, const double& __z2_i,                              double& __res_r, double& __res_i);#if defined (_STLP_MEMBER_TEMPLATES) && defined (_STLP_FUNCTION_TMPL_PARTIAL_ORDER)  template <class _Tp2>  complex<double>& operator=(const complex<_Tp2>& __z) {    _M_re = __z._M_re;    _M_im = __z._M_im;    return *this;  }  template <class _Tp2>  complex<double>& operator+= (const complex<_Tp2>& __z) {    _M_re += __z._M_re;    _M_im += __z._M_im;    return *this;  }  template <class _Tp2>  complex<double>& operator-= (const complex<_Tp2>& __z) {    _M_re -= __z._M_re;    _M_im -= __z._M_im;    return *this;  }  template <class _Tp2>  complex<double>& operator*= (const complex<_Tp2>& __z) {    double __r = _M_re * __z._M_re - _M_im * __z._M_im;    double __i = _M_re * __z._M_im + _M_im * __z._M_re;    _M_re = __r;    _M_im = __i;    return *this;  }  template <class _Tp2>  complex<double>& operator/= (const complex<_Tp2>& __z) {    double __r;    double __i;    _div(_M_re, _M_im, __z._M_re, __z._M_im, __r, __i);    _M_re = __r;    _M_im = __i;    return *this;  }#endif /* _STLP_MEMBER_TEMPLATES */  _Self& operator=(const _Self& __z) {    _M_re = __z._M_re;    _M_im = __z._M_im;    return *this;  }  _Self& operator+= (const _Self& __z) {    _M_re += __z._M_re;    _M_im += __z._M_im;    return *this;  }  _Self& operator-= (const _Self& __z) {    _M_re -= __z._M_re;    _M_im -= __z._M_im;    return *this;  }  _Self& operator*= (const _Self& __z) {    value_type __r = _M_re * __z._M_re - _M_im * __z._M_im;    value_type __i = _M_re * __z._M_im + _M_im * __z._M_re;    _M_re = __r;    _M_im = __i;    return *this;  }  _Self& operator/= (const _Self& __z) {    value_type __r;    value_type __i;    _div(_M_re, _M_im, __z._M_re, __z._M_im, __r, __i);    _M_re = __r;    _M_im = __i;    return *this;  }  // Data members.  value_type _M_re;  value_type _M_im;};#if !defined (_STLP_NO_LONG_DOUBLE)_STLP_TEMPLATE_NULLstruct _STLP_CLASS_DECLSPEC complex<long double> {  typedef long double value_type;  typedef complex<long double> _Self;  // Constructors, destructor, assignment operator.  complex(value_type __x = 0.0l, value_type __y = 0.0l)    : _M_re(__x), _M_im(__y) {}  complex(const complex<long double>& __z)    : _M_re(__z._M_re), _M_im(__z._M_im) {}  inline complex(const complex<float>& __z);

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?