On 2022-10-31, Kenny McCormack <
gaz...@shell.xmission.com> wrote:
> "man trunc" says:
>
> These functions round x to the nearest integer not larger in absolute value.
You're reading something called the "Linux Programmer's Manual", which
is second or third hand information. (Second if it refers to a standard,
third if it cribs from library docs/behavior.)
Glibc has its own documentation, which says it more clearly for regular
people, using the usual "toward zero":
The trunc functions round x towards zero to the nearest integer
(returned in floating-point format). Thus, trunc (1.5) is 1.0 and
trunc (-1.5) is -1.0.
You know from the name what it does, because truncation refers
to discarding the fractional part. The fractional part of a negative
proper fraction is itself negative: -1 1/2 means -1 + (-1/2).
The man page has really poor wording because although it is
mathematically correct, the "nearest" word is being used in a dumb way
in the context of rounding/truncation.
Yes, the integers -4, -3, -2, -1, 0, 1, 2, 3, 4 are all smaller in terms
of absolute value (magnitude) than -4.5, right? Oh, and of all those, -4
is the nearest. You know, in case you were stupidly wondering whether
-3 or -2, or even 2 could possibly be the result of trunc(-4.5)?
The psychology is bad; if your eyes see "nearest integer" in the
documentation of a truncating and rounding function of some kind, you're
automatically thinking about "the nearest integer in either direction",
like between 1 and 2, 1 is the nearer of the two to 1.2.
This is actually one of the better man pages.
If you're in the mood for amusing ranting, read "man 7 regex".