2
0

math.pp 30 KB

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