home *** CD-ROM | disk | FTP | other *** search
/ Simtel MSDOS - Coast to Coast / simteldosarchivecoasttocoast.iso / pcmag / vol9n21.zip / DGMATH.PAS < prev    next >
Pascal/Delphi Source File  |  1990-09-25  |  7KB  |  203 lines

  1. {
  2.  ▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄
  3.  █                                                                         █
  4.  █        TITLE :      DGMATH.TPU                                          █
  5.  █      PURPOSE :      Number crunching routines.                          █
  6.  █       AUTHOR :      David Gerrold, CompuServe ID:  70307,544            █
  7.  █ ______________________________________________________________________  █
  8.  █                                                                         █
  9.  █   Written in Turbo Pascal, Version 5.5,                                 █
  10.  █   with routines from TurboPower, Object Professional.                   █
  11.  █                                                                         █
  12.  █   Turbo Pascal is a product of Borland International.                   █
  13.  █   Object Professional is a product of TurboPower Software.              █
  14.  █ ______________________________________________________________________  █
  15.  █                                                                         █
  16.  █   This is not public domain software.                                   █
  17.  █   This software is copyright 1990, by David Gerrold.                    █
  18.  █   Permission is hereby granted for personal use.                        █
  19.  █                                                                         █
  20.  █        The Brass Cannon Corporation                                     █
  21.  █        9420 Reseda Blvd., #804                                          █
  22.  █        Northridge, CA  91324-2932.                                      █
  23.  █                                                                         █
  24.  ▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀
  25.                                                                             }
  26. { Compiler Directives ===================================================== }
  27.  
  28. {$A-}    {Switch word alignment off, necessary for cloning}
  29. {$R-}    {Range checking off}
  30. {$B-}    {Boolean complete evaluation off}
  31. {$S-}    {Stack checking off}
  32. {$I-}    {I/O checking off}
  33. {$N+,E+} {Simulate numeric coprocessor}
  34. {$M 16384,0,327680} {stack and heap}
  35. {$V-}    {Variable range checking off}
  36.  
  37. { Name ==================================================================== }
  38.  
  39. UNIT DgMath;
  40. {
  41.   The purpose of DgMath is to provide useful number-crunching routines.
  42. }
  43.  
  44. { Interface =============================================================== }
  45.  
  46. INTERFACE
  47.  
  48. { Functions and Procedures ================================================ }
  49.  
  50. PROCEDURE UpCycle (VAR Num : integer;  Bottom, Top : word);
  51. { Increases Num through cycle, bounded by Bottom and Top. }
  52.  
  53. PROCEDURE DownCycle (VAR Num : integer;  Bottom, Top : word);
  54. { Decreases Num through cycle, bounded by Bottom and Top. }
  55.  
  56. FUNCTION Max (Num1, Num2 : integer) : integer;
  57. { Returns greater value. }
  58.  
  59. FUNCTION Min (Num1, Num2 : integer) : integer;
  60. { Returns lesser value. }
  61.  
  62. FUNCTION MaxReal (Num1, Num2 : real) : real;
  63. { Returns greater value. }
  64.  
  65. FUNCTION MinReal (Num1, Num2 : real) : real;
  66. { Returns lesser value. }
  67.  
  68. FUNCTION Dec2Hex (Num : word) : string;
  69. { Returns decimal value as hex string }
  70.  
  71. FUNCTION Hex2Dec (S : string) : longint;
  72. { returns hexadecimal string as decimal value }
  73.  
  74. { ========================================================================= }
  75. { Implementation ========================================================== }
  76.  
  77. IMPLEMENTATION
  78.  
  79. CONST
  80.   HexString : array [0..15] of char = '0123456789ABCDEF';
  81.  
  82. { ========================================================================= }
  83. { UpCycle ================================================================= }
  84.  
  85. PROCEDURE UpCycle (VAR Num : integer;  Bottom, Top : word);
  86. { Increases Num through cycle, bounded by Bottom and Top. }
  87.  
  88. BEGIN
  89.   inc (Num);
  90.   if Num > Top then Num := Bottom;
  91. END;
  92.  
  93. { DownCycle =============================================================== }
  94.  
  95. PROCEDURE DownCycle (VAR Num : integer;  Bottom, Top : word);
  96. { Decreases Num through cycle, bounded by Bottom and Top. }
  97.  
  98. BEGIN
  99.   dec (Num);
  100.   if Num < Bottom then Num := Top;
  101. END;
  102.  
  103. { Max ===================================================================== }
  104.  
  105. FUNCTION Max (Num1, Num2 : integer) : integer;
  106. BEGIN
  107.   if Num1 > Num2 Then Max := Num1 Else Max := Num2;
  108. END;
  109.  
  110. { Min ===================================================================== }
  111.  
  112. FUNCTION Min (Num1, Num2 : integer) : integer;
  113. BEGIN
  114.   if Num1 < Num2 Then Min := Num1 Else Min := Num2;
  115. END;
  116.  
  117. { MaxReal ================================================================= }
  118.  
  119. FUNCTION MaxReal (Num1, Num2 : real) : real;
  120. BEGIN
  121.   if Num1 > Num2 Then MaxReal := Num1 Else MaxReal := Num2;
  122. END;
  123.  
  124. { MinReal ================================================================= }
  125.  
  126. FUNCTION MinReal (Num1, Num2 : real) : real;
  127. BEGIN
  128.   if Num1 < Num2 Then MinReal := Num1 Else MinReal := Num2;
  129. END;
  130.  
  131. { Dec2Hex ================================================================= }
  132.  
  133. FUNCTION Dec2Hex (Num : word) : string;
  134. { Returns decimal value as hex string }
  135. VAR
  136.   Loop,
  137.   Bits  : byte;
  138.   S     : string [10];
  139.  
  140. BEGIN
  141.   S := '';
  142.   for Loop := 1 to 4 do begin
  143.     S := HexString [Lo (Num) and $F] + S;
  144.     Num := Num shr 4;
  145.     end;
  146.   Dec2Hex := '$' + S;
  147. END;
  148.  
  149. { Hex2Dec ================================================================= }
  150.  
  151. FUNCTION Hex2Dec (S : string) : longint;
  152. { returns hexadecimal string as decimal value }
  153. VAR
  154.   Len   : byte absolute S;
  155.   Loop  : byte;
  156.   Li    : longint;
  157.   Num   : longint;
  158.  
  159. BEGIN
  160.   if S [1] = '$' then delete (S, 1, 1);
  161.   if upcase (S [Len]) = 'H' then dec (S [0]);
  162.   Num := 0;
  163.   for Loop := 1 to Len do begin                            { get end letter }
  164.     Li := 0;
  165.     while
  166.       (HexString [Li] <> S [Loop])                         { compare letter }
  167.         and
  168.       (Li < 16)
  169.     do
  170.       inc (Li);                                            { inc counter }
  171.     if Li = 16 then begin
  172.       Num := -1;                                           { if invalid }
  173.       exit;
  174.       end;
  175.     Num := Num + Li shl ((Len - Loop) * 4);
  176.     end;
  177.   Hex2Dec := Num;                                          { return }
  178. END;
  179.  
  180. { Initialization ========================================================== }
  181.  
  182. { no initialization needed }
  183. END.
  184.  
  185. { ========================================================================= }
  186. { DgMath History ========================================================== }
  187.  
  188. VERSION HISTORY:
  189.   9005.05
  190.     Totally restructured for consistency with Object Professional.
  191.  
  192. { DgMath Needs ============================================================ }
  193.  
  194. NEED TO ADD:
  195.   Nothing right now.
  196.  
  197. { Bug Reports ============================================================= }
  198.  
  199. BUGS:
  200.   Don't be silly.
  201.  
  202. { ========================================================================= }
  203.