温馨提示×

c++中floor函数的用法是什么

c++
小亿
145
2024-01-31 14:22:36
栏目: 编程语言

在C++中,floor函数是一个数学函数,用于返回小于或等于指定浮点数的最大整数值。

floor函数的用法如下:

#include <iostream>
#include <cmath>

int main() {
    double x = 2.7;
    double y = -3.4;
    
    // 返回小于或等于x的最大整数值,结果为2
    std::cout << "floor(" << x << ") = " << floor(x) << std::endl;
    
    // 返回小于或等于y的最大整数值,结果为-4
    std::cout << "floor(" << y << ") = " << floor(y) << std::endl;
    
    return 0;
}

输出结果:

floor(2.7) = 2
floor(-3.4) = -4

需要包含头文件 <cmath> 来使用floor函数。在函数参数中传入一个浮点数,floor函数会返回小于或等于该浮点数的最大整数值。

0