math.pp 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710
  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. ResourceString
  137. SMathError = 'Math Error : %s';
  138. SInvalidArgument = 'Invalid argument';
  139. Procedure DoMathError(Const S : String);
  140. begin
  141. Raise EMathError.CreateFmt(SMathError,[S]);
  142. end;
  143. Procedure InvalidArgument;
  144. begin
  145. DoMathError(SInvalidArgument);
  146. end;
  147. function degtorad(deg : float) : float;
  148. begin
  149. degtorad:=deg*(pi/180.0);
  150. end;
  151. function radtodeg(rad : float) : float;
  152. begin
  153. radtodeg:=rad*(180.0/pi);
  154. end;
  155. function gradtorad(grad : float) : float;
  156. begin
  157. gradtorad:=grad*(pi/200.0);
  158. end;
  159. function radtograd(rad : float) : float;
  160. begin
  161. radtograd:=rad*(200.0/pi);
  162. end;
  163. function degtograd(deg : float) : float;
  164. begin
  165. degtograd:=deg*(200.0/180.0);
  166. end;
  167. function gradtodeg(grad : float) : float;
  168. begin
  169. gradtodeg:=grad*(180.0/200.0);
  170. end;
  171. function cycletorad(cycle : float) : float;
  172. begin
  173. cycletorad:=(2*pi)*cycle;
  174. end;
  175. function radtocycle(rad : float) : float;
  176. begin
  177. { avoid division }
  178. radtocycle:=rad*(1/(2*pi));
  179. end;
  180. function tan(x : float) : float;
  181. begin
  182. Tan:=Sin(x)/Cos(x)
  183. end;
  184. function cotan(x : float) : float;
  185. begin
  186. cotan:=Cos(X)/Sin(X);
  187. end;
  188. procedure sincos(theta : float;var sinus,cosinus : float);
  189. begin
  190. {$ifndef i386}
  191. sinus:=sin(theta);
  192. cosinus:=cos(theta);
  193. {$else}
  194. asm
  195. fldl theta
  196. fsincos
  197. fwait
  198. movl cosinus,%eax
  199. fstpl (%eax)
  200. movl sinus,%eax
  201. fstpl (%eax)
  202. end;
  203. {$endif}
  204. end;
  205. { Sign, ArcSin and ArcCos from Arjan van Dijk ([email protected]) }
  206. function sign(x : float) : float;
  207. begin
  208. if x > 0 then sign := 1.0
  209. else if x < 0 then sign := -1.0
  210. else sign := 0.0;
  211. end;
  212. function arcsin(x : float) : float;
  213. begin
  214. if abs(x) > 1 then InvalidArgument
  215. else if abs(x) < 0.5 then
  216. arcsin := arctan(x/sqrt(1-sqr(x)))
  217. else
  218. arcsin := sign(x) * (pi*0.5 - arctan(sqrt(1 / sqr(x) - 1)));
  219. end;
  220. function Arccos(x : Float) : Float;
  221. begin
  222. arccos := pi*0.5 - arcsin(x);
  223. end;
  224. function arctan2( x,y : float) : float;
  225. {$ifndef i386}
  226. begin
  227. ArcTan2:=ArcTan(x/y);
  228. {$else}
  229. { without the assembler keyword, you have to store the result to }
  230. { __result at the end of the assembler block (JM) }
  231. assembler;
  232. asm
  233. fldt X
  234. fldt Y
  235. fpatan
  236. //leave
  237. // ret $20 This is wrong for 4 byte aligned OS !!
  238. {$endif}
  239. end;
  240. function cosh(x : float) : float;
  241. var
  242. temp : float;
  243. begin
  244. temp:=exp(x);
  245. cosh:=0.5*(temp+1.0/temp);
  246. end;
  247. function sinh(x : float) : float;
  248. var
  249. temp : float;
  250. begin
  251. temp:=exp(x);
  252. sinh:=0.5*(temp-1.0/temp);
  253. end;
  254. Const MaxTanh=5000; { rather arbitrary, but more or less correct }
  255. function tanh(x : float) : float;
  256. var Temp : float;
  257. begin
  258. if x>MaxTanh then exit(1.0)
  259. else if x<-MaxTanh then exit (-1.0);
  260. temp:=exp(-2*x);
  261. tanh:=(1-temp)/(1+temp)
  262. end;
  263. function arccosh(x : float) : float;
  264. begin
  265. arccosh:=arcosh(x);
  266. end;
  267. function arcsinh(x : float) : float;
  268. begin
  269. arcsinh:=arsinh(x);
  270. end;
  271. function arctanh(x : float) : float;
  272. begin
  273. if x>1 then InvalidArgument;
  274. arctanh:=artanh(x);
  275. end;
  276. function arcosh(x : float) : float;
  277. begin
  278. if x<1 then InvalidArgument;
  279. arcosh:=Ln(x+Sqrt(x*x-1));
  280. end;
  281. function arsinh(x : float) : float;
  282. begin
  283. arsinh:=Ln(x-Sqrt(1+x*x));
  284. end;
  285. function artanh(x : float) : float;
  286. begin
  287. If abs(x)>1 then InvalidArgument;
  288. artanh:=(Ln((1+x)/(1-x)))*0.5;
  289. end;
  290. function hypot(x,y : float) : float;
  291. begin
  292. hypot:=Sqrt(x*x+y*y)
  293. end;
  294. function log10(x : float) : float;
  295. begin
  296. log10:=ln(x)/ln(10);
  297. end;
  298. function log2(x : float) : float;
  299. begin
  300. log2:=ln(x)/ln(2)
  301. end;
  302. function logn(n,x : float) : float;
  303. begin
  304. if n<0 then InvalidArgument;
  305. logn:=ln(x)/ln(n);
  306. end;
  307. function lnxpi(x : float) : float;
  308. begin
  309. lnxpi:=ln(1+x);
  310. end;
  311. function power(base,exponent : float) : float;
  312. begin
  313. Power:=exp(exponent * ln (base));
  314. end;
  315. function intpower(base : float;exponent : longint) : float;
  316. var
  317. i : longint;
  318. begin
  319. i:=abs(exponent);
  320. intpower:=1.0;
  321. while i>0 do
  322. begin
  323. while (i and 1)=0 do
  324. begin
  325. i:=i shr 1;
  326. base:=sqr(base);
  327. end;
  328. i:=i-1;
  329. intpower:=intpower*base;
  330. end;
  331. if exponent<0 then
  332. intpower:=1.0/intpower;
  333. end;
  334. function ceil(x : float) : longint;
  335. begin
  336. Ceil:=Trunc(x);
  337. If Frac(x)>0 then
  338. Ceil:=Ceil+1;
  339. end;
  340. function floor(x : float) : longint;
  341. begin
  342. Floor:=Trunc(x);
  343. If Frac(x)<0 then
  344. Floor := Floor-1;
  345. end;
  346. procedure frexp(x : float;var mantissa,exponent : float);
  347. begin
  348. { !!!!!!! }
  349. end;
  350. function ldexp(x : float;p : longint) : float;
  351. begin
  352. ldexp:=x*intpower(2.0,p);
  353. end;
  354. function mean(const data : array of float) : float;
  355. begin
  356. mean:=sum(data);
  357. mean:=mean/(high(data)-low(data)+1);
  358. end;
  359. function sum(const data : array of float) : float;
  360. var
  361. i : longint;
  362. begin
  363. sum:=0.0;
  364. for i:=low(data) to high(data) do
  365. sum:=sum+data[i];
  366. end;
  367. function sumofsquares(const data : array of float) : float;
  368. var
  369. i : longint;
  370. begin
  371. sumofsquares:=0.0;
  372. for i:=low(data) to high(data) do
  373. sumofsquares:=sumofsquares+sqr(data[i]);
  374. end;
  375. procedure sumsandsquares(const data : array of float;
  376. var sum,sumofsquares : float);
  377. var
  378. i : longint;
  379. temp : float;
  380. begin
  381. sumofsquares:=0.0;
  382. sum:=0.0;
  383. for i:=low(data) to high(data) do
  384. begin
  385. temp:=data[i];
  386. sumofsquares:=sumofsquares+sqr(temp);
  387. sum:=sum+temp;
  388. end;
  389. end;
  390. function minvalue(const data : array of float) : float;
  391. var
  392. i : longint;
  393. begin
  394. { get an initial value }
  395. minvalue:=data[low(data)];
  396. for i:=low(data) to high(data) do
  397. if data[i]<minvalue then
  398. minvalue:=data[i];
  399. end;
  400. function maxvalue(const data : array of float) : float;
  401. var
  402. i : longint;
  403. begin
  404. { get an initial value }
  405. maxvalue:=data[low(data)];
  406. for i:=low(data) to high(data) do
  407. if data[i]>maxvalue then
  408. maxvalue:=data[i];
  409. end;
  410. function stddev(const data : array of float) : float;
  411. begin
  412. StdDev:=Sqrt(Variance(Data));
  413. end;
  414. procedure meanandstddev(const data : array of float;
  415. var mean,stddev : float);
  416. begin
  417. end;
  418. function variance(const data : array of float) : float;
  419. begin
  420. Variance:=TotalVariance(Data)/(High(Data)-Low(Data));
  421. end;
  422. function totalvariance(const data : array of float) : float;
  423. var S,SS : Float;
  424. begin
  425. SumsAndSquares(Data,S,SS);
  426. TotalVariance := SS-Sqr(S)/(High(Data)-Low(Data));
  427. end;
  428. function randg(mean,stddev : float) : float;
  429. Var U1,S2 : Float;
  430. begin
  431. repeat
  432. u1:= 2*random-1;
  433. S2:=Sqr(U1)+sqr(2*random-1);
  434. until s2<1;
  435. randg:=Sqrt(-2*ln(S2)/S2)*u1*stddev+Mean;
  436. end;
  437. function popnstddev(const data : array of float) : float;
  438. begin
  439. PopnStdDev:=Sqrt(PopnVariance(Data));
  440. end;
  441. function popnvariance(const data : array of float) : float;
  442. begin
  443. PopnVariance:=TotalVariance(Data)/(High(Data)-Low(Data)+1);
  444. end;
  445. procedure momentskewkurtosis(const data : array of float;
  446. var m1,m2,m3,m4,skew,kurtosis : float);
  447. Var S,SS,SC,SQ,invN,Acc,M1S,S2N,S3N,temp : Float;
  448. I : Longint;
  449. begin
  450. invN:=1.0/(High(Data)-Low(Data)+1);
  451. s:=0;
  452. ss:=0;
  453. sq:=0;
  454. sc:=0;
  455. for i:=Low(Data) to High(Data) do
  456. begin
  457. temp:=Data[i]; { faster }
  458. S:=S+temp;
  459. acc:=temp*temp;
  460. ss:=ss+acc;
  461. Acc:=acc*temp;
  462. Sc:=sc+acc;
  463. acc:=acc*temp;
  464. sq:=sq+acc;
  465. end;
  466. M1:=s*invN;
  467. M1S:=M1*M1;
  468. S2N:=SS*invN;
  469. S3N:=SC*invN;
  470. M2:=S2N-M1S;
  471. M3:=S3N-(M1*3*S2N) + 2*M1S*M1;
  472. M4:=SQ*invN - (M1 * 4 * S3N) + (M1S*6*S2N-3*Sqr(M1S));
  473. Skew:=M3*power(M2,-3/2);
  474. Kurtosis:=M4 / Sqr(M2);
  475. end;
  476. function norm(const data : array of float) : float;
  477. begin
  478. norm:=sqrt(sumofsquares(data));
  479. end;
  480. function MinIntValue(const Data: array of Integer): Integer;
  481. var
  482. I: Integer;
  483. begin
  484. Result := Data[Low(Data)];
  485. For I := Succ(Low(Data)) To High(Data) Do
  486. If Data[I] < Result Then Result := Data[I];
  487. end;
  488. function MaxIntValue(const Data: array of Integer): Integer;
  489. var
  490. I: Integer;
  491. begin
  492. Result := Data[Low(Data)];
  493. For I := Succ(Low(Data)) To High(Data) Do
  494. If Data[I] > Result Then Result := Data[I];
  495. end;
  496. function Min(Int1,Int2:Integer):Integer;
  497. begin
  498. If Int1 < Int2 Then Result := Int1
  499. Else Result := Int2;
  500. end;
  501. function Min(Int1,Int2:Cardinal):Cardinal;
  502. begin
  503. If Int1 < Int2 Then Result := Int1
  504. Else Result := Int2;
  505. end;
  506. function Max(Int1,Int2:Integer):Integer;
  507. begin
  508. If Int1 > Int2 Then Result := Int1
  509. Else Result := Int2;
  510. end;
  511. function Max(Int1,Int2:Cardinal):Cardinal;
  512. begin
  513. If Int1 > Int2 Then Result := Int1
  514. Else Result := Int2;
  515. end;
  516. end.
  517. {
  518. $Log$
  519. Revision 1.19 2000-07-04 20:53:22 michael
  520. + Exceptions now used for errors
  521. Revision 1.18 2000/04/29 10:10:51 jonas
  522. * fixed arctan2 (tbug788 now works correctly)
  523. Revision 1.17 2000/04/20 13:12:40 pierre
  524. * fix bug visible in new tests/webtbs/tbug788 file
  525. Revision 1.16 2000/04/20 08:14:27 jonas
  526. * better arcsin/arccos from Arjan van Dijk
  527. Revision 1.15 2000/02/09 16:59:32 peter
  528. * truncated log
  529. Revision 1.14 2000/01/11 21:07:33 marco
  530. * Changed some (%ebp) to real parameters
  531. Revision 1.13 2000/01/07 16:41:43 daniel
  532. * copyright 2000
  533. Revision 1.12 1999/09/21 20:47:05 florian
  534. * ceil and floor still had bugs :), hopefully it's the final fix now
  535. }