genmath.inc 64 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920
  1. {
  2. This file is part of the Free Pascal run time library.
  3. Copyright (c) 1999-2007 by Several contributors
  4. Generic mathematical 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. {-------------------------------------------------------------------------
  34. Using functions from AMath/DAMath libraries, which are covered by the
  35. following license:
  36. (C) Copyright 2009-2013 Wolfgang Ehrhardt
  37. This software is provided 'as-is', without any express or implied warranty.
  38. In no event will the authors be held liable for any damages arising from
  39. the use of this software.
  40. Permission is granted to anyone to use this software for any purpose,
  41. including commercial applications, and to alter it and redistribute it
  42. freely, subject to the following restrictions:
  43. 1. The origin of this software must not be misrepresented; you must not
  44. claim that you wrote the original software. If you use this software in
  45. a product, an acknowledgment in the product documentation would be
  46. appreciated but is not required.
  47. 2. Altered source versions must be plainly marked as such, and must not be
  48. misrepresented as being the original software.
  49. 3. This notice may not be removed or altered from any source distribution.
  50. ----------------------------------------------------------------------------}
  51. type
  52. PReal = ^Real;
  53. { also necessary for Int() on systems with 64bit floats (JM) }
  54. {$ifndef FPC_SYSTEM_HAS_float64}
  55. {$ifdef ENDIAN_LITTLE}
  56. float64 = record
  57. {$ifndef FPC_DOUBLE_HILO_SWAPPED}
  58. low,high: longint;
  59. {$else}
  60. high,low: longint;
  61. {$endif FPC_DOUBLE_HILO_SWAPPED}
  62. end;
  63. {$else}
  64. float64 = record
  65. {$ifndef FPC_DOUBLE_HILO_SWAPPED}
  66. high,low: longint;
  67. {$else}
  68. low,high: longint;
  69. {$endif FPC_DOUBLE_HILO_SWAPPED}
  70. end;
  71. {$endif}
  72. {$endif FPC_SYSTEM_HAS_float64}
  73. const
  74. PIO4 = 7.85398163397448309616E-1; { pi/4 }
  75. SQRT2 = 1.41421356237309504880; { sqrt(2) }
  76. LOG2E = 1.4426950408889634073599; { 1/log(2) }
  77. lossth = 1.073741824e9;
  78. MAXLOG = 8.8029691931113054295988E1; { log(2**127) }
  79. MINLOG = -8.872283911167299960540E1; { log(2**-128) }
  80. zero: double = 0;
  81. {$if not defined(FPC_SYSTEM_HAS_SIN) or not defined(FPC_SYSTEM_HAS_COS)}
  82. const sincof : array[0..5] of Real = (
  83. 1.58962301576546568060E-10,
  84. -2.50507477628578072866E-8,
  85. 2.75573136213857245213E-6,
  86. -1.98412698295895385996E-4,
  87. 8.33333333332211858878E-3,
  88. -1.66666666666666307295E-1);
  89. coscof : array[0..5] of Real = (
  90. -1.13585365213876817300E-11,
  91. 2.08757008419747316778E-9,
  92. -2.75573141792967388112E-7,
  93. 2.48015872888517045348E-5,
  94. -1.38888888888730564116E-3,
  95. 4.16666666666665929218E-2);
  96. {$endif}
  97. {*
  98. -------------------------------------------------------------------------------
  99. Raises the exceptions specified by `flags'. Floating-point traps can be
  100. defined here if desired. It is currently not possible for such a trap
  101. to substitute a result value. If traps are not implemented, this routine
  102. should be simply `softfloat_exception_flags |= flags;'.
  103. -------------------------------------------------------------------------------
  104. *}
  105. procedure float_raise(i: TFPUException);
  106. begin
  107. float_raise([i]);
  108. end;
  109. procedure float_raise(i: TFPUExceptionMask);
  110. var
  111. pflags: ^TFPUExceptionMask;
  112. unmasked_flags: TFPUExceptionMask;
  113. Begin
  114. { taking address of threadvar produces somewhat more compact code }
  115. pflags := @softfloat_exception_flags;
  116. pflags^:=pflags^ + i;
  117. unmasked_flags := pflags^ - softfloat_exception_mask;
  118. if (float_flag_invalid in unmasked_flags) then
  119. HandleError(207)
  120. else
  121. if (float_flag_divbyzero in unmasked_flags) then
  122. HandleError(200)
  123. else
  124. if (float_flag_overflow in unmasked_flags) then
  125. HandleError(205)
  126. else
  127. if (float_flag_underflow in unmasked_flags) then
  128. HandleError(206)
  129. else
  130. if (float_flag_inexact in unmasked_flags) then
  131. HandleError(207);
  132. end;
  133. { This function does nothing, but its argument is expected to be an expression
  134. which causes FPE when calculated. If exception is masked, it just returns true,
  135. allowing to use it in expressions. }
  136. function fpe_helper(x: valreal): boolean;
  137. begin
  138. result:=true;
  139. end;
  140. {$ifdef SUPPORT_DOUBLE}
  141. {$ifndef FPC_HAS_FLOAT64HIGH}
  142. {$define FPC_HAS_FLOAT64HIGH}
  143. function float64high(d: double): longint; inline;
  144. begin
  145. result:=float64(d).high;
  146. end;
  147. procedure float64sethigh(var d: double; l: longint); inline;
  148. begin
  149. float64(d).high:=l;
  150. end;
  151. {$endif FPC_HAS_FLOAT64HIGH}
  152. {$ifndef FPC_HAS_FLOAT64LOW}
  153. {$define FPC_HAS_FLOAT64LOW}
  154. function float64low(d: double): longint; inline;
  155. begin
  156. result:=float64(d).low;
  157. end;
  158. procedure float64setlow(var d: double; l: longint); inline;
  159. begin
  160. float64(d).low:=l;
  161. end;
  162. {$endif FPC_HAS_FLOAT64LOW}
  163. {$endif SUPPORT_DOUBLE}
  164. {$ifndef FPC_SYSTEM_HAS_TRUNC}
  165. {$ifndef FPC_SYSTEM_HAS_float32}
  166. type
  167. float32 = longint;
  168. {$endif FPC_SYSTEM_HAS_float32}
  169. {$ifdef SUPPORT_DOUBLE}
  170. { based on softfloat float64_to_int64_round_to_zero }
  171. function fpc_trunc_real(d : valreal) : int64; compilerproc;
  172. var
  173. aExp, shiftCount : smallint;
  174. aSig : int64;
  175. z : int64;
  176. a: float64;
  177. begin
  178. a:=float64(d);
  179. aSig:=(int64(a.high and $000fffff) shl 32) or longword(a.low);
  180. aExp:=(a.high shr 20) and $7FF;
  181. if aExp<>0 then
  182. aSig:=aSig or $0010000000000000;
  183. shiftCount:= aExp-$433;
  184. if 0<=shiftCount then
  185. begin
  186. if aExp>=$43e then
  187. begin
  188. if (a.high<>longint($C3E00000)) or (a.low<>0) then
  189. begin
  190. fpe_helper(zero/zero);
  191. if (longint(a.high)>=0) or ((aExp=$7FF) and
  192. (aSig<>$0010000000000000 )) then
  193. begin
  194. result:=$7FFFFFFFFFFFFFFF;
  195. exit;
  196. end;
  197. end;
  198. result:=$8000000000000000;
  199. exit;
  200. end;
  201. z:=aSig shl shiftCount;
  202. end
  203. else
  204. begin
  205. if aExp<$3fe then
  206. begin
  207. result:=0;
  208. exit;
  209. end;
  210. z:=aSig shr -shiftCount;
  211. {
  212. if (aSig shl (shiftCount and 63))<>0 then
  213. float_exception_flags |= float_flag_inexact;
  214. }
  215. end;
  216. if longint(a.high)<0 then
  217. z:=-z;
  218. result:=z;
  219. end;
  220. {$else SUPPORT_DOUBLE}
  221. { based on softfloat float32_to_int64_round_to_zero }
  222. Function fpc_trunc_real( d: valreal ): int64; compilerproc;
  223. Var
  224. a : float32;
  225. aExp, shiftCount : smallint;
  226. aSig : longint;
  227. aSig64, z : int64;
  228. Begin
  229. a := float32(d);
  230. aSig := a and $007FFFFF;
  231. aExp := (a shr 23) and $FF;
  232. shiftCount := aExp - $BE;
  233. if ( 0 <= shiftCount ) then
  234. Begin
  235. if ( a <> Float32($DF000000) ) then
  236. Begin
  237. fpe_helper( zero/zero );
  238. if ( (longint(a)>=0) or ( ( aExp = $FF ) and (aSig<>0) ) ) then
  239. Begin
  240. result:=$7fffffffffffffff;
  241. exit;
  242. end;
  243. End;
  244. result:=$8000000000000000;
  245. exit;
  246. End
  247. else
  248. if ( aExp <= $7E ) then
  249. Begin
  250. result := 0;
  251. exit;
  252. End;
  253. aSig64 := int64( aSig or $00800000 ) shl 40;
  254. z := aSig64 shr ( - shiftCount );
  255. if ( longint(a)<0 ) then z := - z;
  256. result := z;
  257. End;
  258. {$endif SUPPORT_DOUBLE}
  259. {$endif not FPC_SYSTEM_HAS_TRUNC}
  260. {$ifndef FPC_SYSTEM_HAS_INT}
  261. {$ifdef SUPPORT_DOUBLE}
  262. { straight Pascal translation of the code for __trunc() in }
  263. { the file sysdeps/libm-ieee754/s_trunc.c of glibc (JM) }
  264. function fpc_int_real(d: ValReal): ValReal;compilerproc;
  265. var
  266. i0, j0: longint;
  267. i1: cardinal;
  268. sx: longint;
  269. f64 : float64;
  270. begin
  271. f64:=float64(d);
  272. i0 := f64.high;
  273. i1 := cardinal(f64.low);
  274. sx := i0 and $80000000;
  275. j0 := ((i0 shr 20) and $7ff) - $3ff;
  276. if (j0 < 20) then
  277. begin
  278. if (j0 < 0) then
  279. begin
  280. { the magnitude of the number is < 1 so the result is +-0. }
  281. f64.high := sx;
  282. f64.low := 0;
  283. end
  284. else
  285. begin
  286. f64.high := sx or (i0 and not($fffff shr j0));
  287. f64.low := 0;
  288. end
  289. end
  290. else if (j0 > 51) then
  291. begin
  292. if (j0 = $400) then
  293. { d is inf or NaN }
  294. exit(d + d); { don't know why they do this (JM) }
  295. end
  296. else
  297. begin
  298. f64.high := i0;
  299. f64.low := longint(i1 and not(cardinal($ffffffff) shr (j0 - 20)));
  300. end;
  301. result:=double(f64);
  302. end;
  303. {$else SUPPORT_DOUBLE}
  304. function fpc_int_real(d : ValReal) : ValReal;compilerproc;
  305. begin
  306. { this will be correct since real = single in the case of }
  307. { the motorola version of the compiler... }
  308. result:=ValReal(trunc(d));
  309. end;
  310. {$endif SUPPORT_DOUBLE}
  311. {$endif not FPC_SYSTEM_HAS_INT}
  312. {$ifndef FPC_SYSTEM_HAS_ABS}
  313. function fpc_abs_real(d : ValReal) : ValReal;compilerproc;
  314. begin
  315. if (d<0.0) then
  316. result := -d
  317. else
  318. result := d ;
  319. end;
  320. {$endif not FPC_SYSTEM_HAS_ABS}
  321. {$ifndef SYSTEM_HAS_FREXP}
  322. function frexp(x:Real; out e:Integer ):Real;
  323. {* frexp() extracts the exponent from x. It returns an integer *}
  324. {* power of two to expnt and the significand between 0.5 and 1 *}
  325. {* to y. Thus x = y * 2**expn. *}
  326. begin
  327. e :=0;
  328. if (abs(x)<0.5) then
  329. While (abs(x)<0.5) do
  330. begin
  331. x := x*2;
  332. Dec(e);
  333. end
  334. else
  335. While (abs(x)>1) do
  336. begin
  337. x := x/2;
  338. Inc(e);
  339. end;
  340. frexp := x;
  341. end;
  342. {$endif not SYSTEM_HAS_FREXP}
  343. {$ifndef SYSTEM_HAS_LDEXP}
  344. {$ifdef SUPPORT_DOUBLE}
  345. { ldexpd function adapted from DAMath library (C) Copyright 2013 Wolfgang Ehrhardt }
  346. function ldexp( x: Real; N: Integer):Real;
  347. {* ldexp() multiplies x by 2**n. *}
  348. var
  349. i: integer;
  350. const
  351. H2_54: double = 18014398509481984.0; {2^54}
  352. huge: double = 1e300;
  353. begin
  354. i := (float64high(x) and $7ff00000) shr 20;
  355. {if +-INF, NaN, 0 or if e=0 return d}
  356. if (i=$7FF) or (N=0) or (x=0.0) then
  357. ldexp := x
  358. else if i=0 then {Denormal: result = d*2^54*2^(e-54)}
  359. ldexp := ldexp(x*H2_54, N-54)
  360. else
  361. begin
  362. N := N+i;
  363. if N>$7FE then { overflow }
  364. begin
  365. if x>0.0 then
  366. ldexp := 2.0*huge
  367. else
  368. ldexp := (-2.0)*huge;
  369. end
  370. else if N<1 then
  371. begin
  372. {underflow or denormal}
  373. if N<-53 then
  374. ldexp := 0.0
  375. else
  376. begin
  377. {Denormal: result = d*2^(e+54)/2^54}
  378. inc(N,54);
  379. float64sethigh(x,(float64high(x) and $800FFFFF) or (longint(N) shl 20));
  380. ldexp := x/H2_54;
  381. end;
  382. end
  383. else
  384. begin
  385. float64sethigh(x,(float64high(x) and $800FFFFF) or (longint(N) shl 20));
  386. ldexp := x;
  387. end;
  388. end;
  389. end;
  390. {$else SUPPORT_DOUBLE}
  391. function ldexp( x: Real; N: Integer):Real;
  392. {* ldexp() multiplies x by 2**n. *}
  393. var r : Real;
  394. begin
  395. R := 1;
  396. if N>0 then
  397. while N>0 do
  398. begin
  399. R:=R*2;
  400. Dec(N);
  401. end
  402. else
  403. while N<0 do
  404. begin
  405. R:=R/2;
  406. Inc(N);
  407. end;
  408. ldexp := x * R;
  409. end;
  410. {$endif SUPPORT_DOUBLE}
  411. {$endif not SYSTEM_HAS_LDEXP}
  412. function polevl(x:Real; Coef:PReal; N:sizeint):Real;
  413. {*****************************************************************}
  414. { Evaluate polynomial }
  415. {*****************************************************************}
  416. { }
  417. { SYNOPSIS: }
  418. { }
  419. { int N; }
  420. { double x, y, coef[N+1], polevl[]; }
  421. { }
  422. { y = polevl( x, coef, N ); }
  423. { }
  424. { DESCRIPTION: }
  425. { }
  426. { Evaluates polynomial of degree N: }
  427. { }
  428. { 2 N }
  429. { y = C + C x + C x +...+ C x }
  430. { 0 1 2 N }
  431. { }
  432. { Coefficients are stored in reverse order: }
  433. { }
  434. { coef[0] = C , ..., coef[N] = C . }
  435. { N 0 }
  436. { }
  437. { The function p1evl() assumes that coef[N] = 1.0 and is }
  438. { omitted from the array. Its calling arguments are }
  439. { otherwise the same as polevl(). }
  440. { }
  441. { SPEED: }
  442. { }
  443. { In the interest of speed, there are no checks for out }
  444. { of bounds arithmetic. This routine is used by most of }
  445. { the functions in the library. Depending on available }
  446. { equipment features, the user may wish to rewrite the }
  447. { program in microcode or assembly language. }
  448. {*****************************************************************}
  449. var ans : Real;
  450. i : sizeint;
  451. begin
  452. ans := Coef[0];
  453. for i:=1 to N do
  454. ans := ans * x + Coef[i];
  455. polevl:=ans;
  456. end;
  457. function p1evl(x:Real; Coef:PReal; N:sizeint):Real;
  458. { }
  459. { Evaluate polynomial when coefficient of x is 1.0. }
  460. { Otherwise same as polevl. }
  461. { }
  462. var
  463. ans : Real;
  464. i : sizeint;
  465. begin
  466. ans := x + Coef[0];
  467. for i:=1 to N-1 do
  468. ans := ans * x + Coef[i];
  469. p1evl := ans;
  470. end;
  471. function floord(x: double): double;
  472. var
  473. t: double;
  474. begin
  475. t := int(x);
  476. if (x>=0.0) or (x=t) then
  477. floord := t
  478. else
  479. floord := t - 1.0;
  480. end;
  481. {*
  482. * ====================================================
  483. * Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved.
  484. *
  485. * Developed at SunPro, a Sun Microsystems, Inc. business.
  486. * Permission to use, copy, modify, and distribute this
  487. * software is freely granted, provided that this notice
  488. * is preserved.
  489. * ====================================================
  490. *
  491. * Pascal port of this routine comes from DAMath library
  492. * (C) Copyright 2013 Wolfgang Ehrhardt
  493. *
  494. * k_rem_pio2 return the last three bits of N with y = x - N*pi/2
  495. * so that |y| < pi/2.
  496. *
  497. * The method is to compute the integer (mod 8) and fraction parts of
  498. * (2/pi)*x without doing the full multiplication. In general we
  499. * skip the part of the product that are known to be a huge integer
  500. * (more accurately, = 0 mod 8 ). Thus the number of operations are
  501. * independent of the exponent of the input.
  502. *
  503. * (2/pi) is represented by an array of 24-bit integers in ipio2[].
  504. *
  505. * Input parameters:
  506. * x[] The input value (must be positive) is broken into nx
  507. * pieces of 24-bit integers in double precision format.
  508. * x[i] will be the i-th 24 bit of x. The scaled exponent
  509. * of x[0] is given in input parameter e0 (i.e., x[0]*2^e0
  510. * match x's up to 24 bits.
  511. *
  512. * Example of breaking a double positive z into x[0]+x[1]+x[2]:
  513. * e0 = ilogb(z)-23
  514. * z = scalbn(z,-e0)
  515. * for i = 0,1,2
  516. * x[i] = floor(z)
  517. * z = (z-x[i])*2**24
  518. *
  519. *
  520. * y[] output result in an array of double precision numbers.
  521. * The dimension of y[] is:
  522. * 24-bit precision 1
  523. * 53-bit precision 2
  524. * 64-bit precision 2
  525. * 113-bit precision 3
  526. * The actual value is the sum of them. Thus for 113-bit
  527. * precison, one may have to do something like:
  528. *
  529. * long double t,w,r_head, r_tail;
  530. * t = (long double)y[2] + (long double)y[1];
  531. * w = (long double)y[0];
  532. * r_head = t+w;
  533. * r_tail = w - (r_head - t);
  534. *
  535. * e0 The exponent of x[0]. Must be <= 16360 or you need to
  536. * expand the ipio2 table.
  537. *
  538. * nx dimension of x[]
  539. *
  540. * prec an integer indicating the precision:
  541. * 0 24 bits (single)
  542. * 1 53 bits (double)
  543. * 2 64 bits (extended)
  544. * 3 113 bits (quad)
  545. *
  546. * Here is the description of some local variables:
  547. *
  548. * jk jk+1 is the initial number of terms of ipio2[] needed
  549. * in the computation. The recommended value is 2,3,4,
  550. * 6 for single, double, extended,and quad.
  551. *
  552. * jz local integer variable indicating the number of
  553. * terms of ipio2[] used.
  554. *
  555. * jx nx - 1
  556. *
  557. * jv index for pointing to the suitable ipio2[] for the
  558. * computation. In general, we want
  559. * ( 2^e0*x[0] * ipio2[jv-1]*2^(-24jv) )/8
  560. * is an integer. Thus
  561. * e0-3-24*jv >= 0 or (e0-3)/24 >= jv
  562. * Hence jv = max(0,(e0-3)/24).
  563. *
  564. * jp jp+1 is the number of terms in PIo2[] needed, jp = jk.
  565. *
  566. * q[] double array with integral value, representing the
  567. * 24-bits chunk of the product of x and 2/pi.
  568. *
  569. * q0 the corresponding exponent of q[0]. Note that the
  570. * exponent for q[i] would be q0-24*i.
  571. *
  572. * PIo2[] double precision array, obtained by cutting pi/2
  573. * into 24 bits chunks.
  574. *
  575. * f[] ipio2[] in floating point
  576. *
  577. * iq[] integer array by breaking up q[] in 24-bits chunk.
  578. *
  579. * fq[] final product of x*(2/pi) in fq[0],..,fq[jk]
  580. *
  581. * ih integer. If >0 it indicates q[] is >= 0.5, hence
  582. * it also indicates the *sign* of the result.
  583. *}
  584. {PIo2[] double array, obtained by cutting pi/2 into 24 bits chunks.}
  585. const
  586. PIo2chunked: array[0..7] of double = (
  587. 1.57079625129699707031e+00, { 0x3FF921FB, 0x40000000 }
  588. 7.54978941586159635335e-08, { 0x3E74442D, 0x00000000 }
  589. 5.39030252995776476554e-15, { 0x3CF84698, 0x80000000 }
  590. 3.28200341580791294123e-22, { 0x3B78CC51, 0x60000000 }
  591. 1.27065575308067607349e-29, { 0x39F01B83, 0x80000000 }
  592. 1.22933308981111328932e-36, { 0x387A2520, 0x40000000 }
  593. 2.73370053816464559624e-44, { 0x36E38222, 0x80000000 }
  594. 2.16741683877804819444e-51 { 0x3569F31D, 0x00000000 }
  595. );
  596. {Table of constants for 2/pi, 396 Hex digits (476 decimal) of 2/pi }
  597. ipio2: array[0..65] of longint = (
  598. $A2F983, $6E4E44, $1529FC, $2757D1, $F534DD, $C0DB62,
  599. $95993C, $439041, $FE5163, $ABDEBB, $C561B7, $246E3A,
  600. $424DD2, $E00649, $2EEA09, $D1921C, $FE1DEB, $1CB129,
  601. $A73EE8, $8235F5, $2EBB44, $84E99C, $7026B4, $5F7E41,
  602. $3991D6, $398353, $39F49C, $845F8B, $BDF928, $3B1FF8,
  603. $97FFDE, $05980F, $EF2F11, $8B5A0A, $6D1F6D, $367ECF,
  604. $27CB09, $B74F46, $3F669E, $5FEA2D, $7527BA, $C7EBE5,
  605. $F17B3D, $0739F7, $8A5292, $EA6BFB, $5FB11F, $8D5D08,
  606. $560330, $46FC7B, $6BABF0, $CFBC20, $9AF436, $1DA9E3,
  607. $91615E, $E61B08, $659985, $5F14A0, $68408D, $FFD880,
  608. $4D7327, $310606, $1556CA, $73A8C9, $60E27B, $C08C6B);
  609. init_jk: array[0..3] of integer = (2,3,4,6); {initial value for jk}
  610. two24: double = 16777216.0; {2^24}
  611. twon24: double = 5.9604644775390625e-08; {1/2^24}
  612. type
  613. TDA02 = array[0..2] of double; { 3 elements is enough for float128 }
  614. function k_rem_pio2(const x: TDA02; out y: TDA02; e0, nx, prec: integer): sizeint;
  615. var
  616. i,ih,j,jz,jx,jv,jp,jk,carry,k,n,q0: longint;
  617. t: longint;
  618. iq: array[0..19] of longint;
  619. f,fq,q: array[0..19] of double;
  620. z,fw: double;
  621. begin
  622. {initialize jk}
  623. jk := init_jk[prec];
  624. jp := jk;
  625. {determine jx,jv,q0, note that 3>q0}
  626. jx := nx-1;
  627. jv := (e0-3) div 24; if jv<0 then jv := 0;
  628. q0 := e0-24*(jv+1);
  629. {set up f[0] to f[jx+jk] where f[jx+jk] = ipio2[jv+jk]}
  630. j := jv-jx;
  631. for i:=0 to jx+jk do
  632. begin
  633. if j<0 then f[i] := 0.0 else f[i] := ipio2[j];
  634. inc(j);
  635. end;
  636. {compute q[0],q[1],...q[jk]}
  637. for i:=0 to jk do
  638. begin
  639. fw := 0.0;
  640. for j:=0 to jx do
  641. fw := fw + x[j]*f[jx+i-j];
  642. q[i] := fw;
  643. end;
  644. jz := jk;
  645. repeat
  646. {distill q[] into iq[] reversingly}
  647. i := 0;
  648. z := q[jz];
  649. for j:=jz downto 1 do
  650. begin
  651. fw := trunc(twon24*z);
  652. iq[i] := trunc(z-two24*fw);
  653. z := q[j-1]+fw;
  654. inc(i);
  655. end;
  656. {compute n}
  657. z := ldexp(z,q0); {actual value of z}
  658. z := z - 8.0*floord(z*0.125); {trim off integer >= 8}
  659. n := trunc(z);
  660. z := z - n;
  661. ih := 0;
  662. if q0>0 then
  663. begin
  664. {need iq[jz-1] to determine n}
  665. t := (iq[jz-1] shr (24-q0));
  666. inc(n,t);
  667. dec(iq[jz-1], t shl (24-q0));
  668. ih := iq[jz-1] shr (23-q0);
  669. end
  670. else if q0=0 then
  671. ih := iq[jz-1] shr 23
  672. else if z>=0.5 then
  673. ih := 2;
  674. if ih>0 then {q > 0.5}
  675. begin
  676. inc(n);
  677. carry := 0;
  678. for i:=0 to jz-1 do
  679. begin
  680. {compute 1-q}
  681. t := iq[i];
  682. if carry=0 then
  683. begin
  684. if t<>0 then
  685. begin
  686. carry := 1;
  687. iq[i] := $1000000 - t;
  688. end
  689. end
  690. else
  691. iq[i] := $ffffff - t;
  692. end;
  693. if q0>0 then
  694. begin
  695. {rare case: chance is 1 in 12}
  696. case q0 of
  697. 1: iq[jz-1] := iq[jz-1] and $7fffff;
  698. 2: iq[jz-1] := iq[jz-1] and $3fffff;
  699. end;
  700. end;
  701. if ih=2 then
  702. begin
  703. z := 1.0 - z;
  704. if carry<>0 then
  705. z := z - ldexp(1.0,q0);
  706. end;
  707. end;
  708. {check if recomputation is needed}
  709. if z<>0.0 then
  710. break;
  711. t := 0;
  712. for i:=jz-1 downto jk do
  713. t := t or iq[i];
  714. if t<>0 then
  715. break;
  716. {need recomputation}
  717. k := 1;
  718. while iq[jk-k]=0 do {k = no. of terms needed}
  719. inc(k);
  720. for i:=jz+1 to jz+k do
  721. begin
  722. {add q[jz+1] to q[jz+k]}
  723. f[jx+i] := ipio2[jv+i];
  724. fw := 0.0;
  725. for j:=0 to jx do
  726. fw := fw + x[j]*f[jx+i-j];
  727. q[i] := fw;
  728. end;
  729. inc(jz,k);
  730. until False;
  731. {chop off zero terms}
  732. if z=0.0 then
  733. begin
  734. repeat
  735. dec(jz);
  736. dec(q0,24);
  737. until iq[jz]<>0;
  738. end
  739. else
  740. begin
  741. {break z into 24-bit if necessary}
  742. z := ldexp(z,-q0);
  743. if z>=two24 then
  744. begin
  745. fw := trunc(twon24*z);
  746. iq[jz] := trunc(z-two24*fw);
  747. inc(jz);
  748. inc(q0,24);
  749. iq[jz] := trunc(fw);
  750. end
  751. else
  752. iq[jz] := trunc(z);
  753. end;
  754. {convert integer "bit" chunk to floating-point value}
  755. fw := ldexp(1.0,q0);
  756. for i:=jz downto 0 do
  757. begin
  758. q[i] := fw*iq[i];
  759. fw := fw*twon24;
  760. end;
  761. {compute PIo2[0,...,jp]*q[jz,...,0]}
  762. for i:=jz downto 0 do
  763. begin
  764. fw :=0.0;
  765. k := 0;
  766. while (k<=jp) and (k<=jz-i) do
  767. begin
  768. fw := fw + double(PIo2chunked[k])*(q[i+k]);
  769. inc(k);
  770. end;
  771. fq[jz-i] := fw;
  772. end;
  773. {compress fq[] into y[]}
  774. case prec of
  775. 0:
  776. begin
  777. fw := 0.0;
  778. for i:=jz downto 0 do
  779. fw := fw + fq[i];
  780. if ih=0 then
  781. y[0] := fw
  782. else
  783. y[0] := -fw;
  784. end;
  785. 1, 2:
  786. begin
  787. fw := 0.0;
  788. for i:=jz downto 0 do
  789. fw := fw + fq[i];
  790. if ih=0 then
  791. y[0] := fw
  792. else
  793. y[0] := -fw;
  794. fw := fq[0]-fw;
  795. for i:=1 to jz do
  796. fw := fw + fq[i];
  797. if ih=0 then
  798. y[1] := fw
  799. else
  800. y[1] := -fw;
  801. end;
  802. 3:
  803. begin
  804. {painful}
  805. for i:=jz downto 1 do
  806. begin
  807. fw := fq[i-1]+fq[i];
  808. fq[i] := fq[i]+(fq[i-1]-fw);
  809. fq[i-1]:= fw;
  810. end;
  811. for i:=jz downto 2 do
  812. begin
  813. fw := fq[i-1]+fq[i];
  814. fq[i] := fq[i]+(fq[i-1]-fw);
  815. fq[i-1]:= fw;
  816. end;
  817. fw := 0.0;
  818. for i:=jz downto 2 do
  819. fw := fw + fq[i];
  820. if ih=0 then
  821. begin
  822. y[0] := fq[0];
  823. y[1] := fq[1];
  824. y[2] := fw;
  825. end
  826. else
  827. begin
  828. y[0] := -fq[0];
  829. y[1] := -fq[1];
  830. y[2] := -fw;
  831. end;
  832. end;
  833. end;
  834. k_rem_pio2 := n and 7;
  835. end;
  836. { Argument reduction of x: z = x - n*Pi/2, |z| <= Pi/4, result = n mod 8.}
  837. { Uses Payne/Hanek if |x| >= lossth, Cody/Waite otherwise}
  838. function rem_pio2(x: double; out z: double): sizeint;
  839. const
  840. tol: double = 2.384185791015625E-7; {lossth*eps_d}
  841. DP1 = double(7.85398125648498535156E-1);
  842. DP2 = double(3.77489470793079817668E-8);
  843. DP3 = double(2.69515142907905952645E-15);
  844. var
  845. i,e0,nx: longint;
  846. y: double;
  847. tx,ty: TDA02;
  848. begin
  849. y := abs(x);
  850. if (y < PIO4) then
  851. begin
  852. z := x;
  853. result := 0;
  854. exit;
  855. end
  856. else if (y < lossth) then
  857. begin
  858. y := floord(x/PIO4);
  859. i := trunc(y - 16.0*floord(y*0.0625));
  860. if odd(i) then
  861. begin
  862. inc(i);
  863. y := y + 1.0;
  864. end;
  865. z := ((x - y * DP1) - y * DP2) - y * DP3;
  866. result := (i shr 1) and 7;
  867. {If x is near a multiple of Pi/2, the C/W relative error may be large.}
  868. {In this case redo the calculation with the Payne/Hanek algorithm. }
  869. if abs(z) > tol then
  870. exit;
  871. end;
  872. z := abs(x);
  873. e0 := (float64high(z) shr 20)-1046;
  874. float64sethigh(z,float64high(z) - (e0 shl 20));
  875. tx[0] := trunc(z);
  876. z := (z-tx[0])*two24;
  877. tx[1] := trunc(z);
  878. tx[2] := (z-tx[1])*two24;
  879. nx := 3;
  880. while (tx[nx-1]=0.0) do dec(nx); { skip zero terms }
  881. result := k_rem_pio2(tx,ty,e0,nx,2);
  882. if (x<0) then
  883. begin
  884. result := (-result) and 7;
  885. z := -ty[0] - ty[1];
  886. end
  887. else
  888. z := ty[0] + ty[1];
  889. end;
  890. {$ifndef FPC_SYSTEM_HAS_SQR}
  891. function fpc_sqr_real(d : ValReal) : ValReal;compilerproc;{$ifdef MATHINLINE}inline;{$endif}
  892. begin
  893. result := d*d;
  894. end;
  895. {$endif}
  896. {$ifndef FPC_SYSTEM_HAS_SQRT}
  897. function fpc_sqrt_real(d:ValReal):ValReal;compilerproc;
  898. {*****************************************************************}
  899. { Square root }
  900. {*****************************************************************}
  901. { }
  902. { SYNOPSIS: }
  903. { }
  904. { double x, y, sqrt(); }
  905. { }
  906. { y = sqrt( x ); }
  907. { }
  908. { DESCRIPTION: }
  909. { }
  910. { Returns the square root of x. }
  911. { }
  912. { Range reduction involves isolating the power of two of the }
  913. { argument and using a polynomial approximation to obtain }
  914. { a rough value for the square root. Then Heron's iteration }
  915. { is used three times to converge to an accurate value. }
  916. {*****************************************************************}
  917. var e : Integer;
  918. w,z : Real;
  919. begin
  920. if( d <= 0.0 ) then
  921. begin
  922. if d < 0.0 then
  923. result:=(d-d)/zero
  924. else
  925. result := 0.0;
  926. end
  927. else
  928. begin
  929. w := d;
  930. { separate exponent and significand }
  931. z := frexp( d, e );
  932. { approximate square root of number between 0.5 and 1 }
  933. { relative error of approximation = 7.47e-3 }
  934. d := 4.173075996388649989089E-1 + 5.9016206709064458299663E-1 * z;
  935. { adjust for odd powers of 2 }
  936. if odd(e) then
  937. d := d*SQRT2;
  938. { re-insert exponent }
  939. d := ldexp( d, (e div 2) );
  940. { Newton iterations: }
  941. d := 0.5*(d + w/d);
  942. d := 0.5*(d + w/d);
  943. d := 0.5*(d + w/d);
  944. d := 0.5*(d + w/d);
  945. d := 0.5*(d + w/d);
  946. d := 0.5*(d + w/d);
  947. result := d;
  948. end;
  949. end;
  950. {$endif}
  951. {$ifndef FPC_SYSTEM_HAS_EXP}
  952. {$ifdef SUPPORT_DOUBLE}
  953. {
  954. This code was translated from uclib code, the original code
  955. had the following copyright notice:
  956. *
  957. * ====================================================
  958. * Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved.
  959. *
  960. * Developed at SunPro, a Sun Microsystems, Inc. business.
  961. * Permission to use, copy, modify, and distribute this
  962. * software is freely granted, provided that this notice
  963. * is preserved.
  964. * ====================================================
  965. *}
  966. {*
  967. * Returns the exponential of x.
  968. *
  969. * Method
  970. * 1. Argument reduction:
  971. * Reduce x to an r so that |r| <= 0.5*ln2 ~ 0.34658.
  972. * Given x, find r and integer k such that
  973. *
  974. * x = k*ln2 + r, |r| <= 0.5*ln2.
  975. *
  976. * Here r will be represented as r = hi-lo for better
  977. * accuracy.
  978. *
  979. * 2. Approximation of exp(r) by a special rational function on
  980. * the interval [0,0.34658]:
  981. * Write
  982. * R(r**2) = r*(exp(r)+1)/(exp(r)-1) = 2 + r*r/6 - r**4/360 + ...
  983. * We use a special Reme algorithm on [0,0.34658] to generate
  984. * a polynomial of degree 5 to approximate R. The maximum error
  985. * of this polynomial approximation is bounded by 2**-59. In
  986. * other words,
  987. * R(z) ~ 2.0 + P1*z + P2*z**2 + P3*z**3 + P4*z**4 + P5*z**5
  988. * (where z=r*r, and the values of P1 to P5 are listed below)
  989. * and
  990. * | 5 | -59
  991. * | 2.0+P1*z+...+P5*z - R(z) | <= 2
  992. * | |
  993. * The computation of exp(r) thus becomes
  994. * 2*r
  995. * exp(r) = 1 + -------
  996. * R - r
  997. * r*R1(r)
  998. * = 1 + r + ----------- (for better accuracy)
  999. * 2 - R1(r)
  1000. * where
  1001. 2 4 10
  1002. * R1(r) = r - (P1*r + P2*r + ... + P5*r ).
  1003. *
  1004. * 3. Scale back to obtain exp(x):
  1005. * From step 1, we have
  1006. * exp(x) = 2^k * exp(r)
  1007. *
  1008. * Special cases:
  1009. * exp(INF) is INF, exp(NaN) is NaN;
  1010. * exp(-INF) is 0, and
  1011. * for finite argument, only exp(0)=1 is exact.
  1012. *
  1013. * Accuracy:
  1014. * according to an error analysis, the error is always less than
  1015. * 1 ulp (unit in the last place).
  1016. *
  1017. * Misc. info.
  1018. * For IEEE double
  1019. * if x > 7.09782712893383973096e+02 then exp(x) overflow
  1020. * if x < -7.45133219101941108420e+02 then exp(x) underflow
  1021. *
  1022. * Constants:
  1023. * The hexadecimal values are the intended ones for the following
  1024. * constants. The decimal values may be used, provided that the
  1025. * compiler will convert from decimal to binary accurately enough
  1026. * to produce the hexadecimal values shown.
  1027. *
  1028. }
  1029. function fpc_exp_real(d: ValReal):ValReal;compilerproc;
  1030. const
  1031. one: double = 1.0;
  1032. halF : array[0..1] of double = (0.5,-0.5);
  1033. huge: double = 1.0e+300;
  1034. twom1000: double = 9.33263618503218878990e-302; { 2**-1000=0x01700000,0}
  1035. o_threshold: double = 7.09782712893383973096e+02; { 0x40862E42, 0xFEFA39EF }
  1036. u_threshold: double = -7.45133219101941108420e+02; { 0xc0874910, 0xD52D3051 }
  1037. ln2HI : array[0..1] of double = ( 6.93147180369123816490e-01, { 0x3fe62e42, 0xfee00000 }
  1038. -6.93147180369123816490e-01); { 0xbfe62e42, 0xfee00000 }
  1039. ln2LO : array[0..1] of double = (1.90821492927058770002e-10, { 0x3dea39ef, 0x35793c76 }
  1040. -1.90821492927058770002e-10); { 0xbdea39ef, 0x35793c76 }
  1041. invln2: double = 1.44269504088896338700e+00; { 0x3ff71547, 0x652b82fe }
  1042. P1: double = 1.66666666666666019037e-01; { 0x3FC55555, 0x5555553E }
  1043. P2: double = -2.77777777770155933842e-03; { 0xBF66C16C, 0x16BEBD93 }
  1044. P3: double = 6.61375632143793436117e-05; { 0x3F11566A, 0xAF25DE2C }
  1045. P4: double = -1.65339022054652515390e-06; { 0xBEBBBD41, 0xC5D26BF1 }
  1046. P5: double = 4.13813679705723846039e-08; { 0x3E663769, 0x72BEA4D0 }
  1047. var
  1048. c,hi,lo,t,y : double;
  1049. k,xsb : longint;
  1050. hx,hy,lx : dword;
  1051. begin
  1052. hi:=0.0;
  1053. lo:=0.0;
  1054. k:=0;
  1055. hx:=float64high(d);
  1056. xsb := (hx shr 31) and 1; { sign bit of d }
  1057. hx := hx and $7fffffff; { high word of |d| }
  1058. { filter out non-finite argument }
  1059. if hx >= $40862E42 then
  1060. begin { if |d|>=709.78... }
  1061. if hx >= $7ff00000 then
  1062. begin
  1063. lx:=float64low(d);
  1064. if ((hx and $fffff) or lx)<>0 then
  1065. begin
  1066. result:=d+d; { NaN }
  1067. exit;
  1068. end
  1069. else
  1070. begin
  1071. if xsb=0 then
  1072. result:=d
  1073. else
  1074. result:=0.0; { exp(+-inf)=(inf,0) }
  1075. exit;
  1076. end;
  1077. end;
  1078. if d > o_threshold then begin
  1079. result:=huge*huge; { overflow }
  1080. exit;
  1081. end;
  1082. if d < u_threshold then begin
  1083. result:=twom1000*twom1000; { underflow }
  1084. exit;
  1085. end;
  1086. end;
  1087. { argument reduction }
  1088. if hx > $3fd62e42 then
  1089. begin { if |d| > 0.5 ln2 }
  1090. if hx < $3FF0A2B2 then { and |d| < 1.5 ln2 }
  1091. begin
  1092. hi := d-ln2HI[xsb];
  1093. lo:=ln2LO[xsb];
  1094. k := 1-xsb-xsb;
  1095. end
  1096. else
  1097. begin
  1098. k := trunc(invln2*d+halF[xsb]);
  1099. t := k;
  1100. hi := d - t*ln2HI[0]; { t*ln2HI is exact here }
  1101. lo := t*ln2LO[0];
  1102. end;
  1103. d := hi - lo;
  1104. end
  1105. else if hx < $3e300000 then
  1106. begin { when |d|<2**-28 }
  1107. if huge+d>one then
  1108. begin
  1109. result:=one+d;{ trigger inexact }
  1110. exit;
  1111. end;
  1112. end
  1113. else
  1114. k := 0;
  1115. { d is now in primary range }
  1116. t:=d*d;
  1117. c:=d - t*(P1+t*(P2+t*(P3+t*(P4+t*P5))));
  1118. if k=0 then
  1119. begin
  1120. result:=one-((d*c)/(c-2.0)-d);
  1121. exit;
  1122. end
  1123. else
  1124. y := one-((lo-(d*c)/(2.0-c))-hi);
  1125. if k >= -1021 then
  1126. begin
  1127. hy:=float64high(y);
  1128. float64sethigh(y,longint(hy)+(k shl 20)); { add k to y's exponent }
  1129. result:=y;
  1130. end
  1131. else
  1132. begin
  1133. hy:=float64high(y);
  1134. float64sethigh(y,longint(hy)+((k+1000) shl 20)); { add k to y's exponent }
  1135. result:=y*twom1000;
  1136. end;
  1137. end;
  1138. {$else SUPPORT_DOUBLE}
  1139. function fpc_exp_real(d: ValReal):ValReal;compilerproc;
  1140. {*****************************************************************}
  1141. { Exponential Function }
  1142. {*****************************************************************}
  1143. { }
  1144. { SYNOPSIS: }
  1145. { }
  1146. { double x, y, exp(); }
  1147. { }
  1148. { y = exp( x ); }
  1149. { }
  1150. { DESCRIPTION: }
  1151. { }
  1152. { Returns e (2.71828...) raised to the x power. }
  1153. { }
  1154. { Range reduction is accomplished by separating the argument }
  1155. { into an integer k and fraction f such that }
  1156. { }
  1157. { x k f }
  1158. { e = 2 e. }
  1159. { }
  1160. { A Pade' form of degree 2/3 is used to approximate exp(f)- 1 }
  1161. { in the basic range [-0.5 ln 2, 0.5 ln 2]. }
  1162. {*****************************************************************}
  1163. const P : array[0..2] of Real = (
  1164. 1.26183092834458542160E-4,
  1165. 3.02996887658430129200E-2,
  1166. 1.00000000000000000000E0);
  1167. Q : array[0..3] of Real = (
  1168. 3.00227947279887615146E-6,
  1169. 2.52453653553222894311E-3,
  1170. 2.27266044198352679519E-1,
  1171. 2.00000000000000000005E0);
  1172. C1 = 6.9335937500000000000E-1;
  1173. C2 = 2.1219444005469058277E-4;
  1174. var n : Integer;
  1175. px, qx, xx : Real;
  1176. begin
  1177. if( d > MAXLOG) then
  1178. float_raise(float_flag_overflow)
  1179. else
  1180. if( d < MINLOG ) then
  1181. begin
  1182. float_raise(float_flag_underflow);
  1183. result:=0; { Result if underflow masked }
  1184. end
  1185. else
  1186. begin
  1187. { Express e**x = e**g 2**n }
  1188. { = e**g e**( n loge(2) ) }
  1189. { = e**( g + n loge(2) ) }
  1190. px := d * LOG2E;
  1191. qx := Trunc( px + 0.5 ); { Trunc() truncates toward -infinity. }
  1192. n := Trunc(qx);
  1193. d := d - qx * C1;
  1194. d := d + qx * C2;
  1195. { rational approximation for exponential }
  1196. { of the fractional part: }
  1197. { e**x - 1 = 2x P(x**2)/( Q(x**2) - P(x**2) ) }
  1198. xx := d * d;
  1199. px := d * polevl( xx, P, 2 );
  1200. d := px/( polevl( xx, Q, 3 ) - px );
  1201. d := ldexp( d, 1 );
  1202. d := d + 1.0;
  1203. d := ldexp( d, n );
  1204. result := d;
  1205. end;
  1206. end;
  1207. {$endif SUPPORT_DOUBLE}
  1208. {$endif}
  1209. {$ifndef FPC_SYSTEM_HAS_ROUND}
  1210. function fpc_round_real(d : ValReal) : int64;compilerproc;
  1211. var
  1212. tmp: double;
  1213. j0: longint;
  1214. hx: longword;
  1215. sx: longint;
  1216. const
  1217. H2_52: array[0..1] of double = (
  1218. 4.50359962737049600000e+15,
  1219. -4.50359962737049600000e+15
  1220. );
  1221. Begin
  1222. { This basically calculates trunc((d+2**52)-2**52) }
  1223. hx:=float64high(d);
  1224. j0:=((hx shr 20) and $7ff) - $3ff;
  1225. sx:=hx shr 31;
  1226. hx:=(hx and $fffff) or $100000;
  1227. if j0>=52 then { No fraction bits, already integer }
  1228. begin
  1229. if j0>=63 then { Overflow, let trunc() raise an exception }
  1230. exit(trunc(d)) { and/or return +/-MaxInt64 if it's masked }
  1231. else
  1232. result:=((int64(hx) shl 32) or float64low(d)) shl (j0-52);
  1233. end
  1234. else
  1235. begin
  1236. { Rounding happens here. It is important that the expression is not
  1237. optimized by selecting a larger type to store 'tmp'. }
  1238. tmp:=H2_52[sx]+d;
  1239. d:=tmp-H2_52[sx];
  1240. hx:=float64high(d);
  1241. j0:=((hx shr 20) and $7ff)-$3ff;
  1242. hx:=(hx and $fffff) or $100000;
  1243. if j0<=20 then
  1244. begin
  1245. if j0<0 then
  1246. exit(0)
  1247. else { more than 32 fraction bits, low dword discarded }
  1248. result:=hx shr (20-j0);
  1249. end
  1250. else
  1251. result:=(int64(hx) shl (j0-20)) or (float64low(d) shr (52-j0));
  1252. end;
  1253. if sx<>0 then
  1254. result:=-result;
  1255. end;
  1256. {$endif FPC_SYSTEM_HAS_ROUND}
  1257. {$ifndef FPC_SYSTEM_HAS_LN}
  1258. function fpc_ln_real(d:ValReal):ValReal;compilerproc;
  1259. {
  1260. This code was translated from uclib code, the original code
  1261. had the following copyright notice:
  1262. *
  1263. * ====================================================
  1264. * Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved.
  1265. *
  1266. * Developed at SunPro, a Sun Microsystems, Inc. business.
  1267. * Permission to use, copy, modify, and distribute this
  1268. * software is freely granted, provided that this notice
  1269. * is preserved.
  1270. * ====================================================
  1271. *}
  1272. {*****************************************************************}
  1273. { Natural Logarithm }
  1274. {*****************************************************************}
  1275. {*
  1276. * SYNOPSIS:
  1277. *
  1278. * double x, y, log();
  1279. *
  1280. * y = ln( x );
  1281. *
  1282. * DESCRIPTION:
  1283. *
  1284. * Returns the base e (2.718...) logarithm of x.
  1285. *
  1286. * Method :
  1287. * 1. Argument Reduction: find k and f such that
  1288. * x = 2^k * (1+f),
  1289. * where sqrt(2)/2 < 1+f < sqrt(2) .
  1290. *
  1291. * 2. Approximation of log(1+f).
  1292. * Let s = f/(2+f) ; based on log(1+f) = log(1+s) - log(1-s)
  1293. * = 2s + 2/3 s**3 + 2/5 s**5 + .....,
  1294. * = 2s + s*R
  1295. * We use a special Reme algorithm on [0,0.1716] to generate
  1296. * a polynomial of degree 14 to approximate R The maximum error
  1297. * of this polynomial approximation is bounded by 2**-58.45. In
  1298. * other words,
  1299. * 2 4 6 8 10 12 14
  1300. * R(z) ~ Lg1*s +Lg2*s +Lg3*s +Lg4*s +Lg5*s +Lg6*s +Lg7*s
  1301. * (the values of Lg1 to Lg7 are listed in the program)
  1302. * and
  1303. * | 2 14 | -58.45
  1304. * | Lg1*s +...+Lg7*s - R(z) | <= 2
  1305. * | |
  1306. * Note that 2s = f - s*f = f - hfsq + s*hfsq, where hfsq = f*f/2.
  1307. * In order to guarantee error in log below 1ulp, we compute log
  1308. * by
  1309. * log(1+f) = f - s*(f - R) (if f is not too large)
  1310. * log(1+f) = f - (hfsq - s*(hfsq+R)). (better accuracy)
  1311. *
  1312. * 3. Finally, log(x) = k*ln2 + log(1+f).
  1313. * = k*ln2_hi+(f-(hfsq-(s*(hfsq+R)+k*ln2_lo)))
  1314. * Here ln2 is split into two floating point number:
  1315. * ln2_hi + ln2_lo,
  1316. * where n*ln2_hi is always exact for |n| < 2000.
  1317. *
  1318. * Special cases:
  1319. * log(x) is NaN with signal if x < 0 (including -INF) ;
  1320. * log(+INF) is +INF; log(0) is -INF with signal;
  1321. * log(NaN) is that NaN with no signal.
  1322. *
  1323. * Accuracy:
  1324. * according to an error analysis, the error is always less than
  1325. * 1 ulp (unit in the last place).
  1326. *}
  1327. const
  1328. ln2_hi: double = 6.93147180369123816490e-01; { 3fe62e42 fee00000 }
  1329. ln2_lo: double = 1.90821492927058770002e-10; { 3dea39ef 35793c76 }
  1330. two54: double = 1.80143985094819840000e+16; { 43500000 00000000 }
  1331. Lg1: double = 6.666666666666735130e-01; { 3FE55555 55555593 }
  1332. Lg2: double = 3.999999999940941908e-01; { 3FD99999 9997FA04 }
  1333. Lg3: double = 2.857142874366239149e-01; { 3FD24924 94229359 }
  1334. Lg4: double = 2.222219843214978396e-01; { 3FCC71C5 1D8E78AF }
  1335. Lg5: double = 1.818357216161805012e-01; { 3FC74664 96CB03DE }
  1336. Lg6: double = 1.531383769920937332e-01; { 3FC39A09 D078C69F }
  1337. Lg7: double = 1.479819860511658591e-01; { 3FC2F112 DF3E5244 }
  1338. zero: double = 0.0;
  1339. var
  1340. hfsq,f,s,z,R,w,t1,t2,dk: double;
  1341. k,hx,i,j: longint;
  1342. lx: longword;
  1343. begin
  1344. hx := float64high(d);
  1345. lx := float64low(d);
  1346. k := 0;
  1347. if (hx < $00100000) then { x < 2**-1022 }
  1348. begin
  1349. if (((hx and $7fffffff) or lx)=0) then
  1350. exit(-two54/zero); { log(+-0)=-inf }
  1351. if (hx<0) then
  1352. exit((d-d)/zero); { log(-#) = NaN }
  1353. dec(k, 54); d := d * two54; { subnormal number, scale up x }
  1354. hx := float64high(d);
  1355. end;
  1356. if (hx >= $7ff00000) then
  1357. exit(d+d);
  1358. inc(k, (hx shr 20)-1023);
  1359. hx := hx and $000fffff;
  1360. i := (hx + $95f64) and $100000;
  1361. float64sethigh(d,hx or (i xor $3ff00000)); { normalize x or x/2 }
  1362. inc(k, (i shr 20));
  1363. f := d-1.0;
  1364. if (($000fffff and (2+hx))<3) then { |f| < 2**-20 }
  1365. begin
  1366. if (f=zero) then
  1367. begin
  1368. if (k=0) then
  1369. exit(zero)
  1370. else
  1371. begin
  1372. dk := k;
  1373. exit(dk*ln2_hi+dk*ln2_lo);
  1374. end;
  1375. end;
  1376. R := f*f*(0.5-0.33333333333333333*f);
  1377. if (k=0) then
  1378. exit(f-R)
  1379. else
  1380. begin
  1381. dk := k;
  1382. exit(dk*ln2_hi-((R-dk*ln2_lo)-f));
  1383. end;
  1384. end;
  1385. s := f/(2.0+f);
  1386. dk := k;
  1387. z := s*s;
  1388. i := hx-$6147a;
  1389. w := z*z;
  1390. j := $6b851-hx;
  1391. t1 := w*(Lg2+w*(Lg4+w*Lg6));
  1392. t2 := z*(Lg1+w*(Lg3+w*(Lg5+w*Lg7)));
  1393. i := i or j;
  1394. R := t2+t1;
  1395. if (i>0) then
  1396. begin
  1397. hfsq := 0.5*f*f;
  1398. if (k=0) then
  1399. result := f-(hfsq-s*(hfsq+R))
  1400. else
  1401. result := dk*ln2_hi-((hfsq-(s*(hfsq+R)+dk*ln2_lo))-f);
  1402. end
  1403. else
  1404. begin
  1405. if (k=0) then
  1406. result := f-s*(f-R)
  1407. else
  1408. result := dk*ln2_hi-((s*(f-R)-dk*ln2_lo)-f);
  1409. end;
  1410. end;
  1411. {$endif}
  1412. {$ifndef FPC_SYSTEM_HAS_SIN}
  1413. function fpc_Sin_real(d:ValReal):ValReal;compilerproc;
  1414. {*****************************************************************}
  1415. { Circular Sine }
  1416. {*****************************************************************}
  1417. { }
  1418. { SYNOPSIS: }
  1419. { }
  1420. { double x, y, sin(); }
  1421. { }
  1422. { y = sin( x ); }
  1423. { }
  1424. { DESCRIPTION: }
  1425. { }
  1426. { Range reduction is into intervals of pi/4. The reduction }
  1427. { error is nearly eliminated by contriving an extended }
  1428. { precision modular arithmetic. }
  1429. { }
  1430. { Two polynomial approximating functions are employed. }
  1431. { Between 0 and pi/4 the sine is approximated by }
  1432. { x + x**3 P(x**2). }
  1433. { Between pi/4 and pi/2 the cosine is represented as }
  1434. { 1 - x**2 Q(x**2). }
  1435. {*****************************************************************}
  1436. var y, z, zz : Real;
  1437. j : sizeint;
  1438. begin
  1439. j := rem_pio2(d,z) and 3;
  1440. zz := z * z;
  1441. if( (j=1) or (j=3) ) then
  1442. y := 1.0 - ldexp(zz,-1) + zz * zz * polevl( zz, coscof, 5 )
  1443. else
  1444. { y = z + z * (zz * polevl( zz, sincof, 5 )); }
  1445. y := z + z * z * z * polevl( zz, sincof, 5 );
  1446. if (j > 1) then
  1447. result := -y
  1448. else
  1449. result := y;
  1450. end;
  1451. {$endif}
  1452. {$ifndef FPC_SYSTEM_HAS_COS}
  1453. function fpc_Cos_real(d:ValReal):ValReal;compilerproc;
  1454. {*****************************************************************}
  1455. { Circular cosine }
  1456. {*****************************************************************}
  1457. { }
  1458. { Circular cosine }
  1459. { }
  1460. { SYNOPSIS: }
  1461. { }
  1462. { double x, y, cos(); }
  1463. { }
  1464. { y = cos( x ); }
  1465. { }
  1466. { DESCRIPTION: }
  1467. { }
  1468. { Range reduction is into intervals of pi/4. The reduction }
  1469. { error is nearly eliminated by contriving an extended }
  1470. { precision modular arithmetic. }
  1471. { }
  1472. { Two polynomial approximating functions are employed. }
  1473. { Between 0 and pi/4 the cosine is approximated by }
  1474. { 1 - x**2 Q(x**2). }
  1475. { Between pi/4 and pi/2 the sine is represented as }
  1476. { x + x**3 P(x**2). }
  1477. {*****************************************************************}
  1478. var y, z, zz : Real;
  1479. j : sizeint;
  1480. begin
  1481. j := rem_pio2(d,z) and 3;
  1482. zz := z * z;
  1483. if( (j=1) or (j=3) ) then
  1484. { y = z + z * (zz * polevl( zz, sincof, 5 )); }
  1485. y := z + z * z * z * polevl( zz, sincof, 5 )
  1486. else
  1487. y := 1.0 - ldexp(zz,-1) + zz * zz * polevl( zz, coscof, 5 );
  1488. if (j = 1) or (j = 2) then
  1489. result := -y
  1490. else
  1491. result := y ;
  1492. end;
  1493. {$endif}
  1494. {$ifndef FPC_SYSTEM_HAS_ARCTAN}
  1495. function fpc_ArcTan_real(d:ValReal):ValReal;compilerproc;
  1496. {
  1497. This code was translated from uclibc code, the original code
  1498. had the following copyright notice:
  1499. *
  1500. * ====================================================
  1501. * Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved.
  1502. *
  1503. * Developed at SunPro, a Sun Microsystems, Inc. business.
  1504. * Permission to use, copy, modify, and distribute this
  1505. * software is freely granted, provided that this notice
  1506. * is preserved.
  1507. * ====================================================
  1508. *}
  1509. {********************************************************************}
  1510. { Inverse circular tangent (arctangent) }
  1511. {********************************************************************}
  1512. { }
  1513. { SYNOPSIS: }
  1514. { }
  1515. { double x, y, atan(); }
  1516. { }
  1517. { y = atan( x ); }
  1518. { }
  1519. { DESCRIPTION: }
  1520. { }
  1521. { Returns radian angle between -pi/2 and +pi/2 whose tangent }
  1522. { is x. }
  1523. { }
  1524. { Method }
  1525. { 1. Reduce x to positive by atan(x) = -atan(-x). }
  1526. { 2. According to the integer k=4t+0.25 chopped, t=x, the argument }
  1527. { is further reduced to one of the following intervals and the }
  1528. { arctangent of t is evaluated by the corresponding formula: }
  1529. { }
  1530. { [0,7/16] atan(x) = t-t^3*(a1+t^2*(a2+...(a10+t^2*a11)...) }
  1531. { [7/16,11/16] atan(x) = atan(1/2) + atan( (t-0.5)/(1+t/2) ) }
  1532. { [11/16.19/16] atan(x) = atan( 1 ) + atan( (t-1)/(1+t) ) }
  1533. { [19/16,39/16] atan(x) = atan(3/2) + atan( (t-1.5)/(1+1.5t) ) }
  1534. { [39/16,INF] atan(x) = atan(INF) + atan( -1/t ) }
  1535. {********************************************************************}
  1536. const
  1537. atanhi: array [0..3] of double = (
  1538. 4.63647609000806093515e-01, { atan(0.5)hi 0x3FDDAC67, 0x0561BB4F }
  1539. 7.85398163397448278999e-01, { atan(1.0)hi 0x3FE921FB, 0x54442D18 }
  1540. 9.82793723247329054082e-01, { atan(1.5)hi 0x3FEF730B, 0xD281F69B }
  1541. 1.57079632679489655800e+00 { atan(inf)hi 0x3FF921FB, 0x54442D18 }
  1542. );
  1543. atanlo: array [0..3] of double = (
  1544. 2.26987774529616870924e-17, { atan(0.5)lo 0x3C7A2B7F, 0x222F65E2 }
  1545. 3.06161699786838301793e-17, { atan(1.0)lo 0x3C81A626, 0x33145C07 }
  1546. 1.39033110312309984516e-17, { atan(1.5)lo 0x3C700788, 0x7AF0CBBD }
  1547. 6.12323399573676603587e-17 { atan(inf)lo 0x3C91A626, 0x33145C07 }
  1548. );
  1549. aT: array[0..10] of double = (
  1550. 3.33333333333329318027e-01, { 0x3FD55555, 0x5555550D }
  1551. -1.99999999998764832476e-01, { 0xBFC99999, 0x9998EBC4 }
  1552. 1.42857142725034663711e-01, { 0x3FC24924, 0x920083FF }
  1553. -1.11111104054623557880e-01, { 0xBFBC71C6, 0xFE231671 }
  1554. 9.09088713343650656196e-02, { 0x3FB745CD, 0xC54C206E }
  1555. -7.69187620504482999495e-02, { 0xBFB3B0F2, 0xAF749A6D }
  1556. 6.66107313738753120669e-02, { 0x3FB10D66, 0xA0D03D51 }
  1557. -5.83357013379057348645e-02, { 0xBFADDE2D, 0x52DEFD9A }
  1558. 4.97687799461593236017e-02, { 0x3FA97B4B, 0x24760DEB }
  1559. -3.65315727442169155270e-02, { 0xBFA2B444, 0x2C6A6C2F }
  1560. 1.62858201153657823623e-02 { 0x3F90AD3A, 0xE322DA11 }
  1561. );
  1562. one: double = 1.0;
  1563. huge: double = 1.0e300;
  1564. var
  1565. w,s1,s2,z: double;
  1566. ix,hx,id: longint;
  1567. low: longword;
  1568. begin
  1569. hx:=float64high(d);
  1570. ix := hx and $7fffffff;
  1571. if (ix>=$44100000) then { if |x| >= 2^66 }
  1572. begin
  1573. low:=float64low(d);
  1574. if (ix > $7ff00000) or ((ix = $7ff00000) and (low<>0)) then
  1575. exit(d+d); { NaN }
  1576. if (hx>0) then
  1577. exit(atanhi[3]+atanlo[3])
  1578. else
  1579. exit(-atanhi[3]-atanlo[3]);
  1580. end;
  1581. if (ix < $3fdc0000) then { |x| < 0.4375 }
  1582. begin
  1583. if (ix < $3e200000) then { |x| < 2^-29 }
  1584. begin
  1585. if (huge+d>one) then exit(d); { raise inexact }
  1586. end;
  1587. id := -1;
  1588. end
  1589. else
  1590. begin
  1591. d := abs(d);
  1592. if (ix < $3ff30000) then { |x| < 1.1875 }
  1593. begin
  1594. if (ix < $3fe60000) then { 7/16 <=|x|<11/16 }
  1595. begin
  1596. id := 0; d := (2.0*d-one)/(2.0+d);
  1597. end
  1598. else { 11/16<=|x|< 19/16 }
  1599. begin
  1600. id := 1; d := (d-one)/(d+one);
  1601. end
  1602. end
  1603. else
  1604. begin
  1605. if (ix < $40038000) then { |x| < 2.4375 }
  1606. begin
  1607. id := 2; d := (d-1.5)/(one+1.5*d);
  1608. end
  1609. else { 2.4375 <= |x| < 2^66 }
  1610. begin
  1611. id := 3; d := -1.0/d;
  1612. end;
  1613. end;
  1614. end;
  1615. { end of argument reduction }
  1616. z := d*d;
  1617. w := z*z;
  1618. { break sum from i=0 to 10 aT[i]z**(i+1) into odd and even poly }
  1619. s1 := z*(aT[0]+w*(aT[2]+w*(aT[4]+w*(aT[6]+w*(aT[8]+w*aT[10])))));
  1620. s2 := w*(aT[1]+w*(aT[3]+w*(aT[5]+w*(aT[7]+w*aT[9]))));
  1621. if (id<0) then
  1622. result := d - d*(s1+s2)
  1623. else
  1624. begin
  1625. z := atanhi[id] - ((d*(s1+s2) - atanlo[id]) - d);
  1626. if hx<0 then
  1627. result := -z
  1628. else
  1629. result := z;
  1630. end;
  1631. end;
  1632. {$endif}
  1633. {$ifndef FPC_SYSTEM_HAS_FRAC}
  1634. function fpc_frac_real(d : ValReal) : ValReal;compilerproc;
  1635. begin
  1636. result := d - Int(d);
  1637. end;
  1638. {$endif}
  1639. {$ifdef FPC_INCLUDE_SOFTWARE_INT64_TO_DOUBLE}
  1640. {$ifndef FPC_SYSTEM_HAS_QWORD_TO_DOUBLE}
  1641. function fpc_qword_to_double(q : qword): double; compilerproc;
  1642. begin
  1643. result:=dword(q and $ffffffff)+dword(q shr 32)*double(4294967296.0);
  1644. end;
  1645. {$endif FPC_SYSTEM_HAS_INT64_TO_DOUBLE}
  1646. {$ifndef FPC_SYSTEM_HAS_INT64_TO_DOUBLE}
  1647. function fpc_int64_to_double(i : int64): double; compilerproc;
  1648. begin
  1649. result:=dword(i and $ffffffff)+longint(i shr 32)*double(4294967296.0);
  1650. end;
  1651. {$endif FPC_SYSTEM_HAS_INT64_TO_DOUBLE}
  1652. {$endif FPC_INCLUDE_SOFTWARE_INT64_TO_DOUBLE}
  1653. {$ifdef SUPPORT_DOUBLE}
  1654. {****************************************************************************
  1655. Helper routines to support old TP styled reals
  1656. ****************************************************************************}
  1657. {$ifndef FPC_SYSTEM_HAS_REAL2DOUBLE}
  1658. function real2double(r : real48) : double;
  1659. var
  1660. res : array[0..7] of byte;
  1661. exponent : word;
  1662. begin
  1663. { check for zero }
  1664. if r[0]=0 then
  1665. begin
  1666. real2double:=0.0;
  1667. exit;
  1668. end;
  1669. { copy mantissa }
  1670. res[0]:=0;
  1671. res[1]:=r[1] shl 5;
  1672. res[2]:=(r[1] shr 3) or (r[2] shl 5);
  1673. res[3]:=(r[2] shr 3) or (r[3] shl 5);
  1674. res[4]:=(r[3] shr 3) or (r[4] shl 5);
  1675. res[5]:=(r[4] shr 3) or (r[5] and $7f) shl 5;
  1676. res[6]:=(r[5] and $7f) shr 3;
  1677. { copy exponent }
  1678. { correct exponent: }
  1679. exponent:=(word(r[0])+(1023-129));
  1680. res[6]:=res[6] or ((exponent and $f) shl 4);
  1681. res[7]:=exponent shr 4;
  1682. { set sign }
  1683. res[7]:=res[7] or (r[5] and $80);
  1684. real2double:=double(res);
  1685. end;
  1686. {$endif FPC_SYSTEM_HAS_REAL2DOUBLE}
  1687. {$endif SUPPORT_DOUBLE}
  1688. {$ifdef SUPPORT_EXTENDED}
  1689. { fast 10^n routine }
  1690. function FPower10(val: Extended; Power: Longint): Extended;
  1691. const
  1692. pow32 : array[0..31] of extended =
  1693. (
  1694. 1e0,1e1,1e2,1e3,1e4,1e5,1e6,1e7,1e8,1e9,1e10,
  1695. 1e11,1e12,1e13,1e14,1e15,1e16,1e17,1e18,1e19,1e20,
  1696. 1e21,1e22,1e23,1e24,1e25,1e26,1e27,1e28,1e29,1e30,
  1697. 1e31
  1698. );
  1699. pow512 : array[0..15] of extended =
  1700. (
  1701. 1,1e32,1e64,1e96,1e128,1e160,1e192,1e224,
  1702. 1e256,1e288,1e320,1e352,1e384,1e416,1e448,
  1703. 1e480
  1704. );
  1705. pow4096 : array[0..9] of extended =
  1706. (1,1e512,1e1024,1e1536,
  1707. 1e2048,1e2560,1e3072,1e3584,
  1708. 1e4096,1e4608
  1709. );
  1710. negpow32 : array[0..31] of extended =
  1711. (
  1712. 1e-0,1e-1,1e-2,1e-3,1e-4,1e-5,1e-6,1e-7,1e-8,1e-9,1e-10,
  1713. 1e-11,1e-12,1e-13,1e-14,1e-15,1e-16,1e-17,1e-18,1e-19,1e-20,
  1714. 1e-21,1e-22,1e-23,1e-24,1e-25,1e-26,1e-27,1e-28,1e-29,1e-30,
  1715. 1e-31
  1716. );
  1717. negpow512 : array[0..15] of extended =
  1718. (
  1719. 0,1e-32,1e-64,1e-96,1e-128,1e-160,1e-192,1e-224,
  1720. 1e-256,1e-288,1e-320,1e-352,1e-384,1e-416,1e-448,
  1721. 1e-480
  1722. );
  1723. negpow4096 : array[0..9] of extended =
  1724. (
  1725. 0,1e-512,1e-1024,1e-1536,
  1726. 1e-2048,1e-2560,1e-3072,1e-3584,
  1727. 1e-4096,1e-4608
  1728. );
  1729. begin
  1730. if Power<0 then
  1731. begin
  1732. Power:=-Power;
  1733. result:=val*negpow32[Power and $1f];
  1734. power:=power shr 5;
  1735. if power<>0 then
  1736. begin
  1737. result:=result*negpow512[Power and $f];
  1738. power:=power shr 4;
  1739. if power<>0 then
  1740. begin
  1741. if power<=9 then
  1742. result:=result*negpow4096[Power]
  1743. else
  1744. result:=1.0/0.0;
  1745. end;
  1746. end;
  1747. end
  1748. else
  1749. begin
  1750. result:=val*pow32[Power and $1f];
  1751. power:=power shr 5;
  1752. if power<>0 then
  1753. begin
  1754. result:=result*pow512[Power and $f];
  1755. power:=power shr 4;
  1756. if power<>0 then
  1757. begin
  1758. if power<=9 then
  1759. result:=result*pow4096[Power]
  1760. else
  1761. result:=1.0/0.0;
  1762. end;
  1763. end;
  1764. end;
  1765. end;
  1766. {$endif SUPPORT_EXTENDED}