2

A recent conversation about bug-free software at work has lead to a discussion about TeX (which is considered virtually bug-free, while it might technically be false). Since TeX's source code is extensively documented in "TeX: The Program", we've taken a look inside and picked one procedure which has been unclear on first and second sight. It's an implementation of print_int, which is §65 in that book on page 28. Its documentation says:

@ The following procedure, which prints out the decimal representation of a given integer |n|, has been written carefully so that it works properly if |n=0| or if |(-n)| would cause overflow. It does not apply |mod| or |div| to negative arguments, since such operations are not implemented consistently by all \PASCAL\ compilers.

The procedure itself looks like this (code taken from WEB source):

procedure print_int(@!n:integer); {prints an integer in decimal form}
var k:0..23; {index to current digit; we assume that $|n|<10^{23}$}
@!m:integer; {used to negate |n| in possibly dangerous cases}
begin k:=0;
if n<0 then
  begin print_char("-");
  if n>-100000000 then negate(n)
  else  begin m:=-1-n; n:=m div 10; m:=(m mod 10)+1; k:=1;
    if m<10 then dig[0]:=m
    else  begin dig[0]:=0; incr(n);
      end;
    end;
  end;
repeat dig[k]:=n mod 10; n:=n div 10; incr(k);
until n=0;
print_the_digs(k);
end;

While the algorithm itself is quite understandable (after all, it's just printing a number), we couldn't understand which significance the number 100000000 has in context of Pascal compilers of that time. The documentation mentions overflows, however I'd expect an overflow to happen on a "power-of-two" boundary and not on "power-of-ten".

What is the explanation or significance of this number? Is this a compiler or a language standard quirk?

6
  • Can't exactly remember but Knuth's said many things about such thing in the TeX book, about constants and max values... I suppose that he calculates that nobody will use this function with a value greater than that value (he likes computing such so much). Mar 23, 2020 at 19:35
  • @samcarter_is_at_topanswers.xyz wouldn't bet on that, the question has little to do with TeX per se, it's more or less Pascal of the 80s.... Mar 23, 2020 at 22:29
  • @Jean-BaptisteYunès but his own comments are talking about 10^32, which is quite a bit larger than a 100 million... Mar 23, 2020 at 22:31
  • 4
    Actually, the comment says 10^23, not 10^32. Although that's still a lot larger than 100 million. My speculation is that Knuth picked a safe number (100 million is < 2^27) that he know could be handled by a 32-bit runtime. Probably figured that 100 million was large enough that it would handle the majority of cases.
    – Jim Mischel
    Mar 24, 2020 at 2:27
  • 2
    To echo the comment by Jim Mischel above: the "else" path (that involves m) always works, so the use of "negate" is needed only as a "fast path", an optimization for the common case. 100 million seems large enough to cover most of the calls to this function, so it's not worth optimizing further. (Of course the constant needs to smaller than, say, 2^31.) (Also, there was no Pascal standard at that time, just the Jensen&Wirth book. TeX assumes at least 32-bit integers.) Mar 25, 2020 at 6:33

0

You must log in to answer this question.

Browse other questions tagged .