genmath.inc 40 KB

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