math.pp 32 KB

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