beta-0.89.2
[luatex.git] / source / texk / web2c / lib / zround.c
blobe51eed8fa2ae20b472e6d84c4b410c98afe03e8b
1 /* zround.c: round R to the nearest whole number. This is supposed to
2 implement the predefined Pascal round function. Public domain. */
4 #include <w2c/config.h>
5 #include "lib.h"
7 integer
8 zround (double r)
10 integer i;
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 phil@cs.arizona.edu:
17 \documentstyle{article}
18 \begin{document}
19 \begin{flushleft}
20 $\hbox{} $\hfill
21 \filbreak
22 \eject
24 djb@silverton.berkeley.edu 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);
41 return i;