math.pp 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681
  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. About assembler usage:
  16. ----------------------
  17. I used as few as possible assembler to allow an easy port
  18. to other processors. Today, I think it's wasted time to write
  19. assembler because different versions of a family of processors
  20. need different implementations.
  21. To improve performance, I changed all integer arguments and
  22. functions results to longint, because 16 bit instructions are
  23. lethal for a modern intel processor.
  24. (FK)
  25. What's to do:
  26. o a lot of function :), search for !!!!
  27. o some statistical functions
  28. o all financial functions
  29. o optimizations
  30. }
  31. unit math;
  32. interface
  33. {$MODE objfpc}
  34. uses
  35. sysutils;
  36. type
  37. { the original delphi functions use extended as argument, }
  38. { but I would prefer double, because 8 bytes is a very }
  39. { natural size for the processor }
  40. float = extended;
  41. tpaymenttime = (ptendofperiod,ptstartofperiod);
  42. einvalidargument = class(ematherror);
  43. { Min/max determination }
  44. function MinIntValue(const Data: array of Integer): Integer;
  45. function MaxIntValue(const Data: array of Integer): Integer;
  46. { Extra, not present in Delphi, but used frequently }
  47. function Min(Int1,Int2:Integer):Integer;
  48. function Min(Int1,Int2:Cardinal):Cardinal;
  49. function Max(Int1,Int2:Integer):Integer;
  50. function Max(Int1,Int2:Cardinal):Cardinal;
  51. { angle conversion }
  52. function degtorad(deg : float) : float;
  53. function radtodeg(rad : float) : float;
  54. function gradtorad(grad : float) : float;
  55. function radtograd(rad : float) : float;
  56. function degtograd(deg : float) : float;
  57. function gradtodeg(grad : float) : float;
  58. { one cycle are 2*Pi rad }
  59. function cycletorad(cycle : float) : float;
  60. function radtocycle(rad : float) : float;
  61. { trigoniometric functions }
  62. function tan(x : float) : float;
  63. function cotan(x : float) : float;
  64. procedure sincos(theta : float;var sinus,cosinus : float);
  65. { inverse functions }
  66. function arccos(x : float) : float;
  67. function arcsin(x : float) : float;
  68. { calculates arctan(x/y) and returns an angle in the correct quadrant }
  69. function arctan2(x,y : float) : float;
  70. { hyperbolic functions }
  71. function cosh(x : float) : float;
  72. function sinh(x : float) : float;
  73. function tanh(x : float) : float;
  74. { area functions }
  75. { delphi names: }
  76. function arccosh(x : float) : float;
  77. function arcsinh(x : float) : float;
  78. function arctanh(x : float) : float;
  79. { IMHO the function should be called as follows (FK) }
  80. function arcosh(x : float) : float;
  81. function arsinh(x : float) : float;
  82. function artanh(x : float) : float;
  83. { triangle functions }
  84. { returns the length of the hypotenuse of a right triangle }
  85. { if x and y are the other sides }
  86. function hypot(x,y : float) : float;
  87. { logarithm functions }
  88. function log10(x : float) : float;
  89. function log2(x : float) : float;
  90. function logn(n,x : float) : float;
  91. { returns natural logarithm of x+1 }
  92. function lnxpi(x : float) : float;
  93. { exponential functions }
  94. function power(base,exponent : float) : float;
  95. { base^exponent }
  96. function intpower(base : float;exponent : longint) : float;
  97. { number converting }
  98. { rounds x towards positive infinity }
  99. function ceil(x : float) : longint;
  100. { rounds x towards negative infinity }
  101. function floor(x : float) : longint;
  102. { misc. functions }
  103. { splits x into mantissa and exponent (to base 2) }
  104. procedure frexp(x : float;var mantissa,exponent : float);
  105. { returns x*(2^p) }
  106. function ldexp(x : float;p : longint) : float;
  107. { statistical functions }
  108. function mean(const data : array of float) : float;
  109. function sum(const data : array of float) : float;
  110. function sumofsquares(const data : array of float) : float;
  111. { calculates the sum and the sum of squares of data }
  112. procedure sumsandsquares(const data : array of float;
  113. var sum,sumofsquares : float);
  114. function minvalue(const data : array of float) : float;
  115. function maxvalue(const data : array of float) : float;
  116. { calculates the standard deviation }
  117. function stddev(const data : array of float) : float;
  118. { calculates the mean and stddev }
  119. procedure meanandstddev(const data : array of float;
  120. var mean,stddev : float);
  121. function variance(const data : array of float) : float;
  122. function totalvariance(const data : array of float) : float;
  123. { returns random values with gaussian distribution }
  124. function randg(mean,stddev : float) : float;
  125. { I don't know what the following functions do: }
  126. function popnstddev(const data : array of float) : float;
  127. function popnvariance(const data : array of float) : float;
  128. procedure momentskewkurtosis(const data : array of float;
  129. var m1,m2,m3,m4,skew,kurtosis : float);
  130. { geometrical function }
  131. { returns the euclidean L2 norm }
  132. function norm(const data : array of float) : float;
  133. implementation
  134. Procedure DoMathError(Const S : String);
  135. begin
  136. writeln (StdErr,'Math Error : ',S);
  137. end;
  138. Procedure InvalidArgument;
  139. begin
  140. DoMathError ('Invalid argument');
  141. end;
  142. function degtorad(deg : float) : float;
  143. begin
  144. degtorad:=deg*(pi/180.0);
  145. end;
  146. function radtodeg(rad : float) : float;
  147. begin
  148. radtodeg:=rad*(180.0/pi);
  149. end;
  150. function gradtorad(grad : float) : float;
  151. begin
  152. gradtorad:=grad*(pi/200.0);
  153. end;
  154. function radtograd(rad : float) : float;
  155. begin
  156. radtograd:=rad*(200.0/pi);
  157. end;
  158. function degtograd(deg : float) : float;
  159. begin
  160. degtograd:=deg*(200.0/180.0);
  161. end;
  162. function gradtodeg(grad : float) : float;
  163. begin
  164. gradtodeg:=grad*(180.0/200.0);
  165. end;
  166. function cycletorad(cycle : float) : float;
  167. begin
  168. cycletorad:=(2*pi)*cycle;
  169. end;
  170. function radtocycle(rad : float) : float;
  171. begin
  172. { avoid division }
  173. radtocycle:=rad*(1/(2*pi));
  174. end;
  175. function tan(x : float) : float;
  176. begin
  177. Tan:=Sin(x)/Cos(x)
  178. end;
  179. function cotan(x : float) : float;
  180. begin
  181. cotan:=Cos(X)/Sin(X);
  182. end;
  183. procedure sincos(theta : float;var sinus,cosinus : float);
  184. begin
  185. {$ifndef i386}
  186. sinus:=sin(theta);
  187. cosinus:=cos(theta);
  188. {$else}
  189. asm
  190. fldl theta
  191. fsincos
  192. fwait
  193. movl cosinus,%eax
  194. fstpl (%eax)
  195. movl sinus,%eax
  196. fstpl (%eax)
  197. end;
  198. {$endif}
  199. end;
  200. function arccos(x : float) : float;
  201. { There is some discussion as to what the correct formula is
  202. for arccos and arcsin is, but I take the one from my book...}
  203. begin
  204. ArcCos:=ArcTan2(Sqrt(1-x*x),x);
  205. end;
  206. function arcsin(x : float) : float;
  207. begin
  208. ArcSin:=ArcTan2(x,Sqrt(1-x*x))
  209. end;
  210. function arctan2( x,y : float) : float;
  211. begin
  212. {$ifndef i386}
  213. ArcTan2:=ArcTan(x/y);
  214. {$else}
  215. asm
  216. fldt X
  217. fldt Y
  218. fpatan
  219. leave
  220. ret $20
  221. end;
  222. {$endif}
  223. end;
  224. function cosh(x : float) : float;
  225. var
  226. temp : float;
  227. begin
  228. temp:=exp(x);
  229. cosh:=0.5*(temp+1.0/temp);
  230. end;
  231. function sinh(x : float) : float;
  232. var
  233. temp : float;
  234. begin
  235. temp:=exp(x);
  236. sinh:=0.5*(temp-1.0/temp);
  237. end;
  238. Const MaxTanh=5000; { rather arbitrary, but more or less correct }
  239. function tanh(x : float) : float;
  240. var Temp : float;
  241. begin
  242. if x>MaxTanh then exit(1.0)
  243. else if x<-MaxTanh then exit (-1.0);
  244. temp:=exp(-2*x);
  245. tanh:=(1-temp)/(1+temp)
  246. end;
  247. function arccosh(x : float) : float;
  248. begin
  249. arccosh:=arcosh(x);
  250. end;
  251. function arcsinh(x : float) : float;
  252. begin
  253. arcsinh:=arsinh(x);
  254. end;
  255. function arctanh(x : float) : float;
  256. begin
  257. if x>1 then InvalidArgument;
  258. arctanh:=artanh(x);
  259. end;
  260. function arcosh(x : float) : float;
  261. begin
  262. if x<1 then InvalidArgument;
  263. arcosh:=Ln(x+Sqrt(x*x-1));
  264. end;
  265. function arsinh(x : float) : float;
  266. begin
  267. arsinh:=Ln(x-Sqrt(1+x*x));
  268. end;
  269. function artanh(x : float) : float;
  270. begin
  271. If abs(x)>1 then InvalidArgument;
  272. artanh:=(Ln((1+x)/(1-x)))*0.5;
  273. end;
  274. function hypot(x,y : float) : float;
  275. begin
  276. hypot:=Sqrt(x*x+y*y)
  277. end;
  278. function log10(x : float) : float;
  279. begin
  280. log10:=ln(x)/ln(10);
  281. end;
  282. function log2(x : float) : float;
  283. begin
  284. log2:=ln(x)/ln(2)
  285. end;
  286. function logn(n,x : float) : float;
  287. begin
  288. if n<0 then InvalidArgument;
  289. logn:=ln(x)/ln(n);
  290. end;
  291. function lnxpi(x : float) : float;
  292. begin
  293. lnxpi:=ln(1+x);
  294. end;
  295. function power(base,exponent : float) : float;
  296. begin
  297. Power:=exp(exponent * ln (base));
  298. end;
  299. function intpower(base : float;exponent : longint) : float;
  300. var
  301. i : longint;
  302. begin
  303. i:=abs(exponent);
  304. intpower:=1.0;
  305. while i>0 do
  306. begin
  307. while (i and 1)=0 do
  308. begin
  309. i:=i shr 1;
  310. base:=sqr(base);
  311. end;
  312. i:=i-1;
  313. intpower:=intpower*base;
  314. end;
  315. if exponent<0 then
  316. intpower:=1.0/intpower;
  317. end;
  318. function ceil(x : float) : longint;
  319. begin
  320. Ceil:=Trunc(x);
  321. If Frac(x)>0 then
  322. Ceil:=Ceil+1;
  323. end;
  324. function floor(x : float) : longint;
  325. begin
  326. Floor:=Trunc(x);
  327. If Frac(x)<0 then
  328. Floor := Floor-1;
  329. end;
  330. procedure frexp(x : float;var mantissa,exponent : float);
  331. begin
  332. { !!!!!!! }
  333. end;
  334. function ldexp(x : float;p : longint) : float;
  335. begin
  336. ldexp:=x*intpower(2.0,p);
  337. end;
  338. function mean(const data : array of float) : float;
  339. begin
  340. mean:=sum(data);
  341. mean:=mean/(high(data)-low(data)+1);
  342. end;
  343. function sum(const data : array of float) : float;
  344. var
  345. i : longint;
  346. begin
  347. sum:=0.0;
  348. for i:=low(data) to high(data) do
  349. sum:=sum+data[i];
  350. end;
  351. function sumofsquares(const data : array of float) : float;
  352. var
  353. i : longint;
  354. begin
  355. sumofsquares:=0.0;
  356. for i:=low(data) to high(data) do
  357. sumofsquares:=sumofsquares+sqr(data[i]);
  358. end;
  359. procedure sumsandsquares(const data : array of float;
  360. var sum,sumofsquares : float);
  361. var
  362. i : longint;
  363. temp : float;
  364. begin
  365. sumofsquares:=0.0;
  366. sum:=0.0;
  367. for i:=low(data) to high(data) do
  368. begin
  369. temp:=data[i];
  370. sumofsquares:=sumofsquares+sqr(temp);
  371. sum:=sum+temp;
  372. end;
  373. end;
  374. function minvalue(const data : array of float) : float;
  375. var
  376. i : longint;
  377. begin
  378. { get an initial value }
  379. minvalue:=data[low(data)];
  380. for i:=low(data) to high(data) do
  381. if data[i]<minvalue then
  382. minvalue:=data[i];
  383. end;
  384. function maxvalue(const data : array of float) : float;
  385. var
  386. i : longint;
  387. begin
  388. { get an initial value }
  389. maxvalue:=data[low(data)];
  390. for i:=low(data) to high(data) do
  391. if data[i]>maxvalue then
  392. maxvalue:=data[i];
  393. end;
  394. function stddev(const data : array of float) : float;
  395. begin
  396. StdDev:=Sqrt(Variance(Data));
  397. end;
  398. procedure meanandstddev(const data : array of float;
  399. var mean,stddev : float);
  400. begin
  401. end;
  402. function variance(const data : array of float) : float;
  403. begin
  404. Variance:=TotalVariance(Data)/(High(Data)-Low(Data));
  405. end;
  406. function totalvariance(const data : array of float) : float;
  407. var S,SS : Float;
  408. begin
  409. SumsAndSquares(Data,S,SS);
  410. TotalVariance := SS-Sqr(S)/(High(Data)-Low(Data));
  411. end;
  412. function randg(mean,stddev : float) : float;
  413. Var U1,S2 : Float;
  414. begin
  415. repeat
  416. u1:= 2*random-1;
  417. S2:=Sqr(U1)+sqr(2*random-1);
  418. until s2<1;
  419. randg:=Sqrt(-2*ln(S2)/S2)*u1*stddev+Mean;
  420. end;
  421. function popnstddev(const data : array of float) : float;
  422. begin
  423. PopnStdDev:=Sqrt(PopnVariance(Data));
  424. end;
  425. function popnvariance(const data : array of float) : float;
  426. begin
  427. PopnVariance:=TotalVariance(Data)/(High(Data)-Low(Data)+1);
  428. end;
  429. procedure momentskewkurtosis(const data : array of float;
  430. var m1,m2,m3,m4,skew,kurtosis : float);
  431. Var S,SS,SC,SQ,invN,Acc,M1S,S2N,S3N,temp : Float;
  432. I : Longint;
  433. begin
  434. invN:=1.0/(High(Data)-Low(Data)+1);
  435. s:=0;
  436. ss:=0;
  437. sq:=0;
  438. sc:=0;
  439. for i:=Low(Data) to High(Data) do
  440. begin
  441. temp:=Data[i]; { faster }
  442. S:=S+temp;
  443. acc:=temp*temp;
  444. ss:=ss+acc;
  445. Acc:=acc*temp;
  446. Sc:=sc+acc;
  447. acc:=acc*temp;
  448. sq:=sq+acc;
  449. end;
  450. M1:=s*invN;
  451. M1S:=M1*M1;
  452. S2N:=SS*invN;
  453. S3N:=SC*invN;
  454. M2:=S2N-M1S;
  455. M3:=S3N-(M1*3*S2N) + 2*M1S*M1;
  456. M4:=SQ*invN - (M1 * 4 * S3N) + (M1S*6*S2N-3*Sqr(M1S));
  457. Skew:=M3*power(M2,-3/2);
  458. Kurtosis:=M4 / Sqr(M2);
  459. end;
  460. function norm(const data : array of float) : float;
  461. begin
  462. norm:=sqrt(sumofsquares(data));
  463. end;
  464. function MinIntValue(const Data: array of Integer): Integer;
  465. var
  466. I: Integer;
  467. begin
  468. Result := Data[Low(Data)];
  469. For I := Succ(Low(Data)) To High(Data) Do
  470. If Data[I] < Result Then Result := Data[I];
  471. end;
  472. function MaxIntValue(const Data: array of Integer): Integer;
  473. var
  474. I: Integer;
  475. begin
  476. Result := Data[Low(Data)];
  477. For I := Succ(Low(Data)) To High(Data) Do
  478. If Data[I] > Result Then Result := Data[I];
  479. end;
  480. function Min(Int1,Int2:Integer):Integer;
  481. begin
  482. If Int1 < Int2 Then Result := Int1
  483. Else Result := Int2;
  484. end;
  485. function Min(Int1,Int2:Cardinal):Cardinal;
  486. begin
  487. If Int1 < Int2 Then Result := Int1
  488. Else Result := Int2;
  489. end;
  490. function Max(Int1,Int2:Integer):Integer;
  491. begin
  492. If Int1 > Int2 Then Result := Int1
  493. Else Result := Int2;
  494. end;
  495. function Max(Int1,Int2:Cardinal):Cardinal;
  496. begin
  497. If Int1 > Int2 Then Result := Int1
  498. Else Result := Int2;
  499. end;
  500. end.
  501. {
  502. $Log$
  503. Revision 1.15 2000-02-09 16:59:32 peter
  504. * truncated log
  505. Revision 1.14 2000/01/11 21:07:33 marco
  506. * Changed some (%ebp) to real parameters
  507. Revision 1.13 2000/01/07 16:41:43 daniel
  508. * copyright 2000
  509. Revision 1.12 1999/09/21 20:47:05 florian
  510. * ceil and floor still had bugs :), hopefully it's the final fix now
  511. }