math.pp 13 KB

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