math.pp 22 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021
  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. uses
  25. sysutils;
  26. const { Ranges of the IEEE floating point types, including denormals }
  27. MinSingle = 1.5e-45;
  28. MaxSingle = 3.4e+38;
  29. MinDouble = 5.0e-324;
  30. MaxDouble = 1.7e+308;
  31. MinExtended = 3.4e-4932;
  32. MaxExtended = 1.1e+4932;
  33. MinComp = -9.223372036854775807e+18;
  34. MaxComp = 9.223372036854775807e+18;
  35. type
  36. { the original delphi functions use extended as argument, }
  37. { but I would prefer double, because 8 bytes is a very }
  38. { natural size for the processor }
  39. { WARNING : changing float type will }
  40. { break all assembler code PM }
  41. float = extended;
  42. PFloat = ^Float;
  43. PInteger = ^Integer;
  44. tpaymenttime = (ptendofperiod,ptstartofperiod);
  45. einvalidargument = class(ematherror);
  46. TValueRelationship = -1..1;
  47. const
  48. EqualsValue = 0;
  49. LessThanValue = Low(TValueRelationship);
  50. GreaterThanValue = High(TValueRelationship);
  51. { Min/max determination }
  52. function MinIntValue(const Data: array of Integer): Integer;
  53. function MaxIntValue(const Data: array of Integer): Integer;
  54. { Extra, not present in Delphi, but used frequently }
  55. function Min(a, b: Integer): Integer;
  56. function Max(a, b: Integer): Integer;
  57. function Min(a, b: Cardinal): Cardinal;
  58. function Max(a, b: Cardinal): Cardinal;
  59. function Min(a, b: Int64): Int64;
  60. function Max(a, b: Int64): Int64;
  61. function Min(a, b: Single): Single;
  62. function Max(a, b: Single): Single;
  63. function Min(a, b: Double): Double;
  64. function Max(a, b: Double): Double;
  65. function Min(a, b: Extended): Extended;
  66. function Max(a, b: Extended): Extended;
  67. { angle conversion }
  68. function degtorad(deg : float) : float;
  69. function radtodeg(rad : float) : float;
  70. function gradtorad(grad : float) : float;
  71. function radtograd(rad : float) : float;
  72. function degtograd(deg : float) : float;
  73. function gradtodeg(grad : float) : float;
  74. { one cycle are 2*Pi rad }
  75. function cycletorad(cycle : float) : float;
  76. function radtocycle(rad : float) : float;
  77. { trigoniometric functions }
  78. function tan(x : float) : float;
  79. function cotan(x : float) : float;
  80. procedure sincos(theta : float;var sinus,cosinus : float);
  81. { inverse functions }
  82. function arccos(x : float) : float;
  83. function arcsin(x : float) : float;
  84. { calculates arctan(x/y) and returns an angle in the correct quadrant }
  85. function arctan2(x,y : float) : float;
  86. { hyperbolic functions }
  87. function cosh(x : float) : float;
  88. function sinh(x : float) : float;
  89. function tanh(x : float) : float;
  90. { area functions }
  91. { delphi names: }
  92. function arccosh(x : float) : float;
  93. function arcsinh(x : float) : float;
  94. function arctanh(x : float) : float;
  95. { IMHO the function should be called as follows (FK) }
  96. function arcosh(x : float) : float;
  97. function arsinh(x : float) : float;
  98. function artanh(x : float) : float;
  99. { triangle functions }
  100. { returns the length of the hypotenuse of a right triangle }
  101. { if x and y are the other sides }
  102. function hypot(x,y : float) : float;
  103. { logarithm functions }
  104. function log10(x : float) : float;
  105. function log2(x : float) : float;
  106. function logn(n,x : float) : float;
  107. { returns natural logarithm of x+1 }
  108. function lnxp1(x : float) : float;
  109. { exponential functions }
  110. function power(base,exponent : float) : float;
  111. { base^exponent }
  112. function intpower(base : float;const exponent : Integer) : float;
  113. { number converting }
  114. { rounds x towards positive infinity }
  115. function ceil(x : float) : Integer;
  116. { rounds x towards negative infinity }
  117. function floor(x : float) : Integer;
  118. { misc. functions }
  119. { splits x into mantissa and exponent (to base 2) }
  120. procedure Frexp(X: float; var Mantissa: float; var Exponent: integer);
  121. { returns x*(2^p) }
  122. function ldexp(x : float; const p : Integer) : float;
  123. { statistical functions }
  124. function mean(const data : array of float) : float;
  125. function sum(const data : array of float) : float;
  126. function mean(const data : PFloat; Const N : longint) : float;
  127. function sum(const data : PFloat; Const N : Longint) : float;
  128. function sumofsquares(const data : array of float) : float;
  129. function sumofsquares(const data : PFloat; Const N : Integer) : float;
  130. { calculates the sum and the sum of squares of data }
  131. procedure sumsandsquares(const data : array of float;
  132. var sum,sumofsquares : float);
  133. procedure sumsandsquares(const data : PFloat; Const N : Integer;
  134. var sum,sumofsquares : float);
  135. function minvalue(const data : array of float) : float;
  136. function minvalue(const data : array of integer) : Integer;
  137. function minvalue(const data : PFloat; Const N : Integer) : float;
  138. function MinValue(const Data : PInteger; Const N : Integer): Integer;
  139. function maxvalue(const data : array of float) : float;
  140. function maxvalue(const data : array of integer) : Integer;
  141. function maxvalue(const data : PFloat; Const N : Integer) : float;
  142. function maxvalue(const data : PInteger; Const N : Integer) : Integer;
  143. { calculates the standard deviation }
  144. function stddev(const data : array of float) : float;
  145. function stddev(const data : PFloat; Const N : Integer) : float;
  146. { calculates the mean and stddev }
  147. procedure meanandstddev(const data : array of float;
  148. var mean,stddev : float);
  149. procedure meanandstddev(const data : PFloat;
  150. Const N : Longint;var mean,stddev : float);
  151. function variance(const data : array of float) : float;
  152. function totalvariance(const data : array of float) : float;
  153. function variance(const data : PFloat; Const N : Integer) : float;
  154. function totalvariance(const data : PFloat; Const N : Integer) : float;
  155. { returns random values with gaussian distribution }
  156. function randg(mean,stddev : float) : float;
  157. { I don't know what the following functions do: }
  158. function popnstddev(const data : array of float) : float;
  159. function popnstddev(const data : PFloat; Const N : Integer) : float;
  160. function popnvariance(const data : PFloat; Const N : Integer) : float;
  161. function popnvariance(const data : array of float) : float;
  162. procedure momentskewkurtosis(const data : array of float;
  163. var m1,m2,m3,m4,skew,kurtosis : float);
  164. procedure momentskewkurtosis(const data : PFloat; Const N : Integer;
  165. var m1,m2,m3,m4,skew,kurtosis : float);
  166. { geometrical function }
  167. { returns the euclidean L2 norm }
  168. function norm(const data : array of float) : float;
  169. function norm(const data : PFloat; Const N : Integer) : float;
  170. { i386 fpu control word }
  171. {$ifdef cpui386}
  172. type
  173. TFPURoundingMode = (rmNearest, rmDown, rmUp, rmTruncate);
  174. TFPUPrecisionMode = (pmSingle, pmReserved, pmDouble, pmExtended);
  175. TFPUException = (exInvalidOp, exDenormalized, exZeroDivide,
  176. exOverflow, exUnderflow, exPrecision);
  177. TFPUExceptionMask = set of TFPUException;
  178. function GetRoundMode: TFPURoundingMode;
  179. function SetRoundMode(const RoundMode: TFPURoundingMode): TFPURoundingMode;
  180. function GetPrecisionMode: TFPUPrecisionMode;
  181. function SetPrecisionMode(const Precision: TFPUPrecisionMode): TFPUPrecisionMode;
  182. function GetExceptionMask: TFPUExceptionMask;
  183. function SetExceptionMask(const Mask: TFPUExceptionMask): TFPUExceptionMask;
  184. procedure ClearExceptions(RaisePending: Boolean {$ifndef VER1_0}=true{$endif});
  185. {$endif cpui386}
  186. implementation
  187. ResourceString
  188. SMathError = 'Math Error : %s';
  189. SInvalidArgument = 'Invalid argument';
  190. Procedure DoMathError(Const S : String);
  191. begin
  192. Raise EMathError.CreateFmt(SMathError,[S]);
  193. end;
  194. Procedure InvalidArgument;
  195. begin
  196. Raise EInvalidArgument.Create(SInvalidArgument);
  197. end;
  198. function degtorad(deg : float) : float;
  199. begin
  200. degtorad:=deg*(pi/180.0);
  201. end;
  202. function radtodeg(rad : float) : float;
  203. begin
  204. radtodeg:=rad*(180.0/pi);
  205. end;
  206. function gradtorad(grad : float) : float;
  207. begin
  208. gradtorad:=grad*(pi/200.0);
  209. end;
  210. function radtograd(rad : float) : float;
  211. begin
  212. radtograd:=rad*(200.0/pi);
  213. end;
  214. function degtograd(deg : float) : float;
  215. begin
  216. degtograd:=deg*(200.0/180.0);
  217. end;
  218. function gradtodeg(grad : float) : float;
  219. begin
  220. gradtodeg:=grad*(180.0/200.0);
  221. end;
  222. function cycletorad(cycle : float) : float;
  223. begin
  224. cycletorad:=(2*pi)*cycle;
  225. end;
  226. function radtocycle(rad : float) : float;
  227. begin
  228. { avoid division }
  229. radtocycle:=rad*(1/(2*pi));
  230. end;
  231. function tan(x : float) : float;
  232. begin
  233. Tan:=Sin(x)/Cos(x)
  234. end;
  235. function cotan(x : float) : float;
  236. begin
  237. cotan:=Cos(X)/Sin(X);
  238. end;
  239. procedure sincos(theta : float;var sinus,cosinus : float);
  240. begin
  241. sinus:=sin(theta);
  242. cosinus:=cos(theta);
  243. end;
  244. { Sign, ArcSin and ArcCos from Arjan van Dijk ([email protected]) }
  245. function sign(x : float) : float;
  246. begin
  247. if x > 0 then sign := 1.0
  248. else if x < 0 then sign := -1.0
  249. else sign := 0.0;
  250. end;
  251. function arcsin(x : float) : float;
  252. begin
  253. if abs(x) > 1 then InvalidArgument
  254. else if abs(x) < 0.5 then
  255. arcsin := arctan(x/sqrt(1-sqr(x)))
  256. else
  257. arcsin := sign(x) * (pi*0.5 - arctan(sqrt(1 / sqr(x) - 1)));
  258. end;
  259. function Arccos(x : Float) : Float;
  260. begin
  261. arccos := pi*0.5 - arcsin(x);
  262. end;
  263. function arctan2( x,y : float) : float;
  264. begin
  265. ArcTan2:=ArcTan(x/y);
  266. end;
  267. function cosh(x : float) : float;
  268. var
  269. temp : float;
  270. begin
  271. temp:=exp(x);
  272. cosh:=0.5*(temp+1.0/temp);
  273. end;
  274. function sinh(x : float) : float;
  275. var
  276. temp : float;
  277. begin
  278. temp:=exp(x);
  279. sinh:=0.5*(temp-1.0/temp);
  280. end;
  281. Const MaxTanh = 5678.22249441322; // Ln(MaxExtended)/2
  282. function tanh(x : float) : float;
  283. var Temp : float;
  284. begin
  285. if x>MaxTanh then exit(1.0)
  286. else if x<-MaxTanh then exit (-1.0);
  287. temp:=exp(-2*x);
  288. tanh:=(1-temp)/(1+temp)
  289. end;
  290. function arccosh(x : float) : float;
  291. begin
  292. arccosh:=arcosh(x);
  293. end;
  294. function arcsinh(x : float) : float;
  295. begin
  296. arcsinh:=arsinh(x);
  297. end;
  298. function arctanh(x : float) : float;
  299. begin
  300. if x>1 then InvalidArgument;
  301. arctanh:=artanh(x);
  302. end;
  303. function arcosh(x : float) : float;
  304. begin
  305. if x<1 then InvalidArgument;
  306. arcosh:=Ln(x+Sqrt(x*x-1));
  307. end;
  308. function arsinh(x : float) : float;
  309. begin
  310. arsinh:=Ln(x+Sqrt(1+x*x));
  311. end;
  312. function artanh(x : float) : float;
  313. begin
  314. If abs(x)>1 then InvalidArgument;
  315. artanh:=(Ln((1+x)/(1-x)))*0.5;
  316. end;
  317. function hypot(x,y : float) : float;
  318. begin
  319. hypot:=Sqrt(x*x+y*y)
  320. end;
  321. function log10(x : float) : float;
  322. begin
  323. log10:=ln(x)/ln(10);
  324. end;
  325. function log2(x : float) : float;
  326. begin
  327. log2:=ln(x)/ln(2)
  328. end;
  329. function logn(n,x : float) : float;
  330. begin
  331. if n<0 then InvalidArgument;
  332. logn:=ln(x)/ln(n);
  333. end;
  334. function lnxp1(x : float) : float;
  335. begin
  336. if x<-1 then
  337. InvalidArgument;
  338. lnxp1:=ln(1+x);
  339. end;
  340. function power(base,exponent : float) : float;
  341. begin
  342. If Exponent=0.0 then
  343. Result:=1.0
  344. else
  345. If base>0.0 then
  346. Power:=exp(exponent * ln (base))
  347. else if base=0.0 then
  348. Result:=0.0
  349. else
  350. InvalidArgument
  351. end;
  352. function intpower(base : float;const exponent : Integer) : float;
  353. var
  354. i : longint;
  355. begin
  356. i:=abs(exponent);
  357. intpower:=1.0;
  358. while i>0 do
  359. begin
  360. while (i and 1)=0 do
  361. begin
  362. i:=i shr 1;
  363. base:=sqr(base);
  364. end;
  365. i:=i-1;
  366. intpower:=intpower*base;
  367. end;
  368. if exponent<0 then
  369. intpower:=1.0/intpower;
  370. end;
  371. function ceil(x : float) : integer;
  372. begin
  373. Ceil:=Trunc(x);
  374. If Frac(x)>0 then
  375. Ceil:=Ceil+1;
  376. end;
  377. function floor(x : float) : integer;
  378. begin
  379. Floor:=Trunc(x);
  380. If Frac(x)<0 then
  381. Floor := Floor-1;
  382. end;
  383. procedure Frexp(X: float; var Mantissa: float; var Exponent: integer);
  384. begin
  385. Exponent :=0;
  386. if (abs(x)<0.5) then
  387. While (abs(x)<0.5) do
  388. begin
  389. x := x*2;
  390. Dec(Exponent);
  391. end
  392. else
  393. While (abs(x)>1) do
  394. begin
  395. x := x/2;
  396. Inc(Exponent);
  397. end;
  398. mantissa := x;
  399. end;
  400. function ldexp(x : float;const p : Integer) : float;
  401. begin
  402. ldexp:=x*intpower(2.0,p);
  403. end;
  404. function mean(const data : array of float) : float;
  405. begin
  406. Result:=Mean(@data[0],High(Data)+1);
  407. end;
  408. function mean(const data : PFloat; Const N : longint) : float;
  409. begin
  410. mean:=sum(Data,N);
  411. mean:=mean/N;
  412. end;
  413. function sum(const data : array of float) : float;
  414. begin
  415. Result:=Sum(@Data[0],High(Data)+1);
  416. end;
  417. function sum(const data : PFloat;Const N : longint) : float;
  418. var
  419. i : longint;
  420. begin
  421. sum:=0.0;
  422. for i:=0 to N-1 do
  423. sum:=sum+data[i];
  424. end;
  425. function sumofsquares(const data : array of float) : float;
  426. begin
  427. Result:=sumofsquares(@data[0],High(Data)+1);
  428. end;
  429. function sumofsquares(const data : PFloat; Const N : Integer) : float;
  430. var
  431. i : longint;
  432. begin
  433. sumofsquares:=0.0;
  434. for i:=0 to N-1 do
  435. sumofsquares:=sumofsquares+sqr(data[i]);
  436. end;
  437. procedure sumsandsquares(const data : array of float;
  438. var sum,sumofsquares : float);
  439. begin
  440. sumsandsquares (@Data[0],High(Data)+1,Sum,sumofsquares);
  441. end;
  442. procedure sumsandsquares(const data : PFloat; Const N : Integer;
  443. var sum,sumofsquares : float);
  444. var
  445. i : Integer;
  446. temp : float;
  447. begin
  448. sumofsquares:=0.0;
  449. sum:=0.0;
  450. for i:=0 to N-1 do
  451. begin
  452. temp:=data[i];
  453. sumofsquares:=sumofsquares+sqr(temp);
  454. sum:=sum+temp;
  455. end;
  456. end;
  457. function stddev(const data : array of float) : float;
  458. begin
  459. Result:=Stddev(@Data[0],High(Data)+1)
  460. end;
  461. function stddev(const data : PFloat; Const N : Integer) : float;
  462. begin
  463. StdDev:=Sqrt(Variance(Data,N));
  464. end;
  465. procedure meanandstddev(const data : array of float;
  466. var mean,stddev : float);
  467. begin
  468. Meanandstddev(@Data[0],High(Data)+1,Mean,stddev);
  469. end;
  470. procedure meanandstddev(const data : PFloat;
  471. Const N : Longint;var mean,stddev : float);
  472. Var I : longint;
  473. begin
  474. Mean:=0;
  475. StdDev:=0;
  476. For I:=0 to N-1 do
  477. begin
  478. Mean:=Mean+Data[i];
  479. StdDev:=StdDev+Sqr(Data[i]);
  480. end;
  481. Mean:=Mean/N;
  482. StdDev:=(StdDev-N*Sqr(Mean));
  483. If N>1 then
  484. StdDev:=Sqrt(Stddev/(N-1))
  485. else
  486. StdDev:=0;
  487. end;
  488. function variance(const data : array of float) : float;
  489. begin
  490. Variance:=Variance(@Data[0],High(Data)+1);
  491. end;
  492. function variance(const data : PFloat; Const N : Integer) : float;
  493. begin
  494. If N=1 then
  495. Result:=0
  496. else
  497. Result:=TotalVariance(Data,N)/(N-1);
  498. end;
  499. function totalvariance(const data : array of float) : float;
  500. begin
  501. Result:=TotalVariance(@Data[0],High(Data)+1);
  502. end;
  503. function totalvariance(const data : Pfloat;Const N : Integer) : float;
  504. var S,SS : Float;
  505. begin
  506. If N=1 then
  507. Result:=0
  508. else
  509. begin
  510. SumsAndSquares(Data,N,S,SS);
  511. Result := SS-Sqr(S)/N;
  512. end;
  513. end;
  514. function randg(mean,stddev : float) : float;
  515. Var U1,S2 : Float;
  516. begin
  517. repeat
  518. u1:= 2*random-1;
  519. S2:=Sqr(U1)+sqr(2*random-1);
  520. until s2<1;
  521. randg:=Sqrt(-2*ln(S2)/S2)*u1*stddev+Mean;
  522. end;
  523. function popnstddev(const data : array of float) : float;
  524. begin
  525. PopnStdDev:=Sqrt(PopnVariance(@Data[0],High(Data)+1));
  526. end;
  527. function popnstddev(const data : PFloat; Const N : Integer) : float;
  528. begin
  529. PopnStdDev:=Sqrt(PopnVariance(Data,N));
  530. end;
  531. function popnvariance(const data : array of float) : float;
  532. begin
  533. popnvariance:=popnvariance(@data[0],high(Data)+1);
  534. end;
  535. function popnvariance(const data : PFloat; Const N : Integer) : float;
  536. begin
  537. PopnVariance:=TotalVariance(Data,N)/N;
  538. end;
  539. procedure momentskewkurtosis(const data : array of float;
  540. var m1,m2,m3,m4,skew,kurtosis : float);
  541. begin
  542. momentskewkurtosis(@Data[0],High(Data)+1,m1,m2,m3,m4,skew,kurtosis);
  543. end;
  544. procedure momentskewkurtosis(const data : PFloat; Const N : Integer;
  545. var m1,m2,m3,m4,skew,kurtosis : float);
  546. Var S,SS,SC,SQ,invN,Acc,M1S,S2N,S3N,temp : Float;
  547. I : Longint;
  548. begin
  549. invN:=1.0/N;
  550. s:=0;
  551. ss:=0;
  552. sq:=0;
  553. sc:=0;
  554. for i:=0 to N-1 do
  555. begin
  556. temp:=Data[i]; { faster }
  557. S:=S+temp;
  558. acc:=temp*temp;
  559. ss:=ss+acc;
  560. Acc:=acc*temp;
  561. Sc:=sc+acc;
  562. acc:=acc*temp;
  563. sq:=sq+acc;
  564. end;
  565. M1:=s*invN;
  566. M1S:=M1*M1;
  567. S2N:=SS*invN;
  568. S3N:=SC*invN;
  569. M2:=S2N-M1S;
  570. M3:=S3N-(M1*3*S2N) + 2*M1S*M1;
  571. M4:=SQ*invN - (M1 * 4 * S3N) + (M1S*6*S2N-3*Sqr(M1S));
  572. Skew:=M3*power(M2,-3/2);
  573. Kurtosis:=M4 / Sqr(M2);
  574. end;
  575. function norm(const data : array of float) : float;
  576. begin
  577. norm:=Norm(@data[0],High(Data)+1);
  578. end;
  579. function norm(const data : PFloat; Const N : Integer) : float;
  580. begin
  581. norm:=sqrt(sumofsquares(data,N));
  582. end;
  583. function MinIntValue(const Data: array of Integer): Integer;
  584. var
  585. I: Integer;
  586. begin
  587. Result := Data[Low(Data)];
  588. For I := Succ(Low(Data)) To High(Data) Do
  589. If Data[I] < Result Then Result := Data[I];
  590. end;
  591. function MinValue(const Data: array of Integer): Integer;
  592. begin
  593. Result:=MinValue(Pinteger(@Data[0]),High(Data)+1)
  594. end;
  595. function MinValue(const Data: PInteger; Const N : Integer): Integer;
  596. var
  597. I: Integer;
  598. begin
  599. Result := Data[0];
  600. For I := 1 To N-1 do
  601. If Data[I] < Result Then Result := Data[I];
  602. end;
  603. function minvalue(const data : array of float) : float;
  604. begin
  605. Result:=minvalue(PFloat(@data[0]),High(Data)+1);
  606. end;
  607. function minvalue(const data : PFloat; Const N : Integer) : float;
  608. var
  609. i : longint;
  610. begin
  611. { get an initial value }
  612. minvalue:=data[0];
  613. for i:=1 to N-1 do
  614. if data[i]<minvalue then
  615. minvalue:=data[i];
  616. end;
  617. function MaxIntValue(const Data: array of Integer): Integer;
  618. var
  619. I: Integer;
  620. begin
  621. Result := Data[Low(Data)];
  622. For I := Succ(Low(Data)) To High(Data) Do
  623. If Data[I] > Result Then Result := Data[I];
  624. end;
  625. function maxvalue(const data : array of float) : float;
  626. begin
  627. Result:=maxvalue(PFloat(@data[0]),High(Data)+1);
  628. end;
  629. function maxvalue(const data : PFloat; Const N : Integer) : float;
  630. var
  631. i : longint;
  632. begin
  633. { get an initial value }
  634. maxvalue:=data[0];
  635. for i:=1 to N-1 do
  636. if data[i]>maxvalue then
  637. maxvalue:=data[i];
  638. end;
  639. function MaxValue(const Data: array of Integer): Integer;
  640. begin
  641. Result:=MaxValue(PInteger(@Data[0]),High(Data)+1)
  642. end;
  643. function maxvalue(const data : PInteger; Const N : Integer) : Integer;
  644. var
  645. i : longint;
  646. begin
  647. { get an initial value }
  648. maxvalue:=data[0];
  649. for i:=1 to N-1 do
  650. if data[i]>maxvalue then
  651. maxvalue:=data[i];
  652. end;
  653. function Min(a, b: Integer): Integer;
  654. begin
  655. if a < b then
  656. Result := a
  657. else
  658. Result := b;
  659. end;
  660. function Max(a, b: Integer): Integer;
  661. begin
  662. if a > b then
  663. Result := a
  664. else
  665. Result := b;
  666. end;
  667. function Min(a, b: Cardinal): Cardinal;
  668. begin
  669. if a < b then
  670. Result := a
  671. else
  672. Result := b;
  673. end;
  674. function Max(a, b: Cardinal): Cardinal;
  675. begin
  676. if a > b then
  677. Result := a
  678. else
  679. Result := b;
  680. end;
  681. function Min(a, b: Int64): Int64;
  682. begin
  683. if a < b then
  684. Result := a
  685. else
  686. Result := b;
  687. end;
  688. function Max(a, b: Int64): Int64;
  689. begin
  690. if a > b then
  691. Result := a
  692. else
  693. Result := b;
  694. end;
  695. function Min(a, b: Single): Single;
  696. begin
  697. if a < b then
  698. Result := a
  699. else
  700. Result := b;
  701. end;
  702. function Max(a, b: Single): Single;
  703. begin
  704. if a > b then
  705. Result := a
  706. else
  707. Result := b;
  708. end;
  709. function Min(a, b: Double): Double;
  710. begin
  711. if a < b then
  712. Result := a
  713. else
  714. Result := b;
  715. end;
  716. function Max(a, b: Double): Double;
  717. begin
  718. if a > b then
  719. Result := a
  720. else
  721. Result := b;
  722. end;
  723. function Min(a, b: Extended): Extended;
  724. begin
  725. if a < b then
  726. Result := a
  727. else
  728. Result := b;
  729. end;
  730. function Max(a, b: Extended): Extended;
  731. begin
  732. if a > b then
  733. Result := a
  734. else
  735. Result := b;
  736. end;
  737. {$ifdef cpui386}
  738. function GetRoundMode: TFPURoundingMode;
  739. begin
  740. Result := TFPURoundingMode((Get8087CW shr 10) and 3);
  741. end;
  742. function SetRoundMode(const RoundMode: TFPURoundingMode): TFPURoundingMode;
  743. var
  744. CtlWord: Word;
  745. begin
  746. CtlWord := Get8087CW;
  747. Set8087CW((CtlWord and $F3FF) or (Ord(RoundMode) shl 10));
  748. Result := TFPURoundingMode((CtlWord shr 10) and 3);
  749. end;
  750. function GetPrecisionMode: TFPUPrecisionMode;
  751. begin
  752. Result := TFPUPrecisionMode((Get8087CW shr 8) and 3);
  753. end;
  754. function SetPrecisionMode(const Precision: TFPUPrecisionMode): TFPUPrecisionMode;
  755. var
  756. CtlWord: Word;
  757. begin
  758. CtlWord := Get8087CW;
  759. Set8087CW((CtlWord and $FCFF) or (Ord(Precision) shl 8));
  760. Result := TFPUPrecisionMode((CtlWord shr 8) and 3);
  761. end;
  762. function GetExceptionMask: TFPUExceptionMask;
  763. begin
  764. Result := TFPUExceptionMask(Get8087CW and $3F);
  765. end;
  766. function SetExceptionMask(const Mask: TFPUExceptionMask): TFPUExceptionMask;
  767. var
  768. CtlWord: Word;
  769. begin
  770. CtlWord := Get8087CW;
  771. Set8087CW( (CtlWord and $FFC0) or Byte(Longint(Mask)) );
  772. Result := TFPUExceptionMask(CtlWord and $3F);
  773. end;
  774. procedure ClearExceptions(RaisePending: Boolean);assembler;
  775. asm
  776. cmpb $0,RaisePending
  777. je .Lclear
  778. fwait
  779. .Lclear:
  780. fnclex
  781. end;
  782. {$endif cpui386}
  783. end.
  784. {
  785. $Log$
  786. Revision 1.9 2003-01-03 20:34:02 peter
  787. * i386 fpu controlword functions added
  788. Revision 1.8 2002/09/07 21:06:12 carl
  789. * cleanup of parameters
  790. - remove assembler code
  791. Revision 1.7 2002/09/07 16:01:22 peter
  792. * old logs removed and tabs fixed
  793. }