math.pp 31 KB

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