procedure rquot(var x1,x2,x3: hreal);
{ uses external array e }
var
  ix2,fac,p,k,n,d,last,et: integer;
  j: integer32;
begin { rquot }
  trunc:=false;
  if x2.si=0 then writeln('rquot: error - division by zero')
  else if x1.si=0 then x3.si:=0
  else begin
    if x1.si=x2.si then x3.si:=1 else x3.si:=-1;
    with x3 do for n:=0 to hdim do ma[n]:=0;
    with x1 do for n:=0 to hdim do e[n]:=ma[n];
    for n:=hdim+1 to edim do e[n]:=0;
    j:=10000*x2.ma[0]+x2.ma[1];
    fac:=10000;
    while j>10000 do begin
      j:=j div 10;
      fac:=fac div 10;
    end;
    ix2:=j+1;                                { 1 + first four digits of x2 }
    p:=0;
    k:=-1;
    last:=0;
    repeat
      d:=(fac*(10000*last+e[p])) div ix2;         { guess next digit of x3 }
      if d=0 then with x2 do begin
        n:=0;
        while (e[n+p]=ma[n]) and (n<hdim) do n:=n+1;
        if e[n+p]>=ma[n] then d:=1;
      end;
      if d=0 then begin
        last:=e[p];
        k:=k+1;
        p:=p+1;
      end
      else with x2 do begin
        j:=0;
        for n:=hdim downto 0 do begin              { subtract shifted d*x2 }
          j:=j+d*ma[n];
          et:=e[n+p]-(j mod 10000);
          j:=j div 10000;
          if et>=0 then e[n+p]:=et
          else begin
            e[n+p]:=et+10000;
            j:=j+1;
          end;
        end;
        if k<0 then k:=0 else last:=last-j;
        x3.ma[k]:=x3.ma[k]+d;
      end;
    until k>hdim;
    with x3 do begin
      if p=k then ex:=x1.ex-x2.ex
      else ex:=x1.ex-x2.ex-1;
      if abs(ex)>exmax then begin
        if ex<0 then underflow:=true else overflow:=true;
      end;
    end;
    if last>0 then trunc:=true
    else begin
      while (e[p]=0) and (p<edim) do p:=p+1;
      trunc:=(e[p]>0);
    end;
  end;
end { rquot };

