math.pp 29 KB

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