genmath.inc 37 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130
  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]; [public, alias: 'FPC_SQRT_REAL'];
  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. {$ifdef hascompilerproc}
  451. function fpc_sqrt_real(d:Real):Real;compilerproc; external name 'FPC_SQRT_REAL';
  452. {$endif hascompilerproc}
  453. {$endif}
  454. {$ifndef FPC_SYSTEM_HAS_EXP}
  455. function Exp(d:Real):Real;[internconst:in_const_exp];
  456. {*****************************************************************}
  457. { Exponential Function }
  458. {*****************************************************************}
  459. { }
  460. { SYNOPSIS: }
  461. { }
  462. { double x, y, exp(); }
  463. { }
  464. { y = exp( x ); }
  465. { }
  466. { DESCRIPTION: }
  467. { }
  468. { Returns e (2.71828...) raised to the x power. }
  469. { }
  470. { Range reduction is accomplished by separating the argument }
  471. { into an integer k and fraction f such that }
  472. { }
  473. { x k f }
  474. { e = 2 e. }
  475. { }
  476. { A Pade' form of degree 2/3 is used to approximate exp(f)- 1 }
  477. { in the basic range [-0.5 ln 2, 0.5 ln 2]. }
  478. {*****************************************************************}
  479. const P : TabCoef = (
  480. 1.26183092834458542160E-4,
  481. 3.02996887658430129200E-2,
  482. 1.00000000000000000000E0, 0, 0, 0, 0);
  483. Q : TabCoef = (
  484. 3.00227947279887615146E-6,
  485. 2.52453653553222894311E-3,
  486. 2.27266044198352679519E-1,
  487. 2.00000000000000000005E0, 0 ,0 ,0);
  488. C1 = 6.9335937500000000000E-1;
  489. C2 = 2.1219444005469058277E-4;
  490. var n : Integer;
  491. px, qx, xx : Real;
  492. begin
  493. if( d > MAXLOG) then
  494. RunError(205)
  495. else
  496. if( d < MINLOG ) then
  497. begin
  498. Runerror(205);
  499. end
  500. else
  501. begin
  502. { Express e**x = e**g 2**n }
  503. { = e**g e**( n loge(2) ) }
  504. { = e**( g + n loge(2) ) }
  505. px := d * LOG2E;
  506. qx := Trunc( px + 0.5 ); { Trunc() truncates toward -infinity. }
  507. n := Trunc(qx);
  508. d := d - qx * C1;
  509. d := d + qx * C2;
  510. { rational approximation for exponential }
  511. { of the fractional part: }
  512. { e**x - 1 = 2x P(x**2)/( Q(x**2) - P(x**2) ) }
  513. xx := d * d;
  514. px := d * polevl( xx, P, 2 );
  515. d := px/( polevl( xx, Q, 3 ) - px );
  516. d := ldexp( d, 1 );
  517. d := d + 1.0;
  518. d := ldexp( d, n );
  519. Exp := d;
  520. end;
  521. end;
  522. {$endif}
  523. {$ifndef FPC_SYSTEM_HAS_ROUND}
  524. {$ifdef hascompilerproc}
  525. function round(d : Real) : int64;[internconst:in_const_round, external name 'FPC_ROUND'];
  526. function fpc_round(d : Real) : int64;assembler;[public, alias:'FPC_ROUND'];{$ifdef hascompilerproc}compilerproc;{$endif hascompilerproc}
  527. {$else}
  528. function round(d : Real) : int64;assembler;[internconst:in_const_round];
  529. {$endif hascompilerproc}
  530. var
  531. fr: Real;
  532. tr: Real;
  533. Begin
  534. fr := Frac(d);
  535. tr := Trunc(d);
  536. if fr > 0.5 then
  537. Round:=Trunc(d)+1
  538. else
  539. if fr < 0.5 then
  540. Round:=Trunc(d)
  541. else { fr = 0.5 }
  542. { check sign to decide ... }
  543. { as in Turbo Pascal... }
  544. if d >= 0.0 then
  545. Round := Trunc(d)+1
  546. else
  547. Round := Trunc(d);
  548. end;
  549. {$endif}
  550. {$ifndef FPC_SYSTEM_HAS_LN}
  551. function Ln(d:Real):Real;[internconst:in_const_ln];
  552. {*****************************************************************}
  553. { Natural Logarithm }
  554. {*****************************************************************}
  555. { }
  556. { SYNOPSIS: }
  557. { }
  558. { double x, y, log(); }
  559. { }
  560. { y = ln( x ); }
  561. { }
  562. { DESCRIPTION: }
  563. { }
  564. { Returns the base e (2.718...) logarithm of x. }
  565. { }
  566. { The argument is separated into its exponent and fractional }
  567. { parts. If the exponent is between -1 and +1, the logarithm }
  568. { of the fraction is approximated by }
  569. { }
  570. { log(1+x) = x - 0.5 x**2 + x**3 P(x)/Q(x). }
  571. { }
  572. { Otherwise, setting z = 2(x-1)/x+1), }
  573. { }
  574. { log(x) = z + z**3 P(z)/Q(z). }
  575. { }
  576. {*****************************************************************}
  577. const P : TabCoef = (
  578. { Coefficients for log(1+x) = x - x**2/2 + x**3 P(x)/Q(x)
  579. 1/sqrt(2) <= x < sqrt(2) }
  580. 4.58482948458143443514E-5,
  581. 4.98531067254050724270E-1,
  582. 6.56312093769992875930E0,
  583. 2.97877425097986925891E1,
  584. 6.06127134467767258030E1,
  585. 5.67349287391754285487E1,
  586. 1.98892446572874072159E1);
  587. Q : TabCoef = (
  588. 1.50314182634250003249E1,
  589. 8.27410449222435217021E1,
  590. 2.20664384982121929218E2,
  591. 3.07254189979530058263E2,
  592. 2.14955586696422947765E2,
  593. 5.96677339718622216300E1, 0);
  594. { Coefficients for log(x) = z + z**3 P(z)/Q(z),
  595. where z = 2(x-1)/(x+1)
  596. 1/sqrt(2) <= x < sqrt(2) }
  597. R : TabCoef = (
  598. -7.89580278884799154124E-1,
  599. 1.63866645699558079767E1,
  600. -6.41409952958715622951E1, 0, 0, 0, 0);
  601. S : TabCoef = (
  602. -3.56722798256324312549E1,
  603. 3.12093766372244180303E2,
  604. -7.69691943550460008604E2, 0, 0, 0, 0);
  605. var e : Integer;
  606. z, y : Real;
  607. Label Ldone;
  608. begin
  609. if( d <= 0.0 ) then
  610. RunError(207);
  611. d := frexp( d, e );
  612. { logarithm using log(x) = z + z**3 P(z)/Q(z),
  613. where z = 2(x-1)/x+1) }
  614. if( (e > 2) or (e < -2) ) then
  615. begin
  616. if( d < SQRTH ) then
  617. begin
  618. { 2( 2x-1 )/( 2x+1 ) }
  619. Dec(e, 1);
  620. z := d - 0.5;
  621. y := 0.5 * z + 0.5;
  622. end
  623. else
  624. begin
  625. { 2 (x-1)/(x+1) }
  626. z := d - 0.5;
  627. z := z - 0.5;
  628. y := 0.5 * d + 0.5;
  629. end;
  630. d := z / y;
  631. { /* rational form */ }
  632. z := d*d;
  633. z := d + d * ( z * polevl( z, R, 2 ) / p1evl( z, S, 3 ) );
  634. goto ldone;
  635. end;
  636. { logarithm using log(1+x) = x - .5x**2 + x**3 P(x)/Q(x) }
  637. if( d < SQRTH ) then
  638. begin
  639. Dec(e, 1);
  640. d := ldexp( d, 1 ) - 1.0; { 2x - 1 }
  641. end
  642. else
  643. d := d - 1.0;
  644. { rational form }
  645. z := d*d;
  646. y := d * ( z * polevl( d, P, 6 ) / p1evl( d, Q, 6 ) );
  647. y := y - ldexp( z, -1 ); { y - 0.5 * z }
  648. z := d + y;
  649. ldone:
  650. { recombine with exponent term }
  651. if( e <> 0 ) then
  652. begin
  653. y := e;
  654. z := z - y * 2.121944400546905827679e-4;
  655. z := z + y * 0.693359375;
  656. end;
  657. Ln:= z;
  658. end;
  659. {$endif}
  660. {$ifndef FPC_SYSTEM_HAS_SIN}
  661. function Sin(d:Real):Real;[internconst:in_const_sin];
  662. {*****************************************************************}
  663. { Circular Sine }
  664. {*****************************************************************}
  665. { }
  666. { SYNOPSIS: }
  667. { }
  668. { double x, y, sin(); }
  669. { }
  670. { y = sin( x ); }
  671. { }
  672. { DESCRIPTION: }
  673. { }
  674. { Range reduction is into intervals of pi/4. The reduction }
  675. { error is nearly eliminated by contriving an extended }
  676. { precision modular arithmetic. }
  677. { }
  678. { Two polynomial approximating functions are employed. }
  679. { Between 0 and pi/4 the sine is approximated by }
  680. { x + x**3 P(x**2). }
  681. { Between pi/4 and pi/2 the cosine is represented as }
  682. { 1 - x**2 Q(x**2). }
  683. {*****************************************************************}
  684. var y, z, zz : Real;
  685. j, sign : Integer;
  686. begin
  687. { make argument positive but save the sign }
  688. sign := 1;
  689. if( d < 0 ) then
  690. begin
  691. d := -d;
  692. sign := -1;
  693. end;
  694. { above this value, approximate towards 0 }
  695. if( d > lossth ) then
  696. begin
  697. sin := 0.0;
  698. exit;
  699. end;
  700. y := Trunc( d/PIO4 ); { integer part of x/PIO4 }
  701. { strip high bits of integer part to prevent integer overflow }
  702. z := ldexp( y, -4 );
  703. z := Trunc(z); { integer part of y/8 }
  704. z := y - ldexp( z, 4 ); { y - 16 * (y/16) }
  705. j := Trunc(z); { convert to integer for tests on the phase angle }
  706. { map zeros to origin }
  707. { typecast is to avoid "can't determine which overloaded function }
  708. { to call" }
  709. if odd( longint(j) ) then
  710. begin
  711. inc(j);
  712. y := y + 1.0;
  713. end;
  714. j := j and 7; { octant modulo 360 degrees }
  715. { reflect in x axis }
  716. if( j > 3) then
  717. begin
  718. sign := -sign;
  719. dec(j, 4);
  720. end;
  721. { Extended precision modular arithmetic }
  722. z := ((d - y * DP1) - y * DP2) - y * DP3;
  723. zz := z * z;
  724. if( (j=1) or (j=2) ) then
  725. y := 1.0 - ldexp(zz,-1) + zz * zz * polevl( zz, coscof, 5 )
  726. else
  727. { y = z + z * (zz * polevl( zz, sincof, 5 )); }
  728. y := z + z * z * z * polevl( zz, sincof, 5 );
  729. if(sign < 0) then
  730. y := -y;
  731. sin := y;
  732. end;
  733. {$endif}
  734. {$ifndef FPC_SYSTEM_HAS_COS}
  735. function Cos(d:Real):Real;[internconst:in_const_cos];
  736. {*****************************************************************}
  737. { Circular cosine }
  738. {*****************************************************************}
  739. { }
  740. { Circular cosine }
  741. { }
  742. { SYNOPSIS: }
  743. { }
  744. { double x, y, cos(); }
  745. { }
  746. { y = cos( x ); }
  747. { }
  748. { DESCRIPTION: }
  749. { }
  750. { Range reduction is into intervals of pi/4. The reduction }
  751. { error is nearly eliminated by contriving an extended }
  752. { precision modular arithmetic. }
  753. { }
  754. { Two polynomial approximating functions are employed. }
  755. { Between 0 and pi/4 the cosine is approximated by }
  756. { 1 - x**2 Q(x**2). }
  757. { Between pi/4 and pi/2 the sine is represented as }
  758. { x + x**3 P(x**2). }
  759. {*****************************************************************}
  760. var y, z, zz : Real;
  761. j, sign : Integer;
  762. i : LongInt;
  763. begin
  764. { make argument positive }
  765. sign := 1;
  766. if( d < 0 ) then
  767. d := -d;
  768. { above this value, round towards zero }
  769. if( d > lossth ) then
  770. begin
  771. cos := 0.0;
  772. exit;
  773. end;
  774. y := Trunc( d/PIO4 );
  775. z := ldexp( y, -4 );
  776. z := Trunc(z); { integer part of y/8 }
  777. z := y - ldexp( z, 4 ); { y - 16 * (y/16) }
  778. { integer and fractional part modulo one octant }
  779. i := Trunc(z);
  780. if odd( i ) then { map zeros to origin }
  781. begin
  782. inc(i);
  783. y := y + 1.0;
  784. end;
  785. j := i and 07;
  786. if( j > 3) then
  787. begin
  788. dec(j,4);
  789. sign := -sign;
  790. end;
  791. if( j > 1 ) then
  792. sign := -sign;
  793. { Extended precision modular arithmetic }
  794. z := ((d - y * DP1) - y * DP2) - y * DP3;
  795. zz := z * z;
  796. if( (j=1) or (j=2) ) then
  797. { y = z + z * (zz * polevl( zz, sincof, 5 )); }
  798. y := z + z * z * z * polevl( zz, sincof, 5 )
  799. else
  800. y := 1.0 - ldexp(zz,-1) + zz * zz * polevl( zz, coscof, 5 );
  801. if(sign < 0) then
  802. y := -y;
  803. cos := y ;
  804. end;
  805. {$endif}
  806. {$ifndef FPC_SYSTEM_HAS_ARCTAN}
  807. function ArcTan(d:Real):Real;[internconst:in_const_arctan];
  808. {*****************************************************************}
  809. { Inverse circular tangent (arctangent) }
  810. {*****************************************************************}
  811. { }
  812. { SYNOPSIS: }
  813. { }
  814. { double x, y, atan(); }
  815. { }
  816. { y = atan( x ); }
  817. { }
  818. { DESCRIPTION: }
  819. { }
  820. { Returns radian angle between -pi/2 and +pi/2 whose tangent }
  821. { is x. }
  822. { }
  823. { Range reduction is from four intervals into the interval }
  824. { from zero to tan( pi/8 ). The approximant uses a rational }
  825. { function of degree 3/4 of the form x + x**3 P(x)/Q(x). }
  826. {*****************************************************************}
  827. const P : TabCoef = (
  828. -8.40980878064499716001E-1,
  829. -8.83860837023772394279E0,
  830. -2.18476213081316705724E1,
  831. -1.48307050340438946993E1, 0, 0, 0);
  832. Q : TabCoef = (
  833. 1.54974124675307267552E1,
  834. 6.27906555762653017263E1,
  835. 9.22381329856214406485E1,
  836. 4.44921151021319438465E1, 0, 0, 0);
  837. { tan( 3*pi/8 ) }
  838. T3P8 = 2.41421356237309504880;
  839. { tan( pi/8 ) }
  840. TP8 = 0.41421356237309504880;
  841. var y,z : Real;
  842. Sign : Integer;
  843. begin
  844. { make argument positive and save the sign }
  845. sign := 1;
  846. if( d < 0.0 ) then
  847. begin
  848. sign := -1;
  849. d := -d;
  850. end;
  851. { range reduction }
  852. if( d > T3P8 ) then
  853. begin
  854. y := PIO2;
  855. d := -( 1.0/d );
  856. end
  857. else if( d > TP8 ) then
  858. begin
  859. y := PIO4;
  860. d := (d-1.0)/(d+1.0);
  861. end
  862. else
  863. y := 0.0;
  864. { rational form in x**2 }
  865. z := d * d;
  866. y := y + ( polevl( z, P, 3 ) / p1evl( z, Q, 4 ) ) * z * d + d;
  867. if( sign < 0 ) then
  868. y := -y;
  869. Arctan := y;
  870. end;
  871. {$endif}
  872. {$ifndef FPC_SYSTEM_HAS_FRAC}
  873. function frac(d : Real) : Real;[internconst:in_const_frac];
  874. begin
  875. frac := d - Int(d);
  876. end;
  877. {$endif}
  878. {$ifndef FPC_SYSTEM_HAS_POWER}
  879. function power(bas,expo : real) : real;
  880. begin
  881. if bas=0.0 then
  882. begin
  883. if expo<>0.0 then
  884. power:=0.0
  885. else
  886. HandleError(207);
  887. end
  888. else if expo=0.0 then
  889. power:=1
  890. else
  891. { bas < 0 is not allowed }
  892. if bas<0.0 then
  893. handleerror(207)
  894. else
  895. power:=exp(ln(bas)*expo);
  896. end;
  897. {$endif}
  898. {$ifndef FPC_SYSTEM_HAS_POWER_INT64}
  899. function power(bas,expo : int64) : int64;
  900. begin
  901. if bas=0 then
  902. begin
  903. if expo<>0 then
  904. power:=0
  905. else
  906. HandleError(207);
  907. end
  908. else if expo=0 then
  909. power:=1
  910. else
  911. begin
  912. if bas<0 then
  913. begin
  914. if odd(expo) then
  915. power:=-round(exp(ln(-bas)*expo))
  916. else
  917. power:=round(exp(ln(-bas)*expo));
  918. end
  919. else
  920. power:=round(exp(ln(bas)*expo));
  921. end;
  922. end;
  923. {$endif}
  924. {$ifdef SUPPORT_DOUBLE}
  925. {****************************************************************************
  926. Helper routines to support old TP styled reals
  927. ****************************************************************************}
  928. {$ifndef FPC_SYSTEM_HAS_REAL2DOUBLE}
  929. function real2double(r : real48) : double;
  930. var
  931. res : array[0..7] of byte;
  932. exponent : word;
  933. begin
  934. { copy mantissa }
  935. res[0]:=0;
  936. res[1]:=r[1] shl 5;
  937. res[2]:=(r[1] shr 3) or (r[2] shl 5);
  938. res[3]:=(r[2] shr 3) or (r[3] shl 5);
  939. res[4]:=(r[3] shr 3) or (r[4] shl 5);
  940. res[5]:=(r[4] shr 3) or (r[5] and $7f) shl 5;
  941. res[6]:=(r[5] and $7f) shr 3;
  942. { copy exponent }
  943. { correct exponent: }
  944. exponent:=(word(r[0])+(1023-129));
  945. res[6]:=res[6] or ((exponent and $f) shl 4);
  946. res[7]:=exponent shr 4;
  947. { set sign }
  948. res[7]:=res[7] or (r[5] and $80);
  949. real2double:=double(res);
  950. end;
  951. {$endif FPC_SYSTEM_HAS_REAL2DOUBLE}
  952. {$endif SUPPORT_DOUBLE}
  953. {
  954. $Log$
  955. Revision 1.14 2003-05-24 13:39:32 jonas
  956. * fsqrt is an optional instruction in the ppc architecture and isn't
  957. implemented by any current ppc afaik, so use the generic sqrt routine
  958. instead (adapted so it works with compilerproc)
  959. Revision 1.13 2003/05/23 22:58:31 jonas
  960. * added longint typecase to odd(smallint_var) call to avoid overload
  961. problem
  962. Revision 1.12 2003/05/02 15:12:19 jonas
  963. - removed empty ppc-specific frac()
  964. + added correct generic frac() implementation for doubles (translated
  965. from glibc code)
  966. Revision 1.11 2003/04/23 21:28:21 peter
  967. * fpc_round added, needed for int64 currency
  968. Revision 1.10 2003/01/15 00:45:17 peter
  969. * use generic int64 power
  970. Revision 1.9 2002/10/12 20:28:49 carl
  971. * round returns int64
  972. Revision 1.8 2002/10/07 15:15:02 florian
  973. * fixed wrong commit
  974. Revision 1.7 2002/10/07 15:10:45 florian
  975. + variant wrappers for cmp operators added
  976. Revision 1.6 2002/09/07 15:07:45 peter
  977. * old logs removed and tabs fixed
  978. Revision 1.5 2002/07/28 21:39:29 florian
  979. * made abs a compiler proc if it is generic
  980. Revision 1.4 2002/07/28 20:43:48 florian
  981. * several fixes for linux/powerpc
  982. * several fixes to MT
  983. }