genmath.inc 33 KB

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