{$NOMAIN}
{$NOWALKBACK}
{
.STOP

File		: SC:[22,310]QDIV.PAS
Author		: Peter Stadick
Origin Date	: July 2,1988
Edit History	:

       Last Edit: 2-JUL-1988 15:31:33 

Description:
	This routine will divide two numbers represented arrays of char
	and return a result as an array of char. Calculations are
	done to specified number of decimal places and it can handle
	negitive numbers. The total number of digits in the number
	can not exceed 18.

}
%include lb:[22,320]general3.typ;
%include de:[107,114]clunk.typ;
%include de:[107,114]div64.ext;
%include de:[107,114]mul64.ext;
%include de:[107,114]add64.ext;
%include de:[22,320]castcl.ext;
%include de:[22,320]ccltas.ext;

procedure qdiv(divdend	: packed array [ln1..hn1:integer] of char;
	       divisor	: packed array [ln2..hn2:integer] of char;
	       var quo	: packed array [ls..lh:integer] of char;
	       places	: integer;
	       var ids	: integer); external;

procedure qdiv;

var
  clunk1	: clunk_type;
  clunk2	: clunk_type;
  clunk3	: clunk_type;
  clunk4	: clunk_type;
  clunk_rem	: clunk_type;
  clunk_quo	: clunk_type;
  negitive1	: boolean;
  negitive2	: boolean;
  negitive_sum	: boolean;
  multiplier	: ch18;

begin

  castcl(divdend,1,places,negitive1,clunk1);

  castcl(divisor,1,places,negitive2,clunk2);
{
  writeln('1:  ',clunk1[1],clunk1[2],clunk1[3],clunk1[4]);
  writeln('2:  ',clunk2[1],clunk2[2],clunk2[3],clunk2[4]);
}
  div64(clunk1,clunk2,clunk_rem,clunk_quo,ids);

  if (negitive1 and negitive2) or (not negitive1 and not negitive2) then
    negitive_sum := false
  else
    negitive_sum := true;

{
  writeln('SUM:',clunk_sum[1],clunk_sum[2],clunk_sum[3],clunk_sum[4]);
}
  { need to adjust to get the right number of decimal places }
  multiplier := '000000000000000000';

  if (places > 0) and (places < 18) then  
  begin
    multiplier[18-places] := '1';

    castcl(multiplier,1,0,negitive2,clunk3);

    if ids > 0 then
      mul64(clunk3,clunk_rem,clunk1,ids);

    if ids > 0 then
      mul64(clunk3,clunk_quo,clunk4,ids);
 
    if ids > 0 then
      div64(clunk1,clunk2,clunk_rem,clunk3,ids);

    if ids > 0 then
      add64(clunk4,clunk3,clunk_quo,ids);  

  end;

  ccltas(clunk_quo,places,negitive_sum,quo);

end;
