home *** CD-ROM | disk | FTP | other *** search
/ Power-Programmierung / CD1.mdf / pascal / library / dos / tug__002 / trig.pas < prev    next >
Encoding:
Pascal/Delphi Source File  |  1988-08-14  |  16.8 KB  |  537 lines

  1. {TUG PDS CERT 1.01 (Pascal)
  2.  
  3. ==========================================================================
  4.  
  5.                   TUG PUBLIC DOMAIN SOFTWARE CERTIFICATION
  6.  
  7. The Turbo User Group (TUG) is recognized by Borland International as the
  8. official support organization for Turbo languages.  This file has been
  9. compiled and verified by the TUG library staff.  We are reasonably certain
  10. that the information contained in this file is public domain material, but
  11. it is also subject to any restrictions applied by its author.
  12.  
  13. This diskette contains PROGRAMS and/or DATA determined to be in the PUBLIC
  14. DOMAIN, provided as a service of TUG for the use of its members.  The
  15. Turbo User Group will not be liable for any damages, including any lost
  16. profits, lost savings or other incidental or consequential damages arising
  17. out of the use of or inability to use the contents, even if TUG has been
  18. advised of the possibility of such damages, or for any claim by any
  19. other party.
  20.  
  21. To the best of our knowledge, the routines in this file compile and function
  22. properly in accordance with the information described below.
  23.  
  24. If you discover an error in this file, we would appreciate it if you would
  25. report it to us.  To report bugs, or to request information on membership
  26. in TUG, please contact us at:
  27.  
  28.              Turbo User Group
  29.              PO Box 1510
  30.              Poulsbo, Washington USA  98370
  31.  
  32. --------------------------------------------------------------------------
  33.                        F i l e    I n f o r m a t i o n
  34.  
  35. * DESCRIPTION
  36. Supplies missing trigonometric functions for Turbo Pascal 4.0. Also
  37. provides hyperbolic, logarithmic, power, and root functions. All trig
  38. functions accessibile by radians, decimal degrees, degrees-minutes-seconds
  39. and a global DegreeType. Author: Hugo Hemmerich.
  40.  
  41. * ASSOCIATED FILES
  42.  
  43. * CHECKED BY
  44. DRM - 08/14/88
  45.  
  46. * KEYWORDS
  47. TURBO PASCAL V4.0 PROGRAM TRIG MATH
  48.  
  49. ==========================================================================
  50. }
  51. unit Trig;
  52.  
  53. (*  TRIG  -  Supplies missing trigonometric functions for Turbo Pascal 4.0
  54.  *           Also provides hyperbolic, logarithmic, power, and root functions.
  55.  *           All trig functions accessible by radians, decimal degrees,
  56.  *           degrees-minutes-seconds, and a global DegreeType.  Conversions
  57.  *           between these are supplied.
  58.  *
  59.  *  Written November 23, 1987 by Hugo Hemmerich, Refined Technologies.
  60.  *  All code granted to the public domain.
  61.  *
  62.  *  Questions and comments to CompuServe account number 72376,3505
  63.  *)
  64.  
  65. interface
  66.  
  67. type
  68.   DegreeType =  record
  69.                   Degrees, Minutes, Seconds : real;
  70.                 end;
  71. const
  72.   Infinity = 9.9999999999E+37;
  73.  
  74. {  Radians  }
  75. { sin, cos, and arctan are predefined }
  76.  
  77. function Tan( Radians : real ) : real;
  78. function ArcSin( InValue : real ) : real;
  79. function ArcCos( InValue : real ) : real;
  80.  
  81. {  Degrees, expressed as a real number  }
  82.  
  83. function DegreesToRadians( Degrees : real ) : real;
  84. function RadiansToDegrees( Radians : real ) : real;
  85. function Sin_Degree( Degrees : real ) : real;
  86. function Cos_Degree( Degrees : real ) : real;
  87. function Tan_Degree( Degrees : real ) : real;
  88. function ArcSin_Degree( Degrees : real ) : real;
  89. function ArcCos_Degree( Degrees : real ) : real;
  90. function ArcTan_Degree( Degrees : real ) : real;
  91.  
  92. {  Degrees, in Degrees, Minutes, and Seconds, as real numbers  }
  93.  
  94. function DegreePartsToDegrees( Degrees, Minutes, Seconds : real ) : real;
  95. function DegreePartsToRadians( Degrees, Minutes, Seconds : real ) : real;
  96. procedure DegreesToDegreeParts( DegreesIn : real;
  97.                                 var Degrees, Minutes, Seconds : real );
  98. procedure RadiansToDegreeParts( Radians : real;
  99.                                 var Degrees, Minutes, Seconds : real );
  100. function Sin_DegreeParts( Degrees, Minutes, Seconds : real ) : real;
  101. function Cos_DegreeParts( Degrees, Minutes, Seconds : real ) : real;
  102. function Tan_DegreeParts( Degrees, Minutes, Seconds : real ) : real;
  103. function ArcSin_DegreeParts( Degrees, Minutes, Seconds : real ) : real;
  104. function ArcCos_DegreeParts( Degrees, Minutes, Seconds : real ) : real;
  105. function ArcTan_DegreeParts( Degrees, Minutes, Seconds : real ) : real;
  106.  
  107. {  Degrees, expressed as DegreeType ( reals in record ) }
  108.  
  109. function DegreeTypeToDegrees( DegreeVar : DegreeType ) : real;
  110. function DegreeTypeToRadians( DegreeVar : DegreeType ) : real;
  111. procedure DegreeTypeToDegreeParts( DegreeVar : DegreeType;
  112.                                    var Degrees, Minutes, Seconds : real );
  113. procedure DegreesToDegreeType( Degrees : real; var DegreeVar : DegreeType );
  114. procedure RadiansToDegreeType( Radians : real; var DegreeVar : DegreeType );
  115. procedure DegreePartsToDegreeType( Degrees, Minutes, Seconds : real;
  116.                                    var DegreeVar : DegreeType );
  117. function Sin_DegreeType( DegreeVar : DegreeType ) : real;
  118. function Cos_DegreeType( DegreeVar : DegreeType ) : real;
  119. function Tan_DegreeType( DegreeVar : DegreeType ) : real;
  120. function ArcSin_DegreeType( DegreeVar : DegreeType ) : real;
  121. function ArcCos_DegreeType( DegreeVar : DegreeType ) : real;
  122. function ArcTan_DegreeType( DegreeVar : DegreeType ) : real;
  123.  
  124. {  Hyperbolic functions  }
  125.  
  126. function Sinh( Invalue : real ) : real;
  127. function Cosh( Invalue : real ) : real;
  128. function Tanh( Invalue : real ) : real;
  129. function Coth( Invalue : real ) : real;
  130. function Sech( Invalue : real ) : real;
  131. function Csch( Invalue : real ) : real;
  132. function ArcSinh( Invalue : real ) : real;
  133. function ArcCosh( Invalue : real ) : real;
  134. function ArcTanh( Invalue : real ) : real;
  135. function ArcCoth( Invalue : real ) : real;
  136. function ArcSech( Invalue : real ) : real;
  137. function ArcCsch( Invalue : real ) : real;
  138.  
  139. {  Logarithms, Powers, and Roots  }
  140.  
  141. { e to the x  is  exp() }
  142. { natural log is  ln()  }
  143. function Log10( InNumber : real ) : real;
  144. function Log( Base, InNumber : real ) : real;  { log of any base }
  145. function Power( InNumber, Exponent : real ) : real;
  146. function Root( InNumber, TheRoot : real ) : real;
  147.  
  148. {----------------------------------------------------------------------}
  149. implementation
  150.  
  151. const
  152.   RadiansPerDegree =  0.017453292520;
  153.   DegreesPerRadian = 57.295779513;
  154.   MinutesPerDegree =   60.0;
  155.   SecondsPerDegree = 3600.0;
  156.   SecondsPerMinute = 60.0;
  157.   LnOf10 = 2.3025850930;
  158.  
  159. {-----------}
  160. {  Radians  }
  161. {-----------}
  162.  
  163. { sin, cos, and arctan are predefined }
  164.  
  165. function Tan { ( Radians : real ) : real };
  166.   { note: returns Infinity where appropriate }
  167.   var
  168.     CosineVal : real;
  169.     TangentVal : real;
  170.   begin
  171.   CosineVal := cos( Radians );
  172.   if CosineVal = 0.0 then
  173.     Tan := Infinity
  174.   else
  175.     begin
  176.     TangentVal := sin( Radians ) / CosineVal;
  177.     if ( TangentVal < -Infinity ) or ( TangentVal > Infinity ) then
  178.       Tan := Infinity
  179.     else
  180.       Tan := TangentVal;
  181.     end;
  182.   end;
  183.  
  184. function ArcSin{ ( InValue : real ) : real };
  185.   { notes: 1) exceeding input range of -1 through +1 will cause runtime error }
  186.   {        2) only returns principal values                                   }
  187.   {             ( -pi/2 through pi/2 radians ) ( -90 through +90 degrees )    }
  188.   begin
  189.   if abs( InValue ) = 1.0 then
  190.     ArcSin := pi / 2.0
  191.   else
  192.     ArcSin := arctan( InValue / sqrt( 1 - InValue * InValue ) );
  193.   end;
  194.  
  195. function ArcCos{ ( InValue : real ) : real };
  196.   { notes: 1) exceeding input range of -1 through +1 will cause runtime error }
  197.   {        2) only returns principal values                                   }
  198.   {             ( 0 through pi radians ) ( 0 through +180 degrees )           }
  199.   var
  200.     Result : real;
  201.   begin
  202.   if InValue = 0.0 then
  203.     ArcCos := pi / 2.0
  204.   else
  205.     begin
  206.     Result := arctan( sqrt( 1 - InValue * InValue ) / InValue );
  207.     if InValue < 0.0 then
  208.       ArcCos := Result + pi
  209.     else
  210.       ArcCos := Result;
  211.     end;
  212.   end;
  213.  
  214. {---------------------------------------}
  215. {  Degrees, expressed as a real number  }
  216. {---------------------------------------}
  217.  
  218. function DegreesToRadians{ ( Degrees : real ) : real };
  219.   begin
  220.   DegreesToRadians := Degrees * RadiansPerDegree;
  221.   end;
  222.  
  223. function RadiansToDegrees{ ( Radians : real ) : real };
  224.   begin
  225.   RadiansToDegrees := Radians * DegreesPerRadian;
  226.   end;
  227.  
  228. function Sin_Degree{ ( Degrees : real ) : real };
  229.   begin
  230.   Sin_Degree := sin( DegreesToRadians( Degrees ) );
  231.   end;
  232.  
  233. function Cos_Degree{ ( Degrees : real ) : real };
  234.   begin
  235.   Cos_Degree := cos( DegreesToRadians( Degrees ) );
  236.   end;
  237.  
  238. function Tan_Degree{ ( Degrees : real ) : real };
  239.   begin
  240.   Tan_Degree := Tan( DegreesToRadians( Degrees ) );
  241.   end;
  242.  
  243. function ArcSin_Degree{ ( Degrees : real ) : real };
  244.   begin
  245.   ArcSin_Degree := ArcSin( DegreesToRadians( Degrees ) );
  246.   end;
  247.  
  248. function ArcCos_Degree{ ( Degrees : real ) : real };
  249.   begin
  250.   ArcCos_Degree := ArcCos( DegreesToRadians( Degrees ) );
  251.   end;
  252.  
  253. function ArcTan_Degree{ ( Degrees : real ) : real };
  254.   begin
  255.   ArcTan_Degree := arctan( DegreesToRadians( Degrees ) );
  256.   end;
  257.  
  258. {--------------------------------------------------------------}
  259. {  Degrees, in Degrees, Minutes, and Seconds, as real numbers  }
  260. {--------------------------------------------------------------}
  261.  
  262. function DegreePartsToDegrees{ ( Degrees, Minutes, Seconds : real ) : real };
  263.   begin
  264.   DegreePartsToDegrees := Degrees + ( Minutes / MinutesPerDegree ) +
  265.                                        ( Seconds / SecondsPerDegree );
  266.   end;
  267.  
  268. function DegreePartsToRadians{ ( Degrees, Minutes, Seconds : real ) : real };
  269.   begin
  270.   DegreePartsToRadians := DegreesToRadians( DegreePartsToDegrees( Degrees,
  271.                                                         Minutes, Seconds ) );
  272.   end;
  273.  
  274. procedure DegreesToDegreeParts{ ( DegreesIn : real;
  275.                                   var Degrees, Minutes, Seconds : real ) };
  276.   begin
  277.   Degrees := int( DegreesIn );
  278.   Minutes := ( DegreesIn - Degrees ) * MinutesPerDegree;
  279.   Seconds := frac( Minutes );
  280.   Minutes := int( Minutes );
  281.   Seconds := Seconds * SecondsPerMinute;
  282.   end;
  283.  
  284. procedure RadiansToDegreeParts{ ( Radians : real;
  285.                                   var Degrees, Minutes, Seconds : real ) };
  286.   begin
  287.   DegreesToDegreeParts( RadiansToDegrees( Radians ),
  288.                           Degrees, Minutes, Seconds );
  289.   end;
  290.  
  291. function Sin_DegreeParts{ ( Degrees, Minutes, Seconds : real ) : real };
  292.   begin
  293.   Sin_DegreeParts := sin( DegreePartsToRadians( Degrees, Minutes, Seconds ) );
  294.   end;
  295.  
  296. function Cos_DegreeParts{ ( Degrees, Minutes, Seconds : real ) : real };
  297.   begin
  298.   Cos_DegreeParts := cos( DegreePartsToRadians( Degrees, Minutes, Seconds ) );
  299.   end;
  300.  
  301. function Tan_DegreeParts{ ( Degrees, Minutes, Seconds : real ) : real };
  302.   begin
  303.   Tan_DegreeParts := Tan( DegreePartsToRadians( Degrees, Minutes, Seconds ) );
  304.   end;
  305.  
  306. function ArcSin_DegreeParts{ ( Degrees, Minutes, Seconds : real ) : real };
  307.   begin
  308.   ArcSin_DegreeParts := ArcSin( DegreePartsToRadians( Degrees,
  309.                                                       Minutes, Seconds ) );
  310.   end;
  311.  
  312. function ArcCos_DegreeParts{ ( Degrees, Minutes, Seconds : real ) : real };
  313.   begin
  314.   ArcCos_DegreeParts := ArcCos( DegreePartsToRadians( Degrees,
  315.                                                       Minutes, Seconds ) );
  316.   end;
  317.  
  318. function ArcTan_DegreeParts{ ( Degrees, Minutes, Seconds : real ) : real };
  319.   begin
  320.   ArcTan_DegreeParts := arctan( DegreePartsToRadians( Degrees,
  321.                                                       Minutes, Seconds ) );
  322.   end;
  323.  
  324. {-------------------------------------------------------}
  325. {  Degrees, expressed as DegreeType ( reals in record ) }
  326. {-------------------------------------------------------}
  327.  
  328. function DegreeTypeToDegrees{ ( DegreeVar : DegreeType ) : real };
  329.   begin
  330.   DegreeTypeToDegrees := DegreePartsToDegrees( DegreeVar.Degrees,
  331.                                        DegreeVar.Minutes, DegreeVar.Seconds );
  332.   end;
  333.  
  334. function DegreeTypeToRadians{ ( DegreeVar : DegreeType ) : real };
  335.   begin
  336.   DegreeTypeToRadians := DegreesToRadians( DegreeTypeToDegrees( DegreeVar ) );
  337.   end;
  338.  
  339. procedure DegreeTypeToDegreeParts{ ( DegreeVar : DegreeType;
  340.                                      var Degrees, Minutes, Seconds : real ) };
  341.   begin
  342.   Degrees := DegreeVar.Degrees;
  343.   Minutes := DegreeVar.Minutes;
  344.   Seconds := DegreeVar.Seconds;
  345.   end;
  346.  
  347. procedure DegreesToDegreeType{ ( Degrees : real; var DegreeVar : DegreeType )};
  348.   begin
  349.   DegreesToDegreeParts( Degrees, DegreeVar.Degrees,
  350.                         DegreeVar.Minutes, DegreeVar.Seconds );
  351.   end;
  352.  
  353. procedure RadiansToDegreeType{ ( Radians : real; var DegreeVar : DegreeType )};
  354.   begin
  355.   DegreesToDegreeParts( RadiansToDegrees( Radians ), DegreeVar.Degrees,
  356.                         DegreeVar.Minutes, DegreeVar.Seconds );
  357.   end;
  358.  
  359. procedure DegreePartsToDegreeType{ ( Degrees, Minutes, Seconds : real;
  360.                                      var DegreeVar : DegreeType ) };
  361.   begin
  362.   DegreeVar.Degrees := Degrees;
  363.   DegreeVar.Minutes := Minutes;
  364.   DegreeVar.Seconds := Seconds;
  365.   end;
  366.  
  367. function Sin_DegreeType{ ( DegreeVar : DegreeType ) : real };
  368.   begin
  369.   Sin_DegreeType := sin( DegreeTypeToRadians( DegreeVar ) );
  370.   end;
  371.  
  372. function Cos_DegreeType{ ( DegreeVar : DegreeType ) : real };
  373.   begin
  374.   Cos_DegreeType := cos( DegreeTypeToRadians( DegreeVar ) );
  375.   end;
  376.  
  377. function Tan_DegreeType{ ( DegreeVar : DegreeType ) : real };
  378.   begin
  379.   Tan_DegreeType := Tan( DegreeTypeToRadians( DegreeVar ) );
  380.   end;
  381.  
  382. function ArcSin_DegreeType{ ( DegreeVar : DegreeType ) : real };
  383.   begin
  384.   ArcSin_DegreeType := ArcSin( DegreeTypeToRadians( DegreeVar ) );
  385.   end;
  386.  
  387. function ArcCos_DegreeType{ ( DegreeVar : DegreeType ) : real };
  388.   begin
  389.   ArcCos_DegreeType := ArcCos( DegreeTypeToRadians( DegreeVar ) );
  390.   end;
  391.  
  392. function ArcTan_DegreeType{ ( DegreeVar : DegreeType ) : real };
  393.   begin
  394.   ArcTan_DegreeType := arctan( DegreeTypeToRadians( DegreeVar ) );
  395.   end;
  396.  
  397. {------------------------}
  398. {  Hyperbolic functions  }
  399. {------------------------}
  400.  
  401. function Sinh{ ( Invalue : real ) : real };
  402.   const
  403.     MaxValue = 88.029691931;  { exceeds standard turbo precision }
  404.   var
  405.     Sign : real;
  406.   begin
  407.   Sign := 1.0;
  408.   if Invalue < 0 then
  409.     begin
  410.     Sign := -1.0;
  411.     Invalue := -Invalue;
  412.     end;
  413.   if Invalue > MaxValue then
  414.     Sinh := Infinity
  415.   else
  416.     Sinh := ( exp( Invalue ) - exp( -Invalue ) ) / 2.0 * Sign;
  417.   end;
  418.  
  419. function Cosh{ ( Invalue : real ) : real };
  420.   const
  421.     MaxValue = 88.029691931;  { exceeds standard turbo precision }
  422.   begin
  423.   Invalue := abs( Invalue );
  424.   if Invalue > MaxValue then
  425.     Cosh := Infinity
  426.   else
  427.     Cosh := ( exp( Invalue ) + exp( -Invalue ) ) / 2.0;
  428.   end;
  429.  
  430. function Tanh{ ( Invalue : real ) : real };
  431.   begin
  432.   Tanh := Sinh( Invalue ) / Cosh( Invalue );
  433.   end;
  434.  
  435. function Coth{ ( Invalue : real ) : real };
  436.   begin
  437.   Coth := Cosh( Invalue ) / Sinh( Invalue );
  438.   end;
  439.  
  440. function Sech{ ( Invalue : real ) : real };
  441.   begin
  442.   Sech := 1.0 / Cosh( Invalue );
  443.   end;
  444.  
  445. function Csch{ ( Invalue : real ) : real };
  446.   begin
  447.   Csch := 1.0 / Sinh( Invalue );
  448.   end;
  449.  
  450. function ArcSinh{ ( Invalue : real ) : real };
  451.   var
  452.     Sign : real;
  453.   begin
  454.   Sign := 1.0;
  455.   if Invalue < 0 then
  456.     begin
  457.     Sign := -1.0;
  458.     Invalue := -Invalue;
  459.     end;
  460.   ArcSinh := ln( Invalue + sqrt( Invalue*Invalue + 1 ) ) * Sign;
  461.   end;
  462.  
  463. function ArcCosh{ ( Invalue : real ) : real };
  464.   var
  465.     Sign : real;
  466.   begin
  467.   Sign := 1.0;
  468.   if Invalue < 0 then
  469.     begin
  470.     Sign := -1.0;
  471.     Invalue := -Invalue;
  472.     end;
  473.   ArcCosh := ln( Invalue + sqrt( Invalue*Invalue - 1 ) ) * Sign;
  474.   end;
  475.  
  476. function ArcTanh{ ( Invalue : real ) : real };
  477.   var
  478.     Sign : real;
  479.   begin
  480.   Sign := 1.0;
  481.   if Invalue < 0 then
  482.     begin
  483.     Sign := -1.0;
  484.     Invalue := -Invalue;
  485.     end;
  486.   ArcTanh := ln( ( 1 + Invalue ) / ( 1 - Invalue ) ) / 2.0 * Sign;
  487.   end;
  488.  
  489. function ArcCoth{ ( Invalue : real ) : real };
  490.   begin
  491.   ArcCoth := ArcTanh( 1.0 / Invalue );
  492.   end;
  493.  
  494. function ArcSech{ ( Invalue : real ) : real };
  495.   begin
  496.   ArcSech := ArcCosh( 1.0 / Invalue );
  497.   end;
  498.  
  499. function ArcCsch{ ( Invalue : real ) : real };
  500.   begin
  501.   ArcCsch := ArcSinh( 1.0 / Invalue );
  502.   end;
  503.  
  504. {---------------------------------}
  505. {  Logarithms, Powers, and Roots  }
  506. {---------------------------------}
  507.  
  508. { e to the x  is  exp() }
  509. { natural log is  ln()  }
  510.  
  511. function Log10{ ( InNumber : real ) : real };
  512.   begin
  513.   Log10 := ln( InNumber ) / LnOf10;
  514.   end;
  515.  
  516. function Log{ ( Base, InNumber : real ) : real };  { log of any base }
  517.   begin
  518.   Log := ln( InNumber ) / ln( Base );
  519.   end;
  520.  
  521. function Power{ ( InNumber, Exponent : real ) : real };
  522.   begin
  523.   if InNumber > 0.0 then
  524.     Power := exp( Exponent * ln( InNumber ) )
  525.   else if InNumber = 0.0 then
  526.     Power := 1.0
  527.   else { force runtime error }
  528.     Power := InNumber / 0.0;
  529.   end;
  530.  
  531. function Root{ ( InNumber, TheRoot : real ) : real };
  532.   begin
  533.   Root := Power( InNumber, ( 1.0 / TheRoot ) );
  534.   end;
  535.  
  536. end. { unit Trig }
  537.