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. zero: double = 0;
  63. {$if not defined(FPC_SYSTEM_HAS_SIN) or not defined(FPC_SYSTEM_HAS_COS)}
  64. const sincof : array[0..5] of Real = (
  65. 1.58962301576546568060E-10,
  66. -2.50507477628578072866E-8,
  67. 2.75573136213857245213E-6,
  68. -1.98412698295895385996E-4,
  69. 8.33333333332211858878E-3,
  70. -1.66666666666666307295E-1);
  71. coscof : array[0..5] of Real = (
  72. -1.13585365213876817300E-11,
  73. 2.08757008419747316778E-9,
  74. -2.75573141792967388112E-7,
  75. 2.48015872888517045348E-5,
  76. -1.38888888888730564116E-3,
  77. 4.16666666666665929218E-2);
  78. {$endif}
  79. {*
  80. -------------------------------------------------------------------------------
  81. Raises the exceptions specified by `flags'. Floating-point traps can be
  82. defined here if desired. It is currently not possible for such a trap
  83. to substitute a result value. If traps are not implemented, this routine
  84. should be simply `softfloat_exception_flags |= flags;'.
  85. -------------------------------------------------------------------------------
  86. *}
  87. procedure float_raise(i: TFPUException);
  88. begin
  89. float_raise([i]);
  90. end;
  91. procedure float_raise(i: TFPUExceptionMask);
  92. var
  93. pflags: ^TFPUExceptionMask;
  94. unmasked_flags: TFPUExceptionMask;
  95. Begin
  96. { taking address of threadvar produces somewhat more compact code }
  97. pflags := @softfloat_exception_flags;
  98. pflags^:=pflags^ + i;
  99. unmasked_flags := pflags^ - softfloat_exception_mask;
  100. if (float_flag_invalid in unmasked_flags) then
  101. HandleError(207)
  102. else
  103. if (float_flag_divbyzero in unmasked_flags) then
  104. HandleError(200)
  105. else
  106. if (float_flag_overflow in unmasked_flags) then
  107. HandleError(205)
  108. else
  109. if (float_flag_underflow in unmasked_flags) then
  110. HandleError(206)
  111. else
  112. if (float_flag_inexact in unmasked_flags) then
  113. HandleError(207);
  114. end;
  115. { This function does nothing, but its argument is expected to be an expression
  116. which causes FPE when calculated. If exception is masked, it just returns true,
  117. allowing to use it in expressions. }
  118. function fpe_helper(x: valreal): boolean;
  119. begin
  120. result:=true;
  121. end;
  122. {$ifdef SUPPORT_DOUBLE}
  123. {$ifndef FPC_HAS_FLOAT64HIGH}
  124. {$define FPC_HAS_FLOAT64HIGH}
  125. function float64high(d: double): longint; inline;
  126. begin
  127. result:=float64(d).high;
  128. end;
  129. procedure float64sethigh(var d: double; l: longint); inline;
  130. begin
  131. float64(d).high:=l;
  132. end;
  133. {$endif FPC_HAS_FLOAT64HIGH}
  134. {$ifndef FPC_HAS_FLOAT64LOW}
  135. {$define FPC_HAS_FLOAT64LOW}
  136. function float64low(d: double): longint; inline;
  137. begin
  138. result:=float64(d).low;
  139. end;
  140. procedure float64setlow(var d: double; l: longint); inline;
  141. begin
  142. float64(d).low:=l;
  143. end;
  144. {$endif FPC_HAS_FLOAT64LOW}
  145. {$endif SUPPORT_DOUBLE}
  146. {$ifndef FPC_SYSTEM_HAS_TRUNC}
  147. {$ifndef FPC_SYSTEM_HAS_float32}
  148. type
  149. float32 = longint;
  150. {$endif FPC_SYSTEM_HAS_float32}
  151. {$ifdef SUPPORT_DOUBLE}
  152. { based on softfloat float64_to_int64_round_to_zero }
  153. function fpc_trunc_real(d : valreal) : int64; compilerproc;
  154. var
  155. aExp, shiftCount : smallint;
  156. aSig : int64;
  157. z : int64;
  158. a: float64;
  159. begin
  160. a:=float64(d);
  161. aSig:=(int64(a.high and $000fffff) shl 32) or longword(a.low);
  162. aExp:=(a.high shr 20) and $7FF;
  163. if aExp<>0 then
  164. aSig:=aSig or $0010000000000000;
  165. shiftCount:= aExp-$433;
  166. if 0<=shiftCount then
  167. begin
  168. if aExp>=$43e then
  169. begin
  170. if (a.high<>longint($C3E00000)) or (a.low<>0) then
  171. begin
  172. fpe_helper(zero/zero);
  173. if (longint(a.high)>=0) or ((aExp=$7FF) and
  174. (aSig<>$0010000000000000 )) then
  175. begin
  176. result:=$7FFFFFFFFFFFFFFF;
  177. exit;
  178. end;
  179. end;
  180. result:=$8000000000000000;
  181. exit;
  182. end;
  183. z:=aSig shl shiftCount;
  184. end
  185. else
  186. begin
  187. if aExp<$3fe then
  188. begin
  189. result:=0;
  190. exit;
  191. end;
  192. z:=aSig shr -shiftCount;
  193. {
  194. if (aSig shl (shiftCount and 63))<>0 then
  195. float_exception_flags |= float_flag_inexact;
  196. }
  197. end;
  198. if longint(a.high)<0 then
  199. z:=-z;
  200. result:=z;
  201. end;
  202. {$else SUPPORT_DOUBLE}
  203. { based on softfloat float32_to_int64_round_to_zero }
  204. Function fpc_trunc_real( d: valreal ): int64; compilerproc;
  205. Var
  206. a : float32;
  207. aExp, shiftCount : smallint;
  208. aSig : longint;
  209. aSig64, z : int64;
  210. Begin
  211. a := float32(d);
  212. aSig := a and $007FFFFF;
  213. aExp := (a shr 23) and $FF;
  214. shiftCount := aExp - $BE;
  215. if ( 0 <= shiftCount ) then
  216. Begin
  217. if ( a <> Float32($DF000000) ) then
  218. Begin
  219. fpe_helper( zero/zero );
  220. if ( (longint(a)>=0) or ( ( aExp = $FF ) and (aSig<>0) ) ) then
  221. Begin
  222. result:=$7fffffffffffffff;
  223. exit;
  224. end;
  225. End;
  226. result:=$8000000000000000;
  227. exit;
  228. End
  229. else
  230. if ( aExp <= $7E ) then
  231. Begin
  232. result := 0;
  233. exit;
  234. End;
  235. aSig64 := int64( aSig or $00800000 ) shl 40;
  236. z := aSig64 shr ( - shiftCount );
  237. if ( longint(a)<0 ) then z := - z;
  238. result := z;
  239. End;
  240. {$endif SUPPORT_DOUBLE}
  241. {$endif not FPC_SYSTEM_HAS_TRUNC}
  242. {$ifndef FPC_SYSTEM_HAS_INT}
  243. {$ifdef SUPPORT_DOUBLE}
  244. { straight Pascal translation of the code for __trunc() in }
  245. { the file sysdeps/libm-ieee754/s_trunc.c of glibc (JM) }
  246. function fpc_int_real(d: ValReal): ValReal;compilerproc;
  247. var
  248. i0, j0: longint;
  249. i1: cardinal;
  250. sx: longint;
  251. f64 : float64;
  252. begin
  253. f64:=float64(d);
  254. i0 := f64.high;
  255. i1 := cardinal(f64.low);
  256. sx := i0 and $80000000;
  257. j0 := ((i0 shr 20) and $7ff) - $3ff;
  258. if (j0 < 20) then
  259. begin
  260. if (j0 < 0) then
  261. begin
  262. { the magnitude of the number is < 1 so the result is +-0. }
  263. f64.high := sx;
  264. f64.low := 0;
  265. end
  266. else
  267. begin
  268. f64.high := sx or (i0 and not($fffff shr j0));
  269. f64.low := 0;
  270. end
  271. end
  272. else if (j0 > 51) then
  273. begin
  274. if (j0 = $400) then
  275. { d is inf or NaN }
  276. exit(d + d); { don't know why they do this (JM) }
  277. end
  278. else
  279. begin
  280. f64.high := i0;
  281. f64.low := longint(i1 and not(cardinal($ffffffff) shr (j0 - 20)));
  282. end;
  283. result:=double(f64);
  284. end;
  285. {$else SUPPORT_DOUBLE}
  286. function fpc_int_real(d : ValReal) : ValReal;compilerproc;
  287. begin
  288. { this will be correct since real = single in the case of }
  289. { the motorola version of the compiler... }
  290. result:=ValReal(trunc(d));
  291. end;
  292. {$endif SUPPORT_DOUBLE}
  293. {$endif not FPC_SYSTEM_HAS_INT}
  294. {$ifndef FPC_SYSTEM_HAS_ABS}
  295. function fpc_abs_real(d : ValReal) : ValReal;compilerproc;
  296. begin
  297. if (d<0.0) then
  298. result := -d
  299. else
  300. result := d ;
  301. end;
  302. {$endif not FPC_SYSTEM_HAS_ABS}
  303. {$ifndef SYSTEM_HAS_FREXP}
  304. function frexp(x:Real; out e:Integer ):Real;
  305. {* frexp() extracts the exponent from x. It returns an integer *}
  306. {* power of two to expnt and the significand between 0.5 and 1 *}
  307. {* to y. Thus x = y * 2**expn. *}
  308. begin
  309. e :=0;
  310. if (abs(x)<0.5) then
  311. While (abs(x)<0.5) do
  312. begin
  313. x := x*2;
  314. Dec(e);
  315. end
  316. else
  317. While (abs(x)>1) do
  318. begin
  319. x := x/2;
  320. Inc(e);
  321. end;
  322. frexp := x;
  323. end;
  324. {$endif not SYSTEM_HAS_FREXP}
  325. {$ifndef SYSTEM_HAS_LDEXP}
  326. {$ifdef SUPPORT_DOUBLE}
  327. function ldexp( x: Real; N: Integer):Real;
  328. {* ldexp() multiplies x by 2**n. *}
  329. var
  330. i: integer;
  331. const
  332. H2_54: double = 18014398509481984.0; {2^54}
  333. huge: double = 1e300;
  334. begin
  335. i := (float64high(x) and $7ff00000) shr 20;
  336. {if +-INF, NaN, 0 or if e=0 return d}
  337. if (i=$7FF) or (N=0) or (x=0.0) then
  338. ldexp := x
  339. else if i=0 then {Denormal: result = d*2^54*2^(e-54)}
  340. ldexp := ldexp(x*H2_54, N-54)
  341. else
  342. begin
  343. N := N+i;
  344. if N>$7FE then { overflow }
  345. begin
  346. if x>0.0 then
  347. ldexp := 2.0*huge
  348. else
  349. ldexp := (-2.0)*huge;
  350. end
  351. else if N<1 then
  352. begin
  353. {underflow or denormal}
  354. if N<-53 then
  355. ldexp := 0.0
  356. else
  357. begin
  358. {Denormal: result = d*2^(e+54)/2^54}
  359. inc(N,54);
  360. float64sethigh(x,(float64high(x) and $800FFFFF) or (longint(N) shl 20));
  361. ldexp := x/H2_54;
  362. end;
  363. end
  364. else
  365. begin
  366. float64sethigh(x,(float64high(x) and $800FFFFF) or (longint(N) shl 20));
  367. ldexp := x;
  368. end;
  369. end;
  370. end;
  371. {$else SUPPORT_DOUBLE}
  372. function ldexp( x: Real; N: Integer):Real;
  373. {* ldexp() multiplies x by 2**n. *}
  374. var r : Real;
  375. begin
  376. R := 1;
  377. if N>0 then
  378. while N>0 do
  379. begin
  380. R:=R*2;
  381. Dec(N);
  382. end
  383. else
  384. while N<0 do
  385. begin
  386. R:=R/2;
  387. Inc(N);
  388. end;
  389. ldexp := x * R;
  390. end;
  391. {$endif SUPPORT_DOUBLE}
  392. {$endif not SYSTEM_HAS_LDEXP}
  393. function polevl(x:Real; Coef:PReal; N:sizeint):Real;
  394. {*****************************************************************}
  395. { Evaluate polynomial }
  396. {*****************************************************************}
  397. { }
  398. { SYNOPSIS: }
  399. { }
  400. { int N; }
  401. { double x, y, coef[N+1], polevl[]; }
  402. { }
  403. { y = polevl( x, coef, N ); }
  404. { }
  405. { DESCRIPTION: }
  406. { }
  407. { Evaluates polynomial of degree N: }
  408. { }
  409. { 2 N }
  410. { y = C + C x + C x +...+ C x }
  411. { 0 1 2 N }
  412. { }
  413. { Coefficients are stored in reverse order: }
  414. { }
  415. { coef[0] = C , ..., coef[N] = C . }
  416. { N 0 }
  417. { }
  418. { The function p1evl() assumes that coef[N] = 1.0 and is }
  419. { omitted from the array. Its calling arguments are }
  420. { otherwise the same as polevl(). }
  421. { }
  422. { SPEED: }
  423. { }
  424. { In the interest of speed, there are no checks for out }
  425. { of bounds arithmetic. This routine is used by most of }
  426. { the functions in the library. Depending on available }
  427. { equipment features, the user may wish to rewrite the }
  428. { program in microcode or assembly language. }
  429. {*****************************************************************}
  430. var ans : Real;
  431. i : sizeint;
  432. begin
  433. ans := Coef[0];
  434. for i:=1 to N do
  435. ans := ans * x + Coef[i];
  436. polevl:=ans;
  437. end;
  438. function p1evl(x:Real; Coef:PReal; N:sizeint):Real;
  439. { }
  440. { Evaluate polynomial when coefficient of x is 1.0. }
  441. { Otherwise same as polevl. }
  442. { }
  443. var
  444. ans : Real;
  445. i : sizeint;
  446. begin
  447. ans := x + Coef[0];
  448. for i:=1 to N-1 do
  449. ans := ans * x + Coef[i];
  450. p1evl := ans;
  451. end;
  452. function floord(x: double): double;
  453. var
  454. t: double;
  455. begin
  456. t := int(x);
  457. if (x>=0.0) or (x=t) then
  458. floord := t
  459. else
  460. floord := t - 1.0;
  461. end;
  462. {*
  463. * ====================================================
  464. * Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved.
  465. *
  466. * Developed at SunPro, a Sun Microsystems, Inc. business.
  467. * Permission to use, copy, modify, and distribute this
  468. * software is freely granted, provided that this notice
  469. * is preserved.
  470. * ====================================================
  471. *
  472. * Pascal port of this routine comes from DAMath library
  473. * (C) Copyright 2013 Wolfgang Ehrhardt
  474. *
  475. * k_rem_pio2 return the last three bits of N with y = x - N*pi/2
  476. * so that |y| < pi/2.
  477. *
  478. * The method is to compute the integer (mod 8) and fraction parts of
  479. * (2/pi)*x without doing the full multiplication. In general we
  480. * skip the part of the product that are known to be a huge integer
  481. * (more accurately, = 0 mod 8 ). Thus the number of operations are
  482. * independent of the exponent of the input.
  483. *
  484. * (2/pi) is represented by an array of 24-bit integers in ipio2[].
  485. *
  486. * Input parameters:
  487. * x[] The input value (must be positive) is broken into nx
  488. * pieces of 24-bit integers in double precision format.
  489. * x[i] will be the i-th 24 bit of x. The scaled exponent
  490. * of x[0] is given in input parameter e0 (i.e., x[0]*2^e0
  491. * match x's up to 24 bits.
  492. *
  493. * Example of breaking a double positive z into x[0]+x[1]+x[2]:
  494. * e0 = ilogb(z)-23
  495. * z = scalbn(z,-e0)
  496. * for i = 0,1,2
  497. * x[i] = floor(z)
  498. * z = (z-x[i])*2**24
  499. *
  500. *
  501. * y[] output result in an array of double precision numbers.
  502. * The dimension of y[] is:
  503. * 24-bit precision 1
  504. * 53-bit precision 2
  505. * 64-bit precision 2
  506. * 113-bit precision 3
  507. * The actual value is the sum of them. Thus for 113-bit
  508. * precison, one may have to do something like:
  509. *
  510. * long double t,w,r_head, r_tail;
  511. * t = (long double)y[2] + (long double)y[1];
  512. * w = (long double)y[0];
  513. * r_head = t+w;
  514. * r_tail = w - (r_head - t);
  515. *
  516. * e0 The exponent of x[0]. Must be <= 16360 or you need to
  517. * expand the ipio2 table.
  518. *
  519. * nx dimension of x[]
  520. *
  521. * prec an integer indicating the precision:
  522. * 0 24 bits (single)
  523. * 1 53 bits (double)
  524. * 2 64 bits (extended)
  525. * 3 113 bits (quad)
  526. *
  527. * Here is the description of some local variables:
  528. *
  529. * jk jk+1 is the initial number of terms of ipio2[] needed
  530. * in the computation. The recommended value is 2,3,4,
  531. * 6 for single, double, extended,and quad.
  532. *
  533. * jz local integer variable indicating the number of
  534. * terms of ipio2[] used.
  535. *
  536. * jx nx - 1
  537. *
  538. * jv index for pointing to the suitable ipio2[] for the
  539. * computation. In general, we want
  540. * ( 2^e0*x[0] * ipio2[jv-1]*2^(-24jv) )/8
  541. * is an integer. Thus
  542. * e0-3-24*jv >= 0 or (e0-3)/24 >= jv
  543. * Hence jv = max(0,(e0-3)/24).
  544. *
  545. * jp jp+1 is the number of terms in PIo2[] needed, jp = jk.
  546. *
  547. * q[] double array with integral value, representing the
  548. * 24-bits chunk of the product of x and 2/pi.
  549. *
  550. * q0 the corresponding exponent of q[0]. Note that the
  551. * exponent for q[i] would be q0-24*i.
  552. *
  553. * PIo2[] double precision array, obtained by cutting pi/2
  554. * into 24 bits chunks.
  555. *
  556. * f[] ipio2[] in floating point
  557. *
  558. * iq[] integer array by breaking up q[] in 24-bits chunk.
  559. *
  560. * fq[] final product of x*(2/pi) in fq[0],..,fq[jk]
  561. *
  562. * ih integer. If >0 it indicates q[] is >= 0.5, hence
  563. * it also indicates the *sign* of the result.
  564. *}
  565. {PIo2[] double array, obtained by cutting pi/2 into 24 bits chunks.}
  566. const
  567. PIo2chunked: array[0..7] of double = (
  568. 1.57079625129699707031e+00, { 0x3FF921FB, 0x40000000 }
  569. 7.54978941586159635335e-08, { 0x3E74442D, 0x00000000 }
  570. 5.39030252995776476554e-15, { 0x3CF84698, 0x80000000 }
  571. 3.28200341580791294123e-22, { 0x3B78CC51, 0x60000000 }
  572. 1.27065575308067607349e-29, { 0x39F01B83, 0x80000000 }
  573. 1.22933308981111328932e-36, { 0x387A2520, 0x40000000 }
  574. 2.73370053816464559624e-44, { 0x36E38222, 0x80000000 }
  575. 2.16741683877804819444e-51 { 0x3569F31D, 0x00000000 }
  576. );
  577. {Table of constants for 2/pi, 396 Hex digits (476 decimal) of 2/pi }
  578. ipio2: array[0..65] of longint = (
  579. $A2F983, $6E4E44, $1529FC, $2757D1, $F534DD, $C0DB62,
  580. $95993C, $439041, $FE5163, $ABDEBB, $C561B7, $246E3A,
  581. $424DD2, $E00649, $2EEA09, $D1921C, $FE1DEB, $1CB129,
  582. $A73EE8, $8235F5, $2EBB44, $84E99C, $7026B4, $5F7E41,
  583. $3991D6, $398353, $39F49C, $845F8B, $BDF928, $3B1FF8,
  584. $97FFDE, $05980F, $EF2F11, $8B5A0A, $6D1F6D, $367ECF,
  585. $27CB09, $B74F46, $3F669E, $5FEA2D, $7527BA, $C7EBE5,
  586. $F17B3D, $0739F7, $8A5292, $EA6BFB, $5FB11F, $8D5D08,
  587. $560330, $46FC7B, $6BABF0, $CFBC20, $9AF436, $1DA9E3,
  588. $91615E, $E61B08, $659985, $5F14A0, $68408D, $FFD880,
  589. $4D7327, $310606, $1556CA, $73A8C9, $60E27B, $C08C6B);
  590. init_jk: array[0..3] of integer = (2,3,4,6); {initial value for jk}
  591. two24: double = 16777216.0; {2^24}
  592. twon24: double = 5.9604644775390625e-08; {1/2^24}
  593. type
  594. TDA02 = array[0..2] of double; { 3 elements is enough for float128 }
  595. function k_rem_pio2(const x: TDA02; out y: TDA02; e0, nx, prec: integer): sizeint;
  596. var
  597. i,ih,j,jz,jx,jv,jp,jk,carry,k,n,q0: longint;
  598. t: longint;
  599. iq: array[0..19] of longint;
  600. f,fq,q: array[0..19] of double;
  601. z,fw: double;
  602. begin
  603. {initialize jk}
  604. jk := init_jk[prec];
  605. jp := jk;
  606. {determine jx,jv,q0, note that 3>q0}
  607. jx := nx-1;
  608. jv := (e0-3) div 24; if jv<0 then jv := 0;
  609. q0 := e0-24*(jv+1);
  610. {set up f[0] to f[jx+jk] where f[jx+jk] = ipio2[jv+jk]}
  611. j := jv-jx;
  612. for i:=0 to jx+jk do
  613. begin
  614. if j<0 then f[i] := 0.0 else f[i] := ipio2[j];
  615. inc(j);
  616. end;
  617. {compute q[0],q[1],...q[jk]}
  618. for i:=0 to jk do
  619. begin
  620. fw := 0.0;
  621. for j:=0 to jx do
  622. fw := fw + x[j]*f[jx+i-j];
  623. q[i] := fw;
  624. end;
  625. jz := jk;
  626. repeat
  627. {distill q[] into iq[] reversingly}
  628. i := 0;
  629. z := q[jz];
  630. for j:=jz downto 1 do
  631. begin
  632. fw := trunc(twon24*z);
  633. iq[i] := trunc(z-two24*fw);
  634. z := q[j-1]+fw;
  635. inc(i);
  636. end;
  637. {compute n}
  638. z := ldexp(z,q0); {actual value of z}
  639. z := z - 8.0*floord(z*0.125); {trim off integer >= 8}
  640. n := trunc(z);
  641. z := z - n;
  642. ih := 0;
  643. if q0>0 then
  644. begin
  645. {need iq[jz-1] to determine n}
  646. t := (iq[jz-1] shr (24-q0));
  647. inc(n,t);
  648. dec(iq[jz-1], t shl (24-q0));
  649. ih := iq[jz-1] shr (23-q0);
  650. end
  651. else if q0=0 then
  652. ih := iq[jz-1] shr 23
  653. else if z>=0.5 then
  654. ih := 2;
  655. if ih>0 then {q > 0.5}
  656. begin
  657. inc(n);
  658. carry := 0;
  659. for i:=0 to jz-1 do
  660. begin
  661. {compute 1-q}
  662. t := iq[i];
  663. if carry=0 then
  664. begin
  665. if t<>0 then
  666. begin
  667. carry := 1;
  668. iq[i] := $1000000 - t;
  669. end
  670. end
  671. else
  672. iq[i] := $ffffff - t;
  673. end;
  674. if q0>0 then
  675. begin
  676. {rare case: chance is 1 in 12}
  677. case q0 of
  678. 1: iq[jz-1] := iq[jz-1] and $7fffff;
  679. 2: iq[jz-1] := iq[jz-1] and $3fffff;
  680. end;
  681. end;
  682. if ih=2 then
  683. begin
  684. z := 1.0 - z;
  685. if carry<>0 then
  686. z := z - ldexp(1.0,q0);
  687. end;
  688. end;
  689. {check if recomputation is needed}
  690. if z<>0.0 then
  691. break;
  692. t := 0;
  693. for i:=jz-1 downto jk do
  694. t := t or iq[i];
  695. if t<>0 then
  696. break;
  697. {need recomputation}
  698. k := 1;
  699. while iq[jk-k]=0 do {k = no. of terms needed}
  700. inc(k);
  701. for i:=jz+1 to jz+k do
  702. begin
  703. {add q[jz+1] to q[jz+k]}
  704. f[jx+i] := ipio2[jv+i];
  705. fw := 0.0;
  706. for j:=0 to jx do
  707. fw := fw + x[j]*f[jx+i-j];
  708. q[i] := fw;
  709. end;
  710. inc(jz,k);
  711. until False;
  712. {chop off zero terms}
  713. if z=0.0 then
  714. begin
  715. repeat
  716. dec(jz);
  717. dec(q0,24);
  718. until iq[jz]<>0;
  719. end
  720. else
  721. begin
  722. {break z into 24-bit if necessary}
  723. z := ldexp(z,-q0);
  724. if z>=two24 then
  725. begin
  726. fw := trunc(twon24*z);
  727. iq[jz] := trunc(z-two24*fw);
  728. inc(jz);
  729. inc(q0,24);
  730. iq[jz] := trunc(fw);
  731. end
  732. else
  733. iq[jz] := trunc(z);
  734. end;
  735. {convert integer "bit" chunk to floating-point value}
  736. fw := ldexp(1.0,q0);
  737. for i:=jz downto 0 do
  738. begin
  739. q[i] := fw*iq[i];
  740. fw := fw*twon24;
  741. end;
  742. {compute PIo2[0,...,jp]*q[jz,...,0]}
  743. for i:=jz downto 0 do
  744. begin
  745. fw :=0.0;
  746. k := 0;
  747. while (k<=jp) and (k<=jz-i) do
  748. begin
  749. fw := fw + double(PIo2chunked[k])*(q[i+k]);
  750. inc(k);
  751. end;
  752. fq[jz-i] := fw;
  753. end;
  754. {compress fq[] into y[]}
  755. case prec of
  756. 0:
  757. begin
  758. fw := 0.0;
  759. for i:=jz downto 0 do
  760. fw := fw + fq[i];
  761. if ih=0 then
  762. y[0] := fw
  763. else
  764. y[0] := -fw;
  765. end;
  766. 1, 2:
  767. begin
  768. fw := 0.0;
  769. for i:=jz downto 0 do
  770. fw := fw + fq[i];
  771. if ih=0 then
  772. y[0] := fw
  773. else
  774. y[0] := -fw;
  775. fw := fq[0]-fw;
  776. for i:=1 to jz do
  777. fw := fw + fq[i];
  778. if ih=0 then
  779. y[1] := fw
  780. else
  781. y[1] := -fw;
  782. end;
  783. 3:
  784. begin
  785. {painful}
  786. for i:=jz downto 1 do
  787. begin
  788. fw := fq[i-1]+fq[i];
  789. fq[i] := fq[i]+(fq[i-1]-fw);
  790. fq[i-1]:= fw;
  791. end;
  792. for i:=jz downto 2 do
  793. begin
  794. fw := fq[i-1]+fq[i];
  795. fq[i] := fq[i]+(fq[i-1]-fw);
  796. fq[i-1]:= fw;
  797. end;
  798. fw := 0.0;
  799. for i:=jz downto 2 do
  800. fw := fw + fq[i];
  801. if ih=0 then
  802. begin
  803. y[0] := fq[0];
  804. y[1] := fq[1];
  805. y[2] := fw;
  806. end
  807. else
  808. begin
  809. y[0] := -fq[0];
  810. y[1] := -fq[1];
  811. y[2] := -fw;
  812. end;
  813. end;
  814. end;
  815. k_rem_pio2 := n and 7;
  816. end;
  817. { Argument reduction of x: z = x - n*Pi/2, |z| <= Pi/4, result = n mod 8.}
  818. { Uses Payne/Hanek if |x| >= lossth, Cody/Waite otherwise}
  819. function rem_pio2(x: double; out z: double): sizeint;
  820. const
  821. tol: double = 2.384185791015625E-7; {lossth*eps_d}
  822. DP1 = double(7.85398125648498535156E-1);
  823. DP2 = double(3.77489470793079817668E-8);
  824. DP3 = double(2.69515142907905952645E-15);
  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}