Subversion Repositories Kolibri OS

Rev

Go to most recent revision | Blame | Last modification | View Log | Download | RSS feed

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