math.pp 28 KB

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