math.pp 21 KB

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