math.pp 21 KB

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