genmath.inc 35 KB

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