math.pp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643
  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_EXCEPTIONS}
  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,kurtosis : 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. begin
  266. If abs(x)>1 then InvalidArgument;
  267. artanh:=(Ln((1+x)/(1-x)))*0.5;
  268. end;
  269. function hypot(x,y : float) : float;
  270. begin
  271. hypot:=Sqrt(x*x+y*y)
  272. end;
  273. function log10(x : float) : float;
  274. begin
  275. log10:=ln(x)/ln(10);
  276. end;
  277. function log2(x : float) : float;
  278. begin
  279. log2:=ln(x)/ln(2)
  280. end;
  281. function logn(n,x : float) : float;
  282. begin
  283. if n<0 then InvalidArgument;
  284. logn:=ln(x)/ln(n);
  285. end;
  286. function lnxpi(x : float) : float;
  287. begin
  288. lnxpi:=ln(1+x);
  289. end;
  290. function power(base,exponent : float) : float;
  291. begin
  292. Power:=exp(exponent * ln (base));
  293. end;
  294. function intpower(base : float;exponent : longint) : float;
  295. var
  296. i : longint;
  297. begin
  298. i:=abs(exponent);
  299. intpower:=1.0;
  300. while i>0 do
  301. begin
  302. while (i and 1)=0 do
  303. begin
  304. i:=i shr 1;
  305. base:=sqr(base);
  306. end;
  307. i:=i-1;
  308. intpower:=intpower*base;
  309. end;
  310. if exponent<0 then
  311. intpower:=1.0/intpower;
  312. end;
  313. function ceil(x : float) : longint;
  314. begin
  315. Ceil:=Trunc(x);
  316. If Frac(x)>0 then Ceil:=Ceil+1;
  317. end;
  318. function floor(x : float) : longint;
  319. begin
  320. Floor:=Trunc(x);
  321. If frac(x)<0 then Floor:=Floor-1;
  322. end;
  323. procedure frexp(x : float;var mantissa,exponent : float);
  324. begin
  325. { !!!!!!! }
  326. end;
  327. function ldexp(x : float;p : longint) : float;
  328. begin
  329. ldexp:=x*intpower(2.0,p);
  330. end;
  331. function mean(const data : array of float) : float;
  332. begin
  333. mean:=sum(data);
  334. mean:=mean/(high(data)-low(data)+1);
  335. end;
  336. function sum(const data : array of float) : float;
  337. var
  338. i : longint;
  339. begin
  340. sum:=0.0;
  341. for i:=low(data) to high(data) do
  342. sum:=sum+data[i];
  343. end;
  344. function sumofsquares(const data : array of float) : float;
  345. var
  346. i : longint;
  347. begin
  348. sumofsquares:=0.0;
  349. for i:=low(data) to high(data) do
  350. sumofsquares:=sumofsquares+sqr(data[i]);
  351. end;
  352. procedure sumsandsquares(const data : array of float;
  353. var sum,sumofsquares : float);
  354. var
  355. i : longint;
  356. temp : float;
  357. begin
  358. sumofsquares:=0.0;
  359. sum:=0.0;
  360. for i:=low(data) to high(data) do
  361. begin
  362. temp:=data[i];
  363. sumofsquares:=sumofsquares+sqr(temp);
  364. sum:=sum+temp;
  365. end;
  366. end;
  367. function minvalue(const data : array of float) : float;
  368. var
  369. i : longint;
  370. begin
  371. { get an initial value }
  372. minvalue:=data[low(data)];
  373. for i:=low(data) to high(data) do
  374. if data[i]<minvalue then
  375. minvalue:=data[i];
  376. end;
  377. function maxvalue(const data : array of float) : float;
  378. var
  379. i : longint;
  380. begin
  381. { get an initial value }
  382. maxvalue:=data[low(data)];
  383. for i:=low(data) to high(data) do
  384. if data[i]>maxvalue then
  385. maxvalue:=data[i];
  386. end;
  387. function stddev(const data : array of float) : float;
  388. begin
  389. StdDev:=Sqrt(Variance(Data));
  390. end;
  391. procedure meanandstddev(const data : array of float;
  392. var mean,stddev : float);
  393. begin
  394. end;
  395. function variance(const data : array of float) : float;
  396. begin
  397. Variance:=TotalVariance(Data)/(High(Data)-Low(Data));
  398. end;
  399. function totalvariance(const data : array of float) : float;
  400. var S,SS : Float;
  401. begin
  402. SumsAndSquares(Data,S,SS);
  403. TotalVariance := SS-Sqr(S)/(High(Data)-Low(Data));
  404. end;
  405. function randg(mean,stddev : float) : float;
  406. Var U1,S2 : Float;
  407. begin
  408. repeat
  409. u1:= 2*random-1;
  410. S2:=Sqr(U1)+sqr(2*random-1);
  411. until s2<1;
  412. randg:=Sqrt(-2*ln(S2)/S2)*u1*stddev+Mean;
  413. end;
  414. function popnstddev(const data : array of float) : float;
  415. begin
  416. PopnStdDev:=Sqrt(PopnVariance(Data));
  417. end;
  418. function popnvariance(const data : array of float) : float;
  419. begin
  420. PopnVariance:=TotalVariance(Data)/(High(Data)-Low(Data)+1);
  421. end;
  422. procedure momentskewkurtosis(const data : array of float;
  423. var m1,m2,m3,m4,skew,kurtosis : float);
  424. Var S,SS,SC,SQ,invN,Acc,M1S,S2N,S3N,temp : Float;
  425. I : Longint;
  426. begin
  427. invN:=1.0/(High(Data)-Low(Data)+1);
  428. s:=0;
  429. ss:=0;
  430. sq:=0;
  431. sc:=0;
  432. for i:=Low(Data) to High(Data) do
  433. begin
  434. temp:=Data[i]; { faster }
  435. S:=S+temp;
  436. acc:=temp*temp;
  437. ss:=ss+acc;
  438. Acc:=acc*temp;
  439. Sc:=sc+acc;
  440. acc:=acc*temp;
  441. sq:=sq+acc;
  442. end;
  443. M1:=s*invN;
  444. M1S:=M1*M1;
  445. S2N:=SS*invN;
  446. S3N:=SC*invN;
  447. M2:=S2N-M1S;
  448. M3:=S3N-(M1*3*S2N) + 2*M1S*M1;
  449. M4:=SQ*invN - (M1 * 4 * S3N) + (M1S*6*S2N-3*Sqr(M1S));
  450. Skew:=M3*power(M2,-3/2);
  451. Kurtosis:=M4 / Sqr(M2);
  452. end;
  453. function norm(const data : array of float) : float;
  454. begin
  455. norm:=sqrt(sumofsquares(data));
  456. end;
  457. end.
  458. {
  459. $Log$
  460. Revision 1.4 1998-09-18 23:57:27 michael
  461. * Changed use_excepions to useexceptions
  462. Revision 1.3 1998/09/09 15:29:05 peter
  463. * removed some warnings
  464. Revision 1.2 1998/07/29 15:44:34 michael
  465. included sysutils and math.pp as target. They compile now.
  466. Revision 1.1.1.1 1998/03/25 11:18:49 root
  467. * Restored version
  468. Revision 1.2 1998/02/12 22:23:14 michael
  469. + All functions implemented, but untested
  470. Revision 1.1 1998/02/05 11:11:31 michael
  471. + moved to objpas directory
  472. Revision 1.3 1998/02/03 15:27:06 florian
  473. *** empty log message ***
  474. Revision 1.2 1998/02/01 23:32:37 florian
  475. + some basic statistical functions
  476. Revision 1.1 1998/02/01 22:38:31 florian
  477. + initial revision
  478. }