Back to index

tetex-bin  3.0
zround.c
Go to the documentation of this file.
00001 /* zround.c: round R to the nearest whole number.  This is supposed to
00002    implement the predefined Pascal round function.  Public domain. */
00003 
00004 #include "config.h"
00005 
00006 integer
00007 zround P1C(double, r)
00008 {
00009   integer i;
00010 
00011   /* R can be outside the range of an integer if glue is stretching or
00012      shrinking a lot.  We can't do any better than returning the largest
00013      or smallest integer possible in that case.  It doesn't seem to make
00014      any practical difference.  Here is a sample input file which
00015      demonstrates the problem, from phil@cs.arizona.edu:
00016        \documentstyle{article}
00017        \begin{document}
00018        \begin{flushleft}
00019        $\hbox{} $\hfill 
00020        \filbreak
00021        \eject
00022     
00023      djb@silverton.berkeley.edu points out we should testing against
00024      TeX's largest or smallest integer (32 bits), not the machine's.  So
00025      we might as well use a floating-point constant, and avoid potential
00026      compiler bugs (also noted by djb, on BSDI).  */
00027   if (r > 2147483647.0)
00028     i = 2147483647;
00029   /* should be ...8, but atof bugs are too common */
00030   else if (r < -2147483647.0)
00031     i = -2147483647;
00032   /* Admittedly some compilers don't follow the ANSI rules of casting
00033      meaning truncating toward zero; but it doesn't matter enough to do
00034      anything more complicated here.  */
00035   else if (r >= 0.0)
00036     i = (integer)(r + 0.5);
00037   else
00038     i = (integer)(r - 0.5);
00039 
00040   return i;
00041 }