math.pp 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425
  1. {
  2. $Id$
  3. This file is part of the Free Pascal run time library.
  4. Copyright (c) 1999-2000 by Florian Klaempfl
  5. member of the Free Pascal development team
  6. See the file COPYING.FPC, included in this distribution,
  7. for details about the copyright.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  11. **********************************************************************}
  12. {
  13. This unit is an equivalent to the Delphi math unit
  14. (with some improvements)
  15. What's to do:
  16. o a lot of function :), search for !!!!
  17. o some statistical functions
  18. o all financial functions
  19. o optimizations
  20. }
  21. unit math;
  22. interface
  23. {$MODE objfpc}
  24. {$ifdef VER1_0}
  25. { we don't assume cross compiling from 1.0.x-m68k ... }
  26. {$define FPC_HAS_TYPE_EXTENDED}
  27. {$endif VER1_0}
  28. uses
  29. sysutils;
  30. { Ranges of the IEEE floating point types, including denormals }
  31. {$ifdef FPC_HAS_TYPE_SINGLE}
  32. const
  33. MinSingle = 1.5e-45;
  34. MaxSingle = 3.4e+38;
  35. {$endif FPC_HAS_TYPE_SINGLE}
  36. {$ifdef FPC_HAS_TYPE_DOUBLE}
  37. const
  38. MinDouble = 5.0e-324;
  39. MaxDouble = 1.7e+308;
  40. {$endif FPC_HAS_TYPE_DOUBLE}
  41. {$ifdef FPC_HAS_TYPE_EXTENDED}
  42. const
  43. MinExtended = 3.4e-4932;
  44. MaxExtended = 1.1e+4932;
  45. {$endif FPC_HAS_TYPE_EXTENDED}
  46. {$ifdef FPC_HAS_TYPE_COMP}
  47. const
  48. MinComp = -9.223372036854775807e+18;
  49. MaxComp = 9.223372036854775807e+18;
  50. {$endif FPC_HAS_TYPE_COMP}
  51. { the original delphi functions use extended as argument, }
  52. { but I would prefer double, because 8 bytes is a very }
  53. { natural size for the processor }
  54. { WARNING : changing float type will }
  55. { break all assembler code PM }
  56. {$ifdef FPC_HAS_TYPE_FLOAT128}
  57. type
  58. float = float128;
  59. const
  60. MinFloat = MinFloat128;
  61. MaxFloat = MaxFloat128;
  62. {$else FPC_HAS_TYPE_FLOAT128}
  63. {$ifdef FPC_HAS_TYPE_EXTENDED}
  64. type
  65. float = extended;
  66. const
  67. MinFloat = MinExtended;
  68. MaxFloat = MaxExtended;
  69. {$else FPC_HAS_TYPE_EXTENDED}
  70. {$ifdef FPC_HAS_TYPE_DOUBLE}
  71. type
  72. float = double;
  73. const
  74. MinFloat = MinDouble;
  75. MaxFloat = MaxDouble;
  76. {$else FPC_HAS_TYPE_DOUBLE}
  77. {$ifdef FPC_HAS_TYPE_SINGLE}
  78. type
  79. float = single;
  80. const
  81. MinFloat = MinSingle;
  82. MaxFloat = MaxSingle;
  83. {$else FPC_HAS_TYPE_SINGLE}
  84. {$fatal At least one floating point type must be supported}
  85. {$endif FPC_HAS_TYPE_SINGLE}
  86. {$endif FPC_HAS_TYPE_DOUBLE}
  87. {$endif FPC_HAS_TYPE_EXTENDED}
  88. {$endif FPC_HAS_TYPE_FLOAT128}
  89. type
  90. PFloat = ^Float;
  91. PInteger = ^Integer;
  92. tpaymenttime = (ptendofperiod,ptstartofperiod);
  93. einvalidargument = class(ematherror);
  94. TValueRelationship = -1..1;
  95. const
  96. EqualsValue = 0;
  97. LessThanValue = Low(TValueRelationship);
  98. GreaterThanValue = High(TValueRelationship);
  99. {$ifndef ver1_0}
  100. {$ifopt R+}
  101. {$define RangeCheckWasOn}
  102. {$R-}
  103. {$endif opt R+}
  104. {$ifopt Q+}
  105. {$define OverflowCheckWasOn}
  106. {$Q-}
  107. {$endif opt Q+}
  108. {$ifdef CPUARM}
  109. { the ARM linux emulator doesn't like 0.0/0.0 }
  110. NaN = ln(-1.0);
  111. {$else CPUARM}
  112. NaN = 0.0/0.0;
  113. {$endif CPUARM}
  114. Infinity = 1.0/0.0;
  115. {$ifdef RangeCheckWasOn}
  116. {$R+}
  117. {$undef RangeCheckWasOn}
  118. {$endif}
  119. {$ifdef OverflowCheckWasOn}
  120. {$Q+}
  121. {$undef OverflowCheckWasOn}
  122. {$endif}
  123. {$endif ver1_0}
  124. { Min/max determination }
  125. function MinIntValue(const Data: array of Integer): Integer;
  126. function MaxIntValue(const Data: array of Integer): Integer;
  127. { Extra, not present in Delphi, but used frequently }
  128. function Min(a, b: Integer): Integer;
  129. function Max(a, b: Integer): Integer;
  130. function Min(a, b: Cardinal): Cardinal;
  131. function Max(a, b: Cardinal): Cardinal;
  132. function Min(a, b: Int64): Int64;
  133. function Max(a, b: Int64): Int64;
  134. {$ifdef FPC_HAS_TYPE_SINGLE}
  135. function Min(a, b: Single): Single;
  136. function Max(a, b: Single): Single;
  137. {$endif FPC_HAS_TYPE_SINGLE}
  138. {$ifdef FPC_HAS_TYPE_DOUBLE}
  139. function Min(a, b: Double): Double;
  140. function Max(a, b: Double): Double;
  141. {$endif FPC_HAS_TYPE_DOUBLE}
  142. {$ifdef FPC_HAS_TYPE_EXTENDED}
  143. function Min(a, b: Extended): Extended;
  144. function Max(a, b: Extended): Extended;
  145. {$endif FPC_HAS_TYPE_EXTENDED}
  146. function InRange(const AValue, AMin, AMax: Integer): Boolean;
  147. function InRange(const AValue, AMin, AMax: Int64): Boolean;
  148. {$ifdef FPC_HAS_TYPE_DOUBLE}
  149. function InRange(const AValue, AMin, AMax: Double): Boolean;
  150. {$endif FPC_HAS_TYPE_DOUBLE}
  151. function EnsureRange(const AValue, AMin, AMax: Integer): Integer;
  152. function EnsureRange(const AValue, AMin, AMax: Int64): Int64;
  153. {$ifdef FPC_HAS_TYPE_DOUBLE}
  154. function EnsureRange(const AValue, AMin, AMax: Double): Double;
  155. {$endif FPC_HAS_TYPE_DOUBLE}
  156. procedure DivMod(Dividend: Integer; Divisor: Word; var Result, Remainder: Word);
  157. // Sign functions
  158. Type
  159. TValueSign = -1..1;
  160. const
  161. NegativeValue = Low(TValueSign);
  162. ZeroValue = 0;
  163. PositiveValue = High(TValueSign);
  164. function Sign(const AValue: Integer): TValueSign;
  165. function Sign(const AValue: Int64): TValueSign;
  166. function Sign(const AValue: Double): TValueSign;
  167. function IsZero(const A: Single; Epsilon: Single): Boolean;
  168. function IsZero(const A: Single): Boolean;
  169. {$ifdef FPC_HAS_TYPE_DOUBLE}
  170. function IsZero(const A: Double; Epsilon: Double): Boolean;
  171. function IsZero(const A: Double): Boolean;
  172. {$endif FPC_HAS_TYPE_DOUBLE}
  173. {$ifdef FPC_HAS_TYPE_EXTENDED}
  174. function IsZero(const A: Extended; Epsilon: Extended): Boolean;
  175. function IsZero(const A: Extended): Boolean;
  176. {$endif FPC_HAS_TYPE_EXTENDED}
  177. function IsNan(const d : Double): Boolean;
  178. function IsInfinite(const d : Double): Boolean;
  179. {$ifdef FPC_HAS_TYPE_EXTENDED}
  180. function SameValue(const A, B: Extended): Boolean;
  181. {$endif}
  182. {$ifdef FPC_HAS_TYPE_DOUBLE}
  183. function SameValue(const A, B: Double): Boolean;
  184. {$endif}
  185. function SameValue(const A, B: Single): Boolean;
  186. {$ifdef FPC_HAS_TYPE_EXTENDED}
  187. function SameValue(const A, B: Extended; Epsilon: Extended): Boolean;
  188. {$endif}
  189. {$ifdef FPC_HAS_TYPE_DOUBLE}
  190. function SameValue(const A, B: Double; Epsilon: Double): Boolean;
  191. {$endif}
  192. function SameValue(const A, B: Single; Epsilon: Single): Boolean;
  193. { angle conversion }
  194. function degtorad(deg : float) : float;
  195. function radtodeg(rad : float) : float;
  196. function gradtorad(grad : float) : float;
  197. function radtograd(rad : float) : float;
  198. function degtograd(deg : float) : float;
  199. function gradtodeg(grad : float) : float;
  200. { one cycle are 2*Pi rad }
  201. function cycletorad(cycle : float) : float;
  202. function radtocycle(rad : float) : float;
  203. { trigoniometric functions }
  204. function tan(x : float) : float;
  205. function cotan(x : float) : float;
  206. procedure sincos(theta : float;var sinus,cosinus : float);
  207. { inverse functions }
  208. function arccos(x : float) : float;
  209. function arcsin(x : float) : float;
  210. { calculates arctan(y/x) and returns an angle in the correct quadrant }
  211. function arctan2(y,x : float) : float;
  212. { hyperbolic functions }
  213. function cosh(x : float) : float;
  214. function sinh(x : float) : float;
  215. function tanh(x : float) : float;
  216. { area functions }
  217. { delphi names: }
  218. function arccosh(x : float) : float;
  219. function arcsinh(x : float) : float;
  220. function arctanh(x : float) : float;
  221. { IMHO the function should be called as follows (FK) }
  222. function arcosh(x : float) : float;
  223. function arsinh(x : float) : float;
  224. function artanh(x : float) : float;
  225. { triangle functions }
  226. { returns the length of the hypotenuse of a right triangle }
  227. { if x and y are the other sides }
  228. function hypot(x,y : float) : float;
  229. { logarithm functions }
  230. function log10(x : float) : float;
  231. function log2(x : float) : float;
  232. function logn(n,x : float) : float;
  233. { returns natural logarithm of x+1 }
  234. function lnxp1(x : float) : float;
  235. { exponential functions }
  236. function power(base,exponent : float) : float;
  237. { base^exponent }
  238. function intpower(base : float;const exponent : Integer) : float;
  239. { number converting }
  240. { rounds x towards positive infinity }
  241. function ceil(x : float) : Integer;
  242. { rounds x towards negative infinity }
  243. function floor(x : float) : Integer;
  244. { misc. functions }
  245. { splits x into mantissa and exponent (to base 2) }
  246. procedure Frexp(X: float; var Mantissa: float; var Exponent: integer);
  247. { returns x*(2^p) }
  248. function ldexp(x : float; const p : Integer) : float;
  249. { statistical functions }
  250. function mean(const data : array of float) : float;
  251. function sum(const data : array of float) : float;
  252. function mean(const data : PFloat; Const N : longint) : float;
  253. function sum(const data : PFloat; Const N : Longint) : float;
  254. function sumofsquares(const data : array of float) : float;
  255. function sumofsquares(const data : PFloat; Const N : Integer) : float;
  256. { calculates the sum and the sum of squares of data }
  257. procedure sumsandsquares(const data : array of float;
  258. var sum,sumofsquares : float);
  259. procedure sumsandsquares(const data : PFloat; Const N : Integer;
  260. var sum,sumofsquares : float);
  261. function minvalue(const data : array of float) : float;
  262. function minvalue(const data : array of integer) : Integer;
  263. function minvalue(const data : PFloat; Const N : Integer) : float;
  264. function MinValue(const Data : PInteger; Const N : Integer): Integer;
  265. function maxvalue(const data : array of float) : float;
  266. function maxvalue(const data : array of integer) : Integer;
  267. function maxvalue(const data : PFloat; Const N : Integer) : float;
  268. function maxvalue(const data : PInteger; Const N : Integer) : Integer;
  269. { calculates the standard deviation }
  270. function stddev(const data : array of float) : float;
  271. function stddev(const data : PFloat; Const N : Integer) : float;
  272. { calculates the mean and stddev }
  273. procedure meanandstddev(const data : array of float;
  274. var mean,stddev : float);
  275. procedure meanandstddev(const data : PFloat;
  276. Const N : Longint;var mean,stddev : float);
  277. function variance(const data : array of float) : float;
  278. function totalvariance(const data : array of float) : float;
  279. function variance(const data : PFloat; Const N : Integer) : float;
  280. function totalvariance(const data : PFloat; Const N : Integer) : float;
  281. { returns random values with gaussian distribution }
  282. function randg(mean,stddev : float) : float;
  283. { I don't know what the following functions do: }
  284. function popnstddev(const data : array of float) : float;
  285. function popnstddev(const data : PFloat; Const N : Integer) : float;
  286. function popnvariance(const data : PFloat; Const N : Integer) : float;
  287. function popnvariance(const data : array of float) : float;
  288. procedure momentskewkurtosis(const data : array of float;
  289. var m1,m2,m3,m4,skew,kurtosis : float);
  290. procedure momentskewkurtosis(const data : PFloat; Const N : Integer;
  291. var m1,m2,m3,m4,skew,kurtosis : float);
  292. { geometrical function }
  293. { returns the euclidean L2 norm }
  294. function norm(const data : array of float) : float;
  295. function norm(const data : PFloat; Const N : Integer) : float;
  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;
  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;
  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;
  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. function ceil(x : float) : integer;
  529. begin
  530. Ceil:=Trunc(x);
  531. If Frac(x)>0 then
  532. Ceil:=Ceil+1;
  533. end;
  534. function floor(x : float) : integer;
  535. begin
  536. Floor:=Trunc(x);
  537. If Frac(x)<0 then
  538. Floor := Floor-1;
  539. end;
  540. procedure Frexp(X: float; var Mantissa: float; var Exponent: integer);
  541. begin
  542. Exponent :=0;
  543. if (abs(x)<0.5) then
  544. While (abs(x)<0.5) do
  545. begin
  546. x := x*2;
  547. Dec(Exponent);
  548. end
  549. else
  550. While (abs(x)>1) do
  551. begin
  552. x := x/2;
  553. Inc(Exponent);
  554. end;
  555. mantissa := x;
  556. end;
  557. function ldexp(x : float;const p : Integer) : float;
  558. begin
  559. ldexp:=x*intpower(2.0,p);
  560. end;
  561. function mean(const data : array of float) : float;
  562. begin
  563. Result:=Mean(@data[0],High(Data)+1);
  564. end;
  565. function mean(const data : PFloat; Const N : longint) : float;
  566. begin
  567. mean:=sum(Data,N);
  568. mean:=mean/N;
  569. end;
  570. function sum(const data : array of float) : float;
  571. begin
  572. Result:=Sum(@Data[0],High(Data)+1);
  573. end;
  574. function sum(const data : PFloat;Const N : longint) : float;
  575. var
  576. i : longint;
  577. begin
  578. sum:=0.0;
  579. for i:=0 to N-1 do
  580. sum:=sum+data[i];
  581. end;
  582. function sumofsquares(const data : array of float) : float;
  583. begin
  584. Result:=sumofsquares(@data[0],High(Data)+1);
  585. end;
  586. function sumofsquares(const data : PFloat; Const N : Integer) : float;
  587. var
  588. i : longint;
  589. begin
  590. sumofsquares:=0.0;
  591. for i:=0 to N-1 do
  592. sumofsquares:=sumofsquares+sqr(data[i]);
  593. end;
  594. procedure sumsandsquares(const data : array of float;
  595. var sum,sumofsquares : float);
  596. begin
  597. sumsandsquares (@Data[0],High(Data)+1,Sum,sumofsquares);
  598. end;
  599. procedure sumsandsquares(const data : PFloat; Const N : Integer;
  600. var sum,sumofsquares : float);
  601. var
  602. i : Integer;
  603. temp : float;
  604. begin
  605. sumofsquares:=0.0;
  606. sum:=0.0;
  607. for i:=0 to N-1 do
  608. begin
  609. temp:=data[i];
  610. sumofsquares:=sumofsquares+sqr(temp);
  611. sum:=sum+temp;
  612. end;
  613. end;
  614. function stddev(const data : array of float) : float;
  615. begin
  616. Result:=Stddev(@Data[0],High(Data)+1)
  617. end;
  618. function stddev(const data : PFloat; Const N : Integer) : float;
  619. begin
  620. StdDev:=Sqrt(Variance(Data,N));
  621. end;
  622. procedure meanandstddev(const data : array of float;
  623. var mean,stddev : float);
  624. begin
  625. Meanandstddev(@Data[0],High(Data)+1,Mean,stddev);
  626. end;
  627. procedure meanandstddev(const data : PFloat;
  628. Const N : Longint;var mean,stddev : float);
  629. Var I : longint;
  630. begin
  631. Mean:=0;
  632. StdDev:=0;
  633. For I:=0 to N-1 do
  634. begin
  635. Mean:=Mean+Data[i];
  636. StdDev:=StdDev+Sqr(Data[i]);
  637. end;
  638. Mean:=Mean/N;
  639. StdDev:=(StdDev-N*Sqr(Mean));
  640. If N>1 then
  641. StdDev:=Sqrt(Stddev/(N-1))
  642. else
  643. StdDev:=0;
  644. end;
  645. function variance(const data : array of float) : float;
  646. begin
  647. Variance:=Variance(@Data[0],High(Data)+1);
  648. end;
  649. function variance(const data : PFloat; Const N : Integer) : float;
  650. begin
  651. If N=1 then
  652. Result:=0
  653. else
  654. Result:=TotalVariance(Data,N)/(N-1);
  655. end;
  656. function totalvariance(const data : array of float) : float;
  657. begin
  658. Result:=TotalVariance(@Data[0],High(Data)+1);
  659. end;
  660. function totalvariance(const data : Pfloat;Const N : Integer) : float;
  661. var S,SS : Float;
  662. begin
  663. If N=1 then
  664. Result:=0
  665. else
  666. begin
  667. SumsAndSquares(Data,N,S,SS);
  668. Result := SS-Sqr(S)/N;
  669. end;
  670. end;
  671. function randg(mean,stddev : float) : float;
  672. Var U1,S2 : Float;
  673. begin
  674. repeat
  675. u1:= 2*random-1;
  676. S2:=Sqr(U1)+sqr(2*random-1);
  677. until s2<1;
  678. randg:=Sqrt(-2*ln(S2)/S2)*u1*stddev+Mean;
  679. end;
  680. function popnstddev(const data : array of float) : float;
  681. begin
  682. PopnStdDev:=Sqrt(PopnVariance(@Data[0],High(Data)+1));
  683. end;
  684. function popnstddev(const data : PFloat; Const N : Integer) : float;
  685. begin
  686. PopnStdDev:=Sqrt(PopnVariance(Data,N));
  687. end;
  688. function popnvariance(const data : array of float) : float;
  689. begin
  690. popnvariance:=popnvariance(@data[0],high(Data)+1);
  691. end;
  692. function popnvariance(const data : PFloat; Const N : Integer) : float;
  693. begin
  694. PopnVariance:=TotalVariance(Data,N)/N;
  695. end;
  696. procedure momentskewkurtosis(const data : array of float;
  697. var m1,m2,m3,m4,skew,kurtosis : float);
  698. begin
  699. momentskewkurtosis(@Data[0],High(Data)+1,m1,m2,m3,m4,skew,kurtosis);
  700. end;
  701. procedure momentskewkurtosis(const data : PFloat; Const N : Integer;
  702. var m1,m2,m3,m4,skew,kurtosis : float);
  703. Var S,SS,SC,SQ,invN,Acc,M1S,S2N,S3N,temp : Float;
  704. I : Longint;
  705. begin
  706. invN:=1.0/N;
  707. s:=0;
  708. ss:=0;
  709. sq:=0;
  710. sc:=0;
  711. for i:=0 to N-1 do
  712. begin
  713. temp:=Data[i]; { faster }
  714. S:=S+temp;
  715. acc:=temp*temp;
  716. ss:=ss+acc;
  717. Acc:=acc*temp;
  718. Sc:=sc+acc;
  719. acc:=acc*temp;
  720. sq:=sq+acc;
  721. end;
  722. M1:=s*invN;
  723. M1S:=M1*M1;
  724. S2N:=SS*invN;
  725. S3N:=SC*invN;
  726. M2:=S2N-M1S;
  727. M3:=S3N-(M1*3*S2N) + 2*M1S*M1;
  728. M4:=SQ*invN - (M1 * 4 * S3N) + (M1S*6*S2N-3*Sqr(M1S));
  729. Skew:=M3*power(M2,-3/2);
  730. Kurtosis:=M4 / Sqr(M2);
  731. end;
  732. function norm(const data : array of float) : float;
  733. begin
  734. norm:=Norm(@data[0],High(Data)+1);
  735. end;
  736. function norm(const data : PFloat; Const N : Integer) : float;
  737. begin
  738. norm:=sqrt(sumofsquares(data,N));
  739. end;
  740. function MinIntValue(const Data: array of Integer): Integer;
  741. var
  742. I: Integer;
  743. begin
  744. Result := Data[Low(Data)];
  745. For I := Succ(Low(Data)) To High(Data) Do
  746. If Data[I] < Result Then Result := Data[I];
  747. end;
  748. function MinValue(const Data: array of Integer): Integer;
  749. begin
  750. Result:=MinValue(Pinteger(@Data[0]),High(Data)+1)
  751. end;
  752. function MinValue(const Data: PInteger; Const N : Integer): Integer;
  753. var
  754. I: Integer;
  755. begin
  756. Result := Data[0];
  757. For I := 1 To N-1 do
  758. If Data[I] < Result Then Result := Data[I];
  759. end;
  760. function minvalue(const data : array of float) : float;
  761. begin
  762. Result:=minvalue(PFloat(@data[0]),High(Data)+1);
  763. end;
  764. function minvalue(const data : PFloat; Const N : Integer) : float;
  765. var
  766. i : longint;
  767. begin
  768. { get an initial value }
  769. minvalue:=data[0];
  770. for i:=1 to N-1 do
  771. if data[i]<minvalue then
  772. minvalue:=data[i];
  773. end;
  774. function MaxIntValue(const Data: array of Integer): Integer;
  775. var
  776. I: Integer;
  777. begin
  778. Result := Data[Low(Data)];
  779. For I := Succ(Low(Data)) To High(Data) Do
  780. If Data[I] > Result Then Result := Data[I];
  781. end;
  782. function maxvalue(const data : array of float) : float;
  783. begin
  784. Result:=maxvalue(PFloat(@data[0]),High(Data)+1);
  785. end;
  786. function maxvalue(const data : PFloat; Const N : Integer) : float;
  787. var
  788. i : longint;
  789. begin
  790. { get an initial value }
  791. maxvalue:=data[0];
  792. for i:=1 to N-1 do
  793. if data[i]>maxvalue then
  794. maxvalue:=data[i];
  795. end;
  796. function MaxValue(const Data: array of Integer): Integer;
  797. begin
  798. Result:=MaxValue(PInteger(@Data[0]),High(Data)+1)
  799. end;
  800. function maxvalue(const data : PInteger; Const N : Integer) : Integer;
  801. var
  802. i : longint;
  803. begin
  804. { get an initial value }
  805. maxvalue:=data[0];
  806. for i:=1 to N-1 do
  807. if data[i]>maxvalue then
  808. maxvalue:=data[i];
  809. end;
  810. function Min(a, b: Integer): Integer;
  811. begin
  812. if a < b then
  813. Result := a
  814. else
  815. Result := b;
  816. end;
  817. function Max(a, b: Integer): Integer;
  818. begin
  819. if a > b then
  820. Result := a
  821. else
  822. Result := b;
  823. end;
  824. function Min(a, b: Cardinal): Cardinal;
  825. begin
  826. if a < b then
  827. Result := a
  828. else
  829. Result := b;
  830. end;
  831. function Max(a, b: Cardinal): Cardinal;
  832. begin
  833. if a > b then
  834. Result := a
  835. else
  836. Result := b;
  837. end;
  838. function Min(a, b: Int64): Int64;
  839. begin
  840. if a < b then
  841. Result := a
  842. else
  843. Result := b;
  844. end;
  845. function Max(a, b: Int64): Int64;
  846. begin
  847. if a > b then
  848. Result := a
  849. else
  850. Result := b;
  851. end;
  852. {$ifdef FPC_HAS_TYPE_SINGLE}
  853. function Min(a, b: Single): Single;
  854. begin
  855. if a < b then
  856. Result := a
  857. else
  858. Result := b;
  859. end;
  860. function Max(a, b: Single): Single;
  861. begin
  862. if a > b then
  863. Result := a
  864. else
  865. Result := b;
  866. end;
  867. {$endif FPC_HAS_TYPE_SINGLE}
  868. {$ifdef FPC_HAS_TYPE_DOUBLE}
  869. function Min(a, b: Double): Double;
  870. begin
  871. if a < b then
  872. Result := a
  873. else
  874. Result := b;
  875. end;
  876. function Max(a, b: Double): Double;
  877. begin
  878. if a > b then
  879. Result := a
  880. else
  881. Result := b;
  882. end;
  883. {$endif FPC_HAS_TYPE_DOUBLE}
  884. {$ifdef FPC_HAS_TYPE_EXTENDED}
  885. function Min(a, b: Extended): Extended;
  886. begin
  887. if a < b then
  888. Result := a
  889. else
  890. Result := b;
  891. end;
  892. function Max(a, b: Extended): Extended;
  893. begin
  894. if a > b then
  895. Result := a
  896. else
  897. Result := b;
  898. end;
  899. {$endif FPC_HAS_TYPE_EXTENDED}
  900. function InRange(const AValue, AMin, AMax: Integer): Boolean;
  901. begin
  902. Result:=(AValue>=AMin) and (AValue<=AMax);
  903. end;
  904. function InRange(const AValue, AMin, AMax: Int64): Boolean;
  905. begin
  906. Result:=(AValue>=AMin) and (AValue<=AMax);
  907. end;
  908. {$ifdef FPC_HAS_TYPE_DOUBLE}
  909. function InRange(const AValue, AMin, AMax: Double): Boolean;
  910. begin
  911. Result:=(AValue>=AMin) and (AValue<=AMax);
  912. end;
  913. {$endif FPC_HAS_TYPE_DOUBLE}
  914. function EnsureRange(const AValue, AMin, AMax: Integer): Integer;
  915. begin
  916. Result:=AValue;
  917. If Result<AMin then
  918. Result:=AMin
  919. else if Result>AMax then
  920. Result:=AMax;
  921. end;
  922. function EnsureRange(const AValue, AMin, AMax: Int64): Int64;
  923. begin
  924. Result:=AValue;
  925. If Result<AMin then
  926. Result:=AMin
  927. else if Result>AMax then
  928. Result:=AMax;
  929. end;
  930. {$ifdef FPC_HAS_TYPE_DOUBLE}
  931. function EnsureRange(const AValue, AMin, AMax: Double): Double;
  932. begin
  933. Result:=AValue;
  934. If Result<AMin then
  935. Result:=AMin
  936. else if Result>AMax then
  937. Result:=AMax;
  938. end;
  939. {$endif FPC_HAS_TYPE_DOUBLE}
  940. Const
  941. EZeroResolution = 1E-16;
  942. DZeroResolution = 1E-12;
  943. SZeroResolution = 1E-4;
  944. function IsZero(const A: Single; Epsilon: Single): Boolean;
  945. begin
  946. if (Epsilon=0) then
  947. Epsilon:=SZeroResolution;
  948. Result:=Abs(A)<=Epsilon;
  949. end;
  950. function IsZero(const A: Single): Boolean;
  951. begin
  952. Result:=IsZero(A,single(SZeroResolution));
  953. end;
  954. {$ifdef FPC_HAS_TYPE_DOUBLE}
  955. function IsZero(const A: Double; Epsilon: Double): Boolean;
  956. begin
  957. if (Epsilon=0) then
  958. Epsilon:=DZeroResolution;
  959. Result:=Abs(A)<=Epsilon;
  960. end;
  961. function IsZero(const A: Double): Boolean;
  962. begin
  963. Result:=IsZero(A,DZeroResolution);
  964. end;
  965. {$endif FPC_HAS_TYPE_DOUBLE}
  966. {$ifdef FPC_HAS_TYPE_EXTENDED}
  967. function IsZero(const A: Extended; Epsilon: Extended): Boolean;
  968. begin
  969. if (Epsilon=0) then
  970. Epsilon:=EZeroResolution;
  971. Result:=Abs(A)<=Epsilon;
  972. end;
  973. function IsZero(const A: Extended): Boolean;
  974. begin
  975. Result:=IsZero(A,EZeroResolution);
  976. end;
  977. {$endif FPC_HAS_TYPE_EXTENDED}
  978. type
  979. TSplitDouble = packed record
  980. cards: Array[0..1] of cardinal;
  981. end;
  982. function IsNan(const d : Double): Boolean;
  983. var
  984. fraczero, expMaximal: boolean;
  985. begin
  986. {$if defined(FPC_BIG_ENDIAN) or (defined(CPUARM) and defined(FPUFPA))}
  987. expMaximal := ((TSplitDouble(d).cards[0] shr 20) and $7ff) = 2047;
  988. fraczero:= (TSplitDouble(d).cards[0] and $fffff = 0) and
  989. (TSplitDouble(d).cards[1] = 0);
  990. {$else FPC_BIG_ENDIAN}
  991. expMaximal := ((TSplitDouble(d).cards[1] shr 20) and $7ff) = 2047;
  992. fraczero := (TSplitDouble(d).cards[1] and $fffff = 0) and
  993. (TSplitDouble(d).cards[0] = 0);
  994. {$endif FPC_BIG_ENDIAN}
  995. Result:=expMaximal and not(fraczero);
  996. end;
  997. function IsInfinite(const d : Double): Boolean;
  998. var
  999. fraczero, expMaximal: boolean;
  1000. begin
  1001. {$if defined(FPC_BIG_ENDIAN) or (defined(CPUARM) and defined(FPUFPA))}
  1002. expMaximal := ((TSplitDouble(d).cards[0] shr 20) and $7ff) = 2047;
  1003. fraczero:= (TSplitDouble(d).cards[0] and $fffff = 0) and
  1004. (TSplitDouble(d).cards[1] = 0);
  1005. {$else FPC_BIG_ENDIAN}
  1006. expMaximal := ((TSplitDouble(d).cards[1] shr 20) and $7ff) = 2047;
  1007. fraczero := (TSplitDouble(d).cards[1] and $fffff = 0) and
  1008. (TSplitDouble(d).cards[0] = 0);
  1009. {$endif FPC_BIG_ENDIAN}
  1010. Result:=expMaximal and fraczero;
  1011. end;
  1012. {$ifdef FPC_HAS_TYPE_EXTENDED}
  1013. function SameValue(const A, B: Extended; Epsilon: Extended): Boolean;
  1014. begin
  1015. if (Epsilon=0) then
  1016. Epsilon:=Max(Min(Abs(A),Abs(B))*EZeroResolution,EZeroResolution);
  1017. if (A>B) then
  1018. Result:=((A-B)<=Epsilon)
  1019. else
  1020. Result:=((B-A)<=Epsilon);
  1021. end;
  1022. function SameValue(const A, B: Extended): Boolean;
  1023. begin
  1024. Result:=SameValue(A,B,0);
  1025. end;
  1026. {$endif FPC_HAS_TYPE_EXTENDED}
  1027. {$ifdef FPC_HAS_TYPE_DOUBLE}
  1028. function SameValue(const A, B: Double): Boolean;
  1029. begin
  1030. Result:=SameValue(A,B,0);
  1031. end;
  1032. function SameValue(const A, B: Double; Epsilon: Double): Boolean;
  1033. begin
  1034. if (Epsilon=0) then
  1035. Epsilon:=Max(Min(Abs(A),Abs(B))*DZeroResolution,DZeroResolution);
  1036. if (A>B) then
  1037. Result:=((A-B)<=Epsilon)
  1038. else
  1039. Result:=((B-A)<=Epsilon);
  1040. end;
  1041. {$endif FPC_HAS_TYPE_DOUBLE}
  1042. function SameValue(const A, B: Single): Boolean;
  1043. begin
  1044. Result:=SameValue(A,B,0);
  1045. end;
  1046. function SameValue(const A, B: Single; Epsilon: Single): Boolean;
  1047. begin
  1048. if (Epsilon=0) then
  1049. Epsilon:=Max(Min(Abs(A),Abs(B))*SZeroResolution,SZeroResolution);
  1050. if (A>B) then
  1051. Result:=((A-B)<=Epsilon)
  1052. else
  1053. Result:=((B-A)<=Epsilon);
  1054. end;
  1055. // Some CPUs probably allow a faster way of doing this in a single operation...
  1056. // There weshould define CPUDIVMOD in the header mathuh.inc and implement it using asm.
  1057. {$ifndef CPUDIVMOD}
  1058. procedure DivMod(Dividend: Integer; Divisor: Word; var Result, Remainder: Word);
  1059. begin
  1060. Result:=Dividend Div Divisor;
  1061. Remainder:=Dividend Mod Divisor;
  1062. end;
  1063. {$endif}
  1064. end.
  1065. {
  1066. $Log$
  1067. Revision 1.28 2005-01-12 20:17:39 florian
  1068. * generic arctan2 for 3rd and 4th quadrand fixed
  1069. Revision 1.27 2005/01/04 16:47:05 florian
  1070. * compilation on ARM fixed
  1071. Revision 1.26 2004/12/05 16:52:34 jonas
  1072. * also invalid argument if trying to calculate 0^0 with intpower()
  1073. Revision 1.25 2004/12/05 16:43:57 jonas
  1074. * fixed power() in genmath.inc (code duplication from math.pp for **
  1075. support!)
  1076. * fixed power() in math.pp to give an error from 0^0
  1077. Revision 1.24 2004/12/04 23:38:59 florian
  1078. * fixed power(float,float) for negative exponents
  1079. Revision 1.23 2004/07/25 16:46:08 michael
  1080. + Implemented DivMod
  1081. Revision 1.22 2004/05/29 12:28:59 florian
  1082. * fixed IsNan and IsInf for big endian systems
  1083. Revision 1.21 2004/04/08 16:37:08 peter
  1084. * disable range,overflow check when generating Nan/Inf
  1085. Revision 1.20 2004/02/20 20:10:44 florian
  1086. + added Inf/Nan stuff
  1087. Revision 1.19 2004/02/09 18:53:09 florian
  1088. * compilation on ppc fixed
  1089. Revision 1.18 2004/02/09 17:21:04 marco
  1090. * 1.0 compilation fixes
  1091. Revision 1.17 2004/02/09 09:11:46 michael
  1092. + Implemented SameValue
  1093. Revision 1.16 2004/02/09 08:55:45 michael
  1094. + Missing functions IsZero,InRange,EnsureRange implemented
  1095. Revision 1.15 2003/11/09 21:52:54 michael
  1096. + Added missing sign functions
  1097. Revision 1.14 2003/10/29 19:10:07 jonas
  1098. * fixed arctan2
  1099. Revision 1.13 2003/10/26 15:58:05 florian
  1100. * fixed arctan2 to handle x=0 correctly as well
  1101. Revision 1.12 2003/09/01 20:46:59 peter
  1102. * small fixes for sparc
  1103. Revision 1.11 2003/04/24 09:38:12 florian
  1104. * min/max must check the compiler capabilities
  1105. Revision 1.10 2003/04/24 09:21:59 florian
  1106. + moved cpu dependend code to mathuh.inc and mathu.inc
  1107. Revision 1.9 2003/01/03 20:34:02 peter
  1108. * i386 fpu controlword functions added
  1109. Revision 1.8 2002/09/07 21:06:12 carl
  1110. * cleanup of parameters
  1111. - remove assembler code
  1112. Revision 1.7 2002/09/07 16:01:22 peter
  1113. * old logs removed and tabs fixed
  1114. }