math.inc 33 KB

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