C++ std::floor 返回不大于参数的最大整数,结果仍为浮点。2.9 得到 2.0,-2.1 得到 -3.0(向负无穷方向取整)。
原型
double floor(double x);参数
x:浮点实参。
返回值
不大于 x 的最大整数,以 double 表示。
示例
#include <cmath>
#include <iostream>
int main() {
std::cout << std::floor(2.9) << std::endl;
std::cout << std::floor(-2.1) << std::endl;
return 0;
}输出:
2
-3
注意事项
截断小数部分(向零取整)应使用 trunc,与 floor 在负数上不同。分页、分桶时常把 floor 的结果转为整数下标。
下一页介绍fmod:浮点余数。