61. Trunc and floor
Many computer languages have one or both of two operators trunc and floor which are related to div and are confusingly similar. Both are applied to real numbers.
61.1 Definition: floor
$\operatorname{Floor}(r)$, or the greatest integer in $r$, is the largest integer $n$ with the property $n\leq r$.
61.1.1 Example $\operatorname{floor}(3.1415) = 3$, $\operatorname{floor}(7/8) = 0$, and $\operatorname{floor}(-4.3) = -5$.
61.1.2 Usage $\operatorname{Floor}(r)$ is denoted by $\lfloor r\rfloor$ in modern texts, or by $[r]$ in older ones.
61.1.3 Exercise State a rule of inference for $\operatorname{floor}(r)$. (Answer on page 246.)
61.2 Definition: trunc
$\operatorname{Trunc}(r)$ is obtained from $r$ by expressing $r$ in decimal notation and dropping all digits after the decimal point.
61.2.1 Fact The function trunc satisfies the equation
$$\operatorname{trunc}(r) = \begin{cases} \operatorname{floor}(r) & r \geq 0 \text{ or } r \text{ an integer} \\ \operatorname{floor}(r) + 1 & r < 0 \text{ and not an integer} \end{cases}$$
61.2.2 Example $\operatorname{trunc}(-4.3) = -4$, but $\operatorname{floor}(-4.3) = -5$. On the other hand, $\operatorname{trunc}(-4) = \operatorname{floor}(-4) = -4$, and if $r$ is any positive real number, $\operatorname{trunc}(r) = \operatorname{floor}(r)$.
61.2.3 Exercise Find $\operatorname{trunc}(x)$ and $\operatorname{floor}(x)$ for
a) $x = 7/5$.
b) $x = -7/5$.
c) $x = -7$.
d) $x = -6.7$. (Answer on page 246.)
61.3 Quotients and remainders for negative integers
61.3.1 Example According to Definition 60.1, $-17 \operatorname{div} 5 = -4$ and $-17 \bmod 5 = 3$, because $-17 = (-4)\cdot 5 + 3$ and $0\leq 3<5$. In other words, the quotient is $\operatorname{floor}(-17/5)$, but not $\operatorname{trunc}(-17/5)$.
61.3.2 Usage A computer language which has an integer division (typically called div or “/”) which gives this answer for the quotient is said to have floored division. Mathematica has floored division.
Other possibilities include allowing the remainder in Definition 60.1 to be negative when $m$ is negative. This results in the quotient being trunc instead of floor, and, when implemented in a computer language, is called centered division. That is how many implementations of Pascal behave. When $n$ is negative the situation also allows several possibilities (depending on whether $m$ is negative or not).
In this book, integer division means floored division, so that it conforms to Definition 60.1.
Pages 86–87 of the book (PDF).