genmath.inc 39 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224
  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. HandleError(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. HandleError(207);
  188. exit;
  189. end;
  190. End;
  191. HandleError(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. HandleError(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. HandleError(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. HandleError(205)
  496. else
  497. if( d < MINLOG ) then
  498. begin
  499. HandleError(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 := abs(Frac(d));
  536. tr := Trunc(d);
  537. if fr > 0.5 then
  538. if d >= 0 then
  539. result:=Trunc(d)+1
  540. else
  541. result:=Trunc(d)-1
  542. else
  543. if fr < 0.5 then
  544. result:=Trunc(d)
  545. else { fr = 0.5 }
  546. { check sign to decide ... }
  547. { as in Turbo Pascal... }
  548. if d >= 0.0 then
  549. result:=Trunc(d)+1
  550. else
  551. result:=Trunc(d);
  552. end;
  553. {$endif}
  554. {$ifdef FPC_CURRENCY_IS_INT64}
  555. function trunc(c : currency) : int64;
  556. type
  557. tmyrec = record
  558. i: int64;
  559. end;
  560. begin
  561. result := int64(tmyrec(c)) div 10000
  562. end;
  563. function trunc(c : comp) : int64;
  564. begin
  565. result := c
  566. end;
  567. function round(c : currency) : int64;
  568. type
  569. tmyrec = record
  570. i: int64;
  571. end;
  572. var
  573. rem, absrem: longint;
  574. begin
  575. { (int64(tmyrec(c))(+/-)5000) div 10000 can overflow }
  576. result := int64(tmyrec(c)) div 10000;
  577. rem := int64(tmyrec(c)) - result * 10000;
  578. absrem := abs(rem);
  579. if (absrem > 5000) or
  580. ((absrem = 5000) and
  581. (rem > 0)) then
  582. if (rem > 0) then
  583. inc(result)
  584. else
  585. dec(result);
  586. end;
  587. function round(c : comp) : int64;
  588. begin
  589. result := c
  590. end;
  591. {$endif FPC_CURRENCY_IS_INT64}
  592. {$ifndef FPC_SYSTEM_HAS_LN}
  593. function Ln(d:Real):Real;[internconst:in_const_ln];
  594. {*****************************************************************}
  595. { Natural Logarithm }
  596. {*****************************************************************}
  597. { }
  598. { SYNOPSIS: }
  599. { }
  600. { double x, y, log(); }
  601. { }
  602. { y = ln( x ); }
  603. { }
  604. { DESCRIPTION: }
  605. { }
  606. { Returns the base e (2.718...) logarithm of x. }
  607. { }
  608. { The argument is separated into its exponent and fractional }
  609. { parts. If the exponent is between -1 and +1, the logarithm }
  610. { of the fraction is approximated by }
  611. { }
  612. { log(1+x) = x - 0.5 x**2 + x**3 P(x)/Q(x). }
  613. { }
  614. { Otherwise, setting z = 2(x-1)/x+1), }
  615. { }
  616. { log(x) = z + z**3 P(z)/Q(z). }
  617. { }
  618. {*****************************************************************}
  619. const P : TabCoef = (
  620. { Coefficients for log(1+x) = x - x**2/2 + x**3 P(x)/Q(x)
  621. 1/sqrt(2) <= x < sqrt(2) }
  622. 4.58482948458143443514E-5,
  623. 4.98531067254050724270E-1,
  624. 6.56312093769992875930E0,
  625. 2.97877425097986925891E1,
  626. 6.06127134467767258030E1,
  627. 5.67349287391754285487E1,
  628. 1.98892446572874072159E1);
  629. Q : TabCoef = (
  630. 1.50314182634250003249E1,
  631. 8.27410449222435217021E1,
  632. 2.20664384982121929218E2,
  633. 3.07254189979530058263E2,
  634. 2.14955586696422947765E2,
  635. 5.96677339718622216300E1, 0);
  636. { Coefficients for log(x) = z + z**3 P(z)/Q(z),
  637. where z = 2(x-1)/(x+1)
  638. 1/sqrt(2) <= x < sqrt(2) }
  639. R : TabCoef = (
  640. -7.89580278884799154124E-1,
  641. 1.63866645699558079767E1,
  642. -6.41409952958715622951E1, 0, 0, 0, 0);
  643. S : TabCoef = (
  644. -3.56722798256324312549E1,
  645. 3.12093766372244180303E2,
  646. -7.69691943550460008604E2, 0, 0, 0, 0);
  647. var e : Integer;
  648. z, y : Real;
  649. Label Ldone;
  650. begin
  651. if( d <= 0.0 ) then
  652. HandleError(207);
  653. d := frexp( d, e );
  654. { logarithm using log(x) = z + z**3 P(z)/Q(z),
  655. where z = 2(x-1)/x+1) }
  656. if( (e > 2) or (e < -2) ) then
  657. begin
  658. if( d < SQRTH ) then
  659. begin
  660. { 2( 2x-1 )/( 2x+1 ) }
  661. Dec(e, 1);
  662. z := d - 0.5;
  663. y := 0.5 * z + 0.5;
  664. end
  665. else
  666. begin
  667. { 2 (x-1)/(x+1) }
  668. z := d - 0.5;
  669. z := z - 0.5;
  670. y := 0.5 * d + 0.5;
  671. end;
  672. d := z / y;
  673. { /* rational form */ }
  674. z := d*d;
  675. z := d + d * ( z * polevl( z, R, 2 ) / p1evl( z, S, 3 ) );
  676. goto ldone;
  677. end;
  678. { logarithm using log(1+x) = x - .5x**2 + x**3 P(x)/Q(x) }
  679. if( d < SQRTH ) then
  680. begin
  681. Dec(e, 1);
  682. d := ldexp( d, 1 ) - 1.0; { 2x - 1 }
  683. end
  684. else
  685. d := d - 1.0;
  686. { rational form }
  687. z := d*d;
  688. y := d * ( z * polevl( d, P, 6 ) / p1evl( d, Q, 6 ) );
  689. y := y - ldexp( z, -1 ); { y - 0.5 * z }
  690. z := d + y;
  691. ldone:
  692. { recombine with exponent term }
  693. if( e <> 0 ) then
  694. begin
  695. y := e;
  696. z := z - y * 2.121944400546905827679e-4;
  697. z := z + y * 0.693359375;
  698. end;
  699. Ln:= z;
  700. end;
  701. {$endif}
  702. {$ifndef FPC_SYSTEM_HAS_SIN}
  703. function Sin(d:Real):Real;[internconst:in_const_sin];
  704. {*****************************************************************}
  705. { Circular Sine }
  706. {*****************************************************************}
  707. { }
  708. { SYNOPSIS: }
  709. { }
  710. { double x, y, sin(); }
  711. { }
  712. { y = sin( x ); }
  713. { }
  714. { DESCRIPTION: }
  715. { }
  716. { Range reduction is into intervals of pi/4. The reduction }
  717. { error is nearly eliminated by contriving an extended }
  718. { precision modular arithmetic. }
  719. { }
  720. { Two polynomial approximating functions are employed. }
  721. { Between 0 and pi/4 the sine is approximated by }
  722. { x + x**3 P(x**2). }
  723. { Between pi/4 and pi/2 the cosine is represented as }
  724. { 1 - x**2 Q(x**2). }
  725. {*****************************************************************}
  726. var y, z, zz : Real;
  727. j, sign : Integer;
  728. begin
  729. { make argument positive but save the sign }
  730. sign := 1;
  731. if( d < 0 ) then
  732. begin
  733. d := -d;
  734. sign := -1;
  735. end;
  736. { above this value, approximate towards 0 }
  737. if( d > lossth ) then
  738. begin
  739. sin := 0.0;
  740. exit;
  741. end;
  742. y := Trunc( d/PIO4 ); { integer part of x/PIO4 }
  743. { strip high bits of integer part to prevent integer overflow }
  744. z := ldexp( y, -4 );
  745. z := Trunc(z); { integer part of y/8 }
  746. z := y - ldexp( z, 4 ); { y - 16 * (y/16) }
  747. j := Trunc(z); { convert to integer for tests on the phase angle }
  748. { map zeros to origin }
  749. { typecast is to avoid "can't determine which overloaded function }
  750. { to call" }
  751. if odd( longint(j) ) then
  752. begin
  753. inc(j);
  754. y := y + 1.0;
  755. end;
  756. j := j and 7; { octant modulo 360 degrees }
  757. { reflect in x axis }
  758. if( j > 3) then
  759. begin
  760. sign := -sign;
  761. dec(j, 4);
  762. end;
  763. { Extended precision modular arithmetic }
  764. z := ((d - y * DP1) - y * DP2) - y * DP3;
  765. zz := z * z;
  766. if( (j=1) or (j=2) ) then
  767. y := 1.0 - ldexp(zz,-1) + zz * zz * polevl( zz, coscof, 5 )
  768. else
  769. { y = z + z * (zz * polevl( zz, sincof, 5 )); }
  770. y := z + z * z * z * polevl( zz, sincof, 5 );
  771. if(sign < 0) then
  772. y := -y;
  773. sin := y;
  774. end;
  775. {$endif}
  776. {$ifndef FPC_SYSTEM_HAS_COS}
  777. function Cos(d:Real):Real;[internconst:in_const_cos];
  778. {*****************************************************************}
  779. { Circular cosine }
  780. {*****************************************************************}
  781. { }
  782. { Circular cosine }
  783. { }
  784. { SYNOPSIS: }
  785. { }
  786. { double x, y, cos(); }
  787. { }
  788. { y = cos( x ); }
  789. { }
  790. { DESCRIPTION: }
  791. { }
  792. { Range reduction is into intervals of pi/4. The reduction }
  793. { error is nearly eliminated by contriving an extended }
  794. { precision modular arithmetic. }
  795. { }
  796. { Two polynomial approximating functions are employed. }
  797. { Between 0 and pi/4 the cosine is approximated by }
  798. { 1 - x**2 Q(x**2). }
  799. { Between pi/4 and pi/2 the sine is represented as }
  800. { x + x**3 P(x**2). }
  801. {*****************************************************************}
  802. var y, z, zz : Real;
  803. j, sign : Integer;
  804. i : LongInt;
  805. begin
  806. { make argument positive }
  807. sign := 1;
  808. if( d < 0 ) then
  809. d := -d;
  810. { above this value, round towards zero }
  811. if( d > lossth ) then
  812. begin
  813. cos := 0.0;
  814. exit;
  815. end;
  816. y := Trunc( d/PIO4 );
  817. z := ldexp( y, -4 );
  818. z := Trunc(z); { integer part of y/8 }
  819. z := y - ldexp( z, 4 ); { y - 16 * (y/16) }
  820. { integer and fractional part modulo one octant }
  821. i := Trunc(z);
  822. if odd( i ) then { map zeros to origin }
  823. begin
  824. inc(i);
  825. y := y + 1.0;
  826. end;
  827. j := i and 07;
  828. if( j > 3) then
  829. begin
  830. dec(j,4);
  831. sign := -sign;
  832. end;
  833. if( j > 1 ) then
  834. sign := -sign;
  835. { Extended precision modular arithmetic }
  836. z := ((d - y * DP1) - y * DP2) - y * DP3;
  837. zz := z * z;
  838. if( (j=1) or (j=2) ) then
  839. { y = z + z * (zz * polevl( zz, sincof, 5 )); }
  840. y := z + z * z * z * polevl( zz, sincof, 5 )
  841. else
  842. y := 1.0 - ldexp(zz,-1) + zz * zz * polevl( zz, coscof, 5 );
  843. if(sign < 0) then
  844. y := -y;
  845. cos := y ;
  846. end;
  847. {$endif}
  848. {$ifndef FPC_SYSTEM_HAS_ARCTAN}
  849. function ArcTan(d:Real):Real;[internconst:in_const_arctan];
  850. {*****************************************************************}
  851. { Inverse circular tangent (arctangent) }
  852. {*****************************************************************}
  853. { }
  854. { SYNOPSIS: }
  855. { }
  856. { double x, y, atan(); }
  857. { }
  858. { y = atan( x ); }
  859. { }
  860. { DESCRIPTION: }
  861. { }
  862. { Returns radian angle between -pi/2 and +pi/2 whose tangent }
  863. { is x. }
  864. { }
  865. { Range reduction is from four intervals into the interval }
  866. { from zero to tan( pi/8 ). The approximant uses a rational }
  867. { function of degree 3/4 of the form x + x**3 P(x)/Q(x). }
  868. {*****************************************************************}
  869. const P : TabCoef = (
  870. -8.40980878064499716001E-1,
  871. -8.83860837023772394279E0,
  872. -2.18476213081316705724E1,
  873. -1.48307050340438946993E1, 0, 0, 0);
  874. Q : TabCoef = (
  875. 1.54974124675307267552E1,
  876. 6.27906555762653017263E1,
  877. 9.22381329856214406485E1,
  878. 4.44921151021319438465E1, 0, 0, 0);
  879. { tan( 3*pi/8 ) }
  880. T3P8 = 2.41421356237309504880;
  881. { tan( pi/8 ) }
  882. TP8 = 0.41421356237309504880;
  883. var y,z : Real;
  884. Sign : Integer;
  885. begin
  886. { make argument positive and save the sign }
  887. sign := 1;
  888. if( d < 0.0 ) then
  889. begin
  890. sign := -1;
  891. d := -d;
  892. end;
  893. { range reduction }
  894. if( d > T3P8 ) then
  895. begin
  896. y := PIO2;
  897. d := -( 1.0/d );
  898. end
  899. else if( d > TP8 ) then
  900. begin
  901. y := PIO4;
  902. d := (d-1.0)/(d+1.0);
  903. end
  904. else
  905. y := 0.0;
  906. { rational form in x**2 }
  907. z := d * d;
  908. y := y + ( polevl( z, P, 3 ) / p1evl( z, Q, 4 ) ) * z * d + d;
  909. if( sign < 0 ) then
  910. y := -y;
  911. Arctan := y;
  912. end;
  913. {$endif}
  914. {$ifndef FPC_SYSTEM_HAS_FRAC}
  915. function frac(d : Real) : Real;[internconst:in_const_frac];
  916. begin
  917. frac := d - Int(d);
  918. end;
  919. {$endif}
  920. {$ifndef FPC_SYSTEM_HAS_POWER}
  921. function power(bas,expo : real) : real;
  922. begin
  923. if bas=0.0 then
  924. begin
  925. if expo<>0.0 then
  926. power:=0.0
  927. else
  928. HandleError(207);
  929. end
  930. else if expo=0.0 then
  931. power:=1
  932. else
  933. { bas < 0 is not allowed }
  934. if bas<0.0 then
  935. handleerror(207)
  936. else
  937. power:=exp(ln(bas)*expo);
  938. end;
  939. {$endif}
  940. {$ifndef FPC_SYSTEM_HAS_POWER_INT64}
  941. function power(bas,expo : int64) : int64;
  942. begin
  943. if bas=0 then
  944. begin
  945. if expo<>0 then
  946. power:=0
  947. else
  948. HandleError(207);
  949. end
  950. else if expo=0 then
  951. power:=1
  952. else
  953. begin
  954. if bas<0 then
  955. begin
  956. if odd(expo) then
  957. power:=-round(exp(ln(-bas)*expo))
  958. else
  959. power:=round(exp(ln(-bas)*expo));
  960. end
  961. else
  962. power:=round(exp(ln(bas)*expo));
  963. end;
  964. end;
  965. {$endif}
  966. {$ifdef FPC_INCLUDE_SOFTWARE_INT64_TO_DOUBLE}
  967. {$ifndef FPC_SYSTEM_HAS_QWORD_TO_DOUBLE}
  968. function fpc_qword_to_double(q : qword): double; compilerproc;
  969. begin
  970. result:=dword(q and $ffffffff)+dword(q shr 32)*4294967296.0;
  971. end;
  972. {$endif FPC_SYSTEM_HAS_INT64_TO_DOUBLE}
  973. {$ifndef FPC_SYSTEM_HAS_INT64_TO_DOUBLE}
  974. function fpc_int64_to_double(i : int64): double; compilerproc;
  975. begin
  976. if i<0 then
  977. result:=-double(qword(-i))
  978. else
  979. result:=qword(i);
  980. end;
  981. {$endif FPC_SYSTEM_HAS_INT64_TO_DOUBLE}
  982. {$endif FPC_INCLUDE_SOFTWARE_INT64_TO_DOUBLE}
  983. {$ifdef SUPPORT_DOUBLE}
  984. {****************************************************************************
  985. Helper routines to support old TP styled reals
  986. ****************************************************************************}
  987. {$ifndef FPC_SYSTEM_HAS_REAL2DOUBLE}
  988. function real2double(r : real48) : double;
  989. var
  990. res : array[0..7] of byte;
  991. exponent : word;
  992. begin
  993. { copy mantissa }
  994. res[0]:=0;
  995. res[1]:=r[1] shl 5;
  996. res[2]:=(r[1] shr 3) or (r[2] shl 5);
  997. res[3]:=(r[2] shr 3) or (r[3] shl 5);
  998. res[4]:=(r[3] shr 3) or (r[4] shl 5);
  999. res[5]:=(r[4] shr 3) or (r[5] and $7f) shl 5;
  1000. res[6]:=(r[5] and $7f) shr 3;
  1001. { copy exponent }
  1002. { correct exponent: }
  1003. exponent:=(word(r[0])+(1023-129));
  1004. res[6]:=res[6] or ((exponent and $f) shl 4);
  1005. res[7]:=exponent shr 4;
  1006. { set sign }
  1007. res[7]:=res[7] or (r[5] and $80);
  1008. real2double:=double(res);
  1009. end;
  1010. {$endif FPC_SYSTEM_HAS_REAL2DOUBLE}
  1011. {$endif SUPPORT_DOUBLE}
  1012. {
  1013. $Log$
  1014. Revision 1.17 2004-01-02 17:19:04 jonas
  1015. * if currency = int64, FPC_CURRENCY_IS_INT64 is defined
  1016. + round and trunc for currency and comp if FPC_CURRENCY_IS_INT64 is
  1017. defined
  1018. * if currency = orddef, prefer currency -> int64/qword conversion over
  1019. currency -> float conversions
  1020. * optimized currency/currency if currency = orddef
  1021. * TODO: write FPC_DIV_CURRENCY and FPC_MUL_CURRENCY routines to prevent
  1022. precision loss if currency=int64 and bestreal = double
  1023. Revision 1.16 2003/12/08 19:44:11 jonas
  1024. * use HandleError instead of RunError so exception catching works
  1025. Revision 1.15 2003/09/03 14:09:37 florian
  1026. * arm fixes to the common rtl code
  1027. * some generic math code fixed
  1028. * ...
  1029. Revision 1.14 2003/05/24 13:39:32 jonas
  1030. * fsqrt is an optional instruction in the ppc architecture and isn't
  1031. implemented by any current ppc afaik, so use the generic sqrt routine
  1032. instead (adapted so it works with compilerproc)
  1033. Revision 1.13 2003/05/23 22:58:31 jonas
  1034. * added longint typecase to odd(smallint_var) call to avoid overload
  1035. problem
  1036. Revision 1.12 2003/05/02 15:12:19 jonas
  1037. - removed empty ppc-specific frac()
  1038. + added correct generic frac() implementation for doubles (translated
  1039. from glibc code)
  1040. Revision 1.11 2003/04/23 21:28:21 peter
  1041. * fpc_round added, needed for int64 currency
  1042. Revision 1.10 2003/01/15 00:45:17 peter
  1043. * use generic int64 power
  1044. Revision 1.9 2002/10/12 20:28:49 carl
  1045. * round returns int64
  1046. Revision 1.8 2002/10/07 15:15:02 florian
  1047. * fixed wrong commit
  1048. Revision 1.7 2002/10/07 15:10:45 florian
  1049. + variant wrappers for cmp operators added
  1050. Revision 1.6 2002/09/07 15:07:45 peter
  1051. * old logs removed and tabs fixed
  1052. Revision 1.5 2002/07/28 21:39:29 florian
  1053. * made abs a compiler proc if it is generic
  1054. Revision 1.4 2002/07/28 20:43:48 florian
  1055. * several fixes for linux/powerpc
  1056. * several fixes to MT
  1057. }