math.inc 33 KB

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