genmath.inc 37 KB

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