Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/135.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 双圆到最近的uint64\U t_C++_Floating Point_Rounding - Fatal编程技术网

C++ 双圆到最近的uint64\U t

C++ 双圆到最近的uint64\U t,c++,floating-point,rounding,C++,Floating Point,Rounding,我需要将双精度整数舍入到最近的有效uint64\u t 所以 std和boost中是否有任何函数可以启用此功能?Adouble无法保存uint64\t的所有值,因为这两个值通常都是64位,double需要为指数留出位 但是,要获得最接近的值并不难: uint64_t Round(double x){ double rounded=round(x); if(rounded<0) return 0; if(rounded>=pow(2,64))

我需要将双精度整数舍入到最近的有效
uint64\u t

所以


std和boost中是否有任何函数可以启用此功能?

A
double
无法保存
uint64\t
的所有值,因为这两个值通常都是64位,
double
需要为指数留出位

但是,要获得最接近的值并不难:

uint64_t Round(double x){
    double rounded=round(x);
    if(rounded<0)
        return 0;
    if(rounded>=pow(2,64))
       return std::numeric_limits<uint64_t>::max();
    return static_cast<uint64_t>(rounded);
}
uint64\u t圆形(双x){
双圆角=圆形(x);
如果(四舍五入=功率(2,64))
返回std::numeric_limits::max();
返回静态_型(四舍五入);
}

A
double
不能保存
uint64\u t
的所有值,因为这两个值通常都是64位,
double
需要为指数留出位

但是,要获得最接近的值并不难:

uint64_t Round(double x){
    double rounded=round(x);
    if(rounded<0)
        return 0;
    if(rounded>=pow(2,64))
       return std::numeric_limits<uint64_t>::max();
    return static_cast<uint64_t>(rounded);
}
uint64\u t圆形(双x){
双圆角=圆形(x);
如果(四舍五入=功率(2,64))
返回std::numeric_limits::max();
返回静态_型(四舍五入);
}

四舍五入到最接近的
uint64\t
与四舍五入到最接近的整数有何不同?我能想到的唯一一件事是它不适合
uint64\u t
,在这种情况下,您会做什么,确切地说?@ScottHunter,在第一个描述的测试用例中包括:最大
(远大于最大
uint64\u t
)应该一直四舍五入到最大的
uint64\u t
。四舍五入到最接近的
uint64\u t
与四舍五入到最接近的整数有何不同?我能想到的唯一一件事是它不适合
uint64\u t
,在这种情况下,您会做什么,确切地说?@ScottHunter,在第一个描述的测试用例中包括:最大
(远大于最大
uint64\u t
)应该一直向下舍入到最大的
uint64\t
。请注意溢出检查不正确。作为解释。
double
通常为64位,但也可能有不同的大小。例如,在一些Arduinos上,它是32位的。
pow
的一些实现不好,因此
pow(2,64)
可能不正确。替代方案包括:C++中的代码> 0x1p64 < /代码>,代码>(UTIN 64×Max/2 + 1)*2。<代码>,和<代码> 1844 67407370955 1616。< /代码>。注意溢出检查不正确。作为解释。
double
通常为64位,但也可能有不同的大小。例如,在一些Arduinos上,它是32位的。
pow
的一些实现不好,因此
pow(2,64)
可能不正确。替代方案包括:C++中的代码> 0x1p64 < /代码>,代码>(UTIN 64×Max/2 + 1)*2。< /C> >和<代码> 1844 67407370955 1616。< /代码>。
uint64_t Round(double x){
    double rounded=round(x);
    if(rounded<0)
        return 0;
    if(rounded>=pow(2,64))
       return std::numeric_limits<uint64_t>::max();
    return static_cast<uint64_t>(rounded);
}