【发布时间】:2019-11-29 22:45:06
【问题描述】:
我需要将双精度数四舍五入到最接近的有效uint64_t。
所以
uint64_t Round(double);
Round(std::numeric_limits<double>::max())==std::numeric_limits<uint64_t>::max();
Round(std::numeric_limits<double>::lowest())==std::numeric_limits<uint64_t>::min();
Round(1.1)==1;
Round 应该等价于但对于 uint64_t 而不是有符号整数
auto Round(double d){
std::fesetround(FE_TONEAREST);
return llrint(d);
}
std && boost 中是否有任何功能可以实现这一点?
【问题讨论】:
-
四舍五入到最接近的
uint64_t与四舍五入到最接近的整数有何不同?我唯一能想到的是它不适合uint64_t,在这种情况下你会做什么,究竟是什么?。 -
@ScottHunter 这在第一个描述的测试用例中有所介绍:最大值
double(远大于最大值uint64_t)应该一直向下舍入到最大值uint64_t。
标签: c++ floating-point rounding