Debian Codegraph @

texk/web2c/lib/zround.c

raw
1 /* zround.c: round R to the nearest whole number. This is supposed to
2 implement the predefined Pascal round function. Public domain. */
3
4 #include <w2c/config.h>
5 #include "lib.h"
6
7 integer
8 zround (double r)
9 {
10 integer i;
11
12 /* R can be outside the range of an integer if glue is stretching or
13 shrinking a lot. We can't do any better than returning the largest
14 or smallest integer possible in that case. It doesn't seem to make
15 any practical difference. Here is a sample input file which
16 demonstrates the problem, from [email protected]:
17 \documentstyle{article}
18 \begin{document}
19 \begin{flushleft}
20 $\hbox{} $\hfill
21 \filbreak
22 \eject
23
24 [email protected] points out we should testing against
25 TeX's largest or smallest integer (32 bits), not the machine's. So
26 we might as well use a floating-point constant, and avoid potential
27 compiler bugs (also noted by djb, on BSDI). */
28 if (r > 2147483647.0)
29 i = 2147483647;
30 /* should be ...8, but atof bugs are too common */
31 else if (r < -2147483647.0)
32 i = -2147483647;
33 /* Admittedly some compilers don't follow the ANSI rules of casting
34 meaning truncating toward zero; but it doesn't matter enough to do
35 anything more complicated here. */
36 else if (r >= 0.0)
37 i = (integer)(r + 0.5);
38 else
39 i = (integer)(r - 0.5);
40
41 return i;
42 }