genmath.inc 63 KB

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