关于有签名的std:圆,我有一个问题:double round (double x);
假设我有这样的代码:
int i = std::round(0.9);在这种情况下,std::round应该返回1.00000000000,但这与0.9999999999999非常接近,我担心浮点错误最终会舍弃它。
我希望i == 1,但这是保证吗?
发布于 2017-11-07 08:40:26
在int中添加0.5然后进行浇铸(截断)如何?
if(x < 0) { //number is negative
return (int) x - 0.5;
} else {
return (int) x + 0.5;//number is positive
}在您的示例中,它肯定会返回1,因为1.49999999999强制转换为int是1。
https://stackoverflow.com/questions/47153149
复制相似问题