math.pp 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698
  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. begin
  223. {$ifndef i386}
  224. ArcTan2:=ArcTan(x/y);
  225. {$else}
  226. asm
  227. fldt X
  228. fldt Y
  229. fpatan
  230. //leave
  231. // ret $20 This is wrong for 4 byte aligned OS !!
  232. end;
  233. {$endif}
  234. end;
  235. function cosh(x : float) : float;
  236. var
  237. temp : float;
  238. begin
  239. temp:=exp(x);
  240. cosh:=0.5*(temp+1.0/temp);
  241. end;
  242. function sinh(x : float) : float;
  243. var
  244. temp : float;
  245. begin
  246. temp:=exp(x);
  247. sinh:=0.5*(temp-1.0/temp);
  248. end;
  249. Const MaxTanh=5000; { rather arbitrary, but more or less correct }
  250. function tanh(x : float) : float;
  251. var Temp : float;
  252. begin
  253. if x>MaxTanh then exit(1.0)
  254. else if x<-MaxTanh then exit (-1.0);
  255. temp:=exp(-2*x);
  256. tanh:=(1-temp)/(1+temp)
  257. end;
  258. function arccosh(x : float) : float;
  259. begin
  260. arccosh:=arcosh(x);
  261. end;
  262. function arcsinh(x : float) : float;
  263. begin
  264. arcsinh:=arsinh(x);
  265. end;
  266. function arctanh(x : float) : float;
  267. begin
  268. if x>1 then InvalidArgument;
  269. arctanh:=artanh(x);
  270. end;
  271. function arcosh(x : float) : float;
  272. begin
  273. if x<1 then InvalidArgument;
  274. arcosh:=Ln(x+Sqrt(x*x-1));
  275. end;
  276. function arsinh(x : float) : float;
  277. begin
  278. arsinh:=Ln(x-Sqrt(1+x*x));
  279. end;
  280. function artanh(x : float) : float;
  281. begin
  282. If abs(x)>1 then InvalidArgument;
  283. artanh:=(Ln((1+x)/(1-x)))*0.5;
  284. end;
  285. function hypot(x,y : float) : float;
  286. begin
  287. hypot:=Sqrt(x*x+y*y)
  288. end;
  289. function log10(x : float) : float;
  290. begin
  291. log10:=ln(x)/ln(10);
  292. end;
  293. function log2(x : float) : float;
  294. begin
  295. log2:=ln(x)/ln(2)
  296. end;
  297. function logn(n,x : float) : float;
  298. begin
  299. if n<0 then InvalidArgument;
  300. logn:=ln(x)/ln(n);
  301. end;
  302. function lnxpi(x : float) : float;
  303. begin
  304. lnxpi:=ln(1+x);
  305. end;
  306. function power(base,exponent : float) : float;
  307. begin
  308. Power:=exp(exponent * ln (base));
  309. end;
  310. function intpower(base : float;exponent : longint) : float;
  311. var
  312. i : longint;
  313. begin
  314. i:=abs(exponent);
  315. intpower:=1.0;
  316. while i>0 do
  317. begin
  318. while (i and 1)=0 do
  319. begin
  320. i:=i shr 1;
  321. base:=sqr(base);
  322. end;
  323. i:=i-1;
  324. intpower:=intpower*base;
  325. end;
  326. if exponent<0 then
  327. intpower:=1.0/intpower;
  328. end;
  329. function ceil(x : float) : longint;
  330. begin
  331. Ceil:=Trunc(x);
  332. If Frac(x)>0 then
  333. Ceil:=Ceil+1;
  334. end;
  335. function floor(x : float) : longint;
  336. begin
  337. Floor:=Trunc(x);
  338. If Frac(x)<0 then
  339. Floor := Floor-1;
  340. end;
  341. procedure frexp(x : float;var mantissa,exponent : float);
  342. begin
  343. { !!!!!!! }
  344. end;
  345. function ldexp(x : float;p : longint) : float;
  346. begin
  347. ldexp:=x*intpower(2.0,p);
  348. end;
  349. function mean(const data : array of float) : float;
  350. begin
  351. mean:=sum(data);
  352. mean:=mean/(high(data)-low(data)+1);
  353. end;
  354. function sum(const data : array of float) : float;
  355. var
  356. i : longint;
  357. begin
  358. sum:=0.0;
  359. for i:=low(data) to high(data) do
  360. sum:=sum+data[i];
  361. end;
  362. function sumofsquares(const data : array of float) : float;
  363. var
  364. i : longint;
  365. begin
  366. sumofsquares:=0.0;
  367. for i:=low(data) to high(data) do
  368. sumofsquares:=sumofsquares+sqr(data[i]);
  369. end;
  370. procedure sumsandsquares(const data : array of float;
  371. var sum,sumofsquares : float);
  372. var
  373. i : longint;
  374. temp : float;
  375. begin
  376. sumofsquares:=0.0;
  377. sum:=0.0;
  378. for i:=low(data) to high(data) do
  379. begin
  380. temp:=data[i];
  381. sumofsquares:=sumofsquares+sqr(temp);
  382. sum:=sum+temp;
  383. end;
  384. end;
  385. function minvalue(const data : array of float) : float;
  386. var
  387. i : longint;
  388. begin
  389. { get an initial value }
  390. minvalue:=data[low(data)];
  391. for i:=low(data) to high(data) do
  392. if data[i]<minvalue then
  393. minvalue:=data[i];
  394. end;
  395. function maxvalue(const data : array of float) : float;
  396. var
  397. i : longint;
  398. begin
  399. { get an initial value }
  400. maxvalue:=data[low(data)];
  401. for i:=low(data) to high(data) do
  402. if data[i]>maxvalue then
  403. maxvalue:=data[i];
  404. end;
  405. function stddev(const data : array of float) : float;
  406. begin
  407. StdDev:=Sqrt(Variance(Data));
  408. end;
  409. procedure meanandstddev(const data : array of float;
  410. var mean,stddev : float);
  411. begin
  412. end;
  413. function variance(const data : array of float) : float;
  414. begin
  415. Variance:=TotalVariance(Data)/(High(Data)-Low(Data));
  416. end;
  417. function totalvariance(const data : array of float) : float;
  418. var S,SS : Float;
  419. begin
  420. SumsAndSquares(Data,S,SS);
  421. TotalVariance := SS-Sqr(S)/(High(Data)-Low(Data));
  422. end;
  423. function randg(mean,stddev : float) : float;
  424. Var U1,S2 : Float;
  425. begin
  426. repeat
  427. u1:= 2*random-1;
  428. S2:=Sqr(U1)+sqr(2*random-1);
  429. until s2<1;
  430. randg:=Sqrt(-2*ln(S2)/S2)*u1*stddev+Mean;
  431. end;
  432. function popnstddev(const data : array of float) : float;
  433. begin
  434. PopnStdDev:=Sqrt(PopnVariance(Data));
  435. end;
  436. function popnvariance(const data : array of float) : float;
  437. begin
  438. PopnVariance:=TotalVariance(Data)/(High(Data)-Low(Data)+1);
  439. end;
  440. procedure momentskewkurtosis(const data : array of float;
  441. var m1,m2,m3,m4,skew,kurtosis : float);
  442. Var S,SS,SC,SQ,invN,Acc,M1S,S2N,S3N,temp : Float;
  443. I : Longint;
  444. begin
  445. invN:=1.0/(High(Data)-Low(Data)+1);
  446. s:=0;
  447. ss:=0;
  448. sq:=0;
  449. sc:=0;
  450. for i:=Low(Data) to High(Data) do
  451. begin
  452. temp:=Data[i]; { faster }
  453. S:=S+temp;
  454. acc:=temp*temp;
  455. ss:=ss+acc;
  456. Acc:=acc*temp;
  457. Sc:=sc+acc;
  458. acc:=acc*temp;
  459. sq:=sq+acc;
  460. end;
  461. M1:=s*invN;
  462. M1S:=M1*M1;
  463. S2N:=SS*invN;
  464. S3N:=SC*invN;
  465. M2:=S2N-M1S;
  466. M3:=S3N-(M1*3*S2N) + 2*M1S*M1;
  467. M4:=SQ*invN - (M1 * 4 * S3N) + (M1S*6*S2N-3*Sqr(M1S));
  468. Skew:=M3*power(M2,-3/2);
  469. Kurtosis:=M4 / Sqr(M2);
  470. end;
  471. function norm(const data : array of float) : float;
  472. begin
  473. norm:=sqrt(sumofsquares(data));
  474. end;
  475. function MinIntValue(const Data: array of Integer): Integer;
  476. var
  477. I: Integer;
  478. begin
  479. Result := Data[Low(Data)];
  480. For I := Succ(Low(Data)) To High(Data) Do
  481. If Data[I] < Result Then Result := Data[I];
  482. end;
  483. function MaxIntValue(const Data: array of Integer): Integer;
  484. var
  485. I: Integer;
  486. begin
  487. Result := Data[Low(Data)];
  488. For I := Succ(Low(Data)) To High(Data) Do
  489. If Data[I] > Result Then Result := Data[I];
  490. end;
  491. function Min(Int1,Int2:Integer):Integer;
  492. begin
  493. If Int1 < Int2 Then Result := Int1
  494. Else Result := Int2;
  495. end;
  496. function Min(Int1,Int2:Cardinal):Cardinal;
  497. begin
  498. If Int1 < Int2 Then Result := Int1
  499. Else Result := Int2;
  500. end;
  501. function Max(Int1,Int2:Integer):Integer;
  502. begin
  503. If Int1 > Int2 Then Result := Int1
  504. Else Result := Int2;
  505. end;
  506. function Max(Int1,Int2:Cardinal):Cardinal;
  507. begin
  508. If Int1 > Int2 Then Result := Int1
  509. Else Result := Int2;
  510. end;
  511. end.
  512. {
  513. $Log$
  514. Revision 1.17 2000-04-20 13:12:40 pierre
  515. * fix bug visible in new tests/webtbs/tbug788 file
  516. Revision 1.16 2000/04/20 08:14:27 jonas
  517. * better arcsin/arccos from Arjan van Dijk
  518. Revision 1.15 2000/02/09 16:59:32 peter
  519. * truncated log
  520. Revision 1.14 2000/01/11 21:07:33 marco
  521. * Changed some (%ebp) to real parameters
  522. Revision 1.13 2000/01/07 16:41:43 daniel
  523. * copyright 2000
  524. Revision 1.12 1999/09/21 20:47:05 florian
  525. * ceil and floor still had bugs :), hopefully it's the final fix now
  526. }