0

所以我正在开发一个表达式评估器,作为与工作相关的项目的内部组件。但是当涉及到浮点数学的输出时,我有一些奇怪的行为......

评估者接受一个字符串

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 科学记数法的浮点数都被四舍五入到最接近的整数。谁能解释为什么会发生这种情况以及我如何克服这个问题。

4

1 回答 1

1

您可以使用std::setprecision设置精度。

std::fixed的帮助下

于 2014-08-20T15:15:35.837 回答