math.pp 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718
  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 lnxp1(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. Raise EInvalidArgument.Create(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 lnxp1(x : float) : float;
  308. begin
  309. if x<-1 then
  310. InvalidArgument;
  311. lnxp1:=ln(1+x);
  312. end;
  313. function power(base,exponent : float) : float;
  314. begin
  315. Power:=exp(exponent * ln (base));
  316. end;
  317. function intpower(base : float;exponent : longint) : float;
  318. var
  319. i : longint;
  320. begin
  321. i:=abs(exponent);
  322. intpower:=1.0;
  323. while i>0 do
  324. begin
  325. while (i and 1)=0 do
  326. begin
  327. i:=i shr 1;
  328. base:=sqr(base);
  329. end;
  330. i:=i-1;
  331. intpower:=intpower*base;
  332. end;
  333. if exponent<0 then
  334. intpower:=1.0/intpower;
  335. end;
  336. function ceil(x : float) : longint;
  337. begin
  338. Ceil:=Trunc(x);
  339. If Frac(x)>0 then
  340. Ceil:=Ceil+1;
  341. end;
  342. function floor(x : float) : longint;
  343. begin
  344. Floor:=Trunc(x);
  345. If Frac(x)<0 then
  346. Floor := Floor-1;
  347. end;
  348. procedure frexp(x : float;var mantissa,exponent : float);
  349. begin
  350. end;
  351. function ldexp(x : float;p : longint) : float;
  352. begin
  353. ldexp:=x*intpower(2.0,p);
  354. end;
  355. function mean(const data : array of float) : float;
  356. begin
  357. mean:=sum(data);
  358. mean:=mean/(high(data)-low(data)+1);
  359. end;
  360. function sum(const data : array of float) : float;
  361. var
  362. i : longint;
  363. begin
  364. sum:=0.0;
  365. for i:=low(data) to high(data) do
  366. sum:=sum+data[i];
  367. end;
  368. function sumofsquares(const data : array of float) : float;
  369. var
  370. i : longint;
  371. begin
  372. sumofsquares:=0.0;
  373. for i:=low(data) to high(data) do
  374. sumofsquares:=sumofsquares+sqr(data[i]);
  375. end;
  376. procedure sumsandsquares(const data : array of float;
  377. var sum,sumofsquares : float);
  378. var
  379. i : longint;
  380. temp : float;
  381. begin
  382. sumofsquares:=0.0;
  383. sum:=0.0;
  384. for i:=low(data) to high(data) do
  385. begin
  386. temp:=data[i];
  387. sumofsquares:=sumofsquares+sqr(temp);
  388. sum:=sum+temp;
  389. end;
  390. end;
  391. function minvalue(const data : array of float) : float;
  392. var
  393. i : longint;
  394. begin
  395. { get an initial value }
  396. minvalue:=data[low(data)];
  397. for i:=low(data) to high(data) do
  398. if data[i]<minvalue then
  399. minvalue:=data[i];
  400. end;
  401. function maxvalue(const data : array of float) : float;
  402. var
  403. i : longint;
  404. begin
  405. { get an initial value }
  406. maxvalue:=data[low(data)];
  407. for i:=low(data) to high(data) do
  408. if data[i]>maxvalue then
  409. maxvalue:=data[i];
  410. end;
  411. function stddev(const data : array of float) : float;
  412. begin
  413. StdDev:=Sqrt(Variance(Data));
  414. end;
  415. procedure meanandstddev(const data : array of float;
  416. var mean,stddev : float);
  417. begin
  418. end;
  419. function variance(const data : array of float) : float;
  420. begin
  421. Variance:=TotalVariance(Data)/(High(Data)-Low(Data));
  422. end;
  423. function totalvariance(const data : array of float) : float;
  424. var S,SS : Float;
  425. begin
  426. SumsAndSquares(Data,S,SS);
  427. TotalVariance := SS-Sqr(S)/(High(Data)-Low(Data));
  428. end;
  429. function randg(mean,stddev : float) : float;
  430. Var U1,S2 : Float;
  431. begin
  432. repeat
  433. u1:= 2*random-1;
  434. S2:=Sqr(U1)+sqr(2*random-1);
  435. until s2<1;
  436. randg:=Sqrt(-2*ln(S2)/S2)*u1*stddev+Mean;
  437. end;
  438. function popnstddev(const data : array of float) : float;
  439. begin
  440. PopnStdDev:=Sqrt(PopnVariance(Data));
  441. end;
  442. function popnvariance(const data : array of float) : float;
  443. begin
  444. PopnVariance:=TotalVariance(Data)/(High(Data)-Low(Data)+1);
  445. end;
  446. procedure momentskewkurtosis(const data : array of float;
  447. var m1,m2,m3,m4,skew,kurtosis : float);
  448. Var S,SS,SC,SQ,invN,Acc,M1S,S2N,S3N,temp : Float;
  449. I : Longint;
  450. begin
  451. invN:=1.0/(High(Data)-Low(Data)+1);
  452. s:=0;
  453. ss:=0;
  454. sq:=0;
  455. sc:=0;
  456. for i:=Low(Data) to High(Data) do
  457. begin
  458. temp:=Data[i]; { faster }
  459. S:=S+temp;
  460. acc:=temp*temp;
  461. ss:=ss+acc;
  462. Acc:=acc*temp;
  463. Sc:=sc+acc;
  464. acc:=acc*temp;
  465. sq:=sq+acc;
  466. end;
  467. M1:=s*invN;
  468. M1S:=M1*M1;
  469. S2N:=SS*invN;
  470. S3N:=SC*invN;
  471. M2:=S2N-M1S;
  472. M3:=S3N-(M1*3*S2N) + 2*M1S*M1;
  473. M4:=SQ*invN - (M1 * 4 * S3N) + (M1S*6*S2N-3*Sqr(M1S));
  474. Skew:=M3*power(M2,-3/2);
  475. Kurtosis:=M4 / Sqr(M2);
  476. end;
  477. function norm(const data : array of float) : float;
  478. begin
  479. norm:=sqrt(sumofsquares(data));
  480. end;
  481. function MinIntValue(const Data: array of Integer): Integer;
  482. var
  483. I: Integer;
  484. begin
  485. Result := Data[Low(Data)];
  486. For I := Succ(Low(Data)) To High(Data) Do
  487. If Data[I] < Result Then Result := Data[I];
  488. end;
  489. function MaxIntValue(const Data: array of Integer): Integer;
  490. var
  491. I: Integer;
  492. begin
  493. Result := Data[Low(Data)];
  494. For I := Succ(Low(Data)) To High(Data) Do
  495. If Data[I] > Result Then Result := Data[I];
  496. end;
  497. function Min(Int1,Int2:Integer):Integer;
  498. begin
  499. If Int1 < Int2 Then Result := Int1
  500. Else Result := Int2;
  501. end;
  502. function Min(Int1,Int2:Cardinal):Cardinal;
  503. begin
  504. If Int1 < Int2 Then Result := Int1
  505. Else Result := Int2;
  506. end;
  507. function Max(Int1,Int2:Integer):Integer;
  508. begin
  509. If Int1 > Int2 Then Result := Int1
  510. Else Result := Int2;
  511. end;
  512. function Max(Int1,Int2:Cardinal):Cardinal;
  513. begin
  514. If Int1 > Int2 Then Result := Int1
  515. Else Result := Int2;
  516. end;
  517. end.
  518. {
  519. $Log$
  520. Revision 1.21 2000-07-06 12:13:59 michael
  521. + SOme changes in error reporting
  522. Revision 1.20 2000/07/05 13:19:59 michael
  523. + Corrected arsinh function
  524. Revision 1.19 2000/07/04 20:53:22 michael
  525. + Exceptions now used for errors
  526. Revision 1.18 2000/04/29 10:10:51 jonas
  527. * fixed arctan2 (tbug788 now works correctly)
  528. Revision 1.17 2000/04/20 13:12:40 pierre
  529. * fix bug visible in new tests/webtbs/tbug788 file
  530. Revision 1.16 2000/04/20 08:14:27 jonas
  531. * better arcsin/arccos from Arjan van Dijk
  532. Revision 1.15 2000/02/09 16:59:32 peter
  533. * truncated log
  534. Revision 1.14 2000/01/11 21:07:33 marco
  535. * Changed some (%ebp) to real parameters
  536. Revision 1.13 2000/01/07 16:41:43 daniel
  537. * copyright 2000
  538. Revision 1.12 1999/09/21 20:47:05 florian
  539. * ceil and floor still had bugs :), hopefully it's the final fix now
  540. }