math.pp 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404
  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 sumInt(const data : PInt64;Const N : longint) : Int64;
  252. function sumInt(const data : array of Int64) : Int64;
  253. function sumofsquares(const data : array of float) : float;
  254. function sumofsquares(const data : PFloat; Const N : Integer) : float;
  255. { calculates the sum and the sum of squares of data }
  256. procedure sumsandsquares(const data : array of float;
  257. var sum,sumofsquares : float);
  258. procedure sumsandsquares(const data : PFloat; Const N : Integer;
  259. var sum,sumofsquares : float);
  260. function minvalue(const data : array of float) : float;
  261. function minvalue(const data : array of integer) : Integer;
  262. function minvalue(const data : PFloat; Const N : Integer) : float;
  263. function MinValue(const Data : PInteger; Const N : Integer): Integer;
  264. function maxvalue(const data : array of float) : float;
  265. function maxvalue(const data : array of integer) : Integer;
  266. function maxvalue(const data : PFloat; Const N : Integer) : float;
  267. function maxvalue(const data : PInteger; Const N : Integer) : Integer;
  268. { calculates the standard deviation }
  269. function stddev(const data : array of float) : float;
  270. function stddev(const data : PFloat; Const N : Integer) : float;
  271. { calculates the mean and stddev }
  272. procedure meanandstddev(const data : array of float;
  273. var mean,stddev : float);
  274. procedure meanandstddev(const data : PFloat;
  275. Const N : Longint;var mean,stddev : float);
  276. function variance(const data : array of float) : float;
  277. function totalvariance(const data : array of float) : float;
  278. function variance(const data : PFloat; Const N : Integer) : float;
  279. function totalvariance(const data : PFloat; Const N : Integer) : float;
  280. { returns random values with gaussian distribution }
  281. function randg(mean,stddev : float) : float;
  282. { I don't know what the following functions do: }
  283. function popnstddev(const data : array of float) : float;
  284. function popnstddev(const data : PFloat; Const N : Integer) : float;
  285. function popnvariance(const data : PFloat; Const N : Integer) : float;
  286. function popnvariance(const data : array of float) : float;
  287. procedure momentskewkurtosis(const data : array of float;
  288. var m1,m2,m3,m4,skew,kurtosis : float);
  289. procedure momentskewkurtosis(const data : PFloat; Const N : Integer;
  290. var m1,m2,m3,m4,skew,kurtosis : float);
  291. { geometrical function }
  292. { returns the euclidean L2 norm }
  293. function norm(const data : array of float) : float;
  294. function norm(const data : PFloat; Const N : Integer) : float;
  295. function ifthen(val:boolean;const iftrue:integer; const iffalse:integer= 0) :integer; {$ifdef MATHINLINE}inline; {$endif}
  296. function ifthen(val:boolean;const iftrue:int64 ; const iffalse:int64 = 0) :int64; {$ifdef MATHINLINE}inline; {$endif}
  297. function ifthen(val:boolean;const iftrue:double ; const iffalse:double =0.0):double; {$ifdef MATHINLINE}inline; {$endif}
  298. { include cpu specific stuff }
  299. {$i mathuh.inc}
  300. implementation
  301. { include cpu specific stuff }
  302. {$i mathu.inc}
  303. ResourceString
  304. SMathError = 'Math Error : %s';
  305. SInvalidArgument = 'Invalid argument';
  306. Procedure DoMathError(Const S : String);
  307. begin
  308. Raise EMathError.CreateFmt(SMathError,[S]);
  309. end;
  310. Procedure InvalidArgument;
  311. begin
  312. Raise EInvalidArgument.Create(SInvalidArgument);
  313. end;
  314. function Sign(const AValue: Integer): TValueSign;inline;
  315. begin
  316. If Avalue<0 then
  317. Result:=NegativeValue
  318. else If Avalue>0 then
  319. Result:=PositiveValue
  320. else
  321. Result:=ZeroValue;
  322. end;
  323. function Sign(const AValue: Int64): TValueSign;inline;
  324. begin
  325. If Avalue<0 then
  326. Result:=NegativeValue
  327. else If Avalue>0 then
  328. Result:=PositiveValue
  329. else
  330. Result:=ZeroValue;
  331. end;
  332. function Sign(const AValue: Double): TValueSign;inline;
  333. begin
  334. If Avalue<0.0 then
  335. Result:=NegativeValue
  336. else If Avalue>0.0 then
  337. Result:=PositiveValue
  338. else
  339. Result:=ZeroValue;
  340. end;
  341. function degtorad(deg : float) : float;
  342. begin
  343. degtorad:=deg*(pi/180.0);
  344. end;
  345. function radtodeg(rad : float) : float;
  346. begin
  347. radtodeg:=rad*(180.0/pi);
  348. end;
  349. function gradtorad(grad : float) : float;
  350. begin
  351. gradtorad:=grad*(pi/200.0);
  352. end;
  353. function radtograd(rad : float) : float;
  354. begin
  355. radtograd:=rad*(200.0/pi);
  356. end;
  357. function degtograd(deg : float) : float;
  358. begin
  359. degtograd:=deg*(200.0/180.0);
  360. end;
  361. function gradtodeg(grad : float) : float;
  362. begin
  363. gradtodeg:=grad*(180.0/200.0);
  364. end;
  365. function cycletorad(cycle : float) : float;
  366. begin
  367. cycletorad:=(2*pi)*cycle;
  368. end;
  369. function radtocycle(rad : float) : float;
  370. begin
  371. { avoid division }
  372. radtocycle:=rad*(1/(2*pi));
  373. end;
  374. function tan(x : float) : float;
  375. begin
  376. Tan:=Sin(x)/Cos(x)
  377. end;
  378. function cotan(x : float) : float;
  379. begin
  380. cotan:=Cos(X)/Sin(X);
  381. end;
  382. procedure sincos(theta : float;var sinus,cosinus : float);
  383. begin
  384. sinus:=sin(theta);
  385. cosinus:=cos(theta);
  386. end;
  387. { ArcSin and ArcCos from Arjan van Dijk ([email protected]) }
  388. function arcsin(x : float) : float;
  389. begin
  390. if abs(x) > 1 then InvalidArgument
  391. else if abs(x) < 0.5 then
  392. arcsin := arctan(x/sqrt(1-sqr(x)))
  393. else
  394. arcsin := sign(x) * (pi*0.5 - arctan(sqrt(1 / sqr(x) - 1)));
  395. end;
  396. function Arccos(x : Float) : Float;
  397. begin
  398. arccos := pi*0.5 - arcsin(x);
  399. end;
  400. {$ifndef FPC_MATH_HAS_ARCTAN2}
  401. function arctan2(y,x : float) : float;
  402. begin
  403. if (x=0) then
  404. begin
  405. if y=0 then
  406. arctan2:=0.0
  407. else if y>0 then
  408. arctan2:=pi/2
  409. else if y<0 then
  410. arctan2:=-pi/2;
  411. end
  412. else
  413. ArcTan2:=ArcTan(y/x);
  414. if x<0.0 then
  415. ArcTan2:=ArcTan2+pi;
  416. if ArcTan2>pi then
  417. ArcTan2:=ArcTan2-2*pi;
  418. end;
  419. {$endif FPC_MATH_HAS_ARCTAN2}
  420. function cosh(x : float) : float;
  421. var
  422. temp : float;
  423. begin
  424. temp:=exp(x);
  425. cosh:=0.5*(temp+1.0/temp);
  426. end;
  427. function sinh(x : float) : float;
  428. var
  429. temp : float;
  430. begin
  431. temp:=exp(x);
  432. sinh:=0.5*(temp-1.0/temp);
  433. end;
  434. Const MaxTanh = 5678.22249441322; // Ln(MaxExtended)/2
  435. function tanh(x : float) : float;
  436. var Temp : float;
  437. begin
  438. if x>MaxTanh then exit(1.0)
  439. else if x<-MaxTanh then exit (-1.0);
  440. temp:=exp(-2*x);
  441. tanh:=(1-temp)/(1+temp)
  442. end;
  443. function arccosh(x : float) : float;
  444. begin
  445. arccosh:=arcosh(x);
  446. end;
  447. function arcsinh(x : float) : float;
  448. begin
  449. arcsinh:=arsinh(x);
  450. end;
  451. function arctanh(x : float) : float;
  452. begin
  453. if x>1 then InvalidArgument;
  454. arctanh:=artanh(x);
  455. end;
  456. function arcosh(x : float) : float;
  457. begin
  458. if x<1 then InvalidArgument;
  459. arcosh:=Ln(x+Sqrt(x*x-1));
  460. end;
  461. function arsinh(x : float) : float;
  462. begin
  463. arsinh:=Ln(x+Sqrt(1+x*x));
  464. end;
  465. function artanh(x : float) : float;
  466. begin
  467. If abs(x)>1 then InvalidArgument;
  468. artanh:=(Ln((1+x)/(1-x)))*0.5;
  469. end;
  470. function hypot(x,y : float) : float;
  471. begin
  472. hypot:=Sqrt(x*x+y*y)
  473. end;
  474. function log10(x : float) : float;
  475. begin
  476. log10:=ln(x)/ln(10);
  477. end;
  478. function log2(x : float) : float;
  479. begin
  480. log2:=ln(x)/ln(2)
  481. end;
  482. function logn(n,x : float) : float;
  483. begin
  484. if n<0 then InvalidArgument;
  485. logn:=ln(x)/ln(n);
  486. end;
  487. function lnxp1(x : float) : float;
  488. begin
  489. if x<-1 then
  490. InvalidArgument;
  491. lnxp1:=ln(1+x);
  492. end;
  493. function power(base,exponent : float) : float;
  494. begin
  495. if Exponent=0.0 then
  496. if base <> 0.0 then
  497. result:=1.0
  498. else
  499. InvalidArgument
  500. else if (base=0.0) and (exponent>0.0) then
  501. result:=0.0
  502. else if (abs(exponent)<=maxint) and (frac(exponent)=0.0) then
  503. result:=intpower(base,trunc(exponent))
  504. else if base>0.0 then
  505. result:=exp(exponent * ln (base))
  506. else
  507. InvalidArgument;
  508. end;
  509. function intpower(base : float;const exponent : Integer) : float;
  510. var
  511. i : longint;
  512. begin
  513. if (base = 0.0) and (exponent = 0) then
  514. InvalidArgument;
  515. i:=abs(exponent);
  516. intpower:=1.0;
  517. while i>0 do
  518. begin
  519. while (i and 1)=0 do
  520. begin
  521. i:=i shr 1;
  522. base:=sqr(base);
  523. end;
  524. i:=i-1;
  525. intpower:=intpower*base;
  526. end;
  527. if exponent<0 then
  528. intpower:=1.0/intpower;
  529. end;
  530. operator ** (bas,expo : float) e: float;
  531. begin
  532. e:=power(bas,expo);
  533. end;
  534. operator ** (bas,expo : int64) i: int64;
  535. begin
  536. i:=round(intpower(bas,expo));
  537. end;
  538. function ceil(x : float) : integer;
  539. begin
  540. Ceil:=Trunc(x);
  541. If Frac(x)>0 then
  542. Ceil:=Ceil+1;
  543. end;
  544. function floor(x : float) : integer;
  545. begin
  546. Floor:=Trunc(x);
  547. If Frac(x)<0 then
  548. Floor := Floor-1;
  549. end;
  550. procedure Frexp(X: float; var Mantissa: float; var Exponent: integer);
  551. begin
  552. Exponent :=0;
  553. if (abs(x)<0.5) then
  554. While (abs(x)<0.5) do
  555. begin
  556. x := x*2;
  557. Dec(Exponent);
  558. end
  559. else
  560. While (abs(x)>1) do
  561. begin
  562. x := x/2;
  563. Inc(Exponent);
  564. end;
  565. mantissa := x;
  566. end;
  567. function ldexp(x : float;const p : Integer) : float;
  568. begin
  569. ldexp:=x*intpower(2.0,p);
  570. end;
  571. function mean(const data : array of float) : float;
  572. begin
  573. Result:=Mean(@data[0],High(Data)+1);
  574. end;
  575. function mean(const data : PFloat; Const N : longint) : float;
  576. begin
  577. mean:=sum(Data,N);
  578. mean:=mean/N;
  579. end;
  580. function sum(const data : array of float) : float;
  581. begin
  582. Result:=Sum(@Data[0],High(Data)+1);
  583. end;
  584. function sum(const data : PFloat;Const N : longint) : float;
  585. var
  586. i : longint;
  587. begin
  588. sum:=0.0;
  589. for i:=0 to N-1 do
  590. sum:=sum+data[i];
  591. end;
  592. function sumInt(const data : PInt64;Const N : longint) : Int64;
  593. var
  594. i : longint;
  595. begin
  596. sumInt:=0;
  597. for i:=0 to N-1 do
  598. sumInt:=sumInt+data[i];
  599. end;
  600. function sumInt(const data : array of Int64) : Int64;
  601. begin
  602. Result:=SumInt(@Data[0],High(Data)+1);
  603. end;
  604. function sumofsquares(const data : array of float) : float;
  605. begin
  606. Result:=sumofsquares(@data[0],High(Data)+1);
  607. end;
  608. function sumofsquares(const data : PFloat; Const N : Integer) : float;
  609. var
  610. i : longint;
  611. begin
  612. sumofsquares:=0.0;
  613. for i:=0 to N-1 do
  614. sumofsquares:=sumofsquares+sqr(data[i]);
  615. end;
  616. procedure sumsandsquares(const data : array of float;
  617. var sum,sumofsquares : float);
  618. begin
  619. sumsandsquares (@Data[0],High(Data)+1,Sum,sumofsquares);
  620. end;
  621. procedure sumsandsquares(const data : PFloat; Const N : Integer;
  622. var sum,sumofsquares : float);
  623. var
  624. i : Integer;
  625. temp : float;
  626. begin
  627. sumofsquares:=0.0;
  628. sum:=0.0;
  629. for i:=0 to N-1 do
  630. begin
  631. temp:=data[i];
  632. sumofsquares:=sumofsquares+sqr(temp);
  633. sum:=sum+temp;
  634. end;
  635. end;
  636. function stddev(const data : array of float) : float;
  637. begin
  638. Result:=Stddev(@Data[0],High(Data)+1)
  639. end;
  640. function stddev(const data : PFloat; Const N : Integer) : float;
  641. begin
  642. StdDev:=Sqrt(Variance(Data,N));
  643. end;
  644. procedure meanandstddev(const data : array of float;
  645. var mean,stddev : float);
  646. begin
  647. Meanandstddev(@Data[0],High(Data)+1,Mean,stddev);
  648. end;
  649. procedure meanandstddev(const data : PFloat;
  650. Const N : Longint;var mean,stddev : float);
  651. Var I : longint;
  652. begin
  653. Mean:=0;
  654. StdDev:=0;
  655. For I:=0 to N-1 do
  656. begin
  657. Mean:=Mean+Data[i];
  658. StdDev:=StdDev+Sqr(Data[i]);
  659. end;
  660. Mean:=Mean/N;
  661. StdDev:=(StdDev-N*Sqr(Mean));
  662. If N>1 then
  663. StdDev:=Sqrt(Stddev/(N-1))
  664. else
  665. StdDev:=0;
  666. end;
  667. function variance(const data : array of float) : float;
  668. begin
  669. Variance:=Variance(@Data[0],High(Data)+1);
  670. end;
  671. function variance(const data : PFloat; Const N : Integer) : float;
  672. begin
  673. If N=1 then
  674. Result:=0
  675. else
  676. Result:=TotalVariance(Data,N)/(N-1);
  677. end;
  678. function totalvariance(const data : array of float) : float;
  679. begin
  680. Result:=TotalVariance(@Data[0],High(Data)+1);
  681. end;
  682. function totalvariance(const data : Pfloat;Const N : Integer) : float;
  683. var S,SS : Float;
  684. begin
  685. If N=1 then
  686. Result:=0
  687. else
  688. begin
  689. SumsAndSquares(Data,N,S,SS);
  690. Result := SS-Sqr(S)/N;
  691. end;
  692. end;
  693. function randg(mean,stddev : float) : float;
  694. Var U1,S2 : Float;
  695. begin
  696. repeat
  697. u1:= 2*random-1;
  698. S2:=Sqr(U1)+sqr(2*random-1);
  699. until s2<1;
  700. randg:=Sqrt(-2*ln(S2)/S2)*u1*stddev+Mean;
  701. end;
  702. function popnstddev(const data : array of float) : float;
  703. begin
  704. PopnStdDev:=Sqrt(PopnVariance(@Data[0],High(Data)+1));
  705. end;
  706. function popnstddev(const data : PFloat; Const N : Integer) : float;
  707. begin
  708. PopnStdDev:=Sqrt(PopnVariance(Data,N));
  709. end;
  710. function popnvariance(const data : array of float) : float;
  711. begin
  712. popnvariance:=popnvariance(@data[0],high(Data)+1);
  713. end;
  714. function popnvariance(const data : PFloat; Const N : Integer) : float;
  715. begin
  716. PopnVariance:=TotalVariance(Data,N)/N;
  717. end;
  718. procedure momentskewkurtosis(const data : array of float;
  719. var m1,m2,m3,m4,skew,kurtosis : float);
  720. begin
  721. momentskewkurtosis(@Data[0],High(Data)+1,m1,m2,m3,m4,skew,kurtosis);
  722. end;
  723. procedure momentskewkurtosis(const data : PFloat; Const N : Integer;
  724. var m1,m2,m3,m4,skew,kurtosis : float);
  725. Var S,SS,SC,SQ,invN,Acc,M1S,S2N,S3N,temp : Float;
  726. I : Longint;
  727. begin
  728. invN:=1.0/N;
  729. s:=0;
  730. ss:=0;
  731. sq:=0;
  732. sc:=0;
  733. for i:=0 to N-1 do
  734. begin
  735. temp:=Data[i]; { faster }
  736. S:=S+temp;
  737. acc:=temp*temp;
  738. ss:=ss+acc;
  739. Acc:=acc*temp;
  740. Sc:=sc+acc;
  741. acc:=acc*temp;
  742. sq:=sq+acc;
  743. end;
  744. M1:=s*invN;
  745. M1S:=M1*M1;
  746. S2N:=SS*invN;
  747. S3N:=SC*invN;
  748. M2:=S2N-M1S;
  749. M3:=S3N-(M1*3*S2N) + 2*M1S*M1;
  750. M4:=SQ*invN - (M1 * 4 * S3N) + (M1S*6*S2N-3*Sqr(M1S));
  751. Skew:=M3*power(M2,-3/2);
  752. Kurtosis:=M4 / Sqr(M2);
  753. end;
  754. function norm(const data : array of float) : float;
  755. begin
  756. norm:=Norm(@data[0],High(Data)+1);
  757. end;
  758. function norm(const data : PFloat; Const N : Integer) : float;
  759. begin
  760. norm:=sqrt(sumofsquares(data,N));
  761. end;
  762. function MinIntValue(const Data: array of Integer): Integer;
  763. var
  764. I: Integer;
  765. begin
  766. Result := Data[Low(Data)];
  767. For I := Succ(Low(Data)) To High(Data) Do
  768. If Data[I] < Result Then Result := Data[I];
  769. end;
  770. function MinValue(const Data: array of Integer): Integer;
  771. begin
  772. Result:=MinValue(Pinteger(@Data[0]),High(Data)+1)
  773. end;
  774. function MinValue(const Data: PInteger; Const N : Integer): Integer;
  775. var
  776. I: Integer;
  777. begin
  778. Result := Data[0];
  779. For I := 1 To N-1 do
  780. If Data[I] < Result Then Result := Data[I];
  781. end;
  782. function minvalue(const data : array of float) : float;
  783. begin
  784. Result:=minvalue(PFloat(@data[0]),High(Data)+1);
  785. end;
  786. function minvalue(const data : PFloat; Const N : Integer) : float;
  787. var
  788. i : longint;
  789. begin
  790. { get an initial value }
  791. minvalue:=data[0];
  792. for i:=1 to N-1 do
  793. if data[i]<minvalue then
  794. minvalue:=data[i];
  795. end;
  796. function MaxIntValue(const Data: array of Integer): Integer;
  797. var
  798. I: Integer;
  799. begin
  800. Result := Data[Low(Data)];
  801. For I := Succ(Low(Data)) To High(Data) Do
  802. If Data[I] > Result Then Result := Data[I];
  803. end;
  804. function maxvalue(const data : array of float) : float;
  805. begin
  806. Result:=maxvalue(PFloat(@data[0]),High(Data)+1);
  807. end;
  808. function maxvalue(const data : PFloat; Const N : Integer) : float;
  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 MaxValue(const Data: array of Integer): Integer;
  819. begin
  820. Result:=MaxValue(PInteger(@Data[0]),High(Data)+1)
  821. end;
  822. function maxvalue(const data : PInteger; Const N : Integer) : Integer;
  823. var
  824. i : longint;
  825. begin
  826. { get an initial value }
  827. maxvalue:=data[0];
  828. for i:=1 to N-1 do
  829. if data[i]>maxvalue then
  830. maxvalue:=data[i];
  831. end;
  832. function Min(a, b: Integer): Integer;inline;
  833. begin
  834. if a < b then
  835. Result := a
  836. else
  837. Result := b;
  838. end;
  839. function Max(a, b: Integer): Integer;inline;
  840. begin
  841. if a > b then
  842. Result := a
  843. else
  844. Result := b;
  845. end;
  846. {
  847. function Min(a, b: Cardinal): Cardinal;inline;
  848. begin
  849. if a < b then
  850. Result := a
  851. else
  852. Result := b;
  853. end;
  854. function Max(a, b: Cardinal): Cardinal;inline;
  855. begin
  856. if a > b then
  857. Result := a
  858. else
  859. Result := b;
  860. end;
  861. }
  862. function Min(a, b: Int64): Int64;inline;
  863. begin
  864. if a < b then
  865. Result := a
  866. else
  867. Result := b;
  868. end;
  869. function Max(a, b: Int64): Int64;inline;
  870. begin
  871. if a > b then
  872. Result := a
  873. else
  874. Result := b;
  875. end;
  876. {$ifdef FPC_HAS_TYPE_SINGLE}
  877. function Min(a, b: Single): Single;inline;
  878. begin
  879. if a < b then
  880. Result := a
  881. else
  882. Result := b;
  883. end;
  884. function Max(a, b: Single): Single;inline;
  885. begin
  886. if a > b then
  887. Result := a
  888. else
  889. Result := b;
  890. end;
  891. {$endif FPC_HAS_TYPE_SINGLE}
  892. {$ifdef FPC_HAS_TYPE_DOUBLE}
  893. function Min(a, b: Double): Double;inline;
  894. begin
  895. if a < b then
  896. Result := a
  897. else
  898. Result := b;
  899. end;
  900. function Max(a, b: Double): Double;inline;
  901. begin
  902. if a > b then
  903. Result := a
  904. else
  905. Result := b;
  906. end;
  907. {$endif FPC_HAS_TYPE_DOUBLE}
  908. {$ifdef FPC_HAS_TYPE_EXTENDED}
  909. function Min(a, b: Extended): Extended;inline;
  910. begin
  911. if a < b then
  912. Result := a
  913. else
  914. Result := b;
  915. end;
  916. function Max(a, b: Extended): Extended;inline;
  917. begin
  918. if a > b then
  919. Result := a
  920. else
  921. Result := b;
  922. end;
  923. {$endif FPC_HAS_TYPE_EXTENDED}
  924. function InRange(const AValue, AMin, AMax: Integer): Boolean;inline;
  925. begin
  926. Result:=(AValue>=AMin) and (AValue<=AMax);
  927. end;
  928. function InRange(const AValue, AMin, AMax: Int64): Boolean;inline;
  929. begin
  930. Result:=(AValue>=AMin) and (AValue<=AMax);
  931. end;
  932. {$ifdef FPC_HAS_TYPE_DOUBLE}
  933. function InRange(const AValue, AMin, AMax: Double): Boolean;inline;
  934. begin
  935. Result:=(AValue>=AMin) and (AValue<=AMax);
  936. end;
  937. {$endif FPC_HAS_TYPE_DOUBLE}
  938. function EnsureRange(const AValue, AMin, AMax: Integer): Integer;inline;
  939. begin
  940. Result:=AValue;
  941. If Result<AMin then
  942. Result:=AMin
  943. else if Result>AMax then
  944. Result:=AMax;
  945. end;
  946. function EnsureRange(const AValue, AMin, AMax: Int64): Int64;inline;
  947. begin
  948. Result:=AValue;
  949. If Result<AMin then
  950. Result:=AMin
  951. else if Result>AMax then
  952. Result:=AMax;
  953. end;
  954. {$ifdef FPC_HAS_TYPE_DOUBLE}
  955. function EnsureRange(const AValue, AMin, AMax: Double): Double;inline;
  956. begin
  957. Result:=AValue;
  958. If Result<AMin then
  959. Result:=AMin
  960. else if Result>AMax then
  961. Result:=AMax;
  962. end;
  963. {$endif FPC_HAS_TYPE_DOUBLE}
  964. Const
  965. EZeroResolution = 1E-16;
  966. DZeroResolution = 1E-12;
  967. SZeroResolution = 1E-4;
  968. function IsZero(const A: Single; Epsilon: Single): Boolean;
  969. begin
  970. if (Epsilon=0) then
  971. Epsilon:=SZeroResolution;
  972. Result:=Abs(A)<=Epsilon;
  973. end;
  974. function IsZero(const A: Single): Boolean;inline;
  975. begin
  976. Result:=IsZero(A,single(SZeroResolution));
  977. end;
  978. {$ifdef FPC_HAS_TYPE_DOUBLE}
  979. function IsZero(const A: Double; Epsilon: Double): Boolean;
  980. begin
  981. if (Epsilon=0) then
  982. Epsilon:=DZeroResolution;
  983. Result:=Abs(A)<=Epsilon;
  984. end;
  985. function IsZero(const A: Double): Boolean;inline;
  986. begin
  987. Result:=IsZero(A,DZeroResolution);
  988. end;
  989. {$endif FPC_HAS_TYPE_DOUBLE}
  990. {$ifdef FPC_HAS_TYPE_EXTENDED}
  991. function IsZero(const A: Extended; Epsilon: Extended): Boolean;
  992. begin
  993. if (Epsilon=0) then
  994. Epsilon:=EZeroResolution;
  995. Result:=Abs(A)<=Epsilon;
  996. end;
  997. function IsZero(const A: Extended): Boolean;inline;
  998. begin
  999. Result:=IsZero(A,EZeroResolution);
  1000. end;
  1001. {$endif FPC_HAS_TYPE_EXTENDED}
  1002. type
  1003. TSplitDouble = packed record
  1004. cards: Array[0..1] of cardinal;
  1005. end;
  1006. function IsNan(const d : Double): Boolean;
  1007. var
  1008. fraczero, expMaximal: boolean;
  1009. begin
  1010. {$if defined(FPC_BIG_ENDIAN) or (defined(CPUARM) and defined(FPUFPA))}
  1011. expMaximal := ((TSplitDouble(d).cards[0] shr 20) and $7ff) = 2047;
  1012. fraczero:= (TSplitDouble(d).cards[0] and $fffff = 0) and
  1013. (TSplitDouble(d).cards[1] = 0);
  1014. {$else FPC_BIG_ENDIAN}
  1015. expMaximal := ((TSplitDouble(d).cards[1] shr 20) and $7ff) = 2047;
  1016. fraczero := (TSplitDouble(d).cards[1] and $fffff = 0) and
  1017. (TSplitDouble(d).cards[0] = 0);
  1018. {$endif FPC_BIG_ENDIAN}
  1019. Result:=expMaximal and not(fraczero);
  1020. end;
  1021. function IsInfinite(const d : Double): Boolean;
  1022. var
  1023. fraczero, expMaximal: boolean;
  1024. begin
  1025. {$if defined(FPC_BIG_ENDIAN) or (defined(CPUARM) and defined(FPUFPA))}
  1026. expMaximal := ((TSplitDouble(d).cards[0] shr 20) and $7ff) = 2047;
  1027. fraczero:= (TSplitDouble(d).cards[0] and $fffff = 0) and
  1028. (TSplitDouble(d).cards[1] = 0);
  1029. {$else FPC_BIG_ENDIAN}
  1030. expMaximal := ((TSplitDouble(d).cards[1] shr 20) and $7ff) = 2047;
  1031. fraczero := (TSplitDouble(d).cards[1] and $fffff = 0) and
  1032. (TSplitDouble(d).cards[0] = 0);
  1033. {$endif FPC_BIG_ENDIAN}
  1034. Result:=expMaximal and fraczero;
  1035. end;
  1036. {$ifdef FPC_HAS_TYPE_EXTENDED}
  1037. function SameValue(const A, B: Extended; Epsilon: Extended): Boolean;
  1038. begin
  1039. if (Epsilon=0) then
  1040. Epsilon:=Max(Min(Abs(A),Abs(B))*EZeroResolution,EZeroResolution);
  1041. if (A>B) then
  1042. Result:=((A-B)<=Epsilon)
  1043. else
  1044. Result:=((B-A)<=Epsilon);
  1045. end;
  1046. function SameValue(const A, B: Extended): Boolean;inline;
  1047. begin
  1048. Result:=SameValue(A,B,0);
  1049. end;
  1050. {$endif FPC_HAS_TYPE_EXTENDED}
  1051. {$ifdef FPC_HAS_TYPE_DOUBLE}
  1052. function SameValue(const A, B: Double): Boolean;inline;
  1053. begin
  1054. Result:=SameValue(A,B,0);
  1055. end;
  1056. function SameValue(const A, B: Double; Epsilon: Double): Boolean;
  1057. begin
  1058. if (Epsilon=0) then
  1059. Epsilon:=Max(Min(Abs(A),Abs(B))*DZeroResolution,DZeroResolution);
  1060. if (A>B) then
  1061. Result:=((A-B)<=Epsilon)
  1062. else
  1063. Result:=((B-A)<=Epsilon);
  1064. end;
  1065. {$endif FPC_HAS_TYPE_DOUBLE}
  1066. function SameValue(const A, B: Single): Boolean;inline;
  1067. begin
  1068. Result:=SameValue(A,B,0);
  1069. end;
  1070. function SameValue(const A, B: Single; Epsilon: Single): Boolean;
  1071. begin
  1072. if (Epsilon=0) then
  1073. Epsilon:=Max(Min(Abs(A),Abs(B))*SZeroResolution,SZeroResolution);
  1074. if (A>B) then
  1075. Result:=((A-B)<=Epsilon)
  1076. else
  1077. Result:=((B-A)<=Epsilon);
  1078. end;
  1079. // Some CPUs probably allow a faster way of doing this in a single operation...
  1080. // There weshould define CPUDIVMOD in the header mathuh.inc and implement it using asm.
  1081. {$ifndef CPUDIVMOD}
  1082. procedure DivMod(Dividend: Integer; Divisor: Word; var Result, Remainder: Word);
  1083. begin
  1084. Result:=Dividend Div Divisor;
  1085. Remainder:=Dividend Mod Divisor;
  1086. end;
  1087. {$endif}
  1088. function ifthen(val:boolean;const iftrue:integer; const iffalse:integer= 0) :integer;
  1089. begin
  1090. if val then result:=iftrue else result:=iffalse;
  1091. end;
  1092. function ifthen(val:boolean;const iftrue:int64 ; const iffalse:int64 = 0) :int64;
  1093. begin
  1094. if val then result:=iftrue else result:=iffalse;
  1095. end;
  1096. function ifthen(val:boolean;const iftrue:double ; const iffalse:double =0.0):double;
  1097. begin
  1098. if val then result:=iftrue else result:=iffalse;
  1099. end;
  1100. end.