math.pp 29 KB

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