genmath.inc 50 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533
  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. {$goto on}
  34. type
  35. TabCoef = array[0..6] of Real;
  36. { also necessary for Int() on systems with 64bit floats (JM) }
  37. {$ifndef FPC_SYSTEM_HAS_float64}
  38. {$ifdef ENDIAN_LITTLE}
  39. float64 = packed record
  40. low: longint;
  41. high: longint;
  42. end;
  43. {$else}
  44. float64 = packed record
  45. high: longint;
  46. low: longint;
  47. end;
  48. {$endif}
  49. {$endif FPC_SYSTEM_HAS_float64}
  50. const
  51. PIO2 = 1.57079632679489661923; { pi/2 }
  52. PIO4 = 7.85398163397448309616E-1; { pi/4 }
  53. SQRT2 = 1.41421356237309504880; { sqrt(2) }
  54. SQRTH = 7.07106781186547524401E-1; { sqrt(2)/2 }
  55. LOG2E = 1.4426950408889634073599; { 1/log(2) }
  56. SQ2OPI = 7.9788456080286535587989E-1; { sqrt( 2/pi )}
  57. LOGE2 = 6.93147180559945309417E-1; { log(2) }
  58. LOGSQ2 = 3.46573590279972654709E-1; { log(2)/2 }
  59. THPIO4 = 2.35619449019234492885; { 3*pi/4 }
  60. TWOOPI = 6.36619772367581343075535E-1; { 2/pi }
  61. lossth = 1.073741824e9;
  62. MAXLOG = 8.8029691931113054295988E1; { log(2**127) }
  63. MINLOG = -8.872283911167299960540E1; { log(2**-128) }
  64. DP1 = 7.85398125648498535156E-1;
  65. DP2 = 3.77489470793079817668E-8;
  66. DP3 = 2.69515142907905952645E-15;
  67. const sincof : TabCoef = (
  68. 1.58962301576546568060E-10,
  69. -2.50507477628578072866E-8,
  70. 2.75573136213857245213E-6,
  71. -1.98412698295895385996E-4,
  72. 8.33333333332211858878E-3,
  73. -1.66666666666666307295E-1, 0);
  74. coscof : TabCoef = (
  75. -1.13585365213876817300E-11,
  76. 2.08757008419747316778E-9,
  77. -2.75573141792967388112E-7,
  78. 2.48015872888517045348E-5,
  79. -1.38888888888730564116E-3,
  80. 4.16666666666665929218E-2, 0);
  81. {*
  82. -------------------------------------------------------------------------------
  83. Raises the exceptions specified by `flags'. Floating-point traps can be
  84. defined here if desired. It is currently not possible for such a trap
  85. to substitute a result value. If traps are not implemented, this routine
  86. should be simply `softfloat_exception_flags |= flags;'.
  87. -------------------------------------------------------------------------------
  88. *}
  89. procedure float_raise(i: shortint);
  90. Begin
  91. softfloat_exception_flags := softfloat_exception_flags or i;
  92. if ((softfloat_exception_flags and not(softfloat_exception_mask)) and float_flag_invalid) <> 0 then
  93. HandleError(207)
  94. else
  95. if ((softfloat_exception_flags and not(softfloat_exception_mask)) and float_flag_divbyzero) <> 0 then
  96. HandleError(200)
  97. else
  98. if ((softfloat_exception_flags and not(softfloat_exception_mask)) and float_flag_overflow) <> 0 then
  99. HandleError(205)
  100. else
  101. if ((softfloat_exception_flags and not(softfloat_exception_mask)) and float_flag_underflow) <> 0 then
  102. HandleError(206)
  103. else
  104. if ((softfloat_exception_flags and not(softfloat_exception_mask)) and float_flag_inexact) <> 0 then
  105. HandleError(207);
  106. end;
  107. {$ifndef FPC_SYSTEM_HAS_TRUNC}
  108. {$ifndef FPC_SYSTEM_HAS_float32}
  109. type
  110. float32 = longint;
  111. {$endif FPC_SYSTEM_HAS_float32}
  112. {$ifndef FPC_SYSTEM_HAS_flag}
  113. type
  114. flag = byte;
  115. {$endif FPC_SYSTEM_HAS_flag}
  116. {$ifndef FPC_SYSTEM_HAS_extractFloat64Frac0}
  117. Function extractFloat64Frac0(const a: float64): longint;
  118. Begin
  119. extractFloat64Frac0 := a.high and $000FFFFF;
  120. End;
  121. {$endif not FPC_SYSTEM_HAS_extractFloat64Frac0}
  122. {$ifndef FPC_SYSTEM_HAS_extractFloat64Frac1}
  123. Function extractFloat64Frac1(const a: float64): longint;
  124. Begin
  125. extractFloat64Frac1 := a.low;
  126. End;
  127. {$endif not FPC_SYSTEM_HAS_extractFloat64Frac1}
  128. {$ifndef FPC_SYSTEM_HAS_extractFloat64Exp}
  129. Function extractFloat64Exp(const a: float64): smallint;
  130. Begin
  131. extractFloat64Exp:= ( a.high shr 20 ) AND $7FF;
  132. End;
  133. {$endif not FPC_SYSTEM_HAS_extractFloat64Exp}
  134. {$ifndef FPC_SYSTEM_HAS_extractFloat64Frac}
  135. Function extractFloat64Frac(const a: float64): int64;
  136. Begin
  137. extractFloat64Frac:=int64(a) and $000FFFFFFFFFFFFF;
  138. End;
  139. {$endif not FPC_SYSTEM_HAS_extractFloat64Frac}
  140. {$ifndef FPC_SYSTEM_HAS_extractFloat64Sign}
  141. Function extractFloat64Sign(const a: float64) : flag;
  142. Begin
  143. extractFloat64Sign := a.high shr 31;
  144. End;
  145. {$endif not FPC_SYSTEM_HAS_extractFloat64Sign}
  146. Procedure shortShift64Left(a0:longint; a1:longint; count:smallint; VAR z0Ptr:longint; VAR z1Ptr:longint );
  147. Begin
  148. z1Ptr := a1 shl count;
  149. if count = 0 then
  150. z0Ptr := a0
  151. else
  152. z0Ptr := ( a0 shl count ) OR ( a1 shr ( ( - count ) AND 31 ) );
  153. End;
  154. function float64_to_int32_round_to_zero(a: float64 ): longint;
  155. Var
  156. aSign: flag;
  157. aExp, shiftCount: smallint;
  158. aSig0, aSig1, absZ, aSigExtra: longint;
  159. z: longint;
  160. label
  161. invalid;
  162. Begin
  163. aSig1 := extractFloat64Frac1( a );
  164. aSig0 := extractFloat64Frac0( a );
  165. aExp := extractFloat64Exp( a );
  166. aSign := extractFloat64Sign( a );
  167. shiftCount := aExp - $413;
  168. if 0<=shiftCount then
  169. Begin
  170. if (aExp=$7FF) and ((aSig0 or aSig1)<>0) then
  171. goto invalid;
  172. shortShift64Left(aSig0 OR $00100000, aSig1, shiftCount, absZ, aSigExtra );
  173. End
  174. else
  175. Begin
  176. if aExp<$3FF then
  177. begin
  178. float64_to_int32_round_to_zero := 0;
  179. exit;
  180. end;
  181. aSig0 := aSig0 or $00100000;
  182. aSigExtra := ( aSig0 shl ( shiftCount and 31 ) ) OR aSig1;
  183. absZ := aSig0 shr ( - shiftCount );
  184. End;
  185. if aSign<>0 then
  186. z:=-absZ
  187. else
  188. z:=absZ;
  189. if ((aSign<>0) xor (z<0)) AND (z<>0) then
  190. begin
  191. invalid:
  192. float_raise(float_flag_invalid);
  193. if (aSign <> 0) then
  194. float64_to_int32_round_to_zero:=$80000000
  195. else
  196. float64_to_int32_round_to_zero:=$7FFFFFFF;
  197. exit;
  198. end;
  199. if ( aSigExtra <> 0) then
  200. float_raise(float_flag_inexact);
  201. float64_to_int32_round_to_zero := z;
  202. End;
  203. function float64_to_int64_round_to_zero(a : float64) : int64;
  204. var
  205. aSign : flag;
  206. aExp, shiftCount : smallint;
  207. aSig : int64;
  208. z : int64;
  209. begin
  210. aSig:=extractFloat64Frac(a);
  211. aExp:=extractFloat64Exp(a);
  212. aSign:=extractFloat64Sign(a);
  213. if aExp<>0 then
  214. aSig:=aSig or $0010000000000000;
  215. shiftCount:= aExp-$433;
  216. if 0<=shiftCount then
  217. begin
  218. if aExp>=$43e then
  219. begin
  220. if int64(a)<>$C3E0000000000000 then
  221. begin
  222. float_raise(float_flag_invalid);
  223. if (aSign=0) or ((aExp=$7FF) and
  224. (aSig<>$0010000000000000 )) then
  225. begin
  226. result:=$7FFFFFFFFFFFFFFF;
  227. exit;
  228. end;
  229. end;
  230. result:=$8000000000000000;
  231. exit;
  232. end;
  233. z:=aSig shl shiftCount;
  234. end
  235. else
  236. begin
  237. if aExp<$3fe then
  238. begin
  239. result:=0;
  240. exit;
  241. end;
  242. z:=aSig shr -shiftCount;
  243. {
  244. if (aSig shl (shiftCount and 63))<>0 then
  245. float_exception_flags |= float_flag_inexact;
  246. }
  247. end;
  248. if aSign<>0 then
  249. z:=-z;
  250. result:=z;
  251. end;
  252. {$ifndef FPC_SYSTEM_HAS_ExtractFloat32Frac}
  253. Function ExtractFloat32Frac(a : Float32) : longint;
  254. Begin
  255. ExtractFloat32Frac := A AND $007FFFFF;
  256. End;
  257. {$endif not FPC_SYSTEM_HAS_ExtractFloat32Frac}
  258. {$ifndef FPC_SYSTEM_HAS_extractFloat32Exp}
  259. Function extractFloat32Exp( a: float32 ): smallint;
  260. Begin
  261. extractFloat32Exp := (a shr 23) AND $FF;
  262. End;
  263. {$endif not FPC_SYSTEM_HAS_extractFloat32Exp}
  264. {$ifndef FPC_SYSTEM_HAS_extractFloat32Sign}
  265. Function extractFloat32Sign( a: float32 ): Flag;
  266. Begin
  267. extractFloat32Sign := a shr 31;
  268. End;
  269. {$endif not FPC_SYSTEM_HAS_extractFloat32Sign}
  270. Function float32_to_int32_round_to_zero( a: Float32 ): longint;
  271. Var
  272. aSign : flag;
  273. aExp, shiftCount : smallint;
  274. aSig : longint;
  275. z : longint;
  276. Begin
  277. aSig := extractFloat32Frac( a );
  278. aExp := extractFloat32Exp( a );
  279. aSign := extractFloat32Sign( a );
  280. shiftCount := aExp - $9E;
  281. if ( 0 <= shiftCount ) then
  282. Begin
  283. if ( a <> Float32($CF000000) ) then
  284. Begin
  285. float_raise( float_flag_invalid );
  286. if ( (aSign=0) or ( ( aExp = $FF ) and (aSig<>0) ) ) then
  287. Begin
  288. float32_to_int32_round_to_zero:=$7fffffff;
  289. exit;
  290. end;
  291. End;
  292. float32_to_int32_round_to_zero:=$80000000;
  293. exit;
  294. End
  295. else
  296. if ( aExp <= $7E ) then
  297. Begin
  298. float32_to_int32_round_to_zero := 0;
  299. exit;
  300. End;
  301. aSig := ( aSig or $00800000 ) shl 8;
  302. z := aSig shr ( - shiftCount );
  303. if ( aSign<>0 ) then z := - z;
  304. float32_to_int32_round_to_zero := z;
  305. End;
  306. function fpc_trunc_real(d : ValReal) : int64;compilerproc;
  307. var
  308. {$ifdef FPC_DOUBLE_HILO_SWAPPED}
  309. l: longint;
  310. {$endif FPC_DOUBLE_HILO_SWAPPED}
  311. f32 : float32;
  312. f64 : float64;
  313. Begin
  314. { in emulation mode the real is equal to a single }
  315. { otherwise in fpu mode, it is equal to a double }
  316. { extended is not supported yet. }
  317. if sizeof(D) > 8 then
  318. HandleError(255);
  319. if sizeof(D)=8 then
  320. begin
  321. move(d,f64,sizeof(f64));
  322. {$ifdef FPC_DOUBLE_HILO_SWAPPED}
  323. { the arm fpu has a strange opinion how a double has to be stored }
  324. l:=f64.low;
  325. f64.low:=f64.high;
  326. f64.high:=l;
  327. {$endif FPC_DOUBLE_HILO_SWAPPED}
  328. result:=float64_to_int64_round_to_zero(f64);
  329. end
  330. else
  331. begin
  332. move(d,f32,sizeof(f32));
  333. result:=float32_to_int32_round_to_zero(f32);
  334. end;
  335. end;
  336. {$endif not FPC_SYSTEM_HAS_TRUNC}
  337. {$ifndef FPC_SYSTEM_HAS_INT}
  338. {$ifdef SUPPORT_DOUBLE}
  339. { straight Pascal translation of the code for __trunc() in }
  340. { the file sysdeps/libm-ieee754/s_trunc.c of glibc (JM) }
  341. function fpc_int_real(d: ValReal): ValReal;compilerproc;
  342. var
  343. i0, j0: longint;
  344. i1: cardinal;
  345. sx: longint;
  346. f64 : float64;
  347. begin
  348. f64:=float64(d);
  349. {$ifdef FPC_DOUBLE_HILO_SWAPPED}
  350. { the arm fpu has a strange opinion how a double has to be stored }
  351. i0:=f64.low;
  352. f64.low:=f64.high;
  353. f64.high:=i0;
  354. {$endif FPC_DOUBLE_HILO_SWAPPED}
  355. i0 := f64.high;
  356. i1 := cardinal(f64.low);
  357. sx := i0 and $80000000;
  358. j0 := ((i0 shr 20) and $7ff) - $3ff;
  359. if (j0 < 20) then
  360. begin
  361. if (j0 < 0) then
  362. begin
  363. { the magnitude of the number is < 1 so the result is +-0. }
  364. f64.high := sx;
  365. f64.low := 0;
  366. end
  367. else
  368. begin
  369. f64.high := sx or (i0 and not($fffff shr j0));
  370. f64.low := 0;
  371. end
  372. end
  373. else if (j0 > 51) then
  374. begin
  375. if (j0 = $400) then
  376. { d is inf or NaN }
  377. exit(d + d); { don't know why they do this (JM) }
  378. end
  379. else
  380. begin
  381. f64.high := i0;
  382. f64.low := longint(i1 and not(cardinal($ffffffff) shr (j0 - 20)));
  383. end;
  384. {$ifdef FPC_DOUBLE_HILO_SWAPPED}
  385. { the arm fpu has a strange opinion how a double has to be stored }
  386. i0:=f64.low;
  387. f64.low:=f64.high;
  388. f64.high:=i0;
  389. {$endif FPC_DOUBLE_HILO_SWAPPED}
  390. result:=double(f64);
  391. end;
  392. {$else SUPPORT_DOUBLE}
  393. function fpc_int_real(d : ValReal) : ValReal;compilerproc;
  394. begin
  395. { this will be correct since real = single in the case of }
  396. { the motorola version of the compiler... }
  397. result:=ValReal(trunc(d));
  398. end;
  399. {$endif SUPPORT_DOUBLE}
  400. {$endif not FPC_SYSTEM_HAS_INT}
  401. {$ifndef FPC_SYSTEM_HAS_ABS}
  402. function fpc_abs_real(d : ValReal) : ValReal;compilerproc;
  403. begin
  404. if (d<0.0) then
  405. result := -d
  406. else
  407. result := d ;
  408. end;
  409. {$endif not FPC_SYSTEM_HAS_ABS}
  410. {$ifndef SYSTEM_HAS_FREXP}
  411. function frexp(x:Real; out e:Integer ):Real;
  412. {* frexp() extracts the exponent from x. It returns an integer *}
  413. {* power of two to expnt and the significand between 0.5 and 1 *}
  414. {* to y. Thus x = y * 2**expn. *}
  415. begin
  416. e :=0;
  417. if (abs(x)<0.5) then
  418. While (abs(x)<0.5) do
  419. begin
  420. x := x*2;
  421. Dec(e);
  422. end
  423. else
  424. While (abs(x)>1) do
  425. begin
  426. x := x/2;
  427. Inc(e);
  428. end;
  429. frexp := x;
  430. end;
  431. {$endif not SYSTEM_HAS_FREXP}
  432. {$ifndef SYSTEM_HAS_LDEXP}
  433. function ldexp( x: Real; N: Integer):Real;
  434. {* ldexp() multiplies x by 2**n. *}
  435. var r : Real;
  436. begin
  437. R := 1;
  438. if N>0 then
  439. while N>0 do
  440. begin
  441. R:=R*2;
  442. Dec(N);
  443. end
  444. else
  445. while N<0 do
  446. begin
  447. R:=R/2;
  448. Inc(N);
  449. end;
  450. ldexp := x * R;
  451. end;
  452. {$endif not SYSTEM_HAS_LDEXP}
  453. function polevl(var x:Real; var Coef:TabCoef; N:Integer):Real;
  454. {*****************************************************************}
  455. { Evaluate polynomial }
  456. {*****************************************************************}
  457. { }
  458. { SYNOPSIS: }
  459. { }
  460. { int N; }
  461. { double x, y, coef[N+1], polevl[]; }
  462. { }
  463. { y = polevl( x, coef, N ); }
  464. { }
  465. { DESCRIPTION: }
  466. { }
  467. { Evaluates polynomial of degree N: }
  468. { }
  469. { 2 N }
  470. { y = C + C x + C x +...+ C x }
  471. { 0 1 2 N }
  472. { }
  473. { Coefficients are stored in reverse order: }
  474. { }
  475. { coef[0] = C , ..., coef[N] = C . }
  476. { N 0 }
  477. { }
  478. { The function p1evl() assumes that coef[N] = 1.0 and is }
  479. { omitted from the array. Its calling arguments are }
  480. { otherwise the same as polevl(). }
  481. { }
  482. { SPEED: }
  483. { }
  484. { In the interest of speed, there are no checks for out }
  485. { of bounds arithmetic. This routine is used by most of }
  486. { the functions in the library. Depending on available }
  487. { equipment features, the user may wish to rewrite the }
  488. { program in microcode or assembly language. }
  489. {*****************************************************************}
  490. var ans : Real;
  491. i : Integer;
  492. begin
  493. ans := Coef[0];
  494. for i:=1 to N do
  495. ans := ans * x + Coef[i];
  496. polevl:=ans;
  497. end;
  498. function p1evl(var x:Real; var Coef:TabCoef; N:Integer):Real;
  499. { }
  500. { Evaluate polynomial when coefficient of x is 1.0. }
  501. { Otherwise same as polevl. }
  502. { }
  503. var
  504. ans : Real;
  505. i : Integer;
  506. begin
  507. ans := x + Coef[0];
  508. for i:=1 to N-1 do
  509. ans := ans * x + Coef[i];
  510. p1evl := ans;
  511. end;
  512. {$ifndef FPC_SYSTEM_HAS_SQR}
  513. function fpc_sqr_real(d : ValReal) : ValReal;compilerproc;{$ifdef MATHINLINE}inline;{$endif}
  514. begin
  515. result := d*d;
  516. end;
  517. {$endif}
  518. {$ifndef FPC_SYSTEM_HAS_PI}
  519. function fpc_pi_real : ValReal;compilerproc;{$ifdef MATHINLINE}inline;{$endif}
  520. begin
  521. result := 3.1415926535897932385;
  522. end;
  523. {$endif}
  524. {$ifndef FPC_SYSTEM_HAS_SQRT}
  525. function fpc_sqrt_real(d:ValReal):ValReal;compilerproc;
  526. {*****************************************************************}
  527. { Square root }
  528. {*****************************************************************}
  529. { }
  530. { SYNOPSIS: }
  531. { }
  532. { double x, y, sqrt(); }
  533. { }
  534. { y = sqrt( x ); }
  535. { }
  536. { DESCRIPTION: }
  537. { }
  538. { Returns the square root of x. }
  539. { }
  540. { Range reduction involves isolating the power of two of the }
  541. { argument and using a polynomial approximation to obtain }
  542. { a rough value for the square root. Then Heron's iteration }
  543. { is used three times to converge to an accurate value. }
  544. {*****************************************************************}
  545. var e : Integer;
  546. w,z : Real;
  547. begin
  548. if( d <= 0.0 ) then
  549. begin
  550. if d < 0.0 then begin
  551. float_raise(float_flag_invalid);
  552. d := 0/0;
  553. end;
  554. result := 0.0;
  555. end
  556. else
  557. begin
  558. w := d;
  559. { separate exponent and significand }
  560. z := frexp( d, e );
  561. { approximate square root of number between 0.5 and 1 }
  562. { relative error of approximation = 7.47e-3 }
  563. d := 4.173075996388649989089E-1 + 5.9016206709064458299663E-1 * z;
  564. { adjust for odd powers of 2 }
  565. if odd(e) then
  566. d := d*SQRT2;
  567. { re-insert exponent }
  568. d := ldexp( d, (e div 2) );
  569. { Newton iterations: }
  570. d := 0.5*(d + w/d);
  571. d := 0.5*(d + w/d);
  572. d := 0.5*(d + w/d);
  573. d := 0.5*(d + w/d);
  574. d := 0.5*(d + w/d);
  575. d := 0.5*(d + w/d);
  576. result := d;
  577. end;
  578. end;
  579. {$endif}
  580. {$ifndef FPC_SYSTEM_HAS_EXP}
  581. {$ifdef SUPPORT_DOUBLE}
  582. {
  583. This code was translated from uclib code, the original code
  584. had the following copyright notice:
  585. *
  586. * ====================================================
  587. * Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved.
  588. *
  589. * Developed at SunPro, a Sun Microsystems, Inc. business.
  590. * Permission to use, copy, modify, and distribute this
  591. * software is freely granted, provided that this notice
  592. * is preserved.
  593. * ====================================================
  594. *}
  595. {*
  596. * Returns the exponential of x.
  597. *
  598. * Method
  599. * 1. Argument reduction:
  600. * Reduce x to an r so that |r| <= 0.5*ln2 ~ 0.34658.
  601. * Given x, find r and integer k such that
  602. *
  603. * x = k*ln2 + r, |r| <= 0.5*ln2.
  604. *
  605. * Here r will be represented as r = hi-lo for better
  606. * accuracy.
  607. *
  608. * 2. Approximation of exp(r) by a special rational function on
  609. * the interval [0,0.34658]:
  610. * Write
  611. * R(r**2) = r*(exp(r)+1)/(exp(r)-1) = 2 + r*r/6 - r**4/360 + ...
  612. * We use a special Reme algorithm on [0,0.34658] to generate
  613. * a polynomial of degree 5 to approximate R. The maximum error
  614. * of this polynomial approximation is bounded by 2**-59. In
  615. * other words,
  616. * R(z) ~ 2.0 + P1*z + P2*z**2 + P3*z**3 + P4*z**4 + P5*z**5
  617. * (where z=r*r, and the values of P1 to P5 are listed below)
  618. * and
  619. * | 5 | -59
  620. * | 2.0+P1*z+...+P5*z - R(z) | <= 2
  621. * | |
  622. * The computation of exp(r) thus becomes
  623. * 2*r
  624. * exp(r) = 1 + -------
  625. * R - r
  626. * r*R1(r)
  627. * = 1 + r + ----------- (for better accuracy)
  628. * 2 - R1(r)
  629. * where
  630. 2 4 10
  631. * R1(r) = r - (P1*r + P2*r + ... + P5*r ).
  632. *
  633. * 3. Scale back to obtain exp(x):
  634. * From step 1, we have
  635. * exp(x) = 2^k * exp(r)
  636. *
  637. * Special cases:
  638. * exp(INF) is INF, exp(NaN) is NaN;
  639. * exp(-INF) is 0, and
  640. * for finite argument, only exp(0)=1 is exact.
  641. *
  642. * Accuracy:
  643. * according to an error analysis, the error is always less than
  644. * 1 ulp (unit in the last place).
  645. *
  646. * Misc. info.
  647. * For IEEE double
  648. * if x > 7.09782712893383973096e+02 then exp(x) overflow
  649. * if x < -7.45133219101941108420e+02 then exp(x) underflow
  650. *
  651. * Constants:
  652. * The hexadecimal values are the intended ones for the following
  653. * constants. The decimal values may be used, provided that the
  654. * compiler will convert from decimal to binary accurately enough
  655. * to produce the hexadecimal values shown.
  656. *
  657. }
  658. function fpc_exp_real(d: ValReal):ValReal;compilerproc;
  659. const
  660. one = 1.0;
  661. halF : array[0..1] of double = (0.5,-0.5);
  662. huge = 1.0e+300;
  663. twom1000 = 9.33263618503218878990e-302; { 2**-1000=0x01700000,0}
  664. o_threshold = 7.09782712893383973096e+02; { 0x40862E42, 0xFEFA39EF }
  665. u_threshold = -7.45133219101941108420e+02; { 0xc0874910, 0xD52D3051 }
  666. ln2HI : array[0..1] of double = ( 6.93147180369123816490e-01, { 0x3fe62e42, 0xfee00000 }
  667. -6.93147180369123816490e-01); { 0xbfe62e42, 0xfee00000 }
  668. ln2LO : array[0..1] of double = (1.90821492927058770002e-10, { 0x3dea39ef, 0x35793c76 }
  669. -1.90821492927058770002e-10); { 0xbdea39ef, 0x35793c76 }
  670. invln2 = 1.44269504088896338700e+00; { 0x3ff71547, 0x652b82fe }
  671. P1 = 1.66666666666666019037e-01; { 0x3FC55555, 0x5555553E }
  672. P2 = -2.77777777770155933842e-03; { 0xBF66C16C, 0x16BEBD93 }
  673. P3 = 6.61375632143793436117e-05; { 0x3F11566A, 0xAF25DE2C }
  674. P4 = -1.65339022054652515390e-06; { 0xBEBBBD41, 0xC5D26BF1 }
  675. P5 = 4.13813679705723846039e-08; { 0x3E663769, 0x72BEA4D0 }
  676. var
  677. c,hi,lo,t,y : double;
  678. k,xsb : longint;
  679. hx,hy,lx : dword;
  680. begin
  681. hi:=0.0;
  682. lo:=0.0;
  683. k:=0;
  684. {$ifdef FPC_DOUBLE_HILO_SWAPPED}
  685. hx:=float64(d).low;
  686. {$else}
  687. hx:=float64(d).high;
  688. {$endif FPC_DOUBLE_HILO_SWAPPED}
  689. xsb := (hx shr 31) and 1; { sign bit of d }
  690. hx := hx and $7fffffff; { high word of |d| }
  691. { filter out non-finite argument }
  692. if hx >= $40862E42 then
  693. begin { if |d|>=709.78... }
  694. if hx >= $7ff00000 then
  695. begin
  696. {$ifdef FPC_DOUBLE_HILO_SWAPPED}
  697. lx:=float64(d).high;
  698. {$else}
  699. lx:=float64(d).low;
  700. {$endif FPC_DOUBLE_HILO_SWAPPED}
  701. if ((hx and $fffff) or lx)<>0 then
  702. begin
  703. result:=d+d; { NaN }
  704. exit;
  705. end
  706. else
  707. begin
  708. if xsb=0 then begin
  709. float_raise(float_flag_overflow);
  710. result:=d
  711. end else
  712. result:=0.0; { exp(+-inf)=begininf,0end }
  713. exit;
  714. end;
  715. end;
  716. if d > o_threshold then begin
  717. float_raise(float_flag_overflow); { overflow }
  718. exit;
  719. end;
  720. if d < u_threshold then begin
  721. float_raise(float_flag_underflow); { underflow }
  722. exit;
  723. end;
  724. end;
  725. { argument reduction }
  726. if hx > $3fd62e42 then
  727. begin { if |d| > 0.5 ln2 }
  728. if hx < $3FF0A2B2 then { and |d| < 1.5 ln2 }
  729. begin
  730. hi := d-ln2HI[xsb];
  731. lo:=ln2LO[xsb];
  732. k := 1-xsb-xsb;
  733. end
  734. else
  735. begin
  736. k := round(invln2*d+halF[xsb]);
  737. t := k;
  738. hi := d - t*ln2HI[0]; { t*ln2HI is exact here }
  739. lo := t*ln2LO[0];
  740. end;
  741. d := hi - lo;
  742. end
  743. else if hx < $3e300000 then
  744. begin { when |d|<2**-28 }
  745. if huge+d>one then
  746. begin
  747. result:=one+d;{ trigger inexact }
  748. exit;
  749. end;
  750. end
  751. else
  752. k := 0;
  753. { d is now in primary range }
  754. t:=d*d;
  755. c:=d - t*(P1+t*(P2+t*(P3+t*(P4+t*P5))));
  756. if k=0 then
  757. begin
  758. result:=one-((d*c)/(c-2.0)-d);
  759. exit;
  760. end
  761. else
  762. y := one-((lo-(d*c)/(2.0-c))-hi);
  763. if k >= -1021 then
  764. begin
  765. {$ifdef FPC_DOUBLE_HILO_SWAPPED}
  766. hy:=float64(y).low;
  767. float64(y).low:=longint(hy)+(k shl 20); { add k to y's exponent }
  768. {$else}
  769. hy:=float64(y).high;
  770. float64(y).high:=longint(hy)+(k shl 20); { add k to y's exponent }
  771. {$endif FPC_DOUBLE_HILO_SWAPPED}
  772. result:=y;
  773. end
  774. else
  775. begin
  776. {$ifdef FPC_DOUBLE_HILO_SWAPPED}
  777. hy:=float64(y).low;
  778. float64(y).low:=longint(hy)+((k+1000) shl 20); { add k to y's exponent }
  779. {$else}
  780. hy:=float64(y).high;
  781. float64(y).high:=longint(hy)+((k+1000) shl 20); { add k to y's exponent }
  782. {$endif FPC_DOUBLE_HILO_SWAPPED}
  783. result:=y*twom1000;
  784. end;
  785. end;
  786. {$else SUPPORT_DOUBLE}
  787. function fpc_exp_real(d: ValReal):ValReal;compilerproc;
  788. {*****************************************************************}
  789. { Exponential Function }
  790. {*****************************************************************}
  791. { }
  792. { SYNOPSIS: }
  793. { }
  794. { double x, y, exp(); }
  795. { }
  796. { y = exp( x ); }
  797. { }
  798. { DESCRIPTION: }
  799. { }
  800. { Returns e (2.71828...) raised to the x power. }
  801. { }
  802. { Range reduction is accomplished by separating the argument }
  803. { into an integer k and fraction f such that }
  804. { }
  805. { x k f }
  806. { e = 2 e. }
  807. { }
  808. { A Pade' form of degree 2/3 is used to approximate exp(f)- 1 }
  809. { in the basic range [-0.5 ln 2, 0.5 ln 2]. }
  810. {*****************************************************************}
  811. const P : TabCoef = (
  812. 1.26183092834458542160E-4,
  813. 3.02996887658430129200E-2,
  814. 1.00000000000000000000E0, 0, 0, 0, 0);
  815. Q : TabCoef = (
  816. 3.00227947279887615146E-6,
  817. 2.52453653553222894311E-3,
  818. 2.27266044198352679519E-1,
  819. 2.00000000000000000005E0, 0 ,0 ,0);
  820. C1 = 6.9335937500000000000E-1;
  821. C2 = 2.1219444005469058277E-4;
  822. var n : Integer;
  823. px, qx, xx : Real;
  824. begin
  825. if( d > MAXLOG) then
  826. float_raise(float_flag_overflow)
  827. else
  828. if( d < MINLOG ) then
  829. begin
  830. float_raise(float_flag_underflow);
  831. end
  832. else
  833. begin
  834. { Express e**x = e**g 2**n }
  835. { = e**g e**( n loge(2) ) }
  836. { = e**( g + n loge(2) ) }
  837. px := d * LOG2E;
  838. qx := Trunc( px + 0.5 ); { Trunc() truncates toward -infinity. }
  839. n := Trunc(qx);
  840. d := d - qx * C1;
  841. d := d + qx * C2;
  842. { rational approximation for exponential }
  843. { of the fractional part: }
  844. { e**x - 1 = 2x P(x**2)/( Q(x**2) - P(x**2) ) }
  845. xx := d * d;
  846. px := d * polevl( xx, P, 2 );
  847. d := px/( polevl( xx, Q, 3 ) - px );
  848. d := ldexp( d, 1 );
  849. d := d + 1.0;
  850. d := ldexp( d, n );
  851. result := d;
  852. end;
  853. end;
  854. {$endif SUPPORT_DOUBLE}
  855. {$endif}
  856. {$ifndef FPC_SYSTEM_HAS_ROUND}
  857. function fpc_round_real(d : ValReal) : int64;compilerproc;
  858. var
  859. fr: Real;
  860. tr: Int64;
  861. Begin
  862. fr := abs(Frac(d));
  863. tr := Trunc(d);
  864. if fr > 0.5 then
  865. if d >= 0 then
  866. result:=tr+1
  867. else
  868. result:=tr-1
  869. else
  870. if fr < 0.5 then
  871. result:=tr
  872. else { fr = 0.5 }
  873. { check sign to decide ... }
  874. { as in Turbo Pascal... }
  875. if d >= 0.0 then
  876. result:=tr+1
  877. else
  878. result:=tr;
  879. end;
  880. {$endif FPC_SYSTEM_HAS_EXP}
  881. {$ifdef FPC_CURRENCY_IS_INT64}
  882. function trunc(c : currency) : int64;
  883. type
  884. tmyrec = record
  885. i: int64;
  886. end;
  887. begin
  888. result := int64(tmyrec(c)) div 10000
  889. end;
  890. function trunc(c : comp) : int64;
  891. begin
  892. result := c
  893. end;
  894. function round(c : currency) : int64;
  895. type
  896. tmyrec = record
  897. i: int64;
  898. end;
  899. var
  900. rem, absrem: longint;
  901. begin
  902. { (int64(tmyrec(c))(+/-)5000) div 10000 can overflow }
  903. result := int64(tmyrec(c)) div 10000;
  904. rem := int64(tmyrec(c)) - result * 10000;
  905. absrem := abs(rem);
  906. if (absrem > 5000) or
  907. ((absrem = 5000) and
  908. (rem > 0)) then
  909. if (rem > 0) then
  910. inc(result)
  911. else
  912. dec(result);
  913. end;
  914. function round(c : comp) : int64;
  915. begin
  916. result := c
  917. end;
  918. {$endif FPC_CURRENCY_IS_INT64}
  919. {$ifndef FPC_SYSTEM_HAS_LN}
  920. function fpc_ln_real(d:ValReal):ValReal;compilerproc;
  921. {*****************************************************************}
  922. { Natural Logarithm }
  923. {*****************************************************************}
  924. { }
  925. { SYNOPSIS: }
  926. { }
  927. { double x, y, log(); }
  928. { }
  929. { y = ln( x ); }
  930. { }
  931. { DESCRIPTION: }
  932. { }
  933. { Returns the base e (2.718...) logarithm of x. }
  934. { }
  935. { The argument is separated into its exponent and fractional }
  936. { parts. If the exponent is between -1 and +1, the logarithm }
  937. { of the fraction is approximated by }
  938. { }
  939. { log(1+x) = x - 0.5 x**2 + x**3 P(x)/Q(x). }
  940. { }
  941. { Otherwise, setting z = 2(x-1)/x+1), }
  942. { }
  943. { log(x) = z + z**3 P(z)/Q(z). }
  944. { }
  945. {*****************************************************************}
  946. const P : TabCoef = (
  947. { Coefficients for log(1+x) = x - x**2/2 + x**3 P(x)/Q(x)
  948. 1/sqrt(2) <= x < sqrt(2) }
  949. 4.58482948458143443514E-5,
  950. 4.98531067254050724270E-1,
  951. 6.56312093769992875930E0,
  952. 2.97877425097986925891E1,
  953. 6.06127134467767258030E1,
  954. 5.67349287391754285487E1,
  955. 1.98892446572874072159E1);
  956. Q : TabCoef = (
  957. 1.50314182634250003249E1,
  958. 8.27410449222435217021E1,
  959. 2.20664384982121929218E2,
  960. 3.07254189979530058263E2,
  961. 2.14955586696422947765E2,
  962. 5.96677339718622216300E1, 0);
  963. { Coefficients for log(x) = z + z**3 P(z)/Q(z),
  964. where z = 2(x-1)/(x+1)
  965. 1/sqrt(2) <= x < sqrt(2) }
  966. R : TabCoef = (
  967. -7.89580278884799154124E-1,
  968. 1.63866645699558079767E1,
  969. -6.41409952958715622951E1, 0, 0, 0, 0);
  970. S : TabCoef = (
  971. -3.56722798256324312549E1,
  972. 3.12093766372244180303E2,
  973. -7.69691943550460008604E2, 0, 0, 0, 0);
  974. var e : Integer;
  975. z, y : Real;
  976. Label Ldone;
  977. begin
  978. if( d <= 0.0 ) then
  979. begin
  980. float_raise(float_flag_invalid);
  981. exit;
  982. end;
  983. d := frexp( d, e );
  984. { logarithm using log(x) = z + z**3 P(z)/Q(z),
  985. where z = 2(x-1)/x+1) }
  986. if( (e > 2) or (e < -2) ) then
  987. begin
  988. if( d < SQRTH ) then
  989. begin
  990. { 2( 2x-1 )/( 2x+1 ) }
  991. Dec(e, 1);
  992. z := d - 0.5;
  993. y := 0.5 * z + 0.5;
  994. end
  995. else
  996. begin
  997. { 2 (x-1)/(x+1) }
  998. z := d - 0.5;
  999. z := z - 0.5;
  1000. y := 0.5 * d + 0.5;
  1001. end;
  1002. d := z / y;
  1003. { /* rational form */ }
  1004. z := d*d;
  1005. z := d + d * ( z * polevl( z, R, 2 ) / p1evl( z, S, 3 ) );
  1006. goto ldone;
  1007. end;
  1008. { logarithm using log(1+x) = x - .5x**2 + x**3 P(x)/Q(x) }
  1009. if( d < SQRTH ) then
  1010. begin
  1011. Dec(e, 1);
  1012. d := ldexp( d, 1 ) - 1.0; { 2x - 1 }
  1013. end
  1014. else
  1015. d := d - 1.0;
  1016. { rational form }
  1017. z := d*d;
  1018. y := d * ( z * polevl( d, P, 6 ) / p1evl( d, Q, 6 ) );
  1019. y := y - ldexp( z, -1 ); { y - 0.5 * z }
  1020. z := d + y;
  1021. ldone:
  1022. { recombine with exponent term }
  1023. if( e <> 0 ) then
  1024. begin
  1025. y := e;
  1026. z := z - y * 2.121944400546905827679e-4;
  1027. z := z + y * 0.693359375;
  1028. end;
  1029. result:= z;
  1030. end;
  1031. {$endif}
  1032. {$ifndef FPC_SYSTEM_HAS_SIN}
  1033. function fpc_Sin_real(d:ValReal):ValReal;compilerproc;
  1034. {*****************************************************************}
  1035. { Circular Sine }
  1036. {*****************************************************************}
  1037. { }
  1038. { SYNOPSIS: }
  1039. { }
  1040. { double x, y, sin(); }
  1041. { }
  1042. { y = sin( x ); }
  1043. { }
  1044. { DESCRIPTION: }
  1045. { }
  1046. { Range reduction is into intervals of pi/4. The reduction }
  1047. { error is nearly eliminated by contriving an extended }
  1048. { precision modular arithmetic. }
  1049. { }
  1050. { Two polynomial approximating functions are employed. }
  1051. { Between 0 and pi/4 the sine is approximated by }
  1052. { x + x**3 P(x**2). }
  1053. { Between pi/4 and pi/2 the cosine is represented as }
  1054. { 1 - x**2 Q(x**2). }
  1055. {*****************************************************************}
  1056. var y, z, zz : Real;
  1057. j, sign : Integer;
  1058. begin
  1059. { make argument positive but save the sign }
  1060. sign := 1;
  1061. if( d < 0 ) then
  1062. begin
  1063. d := -d;
  1064. sign := -1;
  1065. end;
  1066. { above this value, approximate towards 0 }
  1067. if( d > lossth ) then
  1068. begin
  1069. result := 0.0;
  1070. exit;
  1071. end;
  1072. y := Trunc( d/PIO4 ); { integer part of x/PIO4 }
  1073. { strip high bits of integer part to prevent integer overflow }
  1074. z := ldexp( y, -4 );
  1075. z := Trunc(z); { integer part of y/8 }
  1076. z := y - ldexp( z, 4 ); { y - 16 * (y/16) }
  1077. j := Trunc(z); { convert to integer for tests on the phase angle }
  1078. { map zeros to origin }
  1079. { typecast is to avoid "can't determine which overloaded function }
  1080. { to call" }
  1081. if odd( longint(j) ) then
  1082. begin
  1083. inc(j);
  1084. y := y + 1.0;
  1085. end;
  1086. j := j and 7; { octant modulo 360 degrees }
  1087. { reflect in x axis }
  1088. if( j > 3) then
  1089. begin
  1090. sign := -sign;
  1091. dec(j, 4);
  1092. end;
  1093. { Extended precision modular arithmetic }
  1094. z := ((d - y * DP1) - y * DP2) - y * DP3;
  1095. zz := z * z;
  1096. if( (j=1) or (j=2) ) then
  1097. y := 1.0 - ldexp(zz,-1) + zz * zz * polevl( zz, coscof, 5 )
  1098. else
  1099. { y = z + z * (zz * polevl( zz, sincof, 5 )); }
  1100. y := z + z * z * z * polevl( zz, sincof, 5 );
  1101. if(sign < 0) then
  1102. y := -y;
  1103. result := y;
  1104. end;
  1105. {$endif}
  1106. {$ifndef FPC_SYSTEM_HAS_COS}
  1107. function fpc_Cos_real(d:ValReal):ValReal;compilerproc;
  1108. {*****************************************************************}
  1109. { Circular cosine }
  1110. {*****************************************************************}
  1111. { }
  1112. { Circular cosine }
  1113. { }
  1114. { SYNOPSIS: }
  1115. { }
  1116. { double x, y, cos(); }
  1117. { }
  1118. { y = cos( x ); }
  1119. { }
  1120. { DESCRIPTION: }
  1121. { }
  1122. { Range reduction is into intervals of pi/4. The reduction }
  1123. { error is nearly eliminated by contriving an extended }
  1124. { precision modular arithmetic. }
  1125. { }
  1126. { Two polynomial approximating functions are employed. }
  1127. { Between 0 and pi/4 the cosine is approximated by }
  1128. { 1 - x**2 Q(x**2). }
  1129. { Between pi/4 and pi/2 the sine is represented as }
  1130. { x + x**3 P(x**2). }
  1131. {*****************************************************************}
  1132. var y, z, zz : Real;
  1133. j, sign : Integer;
  1134. i : LongInt;
  1135. begin
  1136. { make argument positive }
  1137. sign := 1;
  1138. if( d < 0 ) then
  1139. d := -d;
  1140. { above this value, round towards zero }
  1141. if( d > lossth ) then
  1142. begin
  1143. result := 0.0;
  1144. exit;
  1145. end;
  1146. y := Trunc( d/PIO4 );
  1147. z := ldexp( y, -4 );
  1148. z := Trunc(z); { integer part of y/8 }
  1149. z := y - ldexp( z, 4 ); { y - 16 * (y/16) }
  1150. { integer and fractional part modulo one octant }
  1151. i := Trunc(z);
  1152. if odd( i ) then { map zeros to origin }
  1153. begin
  1154. inc(i);
  1155. y := y + 1.0;
  1156. end;
  1157. j := i and 07;
  1158. if( j > 3) then
  1159. begin
  1160. dec(j,4);
  1161. sign := -sign;
  1162. end;
  1163. if( j > 1 ) then
  1164. sign := -sign;
  1165. { Extended precision modular arithmetic }
  1166. z := ((d - y * DP1) - y * DP2) - y * DP3;
  1167. zz := z * z;
  1168. if( (j=1) or (j=2) ) then
  1169. { y = z + z * (zz * polevl( zz, sincof, 5 )); }
  1170. y := z + z * z * z * polevl( zz, sincof, 5 )
  1171. else
  1172. y := 1.0 - ldexp(zz,-1) + zz * zz * polevl( zz, coscof, 5 );
  1173. if(sign < 0) then
  1174. y := -y;
  1175. result := y ;
  1176. end;
  1177. {$endif}
  1178. {$ifndef FPC_SYSTEM_HAS_ARCTAN}
  1179. function fpc_ArcTan_real(d:ValReal):ValReal;compilerproc;
  1180. {*****************************************************************}
  1181. { Inverse circular tangent (arctangent) }
  1182. {*****************************************************************}
  1183. { }
  1184. { SYNOPSIS: }
  1185. { }
  1186. { double x, y, atan(); }
  1187. { }
  1188. { y = atan( x ); }
  1189. { }
  1190. { DESCRIPTION: }
  1191. { }
  1192. { Returns radian angle between -pi/2 and +pi/2 whose tangent }
  1193. { is x. }
  1194. { }
  1195. { Range reduction is from four intervals into the interval }
  1196. { from zero to tan( pi/8 ). The approximant uses a rational }
  1197. { function of degree 3/4 of the form x + x**3 P(x)/Q(x). }
  1198. {*****************************************************************}
  1199. const P : TabCoef = (
  1200. -8.40980878064499716001E-1,
  1201. -8.83860837023772394279E0,
  1202. -2.18476213081316705724E1,
  1203. -1.48307050340438946993E1, 0, 0, 0);
  1204. Q : TabCoef = (
  1205. 1.54974124675307267552E1,
  1206. 6.27906555762653017263E1,
  1207. 9.22381329856214406485E1,
  1208. 4.44921151021319438465E1, 0, 0, 0);
  1209. { tan( 3*pi/8 ) }
  1210. T3P8 = 2.41421356237309504880;
  1211. { tan( pi/8 ) }
  1212. TP8 = 0.41421356237309504880;
  1213. var y,z : Real;
  1214. Sign : Integer;
  1215. begin
  1216. { make argument positive and save the sign }
  1217. sign := 1;
  1218. if( d < 0.0 ) then
  1219. begin
  1220. sign := -1;
  1221. d := -d;
  1222. end;
  1223. { range reduction }
  1224. if( d > T3P8 ) then
  1225. begin
  1226. y := PIO2;
  1227. d := -( 1.0/d );
  1228. end
  1229. else if( d > TP8 ) then
  1230. begin
  1231. y := PIO4;
  1232. d := (d-1.0)/(d+1.0);
  1233. end
  1234. else
  1235. y := 0.0;
  1236. { rational form in x**2 }
  1237. z := d * d;
  1238. y := y + ( polevl( z, P, 3 ) / p1evl( z, Q, 4 ) ) * z * d + d;
  1239. if( sign < 0 ) then
  1240. y := -y;
  1241. result := y;
  1242. end;
  1243. {$endif}
  1244. {$ifndef FPC_SYSTEM_HAS_FRAC}
  1245. function fpc_frac_real(d : ValReal) : ValReal;compilerproc;
  1246. begin
  1247. result := d - Int(d);
  1248. end;
  1249. {$endif}
  1250. {$ifdef FPC_INCLUDE_SOFTWARE_INT64_TO_DOUBLE}
  1251. {$ifndef FPC_SYSTEM_HAS_QWORD_TO_DOUBLE}
  1252. function fpc_qword_to_double(q : qword): double; compilerproc;
  1253. begin
  1254. result:=dword(q and $ffffffff)+dword(q shr 32)*double(4294967296.0);
  1255. end;
  1256. {$endif FPC_SYSTEM_HAS_INT64_TO_DOUBLE}
  1257. {$ifndef FPC_SYSTEM_HAS_INT64_TO_DOUBLE}
  1258. function fpc_int64_to_double(i : int64): double; compilerproc;
  1259. begin
  1260. if i<0 then
  1261. result:=-double(qword(-i))
  1262. else
  1263. result:=qword(i);
  1264. end;
  1265. {$endif FPC_SYSTEM_HAS_INT64_TO_DOUBLE}
  1266. {$endif FPC_INCLUDE_SOFTWARE_INT64_TO_DOUBLE}
  1267. {$ifdef SUPPORT_DOUBLE}
  1268. {****************************************************************************
  1269. Helper routines to support old TP styled reals
  1270. ****************************************************************************}
  1271. {$ifndef FPC_SYSTEM_HAS_REAL2DOUBLE}
  1272. function real2double(r : real48) : double;
  1273. var
  1274. res : array[0..7] of byte;
  1275. exponent : word;
  1276. begin
  1277. { copy mantissa }
  1278. res[0]:=0;
  1279. res[1]:=r[1] shl 5;
  1280. res[2]:=(r[1] shr 3) or (r[2] shl 5);
  1281. res[3]:=(r[2] shr 3) or (r[3] shl 5);
  1282. res[4]:=(r[3] shr 3) or (r[4] shl 5);
  1283. res[5]:=(r[4] shr 3) or (r[5] and $7f) shl 5;
  1284. res[6]:=(r[5] and $7f) shr 3;
  1285. { copy exponent }
  1286. { correct exponent: }
  1287. exponent:=(word(r[0])+(1023-129));
  1288. res[6]:=res[6] or ((exponent and $f) shl 4);
  1289. res[7]:=exponent shr 4;
  1290. { set sign }
  1291. res[7]:=res[7] or (r[5] and $80);
  1292. real2double:=double(res);
  1293. end;
  1294. {$endif FPC_SYSTEM_HAS_REAL2DOUBLE}
  1295. {$endif SUPPORT_DOUBLE}
  1296. {$ifdef SUPPORT_EXTENDED}
  1297. { fast 10^n routine }
  1298. function FPower10(val: Extended; Power: Longint): Extended;
  1299. const
  1300. pow32 : array[0..31] of extended =
  1301. (
  1302. 1e0,1e1,1e2,1e3,1e4,1e5,1e6,1e7,1e8,1e9,1e10,
  1303. 1e11,1e12,1e13,1e14,1e15,1e16,1e17,1e18,1e19,1e20,
  1304. 1e21,1e22,1e23,1e24,1e25,1e26,1e27,1e28,1e29,1e30,
  1305. 1e31
  1306. );
  1307. pow512 : array[0..15] of extended =
  1308. (
  1309. 1,1e32,1e64,1e96,1e128,1e160,1e192,1e224,
  1310. 1e256,1e288,1e320,1e352,1e384,1e416,1e448,
  1311. 1e480
  1312. );
  1313. pow4096 : array[0..9] of extended =
  1314. (1,1e512,1e1024,1e1536,
  1315. 1e2048,1e2560,1e3072,1e3584,
  1316. 1e4096,1e4608
  1317. );
  1318. negpow32 : array[0..31] of extended =
  1319. (
  1320. 1e-0,1e-1,1e-2,1e-3,1e-4,1e-5,1e-6,1e-7,1e-8,1e-9,1e-10,
  1321. 1e-11,1e-12,1e-13,1e-14,1e-15,1e-16,1e-17,1e-18,1e-19,1e-20,
  1322. 1e-21,1e-22,1e-23,1e-24,1e-25,1e-26,1e-27,1e-28,1e-29,1e-30,
  1323. 1e-31
  1324. );
  1325. negpow512 : array[0..15] of extended =
  1326. (
  1327. 0,1e-32,1e-64,1e-96,1e-128,1e-160,1e-192,1e-224,
  1328. 1e-256,1e-288,1e-320,1e-352,1e-384,1e-416,1e-448,
  1329. 1e-480
  1330. );
  1331. negpow4096 : array[0..9] of extended =
  1332. (
  1333. 0,1e-512,1e-1024,1e-1536,
  1334. 1e-2048,1e-2560,1e-3072,1e-3584,
  1335. 1e-4096,1e-4608
  1336. );
  1337. begin
  1338. if Power<0 then
  1339. begin
  1340. Power:=-Power;
  1341. result:=val*negpow32[Power and $1f];
  1342. power:=power shr 5;
  1343. if power<>0 then
  1344. begin
  1345. result:=result*negpow512[Power and $f];
  1346. power:=power shr 4;
  1347. if power<>0 then
  1348. begin
  1349. if power<=9 then
  1350. result:=result*negpow4096[Power]
  1351. else
  1352. result:=1.0/0.0;
  1353. end;
  1354. end;
  1355. end
  1356. else
  1357. begin
  1358. result:=val*pow32[Power and $1f];
  1359. power:=power shr 5;
  1360. if power<>0 then
  1361. begin
  1362. result:=result*pow512[Power and $f];
  1363. power:=power shr 4;
  1364. if power<>0 then
  1365. begin
  1366. if power<=9 then
  1367. result:=result*pow4096[Power]
  1368. else
  1369. result:=1.0/0.0;
  1370. end;
  1371. end;
  1372. end;
  1373. end;
  1374. {$endif SUPPORT_EXTENDED}