// complex standard header #pragma once #ifndef _COMPLEX_ #define _COMPLEX_ #include #include #include #pragma pack(push,8) #pragma warning(push,3) #pragma warning(disable: 4244) typedef struct _C_double_complex { /* double complex */ double _Val[2]; } _C_double_complex; typedef struct _C_float_complex { /* float complex */ float _Val[2]; } _C_float_complex; typedef struct _C_ldouble_complex { /* long double complex */ long double _Val[2]; } _C_ldouble_complex; _STD_BEGIN typedef ::_C_double_complex _Dcomplex_value; typedef ::_C_float_complex _Fcomplex_value; typedef ::_C_ldouble_complex _Lcomplex_value; #define __STD_COMPLEX /* signal presence of complex classes */ // TEMPLATE CLASS _Ctraits template class _Ctraits { // complex traits for _Ty public: static _Ty __cdecl _Cosh(_Ty _Left, _Ty _Right) { // return cosh(_Left) * _Right return (::_Cosh((double)_Left, (double)_Right)); } static short __cdecl _Exp(_Ty *_Pleft, _Ty _Right, short _Exponent) { // compute exp(*_Pleft) * _Right * 2 ^ _Exponent double _Tmp = (double)*_Pleft; short _Ans = ::_Exp(&_Tmp, (double)_Right, _Exponent); *_Pleft = (_Ty)_Tmp; return (_Ans); } static _Ty __cdecl _Infv(_Ty) { // return infinity return (::_Inf._Double); } static bool __cdecl _Isinf(_Ty _Left) { // test for infinity double _Tmp = (double)_Left; return (::_Dtest(&_Tmp) == _INFCODE); } static bool __cdecl _Isnan(_Ty _Left) { // test for NaN double _Tmp = (double)_Left; return (::_Dtest(&_Tmp) == _NANCODE); } static _Ty __cdecl _Nanv(_Ty) { // return NaN return (::_Nan._Double); } static _Ty __cdecl _Sinh(_Ty _Left, _Ty _Right) { // return sinh(_Left) * _Right return (::_Sinh((double)_Left, (double)_Right)); } static _Ty __cdecl atan2(_Ty _Yval, _Ty _Xval) { // return atan(_Yval / _Xval) return (::atan2((double)_Yval, (double)_Xval)); } static _Ty __cdecl cos(_Ty _Left) { // return cos(_Left) return (::cos((double)_Left)); } static _Ty __cdecl exp(_Ty _Left) { // return exp(_Left) return (::exp((double)_Left)); } static _Ty __cdecl ldexp(_Ty _Left, int _Exponent) { // return _Left * 2 ^ _Exponent return (::ldexp((double)_Left, _Exponent)); } static _Ty __cdecl log(_Ty _Left) { // return log(_Left) return (::log((double)_Left)); } static _Ty __cdecl pow(_Ty _Left, _Ty _Right) { // return _Left ^ _Right return (::pow((double)_Left, (double)_Right)); } static _Ty __cdecl sin(_Ty _Left) { // return sin(_Left) return (::sin((double)_Left)); } static _Ty __cdecl sqrt(_Ty _Left) { // return sqrt(_Left) return (::sqrt((double)_Left)); } static _Ty __cdecl tan(_Ty _Left) { // return tan(_Left) return (::tan((double)_Left)); } }; // CLASS _Ctraits template<> class _CRTIMP2 _Ctraits { // complex traits for long double public: typedef long double _Ty; static _Ty __cdecl _Cosh(_Ty _Left, _Ty _Right) { // return cosh(_Left) * _Right return (::_LCosh(_Left, _Right)); } static short __cdecl _Exp(_Ty *_Pleft, _Ty _Right, short _Exponent) { // compute exp(*_Pleft) * _Right * 2 ^ _Exponent return (::_LExp(_Pleft, _Right, _Exponent)); } static _Ty __cdecl _Infv(_Ty) { // return infinity return (::_LInf._Long_double); } static bool __cdecl _Isinf(_Ty _Left) { // test for infinity return (::_LDtest(&_Left) == _INFCODE); } static bool __cdecl _Isnan(_Ty _Left) { // test for NaN return (::_LDtest(&_Left) == _NANCODE); } static _Ty __cdecl _Nanv(_Ty) { // return NaN return (::_LNan._Long_double); } static _Ty __cdecl _Sinh(_Ty _Left, _Ty _Right) { // return sinh(_Left) * _Right return (::_LSinh(_Left, _Right)); } static _Ty __cdecl atan2(_Ty _Yval, _Ty _Xval) { // return atan(_Yval / _Xval) return (::atan2l(_Yval, _Xval)); } static _Ty __cdecl cos(_Ty _Left) { // return cos(_Left) return (::cosl(_Left)); } static _Ty __cdecl exp(_Ty _Left) { // return exp(_Left) return (::expl(_Left)); } static _Ty __cdecl ldexp(_Ty _Left, int _Exponent) { // return _Left * 2 ^ _Exponent return (::ldexpl(_Left, _Exponent)); } static _Ty __cdecl log(_Ty _Left) { // return log(_Left) return (::logl(_Left)); } static _Ty __cdecl pow(_Ty _Left, _Ty _Right) { // return _Left ^ _Right return (::powl(_Left, _Right)); } static _Ty __cdecl sin(_Ty _Left) { // return sin(_Left) return (::sinl(_Left)); } static _Ty __cdecl sqrt(_Ty _Left) { // return sqrt(_Left) return (::sqrtl(_Left)); } static _Ty __cdecl tan(_Ty _Left) { // return tan(_Left) return (::tanl(_Left)); } }; // CLASS _Ctraits template<> class _CRTIMP2 _Ctraits { // complex traits for double public: typedef double _Ty; static _Ty __cdecl _Cosh(_Ty _Left, _Ty _Right) { // return cosh(_Left) * _Right return (::_Cosh(_Left, _Right)); } static short __cdecl _Exp(_Ty *_Pleft, _Ty _Right, short _Exponent) { // compute exp(*_Pleft) * _Right * 2 ^ _Exponent return (::_Exp(_Pleft, _Right, _Exponent)); } static _Ty __cdecl _Infv(_Ty) { // return infinity return (::_Inf._Double); } static bool __cdecl _Isinf(_Ty _Left) { // test for infinity return (::_Dtest(&_Left) == _INFCODE); } static bool __cdecl _Isnan(_Ty _Left) { // test for NaN return (::_Dtest(&_Left) == _NANCODE); } static _Ty __cdecl _Nanv(_Ty) { // return NaN return (::_Nan._Double); } static _Ty __cdecl _Sinh(_Ty _Left, _Ty _Right) { // return sinh(_Left) * _Right return (::_Sinh(_Left, _Right)); } static _Ty __cdecl atan2(_Ty _Yval, _Ty _Xval) { // return atan(_Yval / _Xval) return (::atan2(_Yval, _Xval)); } static _Ty __cdecl cos(_Ty _Left) { // return cos(_Left) return (::cos(_Left)); } static _Ty __cdecl exp(_Ty _Left) { // return exp(_Left) return (::exp(_Left)); } static _Ty __cdecl ldexp(_Ty _Left, int _Exponent) { // return _Left * 2 ^ _Exponent return (::ldexp(_Left, _Exponent)); } static _Ty __cdecl log(_Ty _Left) { // return log(_Left) return (::log(_Left)); } static _Ty __cdecl pow(_Ty _Left, _Ty _Right) { // return _Left ^ _Right return (::pow(_Left, _Right)); } static _Ty __cdecl sin(_Ty _Left) { // return sin(_Left) return (::sin(_Left)); } static _Ty __cdecl sqrt(_Ty _Left) { // return sqrt(_Left) return (::sqrt(_Left)); } static _Ty __cdecl tan(_Ty _Left) { // return tan(_Left) return (::tan(_Left)); } }; // CLASS _Ctraits template<> class _CRTIMP2 _Ctraits { // complex traits for float public: typedef float _Ty; static _Ty __cdecl _Cosh(_Ty _Left, _Ty _Right) { // return cosh(_Left) * _Right return (::_FCosh(_Left, _Right)); } static short __cdecl _Exp(_Ty *_Pleft, _Ty _Right, short _Exponent) { // compute exp(*_Pleft) * _Right * 2 ^ _Exponent return (::_FExp(_Pleft, _Right, _Exponent)); } static _Ty __cdecl _Infv(_Ty) { // return infinity return (::_FInf._Float); } static bool __cdecl _Isinf(_Ty _Left) { // test for infinity return (::_FDtest(&_Left) == _INFCODE); } static bool __cdecl _Isnan(_Ty _Left) { // test for NaN return (::_FDtest(&_Left) == _NANCODE); } static _Ty __cdecl _Nanv(_Ty) { // return NaN return (::_FNan._Float); } static _Ty __cdecl _Sinh(_Ty _Left, _Ty _Right) { // return sinh(_Left) * _Right return (::_FSinh(_Left, _Right)); } static _Ty __cdecl atan2(_Ty _Yval, _Ty _Xval) { // return atan(_Yval / _Xval) return (::atan2f(_Yval, _Xval)); } static _Ty __cdecl cos(_Ty _Left) { // return cos(_Left) return (::cosf(_Left)); } static _Ty __cdecl exp(_Ty _Left) { // return exp(_Left) return (::expf(_Left)); } static _Ty __cdecl ldexp(_Ty _Left, int _Exponent) { // return _Left * 2 ^ _Exponent return (::ldexpf(_Left, _Exponent)); } static _Ty __cdecl log(_Ty _Left) { // return log(_Left) return (::logf(_Left)); } static _Ty __cdecl pow(_Ty _Left, _Ty _Right) { // return _Left ^ _Right return (::powf(_Left, _Right)); } static _Ty __cdecl sin(_Ty _Left) { // return sin(_Left) return (::sinf(_Left)); } static _Ty __cdecl sqrt(_Ty _Left) { // return sqrt(_Left) return (::sqrtf(_Left)); } static _Ty __cdecl tan(_Ty _Left) { // return tan(_Left) return (::tanf(_Left)); } }; template class complex; template<> class _CRTIMP2 complex; template<> class _CRTIMP2 complex; template<> class _CRTIMP2 complex; // TEMPLATE CLASS _Complex_value template struct _Complex_value { /* templatized complex value */ _Ty _Val[2]; }; // TEMPLATE CLASS _Complex_base template class _Complex_base : public _Valbase { // base for all complex types public: typedef _Ctraits<_Ty> _Myctraits; typedef _Complex_base<_Ty, _Valbase> _Myt; typedef _Ty value_type; _Complex_base(const _Ty& _Realval, const _Ty& _Imagval) { // construct from components of same type this->_Val[0] = _Realval; this->_Val[1] = _Imagval; } _Ty real(const _Ty& _Right) { // set real component return (this->_Val[0] = _Right); } _Ty imag(const _Ty& _Right) { // set imaginary component return (this->_Val[1] = _Right); } _Ty real() const { // return real component return (this->_Val[0]); } _Ty imag() const { // return imaginary component return (this->_Val[1]); } protected: template inline void _Add(const complex<_Other>& _Right) { // add other complex this->_Val[0] = this->_Val[0] + (_Ty)_Right.real(); this->_Val[1] = this->_Val[1] + (_Ty)_Right.imag(); } template inline void _Sub(const complex<_Other>& _Right) { // subtract other complex this->_Val[0] = this->_Val[0] - (_Ty)_Right.real(); this->_Val[1] = this->_Val[1] - (_Ty)_Right.imag(); } template inline void _Mul(const complex<_Other>& _Right) { // multiply by other complex _Ty _Rightreal = (_Ty)_Right.real(); _Ty _Rightimag = (_Ty)_Right.imag(); _Ty _Tmp = this->_Val[0] * _Rightreal - this->_Val[1] * _Rightimag; this->_Val[1] = this->_Val[0] * _Rightimag + this->_Val[1] * _Rightreal; this->_Val[0] = _Tmp; } template inline void _Div(const complex<_Other>& _Right) { // divide by other complex typedef _Ctraits<_Ty> _Myctraits; _Ty _Rightreal = (_Ty)_Right.real(); _Ty _Rightimag = (_Ty)_Right.imag(); if (_Myctraits::_Isnan(_Rightreal) || _Myctraits::_Isnan(_Rightimag)) { // set NaN result this->_Val[0] = _Myctraits::_Nanv(_Rightreal); this->_Val[1] = this->_Val[0]; } else if ((_Rightimag < 0 ? -_Rightimag : +_Rightimag) < (_Rightreal < 0 ? -_Rightreal : +_Rightreal)) { // |_Right.imag()| < |_Right.real()| _Ty _Wr = _Rightimag / _Rightreal; _Ty _Wd = _Rightreal + _Wr * _Rightimag; if (_Myctraits::_Isnan(_Wd) || _Wd == 0) { // set NaN result this->_Val[0] = _Myctraits::_Nanv(_Rightreal); this->_Val[1] = this->_Val[0]; } else { // compute representable result _Ty _Tmp = (this->_Val[0] + this->_Val[1] * _Wr) / _Wd; this->_Val[1] = (this->_Val[1] - this->_Val[0] * _Wr) / _Wd; this->_Val[0] = _Tmp; } } else if (_Rightimag == 0) { // set NaN result this->_Val[0] = _Myctraits::_Nanv(_Rightreal); this->_Val[1] = this->_Val[0]; } else { // 0 < |_Right.real()| <= |_Right.imag()| _Ty _Wr = _Rightreal / _Rightimag; _Ty _Wd = _Rightimag + _Wr * _Rightreal; if (_Myctraits::_Isnan(_Wd) || _Wd == 0) { // set NaN result this->_Val[0] = _Myctraits::_Nanv(_Rightreal); this->_Val[1] = this->_Val[0]; } else { // compute representable result _Ty _Tmp = (this->_Val[0] * _Wr + this->_Val[1]) / _Wd; this->_Val[1] = (this->_Val[1] * _Wr - this->_Val[0]) / _Wd; this->_Val[0] = _Tmp; } } } }; // CLASS complex template<> class _CRTIMP2 complex : public _Complex_base { // complex with float components public: typedef float _Ty; typedef complex<_Ty> _Myt; explicit complex(const complex&); // defined below explicit complex(const complex&); // defined below complex(const _Ty& _Realval = 0, const _Ty& _Imagval = 0) : _Complex_base(_Realval, _Imagval) { // construct from float components } complex(const _Fcomplex_value& _Right) : _Complex_base(_Right._Val[0], _Right._Val[1]) { // construct from float complex value } complex<_Ty>& operator=(const _Ty& _Right) { // assign real this->_Val[0] = _Right; this->_Val[1] = 0; return (*this); } _Myt& operator+=(const _Ty& _Right) { // add real this->_Val[0] = this->_Val[0] + _Right; return (*this); } _Myt& operator-=(const _Ty& _Right) { // subtract real this->_Val[0] = this->_Val[0] - _Right; return (*this); } _Myt& operator*=(const _Ty& _Right) { // multiply by real this->_Val[0] = this->_Val[0] * _Right; this->_Val[1] = this->_Val[1] * _Right; return (*this); } _Myt& operator/=(const _Ty& _Right) { // divide by real this->_Val[0] = this->_Val[0] / _Right; this->_Val[1] = this->_Val[1] / _Right; return (*this); } _Myt& operator=(const _Myt& _Right) { // assign other complex this->_Val[0] = _Right.real(); this->_Val[1] = _Right.imag(); return (*this); } _Myt& operator+=(const _Myt& _Right) { // add other complex this->_Add(_Right); return (*this); } _Myt& operator-=(const _Myt& _Right) { // subtract other complex this->_Sub(_Right); return (*this); } _Myt& operator*=(const _Myt& _Right) { // multiply by other complex this->_Mul(_Right); return (*this); } _Myt& operator/=(const _Myt& _Right) { // divide by other complex this->_Div(_Right); return (*this); } template _Myt& operator=(const complex<_Other>& _Right) { // assign other complex type this->_Val[0] = (_Ty)_Right.real(); this->_Val[1] = (_Ty)_Right.imag(); return (*this); } template inline _Myt& operator+=(const complex<_Other>& _Right) { // add other complex this->_Add(_Right); return (*this); } template inline _Myt& operator-=(const complex<_Other>& _Right) { // subtract other complex this->_Sub(_Right); return (*this); } template inline _Myt& operator*=(const complex<_Other>& _Right) { // multiply by other complex this->_Mul(_Right); return (*this); } template inline _Myt& operator/=(const complex<_Other>& _Right) { // divide by other complex this->_Div(_Right); return (*this); } }; // CLASS complex template<> class _CRTIMP2 complex : public _Complex_base { // complex with double components public: typedef double _Ty; typedef complex<_Ty> _Myt; complex(const complex&); // defined below explicit complex(const complex&); // defined below complex(const _Ty& _Realval = 0, const _Ty& _Imagval = 0) : _Complex_base(_Realval, _Imagval) { // construct from double components } complex(const _Dcomplex_value& _Right) : _Complex_base(_Right._Val[0], _Right._Val[1]) { // construct from double complex value } complex<_Ty>& operator=(const _Ty& _Right) { // assign real this->_Val[0] = _Right; this->_Val[1] = 0; return (*this); } _Myt& operator+=(const _Ty& _Right) { // add real this->_Val[0] = this->_Val[0] + _Right; return (*this); } _Myt& operator-=(const _Ty& _Right) { // subtract real this->_Val[0] = this->_Val[0] - _Right; return (*this); } _Myt& operator*=(const _Ty& _Right) { // multiply by real this->_Val[0] = this->_Val[0] * _Right; this->_Val[1] = this->_Val[1] * _Right; return (*this); } _Myt& operator/=(const _Ty& _Right) { // divide by real this->_Val[0] = this->_Val[0] / _Right; this->_Val[1] = this->_Val[1] / _Right; return (*this); } _Myt& operator=(const _Myt& _Right) { // assign other complex this->_Val[0] = _Right.real(); this->_Val[1] = _Right.imag(); return (*this); } _Myt& operator+=(const _Myt& _Right) { // add other complex this->_Add(_Right); return (*this); } _Myt& operator-=(const _Myt& _Right) { // subtract other complex this->_Sub(_Right); return (*this); } _Myt& operator*=(const _Myt& _Right) { // multiply by other complex this->_Mul(_Right); return (*this); } _Myt& operator/=(const _Myt& _Right) { // divide by other complex this->_Div(_Right); return (*this); } template _Myt& operator=(const complex<_Other>& _Right) { // assign other complex type this->_Val[0] = (_Ty)_Right.real(); this->_Val[1] = (_Ty)_Right.imag(); return (*this); } template inline _Myt& operator+=(const complex<_Other>& _Right) { // add other complex this->_Add(_Right); return (*this); } template inline _Myt& operator-=(const complex<_Other>& _Right) { // subtract other complex this->_Sub(_Right); return (*this); } template inline _Myt& operator*=(const complex<_Other>& _Right) { // multiply by other complex this->_Mul(_Right); return (*this); } template inline _Myt& operator/=(const complex<_Other>& _Right) { // divide by other complex this->_Div(_Right); return (*this); } }; // CLASS complex template<> class _CRTIMP2 complex : public _Complex_base { // complex with long double components public: typedef long double _Ty; typedef complex<_Ty> _Myt; complex(const complex&); // defined below complex(const complex&); // defined below complex(const _Ty& _Realval = 0, const _Ty& _Imagval = 0) : _Complex_base(_Realval, _Imagval) { // construct from long double components } complex(const _Lcomplex_value& _Right) : _Complex_base(_Right._Val[0], _Right._Val[1]) { // construct from long double complex value } complex<_Ty>& operator=(const _Ty& _Right) { // assign real this->_Val[0] = _Right; this->_Val[1] = 0; return (*this); } _Myt& operator+=(const _Ty& _Right) { // add real this->_Val[0] = this->_Val[0] + _Right; return (*this); } _Myt& operator-=(const _Ty& _Right) { // subtract real this->_Val[0] = this->_Val[0] - _Right; return (*this); } _Myt& operator*=(const _Ty& _Right) { // multiply by real this->_Val[0] = this->_Val[0] * _Right; this->_Val[1] = this->_Val[1] * _Right; return (*this); } _Myt& operator/=(const _Ty& _Right) { // divide by real this->_Val[0] = this->_Val[0] / _Right; this->_Val[1] = this->_Val[1] / _Right; return (*this); } _Myt& operator=(const _Myt& _Right) { // assign other complex this->_Val[0] = _Right.real(); this->_Val[1] = _Right.imag(); return (*this); } _Myt& operator+=(const _Myt& _Right) { // add other complex this->_Add(_Right); return (*this); } _Myt& operator-=(const _Myt& _Right) { // subtract other complex this->_Sub(_Right); return (*this); } _Myt& operator*=(const _Myt& _Right) { // multiply by other complex this->_Mul(_Right); return (*this); } _Myt& operator/=(const _Myt& _Right) { // divide by other complex this->_Div(_Right); return (*this); } template _Myt& operator=(const complex<_Other>& _Right) { // assign other complex type this->_Val[0] = (_Ty)_Right.real(); this->_Val[1] = (_Ty)_Right.imag(); return (*this); } template inline _Myt& operator+=(const complex<_Other>& _Right) { // add other complex this->_Add(_Right); return (*this); } template inline _Myt& operator-=(const complex<_Other>& _Right) { // subtract other complex this->_Sub(_Right); return (*this); } template inline _Myt& operator*=(const complex<_Other>& _Right) { // multiply by other complex this->_Mul(_Right); return (*this); } template inline _Myt& operator/=(const complex<_Other>& _Right) { // divide by other complex this->_Div(_Right); return (*this); } }; // CONSTRUCTORS FOR complex SPECIALIZATIONS inline complex::complex(const complex& _Right) : _Complex_base( (_Ty)_Right.real(), (_Ty)_Right.imag()) { // construct complex from complex } inline complex::complex(const complex& _Right) : _Complex_base( (_Ty)_Right.real(), (_Ty)_Right.imag()) { // construct complex from complex } inline complex::complex(const complex& _Right) : _Complex_base( (_Ty)_Right.real(), (_Ty)_Right.imag()) { // construct complex from complex } inline complex::complex(const complex& _Right) : _Complex_base( (_Ty)_Right.real(), (_Ty)_Right.imag()) { // construct complex from complex } inline complex::complex(const complex& _Right) : _Complex_base( (_Ty)_Right.real(), (_Ty)_Right.imag()) { // construct complex from complex } inline complex::complex(const complex& _Right) : _Complex_base( (_Ty)_Right.real(), (_Ty)_Right.imag()) { // construct complex from complex } // TEMPLATE CLASS complex template class complex : public _Complex_base<_Ty, _Complex_value<_Ty> > { // complex with _Ty components public: typedef complex<_Ty> _Myt; typedef _Complex_base<_Ty, _Complex_value<_Ty> > _Mybase; complex(const _Ty& _Realval = 0, const _Ty& _Imagval = 0) : _Mybase(_Realval, _Imagval) { // construct from components of same type } _Myt& operator=(const _Ty& _Right) { // assign real this->_Val[0] = _Right; this->_Val[1] = 0; return (*this); } template complex(const complex<_Other>& _Right) : _Mybase((_Ty)_Right.real(), (_Ty)_Right.imag()) { // construct from other complex type } template _Myt& operator=(const complex<_Other>& _Right) { // assign other complex type this->_Val[0] = (_Ty)_Right.real(); this->_Val[1] = (_Ty)_Right.imag(); return (*this); } _Myt& operator+=(const _Ty& _Right) { // add real this->_Val[0] = this->_Val[0] + _Right; return (*this); } _Myt& operator-=(const _Ty& _Right) { // subtract real this->_Val[0] = this->_Val[0] - _Right; return (*this); } _Myt& operator*=(const _Ty& _Right) { // multiply by real this->_Val[0] = this->_Val[0] * _Right; this->_Val[1] = this->_Val[1] * _Right; return (*this); } _Myt& operator/=(const _Ty& _Right) { // divide by real this->_Val[0] = this->_Val[0] / _Right; this->_Val[1] = this->_Val[1] / _Right; return (*this); } _Myt& operator+=(const _Myt& _Right) { // add other complex this->_Add(_Right); return (*this); } _Myt& operator-=(const _Myt& _Right) { // subtract other complex this->_Sub(_Right); return (*this); } _Myt& operator*=(const _Myt& _Right) { // multiply by other complex this->_Mul(_Right); return (*this); } _Myt& operator/=(const _Myt& _Right) { // divide by other complex this->_Div(_Right); return (*this); } template inline _Myt& operator+=(const complex<_Other>& _Right) { // add other complex this->_Add(_Right); return (*this); } template inline _Myt& operator-=(const complex<_Other>& _Right) { // subtract other complex this->_Sub(_Right); return (*this); } template inline _Myt& operator*=(const complex<_Other>& _Right) { // multiply by other complex this->_Mul(_Right); return (*this); } template inline _Myt& operator/=(const complex<_Other>& _Right) { // divide by other complex this->_Div(_Right); return (*this); } }; #define _CMPLX(T) complex #define _CTR(T) _Ctraits #define _TMPLT(T) template #include /* define all complex template functions */ // TEMPLATE FUNCTION operator>> template inline basic_istream<_Elem, _Tr>& __cdecl operator>>( basic_istream<_Elem, _Tr>& _Istr, complex<_Ty>& _Right) { // extract a complex<_Ty> typedef complex<_Ty> _Myt; const ctype<_Elem>& _Ctype_fac = _USE(_Istr.getloc(), ctype<_Elem>); _Elem _Ch; long double _Real, _Imag = 0; if (_Istr >> _Ch && _Ch != _Ctype_fac.widen('(')) { // no leading '(', treat as real only _Istr.putback(_Ch); _Istr >> _Real; _Imag = 0; } else if (_Istr >> _Real >> _Ch && _Ch != _Ctype_fac.widen(',')) if (_Ch == _Ctype_fac.widen(')')) _Imag = 0; // (real) else { // no trailing ')' after real, treat as bad field _Istr.putback(_Ch); _Istr.setstate(ios_base::failbit); } else if (_Istr >> _Imag >> _Ch && _Ch != _Ctype_fac.widen(')')) { // no imag or trailing ')', treat as bad field _Istr.putback(_Ch); _Istr.setstate(ios_base::failbit); } if (!_Istr.fail()) { // store valid result _Ty _Tyreal((_Ty)_Real), _Tyimag((_Ty)_Imag); _Right = _Myt(_Tyreal, _Tyimag); } return (_Istr); } // TEMPLATE FUNCTION operator<< template inline basic_ostream<_Elem, _Tr>& __cdecl operator<<( basic_ostream<_Elem, _Tr>& _Ostr, const complex<_Ty>& _Right) { // insert a complex<_Ty> const ctype<_Elem>& _Ctype_fac = _USE(_Ostr.getloc(), ctype<_Elem>); basic_ostringstream<_Elem, _Tr, allocator<_Elem> > _Sstr; _Sstr.flags(_Ostr.flags()); _Sstr.imbue(_Ostr.getloc()); _Sstr.precision(_Ostr.precision()); _Sstr << _Ctype_fac.widen('(') << real(_Right) << _Ctype_fac.widen(',') << imag(_Right) << _Ctype_fac.widen(')'); basic_string<_Elem, _Tr, allocator<_Elem> > _Str = _Sstr.str(); return (_Ostr << _Str.c_str()); } #ifdef _DLL_CPPLIB template _CRTIMP2 basic_istream >& __cdecl operator>>(basic_istream >&, complex&); template _CRTIMP2 basic_ostream >& __cdecl operator<<(basic_ostream >&, const complex&); template _CRTIMP2 basic_istream >& __cdecl operator>>(basic_istream >&, complex&); template _CRTIMP2 basic_ostream >& __cdecl operator<<(basic_ostream >&, const complex&); template _CRTIMP2 basic_istream >& __cdecl operator>>(basic_istream >&, complex&); template _CRTIMP2 basic_ostream >& __cdecl operator<<(basic_ostream >&, const complex&); template _CRTIMP2 basic_istream >& __cdecl operator>>(basic_istream >&, complex&); template _CRTIMP2 basic_ostream >& __cdecl operator<<(basic_ostream >&, const complex&); template _CRTIMP2 basic_istream >& __cdecl operator>>(basic_istream >&, complex&); template _CRTIMP2 basic_ostream >& __cdecl operator<<(basic_ostream >&, const complex&); template _CRTIMP2 basic_istream >& __cdecl operator>>(basic_istream >&, complex&); template _CRTIMP2 basic_ostream >& __cdecl operator<<(basic_ostream >&, const complex&); #endif /* _DLL_CPPLIB */ _STD_END #pragma warning(default: 4244) #pragma warning(pop) #pragma pack(pop) #endif /* _COMPLEX_ */ /* * Copyright (c) 1992-2002 by P.J. Plauger. ALL RIGHTS RESERVED. * Consult your license regarding permissions and restrictions. V3.13:0009 */