【发布时间】:2021-12-17 04:53:53
【问题描述】:
我在 cppreference 中查看了这些相关的标准函数系列:std::round、std::floor、std::ceil 和 std::trunc。
为什么std::round 是唯一一个为long 和long long 提供特定签名作为返回类型的人?除了历史原因,我很难想出任何原因,但最近 C++11 中添加了 std::round。
【问题讨论】:
-
This isn't 100% a duplicate, but it answers your question。 (“
std::floor的返回值”的第一个谷歌结果) -
返回整数没有意义。
std::round只是出于某种原因。 -
@asynts 这是一个有趣的阅读,但它没有回答我的问题,我会想象问题中突出显示的相同限制将适用于 std::round,但不知何故标准库提供了特定的它的功能,而不是地板。
-
很可能是因为 c 有 exact same set of functions,但有趣的是知道为什么 c 没有为
ceil/floor/ @ 添加long/long long版本987654341@. -
@ALX23z
std::floor你的意思是std::trunc,对吧? :-) 不同之处在于,使用强制转换可以溢出,即 UB,而使用std::trunc则不能。