math.pp 20 KB

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