math.pp 27 KB

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