使用double和oStringStream时的奇怪行为
Odd behavior with doubles and oStringStream
所以我正在工作的表达式求值器作为一个工作相关项目的内部组件。但是当涉及到浮点数学的输出时,我有一些奇怪的行为…
求值器接受字符串
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的浮点数都被舍入到最接近的整数。谁能解释一下为什么会发生这种情况,我该如何克服这个问题?
可以使用std::setprecision来设置精度。
使用std::fixed
相关文章:
- 如何将一个ostringstream十六进制字符串字符对转换为单个unit8t等价的二进制值
- 为什么类中的ostringstream类型的成员会导致";调用隐含删除复制构造函数";错误
- C++在数学计算中将double转换为int
- 在c++中为double类型的数组创建一个unique_ptr
- vector<vector<double>> to mxArray using memcpy
- (double) 和 double() 之间的区别
- C++标准是否允许<double>在没有开销的情况下实现 std::可选
- C++ Version Of Double.longBitsToDouble
- 错误:类型"double()"和"double()"的操作数无效到二进制&quo
- 有没有办法在C++的赋值中将"char**"转换为"double"?
- 为什么 nlohmann/json 序列化 "null" 而不是在 double 上"0"?
- 使用 std::vector<double> 访问由 std::unique_ptr<double[2] 管理的数据>
- 将 std::vector<double> 从 C++ 包装到 C 以在 Swift 中使用
- 错误:无法将"<lambda(double)>"转换为"double(*)(double)"
- 如何正确将"strings"转换为"double"?
- 如何在<double>矢量中创建复杂类型的模板<T>?
- "double* grade"、"double *grade"和"double* fn()"有什么区别?
- 在不损失精度的情况下将double从C++传输到python
- 使用double和oStringStream时的奇怪行为
- 使用ostringstream将double格式化为字符串