阅读量:148
在C++中,floor函数是一个数学函数,用于返回小于或等于指定浮点数的最大整数值。
floor函数的用法如下:
#include
#include
int main() {
double x = 2.7;
double y = -3.4;
// 返回小于或等于x的最大整数值,结果为2
std::cout << "floor(" << x class="hljs-string">") = " << floor(x) << std class="hljs-comment">// 返回小于或等于y的最大整数值,结果为-4
std::cout << "floor(" << y class="hljs-string">") = " << floor(y) << std class="hljs-keyword">return 0;
}
输出结果:
floor(2.7) = 2
floor(-3.4) = -4
需要包含头文件 来使用floor函数。在函数参数中传入一个浮点数,floor函数会返回小于或等于该浮点数的最大整数值。