genmath.inc 40 KB

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