math.pp 21 KB

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