pow.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756
  1. /* pow.c
  2. *
  3. * Power function
  4. *
  5. *
  6. *
  7. * SYNOPSIS:
  8. *
  9. * double x, y, z, pow();
  10. *
  11. * z = pow( 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/16 and pseudo extended precision arithmetic to
  23. * obtain an extra three bits of accuracy in both the logarithm
  24. * and the exponential.
  25. *
  26. *
  27. *
  28. * ACCURACY:
  29. *
  30. * Relative error:
  31. * arithmetic domain # trials peak rms
  32. * IEEE -26,26 30000 4.2e-16 7.7e-17
  33. * DEC -26,26 60000 4.8e-17 9.1e-18
  34. * 1/26 < x < 26, with log(x) uniformly distributed.
  35. * -26 < y < 26, y uniformly distributed.
  36. * IEEE 0,8700 30000 1.5e-14 2.1e-15
  37. * 0.99 < x < 1.01, 0 < y < 8700, uniformly distributed.
  38. *
  39. *
  40. * ERROR MESSAGES:
  41. *
  42. * message condition value returned
  43. * pow overflow x**y > MAXNUM INFINITY
  44. * pow underflow x**y < 1/MAXNUM 0.0
  45. * pow domain x<0 and y noninteger 0.0
  46. *
  47. */
  48. /*
  49. Cephes Math Library Release 2.8: June, 2000
  50. Copyright 1984, 1995, 2000 by Stephen L. Moshier
  51. */
  52. #include <math.h>
  53. static char fname[] = {"pow"};
  54. #define SQRTH 0.70710678118654752440
  55. #ifdef UNK
  56. static double P[] = {
  57. 4.97778295871696322025E-1,
  58. 3.73336776063286838734E0,
  59. 7.69994162726912503298E0,
  60. 4.66651806774358464979E0
  61. };
  62. static double Q[] = {
  63. /* 1.00000000000000000000E0, */
  64. 9.33340916416696166113E0,
  65. 2.79999886606328401649E1,
  66. 3.35994905342304405431E1,
  67. 1.39995542032307539578E1
  68. };
  69. /* 2^(-i/16), IEEE precision */
  70. static double A[] = {
  71. 1.00000000000000000000E0,
  72. 9.57603280698573700036E-1,
  73. 9.17004043204671215328E-1,
  74. 8.78126080186649726755E-1,
  75. 8.40896415253714502036E-1,
  76. 8.05245165974627141736E-1,
  77. 7.71105412703970372057E-1,
  78. 7.38413072969749673113E-1,
  79. 7.07106781186547572737E-1,
  80. 6.77127773468446325644E-1,
  81. 6.48419777325504820276E-1,
  82. 6.20928906036742001007E-1,
  83. 5.94603557501360513449E-1,
  84. 5.69394317378345782288E-1,
  85. 5.45253866332628844837E-1,
  86. 5.22136891213706877402E-1,
  87. 5.00000000000000000000E-1
  88. };
  89. static double B[] = {
  90. 0.00000000000000000000E0,
  91. 1.64155361212281360176E-17,
  92. 4.09950501029074826006E-17,
  93. 3.97491740484881042808E-17,
  94. -4.83364665672645672553E-17,
  95. 1.26912513974441574796E-17,
  96. 1.99100761573282305549E-17,
  97. -1.52339103990623557348E-17,
  98. 0.00000000000000000000E0
  99. };
  100. static double R[] = {
  101. 1.49664108433729301083E-5,
  102. 1.54010762792771901396E-4,
  103. 1.33335476964097721140E-3,
  104. 9.61812908476554225149E-3,
  105. 5.55041086645832347466E-2,
  106. 2.40226506959099779976E-1,
  107. 6.93147180559945308821E-1
  108. };
  109. #define douba(k) A[k]
  110. #define doubb(k) B[k]
  111. #define MEXP 16383.0
  112. #ifdef DENORMAL
  113. #define MNEXP -17183.0
  114. #else
  115. #define MNEXP -16383.0
  116. #endif
  117. #endif
  118. #ifdef DEC
  119. static unsigned short P[] = {
  120. 0037776,0156313,0175332,0163602,
  121. 0040556,0167577,0052366,0174245,
  122. 0040766,0062753,0175707,0055564,
  123. 0040625,0052035,0131344,0155636,
  124. };
  125. static unsigned short Q[] = {
  126. /*0040200,0000000,0000000,0000000,*/
  127. 0041025,0052644,0154404,0105155,
  128. 0041337,0177772,0007016,0047646,
  129. 0041406,0062740,0154273,0020020,
  130. 0041137,0177054,0106127,0044555,
  131. };
  132. static unsigned short A[] = {
  133. 0040200,0000000,0000000,0000000,
  134. 0040165,0022575,0012444,0103314,
  135. 0040152,0140306,0163735,0022071,
  136. 0040140,0146336,0166052,0112341,
  137. 0040127,0042374,0145326,0116553,
  138. 0040116,0022214,0012437,0102201,
  139. 0040105,0063452,0010525,0003333,
  140. 0040075,0004243,0117530,0006067,
  141. 0040065,0002363,0031771,0157145,
  142. 0040055,0054076,0165102,0120513,
  143. 0040045,0177326,0124661,0050471,
  144. 0040036,0172462,0060221,0120422,
  145. 0040030,0033760,0050615,0134251,
  146. 0040021,0141723,0071653,0010703,
  147. 0040013,0112701,0161752,0105727,
  148. 0040005,0125303,0063714,0044173,
  149. 0040000,0000000,0000000,0000000
  150. };
  151. static unsigned short B[] = {
  152. 0000000,0000000,0000000,0000000,
  153. 0021473,0040265,0153315,0140671,
  154. 0121074,0062627,0042146,0176454,
  155. 0121413,0003524,0136332,0066212,
  156. 0121767,0046404,0166231,0012553,
  157. 0121257,0015024,0002357,0043574,
  158. 0021736,0106532,0043060,0056206,
  159. 0121310,0020334,0165705,0035326,
  160. 0000000,0000000,0000000,0000000
  161. };
  162. static unsigned short R[] = {
  163. 0034173,0014076,0137624,0115771,
  164. 0035041,0076763,0003744,0111311,
  165. 0035656,0141766,0041127,0074351,
  166. 0036435,0112533,0073611,0116664,
  167. 0037143,0054106,0134040,0152223,
  168. 0037565,0176757,0176026,0025551,
  169. 0040061,0071027,0173721,0147572
  170. };
  171. /*
  172. static double R[] = {
  173. 0.14928852680595608186e-4,
  174. 0.15400290440989764601e-3,
  175. 0.13333541313585784703e-2,
  176. 0.96181290595172416964e-2,
  177. 0.55504108664085595326e-1,
  178. 0.24022650695909537056e0,
  179. 0.69314718055994529629e0
  180. };
  181. */
  182. #define douba(k) (*(double *)&A[(k)<<2])
  183. #define doubb(k) (*(double *)&B[(k)<<2])
  184. #define MEXP 2031.0
  185. #define MNEXP -2031.0
  186. #endif
  187. #ifdef IBMPC
  188. static unsigned short P[] = {
  189. 0x5cf0,0x7f5b,0xdb99,0x3fdf,
  190. 0xdf15,0xea9e,0xddef,0x400d,
  191. 0xeb6f,0x7f78,0xccbd,0x401e,
  192. 0x9b74,0xb65c,0xaa83,0x4012,
  193. };
  194. static unsigned short Q[] = {
  195. /*0x0000,0x0000,0x0000,0x3ff0,*/
  196. 0x914e,0x9b20,0xaab4,0x4022,
  197. 0xc9f5,0x41c1,0xffff,0x403b,
  198. 0x6402,0x1b17,0xccbc,0x4040,
  199. 0xe92e,0x918a,0xffc5,0x402b,
  200. };
  201. static unsigned short A[] = {
  202. 0x0000,0x0000,0x0000,0x3ff0,
  203. 0x90da,0xa2a4,0xa4af,0x3fee,
  204. 0xa487,0xdcfb,0x5818,0x3fed,
  205. 0x529c,0xdd85,0x199b,0x3fec,
  206. 0xd3ad,0x995a,0xe89f,0x3fea,
  207. 0xf090,0x82a3,0xc491,0x3fe9,
  208. 0xa0db,0x422a,0xace5,0x3fe8,
  209. 0x0187,0x73eb,0xa114,0x3fe7,
  210. 0x3bcd,0x667f,0xa09e,0x3fe6,
  211. 0x5429,0xdd48,0xab07,0x3fe5,
  212. 0x2a27,0xd536,0xbfda,0x3fe4,
  213. 0x3422,0x4c12,0xdea6,0x3fe3,
  214. 0xb715,0x0a31,0x06fe,0x3fe3,
  215. 0x6238,0x6e75,0x387a,0x3fe2,
  216. 0x517b,0x3c7d,0x72b8,0x3fe1,
  217. 0x890f,0x6cf9,0xb558,0x3fe0,
  218. 0x0000,0x0000,0x0000,0x3fe0
  219. };
  220. static unsigned short B[] = {
  221. 0x0000,0x0000,0x0000,0x0000,
  222. 0x3707,0xd75b,0xed02,0x3c72,
  223. 0xcc81,0x345d,0xa1cd,0x3c87,
  224. 0x4b27,0x5686,0xe9f1,0x3c86,
  225. 0x6456,0x13b2,0xdd34,0xbc8b,
  226. 0x42e2,0xafec,0x4397,0x3c6d,
  227. 0x82e4,0xd231,0xf46a,0x3c76,
  228. 0x8a76,0xb9d7,0x9041,0xbc71,
  229. 0x0000,0x0000,0x0000,0x0000
  230. };
  231. static unsigned short R[] = {
  232. 0x937f,0xd7f2,0x6307,0x3eef,
  233. 0x9259,0x60fc,0x2fbe,0x3f24,
  234. 0xef1d,0xc84a,0xd87e,0x3f55,
  235. 0x33b7,0x6ef1,0xb2ab,0x3f83,
  236. 0x1a92,0xd704,0x6b08,0x3fac,
  237. 0xc56d,0xff82,0xbfbd,0x3fce,
  238. 0x39ef,0xfefa,0x2e42,0x3fe6
  239. };
  240. #define douba(k) (*(double *)&A[(k)<<2])
  241. #define doubb(k) (*(double *)&B[(k)<<2])
  242. #define MEXP 16383.0
  243. #ifdef DENORMAL
  244. #define MNEXP -17183.0
  245. #else
  246. #define MNEXP -16383.0
  247. #endif
  248. #endif
  249. #ifdef MIEEE
  250. static unsigned short P[] = {
  251. 0x3fdf,0xdb99,0x7f5b,0x5cf0,
  252. 0x400d,0xddef,0xea9e,0xdf15,
  253. 0x401e,0xccbd,0x7f78,0xeb6f,
  254. 0x4012,0xaa83,0xb65c,0x9b74
  255. };
  256. static unsigned short Q[] = {
  257. 0x4022,0xaab4,0x9b20,0x914e,
  258. 0x403b,0xffff,0x41c1,0xc9f5,
  259. 0x4040,0xccbc,0x1b17,0x6402,
  260. 0x402b,0xffc5,0x918a,0xe92e
  261. };
  262. static unsigned short A[] = {
  263. 0x3ff0,0x0000,0x0000,0x0000,
  264. 0x3fee,0xa4af,0xa2a4,0x90da,
  265. 0x3fed,0x5818,0xdcfb,0xa487,
  266. 0x3fec,0x199b,0xdd85,0x529c,
  267. 0x3fea,0xe89f,0x995a,0xd3ad,
  268. 0x3fe9,0xc491,0x82a3,0xf090,
  269. 0x3fe8,0xace5,0x422a,0xa0db,
  270. 0x3fe7,0xa114,0x73eb,0x0187,
  271. 0x3fe6,0xa09e,0x667f,0x3bcd,
  272. 0x3fe5,0xab07,0xdd48,0x5429,
  273. 0x3fe4,0xbfda,0xd536,0x2a27,
  274. 0x3fe3,0xdea6,0x4c12,0x3422,
  275. 0x3fe3,0x06fe,0x0a31,0xb715,
  276. 0x3fe2,0x387a,0x6e75,0x6238,
  277. 0x3fe1,0x72b8,0x3c7d,0x517b,
  278. 0x3fe0,0xb558,0x6cf9,0x890f,
  279. 0x3fe0,0x0000,0x0000,0x0000
  280. };
  281. static unsigned short B[] = {
  282. 0x0000,0x0000,0x0000,0x0000,
  283. 0x3c72,0xed02,0xd75b,0x3707,
  284. 0x3c87,0xa1cd,0x345d,0xcc81,
  285. 0x3c86,0xe9f1,0x5686,0x4b27,
  286. 0xbc8b,0xdd34,0x13b2,0x6456,
  287. 0x3c6d,0x4397,0xafec,0x42e2,
  288. 0x3c76,0xf46a,0xd231,0x82e4,
  289. 0xbc71,0x9041,0xb9d7,0x8a76,
  290. 0x0000,0x0000,0x0000,0x0000
  291. };
  292. static unsigned short R[] = {
  293. 0x3eef,0x6307,0xd7f2,0x937f,
  294. 0x3f24,0x2fbe,0x60fc,0x9259,
  295. 0x3f55,0xd87e,0xc84a,0xef1d,
  296. 0x3f83,0xb2ab,0x6ef1,0x33b7,
  297. 0x3fac,0x6b08,0xd704,0x1a92,
  298. 0x3fce,0xbfbd,0xff82,0xc56d,
  299. 0x3fe6,0x2e42,0xfefa,0x39ef
  300. };
  301. #define douba(k) (*(double *)&A[(k)<<2])
  302. #define doubb(k) (*(double *)&B[(k)<<2])
  303. #define MEXP 16383.0
  304. #ifdef DENORMAL
  305. #define MNEXP -17183.0
  306. #else
  307. #define MNEXP -16383.0
  308. #endif
  309. #endif
  310. /* log2(e) - 1 */
  311. #define LOG2EA 0.44269504088896340736
  312. #define F W
  313. #define Fa Wa
  314. #define Fb Wb
  315. #define G W
  316. #define Ga Wa
  317. #define Gb u
  318. #define H W
  319. #define Ha Wb
  320. #define Hb Wb
  321. #ifdef ANSIPROT
  322. extern double floor ( double );
  323. extern double fabs ( double );
  324. extern double frexp ( double, int * );
  325. extern double ldexp ( double, int );
  326. extern double polevl ( double, void *, int );
  327. extern double p1evl ( double, void *, int );
  328. extern double powi ( double, int );
  329. extern int signbit ( double );
  330. extern int isnan ( double );
  331. extern int isfinite ( double );
  332. static double reduc ( double );
  333. #else
  334. double floor(), fabs(), frexp(), ldexp();
  335. double polevl(), p1evl(), powi();
  336. int signbit(), isnan(), isfinite();
  337. static double reduc();
  338. #endif
  339. extern double MAXNUM;
  340. #ifdef INFINITIES
  341. extern double INFINITY;
  342. #endif
  343. #ifdef NANS
  344. extern double NAN;
  345. #endif
  346. #ifdef MINUSZERO
  347. extern double NEGZERO;
  348. #endif
  349. double pow( x, y )
  350. double x, y;
  351. {
  352. double w, z, W, Wa, Wb, ya, yb, u;
  353. /* double F, Fa, Fb, G, Ga, Gb, H, Ha, Hb */
  354. double aw, ay, wy;
  355. int e, i, nflg, iyflg, yoddint;
  356. if( y == 0.0 )
  357. return( 1.0 );
  358. #ifdef NANS
  359. if( isnan(x) )
  360. return( x );
  361. if( isnan(y) )
  362. return( y );
  363. #endif
  364. if( y == 1.0 )
  365. return( x );
  366. #ifdef INFINITIES
  367. if( !isfinite(y) && (x == 1.0 || x == -1.0) )
  368. {
  369. mtherr( "pow", DOMAIN );
  370. #ifdef NANS
  371. return( NAN );
  372. #else
  373. return( INFINITY );
  374. #endif
  375. }
  376. #endif
  377. if( x == 1.0 )
  378. return( 1.0 );
  379. if( y >= MAXNUM )
  380. {
  381. #ifdef INFINITIES
  382. if( x > 1.0 )
  383. return( INFINITY );
  384. #else
  385. if( x > 1.0 )
  386. return( MAXNUM );
  387. #endif
  388. if( x > 0.0 && x < 1.0 )
  389. return( 0.0);
  390. if( x < -1.0 )
  391. {
  392. #ifdef INFINITIES
  393. return( INFINITY );
  394. #else
  395. return( MAXNUM );
  396. #endif
  397. }
  398. if( x > -1.0 && x < 0.0 )
  399. return( 0.0 );
  400. }
  401. if( y <= -MAXNUM )
  402. {
  403. if( x > 1.0 )
  404. return( 0.0 );
  405. #ifdef INFINITIES
  406. if( x > 0.0 && x < 1.0 )
  407. return( INFINITY );
  408. #else
  409. if( x > 0.0 && x < 1.0 )
  410. return( MAXNUM );
  411. #endif
  412. if( x < -1.0 )
  413. return( 0.0 );
  414. #ifdef INFINITIES
  415. if( x > -1.0 && x < 0.0 )
  416. return( INFINITY );
  417. #else
  418. if( x > -1.0 && x < 0.0 )
  419. return( MAXNUM );
  420. #endif
  421. }
  422. if( x >= MAXNUM )
  423. {
  424. #if INFINITIES
  425. if( y > 0.0 )
  426. return( INFINITY );
  427. #else
  428. if( y > 0.0 )
  429. return( MAXNUM );
  430. #endif
  431. return(0.0);
  432. }
  433. /* Set iyflg to 1 if y is an integer. */
  434. iyflg = 0;
  435. w = floor(y);
  436. if( w == y )
  437. iyflg = 1;
  438. /* Test for odd integer y. */
  439. yoddint = 0;
  440. if( iyflg )
  441. {
  442. ya = fabs(y);
  443. ya = floor(0.5 * ya);
  444. yb = 0.5 * fabs(w);
  445. if( ya != yb )
  446. yoddint = 1;
  447. }
  448. if( x <= -MAXNUM )
  449. {
  450. if( y > 0.0 )
  451. {
  452. #ifdef INFINITIES
  453. if( yoddint )
  454. return( -INFINITY );
  455. return( INFINITY );
  456. #else
  457. if( yoddint )
  458. return( -MAXNUM );
  459. return( MAXNUM );
  460. #endif
  461. }
  462. if( y < 0.0 )
  463. {
  464. #ifdef MINUSZERO
  465. if( yoddint )
  466. return( NEGZERO );
  467. #endif
  468. return( 0.0 );
  469. }
  470. }
  471. nflg = 0; /* flag = 1 if x<0 raised to integer power */
  472. if( x <= 0.0 )
  473. {
  474. if( x == 0.0 )
  475. {
  476. if( y < 0.0 )
  477. {
  478. #ifdef MINUSZERO
  479. if( signbit(x) && yoddint )
  480. return( -INFINITY );
  481. #endif
  482. #ifdef INFINITIES
  483. return( INFINITY );
  484. #else
  485. return( MAXNUM );
  486. #endif
  487. }
  488. if( y > 0.0 )
  489. {
  490. #ifdef MINUSZERO
  491. if( signbit(x) && yoddint )
  492. return( NEGZERO );
  493. #endif
  494. return( 0.0 );
  495. }
  496. return( 1.0 );
  497. }
  498. else
  499. {
  500. if( iyflg == 0 )
  501. { /* noninteger power of negative number */
  502. mtherr( fname, DOMAIN );
  503. #ifdef NANS
  504. return(NAN);
  505. #else
  506. return(0.0L);
  507. #endif
  508. }
  509. nflg = 1;
  510. }
  511. }
  512. /* Integer power of an integer. */
  513. if( iyflg )
  514. {
  515. i = w;
  516. w = floor(x);
  517. if( (w == x) && (fabs(y) < 32768.0) )
  518. {
  519. w = powi( x, (int) y );
  520. return( w );
  521. }
  522. }
  523. if( nflg )
  524. x = fabs(x);
  525. /* For results close to 1, use a series expansion. */
  526. w = x - 1.0;
  527. aw = fabs(w);
  528. ay = fabs(y);
  529. wy = w * y;
  530. ya = fabs(wy);
  531. if((aw <= 1.0e-3 && ay <= 1.0)
  532. || (ya <= 1.0e-3 && ay >= 1.0))
  533. {
  534. z = (((((w*(y-5.)/720. + 1./120.)*w*(y-4.) + 1./24.)*w*(y-3.)
  535. + 1./6.)*w*(y-2.) + 0.5)*w*(y-1.) )*wy + wy + 1.;
  536. goto done;
  537. }
  538. /* These are probably too much trouble. */
  539. #if 0
  540. w = y * log(x);
  541. if (aw > 1.0e-3 && fabs(w) < 1.0e-3)
  542. {
  543. z = ((((((
  544. w/7. + 1.)*w/6. + 1.)*w/5. + 1.)*w/4. + 1.)*w/3. + 1.)*w/2. + 1.)*w + 1.;
  545. goto done;
  546. }
  547. if(ya <= 1.0e-3 && aw <= 1.0e-4)
  548. {
  549. z = (((((
  550. wy*1./720.
  551. + (-w*1./48. + 1./120.) )*wy
  552. + ((w*17./144. - 1./12.)*w + 1./24.) )*wy
  553. + (((-w*5./16. + 7./24.)*w - 1./4.)*w + 1./6.) )*wy
  554. + ((((w*137./360. - 5./12.)*w + 11./24.)*w - 1./2.)*w + 1./2.) )*wy
  555. + (((((-w*1./6. + 1./5.)*w - 1./4)*w + 1./3.)*w -1./2.)*w ) )*wy
  556. + wy + 1.0;
  557. goto done;
  558. }
  559. #endif
  560. /* separate significand from exponent */
  561. x = frexp( x, &e );
  562. #if 0
  563. /* For debugging, check for gross overflow. */
  564. if( (e * y) > (MEXP + 1024) )
  565. goto overflow;
  566. #endif
  567. /* Find significand of x in antilog table A[]. */
  568. i = 1;
  569. if( x <= douba(9) )
  570. i = 9;
  571. if( x <= douba(i+4) )
  572. i += 4;
  573. if( x <= douba(i+2) )
  574. i += 2;
  575. if( x >= douba(1) )
  576. i = -1;
  577. i += 1;
  578. /* Find (x - A[i])/A[i]
  579. * in order to compute log(x/A[i]):
  580. *
  581. * log(x) = log( a x/a ) = log(a) + log(x/a)
  582. *
  583. * log(x/a) = log(1+v), v = x/a - 1 = (x-a)/a
  584. */
  585. x -= douba(i);
  586. x -= doubb(i/2);
  587. x /= douba(i);
  588. /* rational approximation for log(1+v):
  589. *
  590. * log(1+v) = v - v**2/2 + v**3 P(v) / Q(v)
  591. */
  592. z = x*x;
  593. w = x * ( z * polevl( x, P, 3 ) / p1evl( x, Q, 4 ) );
  594. w = w - ldexp( z, -1 ); /* w - 0.5 * z */
  595. /* Convert to base 2 logarithm:
  596. * multiply by log2(e)
  597. */
  598. w = w + LOG2EA * w;
  599. /* Note x was not yet added in
  600. * to above rational approximation,
  601. * so do it now, while multiplying
  602. * by log2(e).
  603. */
  604. z = w + LOG2EA * x;
  605. z = z + x;
  606. /* Compute exponent term of the base 2 logarithm. */
  607. w = -i;
  608. w = ldexp( w, -4 ); /* divide by 16 */
  609. w += e;
  610. /* Now base 2 log of x is w + z. */
  611. /* Multiply base 2 log by y, in extended precision. */
  612. /* separate y into large part ya
  613. * and small part yb less than 1/16
  614. */
  615. ya = reduc(y);
  616. yb = y - ya;
  617. F = z * y + w * yb;
  618. Fa = reduc(F);
  619. Fb = F - Fa;
  620. G = Fa + w * ya;
  621. Ga = reduc(G);
  622. Gb = G - Ga;
  623. H = Fb + Gb;
  624. Ha = reduc(H);
  625. w = ldexp( Ga+Ha, 4 );
  626. /* Test the power of 2 for overflow */
  627. if( w > MEXP )
  628. {
  629. #ifndef INFINITIES
  630. mtherr( fname, OVERFLOW );
  631. #endif
  632. #ifdef INFINITIES
  633. if( nflg && yoddint )
  634. return( -INFINITY );
  635. return( INFINITY );
  636. #else
  637. if( nflg && yoddint )
  638. return( -MAXNUM );
  639. return( MAXNUM );
  640. #endif
  641. }
  642. if( w < (MNEXP - 1) )
  643. {
  644. #ifndef DENORMAL
  645. mtherr( fname, UNDERFLOW );
  646. #endif
  647. #ifdef MINUSZERO
  648. if( nflg && yoddint )
  649. return( NEGZERO );
  650. #endif
  651. return( 0.0 );
  652. }
  653. e = w;
  654. Hb = H - Ha;
  655. if( Hb > 0.0 )
  656. {
  657. e += 1;
  658. Hb -= 0.0625;
  659. }
  660. /* Now the product y * log2(x) = Hb + e/16.0.
  661. *
  662. * Compute base 2 exponential of Hb,
  663. * where -0.0625 <= Hb <= 0.
  664. */
  665. z = Hb * polevl( Hb, R, 6 ); /* z = 2**Hb - 1 */
  666. /* Express e/16 as an integer plus a negative number of 16ths.
  667. * Find lookup table entry for the fractional power of 2.
  668. */
  669. if( e < 0 )
  670. i = 0;
  671. else
  672. i = 1;
  673. i = e/16 + i;
  674. e = 16*i - e;
  675. w = douba( e );
  676. z = w + w * z; /* 2**-e * ( 1 + (2**Hb-1) ) */
  677. z = ldexp( z, i ); /* multiply by integer power of 2 */
  678. done:
  679. /* Negate if odd integer power of negative number */
  680. if( nflg && yoddint )
  681. {
  682. #ifdef MINUSZERO
  683. if( z == 0.0 )
  684. z = NEGZERO;
  685. else
  686. #endif
  687. z = -z;
  688. }
  689. return( z );
  690. }
  691. /* Find a multiple of 1/16 that is within 1/16 of x. */
  692. static double reduc(x)
  693. double x;
  694. {
  695. double t;
  696. t = ldexp( x, 4 );
  697. t = floor( t );
  698. t = ldexp( t, -4 );
  699. return(t);
  700. }