home *** CD-ROM | disk | FTP | other *** search
/ Chip 2002 June / Chip_2002-06_cd1.bin / zkuste / derive / download / Setup.exe / %MAINDIR% / Math / Support.pas < prev    next >
Encoding:
Pascal/Delphi Source File  |  2002-05-15  |  2.6 KB  |  92 lines

  1. function ModestWholeNumber (X: real): boolean;
  2.   {Returns true iff X is a whole number in the range -maxint .. maxint.}
  3. begin
  4.   if abs (X) < maxint then ModestWholeNumber := X = trunc (X)
  5.   else ModestWholeNumber := false
  6. end;
  7.  
  8. function IntPow (Base: real;  NonNegInt: integer): real;
  9.   {Returns Base raised to the nonnegative integer power NonNegInt.}
  10. begin
  11.   if NonNegInt = 0 then IntPow := 1
  12.   else if odd (NonNegInt) then IntPow := Base * IntPow (Base, NonNegInt - 1)
  13.   else IntPow := sqr (IntPow (Base, NonNegInt div 2));
  14. end;
  15.  
  16. function POW (Base, Exponent: real): real;
  17.   {Returns Base raised to the Exponent power.
  18.    Prerequisite files: EvenReal.lib, IntPow.lib,}
  19. begin
  20.   if Exponent < 0.0 then POW := 1.0 / POW (Base, -Exponent)
  21.   else if (Base = 1.0) or (Exponent = 0.0) then POW := 1.0
  22.     {:assuming POW (0.0, 0.0) = 1.0}
  23.   else if Base = 0.0 then POW := 0.0
  24.   else if ModestWholeNumber (Exponent) then
  25.      POW := IntPow (Base, trunc (Exponent))
  26.   else POW := exp (Exponent * ln (Base));
  27. end;
  28.  
  29. function ASIN (ratio: real): real;
  30.   {Returns the principal inverse sine in radians.}
  31. begin
  32.   ASIN := arctan (ratio / sqrt ((1.0 - ratio) * (1.0 + ratio)));
  33. end;
  34.  
  35. function ACOS (ratio: real): real;
  36.   {Returns the principal inverse cosine in radians.}
  37. begin
  38.   ACOS := arctan (sqrt ((1.0 - ratio) * (1.0 + ratio)) / ratio);
  39. end;
  40.  
  41. function ATAN (ratio: real): real;
  42.   {Returns the principal arctangent of ratio in radians.}
  43. begin
  44.   ATAN := ARCTAN (ratio);
  45. end;
  46.  
  47. function PI: real;
  48. begin
  49.   PI := 4.0 * ARCTAN(1.0)
  50. end
  51.  
  52. function ATAN2 (Y, X: real): real;
  53.   {Returns the radian angle of the point (X, Y).}
  54.   {Returns PI for the degenerate case X=Y=0.}
  55. begin
  56.   if X>0.0 then ATAN2 := ARCTAN (Y/X);
  57.   else if Y>0.0 then ATAN2 := PI/2.0 - ARCTAN (X/Y);
  58.   else if Y<0.0 then ATAN2 := -PI/2.0 - ARCTAN(X/Y);
  59.   else ATAN2 := PI
  60. end;
  61.  
  62. function TAN (angle: real): real;
  63.   {Returns the tangent of angle, which is measured in radians.}
  64. begin
  65.   TAN := SIN (angle) / COS (angle);
  66. end;
  67.  
  68. function COT (angle: real): real;
  69.   {Returns the cotangent of angle, which is measured in radians.}
  70. begin
  71.   COT := COS (angle) / SIN (angle);
  72. end;
  73.  
  74. function SIGN (R: real): real;
  75.   {returns -1 if R<0, 0 if R=0, or 1 if R>0.}
  76. begin
  77.   if R > 0.0 then SIGN := 1.0
  78.   else if R < 0.0 then SIGN := -1.0
  79.   else SIGN := 0.0;
  80. end;
  81.  
  82. function FACT (n: integer): real;
  83.   {Returns n! for n >= 0.  Intentionally provokes zero-divide for n<0.}
  84.   var k: integer;
  85.       ans: real;
  86. begin
  87.   ans := 1.0;
  88.   if n < 0 then FACT := ans / (n - n)
  89.   else for k := 2 to n do ans := k * ans;
  90.   FACT := ans
  91. end;
  92.