math.pp 26 KB

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