C++复数运算
一个complex<double>和一个double相乘比较快还是还是两个complex<double>相乘比较快?前者好像要做一次typeconversion,但我还是...
一个complex<double>和一个double相乘比较快还是还是两个complex<double>相乘比较快?前者好像要做一次type conversion,但我还是比较乐观的猜想:编译器会不会优化成两次double相乘的运算?
希望给出出处,谢谢
如果有可能,希望讲一下c++内部复数运算的优化问题,谢谢 展开
希望给出出处,谢谢
如果有可能,希望讲一下c++内部复数运算的优化问题,谢谢 展开
2个回答
展开全部
可以看看complex的实现源码,希望有所帮助。
1.complex<double>与double
_Myt& operator*=(const _Ty& _Right)
{ // multiply by real
this->_Val[0] = this->_Val[0] * _Right;
this->_Val[1] = this->_Val[1] * _Right;
return (*this);
}
2.complex<double>与complex<double>
这个事两个复数相乘实现,调用_Mul()函数
_Myt& operator*=(const _Myt& _Right)
{ // multiply by other complex
this->_Mul(_Right);
return (*this);
}
_Mul()函数实现
template<class _Other> 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;
}
1.complex<double>与double
_Myt& operator*=(const _Ty& _Right)
{ // multiply by real
this->_Val[0] = this->_Val[0] * _Right;
this->_Val[1] = this->_Val[1] * _Right;
return (*this);
}
2.complex<double>与complex<double>
这个事两个复数相乘实现,调用_Mul()函数
_Myt& operator*=(const _Myt& _Right)
{ // multiply by other complex
this->_Mul(_Right);
return (*this);
}
_Mul()函数实现
template<class _Other> 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;
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询