home *** CD-ROM | disk | FTP | other *** search
/ Programmer 7500 / MAX_PROGRAMMERS.iso / INFO / TURBOPAS / TRANSBCD.ZIP / TRANSBCD.PAS
Encoding:
Pascal/Delphi Source File  |  1987-02-05  |  1.9 KB  |  97 lines

  1. { A first approximation of a set of routines to do transcendental functions in
  2.   the BCD version of Turbo Pascal.  I don't know how to calculate Ln(X) or
  3.   ArcTan(X) so those functions are missing.  Sqrt is missing because the easy
  4.   way of doing it requires Ln(X).
  5.  
  6.   Anyone who figures out how to do ArcTan or Ln is welcome to upload a new set
  7.   of routines.  (Or if my methods are atrocious -- no doubt).
  8.  
  9.   By the way, these routines should give you some idea of why the
  10.   transcendentals were left out of the BCD compiler: they are S*L*O*W!
  11.  
  12.   -  Bela Lubkin
  13.      Borland International Technical Support
  14.      CompuServe 71016,1573 }
  15.  
  16. Const
  17.   HalfPi=1.57079632679489662;
  18.       Pi=3.14159265358979323;
  19.    TwoPi=6.28318530717958646;
  20.  
  21. Function Sin(X: Real): Real;
  22.   Var
  23.     XX,X2,S: Real;
  24.     I: Integer;
  25.   Begin
  26.     If Abs(X)>Pi Then
  27.       X:=X-Round(X/TwoPi)*TwoPi;
  28.     If Abs(X)>HalfPi Then
  29.       If X>0 Then X:=Pi-X
  30.       Else X:=-Pi-X;
  31.     I:=1;
  32.     XX:=X;
  33.     X2:=Sqr(X);
  34.     S:=0.0;
  35.     Repeat
  36.       S:=S+XX;
  37.       I:=I+2;
  38.       XX:=-XX*X2/(I-1)/I;
  39.     Until XX=0.0;
  40.     Sin:=S;
  41.   End;
  42.  
  43. Function Cos(X: Real): Real;
  44.   Begin
  45.     Cos:=Sin(X+HalfPi);
  46.   End;
  47.  
  48. Function Exp(X: Real): Real;
  49.   Var
  50.     XX,S: Real;
  51.     I: Integer;
  52.   Begin
  53.     XX:=1.0;
  54.     S:=0.0;
  55.     I:=1;
  56.     Repeat
  57.       S:=S+XX;
  58.       XX:=XX*X/I;
  59.       I:=I+1;
  60.     Until XX=0.0;
  61.     Exp:=S;
  62.   End;
  63.  
  64. (*
  65. Function Ln(X: Real): Real;
  66.   ???
  67.  
  68. Function Sqrt(X: Real): Real;
  69.   Begin
  70.     Sqrt:=Exp(Ln(X)/2);
  71.   End;
  72.  
  73. Function Power(Y,X: Real): Real;   { Y to the X }
  74.   Begin
  75.     Power:=Exp(Ln(Y)*X);
  76.   End;
  77.  
  78. Function ArcTan(X: Real): Real;
  79.   ???
  80. *)
  81.  
  82. { Demonstration program; delete the next line to enable }
  83. (*
  84.  
  85. Var
  86.   X: Real;
  87.  
  88. Begin
  89.   Repeat
  90.     ReadLn(X);
  91.     WriteLn(Sin(X):1:20);
  92.     WriteLn(Cos(X):1:20);
  93.     WriteLn(Exp(X):1:20);
  94.   Until X=0.0;
  95. End.
  96. *)
  97.