math.pp 31 KB

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