math.pp 30 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385
  1. {
  2. This file is part of the Free Pascal run time library.
  3. Copyright (c) 1999-2005 by Florian Klaempfl
  4. member of the Free Pascal development team
  5. See the file COPYING.FPC, included in this distribution,
  6. for details about the copyright.
  7. This program is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  10. **********************************************************************}
  11. {
  12. This unit is an equivalent to the Delphi math unit
  13. (with some improvements)
  14. What's to do:
  15. o some statistical functions
  16. o all financial functions
  17. o optimizations
  18. }
  19. {$MODE objfpc}
  20. {$inline on }
  21. unit math;
  22. interface
  23. uses
  24. sysutils;
  25. { Ranges of the IEEE floating point types, including denormals }
  26. {$ifdef FPC_HAS_TYPE_SINGLE}
  27. const
  28. MinSingle = 1.5e-45;
  29. MaxSingle = 3.4e+38;
  30. {$endif FPC_HAS_TYPE_SINGLE}
  31. {$ifdef FPC_HAS_TYPE_DOUBLE}
  32. const
  33. MinDouble = 5.0e-324;
  34. MaxDouble = 1.7e+308;
  35. {$endif FPC_HAS_TYPE_DOUBLE}
  36. {$ifdef FPC_HAS_TYPE_EXTENDED}
  37. const
  38. MinExtended = 3.4e-4932;
  39. MaxExtended = 1.1e+4932;
  40. {$endif FPC_HAS_TYPE_EXTENDED}
  41. {$ifdef FPC_HAS_TYPE_COMP}
  42. const
  43. MinComp = -9.223372036854775807e+18;
  44. MaxComp = 9.223372036854775807e+18;
  45. {$endif FPC_HAS_TYPE_COMP}
  46. { the original delphi functions use extended as argument, }
  47. { but I would prefer double, because 8 bytes is a very }
  48. { natural size for the processor }
  49. { WARNING : changing float type will }
  50. { break all assembler code PM }
  51. {$ifdef FPC_HAS_TYPE_FLOAT128}
  52. type
  53. float = float128;
  54. const
  55. MinFloat = MinFloat128;
  56. MaxFloat = MaxFloat128;
  57. {$else FPC_HAS_TYPE_FLOAT128}
  58. {$ifdef FPC_HAS_TYPE_EXTENDED}
  59. type
  60. float = extended;
  61. const
  62. MinFloat = MinExtended;
  63. MaxFloat = MaxExtended;
  64. {$else FPC_HAS_TYPE_EXTENDED}
  65. {$ifdef FPC_HAS_TYPE_DOUBLE}
  66. type
  67. float = double;
  68. const
  69. MinFloat = MinDouble;
  70. MaxFloat = MaxDouble;
  71. {$else FPC_HAS_TYPE_DOUBLE}
  72. {$ifdef FPC_HAS_TYPE_SINGLE}
  73. type
  74. float = single;
  75. const
  76. MinFloat = MinSingle;
  77. MaxFloat = MaxSingle;
  78. {$else FPC_HAS_TYPE_SINGLE}
  79. {$fatal At least one floating point type must be supported}
  80. {$endif FPC_HAS_TYPE_SINGLE}
  81. {$endif FPC_HAS_TYPE_DOUBLE}
  82. {$endif FPC_HAS_TYPE_EXTENDED}
  83. {$endif FPC_HAS_TYPE_FLOAT128}
  84. type
  85. PFloat = ^Float;
  86. PInteger = ^Integer;
  87. tpaymenttime = (ptendofperiod,ptstartofperiod);
  88. einvalidargument = class(ematherror);
  89. TValueRelationship = -1..1;
  90. const
  91. EqualsValue = 0;
  92. LessThanValue = Low(TValueRelationship);
  93. GreaterThanValue = High(TValueRelationship);
  94. {$ifopt R+}
  95. {$define RangeCheckWasOn}
  96. {$R-}
  97. {$endif opt R+}
  98. {$ifopt Q+}
  99. {$define OverflowCheckWasOn}
  100. {$Q-}
  101. {$endif opt Q+}
  102. {$ifdef CPUARM}
  103. { the ARM linux emulator doesn't like 0.0/0.0 }
  104. NaN = ln(-1.0);
  105. {$else CPUARM}
  106. NaN = 0.0/0.0;
  107. {$endif CPUARM}
  108. Infinity = 1.0/0.0;
  109. {$ifdef RangeCheckWasOn}
  110. {$R+}
  111. {$undef RangeCheckWasOn}
  112. {$endif}
  113. {$ifdef OverflowCheckWasOn}
  114. {$Q+}
  115. {$undef OverflowCheckWasOn}
  116. {$endif}
  117. { Min/max determination }
  118. function MinIntValue(const Data: array of Integer): Integer;
  119. function MaxIntValue(const Data: array of Integer): Integer;
  120. { Extra, not present in Delphi, but used frequently }
  121. function Min(a, b: Integer): Integer;inline;
  122. function Max(a, b: Integer): Integer;inline;
  123. { this causes more trouble than it solves
  124. function Min(a, b: Cardinal): Cardinal;
  125. function Max(a, b: Cardinal): Cardinal;
  126. }
  127. function Min(a, b: Int64): Int64;inline;
  128. function Max(a, b: Int64): Int64;inline;
  129. {$ifdef FPC_HAS_TYPE_SINGLE}
  130. function Min(a, b: Single): Single;inline;
  131. function Max(a, b: Single): Single;inline;
  132. {$endif FPC_HAS_TYPE_SINGLE}
  133. {$ifdef FPC_HAS_TYPE_DOUBLE}
  134. function Min(a, b: Double): Double;inline;
  135. function Max(a, b: Double): Double;inline;
  136. {$endif FPC_HAS_TYPE_DOUBLE}
  137. {$ifdef FPC_HAS_TYPE_EXTENDED}
  138. function Min(a, b: Extended): Extended;inline;
  139. function Max(a, b: Extended): Extended;inline;
  140. {$endif FPC_HAS_TYPE_EXTENDED}
  141. function InRange(const AValue, AMin, AMax: Integer): Boolean;inline;
  142. function InRange(const AValue, AMin, AMax: Int64): Boolean;inline;
  143. {$ifdef FPC_HAS_TYPE_DOUBLE}
  144. function InRange(const AValue, AMin, AMax: Double): Boolean;inline;
  145. {$endif FPC_HAS_TYPE_DOUBLE}
  146. function EnsureRange(const AValue, AMin, AMax: Integer): Integer;inline;
  147. function EnsureRange(const AValue, AMin, AMax: Int64): Int64;inline;
  148. {$ifdef FPC_HAS_TYPE_DOUBLE}
  149. function EnsureRange(const AValue, AMin, AMax: Double): Double;inline;
  150. {$endif FPC_HAS_TYPE_DOUBLE}
  151. procedure DivMod(Dividend: Integer; Divisor: Word; var Result, Remainder: Word);
  152. // Sign functions
  153. Type
  154. TValueSign = -1..1;
  155. const
  156. NegativeValue = Low(TValueSign);
  157. ZeroValue = 0;
  158. PositiveValue = High(TValueSign);
  159. function Sign(const AValue: Integer): TValueSign;inline;
  160. function Sign(const AValue: Int64): TValueSign;inline;
  161. function Sign(const AValue: Double): TValueSign;inline;
  162. function IsZero(const A: Single; Epsilon: Single): Boolean;
  163. function IsZero(const A: Single): Boolean;inline;
  164. {$ifdef FPC_HAS_TYPE_DOUBLE}
  165. function IsZero(const A: Double; Epsilon: Double): Boolean;
  166. function IsZero(const A: Double): Boolean;inline;
  167. {$endif FPC_HAS_TYPE_DOUBLE}
  168. {$ifdef FPC_HAS_TYPE_EXTENDED}
  169. function IsZero(const A: Extended; Epsilon: Extended): Boolean;
  170. function IsZero(const A: Extended): Boolean;inline;
  171. {$endif FPC_HAS_TYPE_EXTENDED}
  172. function IsNan(const d : Double): Boolean;
  173. function IsInfinite(const d : Double): Boolean;
  174. {$ifdef FPC_HAS_TYPE_EXTENDED}
  175. function SameValue(const A, B: Extended): Boolean;inline;
  176. {$endif}
  177. {$ifdef FPC_HAS_TYPE_DOUBLE}
  178. function SameValue(const A, B: Double): Boolean;inline;
  179. {$endif}
  180. function SameValue(const A, B: Single): Boolean;inline;
  181. {$ifdef FPC_HAS_TYPE_EXTENDED}
  182. function SameValue(const A, B: Extended; Epsilon: Extended): Boolean;
  183. {$endif}
  184. {$ifdef FPC_HAS_TYPE_DOUBLE}
  185. function SameValue(const A, B: Double; Epsilon: Double): Boolean;
  186. {$endif}
  187. function SameValue(const A, B: Single; Epsilon: Single): Boolean;
  188. { angle conversion }
  189. function degtorad(deg : float) : float;
  190. function radtodeg(rad : float) : float;
  191. function gradtorad(grad : float) : float;
  192. function radtograd(rad : float) : float;
  193. function degtograd(deg : float) : float;
  194. function gradtodeg(grad : float) : float;
  195. { one cycle are 2*Pi rad }
  196. function cycletorad(cycle : float) : float;
  197. function radtocycle(rad : float) : float;
  198. { trigoniometric functions }
  199. function tan(x : float) : float;
  200. function cotan(x : float) : float;
  201. procedure sincos(theta : float;var sinus,cosinus : float);
  202. { inverse functions }
  203. function arccos(x : float) : float;
  204. function arcsin(x : float) : float;
  205. { calculates arctan(y/x) and returns an angle in the correct quadrant }
  206. function arctan2(y,x : float) : float;
  207. { hyperbolic functions }
  208. function cosh(x : float) : float;
  209. function sinh(x : float) : float;
  210. function tanh(x : float) : float;
  211. { area functions }
  212. { delphi names: }
  213. function arccosh(x : float) : float;
  214. function arcsinh(x : float) : float;
  215. function arctanh(x : float) : float;
  216. { IMHO the function should be called as follows (FK) }
  217. function arcosh(x : float) : float;
  218. function arsinh(x : float) : float;
  219. function artanh(x : float) : float;
  220. { triangle functions }
  221. { returns the length of the hypotenuse of a right triangle }
  222. { if x and y are the other sides }
  223. function hypot(x,y : float) : float;
  224. { logarithm functions }
  225. function log10(x : float) : float;
  226. function log2(x : float) : float;
  227. function logn(n,x : float) : float;
  228. { returns natural logarithm of x+1 }
  229. function lnxp1(x : float) : float;
  230. { exponential functions }
  231. function power(base,exponent : float) : float;
  232. { base^exponent }
  233. function intpower(base : float;const exponent : Integer) : float;
  234. operator ** (bas,expo : float) e: float;
  235. operator ** (bas,expo : int64) i: int64;
  236. { number converting }
  237. { rounds x towards positive infinity }
  238. function ceil(x : float) : Integer;
  239. { rounds x towards negative infinity }
  240. function floor(x : float) : Integer;
  241. { misc. functions }
  242. { splits x into mantissa and exponent (to base 2) }
  243. procedure Frexp(X: float; var Mantissa: float; var Exponent: integer);
  244. { returns x*(2^p) }
  245. function ldexp(x : float; const p : Integer) : float;
  246. { statistical functions }
  247. function mean(const data : array of float) : float;
  248. function sum(const data : array of float) : float;
  249. function mean(const data : PFloat; Const N : longint) : float;
  250. function sum(const data : PFloat; Const N : Longint) : float;
  251. function sumofsquares(const data : array of float) : float;
  252. function sumofsquares(const data : PFloat; Const N : Integer) : float;
  253. { calculates the sum and the sum of squares of data }
  254. procedure sumsandsquares(const data : array of float;
  255. var sum,sumofsquares : float);
  256. procedure sumsandsquares(const data : PFloat; Const N : Integer;
  257. var sum,sumofsquares : float);
  258. function minvalue(const data : array of float) : float;
  259. function minvalue(const data : array of integer) : Integer;
  260. function minvalue(const data : PFloat; Const N : Integer) : float;
  261. function MinValue(const Data : PInteger; Const N : Integer): Integer;
  262. function maxvalue(const data : array of float) : float;
  263. function maxvalue(const data : array of integer) : Integer;
  264. function maxvalue(const data : PFloat; Const N : Integer) : float;
  265. function maxvalue(const data : PInteger; Const N : Integer) : Integer;
  266. { calculates the standard deviation }
  267. function stddev(const data : array of float) : float;
  268. function stddev(const data : PFloat; Const N : Integer) : float;
  269. { calculates the mean and stddev }
  270. procedure meanandstddev(const data : array of float;
  271. var mean,stddev : float);
  272. procedure meanandstddev(const data : PFloat;
  273. Const N : Longint;var mean,stddev : float);
  274. function variance(const data : array of float) : float;
  275. function totalvariance(const data : array of float) : float;
  276. function variance(const data : PFloat; Const N : Integer) : float;
  277. function totalvariance(const data : PFloat; Const N : Integer) : float;
  278. { returns random values with gaussian distribution }
  279. function randg(mean,stddev : float) : float;
  280. { I don't know what the following functions do: }
  281. function popnstddev(const data : array of float) : float;
  282. function popnstddev(const data : PFloat; Const N : Integer) : float;
  283. function popnvariance(const data : PFloat; Const N : Integer) : float;
  284. function popnvariance(const data : array of float) : float;
  285. procedure momentskewkurtosis(const data : array of float;
  286. var m1,m2,m3,m4,skew,kurtosis : float);
  287. procedure momentskewkurtosis(const data : PFloat; Const N : Integer;
  288. var m1,m2,m3,m4,skew,kurtosis : float);
  289. { geometrical function }
  290. { returns the euclidean L2 norm }
  291. function norm(const data : array of float) : float;
  292. function norm(const data : PFloat; Const N : Integer) : float;
  293. function ifthen(val:boolean;const iftrue:integer; const iffalse:integer= 0) :integer; {$ifdef MATHINLINE}inline; {$endif}
  294. function ifthen(val:boolean;const iftrue:int64 ; const iffalse:int64 = 0) :int64; {$ifdef MATHINLINE}inline; {$endif}
  295. function ifthen(val:boolean;const iftrue:double ; const iffalse:double =0.0):double; {$ifdef MATHINLINE}inline; {$endif}
  296. { include cpu specific stuff }
  297. {$i mathuh.inc}
  298. implementation
  299. { include cpu specific stuff }
  300. {$i mathu.inc}
  301. ResourceString
  302. SMathError = 'Math Error : %s';
  303. SInvalidArgument = 'Invalid argument';
  304. Procedure DoMathError(Const S : String);
  305. begin
  306. Raise EMathError.CreateFmt(SMathError,[S]);
  307. end;
  308. Procedure InvalidArgument;
  309. begin
  310. Raise EInvalidArgument.Create(SInvalidArgument);
  311. end;
  312. function Sign(const AValue: Integer): TValueSign;inline;
  313. begin
  314. If Avalue<0 then
  315. Result:=NegativeValue
  316. else If Avalue>0 then
  317. Result:=PositiveValue
  318. else
  319. Result:=ZeroValue;
  320. end;
  321. function Sign(const AValue: Int64): TValueSign;inline;
  322. begin
  323. If Avalue<0 then
  324. Result:=NegativeValue
  325. else If Avalue>0 then
  326. Result:=PositiveValue
  327. else
  328. Result:=ZeroValue;
  329. end;
  330. function Sign(const AValue: Double): TValueSign;inline;
  331. begin
  332. If Avalue<0.0 then
  333. Result:=NegativeValue
  334. else If Avalue>0.0 then
  335. Result:=PositiveValue
  336. else
  337. Result:=ZeroValue;
  338. end;
  339. function degtorad(deg : float) : float;
  340. begin
  341. degtorad:=deg*(pi/180.0);
  342. end;
  343. function radtodeg(rad : float) : float;
  344. begin
  345. radtodeg:=rad*(180.0/pi);
  346. end;
  347. function gradtorad(grad : float) : float;
  348. begin
  349. gradtorad:=grad*(pi/200.0);
  350. end;
  351. function radtograd(rad : float) : float;
  352. begin
  353. radtograd:=rad*(200.0/pi);
  354. end;
  355. function degtograd(deg : float) : float;
  356. begin
  357. degtograd:=deg*(200.0/180.0);
  358. end;
  359. function gradtodeg(grad : float) : float;
  360. begin
  361. gradtodeg:=grad*(180.0/200.0);
  362. end;
  363. function cycletorad(cycle : float) : float;
  364. begin
  365. cycletorad:=(2*pi)*cycle;
  366. end;
  367. function radtocycle(rad : float) : float;
  368. begin
  369. { avoid division }
  370. radtocycle:=rad*(1/(2*pi));
  371. end;
  372. function tan(x : float) : float;
  373. begin
  374. Tan:=Sin(x)/Cos(x)
  375. end;
  376. function cotan(x : float) : float;
  377. begin
  378. cotan:=Cos(X)/Sin(X);
  379. end;
  380. procedure sincos(theta : float;var sinus,cosinus : float);
  381. begin
  382. sinus:=sin(theta);
  383. cosinus:=cos(theta);
  384. end;
  385. { ArcSin and ArcCos from Arjan van Dijk ([email protected]) }
  386. function arcsin(x : float) : float;
  387. begin
  388. if abs(x) > 1 then InvalidArgument
  389. else if abs(x) < 0.5 then
  390. arcsin := arctan(x/sqrt(1-sqr(x)))
  391. else
  392. arcsin := sign(x) * (pi*0.5 - arctan(sqrt(1 / sqr(x) - 1)));
  393. end;
  394. function Arccos(x : Float) : Float;
  395. begin
  396. arccos := pi*0.5 - arcsin(x);
  397. end;
  398. {$ifndef FPC_MATH_HAS_ARCTAN2}
  399. function arctan2(y,x : float) : float;
  400. begin
  401. if (x=0) then
  402. begin
  403. if y=0 then
  404. arctan2:=0.0
  405. else if y>0 then
  406. arctan2:=pi/2
  407. else if y<0 then
  408. arctan2:=-pi/2;
  409. end
  410. else
  411. ArcTan2:=ArcTan(y/x);
  412. if x<0.0 then
  413. ArcTan2:=ArcTan2+pi;
  414. if ArcTan2>pi then
  415. ArcTan2:=ArcTan2-2*pi;
  416. end;
  417. {$endif FPC_MATH_HAS_ARCTAN2}
  418. function cosh(x : float) : float;
  419. var
  420. temp : float;
  421. begin
  422. temp:=exp(x);
  423. cosh:=0.5*(temp+1.0/temp);
  424. end;
  425. function sinh(x : float) : float;
  426. var
  427. temp : float;
  428. begin
  429. temp:=exp(x);
  430. sinh:=0.5*(temp-1.0/temp);
  431. end;
  432. Const MaxTanh = 5678.22249441322; // Ln(MaxExtended)/2
  433. function tanh(x : float) : float;
  434. var Temp : float;
  435. begin
  436. if x>MaxTanh then exit(1.0)
  437. else if x<-MaxTanh then exit (-1.0);
  438. temp:=exp(-2*x);
  439. tanh:=(1-temp)/(1+temp)
  440. end;
  441. function arccosh(x : float) : float;
  442. begin
  443. arccosh:=arcosh(x);
  444. end;
  445. function arcsinh(x : float) : float;
  446. begin
  447. arcsinh:=arsinh(x);
  448. end;
  449. function arctanh(x : float) : float;
  450. begin
  451. if x>1 then InvalidArgument;
  452. arctanh:=artanh(x);
  453. end;
  454. function arcosh(x : float) : float;
  455. begin
  456. if x<1 then InvalidArgument;
  457. arcosh:=Ln(x+Sqrt(x*x-1));
  458. end;
  459. function arsinh(x : float) : float;
  460. begin
  461. arsinh:=Ln(x+Sqrt(1+x*x));
  462. end;
  463. function artanh(x : float) : float;
  464. begin
  465. If abs(x)>1 then InvalidArgument;
  466. artanh:=(Ln((1+x)/(1-x)))*0.5;
  467. end;
  468. function hypot(x,y : float) : float;
  469. begin
  470. hypot:=Sqrt(x*x+y*y)
  471. end;
  472. function log10(x : float) : float;
  473. begin
  474. log10:=ln(x)/ln(10);
  475. end;
  476. function log2(x : float) : float;
  477. begin
  478. log2:=ln(x)/ln(2)
  479. end;
  480. function logn(n,x : float) : float;
  481. begin
  482. if n<0 then InvalidArgument;
  483. logn:=ln(x)/ln(n);
  484. end;
  485. function lnxp1(x : float) : float;
  486. begin
  487. if x<-1 then
  488. InvalidArgument;
  489. lnxp1:=ln(1+x);
  490. end;
  491. function power(base,exponent : float) : float;
  492. begin
  493. if Exponent=0.0 then
  494. if base <> 0.0 then
  495. result:=1.0
  496. else
  497. InvalidArgument
  498. else if (base=0.0) and (exponent>0.0) then
  499. result:=0.0
  500. else if (abs(exponent)<=maxint) and (frac(exponent)=0.0) then
  501. result:=intpower(base,trunc(exponent))
  502. else if base>0.0 then
  503. result:=exp(exponent * ln (base))
  504. else
  505. InvalidArgument;
  506. end;
  507. function intpower(base : float;const exponent : Integer) : float;
  508. var
  509. i : longint;
  510. begin
  511. if (base = 0.0) and (exponent = 0) then
  512. InvalidArgument;
  513. i:=abs(exponent);
  514. intpower:=1.0;
  515. while i>0 do
  516. begin
  517. while (i and 1)=0 do
  518. begin
  519. i:=i shr 1;
  520. base:=sqr(base);
  521. end;
  522. i:=i-1;
  523. intpower:=intpower*base;
  524. end;
  525. if exponent<0 then
  526. intpower:=1.0/intpower;
  527. end;
  528. operator ** (bas,expo : float) e: float;
  529. begin
  530. e:=power(bas,expo);
  531. end;
  532. operator ** (bas,expo : int64) i: int64;
  533. begin
  534. i:=round(intpower(bas,expo));
  535. end;
  536. function ceil(x : float) : integer;
  537. begin
  538. Ceil:=Trunc(x);
  539. If Frac(x)>0 then
  540. Ceil:=Ceil+1;
  541. end;
  542. function floor(x : float) : integer;
  543. begin
  544. Floor:=Trunc(x);
  545. If Frac(x)<0 then
  546. Floor := Floor-1;
  547. end;
  548. procedure Frexp(X: float; var Mantissa: float; var Exponent: integer);
  549. begin
  550. Exponent :=0;
  551. if (abs(x)<0.5) then
  552. While (abs(x)<0.5) do
  553. begin
  554. x := x*2;
  555. Dec(Exponent);
  556. end
  557. else
  558. While (abs(x)>1) do
  559. begin
  560. x := x/2;
  561. Inc(Exponent);
  562. end;
  563. mantissa := x;
  564. end;
  565. function ldexp(x : float;const p : Integer) : float;
  566. begin
  567. ldexp:=x*intpower(2.0,p);
  568. end;
  569. function mean(const data : array of float) : float;
  570. begin
  571. Result:=Mean(@data[0],High(Data)+1);
  572. end;
  573. function mean(const data : PFloat; Const N : longint) : float;
  574. begin
  575. mean:=sum(Data,N);
  576. mean:=mean/N;
  577. end;
  578. function sum(const data : array of float) : float;
  579. begin
  580. Result:=Sum(@Data[0],High(Data)+1);
  581. end;
  582. function sum(const data : PFloat;Const N : longint) : float;
  583. var
  584. i : longint;
  585. begin
  586. sum:=0.0;
  587. for i:=0 to N-1 do
  588. sum:=sum+data[i];
  589. end;
  590. function sumofsquares(const data : array of float) : float;
  591. begin
  592. Result:=sumofsquares(@data[0],High(Data)+1);
  593. end;
  594. function sumofsquares(const data : PFloat; Const N : Integer) : float;
  595. var
  596. i : longint;
  597. begin
  598. sumofsquares:=0.0;
  599. for i:=0 to N-1 do
  600. sumofsquares:=sumofsquares+sqr(data[i]);
  601. end;
  602. procedure sumsandsquares(const data : array of float;
  603. var sum,sumofsquares : float);
  604. begin
  605. sumsandsquares (@Data[0],High(Data)+1,Sum,sumofsquares);
  606. end;
  607. procedure sumsandsquares(const data : PFloat; Const N : Integer;
  608. var sum,sumofsquares : float);
  609. var
  610. i : Integer;
  611. temp : float;
  612. begin
  613. sumofsquares:=0.0;
  614. sum:=0.0;
  615. for i:=0 to N-1 do
  616. begin
  617. temp:=data[i];
  618. sumofsquares:=sumofsquares+sqr(temp);
  619. sum:=sum+temp;
  620. end;
  621. end;
  622. function stddev(const data : array of float) : float;
  623. begin
  624. Result:=Stddev(@Data[0],High(Data)+1)
  625. end;
  626. function stddev(const data : PFloat; Const N : Integer) : float;
  627. begin
  628. StdDev:=Sqrt(Variance(Data,N));
  629. end;
  630. procedure meanandstddev(const data : array of float;
  631. var mean,stddev : float);
  632. begin
  633. Meanandstddev(@Data[0],High(Data)+1,Mean,stddev);
  634. end;
  635. procedure meanandstddev(const data : PFloat;
  636. Const N : Longint;var mean,stddev : float);
  637. Var I : longint;
  638. begin
  639. Mean:=0;
  640. StdDev:=0;
  641. For I:=0 to N-1 do
  642. begin
  643. Mean:=Mean+Data[i];
  644. StdDev:=StdDev+Sqr(Data[i]);
  645. end;
  646. Mean:=Mean/N;
  647. StdDev:=(StdDev-N*Sqr(Mean));
  648. If N>1 then
  649. StdDev:=Sqrt(Stddev/(N-1))
  650. else
  651. StdDev:=0;
  652. end;
  653. function variance(const data : array of float) : float;
  654. begin
  655. Variance:=Variance(@Data[0],High(Data)+1);
  656. end;
  657. function variance(const data : PFloat; Const N : Integer) : float;
  658. begin
  659. If N=1 then
  660. Result:=0
  661. else
  662. Result:=TotalVariance(Data,N)/(N-1);
  663. end;
  664. function totalvariance(const data : array of float) : float;
  665. begin
  666. Result:=TotalVariance(@Data[0],High(Data)+1);
  667. end;
  668. function totalvariance(const data : Pfloat;Const N : Integer) : float;
  669. var S,SS : Float;
  670. begin
  671. If N=1 then
  672. Result:=0
  673. else
  674. begin
  675. SumsAndSquares(Data,N,S,SS);
  676. Result := SS-Sqr(S)/N;
  677. end;
  678. end;
  679. function randg(mean,stddev : float) : float;
  680. Var U1,S2 : Float;
  681. begin
  682. repeat
  683. u1:= 2*random-1;
  684. S2:=Sqr(U1)+sqr(2*random-1);
  685. until s2<1;
  686. randg:=Sqrt(-2*ln(S2)/S2)*u1*stddev+Mean;
  687. end;
  688. function popnstddev(const data : array of float) : float;
  689. begin
  690. PopnStdDev:=Sqrt(PopnVariance(@Data[0],High(Data)+1));
  691. end;
  692. function popnstddev(const data : PFloat; Const N : Integer) : float;
  693. begin
  694. PopnStdDev:=Sqrt(PopnVariance(Data,N));
  695. end;
  696. function popnvariance(const data : array of float) : float;
  697. begin
  698. popnvariance:=popnvariance(@data[0],high(Data)+1);
  699. end;
  700. function popnvariance(const data : PFloat; Const N : Integer) : float;
  701. begin
  702. PopnVariance:=TotalVariance(Data,N)/N;
  703. end;
  704. procedure momentskewkurtosis(const data : array of float;
  705. var m1,m2,m3,m4,skew,kurtosis : float);
  706. begin
  707. momentskewkurtosis(@Data[0],High(Data)+1,m1,m2,m3,m4,skew,kurtosis);
  708. end;
  709. procedure momentskewkurtosis(const data : PFloat; Const N : Integer;
  710. var m1,m2,m3,m4,skew,kurtosis : float);
  711. Var S,SS,SC,SQ,invN,Acc,M1S,S2N,S3N,temp : Float;
  712. I : Longint;
  713. begin
  714. invN:=1.0/N;
  715. s:=0;
  716. ss:=0;
  717. sq:=0;
  718. sc:=0;
  719. for i:=0 to N-1 do
  720. begin
  721. temp:=Data[i]; { faster }
  722. S:=S+temp;
  723. acc:=temp*temp;
  724. ss:=ss+acc;
  725. Acc:=acc*temp;
  726. Sc:=sc+acc;
  727. acc:=acc*temp;
  728. sq:=sq+acc;
  729. end;
  730. M1:=s*invN;
  731. M1S:=M1*M1;
  732. S2N:=SS*invN;
  733. S3N:=SC*invN;
  734. M2:=S2N-M1S;
  735. M3:=S3N-(M1*3*S2N) + 2*M1S*M1;
  736. M4:=SQ*invN - (M1 * 4 * S3N) + (M1S*6*S2N-3*Sqr(M1S));
  737. Skew:=M3*power(M2,-3/2);
  738. Kurtosis:=M4 / Sqr(M2);
  739. end;
  740. function norm(const data : array of float) : float;
  741. begin
  742. norm:=Norm(@data[0],High(Data)+1);
  743. end;
  744. function norm(const data : PFloat; Const N : Integer) : float;
  745. begin
  746. norm:=sqrt(sumofsquares(data,N));
  747. end;
  748. function MinIntValue(const Data: array of Integer): Integer;
  749. var
  750. I: Integer;
  751. begin
  752. Result := Data[Low(Data)];
  753. For I := Succ(Low(Data)) To High(Data) Do
  754. If Data[I] < Result Then Result := Data[I];
  755. end;
  756. function MinValue(const Data: array of Integer): Integer;
  757. begin
  758. Result:=MinValue(Pinteger(@Data[0]),High(Data)+1)
  759. end;
  760. function MinValue(const Data: PInteger; Const N : Integer): Integer;
  761. var
  762. I: Integer;
  763. begin
  764. Result := Data[0];
  765. For I := 1 To N-1 do
  766. If Data[I] < Result Then Result := Data[I];
  767. end;
  768. function minvalue(const data : array of float) : float;
  769. begin
  770. Result:=minvalue(PFloat(@data[0]),High(Data)+1);
  771. end;
  772. function minvalue(const data : PFloat; Const N : Integer) : float;
  773. var
  774. i : longint;
  775. begin
  776. { get an initial value }
  777. minvalue:=data[0];
  778. for i:=1 to N-1 do
  779. if data[i]<minvalue then
  780. minvalue:=data[i];
  781. end;
  782. function MaxIntValue(const Data: array of Integer): Integer;
  783. var
  784. I: Integer;
  785. begin
  786. Result := Data[Low(Data)];
  787. For I := Succ(Low(Data)) To High(Data) Do
  788. If Data[I] > Result Then Result := Data[I];
  789. end;
  790. function maxvalue(const data : array of float) : float;
  791. begin
  792. Result:=maxvalue(PFloat(@data[0]),High(Data)+1);
  793. end;
  794. function maxvalue(const data : PFloat; Const N : Integer) : float;
  795. var
  796. i : longint;
  797. begin
  798. { get an initial value }
  799. maxvalue:=data[0];
  800. for i:=1 to N-1 do
  801. if data[i]>maxvalue then
  802. maxvalue:=data[i];
  803. end;
  804. function MaxValue(const Data: array of Integer): Integer;
  805. begin
  806. Result:=MaxValue(PInteger(@Data[0]),High(Data)+1)
  807. end;
  808. function maxvalue(const data : PInteger; Const N : Integer) : Integer;
  809. var
  810. i : longint;
  811. begin
  812. { get an initial value }
  813. maxvalue:=data[0];
  814. for i:=1 to N-1 do
  815. if data[i]>maxvalue then
  816. maxvalue:=data[i];
  817. end;
  818. function Min(a, b: Integer): Integer;inline;
  819. begin
  820. if a < b then
  821. Result := a
  822. else
  823. Result := b;
  824. end;
  825. function Max(a, b: Integer): Integer;inline;
  826. begin
  827. if a > b then
  828. Result := a
  829. else
  830. Result := b;
  831. end;
  832. {
  833. function Min(a, b: Cardinal): Cardinal;inline;
  834. begin
  835. if a < b then
  836. Result := a
  837. else
  838. Result := b;
  839. end;
  840. function Max(a, b: Cardinal): Cardinal;inline;
  841. begin
  842. if a > b then
  843. Result := a
  844. else
  845. Result := b;
  846. end;
  847. }
  848. function Min(a, b: Int64): Int64;inline;
  849. begin
  850. if a < b then
  851. Result := a
  852. else
  853. Result := b;
  854. end;
  855. function Max(a, b: Int64): Int64;inline;
  856. begin
  857. if a > b then
  858. Result := a
  859. else
  860. Result := b;
  861. end;
  862. {$ifdef FPC_HAS_TYPE_SINGLE}
  863. function Min(a, b: Single): Single;inline;
  864. begin
  865. if a < b then
  866. Result := a
  867. else
  868. Result := b;
  869. end;
  870. function Max(a, b: Single): Single;inline;
  871. begin
  872. if a > b then
  873. Result := a
  874. else
  875. Result := b;
  876. end;
  877. {$endif FPC_HAS_TYPE_SINGLE}
  878. {$ifdef FPC_HAS_TYPE_DOUBLE}
  879. function Min(a, b: Double): Double;inline;
  880. begin
  881. if a < b then
  882. Result := a
  883. else
  884. Result := b;
  885. end;
  886. function Max(a, b: Double): Double;inline;
  887. begin
  888. if a > b then
  889. Result := a
  890. else
  891. Result := b;
  892. end;
  893. {$endif FPC_HAS_TYPE_DOUBLE}
  894. {$ifdef FPC_HAS_TYPE_EXTENDED}
  895. function Min(a, b: Extended): Extended;inline;
  896. begin
  897. if a < b then
  898. Result := a
  899. else
  900. Result := b;
  901. end;
  902. function Max(a, b: Extended): Extended;inline;
  903. begin
  904. if a > b then
  905. Result := a
  906. else
  907. Result := b;
  908. end;
  909. {$endif FPC_HAS_TYPE_EXTENDED}
  910. function InRange(const AValue, AMin, AMax: Integer): Boolean;inline;
  911. begin
  912. Result:=(AValue>=AMin) and (AValue<=AMax);
  913. end;
  914. function InRange(const AValue, AMin, AMax: Int64): Boolean;inline;
  915. begin
  916. Result:=(AValue>=AMin) and (AValue<=AMax);
  917. end;
  918. {$ifdef FPC_HAS_TYPE_DOUBLE}
  919. function InRange(const AValue, AMin, AMax: Double): Boolean;inline;
  920. begin
  921. Result:=(AValue>=AMin) and (AValue<=AMax);
  922. end;
  923. {$endif FPC_HAS_TYPE_DOUBLE}
  924. function EnsureRange(const AValue, AMin, AMax: Integer): Integer;inline;
  925. begin
  926. Result:=AValue;
  927. If Result<AMin then
  928. Result:=AMin
  929. else if Result>AMax then
  930. Result:=AMax;
  931. end;
  932. function EnsureRange(const AValue, AMin, AMax: Int64): Int64;inline;
  933. begin
  934. Result:=AValue;
  935. If Result<AMin then
  936. Result:=AMin
  937. else if Result>AMax then
  938. Result:=AMax;
  939. end;
  940. {$ifdef FPC_HAS_TYPE_DOUBLE}
  941. function EnsureRange(const AValue, AMin, AMax: Double): Double;inline;
  942. begin
  943. Result:=AValue;
  944. If Result<AMin then
  945. Result:=AMin
  946. else if Result>AMax then
  947. Result:=AMax;
  948. end;
  949. {$endif FPC_HAS_TYPE_DOUBLE}
  950. Const
  951. EZeroResolution = 1E-16;
  952. DZeroResolution = 1E-12;
  953. SZeroResolution = 1E-4;
  954. function IsZero(const A: Single; Epsilon: Single): Boolean;
  955. begin
  956. if (Epsilon=0) then
  957. Epsilon:=SZeroResolution;
  958. Result:=Abs(A)<=Epsilon;
  959. end;
  960. function IsZero(const A: Single): Boolean;inline;
  961. begin
  962. Result:=IsZero(A,single(SZeroResolution));
  963. end;
  964. {$ifdef FPC_HAS_TYPE_DOUBLE}
  965. function IsZero(const A: Double; Epsilon: Double): Boolean;
  966. begin
  967. if (Epsilon=0) then
  968. Epsilon:=DZeroResolution;
  969. Result:=Abs(A)<=Epsilon;
  970. end;
  971. function IsZero(const A: Double): Boolean;inline;
  972. begin
  973. Result:=IsZero(A,DZeroResolution);
  974. end;
  975. {$endif FPC_HAS_TYPE_DOUBLE}
  976. {$ifdef FPC_HAS_TYPE_EXTENDED}
  977. function IsZero(const A: Extended; Epsilon: Extended): Boolean;
  978. begin
  979. if (Epsilon=0) then
  980. Epsilon:=EZeroResolution;
  981. Result:=Abs(A)<=Epsilon;
  982. end;
  983. function IsZero(const A: Extended): Boolean;inline;
  984. begin
  985. Result:=IsZero(A,EZeroResolution);
  986. end;
  987. {$endif FPC_HAS_TYPE_EXTENDED}
  988. type
  989. TSplitDouble = packed record
  990. cards: Array[0..1] of cardinal;
  991. end;
  992. function IsNan(const d : Double): Boolean;
  993. var
  994. fraczero, expMaximal: boolean;
  995. begin
  996. {$if defined(FPC_BIG_ENDIAN) or (defined(CPUARM) and defined(FPUFPA))}
  997. expMaximal := ((TSplitDouble(d).cards[0] shr 20) and $7ff) = 2047;
  998. fraczero:= (TSplitDouble(d).cards[0] and $fffff = 0) and
  999. (TSplitDouble(d).cards[1] = 0);
  1000. {$else FPC_BIG_ENDIAN}
  1001. expMaximal := ((TSplitDouble(d).cards[1] shr 20) and $7ff) = 2047;
  1002. fraczero := (TSplitDouble(d).cards[1] and $fffff = 0) and
  1003. (TSplitDouble(d).cards[0] = 0);
  1004. {$endif FPC_BIG_ENDIAN}
  1005. Result:=expMaximal and not(fraczero);
  1006. end;
  1007. function IsInfinite(const d : Double): Boolean;
  1008. var
  1009. fraczero, expMaximal: boolean;
  1010. begin
  1011. {$if defined(FPC_BIG_ENDIAN) or (defined(CPUARM) and defined(FPUFPA))}
  1012. expMaximal := ((TSplitDouble(d).cards[0] shr 20) and $7ff) = 2047;
  1013. fraczero:= (TSplitDouble(d).cards[0] and $fffff = 0) and
  1014. (TSplitDouble(d).cards[1] = 0);
  1015. {$else FPC_BIG_ENDIAN}
  1016. expMaximal := ((TSplitDouble(d).cards[1] shr 20) and $7ff) = 2047;
  1017. fraczero := (TSplitDouble(d).cards[1] and $fffff = 0) and
  1018. (TSplitDouble(d).cards[0] = 0);
  1019. {$endif FPC_BIG_ENDIAN}
  1020. Result:=expMaximal and fraczero;
  1021. end;
  1022. {$ifdef FPC_HAS_TYPE_EXTENDED}
  1023. function SameValue(const A, B: Extended; Epsilon: Extended): Boolean;
  1024. begin
  1025. if (Epsilon=0) then
  1026. Epsilon:=Max(Min(Abs(A),Abs(B))*EZeroResolution,EZeroResolution);
  1027. if (A>B) then
  1028. Result:=((A-B)<=Epsilon)
  1029. else
  1030. Result:=((B-A)<=Epsilon);
  1031. end;
  1032. function SameValue(const A, B: Extended): Boolean;inline;
  1033. begin
  1034. Result:=SameValue(A,B,0);
  1035. end;
  1036. {$endif FPC_HAS_TYPE_EXTENDED}
  1037. {$ifdef FPC_HAS_TYPE_DOUBLE}
  1038. function SameValue(const A, B: Double): Boolean;inline;
  1039. begin
  1040. Result:=SameValue(A,B,0);
  1041. end;
  1042. function SameValue(const A, B: Double; Epsilon: Double): Boolean;
  1043. begin
  1044. if (Epsilon=0) then
  1045. Epsilon:=Max(Min(Abs(A),Abs(B))*DZeroResolution,DZeroResolution);
  1046. if (A>B) then
  1047. Result:=((A-B)<=Epsilon)
  1048. else
  1049. Result:=((B-A)<=Epsilon);
  1050. end;
  1051. {$endif FPC_HAS_TYPE_DOUBLE}
  1052. function SameValue(const A, B: Single): Boolean;inline;
  1053. begin
  1054. Result:=SameValue(A,B,0);
  1055. end;
  1056. function SameValue(const A, B: Single; Epsilon: Single): Boolean;
  1057. begin
  1058. if (Epsilon=0) then
  1059. Epsilon:=Max(Min(Abs(A),Abs(B))*SZeroResolution,SZeroResolution);
  1060. if (A>B) then
  1061. Result:=((A-B)<=Epsilon)
  1062. else
  1063. Result:=((B-A)<=Epsilon);
  1064. end;
  1065. // Some CPUs probably allow a faster way of doing this in a single operation...
  1066. // There weshould define CPUDIVMOD in the header mathuh.inc and implement it using asm.
  1067. {$ifndef CPUDIVMOD}
  1068. procedure DivMod(Dividend: Integer; Divisor: Word; var Result, Remainder: Word);
  1069. begin
  1070. Result:=Dividend Div Divisor;
  1071. Remainder:=Dividend Mod Divisor;
  1072. end;
  1073. {$endif}
  1074. function ifthen(val:boolean;const iftrue:integer; const iffalse:integer= 0) :integer;
  1075. begin
  1076. if val then result:=iftrue else result:=iffalse;
  1077. end;
  1078. function ifthen(val:boolean;const iftrue:int64 ; const iffalse:int64 = 0) :int64;
  1079. begin
  1080. if val then result:=iftrue else result:=iffalse;
  1081. end;
  1082. function ifthen(val:boolean;const iftrue:double ; const iffalse:double =0.0):double;
  1083. begin
  1084. if val then result:=iftrue else result:=iffalse;
  1085. end;
  1086. end.