Namespaces
Variants

std:: floor, std:: floorf, std:: floorl

From cppreference.net
Common mathematical functions
Nearest integer floating point operations
floor
(C++11) (C++11) (C++11)
(C++11)
(C++11)
(C++11) (C++11) (C++11)
Floating point manipulation functions
(C++11) (C++11)
(C++11)
(C++11)
Classification and comparison
(C++11)
(C++11)
(C++11)
(C++11)
(C++11)
(C++11)
Types
(C++11)
(C++11)
(C++11)
Macro constants
定义于头文件 <cmath>
(1)
float floor ( float num ) ;

double floor ( double num ) ;

long double floor ( long double num ) ;
(C++23 前)
constexpr /*floating-point-type*/
floor ( /*floating-point-type*/ num ) ;
(C++23 起)
float floorf ( float num ) ;
(2) (C++11 起)
(C++23 起为 constexpr)
long double floorl ( long double num ) ;
(3) (C++11 起)
(C++23 起为 constexpr)
SIMD 重载 (C++26 起)
定义于头文件 <simd>
template < /*math-floating-point*/ V >

constexpr /*deduced-simd-t*/ < V >

floor ( const V & v_num ) ;
(S) (C++26 起)
额外重载 (C++11 起)
定义于头文件 <cmath>
template < class Integer >
double floor ( Integer num ) ;
(A) (C++23 起为 constexpr)
1-3) 计算不大于 num 的最大整数值。 标准库为所有 cv 未限定浮点类型提供了 std::floor 的重载版本作为参数类型。 (since C++23)
S) SIMD 重载对 v_num 执行逐元素的 std::floor 运算。
(参见 math-floating-point deduced-simd-t 了解其定义。)
(since C++26)
A) 为所有整数类型提供了额外的重载,这些类型被视为 double
(since C++11)

目录

参数

num - 浮点数或整数值

返回值

如果没有发生错误,则返回不大于 num 的最大整数值,即 ⌊num⌋

返回值
math-floor.svg
num

错误处理

错误报告方式遵循 math_errhandling 中的规范。

如果实现支持 IEEE 浮点算术 (IEC 60559),

  • 当前的 舍入模式 无效。
  • num 为 ±∞,则直接返回该值。
  • num 为 ±0,则直接返回该值。
  • num 为 NaN,则返回 NaN。

注释

FE_INEXACT 可能在舍入非整数有限值时被引发(但不是必须被引发)。

在所有标准浮点格式中,最大可表示的浮点值都是精确整数,因此该函数本身永远不会溢出;然而当结果存储在整数变量中时,可能会溢出任何整数类型(包括 std::intmax_t )。

额外的重载不需要完全按照 (A) 提供。它们只需确保对于整数类型的实参 num std :: floor ( num ) 能够产生与 std :: floor ( static_cast < double > ( num ) ) 相同的效果。

示例

#include <cmath>
#include <iostream>
int main()
{
    std::cout << std::fixed
              << "floor(+2.7) = " << std::floor(+2.7) << '\n'
              << "floor(-2.7) = " << std::floor(-2.7) << '\n'
              << "floor(-0.0) = " << std::floor(-0.0) << '\n'
              << "floor(-Inf) = " << std::floor(-INFINITY) << '\n';
}

输出:

floor(+2.7) = 2.000000
floor(-2.7) = -3.000000
floor(-0.0) = -0.000000
floor(-Inf) = -inf

参见

(C++11) (C++11)
不小于给定值的最小整数
(函数)
(C++11) (C++11) (C++11)
幅度不大于给定值的最接近整数
(函数)
(C++11) (C++11) (C++11) (C++11) (C++11) (C++11) (C++11) (C++11) (C++11)
最接近整数,中间情况向远离零方向舍入
(函数)
C 文档 for floor