genmath.inc 40 KB

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