<分区>
所以我正在研究一个表达式评估器作为工作相关项目的内部组件。但是当涉及到 float 学的输出时,我有一些奇怪的行为......
求值器接受一个字符串
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 科学记数法的 float 都被四舍五入到最接近的整数。任何人都可以解释为什么会发生这种情况以及我如何克服这个问题。