因此,我正在处理一个表达式评估器,作为与工作相关的项目的内部组件。但是当涉及到浮点数学的输出时我有一些奇怪的行为.
评估器接受一个字符串。
e.evaluate("99989.3 + 2346.4");
//should be 102335.7
//result is 102336
//this function is what returns the result as a string
template <class TYPE> std::string Str( const TYPE & t ) {
//at this point t is equal to 102335.7
std::ostringstream os;
os << t;
// at this point os.str() == 102336
return os.str();似乎任何e+004科学表示法之上的浮点数都被四舍五入到最接近的整数。有谁能解释一下为什么会发生这种事,以及我会如何克服这个问题。
发布于 2014-08-20 15:15:35
您可以使用std:设定精度设置精度。
在科技促进发展:固定的帮助下
https://stackoverflow.com/questions/25408394
复制相似问题