home *** CD-ROM | disk | FTP | other *** search
/ Chip 1998 March / Chip_1998-03_cd.bin / zkuste / delphi / WhiteAnts / PROGRESS.ZIP / NUMUTILS.PAS < prev    next >
Encoding:
Pascal/Delphi Source File  |  1997-01-10  |  9.3 KB  |  330 lines

  1. {
  2. +----------------------------------------------------------------------------+
  3. |                                      ⌐  ⌐                                  |
  4. |                                    ⌐⌐ ⌐ ⌐ ⌐                                |
  5. |                                 ⌐⌐⌐ ⌐   ⌐  ⌐                               |
  6. |                                 ⌐⌐    ⌐ ⌐   ⌐                              |
  7. |                  ⌐             ⌐⌐     ⌐  ⌐                                 |
  8. |                 ⌐ ⌐            ⌐⌐⌐    ⌐⌐  ⌐                                |
  9. |             ⌐⌐  ⌐  ⌐      ⌐⌐⌐⌐⌐⌐⌐⌐⌐⌐  ⌐                                    |
  10. |            ⌐  ⌐⌐  ⌐⌐      ⌐⌐⌐⌐⌐⌐⌐⌐⌐⌐⌐⌐⌐⌐⌐                                  |
  11. |            ⌐ ⌐⌐⌐ ⌐⌐⌐⌐⌐⌐ ⌐⌐⌐ ⌐   ⌐⌐⌐⌐⌐⌐⌐⌐                                   |
  12. |           ⌐ ⌐⌐ ⌐⌐⌐⌐⌐⌐⌐⌐⌐⌐⌐⌐ ⌐   ⌐⌐⌐⌐⌐⌐⌐⌐⌐⌐      Copyright ⌐ 1996-1997 by:  |
  13. |           ⌐ ⌐⌐⌐⌐⌐⌐ ⌐⌐⌐⌐⌐⌐⌐⌐⌐⌐⌐  ⌐ ⌐⌐⌐⌐⌐ ⌐⌐                                 |
  14. |          ⌐ ⌐⌐⌐⌐⌐⌐⌐   ⌐⌐⌐⌐⌐ ⌐⌐⌐⌐    ⌐⌐ ⌐⌐ ⌐      WHITE ANTS SYSTEMHOUSE BV  |
  15. |         ⌐  ⌐⌐⌐⌐⌐⌐⌐ ⌐⌐⌐ ⌐⌐⌐ ⌐⌐ ⌐       ⌐⌐⌐⌐      Geleen 12                  |
  16. |         ⌐ ⌐⌐⌐⌐⌐⌐⌐    ⌐   ⌐⌐   ⌐⌐⌐       ⌐       8032 GB Zwolle             |
  17. |           ⌐⌐⌐⌐⌐⌐     ⌐            ⌐ ⌐           Netherlands                |
  18. |      ⌐⌐⌐  ⌐⌐⌐⌐⌐      ⌐     ⌐⌐     ⌐  ⌐                                     |
  19. |            ⌐⌐       ⌐              ⌐  ⌐⌐⌐ ⌐     Tel. +31 38 453 86 31      |
  20. |      ⌐              ⌐              ⌐            Fax. +31 38 453 41 22      |
  21. |      ⌐             ⌐               ⌐⌐                                      |
  22. |    ⌐              ⌐                  ⌐⌐         www.whiteants.com          |
  23. |  ⌐⌐              ⌐                     ⌐ ⌐      support@whiteants.com      |
  24. |                 ⌐                                                          |
  25. +----------------------------------------------------------------------------+
  26.   file     : NumUtils
  27.   version  : 1.0
  28.   comment  : NumUtils contains numeric utility procedures
  29.   author   : G. Beuze / J. Laarhoven / R. Post
  30.   compiler : Delphi 1.0
  31. +----------------------------------------------------------------------------+
  32. | DISCLAIMER:                                                                |
  33. | THIS SOURCE IS FREEWARE. YOU ARE ALLOWED TO USE IT IN YOUR OWN PROJECTS    |
  34. | WITHOUT ANY RESTRICTIONS, BUT YOU ARE NOT ALLOWED TO SELL IT IN ANY WAY.   |
  35. | THERE IS NO WARRANTY AT ALL - YOU USE IT ON YOUR OWN RISC. WHITE ANTS DOES |
  36. | NOT ASSUME ANY RESPONSIBILITY FOR ANY DAMAGE OR ANY LOSS OF TIME OR MONEY  |
  37. | DUE THE USE OF ANY PART OF THIS SOURCE CODE.                               |
  38. +----------------------------------------------------------------------------+
  39. }
  40. unit NumUtils;
  41.  
  42. interface
  43.  
  44. {
  45. ******************** Ordinal type procedures and functions *******************
  46. }
  47. function MaxNInt(const Values: array of LongInt): LongInt;
  48.   { returns minimum of n longints, Values must contain at least one item }
  49.  
  50. function MinNInt(const Values: array of LongInt): LongInt;
  51.   { returns minimum of n LongInts, Values must contain at least one item }
  52.  
  53. function Min2Int(X, Y: Integer): Integer;
  54.   { returns minimum from x, y }
  55.  
  56. function Max2Int(X, Y: Integer): Integer;
  57.   { Returns maximum from x, y }
  58.  
  59. function MinLong(X, Y: LongInt): LongInt;
  60.   { returns minimum from x, y }
  61.  
  62. function MaxLong(X, Y: LongInt): LongInt;
  63.   { Returns maximum from x, y }
  64.  
  65. function MinWord(X, Y: Word): Word;
  66.   { returns minimum from x, y }
  67.  
  68. function MaxWord(X, Y: Word): Word;
  69.   { Returns maximum from x, y }
  70.  
  71. function MinByte(X, Y: Byte): Byte;
  72.   { returns minimum from x, y }
  73.  
  74. function MaxByte(X, Y: Byte): Byte;
  75.   { Returns maximum from x, y }
  76.  
  77. function CompareInt(L1, L2: LongInt): Integer;
  78.   { Returns 1 if L1 > L2, -1 if L1 < l2 and 0 if L1 = L2 }
  79.  
  80. function InRange(Value, LowBound, UpBound: LongInt): Boolean;
  81.   { Returns True if Value >= LowBound and Value <= UpBound }
  82.  
  83. function LimitToRange(Value, LowBound, UpBound: LongInt): LongInt;
  84.   { Returns Value if Value in range [LowBound..UpBound] else Low or Up.
  85.     If range is invalid (LowBound > UpBound) LowBound is returned }
  86.  
  87. procedure MirrorLong(var L: LongInt);
  88.   { converts order of bytes: $01020304 => $04030201 }
  89.  
  90. function GetMirror(L: LongInt): LongInt;
  91.   { returns MirrorLong(L) }
  92.  
  93. procedure SetBits(var State: Word; ABits: Word; Enable: Boolean);
  94.   { Enables ABits bits in State }
  95.  
  96. function BitState(State, ABits: Word): Boolean;
  97.   { Returns True if State and ABits  <> 0 , so if any bit is set }
  98.  
  99. {
  100. ***************************Floating point utility funtions ****************************
  101. }
  102. function MaxFloat(X, Y: Extended): Extended;
  103.   { Returns maximum from x, y }
  104.  
  105. function MinFloat(X, Y: Extended): Extended;
  106.   { Returns minimum from x, y }
  107.  
  108. function MaxNFloat(const Values: array of Extended): Extended;
  109.   { Returns maximum of Values, Values must contain at least one item }
  110.  
  111. function MinNFloat(const Values: array of Extended): Extended;
  112.   { Returns Minimum of Values, Values must contain at least one item }
  113.  
  114. procedure SwapFloat(var X, Y: Extended);
  115.   { Swaps x with y and vice versa }
  116.  
  117. function Log2(X: Extended): Extended;
  118.   { Returns the 2_Log(x) value }
  119.  
  120. function PWR(X, Y : Extended): Extended;
  121.   { returns X^Y, calculated as PWR := Exp(Y * Ln(X)) for X > 0.
  122.     X MUST be > 0 }
  123.  
  124. function Sign(X: Extended): LongInt;
  125.   { Returns the sign of X: 1 if X >= 0 else -1 }
  126.  
  127. implementation
  128.  
  129. {
  130. ******************** Ordinal type procedures and functions *******************
  131. }
  132. function MaxNInt(const Values: array of LongInt): LongInt;
  133. var I: Integer;
  134. begin
  135.   Result := Values[Low(Values)];
  136.   for I := Low(Values) + 1 to High(Values) do
  137.     if Values[I] > Result then Result := Values[I];
  138. end;
  139.  
  140. function MinNInt(const Values: array of LongInt): LongInt;
  141. var I: Integer;
  142. begin
  143.   Result := Values[Low(Values)];
  144.   for I := Low(Values) + 1 to High(Values) do
  145.     if Values[I] < Result then Result := Values[I];
  146. end;
  147.  
  148. function Min2Int(X, Y: Integer): Integer; assembler;
  149. asm
  150.         MOV     AX,X
  151.         CMP     AX,Y
  152.         JLE     @@1
  153.         MOV     AX,Y
  154. @@1:
  155. end;
  156.  
  157. function Max2Int(X, Y: Integer): Integer; assembler;
  158. asm
  159.         MOV     AX,X
  160.         CMP     AX,Y
  161.         JGE     @@1
  162.         MOV     AX,Y
  163. @@1:
  164. end;
  165.  
  166. function MaxLong(X, Y: LongInt): LongInt;
  167. begin
  168.   if X >= Y then
  169.     Result := X
  170.   else
  171.     Result := Y;
  172. end;
  173.  
  174. function MinLong(X, Y: LongInt): LongInt;
  175. begin
  176.   if X <= Y then
  177.     Result := X
  178.   else
  179.     Result := Y;
  180. end;
  181.  
  182. function MinWord(X, Y: Word): Word; assembler;
  183. asm
  184.         MOV     AX,X
  185.         CMP     AX,Y
  186.         JBE     @@1
  187.         MOV     AX,Y
  188. @@1:
  189. end;
  190.  
  191. function MaxWord(X, Y: Word): Word; assembler;
  192. asm
  193.         MOV     AX,X
  194.         CMP     AX,Y
  195.         JAE     @@1
  196.         MOV     AX,Y
  197. @@1:
  198. end;
  199.  
  200. function MinByte(X, Y: Byte): Byte;
  201. begin
  202.   if X >= Y then
  203.     Result := Y
  204.   else
  205.     Result := X;
  206. end;
  207.  
  208. function MaxByte(X, Y: Byte): Byte;
  209. begin
  210.   if X <= Y then
  211.     Result := Y
  212.   else
  213.     Result := X;
  214. end;
  215.  
  216. function CompareInt(L1, L2: LongInt): Integer;
  217. begin
  218.   if L1 < L2 then Result := -1
  219.   else
  220.     if L1 > L2 then Result := 1
  221.     else  Result := 0;
  222. end;
  223.  
  224. function InRange(Value, LowBound, UpBound: LongInt): Boolean;
  225. begin
  226.   Result := (Value >= LowBound) and (Value <= UpBound);
  227. end;
  228.  
  229. function LimitToRange(Value, LowBound, UpBound: LongInt): LongInt;
  230. begin
  231.   Result := MaxLong(LowBound, MinLong(UpBound, Value));
  232. end;
  233.  
  234. procedure MirrorLong(var L: LongInt);
  235. var
  236.   Buf : record
  237.     case Integer of
  238.       0: (Long: LongInt);
  239.       1: (B: array[0..3] of Byte);
  240.     end;
  241.   AByte: Byte;
  242. begin
  243.   Buf.Long := L;
  244.   AByte := Buf.B[0];
  245.   Buf.B[0] := Buf.B[3];
  246.   Buf.B[3] := AByte;
  247.   AByte := Buf.B[1];
  248.   Buf.B[1] := Buf.B[2];
  249.   Buf.B[2] := AByte;
  250.   L := Buf.Long;
  251. end;
  252.  
  253. function GetMirror(L: LongInt): LongInt;
  254. begin
  255.   MirrorLong(L);
  256.   Result := L;
  257. end;
  258.  
  259. procedure SetBits(var State: Word; ABits: Word; Enable: Boolean);
  260. begin
  261.   if Enable then
  262.     State := State or ABits
  263.   else
  264.     State := State and not ABits;
  265. end;
  266.  
  267. function BitState(State, ABits: Word): Boolean;
  268. begin
  269.   Result := (State and ABits <> 0);
  270. end;
  271.  
  272.  
  273. {
  274. **************************** Floating point utility routines ****************************
  275. }
  276. function MaxFloat(X, Y: Extended): Extended;
  277. begin
  278.   if X >= Y then Result := X else Result := Y;
  279. end;
  280.  
  281. function MinFloat(X, Y: Extended): Extended;
  282. begin
  283.   if X <= Y then Result := X else Result := Y;
  284. end;
  285.  
  286. function MaxNFloat(const Values: array of Extended): Extended;
  287. var I: Integer;
  288. begin
  289.   Result := Values[Low(Values)];
  290.   for I := Low(Values) + 1 to High(Values) do
  291.     if Values[I] > Result then Result := Values[I];
  292. end;
  293.  
  294. function MinNFloat(const Values: array of Extended): Extended;
  295. var I: Integer;
  296. begin
  297.   Result := Values[Low(Values)];
  298.   for I := Low(Values) + 1 to High(Values) do
  299.     if Values[I] < Result then Result := Values[I];
  300. end;
  301.  
  302. procedure SwapFloat(var X, Y: Extended);
  303. var Z: Extended;
  304. begin
  305.   Z := X;
  306.   X := Y;
  307.   Y := Z;
  308. end;
  309.  
  310. function Log2(X: Extended): Extended;
  311. const ln2 : Extended = 0.;
  312. begin
  313.   if ln2 = 0. then ln2 := Ln(2);
  314.   Result := Ln(X)/ln2
  315. end;
  316.  
  317. function PWR(X, Y : Extended): Extended;
  318. begin
  319.   PWR := Exp(Y * Ln(X))
  320. end;
  321.  
  322. function Sign(X: Extended): LongInt;
  323. begin
  324.   if X < 0 then Result := -1 else Result := 1;
  325. end;
  326.  
  327.  
  328.  
  329. end.
  330.