home *** CD-ROM | disk | FTP | other *** search
/ PC Underground / UNDERGROUND.ISO / math / basarith.pas < prev    next >
Pascal/Delphi Source File  |  1995-07-28  |  4KB  |  126 lines

  1. Type Fixed=Record                {structure of a fixed point number}
  2.             BeforeDec,
  3.             AfterDec:Integer
  4.           End;
  5.  
  6. Var Var1,                       {sample variables}
  7.     Var2:Fixed;
  8.  
  9. Const AfterDec_Max=100;            {2 places after decimal point}
  10.       AfterDec_Places=2;
  11.  
  12. Function Strg(FNumber:Fixed):String;
  13. {converts a fixed point number to a string}
  14. Var AfterDec_Str,                  {string for forming the fractional part}
  15.     BeforeDec_Str:String;            {string for forming the integral part}
  16.     i:Word;
  17. Begin
  18.   If FNumber.AfterDec < 0 Then       {output fractional part without sign}
  19.     FNumber.AfterDec:=-FNumber.AfterDec;
  20.   Str(FNumber.AfterDec:AfterDec_Places,AfterDec_Str);
  21.                                 {generate decimal string}
  22.   For i:=0 to AfterDec_Places do  {and replace spaces with 0s}
  23.     If AfterDec_Str[i] = ' ' Then AfterDec_Str[i]:='0';
  24.   Str(FNumber.BeforeDec,BeforeDec_Str);     {generate integral string}
  25.   Strg:=BeforeDec_Str+','+AfterDec_Str; {combine strings}
  26. End;
  27.  
  28. Procedure Convert(RNumber:Real;Var FNumber:Fixed);
  29. {converts Real RNumber to fixed point number FNumber}
  30. Begin
  31.   FNumber.BeforeDec:=Trunc(RNumber);
  32.     {define integral part}
  33.   FNumber.AfterDec:=Trunc(Round(Frac(RNumber)*AfterDec_Max));
  34.     {define fractional part and store as whole number}
  35. End;
  36.  
  37. Procedure Adjust(Var FNumber:Fixed);
  38. {puts passed fixed point number back in legal format}
  39. Begin
  40.   If FNumber.AfterDec > AfterDec_Max Then Begin
  41.     Dec(FNumber.AfterDec,AfterDec_Max); {if fractional part overflows to positive}
  42.     Inc(FNumber.BeforeDec);            {reset and decrement integral part}
  43.   End;
  44.   If FNumber.AfterDec < -AfterDec_Max Then Begin
  45.     Inc(FNumber.AfterDec,AfterDec_Max); {if fractional part overflows to positive}
  46.     Dec(FNumber.BeforeDec);            {reset and increment integral part}
  47.   End;
  48. End;
  49.  
  50. Procedure Add(Var Sum:Fixed;FNumber1,FNumber2:Fixed);
  51. {Adds FNumber1 and FNumber2 and places result in sum}
  52. Var Result:Fixed;
  53. Begin
  54.   Result.AfterDec:=FNumber1.AfterDec+FNumber2.AfterDec;
  55.     {add fractional part}
  56.   Result.BeforeDec:=FNumber1.BeforeDec+FNumber2.BeforeDec;
  57.     {add integral part}
  58.   Adjust(Result);
  59.     {Put result back in correct format}
  60.   Sum:=Result;
  61. End;
  62.  
  63. Procedure Sub(Var Difference:Fixed;FNumber1,FNumber2:Fixed);
  64. {Subtracts FNumber1 from FNumber2 and places result in difference}
  65. Var Result:Fixed;
  66. Begin
  67.   Result.AfterDec:=FNumber1.AfterDec-FNumber2.AfterDec;
  68.     {subtract fractional part}
  69.   Result.BeforeDec:=FNumber1.BeforeDec-FNumber2.BeforeDec;
  70.     {subtract integral part}
  71.   Adjust(Result);
  72.     {put result back in correct format}
  73.   Difference:=Result;
  74. End;
  75.  
  76. Procedure Mul(Var Product:Fixed;FNumber1,FNumber2:Fixed);
  77. {multiplies FNumber1 and FNumber2 and places result in product}
  78. Var Result:LongInt;
  79. Begin
  80.   Result:=Var1.BeforeDec*AfterDec_Max + Var1.AfterDec;
  81.     {form first factor}
  82.   Result:=Result * (Var2.BeforeDec*AfterDec_Max + Var2.AfterDec);
  83.     {form second factor}
  84.   Result:=Result div AfterDec_Max;
  85. Product.BeforeDec:=Result div AfterDec_Max;
  86.     {extract integral and fractional parts}
  87.   Product.AfterDec:=Result mod AfterDec_Max;
  88. End;
  89.  
  90. Procedure Divi(Var Quotient:Fixed;FNumber1,FNumber2:Fixed);
  91. {divides FNumber1 by FNumber2 and places result in quotient}
  92. Var Result:LongInt;           {intermediate result}
  93. Begin
  94.   Result:=FNumber1.BeforeDec*AfterDec_Max + FNumber1.AfterDec;
  95.     {form counter}
  96.   Result:=Result * AfterDec_Max div (FNumber2.BeforeDec*AfterDec_Max+FNumber2.AfterDec);
  97.     {divide by denominator, provide more places beforehand}
  98.   Quotient.BeforeDec:=Result div AfterDec_Max;
  99.     {extract integral and fractional parts}
  100.   Quotient.AfterDec:=Result mod AfterDec_Max;
  101. End;
  102.  
  103. Begin
  104.   WriteLn;
  105.   Convert(-10.2,Var1);          {load two demo numbers}
  106.   Convert(25.3,Var2);
  107.  
  108.   {some calculations for demonstration purposes:}
  109.  
  110.   Write(Strg(Var1),'*',Strg(Var2),'= ');
  111.   Mul(Var1,Var1,Var2);
  112.   WriteLn(Strg(Var1));
  113.  
  114.   Write(Strg(Var1),'-',Strg(Var2),'= ');
  115.   Sub(Var1,Var1,Var2);
  116.   WriteLn(Strg(Var1));
  117.  
  118.   Write(Strg(Var1),'/',Strg(Var2),'= ');
  119.   Divi(Var1,Var1,Var2);
  120.   WriteLn(Strg(Var1));
  121.  
  122.   Write(Strg(Var1),'+',Strg(Var2),'= ');
  123.   Add(Var1,Var1,Var2);
  124.   WriteLn(Strg(Var1));
  125. End.
  126.