math.pp 13 KB

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