genmath.inc 36 KB

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