home *** CD-ROM | disk | FTP | other *** search
/ Chip 2001 October / Chip_2001-10_cd1.bin / zkuste / delphi / kompon / d123456 / MOON20.ZIP / AH_MATH.PAS < prev    next >
Pascal/Delphi Source File  |  2001-07-07  |  4KB  |  172 lines

  1. unit ah_math;
  2.  
  3.  {$i ah_def.inc }
  4. (*$define nomath *)
  5. (*$b-*)   { I may make use of the shortcut boolean eval }
  6.  
  7. (*@/// interface *)
  8. interface
  9.  
  10. { Angular functions }
  11. function tan(x:extended):extended;
  12. function arctan2(a,b:extended):extended;
  13. function arcsin(x:extended):extended;
  14. function arccos(x:extended):extended;
  15.  
  16. { Convert degree and radians }
  17. function deg2rad(x:extended):extended;
  18. function rad2deg(x:extended):extended;
  19.  
  20. { Angular functions with degrees }
  21. function sin_d(x:extended):extended;
  22. function cos_d(x:extended):extended;
  23. function tan_d(x:extended):extended;
  24. function arctan2_d(a,b:extended):extended;
  25. function arcsin_d(x:extended):extended;
  26. function arccos_d(x:extended):extended;
  27. function arctan_d(x:extended):extended;
  28.  
  29. { Limit degree value into 0..360 range }
  30. function put_in_360(x:extended):extended;
  31. { Modulo operation which returns the value in the range 1..b }
  32. function adjusted_mod(a,b:integer):integer;
  33. (*@\\\*)
  34. (*@/// implementation *)
  35. implementation
  36.  
  37. (*$ifndef nomath *)
  38. uses
  39.   math;
  40. (*$endif *)
  41.  
  42. (*@/// function deg2rad(x:extended):extended; *)
  43. function deg2rad(x:extended):extended;
  44. begin
  45.   result:=x/180*pi;
  46.   end;
  47. (*@\\\*)
  48. (*@/// function rad2deg(x:extended):extended; *)
  49. function rad2deg(x:extended):extended;
  50. begin
  51.   result:=x*180/pi;
  52.   end;
  53. (*@\\\*)
  54.  
  55. (*$ifdef nomath *)
  56. { D1 has no unit math, so here are the needed functions }
  57. (*@/// function tan(x:extended):extended; *)
  58. function tan(x:extended):extended;
  59. begin
  60.   result:=sin(x)/cos(x);
  61.   end;
  62. (*@\\\*)
  63. (*@/// function arctan2(a,b:extended):extended; *)
  64. function arctan2(a,b:extended):extended;
  65. begin
  66.   result:=arctan(a/b);
  67.   if b<0 then result:=result+pi;
  68.   end;
  69. (*@\\\*)
  70. (*@/// function arcsin(x:extended):extended; *)
  71. function arcsin(x:extended):extended;
  72. begin
  73.   result:=arctan(x/sqrt(1-x*x));
  74.   end;
  75. (*@\\\*)
  76. (*@/// function arccos(x:extended):extended; *)
  77. function arccos(x:extended):extended;
  78. begin
  79.   result:=pi/2-arcsin(x);
  80.   end;
  81. (*@\\\*)
  82. (*$else
  83. (*@/// function tan(x:extended):extended; *)
  84. function tan(x:extended):extended;
  85. begin
  86.   result:=math.tan(x);
  87.   end;
  88. (*@\\\*)
  89. (*@/// function arctan2(a,b:extended):extended; *)
  90. function arctan2(a,b:extended):extended;
  91. begin
  92.   result:=math.arctan2(a,b);
  93.   end
  94. (*@\\\*)
  95. (*@/// function arcsin(x:extended):extended; *)
  96. function arcsin(x:extended):extended;
  97. begin
  98.   result:=math.arcsin(x);
  99.   end;
  100. (*@\\\*)
  101. (*@/// function arccos(x:extended):extended; *)
  102. function arccos(x:extended):extended;
  103. begin
  104.   result:=math.arccos(x);
  105.   end;
  106. (*@\\\*)
  107. (*$endif *)
  108.  
  109. { Angular functions with degrees }
  110. (*@/// function sin_d(x:extended):extended; *)
  111. function sin_d(x:extended):extended;
  112. begin
  113.   sin_d:=sin(deg2rad(put_in_360(x)));
  114.   end;
  115. (*@\\\000000030E*)
  116. (*@/// function cos_d(x:extended):extended; *)
  117. function cos_d(x:extended):extended;
  118. begin
  119.   cos_d:=cos(deg2rad(put_in_360(x)));
  120.   end;
  121. (*@\\\000000030E*)
  122. (*@/// function tan_d(x:extended):extended; *)
  123. function tan_d(x:extended):extended;
  124. begin
  125.   tan_d:=tan(deg2rad(put_in_360(x)));
  126.   end;
  127. (*@\\\0000000324*)
  128. (*@/// function arctan2_d(a,b:extended):extended; *)
  129. function arctan2_d(a,b:extended):extended;
  130. begin
  131.   result:=rad2deg(arctan2(a,b));
  132.   end;
  133. (*@\\\0000000320*)
  134. (*@/// function arcsin_d(x:extended):extended; *)
  135. function arcsin_d(x:extended):extended;
  136. begin
  137.   result:=rad2deg(arcsin(x));
  138.   end;
  139. (*@\\\000000031D*)
  140. (*@/// function arccos_d(x:extended):extended; *)
  141. function arccos_d(x:extended):extended;
  142. begin
  143.   result:=rad2deg(arccos(x));
  144.   end;
  145. (*@\\\000000031D*)
  146. (*@/// function arctan_d(x:extended):extended; *)
  147. function arctan_d(x:extended):extended;
  148. begin
  149.   result:=rad2deg(arctan(x));
  150.   end;
  151. (*@\\\000000031E*)
  152.  
  153. (*@/// function put_in_360(x:extended):extended; *)
  154. function put_in_360(x:extended):extended;
  155. begin
  156.   result:=x-round(x/360)*360;
  157.   while result<0 do result:=result+360;
  158.   end;
  159. (*@\\\*)
  160. (*@/// function adjusted_mod(a,b:integer):integer; *)
  161. function adjusted_mod(a,b:integer):integer;
  162. begin
  163.   result:=a mod b;
  164.   while result<1 do
  165.     result:=result+b;
  166.   end;
  167. (*@\\\*)
  168. (*@\\\*)
  169. (*$ifdef delphi_ge_2 *) (*$warnings off *) (*$endif *)
  170. end.
  171. (*@\\\003F000901000901000901000A01000701000011000701*)
  172.