# 取整函数

**向下取整函数**⌊x⌋把实数x映到小于或等于x的最大整数，**向上取整函数**⌈x⌉则把x映到大于或等于x的最小整数。二者合起来，便是把任意实数通过向下或向上舍入化为整数的标准手段，它们遍布于数论、组合数学与计算机科学之中。<sup>[1](https://mathworld.wolfram.com/FloorFunction.html)</sup><sup> • </sup><sup>[2](https://encyclopediaofmath.org/wiki/Entier)</sup>

| 要点 | 说明 |
|---|---|
| 定义 | ⌊x⌋是满足n ≤ x的最大整数n；⌈x⌉是满足n ≥ x的最小整数n。等价地，x − 1 < ⌊x⌋ ≤ x，且x ≤ ⌈x⌉ < x + 1。<sup>[3](https://dlmf.nist.gov/front/introduction)</sup> |
| 例子 | ⌊2.31⌋ = 2，⌈2.31⌉ = 3；⌊−1.1⌋ = −2，⌈−1.1⌉ = −1；若x本身是整数，两个函数都返回x。<sup>[4](https://www.mathsisfun.com/sets/function-floor-ceiling.html)</sup> |
| 名称与符号 | 由Kenneth E. Iverson于1962年首创；更早的标准写法是方括号记号[x]，可上溯至Gauss 1808年关于二次互反律的工作，Legendre则用过E(x)。<sup>[5](https://open-std.org/JTC1/SC22/WG21/docs/papers/2026/p4259r0.html)</sup><sup> • </sup><sup>[6](https://math.hawaii.edu/~tom/history/functions.html)</sup> |
| 小数部分 | {x} = x − ⌊x⌋，满足0 ≤ {x} < 1，因此x = ⌊x⌋ + {x}。<sup>[2](https://encyclopediaofmath.org/wiki/Entier)</sup> |
| 形状 | 二者都是阶梯函数，在每个整数处有跳跃间断；向下取整在每个整数处右连续而非左连续，向上取整则相反。<sup>[7](https://www.kufunda.net/publicdocs/IntroRealAnal.pdf)</sup> |
| 编程中的名称 | 几乎所有主流语言都把这两个一元函数称作`floor`与`ceil`或`ceiling`（C、C++、Java、Python、JavaScript、Rust、Go、R、Haskell、Excel等）。<sup>[5](https://open-std.org/JTC1/SC22/WG21/docs/papers/2026/p4259r0.html)</sup> |
| 标准参考书 | Graham、Knuth与Patashnik合著的《具体数学》第3章专论取整函数，涵盖其应用、递推式以及取整求和。<sup>[8](https://www.oreilly.com/library/view/concrete-mathematics-a/9780134389974/ch03.xhtml)</sup> |

## 定义与基本行为

对每个实数x，向下取整⌊x⌋ = max{n ∈ ℤ : n ≤ x}，向上取整⌈x⌉ = min{n ∈ ℤ : n ≥ x}。<sup>[9](https://arxiv.org/abs/2302.01900)</sup> 两个函数恰在整数处重合：⌊5⌋ = ⌈5⌉ = 5。对非整数，二者相差1，因此只要x不是整数，就有⌈x⌉ = ⌊x⌋ + 1。<sup>[4](https://www.mathsisfun.com/sets/function-floor-ceiling.html)</sup>

负的变量按定义、而非按直觉行事：⌊−2.4⌋ = −3，因为−3仍是小于或等于−2.4的最大整数。<sup>[10](https://reference.wolfram.com/language/ref/Floor)</sup> 正是在这里，向下取整与截断的差别最显眼：截断丢弃小数部分、朝零舍入，floor(−2.6) = −3，而trunc(−2.6) = −2。<sup>[11](https://support.ptc.com/help/engineering_notebook/r11.0/en/PTC_Mathcad_Help/truncation_and_rounding_functions.html)</sup>

减去向下取整便余下小数部分，{x} = x − ⌊x⌋，它总落在[0, 1)内。例如frac(3.65) = 0.65，frac(−3.65) = 0.35。<sup>[2](https://encyclopediaofmath.org/wiki/Entier)</sup><sup> • </sup><sup>[4](https://www.mathsisfun.com/sets/function-floor-ceiling.html)</sup> 小数部分函数以1为周期，且分段连续；Wolfram语言把它实现为`SawtoothWave`。<sup>[2](https://encyclopediaofmath.org/wiki/Entier)</sup><sup> • </sup><sup>[1](https://mathworld.wolfram.com/FloorFunction.html)</sup>

## 解析性质

向下取整函数是分段常值的：对每个整数n，它在半开区间[n, n + 1)上取值n。每个整数处都是跳跃间断点，因为该处两侧的单侧极限不相等；函数在每个整数处右连续而非左连续。<sup>[7](https://www.kufunda.net/publicdocs/IntroRealAnal.pdf)</sup> 它单调不减，但既非凸也非凹，且不是解析函数。<sup>[10](https://reference.wolfram.com/language/ref/Floor)</sup>

在任何有界闭区间上，向下取整函数有界且只有有限个间断点，故黎曼可积。在[0, m]上（m为正整数），[k, k + 1)一段贡献k，把等差数列求和，便得积分值m(m − 1)/2。<sup>[12](https://algebrica.org/floor-and-ceiling-functions/)</sup>

一条平移恒等式是大多数取整式子变形的根基：对任意整数n与实数x，⌊n + x⌋ = n + ⌊x⌋，⌈n + x⌉ = n + ⌈x⌉。<sup>[9](https://arxiv.org/abs/2302.01900)</sup> 由定义还能推出一些不等式，例如对任意实数x与y，⌊x⌋ + ⌊y⌋ ≤ ⌊x + y⌋ ≤ ⌊x⌋ + ⌊y⌋ + 1。<sup>[13](https://www.ams.org/bookstore/pspdf/amstext-63-prev.pdf)</sup> 含取整的求和如何处理，《具体数学》把它当作一套系统技法来讲授，晚近的论文也仍在完善这类求和公式。<sup>[8](https://www.oreilly.com/library/view/concrete-mathematics-a/9780134389974/ch03.xhtml)</sup><sup> • </sup><sup>[9](https://arxiv.org/abs/2302.01900)</sup>

## 记号的历史

这个函数本身远早于它的现代名称。据记号史家Grinstein（1970），方括号记号[x]源自Carl Friedrich Gauss在数论方面的工作，具体说是他1808年关于二次互反律的第三个证明；Adrien-Marie Legendre曾用今已废弃的记号E(x)指称此函数。<sup>[6](https://math.hawaii.edu/~tom/history/functions.html)</sup><sup> • </sup><sup>[1](https://mathworld.wolfram.com/FloorFunction.html)</sup> 方括号符号作为最大整数函数的记号，一直通行到二十世纪，如今仍见于许多旧作及部分近作。<sup>[1](https://mathworld.wolfram.com/FloorFunction.html)</sup>

现代记号与名称始于1962年，Kenneth E. Iverson在《一种编程语言》一书中引入了⌊x⌋与⌈x⌉连同“floor”“ceiling”两个词。<sup>[5](https://open-std.org/JTC1/SC22/WG21/docs/papers/2026/p4259r0.html)</sup><sup> • </sup><sup>[14](https://mathworld.wolfram.com/CeilingFunction.html)</sup> Graham、Knuth与Patashnik记载，Iverson发现排字工人可以把方括号的上下边削去，就得到了这两个符号。<sup>[8](https://www.oreilly.com/library/view/concrete-mathematics-a/9780134389974/ch03.xhtml)</sup> MathWorld记载，Manfred Schroeder因向上取整符号的形状，称之为“绞架”。<sup>[14](https://mathworld.wolfram.com/CeilingFunction.html)</sup> 由于[x]作为艾弗森括号（把条件转换成0或1的记号）仍有其用处，MathWorld建议不再用[x]表示向下取整。<sup>[1](https://mathworld.wolfram.com/FloorFunction.html)</sup>

## 在计算中的使用

取整函数在计算机科学中使用极广，多数编程语言都提供它们。<sup>[15](https://www.cs.iit.edu/~xli/cs330/Building%20Blocks%20for%20Theoretical%20Computer%20Science.pdf)</sup> 这两个一元函数的命名整齐得惊人：C、C++、Java、Python、JavaScript、Rust、Go、Ruby、PHP、MATLAB以及许多其他语言都用`floor`与`ceil`，R与Haskell用`ceiling`，Excel用`FLOOR`与`CEILING`。<sup>[5](https://open-std.org/JTC1/SC22/WG21/docs/papers/2026/p4259r0.html)</sup>

**分歧出在除法上。** 许多语言还提供二元整数函数，返回⌊x/y⌋与⌈x/y⌉，这里的命名就远未统一了。Rust提供`div_floor`与`div_ceil`；Julia与Swift Numerics则改用拼作“down”与“up”的舍入模式。Barry Revzin在2026年的一份C++委员会论文中主张，像`std::div_to_neg_inf`这样的拟议名称应当改叫`div_floor`与`div_ceil`，因为人们查找这些运算时用的正是既有的floor与ceiling之名。<sup>[5](https://open-std.org/JTC1/SC22/WG21/docs/papers/2026/p4259r0.html)</sup> 背后的问题确实存在：C++的整数除法朝零截断，<sup>[16](https://eel.is/c%2B%2Bdraft/expr.mul)</sup> 而Python的`//`运算符朝负无穷舍入，因此在负操作数下，同一个表达式在不同语言中可能得到不同的商。<sup>[17](https://www9.open-std.org/JTC1/SC22/WG21/docs/papers/2025/p3724r0.html)</sup> Jan Schultke的配套提案则记录了手写向上取整除法与向下取整除法多么容易出错，常见的Stack Overflow答案在负数或极端输入下会给出错误结果，或触发未定义行为。<sup>[18](https://open-std.org/jtc1/sc22/wg21/docs/papers/2026/p3724r4.html)</sup>

**具体应用**俯拾皆是。在散列中，乘法方法把表下标算作f(k) = ⌊m · frac(kA)⌋，其中m是表大小，Donald Knuth建议取A = (√5 − 1)/2 ≈ 0.6180339887；若键为100、m = 20，则得f(100) = 16。<sup>[19](https://www.cise.ufl.edu/~sahni/dsaac/enrich/c11/function.htm)</sup> 在数论中，素数p在n!的分解式中的指数由Legendre公式算出：α(p) = ⌊n/p⌋ + ⌊n/p²⌋ + ⋯。<sup>[2](https://encyclopediaofmath.org/wiki/Entier)</sup> 有些系统还推广了双参数形式：Wolfram语言的`Floor[x, a]`在a为正时返回小于或等于x的a的最大倍数，故`Floor[16, 3]`得15。<sup>[10](https://reference.wolfram.com/language/ref/Floor)</sup>

## 相关函数

取整函数处在一族舍入函数之中。截断朝零舍入，对正数变量与向下取整一致，对负数变量则与向上取整一致；Python的文档对`math.trunc`陈述了这一等价关系。<sup>[20](https://docs.python.org/3/library/math.html)</sup> 舍入到最近整数又是另一个函数，在Wolfram语言中实现为`Round`。<sup>[14](https://mathworld.wolfram.com/CeilingFunction.html)</sup> 小数部分{x} = x − ⌊x⌋补全了分解式x = ⌊x⌋ + {x}；而借整数部分函数来计算小数部分的计算器与语言，在负输入上可能彼此不一致，究竟取决于那个整数部分取的是向下取整还是截断。<sup>[4](https://www.mathsisfun.com/sets/function-floor-ceiling.html)</sup>

## References

1. [Floor Function, Wolfram MathWorld](https://mathworld.wolfram.com/FloorFunction.html)
2. [Integral part (Entier), Encyclopedia of Mathematics](https://encyclopediaofmath.org/wiki/Entier)
3. [DLMF: Mathematical Introduction, NIST Digital Library of Mathematical Functions](https://dlmf.nist.gov/front/introduction)
4. [Floor and Ceiling Functions, Math is Fun](https://www.mathsisfun.com/sets/function-floor-ceiling.html)
5. [Barry Revzin (2026). The name of the floor division function should have floor in it. P4259R0, WG21.](https://open-std.org/JTC1/SC22/WG21/docs/papers/2026/p4259r0.html)
6. [Earliest Uses of Function Symbols, University of Hawaii](https://math.hawaii.edu/~tom/history/functions.html)
7. [Introduction to Real Analysis (notes discussing continuity of floor and ceiling)](https://www.kufunda.net/publicdocs/IntroRealAnal.pdf)
8. [Graham, Knuth, Patashnik. Concrete Mathematics, 2nd ed., Chapter 3: Integer Functions, O'Reilly](https://www.oreilly.com/library/view/concrete-mathematics-a/9780134389974/ch03.xhtml)
9. [Podrug, Svrtan (2023). Some Refinements of Formulae Involving Floor and Ceiling Functions. arXiv:2302.01900.](https://arxiv.org/abs/2302.01900)
10. [Floor, Wolfram Language Documentation](https://reference.wolfram.com/language/ref/Floor)
11. [Truncation and Rounding Functions, PTC Mathcad Help](https://support.ptc.com/help/engineering_notebook/r11.0/en/PTC_Mathcad_Help/truncation_and_rounding_functions.html)
12. [Floor and Ceiling Functions, Algebrica](https://algebrica.org/floor-and-ceiling-functions/)
13. [AMS textbook preview containing the floor sum inequality exercise](https://www.ams.org/bookstore/pspdf/amstext-63-prev.pdf)
14. [Ceiling Function, Wolfram MathWorld](https://mathworld.wolfram.com/CeilingFunction.html)
15. [Building Blocks for Theoretical Computer Science (M. L. Li), Illinois Institute of Technology](https://www.cs.iit.edu/~xli/cs330/Building%20Blocks%20for%20Theoretical%20Computer%20Science.pdf)
16. [C++ draft standard, [expr.mul]](https://eel.is/c%2B%2Bdraft/expr.mul)
17. [Jan Schultke (2025). Integer division. P3724R0, WG21.](https://www9.open-std.org/JTC1/SC22/WG21/docs/papers/2025/p3724r0.html)
18. [Jan Schultke (2026). Integer division. P3724R4, WG21.](https://open-std.org/jtc1/sc22/wg21/docs/papers/2026/p3724r4.html)
19. [Hash Functions, Sartaj Sahni, Data Structures, Algorithms, & Applications in C++](https://www.cise.ufl.edu/~sahni/dsaac/enrich/c11/function.htm)
20. [math — Mathematical functions, Python 3 documentation](https://docs.python.org/3/library/math.html)

---
*Initially written Sep 17, 2026 · Reviewed: Sep 30, 2026; Oct 15, 2026 · Edited: Sep 30, 2026; Oct 15, 2026 · Last review: Oct 15, 2026*

Topic: 物质世界与数学 › 数学与统计学 › 数与代数 › Arithmetic and number systems › Number systems › Integers and rational numbers

Copyright 2026 Edgepedia

License: Edgepedia Community License 1.0, https://www.edgepedia.org/edgepedia/license

Source: https://www.edgepedia.org/zh/floor-and-ceiling-functions

English original: https://www.edgepedia.org/floor-and-ceiling-functions
