math.pp 30 KB

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