math.pp 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946
  1. {
  2. $Id$
  3. This file is part of the Free Pascal run time library.
  4. Copyright (c) 1999-2000 by Florian Klaempfl
  5. member of the Free Pascal development team
  6. See the file COPYING.FPC, included in this distribution,
  7. for details about the copyright.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  11. **********************************************************************}
  12. {
  13. This unit is an equivalent to the Delphi math unit
  14. (with some improvements)
  15. What's to do:
  16. o a lot of function :), search for !!!!
  17. o some statistical functions
  18. o all financial functions
  19. o optimizations
  20. }
  21. unit math;
  22. interface
  23. {$MODE objfpc}
  24. uses
  25. sysutils;
  26. const { Ranges of the IEEE floating point types, including denormals }
  27. MinSingle = 1.5e-45;
  28. MaxSingle = 3.4e+38;
  29. MinDouble = 5.0e-324;
  30. MaxDouble = 1.7e+308;
  31. MinExtended = 3.4e-4932;
  32. MaxExtended = 1.1e+4932;
  33. MinComp = -9.223372036854775807e+18;
  34. MaxComp = 9.223372036854775807e+18;
  35. type
  36. { the original delphi functions use extended as argument, }
  37. { but I would prefer double, because 8 bytes is a very }
  38. { natural size for the processor }
  39. { WARNING : changing float type will }
  40. { break all assembler code PM }
  41. float = extended;
  42. PFloat = ^Float;
  43. PInteger = ^Integer;
  44. tpaymenttime = (ptendofperiod,ptstartofperiod);
  45. einvalidargument = class(ematherror);
  46. TValueRelationship = -1..1;
  47. const
  48. EqualsValue = 0;
  49. LessThanValue = Low(TValueRelationship);
  50. GreaterThanValue = High(TValueRelationship);
  51. { Min/max determination }
  52. function MinIntValue(const Data: array of Integer): Integer;
  53. function MaxIntValue(const Data: array of Integer): Integer;
  54. { Extra, not present in Delphi, but used frequently }
  55. function Min(a, b: Integer): Integer;
  56. function Max(a, b: Integer): Integer;
  57. function Min(a, b: Cardinal): Cardinal;
  58. function Max(a, b: Cardinal): Cardinal;
  59. function Min(a, b: Int64): Int64;
  60. function Max(a, b: Int64): Int64;
  61. function Min(a, b: Single): Single;
  62. function Max(a, b: Single): Single;
  63. function Min(a, b: Double): Double;
  64. function Max(a, b: Double): Double;
  65. function Min(a, b: Extended): Extended;
  66. function Max(a, b: Extended): Extended;
  67. { angle conversion }
  68. function degtorad(deg : float) : float;
  69. function radtodeg(rad : float) : float;
  70. function gradtorad(grad : float) : float;
  71. function radtograd(rad : float) : float;
  72. function degtograd(deg : float) : float;
  73. function gradtodeg(grad : float) : float;
  74. { one cycle are 2*Pi rad }
  75. function cycletorad(cycle : float) : float;
  76. function radtocycle(rad : float) : float;
  77. { trigoniometric functions }
  78. function tan(x : float) : float;
  79. function cotan(x : float) : float;
  80. procedure sincos(theta : float;var sinus,cosinus : float);
  81. { inverse functions }
  82. function arccos(x : float) : float;
  83. function arcsin(x : float) : float;
  84. { calculates arctan(x/y) and returns an angle in the correct quadrant }
  85. function arctan2(x,y : float) : float;
  86. { hyperbolic functions }
  87. function cosh(x : float) : float;
  88. function sinh(x : float) : float;
  89. function tanh(x : float) : float;
  90. { area functions }
  91. { delphi names: }
  92. function arccosh(x : float) : float;
  93. function arcsinh(x : float) : float;
  94. function arctanh(x : float) : float;
  95. { IMHO the function should be called as follows (FK) }
  96. function arcosh(x : float) : float;
  97. function arsinh(x : float) : float;
  98. function artanh(x : float) : float;
  99. { triangle functions }
  100. { returns the length of the hypotenuse of a right triangle }
  101. { if x and y are the other sides }
  102. function hypot(x,y : float) : float;
  103. { logarithm functions }
  104. function log10(x : float) : float;
  105. function log2(x : float) : float;
  106. function logn(n,x : float) : float;
  107. { returns natural logarithm of x+1 }
  108. function lnxp1(x : float) : float;
  109. { exponential functions }
  110. function power(base,exponent : float) : float;
  111. { base^exponent }
  112. function intpower(base : float;const exponent : Integer) : float;
  113. { number converting }
  114. { rounds x towards positive infinity }
  115. function ceil(x : float) : Integer;
  116. { rounds x towards negative infinity }
  117. function floor(x : float) : Integer;
  118. { misc. functions }
  119. { splits x into mantissa and exponent (to base 2) }
  120. procedure Frexp(X: float; var Mantissa: float; var Exponent: integer);
  121. { returns x*(2^p) }
  122. function ldexp(x : float; const p : Integer) : float;
  123. { statistical functions }
  124. function mean(const data : array of float) : float;
  125. function sum(const data : array of float) : float;
  126. function mean(const data : PFloat; Const N : longint) : float;
  127. function sum(const data : PFloat; Const N : Longint) : float;
  128. function sumofsquares(const data : array of float) : float;
  129. function sumofsquares(const data : PFloat; Const N : Integer) : float;
  130. { calculates the sum and the sum of squares of data }
  131. procedure sumsandsquares(const data : array of float;
  132. var sum,sumofsquares : float);
  133. procedure sumsandsquares(const data : PFloat; Const N : Integer;
  134. var sum,sumofsquares : float);
  135. function minvalue(const data : array of float) : float;
  136. function minvalue(const data : array of integer) : Integer;
  137. function minvalue(const data : PFloat; Const N : Integer) : float;
  138. function MinValue(const Data : PInteger; Const N : Integer): Integer;
  139. function maxvalue(const data : array of float) : float;
  140. function maxvalue(const data : array of integer) : Integer;
  141. function maxvalue(const data : PFloat; Const N : Integer) : float;
  142. function maxvalue(const data : PInteger; Const N : Integer) : Integer;
  143. { calculates the standard deviation }
  144. function stddev(const data : array of float) : float;
  145. function stddev(const data : PFloat; Const N : Integer) : float;
  146. { calculates the mean and stddev }
  147. procedure meanandstddev(const data : array of float;
  148. var mean,stddev : float);
  149. procedure meanandstddev(const data : PFloat;
  150. Const N : Longint;var mean,stddev : float);
  151. function variance(const data : array of float) : float;
  152. function totalvariance(const data : array of float) : float;
  153. function variance(const data : PFloat; Const N : Integer) : float;
  154. function totalvariance(const data : PFloat; Const N : Integer) : float;
  155. { returns random values with gaussian distribution }
  156. function randg(mean,stddev : float) : float;
  157. { I don't know what the following functions do: }
  158. function popnstddev(const data : array of float) : float;
  159. function popnstddev(const data : PFloat; Const N : Integer) : float;
  160. function popnvariance(const data : PFloat; Const N : Integer) : float;
  161. function popnvariance(const data : array of float) : float;
  162. procedure momentskewkurtosis(const data : array of float;
  163. var m1,m2,m3,m4,skew,kurtosis : float);
  164. procedure momentskewkurtosis(const data : PFloat; Const N : Integer;
  165. var m1,m2,m3,m4,skew,kurtosis : float);
  166. { geometrical function }
  167. { returns the euclidean L2 norm }
  168. function norm(const data : array of float) : float;
  169. function norm(const data : PFloat; Const N : Integer) : float;
  170. implementation
  171. ResourceString
  172. SMathError = 'Math Error : %s';
  173. SInvalidArgument = 'Invalid argument';
  174. Procedure DoMathError(Const S : String);
  175. begin
  176. Raise EMathError.CreateFmt(SMathError,[S]);
  177. end;
  178. Procedure InvalidArgument;
  179. begin
  180. Raise EInvalidArgument.Create(SInvalidArgument);
  181. end;
  182. function degtorad(deg : float) : float;
  183. begin
  184. degtorad:=deg*(pi/180.0);
  185. end;
  186. function radtodeg(rad : float) : float;
  187. begin
  188. radtodeg:=rad*(180.0/pi);
  189. end;
  190. function gradtorad(grad : float) : float;
  191. begin
  192. gradtorad:=grad*(pi/200.0);
  193. end;
  194. function radtograd(rad : float) : float;
  195. begin
  196. radtograd:=rad*(200.0/pi);
  197. end;
  198. function degtograd(deg : float) : float;
  199. begin
  200. degtograd:=deg*(200.0/180.0);
  201. end;
  202. function gradtodeg(grad : float) : float;
  203. begin
  204. gradtodeg:=grad*(180.0/200.0);
  205. end;
  206. function cycletorad(cycle : float) : float;
  207. begin
  208. cycletorad:=(2*pi)*cycle;
  209. end;
  210. function radtocycle(rad : float) : float;
  211. begin
  212. { avoid division }
  213. radtocycle:=rad*(1/(2*pi));
  214. end;
  215. function tan(x : float) : float;
  216. begin
  217. Tan:=Sin(x)/Cos(x)
  218. end;
  219. function cotan(x : float) : float;
  220. begin
  221. cotan:=Cos(X)/Sin(X);
  222. end;
  223. procedure sincos(theta : float;var sinus,cosinus : float);
  224. begin
  225. sinus:=sin(theta);
  226. cosinus:=cos(theta);
  227. end;
  228. { Sign, ArcSin and ArcCos from Arjan van Dijk ([email protected]) }
  229. function sign(x : float) : float;
  230. begin
  231. if x > 0 then sign := 1.0
  232. else if x < 0 then sign := -1.0
  233. else sign := 0.0;
  234. end;
  235. function arcsin(x : float) : float;
  236. begin
  237. if abs(x) > 1 then InvalidArgument
  238. else if abs(x) < 0.5 then
  239. arcsin := arctan(x/sqrt(1-sqr(x)))
  240. else
  241. arcsin := sign(x) * (pi*0.5 - arctan(sqrt(1 / sqr(x) - 1)));
  242. end;
  243. function Arccos(x : Float) : Float;
  244. begin
  245. arccos := pi*0.5 - arcsin(x);
  246. end;
  247. function arctan2( x,y : float) : float;
  248. begin
  249. ArcTan2:=ArcTan(x/y);
  250. end;
  251. function cosh(x : float) : float;
  252. var
  253. temp : float;
  254. begin
  255. temp:=exp(x);
  256. cosh:=0.5*(temp+1.0/temp);
  257. end;
  258. function sinh(x : float) : float;
  259. var
  260. temp : float;
  261. begin
  262. temp:=exp(x);
  263. sinh:=0.5*(temp-1.0/temp);
  264. end;
  265. Const MaxTanh = 5678.22249441322; // Ln(MaxExtended)/2
  266. function tanh(x : float) : float;
  267. var Temp : float;
  268. begin
  269. if x>MaxTanh then exit(1.0)
  270. else if x<-MaxTanh then exit (-1.0);
  271. temp:=exp(-2*x);
  272. tanh:=(1-temp)/(1+temp)
  273. end;
  274. function arccosh(x : float) : float;
  275. begin
  276. arccosh:=arcosh(x);
  277. end;
  278. function arcsinh(x : float) : float;
  279. begin
  280. arcsinh:=arsinh(x);
  281. end;
  282. function arctanh(x : float) : float;
  283. begin
  284. if x>1 then InvalidArgument;
  285. arctanh:=artanh(x);
  286. end;
  287. function arcosh(x : float) : float;
  288. begin
  289. if x<1 then InvalidArgument;
  290. arcosh:=Ln(x+Sqrt(x*x-1));
  291. end;
  292. function arsinh(x : float) : float;
  293. begin
  294. arsinh:=Ln(x+Sqrt(1+x*x));
  295. end;
  296. function artanh(x : float) : float;
  297. begin
  298. If abs(x)>1 then InvalidArgument;
  299. artanh:=(Ln((1+x)/(1-x)))*0.5;
  300. end;
  301. function hypot(x,y : float) : float;
  302. begin
  303. hypot:=Sqrt(x*x+y*y)
  304. end;
  305. function log10(x : float) : float;
  306. begin
  307. log10:=ln(x)/ln(10);
  308. end;
  309. function log2(x : float) : float;
  310. begin
  311. log2:=ln(x)/ln(2)
  312. end;
  313. function logn(n,x : float) : float;
  314. begin
  315. if n<0 then InvalidArgument;
  316. logn:=ln(x)/ln(n);
  317. end;
  318. function lnxp1(x : float) : float;
  319. begin
  320. if x<-1 then
  321. InvalidArgument;
  322. lnxp1:=ln(1+x);
  323. end;
  324. function power(base,exponent : float) : float;
  325. begin
  326. If Exponent=0.0 then
  327. Result:=1.0
  328. else
  329. If base>0.0 then
  330. Power:=exp(exponent * ln (base))
  331. else if base=0.0 then
  332. Result:=0.0
  333. else
  334. InvalidArgument
  335. end;
  336. function intpower(base : float;const exponent : Integer) : float;
  337. var
  338. i : longint;
  339. begin
  340. i:=abs(exponent);
  341. intpower:=1.0;
  342. while i>0 do
  343. begin
  344. while (i and 1)=0 do
  345. begin
  346. i:=i shr 1;
  347. base:=sqr(base);
  348. end;
  349. i:=i-1;
  350. intpower:=intpower*base;
  351. end;
  352. if exponent<0 then
  353. intpower:=1.0/intpower;
  354. end;
  355. function ceil(x : float) : integer;
  356. begin
  357. Ceil:=Trunc(x);
  358. If Frac(x)>0 then
  359. Ceil:=Ceil+1;
  360. end;
  361. function floor(x : float) : integer;
  362. begin
  363. Floor:=Trunc(x);
  364. If Frac(x)<0 then
  365. Floor := Floor-1;
  366. end;
  367. procedure Frexp(X: float; var Mantissa: float; var Exponent: integer);
  368. begin
  369. Exponent :=0;
  370. if (abs(x)<0.5) then
  371. While (abs(x)<0.5) do
  372. begin
  373. x := x*2;
  374. Dec(Exponent);
  375. end
  376. else
  377. While (abs(x)>1) do
  378. begin
  379. x := x/2;
  380. Inc(Exponent);
  381. end;
  382. mantissa := x;
  383. end;
  384. function ldexp(x : float;const p : Integer) : float;
  385. begin
  386. ldexp:=x*intpower(2.0,p);
  387. end;
  388. function mean(const data : array of float) : float;
  389. begin
  390. Result:=Mean(@data[0],High(Data)+1);
  391. end;
  392. function mean(const data : PFloat; Const N : longint) : float;
  393. begin
  394. mean:=sum(Data,N);
  395. mean:=mean/N;
  396. end;
  397. function sum(const data : array of float) : float;
  398. begin
  399. Result:=Sum(@Data[0],High(Data)+1);
  400. end;
  401. function sum(const data : PFloat;Const N : longint) : float;
  402. var
  403. i : longint;
  404. begin
  405. sum:=0.0;
  406. for i:=0 to N-1 do
  407. sum:=sum+data[i];
  408. end;
  409. function sumofsquares(const data : array of float) : float;
  410. begin
  411. Result:=sumofsquares(@data[0],High(Data)+1);
  412. end;
  413. function sumofsquares(const data : PFloat; Const N : Integer) : float;
  414. var
  415. i : longint;
  416. begin
  417. sumofsquares:=0.0;
  418. for i:=0 to N-1 do
  419. sumofsquares:=sumofsquares+sqr(data[i]);
  420. end;
  421. procedure sumsandsquares(const data : array of float;
  422. var sum,sumofsquares : float);
  423. begin
  424. sumsandsquares (@Data[0],High(Data)+1,Sum,sumofsquares);
  425. end;
  426. procedure sumsandsquares(const data : PFloat; Const N : Integer;
  427. var sum,sumofsquares : float);
  428. var
  429. i : Integer;
  430. temp : float;
  431. begin
  432. sumofsquares:=0.0;
  433. sum:=0.0;
  434. for i:=0 to N-1 do
  435. begin
  436. temp:=data[i];
  437. sumofsquares:=sumofsquares+sqr(temp);
  438. sum:=sum+temp;
  439. end;
  440. end;
  441. function stddev(const data : array of float) : float;
  442. begin
  443. Result:=Stddev(@Data[0],High(Data)+1)
  444. end;
  445. function stddev(const data : PFloat; Const N : Integer) : float;
  446. begin
  447. StdDev:=Sqrt(Variance(Data,N));
  448. end;
  449. procedure meanandstddev(const data : array of float;
  450. var mean,stddev : float);
  451. begin
  452. Meanandstddev(@Data[0],High(Data)+1,Mean,stddev);
  453. end;
  454. procedure meanandstddev(const data : PFloat;
  455. Const N : Longint;var mean,stddev : float);
  456. Var I : longint;
  457. begin
  458. Mean:=0;
  459. StdDev:=0;
  460. For I:=0 to N-1 do
  461. begin
  462. Mean:=Mean+Data[i];
  463. StdDev:=StdDev+Sqr(Data[i]);
  464. end;
  465. Mean:=Mean/N;
  466. StdDev:=(StdDev-N*Sqr(Mean));
  467. If N>1 then
  468. StdDev:=Sqrt(Stddev/(N-1))
  469. else
  470. StdDev:=0;
  471. end;
  472. function variance(const data : array of float) : float;
  473. begin
  474. Variance:=Variance(@Data[0],High(Data)+1);
  475. end;
  476. function variance(const data : PFloat; Const N : Integer) : float;
  477. begin
  478. If N=1 then
  479. Result:=0
  480. else
  481. Result:=TotalVariance(Data,N)/(N-1);
  482. end;
  483. function totalvariance(const data : array of float) : float;
  484. begin
  485. Result:=TotalVariance(@Data[0],High(Data)+1);
  486. end;
  487. function totalvariance(const data : Pfloat;Const N : Integer) : float;
  488. var S,SS : Float;
  489. begin
  490. If N=1 then
  491. Result:=0
  492. else
  493. begin
  494. SumsAndSquares(Data,N,S,SS);
  495. Result := SS-Sqr(S)/N;
  496. end;
  497. end;
  498. function randg(mean,stddev : float) : float;
  499. Var U1,S2 : Float;
  500. begin
  501. repeat
  502. u1:= 2*random-1;
  503. S2:=Sqr(U1)+sqr(2*random-1);
  504. until s2<1;
  505. randg:=Sqrt(-2*ln(S2)/S2)*u1*stddev+Mean;
  506. end;
  507. function popnstddev(const data : array of float) : float;
  508. begin
  509. PopnStdDev:=Sqrt(PopnVariance(@Data[0],High(Data)+1));
  510. end;
  511. function popnstddev(const data : PFloat; Const N : Integer) : float;
  512. begin
  513. PopnStdDev:=Sqrt(PopnVariance(Data,N));
  514. end;
  515. function popnvariance(const data : array of float) : float;
  516. begin
  517. popnvariance:=popnvariance(@data[0],high(Data)+1);
  518. end;
  519. function popnvariance(const data : PFloat; Const N : Integer) : float;
  520. begin
  521. PopnVariance:=TotalVariance(Data,N)/N;
  522. end;
  523. procedure momentskewkurtosis(const data : array of float;
  524. var m1,m2,m3,m4,skew,kurtosis : float);
  525. begin
  526. momentskewkurtosis(@Data[0],High(Data)+1,m1,m2,m3,m4,skew,kurtosis);
  527. end;
  528. procedure momentskewkurtosis(const data : PFloat; Const N : Integer;
  529. var m1,m2,m3,m4,skew,kurtosis : float);
  530. Var S,SS,SC,SQ,invN,Acc,M1S,S2N,S3N,temp : Float;
  531. I : Longint;
  532. begin
  533. invN:=1.0/N;
  534. s:=0;
  535. ss:=0;
  536. sq:=0;
  537. sc:=0;
  538. for i:=0 to N-1 do
  539. begin
  540. temp:=Data[i]; { faster }
  541. S:=S+temp;
  542. acc:=temp*temp;
  543. ss:=ss+acc;
  544. Acc:=acc*temp;
  545. Sc:=sc+acc;
  546. acc:=acc*temp;
  547. sq:=sq+acc;
  548. end;
  549. M1:=s*invN;
  550. M1S:=M1*M1;
  551. S2N:=SS*invN;
  552. S3N:=SC*invN;
  553. M2:=S2N-M1S;
  554. M3:=S3N-(M1*3*S2N) + 2*M1S*M1;
  555. M4:=SQ*invN - (M1 * 4 * S3N) + (M1S*6*S2N-3*Sqr(M1S));
  556. Skew:=M3*power(M2,-3/2);
  557. Kurtosis:=M4 / Sqr(M2);
  558. end;
  559. function norm(const data : array of float) : float;
  560. begin
  561. norm:=Norm(@data[0],High(Data)+1);
  562. end;
  563. function norm(const data : PFloat; Const N : Integer) : float;
  564. begin
  565. norm:=sqrt(sumofsquares(data,N));
  566. end;
  567. function MinIntValue(const Data: array of Integer): Integer;
  568. var
  569. I: Integer;
  570. begin
  571. Result := Data[Low(Data)];
  572. For I := Succ(Low(Data)) To High(Data) Do
  573. If Data[I] < Result Then Result := Data[I];
  574. end;
  575. function MinValue(const Data: array of Integer): Integer;
  576. begin
  577. Result:=MinValue(Pinteger(@Data[0]),High(Data)+1)
  578. end;
  579. function MinValue(const Data: PInteger; Const N : Integer): Integer;
  580. var
  581. I: Integer;
  582. begin
  583. Result := Data[0];
  584. For I := 1 To N-1 do
  585. If Data[I] < Result Then Result := Data[I];
  586. end;
  587. function minvalue(const data : array of float) : float;
  588. begin
  589. Result:=minvalue(PFloat(@data[0]),High(Data)+1);
  590. end;
  591. function minvalue(const data : PFloat; Const N : Integer) : float;
  592. var
  593. i : longint;
  594. begin
  595. { get an initial value }
  596. minvalue:=data[0];
  597. for i:=1 to N-1 do
  598. if data[i]<minvalue then
  599. minvalue:=data[i];
  600. end;
  601. function MaxIntValue(const Data: array of Integer): Integer;
  602. var
  603. I: Integer;
  604. begin
  605. Result := Data[Low(Data)];
  606. For I := Succ(Low(Data)) To High(Data) Do
  607. If Data[I] > Result Then Result := Data[I];
  608. end;
  609. function maxvalue(const data : array of float) : float;
  610. begin
  611. Result:=maxvalue(PFloat(@data[0]),High(Data)+1);
  612. end;
  613. function maxvalue(const data : PFloat; Const N : Integer) : float;
  614. var
  615. i : longint;
  616. begin
  617. { get an initial value }
  618. maxvalue:=data[0];
  619. for i:=1 to N-1 do
  620. if data[i]>maxvalue then
  621. maxvalue:=data[i];
  622. end;
  623. function MaxValue(const Data: array of Integer): Integer;
  624. begin
  625. Result:=MaxValue(PInteger(@Data[0]),High(Data)+1)
  626. end;
  627. function maxvalue(const data : PInteger; Const N : Integer) : Integer;
  628. var
  629. i : longint;
  630. begin
  631. { get an initial value }
  632. maxvalue:=data[0];
  633. for i:=1 to N-1 do
  634. if data[i]>maxvalue then
  635. maxvalue:=data[i];
  636. end;
  637. function Min(a, b: Integer): Integer;
  638. begin
  639. if a < b then
  640. Result := a
  641. else
  642. Result := b;
  643. end;
  644. function Max(a, b: Integer): Integer;
  645. begin
  646. if a > b then
  647. Result := a
  648. else
  649. Result := b;
  650. end;
  651. function Min(a, b: Cardinal): Cardinal;
  652. begin
  653. if a < b then
  654. Result := a
  655. else
  656. Result := b;
  657. end;
  658. function Max(a, b: Cardinal): Cardinal;
  659. begin
  660. if a > b then
  661. Result := a
  662. else
  663. Result := b;
  664. end;
  665. function Min(a, b: Int64): Int64;
  666. begin
  667. if a < b then
  668. Result := a
  669. else
  670. Result := b;
  671. end;
  672. function Max(a, b: Int64): Int64;
  673. begin
  674. if a > b then
  675. Result := a
  676. else
  677. Result := b;
  678. end;
  679. function Min(a, b: Single): Single;
  680. begin
  681. if a < b then
  682. Result := a
  683. else
  684. Result := b;
  685. end;
  686. function Max(a, b: Single): Single;
  687. begin
  688. if a > b then
  689. Result := a
  690. else
  691. Result := b;
  692. end;
  693. function Min(a, b: Double): Double;
  694. begin
  695. if a < b then
  696. Result := a
  697. else
  698. Result := b;
  699. end;
  700. function Max(a, b: Double): Double;
  701. begin
  702. if a > b then
  703. Result := a
  704. else
  705. Result := b;
  706. end;
  707. function Min(a, b: Extended): Extended;
  708. begin
  709. if a < b then
  710. Result := a
  711. else
  712. Result := b;
  713. end;
  714. function Max(a, b: Extended): Extended;
  715. begin
  716. if a > b then
  717. Result := a
  718. else
  719. Result := b;
  720. end;
  721. end.
  722. {
  723. $Log$
  724. Revision 1.8 2002-09-07 21:06:12 carl
  725. * cleanup of parameters
  726. - remove assembler code
  727. Revision 1.7 2002/09/07 16:01:22 peter
  728. * old logs removed and tabs fixed
  729. }