genmath.inc 41 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266
  1. {
  2. This file is part of the Free Pascal run time library.
  3. Copyright (c) 1999-2001 by Several contributors
  4. Generic mathemtical routines (on type real)
  5. See the file COPYING.FPC, included in this distribution,
  6. for details about the copyright.
  7. This program is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  10. **********************************************************************}
  11. {*************************************************************************}
  12. { Credits }
  13. {*************************************************************************}
  14. { Copyright Abandoned, 1987, Fred Fish }
  15. { }
  16. { This previously copyrighted work has been placed into the }
  17. { public domain by the author (Fred Fish) and may be freely used }
  18. { for any purpose, private or commercial. I would appreciate }
  19. { it, as a courtesy, if this notice is left in all copies and }
  20. { derivative works. Thank you, and enjoy... }
  21. { }
  22. { The author makes no warranty of any kind with respect to this }
  23. { product and explicitly disclaims any implied warranties of }
  24. { merchantability or fitness for any particular purpose. }
  25. {-------------------------------------------------------------------------}
  26. { Copyright (c) 1992 Odent Jean Philippe }
  27. { }
  28. { The source can be modified as long as my name appears and some }
  29. { notes explaining the modifications done are included in the file. }
  30. {-------------------------------------------------------------------------}
  31. { Copyright (c) 1997 Carl Eric Codere }
  32. {-------------------------------------------------------------------------}
  33. {$goto on}
  34. type
  35. TabCoef = array[0..6] of Real;
  36. { also necessary for Int() on systems with 64bit floats (JM) }
  37. {$ifndef FPC_SYSTEM_HAS_float64}
  38. {$ifdef ENDIAN_LITTLE}
  39. float64 = packed record
  40. low: longint;
  41. high: longint;
  42. end;
  43. {$else}
  44. float64 = packed record
  45. high: longint;
  46. low: longint;
  47. end;
  48. {$endif}
  49. {$endif FPC_SYSTEM_HAS_float64}
  50. const
  51. PIO2 = 1.57079632679489661923; { pi/2 }
  52. PIO4 = 7.85398163397448309616E-1; { pi/4 }
  53. SQRT2 = 1.41421356237309504880; { sqrt(2) }
  54. SQRTH = 7.07106781186547524401E-1; { sqrt(2)/2 }
  55. LOG2E = 1.4426950408889634073599; { 1/log(2) }
  56. SQ2OPI = 7.9788456080286535587989E-1; { sqrt( 2/pi )}
  57. LOGE2 = 6.93147180559945309417E-1; { log(2) }
  58. LOGSQ2 = 3.46573590279972654709E-1; { log(2)/2 }
  59. THPIO4 = 2.35619449019234492885; { 3*pi/4 }
  60. TWOOPI = 6.36619772367581343075535E-1; { 2/pi }
  61. lossth = 1.073741824e9;
  62. MAXLOG = 8.8029691931113054295988E1; { log(2**127) }
  63. MINLOG = -8.872283911167299960540E1; { log(2**-128) }
  64. DP1 = 7.85398125648498535156E-1;
  65. DP2 = 3.77489470793079817668E-8;
  66. DP3 = 2.69515142907905952645E-15;
  67. const sincof : TabCoef = (
  68. 1.58962301576546568060E-10,
  69. -2.50507477628578072866E-8,
  70. 2.75573136213857245213E-6,
  71. -1.98412698295895385996E-4,
  72. 8.33333333332211858878E-3,
  73. -1.66666666666666307295E-1, 0);
  74. coscof : TabCoef = (
  75. -1.13585365213876817300E-11,
  76. 2.08757008419747316778E-9,
  77. -2.75573141792967388112E-7,
  78. 2.48015872888517045348E-5,
  79. -1.38888888888730564116E-3,
  80. 4.16666666666665929218E-2, 0);
  81. {$ifndef FPC_SYSTEM_HAS_TRUNC}
  82. {$ifndef FPC_SYSTEM_HAS_float32}
  83. type
  84. float32 = longint;
  85. {$endif FPC_SYSTEM_HAS_float32}
  86. {$ifndef FPC_SYSTEM_HAS_flag}
  87. type
  88. flag = byte;
  89. {$endif FPC_SYSTEM_HAS_flag}
  90. {$ifndef FPC_SYSTEM_HAS_extractFloat64Frac0}
  91. Function extractFloat64Frac0(const a: float64): longint;
  92. Begin
  93. extractFloat64Frac0 := a.high and $000FFFFF;
  94. End;
  95. {$endif not FPC_SYSTEM_HAS_extractFloat64Frac0}
  96. {$ifndef FPC_SYSTEM_HAS_extractFloat64Frac1}
  97. Function extractFloat64Frac1(const a: float64): longint;
  98. Begin
  99. extractFloat64Frac1 := a.low;
  100. End;
  101. {$endif not FPC_SYSTEM_HAS_extractFloat64Frac1}
  102. {$ifndef FPC_SYSTEM_HAS_extractFloat64Exp}
  103. Function extractFloat64Exp(const a: float64): smallint;
  104. Begin
  105. extractFloat64Exp:= ( a.high shr 20 ) AND $7FF;
  106. End;
  107. {$endif not FPC_SYSTEM_HAS_extractFloat64Exp}
  108. {$ifndef FPC_SYSTEM_HAS_extractFloat64Frac}
  109. Function extractFloat64Frac(const a: float64): int64;
  110. Begin
  111. extractFloat64Frac:=int64(a) and $000FFFFFFFFFFFFF;
  112. End;
  113. {$endif not FPC_SYSTEM_HAS_extractFloat64Frac}
  114. {$ifndef FPC_SYSTEM_HAS_extractFloat64Sign}
  115. Function extractFloat64Sign(const a: float64) : flag;
  116. Begin
  117. extractFloat64Sign := a.high shr 31;
  118. End;
  119. {$endif not FPC_SYSTEM_HAS_extractFloat64Sign}
  120. Procedure shortShift64Left(a0:longint; a1:longint; count:smallint; VAR z0Ptr:longint; VAR z1Ptr:longint );
  121. Begin
  122. z1Ptr := a1 shl count;
  123. if count = 0 then
  124. z0Ptr := a0
  125. else
  126. z0Ptr := ( a0 shl count ) OR ( a1 shr ( ( - count ) AND 31 ) );
  127. End;
  128. function float64_to_int32_round_to_zero(a: float64 ): longint;
  129. Var
  130. aSign: flag;
  131. aExp, shiftCount: smallint;
  132. aSig0, aSig1, absZ, aSigExtra: longint;
  133. z: longint;
  134. Begin
  135. aSig1 := extractFloat64Frac1( a );
  136. aSig0 := extractFloat64Frac0( a );
  137. aExp := extractFloat64Exp( a );
  138. aSign := extractFloat64Sign( a );
  139. shiftCount := aExp - $413;
  140. if 0<=shiftCount then
  141. Begin
  142. if (aExp=$7FF) and ((aSig0 or aSig1)<>0) then
  143. HandleError(207);
  144. shortShift64Left(aSig0 OR $00100000, aSig1, shiftCount, absZ, aSigExtra );
  145. End
  146. else
  147. Begin
  148. if aExp<$3FF then
  149. begin
  150. float64_to_int32_round_to_zero := 0;
  151. exit;
  152. end;
  153. aSig0 := aSig0 or $00100000;
  154. aSigExtra := ( aSig0 shl ( shiftCount and 31 ) ) OR aSig1;
  155. absZ := aSig0 shr ( - shiftCount );
  156. End;
  157. if aSign<>0 then
  158. z:=-absZ
  159. else
  160. z:=absZ;
  161. if ((aSign<>0) xor (z<0)) AND (z<>0) then
  162. HandleError(207);
  163. float64_to_int32_round_to_zero := z;
  164. End;
  165. function float64_to_int64_round_to_zero(a : float64) : int64;
  166. var
  167. aSign : flag;
  168. aExp, shiftCount : smallint;
  169. aSig : int64;
  170. z : int64;
  171. begin
  172. aSig:=extractFloat64Frac(a);
  173. aExp:=extractFloat64Exp(a);
  174. aSign:=extractFloat64Sign(a);
  175. if aExp<>0 then
  176. aSig:=aSig or $0010000000000000;
  177. shiftCount:= aExp-$433;
  178. if 0<=shiftCount then
  179. begin
  180. if aExp>=$43e then
  181. begin
  182. if int64(a)<>$C3E0000000000000 then
  183. HandleError(207);
  184. { pascal doesn't know Inf for int64 }
  185. HandleError(207);
  186. end;
  187. z:=aSig shl shiftCount;
  188. end
  189. else
  190. begin
  191. if aExp<$3fe then
  192. begin
  193. result:=0;
  194. exit;
  195. end;
  196. z:=aSig shr -shiftCount;
  197. {
  198. if (aSig shl (shiftCount and 63))<>0 then
  199. float_exception_flags |= float_flag_inexact;
  200. }
  201. end;
  202. if aSign<>0 then
  203. z:=-z;
  204. result:=z;
  205. end;
  206. {$ifndef FPC_SYSTEM_HAS_ExtractFloat32Frac}
  207. Function ExtractFloat32Frac(a : Float32) : longint;
  208. Begin
  209. ExtractFloat32Frac := A AND $007FFFFF;
  210. End;
  211. {$endif not FPC_SYSTEM_HAS_ExtractFloat32Frac}
  212. {$ifndef FPC_SYSTEM_HAS_extractFloat32Exp}
  213. Function extractFloat32Exp( a: float32 ): smallint;
  214. Begin
  215. extractFloat32Exp := (a shr 23) AND $FF;
  216. End;
  217. {$endif not FPC_SYSTEM_HAS_extractFloat32Exp}
  218. {$ifndef FPC_SYSTEM_HAS_extractFloat32Sign}
  219. Function extractFloat32Sign( a: float32 ): Flag;
  220. Begin
  221. extractFloat32Sign := a shr 31;
  222. End;
  223. {$endif not FPC_SYSTEM_HAS_extractFloat32Sign}
  224. Function float32_to_int32_round_to_zero( a: Float32 ): longint;
  225. Var
  226. aSign : flag;
  227. aExp, shiftCount : smallint;
  228. aSig : longint;
  229. z : longint;
  230. Begin
  231. aSig := extractFloat32Frac( a );
  232. aExp := extractFloat32Exp( a );
  233. aSign := extractFloat32Sign( a );
  234. shiftCount := aExp - $9E;
  235. if ( 0 <= shiftCount ) then
  236. Begin
  237. if ( a <> Float32($CF000000) ) then
  238. Begin
  239. if ( (aSign=0) or ( ( aExp = $FF ) and (aSig<>0) ) ) then
  240. Begin
  241. HandleError(207);
  242. exit;
  243. end;
  244. End;
  245. HandleError(207);
  246. exit;
  247. End
  248. else
  249. if ( aExp <= $7E ) then
  250. Begin
  251. float32_to_int32_round_to_zero := 0;
  252. exit;
  253. End;
  254. aSig := ( aSig or $00800000 ) shl 8;
  255. z := aSig shr ( - shiftCount );
  256. if ( aSign<>0 ) then z := - z;
  257. float32_to_int32_round_to_zero := z;
  258. End;
  259. function fpc_trunc_real(d : ValReal) : int64;compilerproc;
  260. var
  261. {$ifdef cpuarm}
  262. l: longint;
  263. {$endif cpuarm}
  264. f32 : float32;
  265. f64 : float64;
  266. Begin
  267. { in emulation mode the real is equal to a single }
  268. { otherwise in fpu mode, it is equal to a double }
  269. { extended is not supported yet. }
  270. if sizeof(D) > 8 then
  271. HandleError(255);
  272. if sizeof(D)=8 then
  273. begin
  274. move(d,f64,sizeof(f64));
  275. {$ifdef cpuarm}
  276. { the arm fpu has a strange opinion how a double has to be stored }
  277. l:=f64.low;
  278. f64.low:=f64.high;
  279. f64.high:=l;
  280. {$endif cpuarm}
  281. result:=float64_to_int64_round_to_zero(f64);
  282. end
  283. else
  284. begin
  285. move(d,f32,sizeof(f32));
  286. result:=float32_to_int32_round_to_zero(f32);
  287. end;
  288. end;
  289. {$endif not FPC_SYSTEM_HAS_TRUNC}
  290. {$ifndef FPC_SYSTEM_HAS_INT}
  291. {$ifdef SUPPORT_DOUBLE}
  292. { straight Pascal translation of the code for __trunc() in }
  293. { the file sysdeps/libm-ieee754/s_trunc.c of glibc (JM) }
  294. function fpc_int_real(d: ValReal): ValReal;compilerproc;
  295. var
  296. i0, j0: longint;
  297. i1: cardinal;
  298. sx: longint;
  299. f64 : float64;
  300. begin
  301. f64:=float64(d);
  302. {$ifdef cpuarm}
  303. { the arm fpu has a strange opinion how a double has to be stored }
  304. i0:=f64.low;
  305. f64.low:=f64.high;
  306. f64.high:=i0;
  307. {$endif cpuarm}
  308. i0 := f64.high;
  309. i1 := cardinal(f64.low);
  310. sx := i0 and $80000000;
  311. j0 := ((i0 shr 20) and $7ff) - $3ff;
  312. if (j0 < 20) then
  313. begin
  314. if (j0 < 0) then
  315. begin
  316. { the magnitude of the number is < 1 so the result is +-0. }
  317. f64.high := sx;
  318. f64.low := 0;
  319. end
  320. else
  321. begin
  322. f64.high := sx or (i0 and not($fffff shr j0));
  323. f64.low := 0;
  324. end
  325. end
  326. else if (j0 > 51) then
  327. begin
  328. if (j0 = $400) then
  329. { d is inf or NaN }
  330. exit(d + d); { don't know why they do this (JM) }
  331. end
  332. else
  333. begin
  334. f64.high := i0;
  335. f64.low := longint(i1 and not(cardinal($ffffffff) shr (j0 - 20)));
  336. end;
  337. {$ifdef cpuarm}
  338. { the arm fpu has a strange opinion how a double has to be stored }
  339. i0:=f64.low;
  340. f64.low:=f64.high;
  341. f64.high:=i0;
  342. {$endif cpuarm}
  343. result:=double(f64);
  344. end;
  345. {$else SUPPORT_DOUBLE}
  346. function fpc_int_real(d : ValReal) : ValReal;compilerproc;
  347. begin
  348. { this will be correct since real = single in the case of }
  349. { the motorola version of the compiler... }
  350. result:=ValReal(trunc(d));
  351. end;
  352. {$endif SUPPORT_DOUBLE}
  353. {$endif not FPC_SYSTEM_HAS_INT}
  354. {$ifndef FPC_SYSTEM_HAS_ABS}
  355. function fpc_abs_real(d : ValReal) : ValReal;compilerproc;
  356. begin
  357. if (d<0.0) then
  358. result := -d
  359. else
  360. result := d ;
  361. end;
  362. {$endif not FPC_SYSTEM_HAS_ABS}
  363. {$ifndef SYSTEM_HAS_FREXP}
  364. function frexp(x:Real; out e:Integer ):Real;
  365. {* frexp() extracts the exponent from x. It returns an integer *}
  366. {* power of two to expnt and the significand between 0.5 and 1 *}
  367. {* to y. Thus x = y * 2**expn. *}
  368. begin
  369. e :=0;
  370. if (abs(x)<0.5) then
  371. While (abs(x)<0.5) do
  372. begin
  373. x := x*2;
  374. Dec(e);
  375. end
  376. else
  377. While (abs(x)>1) do
  378. begin
  379. x := x/2;
  380. Inc(e);
  381. end;
  382. frexp := x;
  383. end;
  384. {$endif not SYSTEM_HAS_FREXP}
  385. {$ifndef SYSTEM_HAS_LDEXP}
  386. function ldexp( x: Real; N: Integer):Real;
  387. {* ldexp() multiplies x by 2**n. *}
  388. var r : Real;
  389. begin
  390. R := 1;
  391. if N>0 then
  392. while N>0 do
  393. begin
  394. R:=R*2;
  395. Dec(N);
  396. end
  397. else
  398. while N<0 do
  399. begin
  400. R:=R/2;
  401. Inc(N);
  402. end;
  403. ldexp := x * R;
  404. end;
  405. {$endif not SYSTEM_HAS_LDEXP}
  406. function polevl(var x:Real; var Coef:TabCoef; N:Integer):Real;
  407. {*****************************************************************}
  408. { Evaluate polynomial }
  409. {*****************************************************************}
  410. { }
  411. { SYNOPSIS: }
  412. { }
  413. { int N; }
  414. { double x, y, coef[N+1], polevl[]; }
  415. { }
  416. { y = polevl( x, coef, N ); }
  417. { }
  418. { DESCRIPTION: }
  419. { }
  420. { Evaluates polynomial of degree N: }
  421. { }
  422. { 2 N }
  423. { y = C + C x + C x +...+ C x }
  424. { 0 1 2 N }
  425. { }
  426. { Coefficients are stored in reverse order: }
  427. { }
  428. { coef[0] = C , ..., coef[N] = C . }
  429. { N 0 }
  430. { }
  431. { The function p1evl() assumes that coef[N] = 1.0 and is }
  432. { omitted from the array. Its calling arguments are }
  433. { otherwise the same as polevl(). }
  434. { }
  435. { SPEED: }
  436. { }
  437. { In the interest of speed, there are no checks for out }
  438. { of bounds arithmetic. This routine is used by most of }
  439. { the functions in the library. Depending on available }
  440. { equipment features, the user may wish to rewrite the }
  441. { program in microcode or assembly language. }
  442. {*****************************************************************}
  443. var ans : Real;
  444. i : Integer;
  445. begin
  446. ans := Coef[0];
  447. for i:=1 to N do
  448. ans := ans * x + Coef[i];
  449. polevl:=ans;
  450. end;
  451. function p1evl(var x:Real; var Coef:TabCoef; N:Integer):Real;
  452. { }
  453. { Evaluate polynomial when coefficient of x is 1.0. }
  454. { Otherwise same as polevl. }
  455. { }
  456. var
  457. ans : Real;
  458. i : Integer;
  459. begin
  460. ans := x + Coef[0];
  461. for i:=1 to N-1 do
  462. ans := ans * x + Coef[i];
  463. p1evl := ans;
  464. end;
  465. {$ifndef FPC_SYSTEM_HAS_SQR}
  466. function fpc_sqr_real(d : ValReal) : ValReal;compilerproc;{$ifdef MATHINLINE}inline;{$endif}
  467. begin
  468. result := d*d;
  469. end;
  470. {$endif}
  471. {$ifndef FPC_SYSTEM_HAS_PI}
  472. function fpc_pi_real : ValReal;compilerproc;{$ifdef MATHINLINE}inline;{$endif}
  473. begin
  474. result := 3.1415926535897932385;
  475. end;
  476. {$endif}
  477. {$ifndef FPC_SYSTEM_HAS_SQRT}
  478. function fpc_sqrt_real(d:ValReal):ValReal;compilerproc;
  479. {*****************************************************************}
  480. { Square root }
  481. {*****************************************************************}
  482. { }
  483. { SYNOPSIS: }
  484. { }
  485. { double x, y, sqrt(); }
  486. { }
  487. { y = sqrt( x ); }
  488. { }
  489. { DESCRIPTION: }
  490. { }
  491. { Returns the square root of x. }
  492. { }
  493. { Range reduction involves isolating the power of two of the }
  494. { argument and using a polynomial approximation to obtain }
  495. { a rough value for the square root. Then Heron's iteration }
  496. { is used three times to converge to an accurate value. }
  497. {*****************************************************************}
  498. var e : Integer;
  499. w,z : Real;
  500. begin
  501. if( d <= 0.0 ) then
  502. begin
  503. if( d < 0.0 ) then
  504. HandleError(207);
  505. result := 0.0;
  506. end
  507. else
  508. begin
  509. w := d;
  510. { separate exponent and significand }
  511. z := frexp( d, e );
  512. { approximate square root of number between 0.5 and 1 }
  513. { relative error of approximation = 7.47e-3 }
  514. d := 4.173075996388649989089E-1 + 5.9016206709064458299663E-1 * z;
  515. { adjust for odd powers of 2 }
  516. if odd(e) then
  517. d := d*SQRT2;
  518. { re-insert exponent }
  519. d := ldexp( d, (e div 2) );
  520. { Newton iterations: }
  521. d := 0.5*(d + w/d);
  522. d := 0.5*(d + w/d);
  523. d := 0.5*(d + w/d);
  524. d := 0.5*(d + w/d);
  525. d := 0.5*(d + w/d);
  526. d := 0.5*(d + w/d);
  527. result := d;
  528. end;
  529. end;
  530. {$endif}
  531. {$ifndef FPC_SYSTEM_HAS_EXP}
  532. function fpc_exp_real(d: ValReal):ValReal;compilerproc;
  533. {*****************************************************************}
  534. { Exponential Function }
  535. {*****************************************************************}
  536. { }
  537. { SYNOPSIS: }
  538. { }
  539. { double x, y, exp(); }
  540. { }
  541. { y = exp( x ); }
  542. { }
  543. { DESCRIPTION: }
  544. { }
  545. { Returns e (2.71828...) raised to the x power. }
  546. { }
  547. { Range reduction is accomplished by separating the argument }
  548. { into an integer k and fraction f such that }
  549. { }
  550. { x k f }
  551. { e = 2 e. }
  552. { }
  553. { A Pade' form of degree 2/3 is used to approximate exp(f)- 1 }
  554. { in the basic range [-0.5 ln 2, 0.5 ln 2]. }
  555. {*****************************************************************}
  556. const P : TabCoef = (
  557. 1.26183092834458542160E-4,
  558. 3.02996887658430129200E-2,
  559. 1.00000000000000000000E0, 0, 0, 0, 0);
  560. Q : TabCoef = (
  561. 3.00227947279887615146E-6,
  562. 2.52453653553222894311E-3,
  563. 2.27266044198352679519E-1,
  564. 2.00000000000000000005E0, 0 ,0 ,0);
  565. C1 = 6.9335937500000000000E-1;
  566. C2 = 2.1219444005469058277E-4;
  567. var n : Integer;
  568. px, qx, xx : Real;
  569. begin
  570. if( d > MAXLOG) then
  571. HandleError(205)
  572. else
  573. if( d < MINLOG ) then
  574. begin
  575. HandleError(205);
  576. end
  577. else
  578. begin
  579. { Express e**x = e**g 2**n }
  580. { = e**g e**( n loge(2) ) }
  581. { = e**( g + n loge(2) ) }
  582. px := d * LOG2E;
  583. qx := Trunc( px + 0.5 ); { Trunc() truncates toward -infinity. }
  584. n := Trunc(qx);
  585. d := d - qx * C1;
  586. d := d + qx * C2;
  587. { rational approximation for exponential }
  588. { of the fractional part: }
  589. { e**x - 1 = 2x P(x**2)/( Q(x**2) - P(x**2) ) }
  590. xx := d * d;
  591. px := d * polevl( xx, P, 2 );
  592. d := px/( polevl( xx, Q, 3 ) - px );
  593. d := ldexp( d, 1 );
  594. d := d + 1.0;
  595. d := ldexp( d, n );
  596. result := d;
  597. end;
  598. end;
  599. {$endif}
  600. {$ifndef FPC_SYSTEM_HAS_ROUND}
  601. function fpc_round_real(d : ValReal) : int64;compilerproc;
  602. var
  603. fr: Real;
  604. tr: Int64;
  605. Begin
  606. fr := abs(Frac(d));
  607. tr := Trunc(d);
  608. if fr > 0.5 then
  609. if d >= 0 then
  610. result:=tr+1
  611. else
  612. result:=tr-1
  613. else
  614. if fr < 0.5 then
  615. result:=tr
  616. else { fr = 0.5 }
  617. { check sign to decide ... }
  618. { as in Turbo Pascal... }
  619. if d >= 0.0 then
  620. result:=tr+1
  621. else
  622. result:=tr;
  623. end;
  624. {$endif}
  625. {$ifdef FPC_CURRENCY_IS_INT64}
  626. function trunc(c : currency) : int64;
  627. type
  628. tmyrec = record
  629. i: int64;
  630. end;
  631. begin
  632. result := int64(tmyrec(c)) div 10000
  633. end;
  634. function trunc(c : comp) : int64;
  635. begin
  636. result := c
  637. end;
  638. function round(c : currency) : int64;
  639. type
  640. tmyrec = record
  641. i: int64;
  642. end;
  643. var
  644. rem, absrem: longint;
  645. begin
  646. { (int64(tmyrec(c))(+/-)5000) div 10000 can overflow }
  647. result := int64(tmyrec(c)) div 10000;
  648. rem := int64(tmyrec(c)) - result * 10000;
  649. absrem := abs(rem);
  650. if (absrem > 5000) or
  651. ((absrem = 5000) and
  652. (rem > 0)) then
  653. if (rem > 0) then
  654. inc(result)
  655. else
  656. dec(result);
  657. end;
  658. function round(c : comp) : int64;
  659. begin
  660. result := c
  661. end;
  662. {$endif FPC_CURRENCY_IS_INT64}
  663. {$ifndef FPC_SYSTEM_HAS_LN}
  664. function fpc_ln_real(d:ValReal):ValReal;compilerproc;
  665. {*****************************************************************}
  666. { Natural Logarithm }
  667. {*****************************************************************}
  668. { }
  669. { SYNOPSIS: }
  670. { }
  671. { double x, y, log(); }
  672. { }
  673. { y = ln( x ); }
  674. { }
  675. { DESCRIPTION: }
  676. { }
  677. { Returns the base e (2.718...) logarithm of x. }
  678. { }
  679. { The argument is separated into its exponent and fractional }
  680. { parts. If the exponent is between -1 and +1, the logarithm }
  681. { of the fraction is approximated by }
  682. { }
  683. { log(1+x) = x - 0.5 x**2 + x**3 P(x)/Q(x). }
  684. { }
  685. { Otherwise, setting z = 2(x-1)/x+1), }
  686. { }
  687. { log(x) = z + z**3 P(z)/Q(z). }
  688. { }
  689. {*****************************************************************}
  690. const P : TabCoef = (
  691. { Coefficients for log(1+x) = x - x**2/2 + x**3 P(x)/Q(x)
  692. 1/sqrt(2) <= x < sqrt(2) }
  693. 4.58482948458143443514E-5,
  694. 4.98531067254050724270E-1,
  695. 6.56312093769992875930E0,
  696. 2.97877425097986925891E1,
  697. 6.06127134467767258030E1,
  698. 5.67349287391754285487E1,
  699. 1.98892446572874072159E1);
  700. Q : TabCoef = (
  701. 1.50314182634250003249E1,
  702. 8.27410449222435217021E1,
  703. 2.20664384982121929218E2,
  704. 3.07254189979530058263E2,
  705. 2.14955586696422947765E2,
  706. 5.96677339718622216300E1, 0);
  707. { Coefficients for log(x) = z + z**3 P(z)/Q(z),
  708. where z = 2(x-1)/(x+1)
  709. 1/sqrt(2) <= x < sqrt(2) }
  710. R : TabCoef = (
  711. -7.89580278884799154124E-1,
  712. 1.63866645699558079767E1,
  713. -6.41409952958715622951E1, 0, 0, 0, 0);
  714. S : TabCoef = (
  715. -3.56722798256324312549E1,
  716. 3.12093766372244180303E2,
  717. -7.69691943550460008604E2, 0, 0, 0, 0);
  718. var e : Integer;
  719. z, y : Real;
  720. Label Ldone;
  721. begin
  722. if( d <= 0.0 ) then
  723. HandleError(207);
  724. d := frexp( d, e );
  725. { logarithm using log(x) = z + z**3 P(z)/Q(z),
  726. where z = 2(x-1)/x+1) }
  727. if( (e > 2) or (e < -2) ) then
  728. begin
  729. if( d < SQRTH ) then
  730. begin
  731. { 2( 2x-1 )/( 2x+1 ) }
  732. Dec(e, 1);
  733. z := d - 0.5;
  734. y := 0.5 * z + 0.5;
  735. end
  736. else
  737. begin
  738. { 2 (x-1)/(x+1) }
  739. z := d - 0.5;
  740. z := z - 0.5;
  741. y := 0.5 * d + 0.5;
  742. end;
  743. d := z / y;
  744. { /* rational form */ }
  745. z := d*d;
  746. z := d + d * ( z * polevl( z, R, 2 ) / p1evl( z, S, 3 ) );
  747. goto ldone;
  748. end;
  749. { logarithm using log(1+x) = x - .5x**2 + x**3 P(x)/Q(x) }
  750. if( d < SQRTH ) then
  751. begin
  752. Dec(e, 1);
  753. d := ldexp( d, 1 ) - 1.0; { 2x - 1 }
  754. end
  755. else
  756. d := d - 1.0;
  757. { rational form }
  758. z := d*d;
  759. y := d * ( z * polevl( d, P, 6 ) / p1evl( d, Q, 6 ) );
  760. y := y - ldexp( z, -1 ); { y - 0.5 * z }
  761. z := d + y;
  762. ldone:
  763. { recombine with exponent term }
  764. if( e <> 0 ) then
  765. begin
  766. y := e;
  767. z := z - y * 2.121944400546905827679e-4;
  768. z := z + y * 0.693359375;
  769. end;
  770. result:= z;
  771. end;
  772. {$endif}
  773. {$ifndef FPC_SYSTEM_HAS_SIN}
  774. function fpc_Sin_real(d:ValReal):ValReal;compilerproc;
  775. {*****************************************************************}
  776. { Circular Sine }
  777. {*****************************************************************}
  778. { }
  779. { SYNOPSIS: }
  780. { }
  781. { double x, y, sin(); }
  782. { }
  783. { y = sin( x ); }
  784. { }
  785. { DESCRIPTION: }
  786. { }
  787. { Range reduction is into intervals of pi/4. The reduction }
  788. { error is nearly eliminated by contriving an extended }
  789. { precision modular arithmetic. }
  790. { }
  791. { Two polynomial approximating functions are employed. }
  792. { Between 0 and pi/4 the sine is approximated by }
  793. { x + x**3 P(x**2). }
  794. { Between pi/4 and pi/2 the cosine is represented as }
  795. { 1 - x**2 Q(x**2). }
  796. {*****************************************************************}
  797. var y, z, zz : Real;
  798. j, sign : Integer;
  799. begin
  800. { make argument positive but save the sign }
  801. sign := 1;
  802. if( d < 0 ) then
  803. begin
  804. d := -d;
  805. sign := -1;
  806. end;
  807. { above this value, approximate towards 0 }
  808. if( d > lossth ) then
  809. begin
  810. result := 0.0;
  811. exit;
  812. end;
  813. y := Trunc( d/PIO4 ); { integer part of x/PIO4 }
  814. { strip high bits of integer part to prevent integer overflow }
  815. z := ldexp( y, -4 );
  816. z := Trunc(z); { integer part of y/8 }
  817. z := y - ldexp( z, 4 ); { y - 16 * (y/16) }
  818. j := Trunc(z); { convert to integer for tests on the phase angle }
  819. { map zeros to origin }
  820. { typecast is to avoid "can't determine which overloaded function }
  821. { to call" }
  822. if odd( longint(j) ) then
  823. begin
  824. inc(j);
  825. y := y + 1.0;
  826. end;
  827. j := j and 7; { octant modulo 360 degrees }
  828. { reflect in x axis }
  829. if( j > 3) then
  830. begin
  831. sign := -sign;
  832. dec(j, 4);
  833. end;
  834. { Extended precision modular arithmetic }
  835. z := ((d - y * DP1) - y * DP2) - y * DP3;
  836. zz := z * z;
  837. if( (j=1) or (j=2) ) then
  838. y := 1.0 - ldexp(zz,-1) + zz * zz * polevl( zz, coscof, 5 )
  839. else
  840. { y = z + z * (zz * polevl( zz, sincof, 5 )); }
  841. y := z + z * z * z * polevl( zz, sincof, 5 );
  842. if(sign < 0) then
  843. y := -y;
  844. result := y;
  845. end;
  846. {$endif}
  847. {$ifndef FPC_SYSTEM_HAS_COS}
  848. function fpc_Cos_real(d:ValReal):ValReal;compilerproc;
  849. {*****************************************************************}
  850. { Circular cosine }
  851. {*****************************************************************}
  852. { }
  853. { Circular cosine }
  854. { }
  855. { SYNOPSIS: }
  856. { }
  857. { double x, y, cos(); }
  858. { }
  859. { y = cos( x ); }
  860. { }
  861. { DESCRIPTION: }
  862. { }
  863. { Range reduction is into intervals of pi/4. The reduction }
  864. { error is nearly eliminated by contriving an extended }
  865. { precision modular arithmetic. }
  866. { }
  867. { Two polynomial approximating functions are employed. }
  868. { Between 0 and pi/4 the cosine is approximated by }
  869. { 1 - x**2 Q(x**2). }
  870. { Between pi/4 and pi/2 the sine is represented as }
  871. { x + x**3 P(x**2). }
  872. {*****************************************************************}
  873. var y, z, zz : Real;
  874. j, sign : Integer;
  875. i : LongInt;
  876. begin
  877. { make argument positive }
  878. sign := 1;
  879. if( d < 0 ) then
  880. d := -d;
  881. { above this value, round towards zero }
  882. if( d > lossth ) then
  883. begin
  884. result := 0.0;
  885. exit;
  886. end;
  887. y := Trunc( d/PIO4 );
  888. z := ldexp( y, -4 );
  889. z := Trunc(z); { integer part of y/8 }
  890. z := y - ldexp( z, 4 ); { y - 16 * (y/16) }
  891. { integer and fractional part modulo one octant }
  892. i := Trunc(z);
  893. if odd( i ) then { map zeros to origin }
  894. begin
  895. inc(i);
  896. y := y + 1.0;
  897. end;
  898. j := i and 07;
  899. if( j > 3) then
  900. begin
  901. dec(j,4);
  902. sign := -sign;
  903. end;
  904. if( j > 1 ) then
  905. sign := -sign;
  906. { Extended precision modular arithmetic }
  907. z := ((d - y * DP1) - y * DP2) - y * DP3;
  908. zz := z * z;
  909. if( (j=1) or (j=2) ) then
  910. { y = z + z * (zz * polevl( zz, sincof, 5 )); }
  911. y := z + z * z * z * polevl( zz, sincof, 5 )
  912. else
  913. y := 1.0 - ldexp(zz,-1) + zz * zz * polevl( zz, coscof, 5 );
  914. if(sign < 0) then
  915. y := -y;
  916. result := y ;
  917. end;
  918. {$endif}
  919. {$ifndef FPC_SYSTEM_HAS_ARCTAN}
  920. function fpc_ArcTan_real(d:ValReal):ValReal;compilerproc;
  921. {*****************************************************************}
  922. { Inverse circular tangent (arctangent) }
  923. {*****************************************************************}
  924. { }
  925. { SYNOPSIS: }
  926. { }
  927. { double x, y, atan(); }
  928. { }
  929. { y = atan( x ); }
  930. { }
  931. { DESCRIPTION: }
  932. { }
  933. { Returns radian angle between -pi/2 and +pi/2 whose tangent }
  934. { is x. }
  935. { }
  936. { Range reduction is from four intervals into the interval }
  937. { from zero to tan( pi/8 ). The approximant uses a rational }
  938. { function of degree 3/4 of the form x + x**3 P(x)/Q(x). }
  939. {*****************************************************************}
  940. const P : TabCoef = (
  941. -8.40980878064499716001E-1,
  942. -8.83860837023772394279E0,
  943. -2.18476213081316705724E1,
  944. -1.48307050340438946993E1, 0, 0, 0);
  945. Q : TabCoef = (
  946. 1.54974124675307267552E1,
  947. 6.27906555762653017263E1,
  948. 9.22381329856214406485E1,
  949. 4.44921151021319438465E1, 0, 0, 0);
  950. { tan( 3*pi/8 ) }
  951. T3P8 = 2.41421356237309504880;
  952. { tan( pi/8 ) }
  953. TP8 = 0.41421356237309504880;
  954. var y,z : Real;
  955. Sign : Integer;
  956. begin
  957. { make argument positive and save the sign }
  958. sign := 1;
  959. if( d < 0.0 ) then
  960. begin
  961. sign := -1;
  962. d := -d;
  963. end;
  964. { range reduction }
  965. if( d > T3P8 ) then
  966. begin
  967. y := PIO2;
  968. d := -( 1.0/d );
  969. end
  970. else if( d > TP8 ) then
  971. begin
  972. y := PIO4;
  973. d := (d-1.0)/(d+1.0);
  974. end
  975. else
  976. y := 0.0;
  977. { rational form in x**2 }
  978. z := d * d;
  979. y := y + ( polevl( z, P, 3 ) / p1evl( z, Q, 4 ) ) * z * d + d;
  980. if( sign < 0 ) then
  981. y := -y;
  982. result := y;
  983. end;
  984. {$endif}
  985. {$ifndef FPC_SYSTEM_HAS_FRAC}
  986. function fpc_frac_real(d : ValReal) : ValReal;compilerproc;
  987. begin
  988. result := d - Int(d);
  989. end;
  990. {$endif}
  991. {$ifdef FPC_INCLUDE_SOFTWARE_INT64_TO_DOUBLE}
  992. {$ifndef FPC_SYSTEM_HAS_QWORD_TO_DOUBLE}
  993. function fpc_qword_to_double(q : qword): double; compilerproc;
  994. begin
  995. result:=dword(q and $ffffffff)+dword(q shr 32)*4294967296.0;
  996. end;
  997. {$endif FPC_SYSTEM_HAS_INT64_TO_DOUBLE}
  998. {$ifndef FPC_SYSTEM_HAS_INT64_TO_DOUBLE}
  999. function fpc_int64_to_double(i : int64): double; compilerproc;
  1000. begin
  1001. if i<0 then
  1002. result:=-double(qword(-i))
  1003. else
  1004. result:=qword(i);
  1005. end;
  1006. {$endif FPC_SYSTEM_HAS_INT64_TO_DOUBLE}
  1007. {$endif FPC_INCLUDE_SOFTWARE_INT64_TO_DOUBLE}
  1008. {$ifdef SUPPORT_DOUBLE}
  1009. {****************************************************************************
  1010. Helper routines to support old TP styled reals
  1011. ****************************************************************************}
  1012. {$ifndef FPC_SYSTEM_HAS_REAL2DOUBLE}
  1013. function real2double(r : real48) : double;
  1014. var
  1015. res : array[0..7] of byte;
  1016. exponent : word;
  1017. begin
  1018. { copy mantissa }
  1019. res[0]:=0;
  1020. res[1]:=r[1] shl 5;
  1021. res[2]:=(r[1] shr 3) or (r[2] shl 5);
  1022. res[3]:=(r[2] shr 3) or (r[3] shl 5);
  1023. res[4]:=(r[3] shr 3) or (r[4] shl 5);
  1024. res[5]:=(r[4] shr 3) or (r[5] and $7f) shl 5;
  1025. res[6]:=(r[5] and $7f) shr 3;
  1026. { copy exponent }
  1027. { correct exponent: }
  1028. exponent:=(word(r[0])+(1023-129));
  1029. res[6]:=res[6] or ((exponent and $f) shl 4);
  1030. res[7]:=exponent shr 4;
  1031. { set sign }
  1032. res[7]:=res[7] or (r[5] and $80);
  1033. real2double:=double(res);
  1034. end;
  1035. {$endif FPC_SYSTEM_HAS_REAL2DOUBLE}
  1036. {$endif SUPPORT_DOUBLE}
  1037. {$ifdef SUPPORT_EXTENDED}
  1038. { fast 10^n routine }
  1039. function FPower10(val: Extended; Power: Longint): Extended;
  1040. const
  1041. pow32 : array[0..31] of extended =
  1042. (
  1043. 1e0,1e1,1e2,1e3,1e4,1e5,1e6,1e7,1e8,1e9,1e10,
  1044. 1e11,1e12,1e13,1e14,1e15,1e16,1e17,1e18,1e19,1e20,
  1045. 1e21,1e22,1e23,1e24,1e25,1e26,1e27,1e28,1e29,1e30,
  1046. 1e31
  1047. );
  1048. pow512 : array[0..15] of extended =
  1049. (
  1050. 1,1e32,1e64,1e96,1e128,1e160,1e192,1e224,
  1051. 1e256,1e288,1e320,1e352,1e384,1e416,1e448,
  1052. 1e480
  1053. );
  1054. pow4096 : array[0..9] of extended =
  1055. (1,1e512,1e1024,1e1536,
  1056. 1e2048,1e2560,1e3072,1e3584,
  1057. 1e4096,1e4608
  1058. );
  1059. negpow32 : array[0..31] of extended =
  1060. (
  1061. 1e-0,1e-1,1e-2,1e-3,1e-4,1e-5,1e-6,1e-7,1e-8,1e-9,1e-10,
  1062. 1e-11,1e-12,1e-13,1e-14,1e-15,1e-16,1e-17,1e-18,1e-19,1e-20,
  1063. 1e-21,1e-22,1e-23,1e-24,1e-25,1e-26,1e-27,1e-28,1e-29,1e-30,
  1064. 1e-31
  1065. );
  1066. negpow512 : array[0..15] of extended =
  1067. (
  1068. 0,1e-32,1e-64,1e-96,1e-128,1e-160,1e-192,1e-224,
  1069. 1e-256,1e-288,1e-320,1e-352,1e-384,1e-416,1e-448,
  1070. 1e-480
  1071. );
  1072. negpow4096 : array[0..9] of extended =
  1073. (
  1074. 0,1e-512,1e-1024,1e-1536,
  1075. 1e-2048,1e-2560,1e-3072,1e-3584,
  1076. 1e-4096,1e-4608
  1077. );
  1078. begin
  1079. if Power<0 then
  1080. begin
  1081. Power:=-Power;
  1082. result:=val*negpow32[Power and $1f];
  1083. power:=power shr 5;
  1084. if power<>0 then
  1085. begin
  1086. result:=result*negpow512[Power and $f];
  1087. power:=power shr 4;
  1088. if power<>0 then
  1089. begin
  1090. if power<=9 then
  1091. result:=result*negpow4096[Power]
  1092. else
  1093. result:=1.0/0.0;
  1094. end;
  1095. end;
  1096. end
  1097. else
  1098. begin
  1099. result:=val*pow32[Power and $1f];
  1100. power:=power shr 5;
  1101. if power<>0 then
  1102. begin
  1103. result:=result*pow512[Power and $f];
  1104. power:=power shr 4;
  1105. if power<>0 then
  1106. begin
  1107. if power<=9 then
  1108. result:=result*pow4096[Power]
  1109. else
  1110. result:=1.0/0.0;
  1111. end;
  1112. end;
  1113. end;
  1114. end;
  1115. {$endif SUPPORT_EXTENDED}