math.pp 15 KB

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