powl.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739
  1. /* powl.c
  2. *
  3. * Power function, long double precision
  4. *
  5. *
  6. *
  7. * SYNOPSIS:
  8. *
  9. * long double x, y, z, powl();
  10. *
  11. * z = powl( x, y );
  12. *
  13. *
  14. *
  15. * DESCRIPTION:
  16. *
  17. * Computes x raised to the yth power. Analytically,
  18. *
  19. * x**y = exp( y log(x) ).
  20. *
  21. * Following Cody and Waite, this program uses a lookup table
  22. * of 2**-i/32 and pseudo extended precision arithmetic to
  23. * obtain several extra bits of accuracy in both the logarithm
  24. * and the exponential.
  25. *
  26. *
  27. *
  28. * ACCURACY:
  29. *
  30. * The relative error of pow(x,y) can be estimated
  31. * by y dl ln(2), where dl is the absolute error of
  32. * the internally computed base 2 logarithm. At the ends
  33. * of the approximation interval the logarithm equal 1/32
  34. * and its relative error is about 1 lsb = 1.1e-19. Hence
  35. * the predicted relative error in the result is 2.3e-21 y .
  36. *
  37. * Relative error:
  38. * arithmetic domain # trials peak rms
  39. *
  40. * IEEE +-1000 40000 2.8e-18 3.7e-19
  41. * .001 < x < 1000, with log(x) uniformly distributed.
  42. * -1000 < y < 1000, y uniformly distributed.
  43. *
  44. * IEEE 0,8700 60000 6.5e-18 1.0e-18
  45. * 0.99 < x < 1.01, 0 < y < 8700, uniformly distributed.
  46. *
  47. *
  48. * ERROR MESSAGES:
  49. *
  50. * message condition value returned
  51. * pow overflow x**y > MAXNUM INFINITY
  52. * pow underflow x**y < 1/MAXNUM 0.0
  53. * pow domain x<0 and y noninteger 0.0
  54. *
  55. */
  56. /*
  57. Cephes Math Library Release 2.7: May, 1998
  58. Copyright 1984, 1991, 1998 by Stephen L. Moshier
  59. */
  60. #include <math.h>
  61. static char fname[] = {"powl"};
  62. /* Table size */
  63. #define NXT 32
  64. /* log2(Table size) */
  65. #define LNXT 5
  66. #ifdef UNK
  67. /* log(1+x) = x - .5x^2 + x^3 * P(z)/Q(z)
  68. * on the domain 2^(-1/32) - 1 <= x <= 2^(1/32) - 1
  69. */
  70. static long double P[] = {
  71. 8.3319510773868690346226E-4L,
  72. 4.9000050881978028599627E-1L,
  73. 1.7500123722550302671919E0L,
  74. 1.4000100839971580279335E0L,
  75. };
  76. static long double Q[] = {
  77. /* 1.0000000000000000000000E0L,*/
  78. 5.2500282295834889175431E0L,
  79. 8.4000598057587009834666E0L,
  80. 4.2000302519914740834728E0L,
  81. };
  82. /* A[i] = 2^(-i/32), rounded to IEEE long double precision.
  83. * If i is even, A[i] + B[i/2] gives additional accuracy.
  84. */
  85. static long double A[33] = {
  86. 1.0000000000000000000000E0L,
  87. 9.7857206208770013448287E-1L,
  88. 9.5760328069857364691013E-1L,
  89. 9.3708381705514995065011E-1L,
  90. 9.1700404320467123175367E-1L,
  91. 8.9735453750155359320742E-1L,
  92. 8.7812608018664974155474E-1L,
  93. 8.5930964906123895780165E-1L,
  94. 8.4089641525371454301892E-1L,
  95. 8.2287773907698242225554E-1L,
  96. 8.0524516597462715409607E-1L,
  97. 7.8799042255394324325455E-1L,
  98. 7.7110541270397041179298E-1L,
  99. 7.5458221379671136985669E-1L,
  100. 7.3841307296974965571198E-1L,
  101. 7.2259040348852331001267E-1L,
  102. 7.0710678118654752438189E-1L,
  103. 6.9195494098191597746178E-1L,
  104. 6.7712777346844636413344E-1L,
  105. 6.6261832157987064729696E-1L,
  106. 6.4841977732550483296079E-1L,
  107. 6.3452547859586661129850E-1L,
  108. 6.2092890603674202431705E-1L,
  109. 6.0762367999023443907803E-1L,
  110. 5.9460355750136053334378E-1L,
  111. 5.8186242938878875689693E-1L,
  112. 5.6939431737834582684856E-1L,
  113. 5.5719337129794626814472E-1L,
  114. 5.4525386633262882960438E-1L,
  115. 5.3357020033841180906486E-1L,
  116. 5.2213689121370692017331E-1L,
  117. 5.1094857432705833910408E-1L,
  118. 5.0000000000000000000000E-1L,
  119. };
  120. static long double B[17] = {
  121. 0.0000000000000000000000E0L,
  122. 2.6176170809902549338711E-20L,
  123. -1.0126791927256478897086E-20L,
  124. 1.3438228172316276937655E-21L,
  125. 1.2207982955417546912101E-20L,
  126. -6.3084814358060867200133E-21L,
  127. 1.3164426894366316434230E-20L,
  128. -1.8527916071632873716786E-20L,
  129. 1.8950325588932570796551E-20L,
  130. 1.5564775779538780478155E-20L,
  131. 6.0859793637556860974380E-21L,
  132. -2.0208749253662532228949E-20L,
  133. 1.4966292219224761844552E-20L,
  134. 3.3540909728056476875639E-21L,
  135. -8.6987564101742849540743E-22L,
  136. -1.2327176863327626135542E-20L,
  137. 0.0000000000000000000000E0L,
  138. };
  139. /* 2^x = 1 + x P(x),
  140. * on the interval -1/32 <= x <= 0
  141. */
  142. static long double R[] = {
  143. 1.5089970579127659901157E-5L,
  144. 1.5402715328927013076125E-4L,
  145. 1.3333556028915671091390E-3L,
  146. 9.6181291046036762031786E-3L,
  147. 5.5504108664798463044015E-2L,
  148. 2.4022650695910062854352E-1L,
  149. 6.9314718055994530931447E-1L,
  150. };
  151. #define douba(k) A[k]
  152. #define doubb(k) B[k]
  153. #define MEXP (NXT*16384.0L)
  154. /* The following if denormal numbers are supported, else -MEXP: */
  155. #ifdef DENORMAL
  156. #define MNEXP (-NXT*(16384.0L+64.0L))
  157. #else
  158. #define MNEXP (-NXT*16384.0L)
  159. #endif
  160. /* log2(e) - 1 */
  161. #define LOG2EA 0.44269504088896340735992L
  162. #endif
  163. #ifdef IBMPC
  164. static short P[] = {
  165. 0xb804,0xa8b7,0xc6f4,0xda6a,0x3ff4, XPD
  166. 0x7de9,0xcf02,0x58c0,0xfae1,0x3ffd, XPD
  167. 0x405a,0x3722,0x67c9,0xe000,0x3fff, XPD
  168. 0xcd99,0x6b43,0x87ca,0xb333,0x3fff, XPD
  169. };
  170. static short Q[] = {
  171. /* 0x0000,0x0000,0x0000,0x8000,0x3fff, */
  172. 0x6307,0xa469,0x3b33,0xa800,0x4001, XPD
  173. 0xfec2,0x62d7,0xa51c,0x8666,0x4002, XPD
  174. 0xda32,0xd072,0xa5d7,0x8666,0x4001, XPD
  175. };
  176. static short A[] = {
  177. 0x0000,0x0000,0x0000,0x8000,0x3fff, XPD
  178. 0x033a,0x722a,0xb2db,0xfa83,0x3ffe, XPD
  179. 0xcc2c,0x2486,0x7d15,0xf525,0x3ffe, XPD
  180. 0xf5cb,0xdcda,0xb99b,0xefe4,0x3ffe, XPD
  181. 0x392f,0xdd24,0xc6e7,0xeac0,0x3ffe, XPD
  182. 0x48a8,0x7c83,0x06e7,0xe5b9,0x3ffe, XPD
  183. 0xe111,0x2a94,0xdeec,0xe0cc,0x3ffe, XPD
  184. 0x3755,0xdaf2,0xb797,0xdbfb,0x3ffe, XPD
  185. 0x6af4,0xd69d,0xfcca,0xd744,0x3ffe, XPD
  186. 0xe45a,0xf12a,0x1d91,0xd2a8,0x3ffe, XPD
  187. 0x80e4,0x1f84,0x8c15,0xce24,0x3ffe, XPD
  188. 0x27a3,0x6e2f,0xbd86,0xc9b9,0x3ffe, XPD
  189. 0xdadd,0x5506,0x2a11,0xc567,0x3ffe, XPD
  190. 0x9456,0x6670,0x4cca,0xc12c,0x3ffe, XPD
  191. 0x36bf,0x580c,0xa39f,0xbd08,0x3ffe, XPD
  192. 0x9ee9,0x62fb,0xaf47,0xb8fb,0x3ffe, XPD
  193. 0x6484,0xf9de,0xf333,0xb504,0x3ffe, XPD
  194. 0x2590,0xd2ac,0xf581,0xb123,0x3ffe, XPD
  195. 0x4ac6,0x42a1,0x3eea,0xad58,0x3ffe, XPD
  196. 0x0ef8,0xea7c,0x5ab4,0xa9a1,0x3ffe, XPD
  197. 0x38ea,0xb151,0xd6a9,0xa5fe,0x3ffe, XPD
  198. 0x6819,0x0c49,0x4303,0xa270,0x3ffe, XPD
  199. 0x11ae,0x91a1,0x3260,0x9ef5,0x3ffe, XPD
  200. 0x5539,0xd54e,0x39b9,0x9b8d,0x3ffe, XPD
  201. 0xa96f,0x8db8,0xf051,0x9837,0x3ffe, XPD
  202. 0x0961,0xfef7,0xefa8,0x94f4,0x3ffe, XPD
  203. 0xc336,0xab11,0xd373,0x91c3,0x3ffe, XPD
  204. 0x53c0,0x45cd,0x398b,0x8ea4,0x3ffe, XPD
  205. 0xd6e7,0xea8b,0xc1e3,0x8b95,0x3ffe, XPD
  206. 0x8527,0x92da,0x0e80,0x8898,0x3ffe, XPD
  207. 0x7b15,0xcc48,0xc367,0x85aa,0x3ffe, XPD
  208. 0xa1d7,0xac2b,0x8698,0x82cd,0x3ffe, XPD
  209. 0x0000,0x0000,0x0000,0x8000,0x3ffe, XPD
  210. };
  211. static short B[] = {
  212. 0x0000,0x0000,0x0000,0x0000,0x0000, XPD
  213. 0x1f87,0xdb30,0x18f5,0xf73a,0x3fbd, XPD
  214. 0xac15,0x3e46,0x2932,0xbf4a,0xbfbc, XPD
  215. 0x7944,0xba66,0xa091,0xcb12,0x3fb9, XPD
  216. 0xff78,0x40b4,0x2ee6,0xe69a,0x3fbc, XPD
  217. 0xc895,0x5069,0xe383,0xee53,0xbfbb, XPD
  218. 0x7cde,0x9376,0x4325,0xf8ab,0x3fbc, XPD
  219. 0xa10c,0x25e0,0xc093,0xaefd,0xbfbd, XPD
  220. 0x7d3e,0xea95,0x1366,0xb2fb,0x3fbd, XPD
  221. 0x5d89,0xeb34,0x5191,0x9301,0x3fbd, XPD
  222. 0x80d9,0xb883,0xfb10,0xe5eb,0x3fbb, XPD
  223. 0x045d,0x288c,0xc1ec,0xbedd,0xbfbd, XPD
  224. 0xeded,0x5c85,0x4630,0x8d5a,0x3fbd, XPD
  225. 0x9d82,0xe5ac,0x8e0a,0xfd6d,0x3fba, XPD
  226. 0x6dfd,0xeb58,0xaf14,0x8373,0xbfb9, XPD
  227. 0xf938,0x7aac,0x91cf,0xe8da,0xbfbc, XPD
  228. 0x0000,0x0000,0x0000,0x0000,0x0000, XPD
  229. };
  230. static short R[] = {
  231. 0xa69b,0x530e,0xee1d,0xfd2a,0x3fee, XPD
  232. 0xc746,0x8e7e,0x5960,0xa182,0x3ff2, XPD
  233. 0x63b6,0xadda,0xfd6a,0xaec3,0x3ff5, XPD
  234. 0xc104,0xfd99,0x5b7c,0x9d95,0x3ff8, XPD
  235. 0xe05e,0x249d,0x46b8,0xe358,0x3ffa, XPD
  236. 0x5d1d,0x162c,0xeffc,0xf5fd,0x3ffc, XPD
  237. 0x79aa,0xd1cf,0x17f7,0xb172,0x3ffe, XPD
  238. };
  239. /* 10 byte sizes versus 12 byte */
  240. #define douba(k) (*(long double *)(&A[(sizeof( long double )/2)*(k)]))
  241. #define doubb(k) (*(long double *)(&B[(sizeof( long double )/2)*(k)]))
  242. #define MEXP (NXT*16384.0L)
  243. #ifdef DENORMAL
  244. #define MNEXP (-NXT*(16384.0L+64.0L))
  245. #else
  246. #define MNEXP (-NXT*16384.0L)
  247. #endif
  248. static short L[] = {0xc2ef,0x705f,0xeca5,0xe2a8,0x3ffd, XPD};
  249. #define LOG2EA (*(long double *)(&L[0]))
  250. #endif
  251. #ifdef MIEEE
  252. static long P[] = {
  253. 0x3ff40000,0xda6ac6f4,0xa8b7b804,
  254. 0x3ffd0000,0xfae158c0,0xcf027de9,
  255. 0x3fff0000,0xe00067c9,0x3722405a,
  256. 0x3fff0000,0xb33387ca,0x6b43cd99,
  257. };
  258. static long Q[] = {
  259. /* 0x3fff0000,0x80000000,0x00000000, */
  260. 0x40010000,0xa8003b33,0xa4696307,
  261. 0x40020000,0x8666a51c,0x62d7fec2,
  262. 0x40010000,0x8666a5d7,0xd072da32,
  263. };
  264. static long A[] = {
  265. 0x3fff0000,0x80000000,0x00000000,
  266. 0x3ffe0000,0xfa83b2db,0x722a033a,
  267. 0x3ffe0000,0xf5257d15,0x2486cc2c,
  268. 0x3ffe0000,0xefe4b99b,0xdcdaf5cb,
  269. 0x3ffe0000,0xeac0c6e7,0xdd24392f,
  270. 0x3ffe0000,0xe5b906e7,0x7c8348a8,
  271. 0x3ffe0000,0xe0ccdeec,0x2a94e111,
  272. 0x3ffe0000,0xdbfbb797,0xdaf23755,
  273. 0x3ffe0000,0xd744fcca,0xd69d6af4,
  274. 0x3ffe0000,0xd2a81d91,0xf12ae45a,
  275. 0x3ffe0000,0xce248c15,0x1f8480e4,
  276. 0x3ffe0000,0xc9b9bd86,0x6e2f27a3,
  277. 0x3ffe0000,0xc5672a11,0x5506dadd,
  278. 0x3ffe0000,0xc12c4cca,0x66709456,
  279. 0x3ffe0000,0xbd08a39f,0x580c36bf,
  280. 0x3ffe0000,0xb8fbaf47,0x62fb9ee9,
  281. 0x3ffe0000,0xb504f333,0xf9de6484,
  282. 0x3ffe0000,0xb123f581,0xd2ac2590,
  283. 0x3ffe0000,0xad583eea,0x42a14ac6,
  284. 0x3ffe0000,0xa9a15ab4,0xea7c0ef8,
  285. 0x3ffe0000,0xa5fed6a9,0xb15138ea,
  286. 0x3ffe0000,0xa2704303,0x0c496819,
  287. 0x3ffe0000,0x9ef53260,0x91a111ae,
  288. 0x3ffe0000,0x9b8d39b9,0xd54e5539,
  289. 0x3ffe0000,0x9837f051,0x8db8a96f,
  290. 0x3ffe0000,0x94f4efa8,0xfef70961,
  291. 0x3ffe0000,0x91c3d373,0xab11c336,
  292. 0x3ffe0000,0x8ea4398b,0x45cd53c0,
  293. 0x3ffe0000,0x8b95c1e3,0xea8bd6e7,
  294. 0x3ffe0000,0x88980e80,0x92da8527,
  295. 0x3ffe0000,0x85aac367,0xcc487b15,
  296. 0x3ffe0000,0x82cd8698,0xac2ba1d7,
  297. 0x3ffe0000,0x80000000,0x00000000,
  298. };
  299. static long B[51] = {
  300. 0x00000000,0x00000000,0x00000000,
  301. 0x3fbd0000,0xf73a18f5,0xdb301f87,
  302. 0xbfbc0000,0xbf4a2932,0x3e46ac15,
  303. 0x3fb90000,0xcb12a091,0xba667944,
  304. 0x3fbc0000,0xe69a2ee6,0x40b4ff78,
  305. 0xbfbb0000,0xee53e383,0x5069c895,
  306. 0x3fbc0000,0xf8ab4325,0x93767cde,
  307. 0xbfbd0000,0xaefdc093,0x25e0a10c,
  308. 0x3fbd0000,0xb2fb1366,0xea957d3e,
  309. 0x3fbd0000,0x93015191,0xeb345d89,
  310. 0x3fbb0000,0xe5ebfb10,0xb88380d9,
  311. 0xbfbd0000,0xbeddc1ec,0x288c045d,
  312. 0x3fbd0000,0x8d5a4630,0x5c85eded,
  313. 0x3fba0000,0xfd6d8e0a,0xe5ac9d82,
  314. 0xbfb90000,0x8373af14,0xeb586dfd,
  315. 0xbfbc0000,0xe8da91cf,0x7aacf938,
  316. 0x00000000,0x00000000,0x00000000,
  317. };
  318. static long R[] = {
  319. 0x3fee0000,0xfd2aee1d,0x530ea69b,
  320. 0x3ff20000,0xa1825960,0x8e7ec746,
  321. 0x3ff50000,0xaec3fd6a,0xadda63b6,
  322. 0x3ff80000,0x9d955b7c,0xfd99c104,
  323. 0x3ffa0000,0xe35846b8,0x249de05e,
  324. 0x3ffc0000,0xf5fdeffc,0x162c5d1d,
  325. 0x3ffe0000,0xb17217f7,0xd1cf79aa,
  326. };
  327. #define douba(k) (*(long double *)&A[3*(k)])
  328. #define doubb(k) (*(long double *)&B[3*(k)])
  329. #define MEXP (NXT*16384.0L)
  330. #ifdef DENORMAL
  331. #define MNEXP (-NXT*(16384.0L+64.0L))
  332. #else
  333. #define MNEXP (-NXT*16382.0L)
  334. #endif
  335. static long L[3] = {0x3ffd0000,0xe2a8eca5,0x705fc2ef};
  336. #define LOG2EA (*(long double *)(&L[0]))
  337. #endif
  338. #define F W
  339. #define Fa Wa
  340. #define Fb Wb
  341. #define G W
  342. #define Ga Wa
  343. #define Gb u
  344. #define H W
  345. #define Ha Wb
  346. #define Hb Wb
  347. extern long double MAXNUML;
  348. static VOLATILE long double z;
  349. static long double w, W, Wa, Wb, ya, yb, u;
  350. #ifdef ANSIPROT
  351. extern long double floorl ( long double );
  352. extern long double fabsl ( long double );
  353. extern long double frexpl ( long double, int * );
  354. extern long double ldexpl ( long double, int );
  355. extern long double polevll ( long double, void *, int );
  356. extern long double p1evll ( long double, void *, int );
  357. extern long double powil ( long double, int );
  358. extern int isnanl ( long double );
  359. extern int isfinitel ( long double );
  360. static long double reducl( long double );
  361. extern int signbitl ( long double );
  362. #else
  363. long double floorl(), fabsl(), frexpl(), ldexpl();
  364. long double polevll(), p1evll(), powil();
  365. static long double reducl();
  366. int isnanl(), isfinitel(), signbitl();
  367. #endif
  368. #ifdef INFINITIES
  369. extern long double INFINITYL;
  370. #else
  371. #define INFINITYL MAXNUML
  372. #endif
  373. #ifdef NANS
  374. extern long double NANL;
  375. #endif
  376. #ifdef MINUSZERO
  377. extern long double NEGZEROL;
  378. #endif
  379. long double powl( x, y )
  380. long double x, y;
  381. {
  382. /* double F, Fa, Fb, G, Ga, Gb, H, Ha, Hb */
  383. int i, nflg, iyflg, yoddint;
  384. long e;
  385. if( y == 0.0L )
  386. return( 1.0L );
  387. #ifdef NANS
  388. if( isnanl(x) )
  389. return( x );
  390. if( isnanl(y) )
  391. return( y );
  392. #endif
  393. if( y == 1.0L )
  394. return( x );
  395. #ifdef INFINITIES
  396. if( !isfinitel(y) && (x == -1.0L || x == 1.0L) )
  397. {
  398. mtherr( "powl", DOMAIN );
  399. #ifdef NANS
  400. return( NANL );
  401. #else
  402. return( INFINITYL );
  403. #endif
  404. }
  405. #endif
  406. if( x == 1.0L )
  407. return( 1.0L );
  408. if( y >= MAXNUML )
  409. {
  410. #ifdef INFINITIES
  411. if( x > 1.0L )
  412. return( INFINITYL );
  413. #else
  414. if( x > 1.0L )
  415. return( MAXNUML );
  416. #endif
  417. if( x > 0.0L && x < 1.0L )
  418. return( 0.0L );
  419. #ifdef INFINITIES
  420. if( x < -1.0L )
  421. return( INFINITYL );
  422. #else
  423. if( x < -1.0L )
  424. return( MAXNUML );
  425. #endif
  426. if( x > -1.0L && x < 0.0L )
  427. return( 0.0L );
  428. }
  429. if( y <= -MAXNUML )
  430. {
  431. if( x > 1.0L )
  432. return( 0.0L );
  433. #ifdef INFINITIES
  434. if( x > 0.0L && x < 1.0L )
  435. return( INFINITYL );
  436. #else
  437. if( x > 0.0L && x < 1.0L )
  438. return( MAXNUML );
  439. #endif
  440. if( x < -1.0L )
  441. return( 0.0L );
  442. #ifdef INFINITIES
  443. if( x > -1.0L && x < 0.0L )
  444. return( INFINITYL );
  445. #else
  446. if( x > -1.0L && x < 0.0L )
  447. return( MAXNUML );
  448. #endif
  449. }
  450. if( x >= MAXNUML )
  451. {
  452. #if INFINITIES
  453. if( y > 0.0L )
  454. return( INFINITYL );
  455. #else
  456. if( y > 0.0L )
  457. return( MAXNUML );
  458. #endif
  459. return( 0.0L );
  460. }
  461. w = floorl(y);
  462. /* Set iyflg to 1 if y is an integer. */
  463. iyflg = 0;
  464. if( w == y )
  465. iyflg = 1;
  466. /* Test for odd integer y. */
  467. yoddint = 0;
  468. if( iyflg )
  469. {
  470. ya = fabsl(y);
  471. ya = floorl(0.5L * ya);
  472. yb = 0.5L * fabsl(w);
  473. if( ya != yb )
  474. yoddint = 1;
  475. }
  476. if( x <= -MAXNUML )
  477. {
  478. if( y > 0.0L )
  479. {
  480. #ifdef INFINITIES
  481. if( yoddint )
  482. return( -INFINITYL );
  483. return( INFINITYL );
  484. #else
  485. if( yoddint )
  486. return( -MAXNUML );
  487. return( MAXNUML );
  488. #endif
  489. }
  490. if( y < 0.0L )
  491. {
  492. #ifdef MINUSZERO
  493. if( yoddint )
  494. return( NEGZEROL );
  495. #endif
  496. return( 0.0 );
  497. }
  498. }
  499. nflg = 0; /* flag = 1 if x<0 raised to integer power */
  500. if( x <= 0.0L )
  501. {
  502. if( x == 0.0L )
  503. {
  504. if( y < 0.0 )
  505. {
  506. #ifdef MINUSZERO
  507. if( signbitl(x) && yoddint )
  508. return( -INFINITYL );
  509. #endif
  510. #ifdef INFINITIES
  511. return( INFINITYL );
  512. #else
  513. return( MAXNUML );
  514. #endif
  515. }
  516. if( y > 0.0 )
  517. {
  518. #ifdef MINUSZERO
  519. if( signbitl(x) && yoddint )
  520. return( NEGZEROL );
  521. #endif
  522. return( 0.0 );
  523. }
  524. if( y == 0.0L )
  525. return( 1.0L ); /* 0**0 */
  526. else
  527. return( 0.0L ); /* 0**y */
  528. }
  529. else
  530. {
  531. if( iyflg == 0 )
  532. { /* noninteger power of negative number */
  533. mtherr( fname, DOMAIN );
  534. #ifdef NANS
  535. return(NANL);
  536. #else
  537. return(0.0L);
  538. #endif
  539. }
  540. nflg = 1;
  541. }
  542. }
  543. /* Integer power of an integer. */
  544. if( iyflg )
  545. {
  546. i = w;
  547. w = floorl(x);
  548. if( (w == x) && (fabsl(y) < 32768.0) )
  549. {
  550. w = powil( x, (int) y );
  551. return( w );
  552. }
  553. }
  554. if( nflg )
  555. x = fabsl(x);
  556. /* separate significand from exponent */
  557. x = frexpl( x, &i );
  558. e = i;
  559. /* find significand in antilog table A[] */
  560. i = 1;
  561. if( x <= douba(17) )
  562. i = 17;
  563. if( x <= douba(i+8) )
  564. i += 8;
  565. if( x <= douba(i+4) )
  566. i += 4;
  567. if( x <= douba(i+2) )
  568. i += 2;
  569. if( x >= douba(1) )
  570. i = -1;
  571. i += 1;
  572. /* Find (x - A[i])/A[i]
  573. * in order to compute log(x/A[i]):
  574. *
  575. * log(x) = log( a x/a ) = log(a) + log(x/a)
  576. *
  577. * log(x/a) = log(1+v), v = x/a - 1 = (x-a)/a
  578. */
  579. x -= douba(i);
  580. x -= doubb(i/2);
  581. x /= douba(i);
  582. /* rational approximation for log(1+v):
  583. *
  584. * log(1+v) = v - v**2/2 + v**3 P(v) / Q(v)
  585. */
  586. z = x*x;
  587. w = x * ( z * polevll( x, P, 3 ) / p1evll( x, Q, 3 ) );
  588. w = w - ldexpl( z, -1 ); /* w - 0.5 * z */
  589. /* Convert to base 2 logarithm:
  590. * multiply by log2(e) = 1 + LOG2EA
  591. */
  592. z = LOG2EA * w;
  593. z += w;
  594. z += LOG2EA * x;
  595. z += x;
  596. /* Compute exponent term of the base 2 logarithm. */
  597. w = -i;
  598. w = ldexpl( w, -LNXT ); /* divide by NXT */
  599. w += e;
  600. /* Now base 2 log of x is w + z. */
  601. /* Multiply base 2 log by y, in extended precision. */
  602. /* separate y into large part ya
  603. * and small part yb less than 1/NXT
  604. */
  605. ya = reducl(y);
  606. yb = y - ya;
  607. /* (w+z)(ya+yb)
  608. * = w*ya + w*yb + z*y
  609. */
  610. F = z * y + w * yb;
  611. Fa = reducl(F);
  612. Fb = F - Fa;
  613. G = Fa + w * ya;
  614. Ga = reducl(G);
  615. Gb = G - Ga;
  616. H = Fb + Gb;
  617. Ha = reducl(H);
  618. w = ldexpl( Ga+Ha, LNXT );
  619. /* Test the power of 2 for overflow */
  620. if( w > MEXP )
  621. {
  622. /* printf( "w = %.4Le ", w ); */
  623. mtherr( fname, OVERFLOW );
  624. return( MAXNUML );
  625. }
  626. if( w < MNEXP )
  627. {
  628. /* printf( "w = %.4Le ", w ); */
  629. mtherr( fname, UNDERFLOW );
  630. return( 0.0L );
  631. }
  632. e = w;
  633. Hb = H - Ha;
  634. if( Hb > 0.0L )
  635. {
  636. e += 1;
  637. Hb -= (1.0L/NXT); /*0.0625L;*/
  638. }
  639. /* Now the product y * log2(x) = Hb + e/NXT.
  640. *
  641. * Compute base 2 exponential of Hb,
  642. * where -0.0625 <= Hb <= 0.
  643. */
  644. z = Hb * polevll( Hb, R, 6 ); /* z = 2**Hb - 1 */
  645. /* Express e/NXT as an integer plus a negative number of (1/NXT)ths.
  646. * Find lookup table entry for the fractional power of 2.
  647. */
  648. if( e < 0 )
  649. i = 0;
  650. else
  651. i = 1;
  652. i = e/NXT + i;
  653. e = NXT*i - e;
  654. w = douba( e );
  655. z = w * z; /* 2**-e * ( 1 + (2**Hb-1) ) */
  656. z = z + w;
  657. z = ldexpl( z, i ); /* multiply by integer power of 2 */
  658. if( nflg )
  659. {
  660. /* For negative x,
  661. * find out if the integer exponent
  662. * is odd or even.
  663. */
  664. w = ldexpl( y, -1 );
  665. w = floorl(w);
  666. w = ldexpl( w, 1 );
  667. if( w != y )
  668. z = -z; /* odd exponent */
  669. }
  670. return( z );
  671. }
  672. /* Find a multiple of 1/NXT that is within 1/NXT of x. */
  673. static long double reducl(x)
  674. long double x;
  675. {
  676. long double t;
  677. t = ldexpl( x, LNXT );
  678. t = floorl( t );
  679. t = ldexpl( t, -LNXT );
  680. return(t);
  681. }