%%% \badness badness

\badness parameter is supposed to tell something about box stretch or shrinkage.
It certaintly does, but one may be surprised by results of the factual algorithm.
Lets take a simple macro that builds a \vbox fullfilling it with 0pt glue plus
some stretch.

% \def\checkbadness#1#2{%
%   \setbox0\vbox to\dimexpr#1{\vskip0pt plus\dimexpr#2}%
%   \immediate\write16{badness=\the\badness}}
%
% \checkbadness{1pt-1sp}{1pt}      % 99
% \checkbadness{1pt}{1pt}          % 100
% \checkbadness{1.00336pt}{1pt}    % 100
% \checkbadness{1.003367pt}{1pt}   % 101

The left border of best-badness threshold is sharp, the right one is more
tolerant. The same happens for larger dimensions

% \checkbadness{100pt-1sp}{100pt}  % 99
% \checkbadness{100.3366pt}{100pt} % 100
% \checkbadness{100.3367pt}{100pt} % 101
%
% \checkbadness{297pt-1sp}{297pt}  % 99
% \checkbadness{298pt-1sp}{297pt}  % 100
% \checkbadness{298pt}{297pt}      % 100



%% 1000pt mod 297 = 277
% \dimen0=\dimexpr(1000pt-277sp)*298/297\relax % 298(s-s%297)/297
% \checkbadness{1000pt-278sp}{1000pt} % 99
% \checkbadness{1000pt-277sp}{1000pt} % 100
% \checkbadness{\dimen0-1sp}{1000pt}  % 100
% \checkbadness{\dimen0}{1000pt}      % 101
%
% \checkbadness{2970pt-1sp}{2970pt}   % 99
% \checkbadness{2980pt-1sp}{2970pt}   % 100
% \checkbadness{2980pt}{2970pt}       % 101

% overfull boxes 10000
% inifinite glues makes \badness 0

% stretch. Lets see the heuristic responsible for all that -- the formula
% factually used to compute \badness parameter (tex.web, line 2320):
%
%   @d inf_bad = 10000 {infinitely bad value}
%   @p function badness(@!t,@!s:scaled):halfword; {compute badness, given |t>=0|}
%   var r:integer; {approximation to $\alpha t/s$, where $\alpha^3\approx
%     100\cdot2^{18}$}
%   begin if t=0 then badness:=0
%   else if s<=0 then badness:=inf_bad
%   else  begin if t<=7230584 then  r:=(t*297) div s {$297^3=99.94\times2^{18}$}
%     else if s>=1663497 then r:=t div (s div 297)
%     else r:=t;
%     if r>1290 then badness:=inf_bad {$1290^3<2^{31}<1291^3$}
%     else badness:=(r*r*r+@'400000) div @'1000000;
%     end; {that was $r^3/2^{18}$, rounded to the nearest integer}
%   end;
%