math.inc 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875
  1. {
  2. $Id$
  3. This file is part of the Free Pascal run time library.
  4. Copyright (c) 1993,97 by xxxx
  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. { math.inc }
  14. {*************************************************************************}
  15. { Copyright Abandoned, 1987, Fred Fish }
  16. { }
  17. { This previously copyrighted work has been placed into the }
  18. { public domain by the author (Fred Fish) and may be freely used }
  19. { for any purpose, private or commercial. I would appreciate }
  20. { it, as a courtesy, if this notice is left in all copies and }
  21. { derivative works. Thank you, and enjoy... }
  22. { }
  23. { The author makes no warranty of any kind with respect to this }
  24. { product and explicitly disclaims any implied warranties of }
  25. { merchantability or fitness for any particular purpose. }
  26. {-------------------------------------------------------------------------}
  27. { Copyright (c) 1992 Odent Jean Philippe }
  28. { }
  29. { The source can be modified as long as my name appears and some }
  30. { notes explaining the modifications done are included in the file. }
  31. {-------------------------------------------------------------------------}
  32. { Copyright (c) 1997 Carl Eric Codere }
  33. { }
  34. { This include implements a template for }
  35. { all real (whatever this type maps to) and fixed point standard }
  36. { rtl routines. }
  37. { NOTE: Trunc and Int must be implemented depending on the target }
  38. { for real values.Sqrt must also be implemented for fixed. }
  39. {*************************************************************************}
  40. type
  41. TabCoef = array[0..6] of Real;
  42. const
  43. PIO2 = 1.57079632679489661923; { pi/2 }
  44. PIO4 = 7.85398163397448309616E-1; { pi/4 }
  45. SQRT2 = 1.41421356237309504880; { sqrt(2) }
  46. SQRTH = 7.07106781186547524401E-1; { sqrt(2)/2 }
  47. LOG2E = 1.4426950408889634073599; { 1/log(2) }
  48. SQ2OPI = 7.9788456080286535587989E-1; { sqrt( 2/pi )}
  49. LOGE2 = 6.93147180559945309417E-1; { log(2) }
  50. LOGSQ2 = 3.46573590279972654709E-1; { log(2)/2 }
  51. THPIO4 = 2.35619449019234492885; { 3*pi/4 }
  52. TWOOPI = 6.36619772367581343075535E-1; { 2/pi }
  53. lossth = 1.073741824e9;
  54. MAXLOG = 8.8029691931113054295988E1; { log(2**127) }
  55. MINLOG = -8.872283911167299960540E1; { log(2**-128) }
  56. DP1 = 7.85398125648498535156E-1;
  57. DP2 = 3.77489470793079817668E-8;
  58. DP3 = 2.69515142907905952645E-15;
  59. const sincof : TabCoef = (
  60. 1.58962301576546568060E-10,
  61. -2.50507477628578072866E-8,
  62. 2.75573136213857245213E-6,
  63. -1.98412698295895385996E-4,
  64. 8.33333333332211858878E-3,
  65. -1.66666666666666307295E-1, 0);
  66. coscof : TabCoef = (
  67. -1.13585365213876817300E-11,
  68. 2.08757008419747316778E-9,
  69. -2.75573141792967388112E-7,
  70. 2.48015872888517045348E-5,
  71. -1.38888888888730564116E-3,
  72. 4.16666666666665929218E-2, 0);
  73. function int(d : real) : real;
  74. { these routine should be implemented all depending on the }
  75. { target processor/operating system. }
  76. begin
  77. end;
  78. function trunc(d : real) : longint;
  79. { these routine should be implemented all depending on the }
  80. { target processor/operating system. }
  81. Begin
  82. end;
  83. function abs(d : Real) : Real;
  84. begin
  85. if( d < 0.0 ) then
  86. abs := -d
  87. else
  88. abs := d ;
  89. end;
  90. function frexp(x:Real; var e:Integer ):Real;
  91. {* frexp() extracts the exponent from x. It returns an integer *}
  92. {* power of two to expnt and the significand between 0.5 and 1 *}
  93. {* to y. Thus x = y * 2**expn. *}
  94. begin
  95. e :=0;
  96. if (abs(x)<0.5) then
  97. While (abs(x)<0.5) do
  98. begin
  99. x := x*2;
  100. Dec(e);
  101. end
  102. else
  103. While (abs(x)>1) do
  104. begin
  105. x := x/2;
  106. Inc(e);
  107. end;
  108. frexp := x;
  109. end;
  110. function ldexp( x: Real; N: Integer):Real;
  111. {* ldexp() multiplies x by 2**n. *}
  112. var r : Real;
  113. begin
  114. R := 1;
  115. if N>0 then
  116. while N>0 do
  117. begin
  118. R:=R*2;
  119. Dec(N);
  120. end
  121. else
  122. while N<0 do
  123. begin
  124. R:=R/2;
  125. Inc(N);
  126. end;
  127. ldexp := x * R;
  128. end;
  129. function polevl(var x:Real; var Coef:TabCoef; N:Integer):Real;
  130. {*****************************************************************}
  131. { Evaluate polynomial }
  132. {*****************************************************************}
  133. { }
  134. { SYNOPSIS: }
  135. { }
  136. { int N; }
  137. { double x, y, coef[N+1], polevl[]; }
  138. { }
  139. { y = polevl( x, coef, N ); }
  140. { }
  141. { DESCRIPTION: }
  142. { }
  143. { Evaluates polynomial of degree N: }
  144. { }
  145. { 2 N }
  146. { y = C + C x + C x +...+ C x }
  147. { 0 1 2 N }
  148. { }
  149. { Coefficients are stored in reverse order: }
  150. { }
  151. { coef[0] = C , ..., coef[N] = C . }
  152. { N 0 }
  153. { }
  154. { The function p1evl() assumes that coef[N] = 1.0 and is }
  155. { omitted from the array. Its calling arguments are }
  156. { otherwise the same as polevl(). }
  157. { }
  158. { SPEED: }
  159. { }
  160. { In the interest of speed, there are no checks for out }
  161. { of bounds arithmetic. This routine is used by most of }
  162. { the functions in the library. Depending on available }
  163. { equipment features, the user may wish to rewrite the }
  164. { program in microcode or assembly language. }
  165. {*****************************************************************}
  166. var ans : Real;
  167. i : Integer;
  168. begin
  169. ans := Coef[0];
  170. for i:=1 to N do
  171. ans := ans * x + Coef[i];
  172. polevl:=ans;
  173. end;
  174. function p1evl(var x:Real; var Coef:TabCoef; N:Integer):Real;
  175. { }
  176. { Evaluate polynomial when coefficient of x is 1.0. }
  177. { Otherwise same as polevl. }
  178. { }
  179. var
  180. ans : Real;
  181. i : Integer;
  182. begin
  183. ans := x + Coef[0];
  184. for i:=1 to N-1 do
  185. ans := ans * x + Coef[i];
  186. p1evl := ans;
  187. end;
  188. function sqr(d : Real) : Real;
  189. begin
  190. sqr := d*d;
  191. end;
  192. function pi : Real;
  193. begin
  194. pi := 3.1415926535897932385;
  195. end;
  196. function sqrt(x:Real):Real;
  197. {*****************************************************************}
  198. { Square root }
  199. {*****************************************************************}
  200. { }
  201. { SYNOPSIS: }
  202. { }
  203. { double x, y, sqrt(); }
  204. { }
  205. { y = sqrt( x ); }
  206. { }
  207. { DESCRIPTION: }
  208. { }
  209. { Returns the square root of x. }
  210. { }
  211. { Range reduction involves isolating the power of two of the }
  212. { argument and using a polynomial approximation to obtain }
  213. { a rough value for the square root. Then Heron's iteration }
  214. { is used three times to converge to an accurate value. }
  215. {*****************************************************************}
  216. var e : Integer;
  217. w,z : Real;
  218. begin
  219. if( x <= 0.0 ) then
  220. begin
  221. if( x < 0.0 ) then
  222. RunError(207);
  223. sqrt := 0.0;
  224. end
  225. else
  226. begin
  227. w := x;
  228. { separate exponent and significand }
  229. z := frexp( x, e );
  230. { approximate square root of number between 0.5 and 1 }
  231. { relative error of approximation = 7.47e-3 }
  232. x := 4.173075996388649989089E-1 + 5.9016206709064458299663E-1 * z;
  233. { adjust for odd powers of 2 }
  234. if odd(e) then
  235. x := x*SQRT2;
  236. { re-insert exponent }
  237. x := ldexp( x, (e div 2) );
  238. { Newton iterations: }
  239. x := 0.5*(x + w/x);
  240. x := 0.5*(x + w/x);
  241. x := 0.5*(x + w/x);
  242. x := 0.5*(x + w/x);
  243. x := 0.5*(x + w/x);
  244. x := 0.5*(x + w/x);
  245. sqrt := x;
  246. end;
  247. end;
  248. function Exp(x:Real):Real;
  249. {*****************************************************************}
  250. { Exponential Function }
  251. {*****************************************************************}
  252. { }
  253. { SYNOPSIS: }
  254. { }
  255. { double x, y, exp(); }
  256. { }
  257. { y = exp( x ); }
  258. { }
  259. { DESCRIPTION: }
  260. { }
  261. { Returns e (2.71828...) raised to the x power. }
  262. { }
  263. { Range reduction is accomplished by separating the argument }
  264. { into an integer k and fraction f such that }
  265. { }
  266. { x k f }
  267. { e = 2 e. }
  268. { }
  269. { A Pade' form of degree 2/3 is used to approximate exp(f)- 1 }
  270. { in the basic range [-0.5 ln 2, 0.5 ln 2]. }
  271. {*****************************************************************}
  272. const P : TabCoef = (
  273. 1.26183092834458542160E-4,
  274. 3.02996887658430129200E-2,
  275. 1.00000000000000000000E0, 0, 0, 0, 0);
  276. Q : TabCoef = (
  277. 3.00227947279887615146E-6,
  278. 2.52453653553222894311E-3,
  279. 2.27266044198352679519E-1,
  280. 2.00000000000000000005E0, 0 ,0 ,0);
  281. C1 = 6.9335937500000000000E-1;
  282. C2 = 2.1219444005469058277E-4;
  283. var n : Integer;
  284. px, qx, xx : Real;
  285. begin
  286. if( x > MAXLOG) then
  287. RunError(205)
  288. else
  289. if( x < MINLOG ) then
  290. begin
  291. Runerror(205);
  292. end
  293. else
  294. begin
  295. { Express e**x = e**g 2**n }
  296. { = e**g e**( n loge(2) ) }
  297. { = e**( g + n loge(2) ) }
  298. px := x * LOG2E;
  299. qx := Trunc( px + 0.5 ); { Trunc() truncates toward -infinity. }
  300. n := Trunc(qx);
  301. x := x - qx * C1;
  302. x := x + qx * C2;
  303. { rational approximation for exponential }
  304. { of the fractional part: }
  305. { e**x - 1 = 2x P(x**2)/( Q(x**2) - P(x**2) ) }
  306. xx := x * x;
  307. px := x * polevl( xx, P, 2 );
  308. x := px/( polevl( xx, Q, 3 ) - px );
  309. x := ldexp( x, 1 );
  310. x := x + 1.0;
  311. x := ldexp( x, n );
  312. Exp := x;
  313. end;
  314. end;
  315. function Round(x: Real): longint;
  316. var
  317. fr: Real;
  318. tr: Real;
  319. Begin
  320. fr := Frac(x);
  321. tr := Trunc(x);
  322. if fr > 0.5 then
  323. Round:=Trunc(x)+1
  324. else
  325. if fr < 0.5 then
  326. Round:=Trunc(x)
  327. else { fr = 0.5 }
  328. { check sign to decide ... }
  329. { as in Turbo Pascal... }
  330. if x >= 0.0 then
  331. Round := Trunc(x)+1
  332. else
  333. Round := Trunc(x);
  334. end;
  335. function Ln(x:Real):Real;
  336. {*****************************************************************}
  337. { Natural Logarithm }
  338. {*****************************************************************}
  339. { }
  340. { SYNOPSIS: }
  341. { }
  342. { double x, y, log(); }
  343. { }
  344. { y = ln( x ); }
  345. { }
  346. { DESCRIPTION: }
  347. { }
  348. { Returns the base e (2.718...) logarithm of x. }
  349. { }
  350. { The argument is separated into its exponent and fractional }
  351. { parts. If the exponent is between -1 and +1, the logarithm }
  352. { of the fraction is approximated by }
  353. { }
  354. { log(1+x) = x - 0.5 x**2 + x**3 P(x)/Q(x). }
  355. { }
  356. { Otherwise, setting z = 2(x-1)/x+1), }
  357. { }
  358. { log(x) = z + z**3 P(z)/Q(z). }
  359. { }
  360. {*****************************************************************}
  361. const P : TabCoef = (
  362. { Coefficients for log(1+x) = x - x**2/2 + x**3 P(x)/Q(x)
  363. 1/sqrt(2) <= x < sqrt(2) }
  364. 4.58482948458143443514E-5,
  365. 4.98531067254050724270E-1,
  366. 6.56312093769992875930E0,
  367. 2.97877425097986925891E1,
  368. 6.06127134467767258030E1,
  369. 5.67349287391754285487E1,
  370. 1.98892446572874072159E1);
  371. Q : TabCoef = (
  372. 1.50314182634250003249E1,
  373. 8.27410449222435217021E1,
  374. 2.20664384982121929218E2,
  375. 3.07254189979530058263E2,
  376. 2.14955586696422947765E2,
  377. 5.96677339718622216300E1, 0);
  378. { Coefficients for log(x) = z + z**3 P(z)/Q(z),
  379. where z = 2(x-1)/(x+1)
  380. 1/sqrt(2) <= x < sqrt(2) }
  381. R : TabCoef = (
  382. -7.89580278884799154124E-1,
  383. 1.63866645699558079767E1,
  384. -6.41409952958715622951E1, 0, 0, 0, 0);
  385. S : TabCoef = (
  386. -3.56722798256324312549E1,
  387. 3.12093766372244180303E2,
  388. -7.69691943550460008604E2, 0, 0, 0, 0);
  389. var e : Integer;
  390. z, y : Real;
  391. Label Ldone;
  392. begin
  393. if( x <= 0.0 ) then
  394. RunError(207);
  395. x := frexp( x, e );
  396. { logarithm using log(x) = z + z**3 P(z)/Q(z),
  397. where z = 2(x-1)/x+1) }
  398. if( (e > 2) or (e < -2) ) then
  399. begin
  400. if( x < SQRTH ) then
  401. begin
  402. { 2( 2x-1 )/( 2x+1 ) }
  403. Dec(e, 1);
  404. z := x - 0.5;
  405. y := 0.5 * z + 0.5;
  406. end
  407. else
  408. begin
  409. { 2 (x-1)/(x+1) }
  410. z := x - 0.5;
  411. z := z - 0.5;
  412. y := 0.5 * x + 0.5;
  413. end;
  414. x := z / y;
  415. { /* rational form */ }
  416. z := x*x;
  417. z := x + x * ( z * polevl( z, R, 2 ) / p1evl( z, S, 3 ) );
  418. goto ldone;
  419. end;
  420. { logarithm using log(1+x) = x - .5x**2 + x**3 P(x)/Q(x) }
  421. if( x < SQRTH ) then
  422. begin
  423. Dec(e, 1);
  424. x := ldexp( x, 1 ) - 1.0; { 2x - 1 }
  425. end
  426. else
  427. x := x - 1.0;
  428. { rational form }
  429. z := x*x;
  430. y := x * ( z * polevl( x, P, 6 ) / p1evl( x, Q, 6 ) );
  431. y := y - ldexp( z, -1 ); { y - 0.5 * z }
  432. z := x + y;
  433. ldone:
  434. { recombine with exponent term }
  435. if( e <> 0 ) then
  436. begin
  437. y := e;
  438. z := z - y * 2.121944400546905827679e-4;
  439. z := z + y * 0.693359375;
  440. end;
  441. Ln:= z;
  442. end;
  443. function Sin(x:Real):Real;
  444. {*****************************************************************}
  445. { Circular Sine }
  446. {*****************************************************************}
  447. { }
  448. { SYNOPSIS: }
  449. { }
  450. { double x, y, sin(); }
  451. { }
  452. { y = sin( x ); }
  453. { }
  454. { DESCRIPTION: }
  455. { }
  456. { Range reduction is into intervals of pi/4. The reduction }
  457. { error is nearly eliminated by contriving an extended }
  458. { precision modular arithmetic. }
  459. { }
  460. { Two polynomial approximating functions are employed. }
  461. { Between 0 and pi/4 the sine is approximated by }
  462. { x + x**3 P(x**2). }
  463. { Between pi/4 and pi/2 the cosine is represented as }
  464. { 1 - x**2 Q(x**2). }
  465. {*****************************************************************}
  466. var y, z, zz : Real;
  467. j, sign : Integer;
  468. begin
  469. { make argument positive but save the sign }
  470. sign := 1;
  471. if( x < 0 ) then
  472. begin
  473. x := -x;
  474. sign := -1;
  475. end;
  476. { above this value, approximate towards 0 }
  477. if( x > lossth ) then
  478. begin
  479. sin := 0.0;
  480. exit;
  481. end;
  482. y := Trunc( x/PIO4 ); { integer part of x/PIO4 }
  483. { strip high bits of integer part to prevent integer overflow }
  484. z := ldexp( y, -4 );
  485. z := Trunc(z); { integer part of y/8 }
  486. z := y - ldexp( z, 4 ); { y - 16 * (y/16) }
  487. j := Trunc(z); { convert to integer for tests on the phase angle }
  488. { map zeros to origin }
  489. if odd( j ) then
  490. begin
  491. inc(j);
  492. y := y + 1.0;
  493. end;
  494. j := j and 7; { octant modulo 360 degrees }
  495. { reflect in x axis }
  496. if( j > 3) then
  497. begin
  498. sign := -sign;
  499. dec(j, 4);
  500. end;
  501. { Extended precision modular arithmetic }
  502. z := ((x - y * DP1) - y * DP2) - y * DP3;
  503. zz := z * z;
  504. if( (j=1) or (j=2) ) then
  505. y := 1.0 - ldexp(zz,-1) + zz * zz * polevl( zz, coscof, 5 )
  506. else
  507. { y = z + z * (zz * polevl( zz, sincof, 5 )); }
  508. y := z + z * z * z * polevl( zz, sincof, 5 );
  509. if(sign < 0) then
  510. y := -y;
  511. sin := y;
  512. end;
  513. function frac(d : Real) : Real;
  514. begin
  515. frac := d - Int(d);
  516. end;
  517. function sqrt(d : fixed) : fixed;
  518. begin
  519. end;
  520. function Cos(x:Real):Real;
  521. {*****************************************************************}
  522. { Circular cosine }
  523. {*****************************************************************}
  524. { }
  525. { Circular cosine }
  526. { }
  527. { SYNOPSIS: }
  528. { }
  529. { double x, y, cos(); }
  530. { }
  531. { y = cos( x ); }
  532. { }
  533. { DESCRIPTION: }
  534. { }
  535. { Range reduction is into intervals of pi/4. The reduction }
  536. { error is nearly eliminated by contriving an extended }
  537. { precision modular arithmetic. }
  538. { }
  539. { Two polynomial approximating functions are employed. }
  540. { Between 0 and pi/4 the cosine is approximated by }
  541. { 1 - x**2 Q(x**2). }
  542. { Between pi/4 and pi/2 the sine is represented as }
  543. { x + x**3 P(x**2). }
  544. {*****************************************************************}
  545. var y, z, zz : Real;
  546. j, sign : Integer;
  547. i : LongInt;
  548. begin
  549. { make argument positive }
  550. sign := 1;
  551. if( x < 0 ) then
  552. x := -x;
  553. { above this value, round towards zero }
  554. if( x > lossth ) then
  555. begin
  556. cos := 0.0;
  557. exit;
  558. end;
  559. y := Trunc( x/PIO4 );
  560. z := ldexp( y, -4 );
  561. z := Trunc(z); { integer part of y/8 }
  562. z := y - ldexp( z, 4 ); { y - 16 * (y/16) }
  563. { integer and fractional part modulo one octant }
  564. i := Trunc(z);
  565. if odd( i ) then { map zeros to origin }
  566. begin
  567. inc(i);
  568. y := y + 1.0;
  569. end;
  570. j := i and 07;
  571. if( j > 3) then
  572. begin
  573. dec(j,4);
  574. sign := -sign;
  575. end;
  576. if( j > 1 ) then
  577. sign := -sign;
  578. { Extended precision modular arithmetic }
  579. z := ((x - y * DP1) - y * DP2) - y * DP3;
  580. zz := z * z;
  581. if( (j=1) or (j=2) ) then
  582. { y = z + z * (zz * polevl( zz, sincof, 5 )); }
  583. y := z + z * z * z * polevl( zz, sincof, 5 )
  584. else
  585. y := 1.0 - ldexp(zz,-1) + zz * zz * polevl( zz, coscof, 5 );
  586. if(sign < 0) then
  587. y := -y;
  588. cos := y ;
  589. end;
  590. function ArcTan(x:Real):Real;
  591. {*****************************************************************}
  592. { Inverse circular tangent (arctangent) }
  593. {*****************************************************************}
  594. { }
  595. { SYNOPSIS: }
  596. { }
  597. { double x, y, atan(); }
  598. { }
  599. { y = atan( x ); }
  600. { }
  601. { DESCRIPTION: }
  602. { }
  603. { Returns radian angle between -pi/2 and +pi/2 whose tangent }
  604. { is x. }
  605. { }
  606. { Range reduction is from four intervals into the interval }
  607. { from zero to tan( pi/8 ). The approximant uses a rational }
  608. { function of degree 3/4 of the form x + x**3 P(x)/Q(x). }
  609. {*****************************************************************}
  610. const P : TabCoef = (
  611. -8.40980878064499716001E-1,
  612. -8.83860837023772394279E0,
  613. -2.18476213081316705724E1,
  614. -1.48307050340438946993E1, 0, 0, 0);
  615. Q : TabCoef = (
  616. 1.54974124675307267552E1,
  617. 6.27906555762653017263E1,
  618. 9.22381329856214406485E1,
  619. 4.44921151021319438465E1, 0, 0, 0);
  620. { tan( 3*pi/8 ) }
  621. T3P8 = 2.41421356237309504880;
  622. { tan( pi/8 ) }
  623. TP8 = 0.41421356237309504880;
  624. var y,z : Real;
  625. Sign : Integer;
  626. begin
  627. { make argument positive and save the sign }
  628. sign := 1;
  629. if( x < 0.0 ) then
  630. begin
  631. sign := -1;
  632. x := -x;
  633. end;
  634. { range reduction }
  635. if( x > T3P8 ) then
  636. begin
  637. y := PIO2;
  638. x := -( 1.0/x );
  639. end
  640. else if( x > TP8 ) then
  641. begin
  642. y := PIO4;
  643. x := (x-1.0)/(x+1.0);
  644. end
  645. else
  646. y := 0.0;
  647. { rational form in x**2 }
  648. z := x * x;
  649. y := y + ( polevl( z, P, 3 ) / p1evl( z, Q, 4 ) ) * z * x + x;
  650. if( sign < 0 ) then
  651. y := -y;
  652. Arctan := y;
  653. end;
  654. function int(d : fixed) : fixed;
  655. {*****************************************************************}
  656. { Returns the integral part of d }
  657. {*****************************************************************}
  658. begin
  659. int:=d and $ffff0000; { keep only upper bits }
  660. end;
  661. function trunc(d : fixed) : longint;
  662. {*****************************************************************}
  663. { Returns the Truncated integral part of d }
  664. {*****************************************************************}
  665. begin
  666. trunc:=longint(integer(d shr 16)); { keep only upper 16 bits }
  667. end;
  668. function frac(d : fixed) : fixed;
  669. {*****************************************************************}
  670. { Returns the Fractional part of d }
  671. {*****************************************************************}
  672. begin
  673. frac:=d AND $ffff; { keep only decimal parts - lower 16 bits }
  674. end;
  675. function abs(d : fixed) : fixed;
  676. {*****************************************************************}
  677. { Returns the Absolute value of d }
  678. {*****************************************************************}
  679. var
  680. w: integer;
  681. begin
  682. w:=integer(d shr 16);
  683. if w < 0 then
  684. begin
  685. w:=-w; { invert sign ... }
  686. d:=d and $ffff;
  687. d:=d or (fixed(w) shl 16); { add this to fixed number ... }
  688. abs:=d;
  689. end
  690. else
  691. abs:=d; { already positive... }
  692. end;
  693. function sqr(d : fixed) : fixed;
  694. {*****************************************************************}
  695. { Returns the Absolute squared value of d }
  696. {*****************************************************************}
  697. begin
  698. {16-bit precision needed, not 32 =)}
  699. sqr := d*d;
  700. { sqr := (d SHR 8 * d) SHR 8; }
  701. end;
  702. function Round(x: fixed): longint;
  703. {*****************************************************************}
  704. { Returns the Rounded value of d as a longint }
  705. {*****************************************************************}
  706. var
  707. lowf:integer;
  708. highf:integer;
  709. begin
  710. lowf:=x and $ffff; { keep decimal part ... }
  711. highf :=integer(x shr 16);
  712. if lowf > 5 then
  713. highf:=highf+1
  714. else
  715. if lowf = 5 then
  716. begin
  717. { here we must check the sign ... }
  718. { if greater or equal to zero, then }
  719. { greater value will be found by adding }
  720. { one... }
  721. if highf >= 0 then
  722. Highf:=Highf+1;
  723. end;
  724. Round:= longint(highf);
  725. end;
  726. function power(bas,expo : real) : real;
  727. begin
  728. power:=exp(ln(bas)*expo);
  729. end;
  730. function power(bas,expo : longint) : longint;
  731. begin
  732. power:=round(exp(ln(bas)*expo));
  733. end;
  734. {
  735. $Log$
  736. Revision 1.1 1998-03-25 11:18:46 root
  737. Initial revision
  738. Revision 1.4 1998/01/26 12:02:04 michael
  739. + Added log at the end
  740. Working file: rtl/template/math.inc
  741. description:
  742. ----------------------------
  743. revision 1.3
  744. date: 1998/01/05 00:41:29; author: carl; state: Exp; lines: +835 -836
  745. * Licence problems fixed
  746. ----------------------------
  747. revision 1.2
  748. date: 1997/12/01 12:45:48; author: michael; state: Exp; lines: +14 -0
  749. + added copyright reference in header.
  750. ----------------------------
  751. revision 1.1
  752. date: 1997/11/28 16:48:02; author: carl; state: Exp;
  753. math.inc template.
  754. =============================================================================
  755. }