
{: test that sqr(n) causes a fatal error if it overflows }

{$d+}
program d020( output );

var
   k, l : 0..maxint;

function side : integer;
begin
   side := k;
   k := k+1;
end;
begin {d020}
   k := 1;
   while sqr(k) <= maxint div 4 do
      k := 2*k;

   l := k;
   {sqr(l) <= maxint < sqr(l+k)}

   while k > 1 do begin
      k := k div 2;
      if sqr(l) <= maxint -sqr(k) - 2*l*k then
         l := l+k;
   end;

   { now, sqr(l) <= maxint < sqr(l+1), ie
     l is largest integer that can be squared,
     l+1 is smallest integer that cannot}

   k := l;
   writeln( 'sqr(', k:1, ') is ', sqr(side):1 ); { this should not overflow }
   if k <> 1+l then
      writeln( 'side effect error evaluating sqr argument' );
   writeln( 'sqr(', l+1:1, ') is ', sqr(l+1):1 ); { this should overflow }
end. { d020 }

