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

c++
1541
2024/1/6 14:22:36
栏目: 编程语言
开发者测试专用服务器限时活动,0元免费领,库存有限,领完即止! 点击查看>>

在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函数会返回小于或等于该浮点数的最大整数值。

辰迅云「云服务器」,即开即用、新一代英特尔至强铂金CPU、三副本存储NVMe SSD云盘,价格低至29元/月。点击查看>>

推荐阅读: 如何测试C++句柄类的稳定性和可靠性