home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
PC Underground
/
UNDERGROUND.ISO
/
math
/
basarith.pas
< prev
next >
Wrap
Pascal/Delphi Source File
|
1995-07-28
|
4KB
|
126 lines
Type Fixed=Record {structure of a fixed point number}
BeforeDec,
AfterDec:Integer
End;
Var Var1, {sample variables}
Var2:Fixed;
Const AfterDec_Max=100; {2 places after decimal point}
AfterDec_Places=2;
Function Strg(FNumber:Fixed):String;
{converts a fixed point number to a string}
Var AfterDec_Str, {string for forming the fractional part}
BeforeDec_Str:String; {string for forming the integral part}
i:Word;
Begin
If FNumber.AfterDec < 0 Then {output fractional part without sign}
FNumber.AfterDec:=-FNumber.AfterDec;
Str(FNumber.AfterDec:AfterDec_Places,AfterDec_Str);
{generate decimal string}
For i:=0 to AfterDec_Places do {and replace spaces with 0s}
If AfterDec_Str[i] = ' ' Then AfterDec_Str[i]:='0';
Str(FNumber.BeforeDec,BeforeDec_Str); {generate integral string}
Strg:=BeforeDec_Str+','+AfterDec_Str; {combine strings}
End;
Procedure Convert(RNumber:Real;Var FNumber:Fixed);
{converts Real RNumber to fixed point number FNumber}
Begin
FNumber.BeforeDec:=Trunc(RNumber);
{define integral part}
FNumber.AfterDec:=Trunc(Round(Frac(RNumber)*AfterDec_Max));
{define fractional part and store as whole number}
End;
Procedure Adjust(Var FNumber:Fixed);
{puts passed fixed point number back in legal format}
Begin
If FNumber.AfterDec > AfterDec_Max Then Begin
Dec(FNumber.AfterDec,AfterDec_Max); {if fractional part overflows to positive}
Inc(FNumber.BeforeDec); {reset and decrement integral part}
End;
If FNumber.AfterDec < -AfterDec_Max Then Begin
Inc(FNumber.AfterDec,AfterDec_Max); {if fractional part overflows to positive}
Dec(FNumber.BeforeDec); {reset and increment integral part}
End;
End;
Procedure Add(Var Sum:Fixed;FNumber1,FNumber2:Fixed);
{Adds FNumber1 and FNumber2 and places result in sum}
Var Result:Fixed;
Begin
Result.AfterDec:=FNumber1.AfterDec+FNumber2.AfterDec;
{add fractional part}
Result.BeforeDec:=FNumber1.BeforeDec+FNumber2.BeforeDec;
{add integral part}
Adjust(Result);
{Put result back in correct format}
Sum:=Result;
End;
Procedure Sub(Var Difference:Fixed;FNumber1,FNumber2:Fixed);
{Subtracts FNumber1 from FNumber2 and places result in difference}
Var Result:Fixed;
Begin
Result.AfterDec:=FNumber1.AfterDec-FNumber2.AfterDec;
{subtract fractional part}
Result.BeforeDec:=FNumber1.BeforeDec-FNumber2.BeforeDec;
{subtract integral part}
Adjust(Result);
{put result back in correct format}
Difference:=Result;
End;
Procedure Mul(Var Product:Fixed;FNumber1,FNumber2:Fixed);
{multiplies FNumber1 and FNumber2 and places result in product}
Var Result:LongInt;
Begin
Result:=Var1.BeforeDec*AfterDec_Max + Var1.AfterDec;
{form first factor}
Result:=Result * (Var2.BeforeDec*AfterDec_Max + Var2.AfterDec);
{form second factor}
Result:=Result div AfterDec_Max;
Product.BeforeDec:=Result div AfterDec_Max;
{extract integral and fractional parts}
Product.AfterDec:=Result mod AfterDec_Max;
End;
Procedure Divi(Var Quotient:Fixed;FNumber1,FNumber2:Fixed);
{divides FNumber1 by FNumber2 and places result in quotient}
Var Result:LongInt; {intermediate result}
Begin
Result:=FNumber1.BeforeDec*AfterDec_Max + FNumber1.AfterDec;
{form counter}
Result:=Result * AfterDec_Max div (FNumber2.BeforeDec*AfterDec_Max+FNumber2.AfterDec);
{divide by denominator, provide more places beforehand}
Quotient.BeforeDec:=Result div AfterDec_Max;
{extract integral and fractional parts}
Quotient.AfterDec:=Result mod AfterDec_Max;
End;
Begin
WriteLn;
Convert(-10.2,Var1); {load two demo numbers}
Convert(25.3,Var2);
{some calculations for demonstration purposes:}
Write(Strg(Var1),'*',Strg(Var2),'= ');
Mul(Var1,Var1,Var2);
WriteLn(Strg(Var1));
Write(Strg(Var1),'-',Strg(Var2),'= ');
Sub(Var1,Var1,Var2);
WriteLn(Strg(Var1));
Write(Strg(Var1),'/',Strg(Var2),'= ');
Divi(Var1,Var1,Var2);
WriteLn(Strg(Var1));
Write(Strg(Var1),'+',Strg(Var2),'= ');
Add(Var1,Var1,Var2);
WriteLn(Strg(Var1));
End.