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

来自cppreference.com
< cpp‎ | numeric‎ | math
 
 
 
 
在标头 <cmath> 定义
(1)
float       floor (float num );

double      floor (double num );

longdouble floor (longdouble num );
(C++23 前)
constexpr/* 浮点数类型 */
            floor (/* 浮点数类型 */ num );
(C++23 起)
float       floorf(float num );
(2) (C++11 起)
(C++23 起为 constexpr)
longdouble floorl(longdouble 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 重载。(C++23 起)
S) SIMD 重载对 v_num 实施逐元素 std::floor
(参见 math-floating-pointdeduced-simd-t 的定义。)
(C++26 起)
A) 为所有整数类型提供额外重载,将它们当做 double
(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)
最接近整数,中间情况下向远离零舍入
(函数)[编辑]
floor 的 C 文档
close