home *** CD-ROM | disk | FTP | other *** search
/ Simtel MSDOS 1992 June / SIMTEL_0692.cdr / msdos / c / cephes.arc / POW.C < prev    next >
Encoding:
C/C++ Source or Header  |  1987-06-20  |  12.0 KB  |  505 lines

  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   range      # trials      peak         rms
  32.  *    IEEE     -26,26       10000      3.6e-16      7.7e-17
  33.  *    DEC      -26,26       18000      4.4e-17      8.9e-18
  34.  * 1/26 < x < 26, with log(x) uniformly distributed.
  35.  * -26 < y < 26, y uniformly distributed.
  36.  *    IEEE     0,8700        1000      9.9e-15      2.0e-15
  37.  *    DEC      0,8700        1000      1.3e-15      2.1e-16
  38.  * 0.99 < x < 1.01, 0 < y < 8700, uniformly distributed.
  39.  *
  40.  *
  41.  * ERROR MESSAGES:
  42.  *
  43.  *   message         condition      value returned
  44.  * pow overflow     x**y > MAXNUM      MAXNUM
  45.  * pow underflow   x**y < 1/MAXNUM       0.0
  46.  * pow domain      x<0 and y noninteger  0.0
  47.  *
  48.  */
  49.  
  50. /*
  51. Cephes Math Library Release 2.0:  April, 1987
  52. Copyright 1984, 1987 by Stephen L. Moshier
  53. Direct inquiries to 30 Frost Street, Cambridge, MA 02140
  54. */
  55.  
  56. #include "mconf.h"
  57. static char fname[] = {"pow"};
  58.  
  59. #define SQRTH 0.70710678118654752440
  60.  
  61. #ifdef UNK
  62. static double P[] = {
  63.   4.97778295871696322025E-1,
  64.   3.73336776063286838734E0,
  65.   7.69994162726912503298E0,
  66.   4.66651806774358464979E0
  67. };
  68. static double Q[] = {
  69. /* 1.00000000000000000000E0, */
  70.   9.33340916416696166113E0,
  71.   2.79999886606328401649E1,
  72.   3.35994905342304405431E1,
  73.   1.39995542032307539578E1
  74. };
  75. /* 2^(-i/16), IEEE precision */
  76. static double A[] = {
  77.   1.00000000000000000000E0,
  78.   9.57603280698573700036E-1,
  79.   9.17004043204671215328E-1,
  80.   8.78126080186649726755E-1,
  81.   8.40896415253714502036E-1,
  82.   8.05245165974627141736E-1,
  83.   7.71105412703970372057E-1,
  84.   7.38413072969749673113E-1,
  85.   7.07106781186547572737E-1,
  86.   6.77127773468446325644E-1,
  87.   6.48419777325504820276E-1,
  88.   6.20928906036742001007E-1,
  89.   5.94603557501360513449E-1,
  90.   5.69394317378345782288E-1,
  91.   5.45253866332628844837E-1,
  92.   5.22136891213706877402E-1,
  93.   5.00000000000000000000E-1
  94. };
  95. static double B[] = {
  96.  0.00000000000000000000E0,
  97.  1.64155361212281360176E-17,
  98.  4.09950501029074826006E-17,
  99.  3.97491740484881042808E-17,
  100. -4.83364665672645672553E-17,
  101.  1.26912513974441574796E-17,
  102.  1.99100761573282305549E-17,
  103. -1.52339103990623557348E-17,
  104.  0.00000000000000000000E0
  105. };
  106. static double R[] = {
  107.  1.49664108433729301083E-5,
  108.  1.54010762792771901396E-4,
  109.  1.33335476964097721140E-3,
  110.  9.61812908476554225149E-3,
  111.  5.55041086645832347466E-2,
  112.  2.40226506959099779976E-1,
  113.  6.93147180559945308821E-1
  114. };
  115.  
  116. #define douba(k) A[k]
  117. #define doubb(k) B[k]
  118. #define MEXP 2031.0
  119. #endif
  120.  
  121. #ifdef DEC
  122. static short P[] = {
  123. 0037776,0156313,0175332,0163602,
  124. 0040556,0167577,0052366,0174245,
  125. 0040766,0062753,0175707,0055564,
  126. 0040625,0052035,0131344,0155636,
  127. };
  128. static short Q[] = {
  129. /*0040200,0000000,0000000,0000000,*/
  130. 0041025,0052644,0154404,0105155,
  131. 0041337,0177772,0007016,0047646,
  132. 0041406,0062740,0154273,0020020,
  133. 0041137,0177054,0106127,0044555,
  134. };
  135. static short A[] = {
  136. 0040200,0000000,0000000,0000000,
  137. 0040165,0022575,0012444,0103314,
  138. 0040152,0140306,0163735,0022071,
  139. 0040140,0146336,0166052,0112341,
  140. 0040127,0042374,0145326,0116553,
  141. 0040116,0022214,0012437,0102201,
  142. 0040105,0063452,0010525,0003333,
  143. 0040075,0004243,0117530,0006067,
  144. 0040065,0002363,0031771,0157145,
  145. 0040055,0054076,0165102,0120513,
  146. 0040045,0177326,0124661,0050471,
  147. 0040036,0172462,0060221,0120422,
  148. 0040030,0033760,0050615,0134251,
  149. 0040021,0141723,0071653,0010703,
  150. 0040013,0112701,0161752,0105727,
  151. 0040005,0125303,0063714,0044173,
  152. 0040000,0000000,0000000,0000000
  153. };
  154. static short B[] = {
  155. 0000000,0000000,0000000,0000000,
  156. 0021473,0040265,0153315,0140671,
  157. 0121074,0062627,0042146,0176454,
  158. 0121413,0003524,0136332,0066212,
  159. 0121767,0046404,0166231,0012553,
  160. 0121257,0015024,0002357,0043574,
  161. 0021736,0106532,0043060,0056206,
  162. 0121310,0020334,0165705,0035326,
  163. 0000000,0000000,0000000,0000000
  164. };
  165.  
  166. static short R[] = {
  167. 0034173,0014076,0137624,0115771,
  168. 0035041,0076763,0003744,0111311,
  169. 0035656,0141766,0041127,0074351,
  170. 0036435,0112533,0073611,0116664,
  171. 0037143,0054106,0134040,0152223,
  172. 0037565,0176757,0176026,0025551,
  173. 0040061,0071027,0173721,0147572
  174. };
  175.  
  176. /*
  177. static double R[] = {
  178. 0.14928852680595608186e-4,
  179. 0.15400290440989764601e-3,
  180. 0.13333541313585784703e-2,
  181. 0.96181290595172416964e-2,
  182. 0.55504108664085595326e-1,
  183. 0.24022650695909537056e0,
  184. 0.69314718055994529629e0
  185. };
  186. */
  187. #define douba(k) (*(double *)&A[(k)<<2])
  188. #define doubb(k) (*(double *)&B[(k)<<2])
  189. #define MEXP 2031.0
  190. #endif
  191.  
  192. #ifdef IBMPC
  193. static short P[] = {
  194. 0x5cf0,0x7f5b,0xdb99,0x3fdf,
  195. 0xdf15,0xea9e,0xddef,0x400d,
  196. 0xeb6f,0x7f78,0xccbd,0x401e,
  197. 0x9b74,0xb65c,0xaa83,0x4012,
  198. };
  199. static short Q[] = {
  200. /*0x0000,0x0000,0x0000,0x3ff0,*/
  201. 0x914e,0x9b20,0xaab4,0x4022,
  202. 0xc9f5,0x41c1,0xffff,0x403b,
  203. 0x6402,0x1b17,0xccbc,0x4040,
  204. 0xe92e,0x918a,0xffc5,0x402b,
  205. };
  206. static short A[] = {
  207. 0x0000,0x0000,0x0000,0x3ff0,
  208. 0x90da,0xa2a4,0xa4af,0x3fee,
  209. 0xa487,0xdcfb,0x5818,0x3fed,
  210. 0x529c,0xdd85,0x199b,0x3fec,
  211. 0xd3ad,0x995a,0xe89f,0x3fea,
  212. 0xf090,0x82a3,0xc491,0x3fe9,
  213. 0xa0db,0x422a,0xace5,0x3fe8,
  214. 0x0187,0x73eb,0xa114,0x3fe7,
  215. 0x3bcd,0x667f,0xa09e,0x3fe6,
  216. 0x5429,0xdd48,0xab07,0x3fe5,
  217. 0x2a27,0xd536,0xbfda,0x3fe4,
  218. 0x3422,0x4c12,0xdea6,0x3fe3,
  219. 0xb715,0x0a31,0x06fe,0x3fe3,
  220. 0x6238,0x6e75,0x387a,0x3fe2,
  221. 0x517b,0x3c7d,0x72b8,0x3fe1,
  222. 0x890f,0x6cf9,0xb558,0x3fe0,
  223. 0x0000,0x0000,0x0000,0x3fe0
  224. };
  225. static short B[] = {
  226. 0x0000,0x0000,0x0000,0x0000,
  227. 0x3707,0xd75b,0xed02,0x3c72,
  228. 0xcc81,0x345d,0xa1cd,0x3c87,
  229. 0x4b27,0x5686,0xe9f1,0x3c86,
  230. 0x6456,0x13b2,0xdd34,0xbc8b,
  231. 0x42e2,0xafec,0x4397,0x3c6d,
  232. 0x82e4,0xd231,0xf46a,0x3c76,
  233. 0x8a76,0xb9d7,0x9041,0xbc71,
  234. 0x0000,0x0000,0x0000,0x0000
  235. };
  236. static short R[] = {
  237. 0x937f,0xd7f2,0x6307,0x3eef,
  238. 0x9259,0x60fc,0x2fbe,0x3f24,
  239. 0xef1d,0xc84a,0xd87e,0x3f55,
  240. 0x33b7,0x6ef1,0xb2ab,0x3f83,
  241. 0x1a92,0xd704,0x6b08,0x3fac,
  242. 0xc56d,0xff82,0xbfbd,0x3fce,
  243. 0x39ef,0xfefa,0x2e42,0x3fe6
  244. };
  245.  
  246. #define douba(k) (*(double *)&A[(k)<<2])
  247. #define doubb(k) (*(double *)&B[(k)<<2])
  248. #define MEXP 16383.0
  249. #endif
  250.  
  251. #ifdef MIEEE
  252. static short P[] = {
  253. 0x3fdf,0xdb99,0x7f5b,0x5cf0,
  254. 0x400d,0xddef,0xea9e,0xdf15,
  255. 0x401e,0xccbd,0x7f78,0xeb6f,
  256. 0x4012,0xaa83,0xb65c,0x9b74
  257. };
  258. static short Q[] = {
  259. 0x4022,0xaab4,0x9b20,0x914e,
  260. 0x403b,0xffff,0x41c1,0xc9f5,
  261. 0x4040,0xccbc,0x1b17,0x6402,
  262. 0x402b,0xffc5,0x918a,0xe92e
  263. };
  264. static short A[] = {
  265. 0x3ff0,0x0000,0x0000,0x0000,
  266. 0x3fee,0xa4af,0xa2a4,0x90da,
  267. 0x3fed,0x5818,0xdcfb,0xa487,
  268. 0x3fec,0x199b,0xdd85,0x529c,
  269. 0x3fea,0xe89f,0x995a,0xd3ad,
  270. 0x3fe9,0xc491,0x82a3,0xf090,
  271. 0x3fe8,0xace5,0x422a,0xa0db,
  272. 0x3fe7,0xa114,0x73eb,0x0187,
  273. 0x3fe6,0xa09e,0x667f,0x3bcd,
  274. 0x3fe5,0xab07,0xdd48,0x5429,
  275. 0x3fe4,0xbfda,0xd536,0x2a27,
  276. 0x3fe3,0xdea6,0x4c12,0x3422,
  277. 0x3fe3,0x06fe,0x0a31,0xb715,
  278. 0x3fe2,0x387a,0x6e75,0x6238,
  279. 0x3fe1,0x72b8,0x3c7d,0x517b,
  280. 0x3fe0,0xb558,0x6cf9,0x890f,
  281. 0x3fe0,0x0000,0x0000,0x0000
  282. };
  283. static short B[] = {
  284. 0x0000,0x0000,0x0000,0x0000,
  285. 0x3c72,0xed02,0xd75b,0x3707,
  286. 0x3c87,0xa1cd,0x345d,0xcc81,
  287. 0x3c86,0xe9f1,0x5686,0x4b27,
  288. 0xbc8b,0xdd34,0x13b2,0x6456,
  289. 0x3c6d,0x4397,0xafec,0x42e2,
  290. 0x3c76,0xf46a,0xd231,0x82e4,
  291. 0xbc71,0x9041,0xb9d7,0x8a76,
  292. 0x0000,0x0000,0x0000,0x0000
  293. };
  294. static short R[] = {
  295. 0x3eef,0x6307,0xd7f2,0x937f,
  296. 0x3f24,0x2fbe,0x60fc,0x9259,
  297. 0x3f55,0xd87e,0xc84a,0xef1d,
  298. 0x3f83,0xb2ab,0x6ef1,0x33b7,
  299. 0x3fac,0x6b08,0xd704,0x1a92,
  300. 0x3fce,0xbfbd,0xff82,0xc56d,
  301. 0x3fe6,0x2e42,0xfefa,0x39ef
  302. };
  303.  
  304. #define douba(k) (*(double *)&A[(k)<<2])
  305. #define doubb(k) (*(double *)&B[(k)<<2])
  306. #define MEXP 16383.0
  307. #endif
  308.  
  309. /* log2(e) - 1 */
  310. #define LOG2EA 0.44269504088896340736
  311.  
  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.  
  322. extern double MAXNUM;
  323.  
  324. double pow( x, y )
  325. double x, y;
  326. {
  327. double w, z, W, Wa, Wb, ya, yb;
  328. double u, v;
  329. /* double F, Fa, Fb, G, Ga, Gb, H, Ha, Hb */
  330. int e, i, nflg;
  331. double floor(), fabs(), frexp(), ldexp();
  332. double reduc(), polevl(), p1evl();
  333.  
  334.  
  335. nflg = 0;    /* flag = 1 if x<0 raised to integer power */
  336.  
  337. if( x <= 0.0 )
  338.     {
  339.     if( x == 0.0 )
  340.         {
  341.         if( y == 0.0 )
  342.             return( 1.0 );  /*   0**0   */
  343.         else  
  344.             return( 0.0 );  /*   0**y   */
  345.         }
  346.     else
  347.         {
  348.         w = floor(y);
  349.         if( w != y )
  350.             { /* noninteger power of negative number */
  351.             mtherr( fname, DOMAIN );
  352.             return(0.0);
  353.             }
  354.         nflg = 1;
  355.         x = fabs(x);
  356.         }
  357.     }
  358.  
  359. /* separate significand from exponent */
  360. x = frexp( x, &e );
  361.  
  362. /* find significand in antilog table A[] */
  363. i = 1;
  364. if( x <= douba(9) )
  365.     i = 9;
  366. if( x <= douba(i+4) )
  367.     i += 4;
  368. if( x <= douba(i+2) )
  369.     i += 2;
  370. if( x >= douba(1) )
  371.     i = -1;
  372. i += 1;
  373.  
  374.  
  375. /* Find (x - A[i])/A[i]
  376.  * in order to compute log(x/A[i]):
  377.  *
  378.  * log(x) = log( a x/a ) = log(a) + log(x/a)
  379.  *
  380.  * log(x/a) = log(1+v),  v = x/a - 1 = (x-a)/a
  381.  */
  382. x -= douba(i);
  383. x -= doubb(i/2);
  384. x /= douba(i);
  385.  
  386.  
  387. /* rational approximation for log(1+v):
  388.  *
  389.  * log(1+v)  =  v  -  v**2/2  +  v**3 P(v) / Q(v)
  390.  */
  391. z = x*x;
  392. w = x * ( z * polevl( x, P, 3 ) / p1evl( x, Q, 4 ) );
  393. w = w - ldexp( z, -1 );   /*  w - 0.5 * z  */
  394.  
  395. /* Convert to base 2 logarithm:
  396.  * multiply by log2(e)
  397.  */
  398. w = w + LOG2EA * w;
  399. /* Note x was not yet added in
  400.  * to above rational approximation,
  401.  * so do it now, while multiplying
  402.  * by log2(e).
  403.  */
  404. z = w + LOG2EA * x;
  405. z = z + x;
  406.  
  407. /* Compute exponent term of the base 2 logarithm. */
  408. w = -i;
  409. w = ldexp( w, -4 );    /* divide by 16 */
  410. w += e;
  411. /* Now base 2 log of x is w + z. */
  412.  
  413. /* Multiply base 2 log by y, in extended precision.
  414.  
  415. /* separate y into large part ya
  416.  * and small part yb less than 1/16
  417.  */
  418. ya = reduc(y);
  419. yb = y - ya;
  420.  
  421.  
  422. F = z * y  +  w * yb;
  423. Fa = reduc(F);
  424. Fb = F - Fa;
  425.  
  426. G = Fa + w * ya;
  427. Ga = reduc(G);
  428. Gb = G - Ga;
  429.  
  430. H = Fb + Gb;
  431. Ha = reduc(H);
  432. w = ldexp( Ga+Ha, 4 );
  433.  
  434. /* Test the power of 2 for overflow */
  435. if( w > MEXP )
  436.     {
  437.     mtherr( fname, OVERFLOW );
  438.     return( MAXNUM );
  439.     }
  440.  
  441. if( w < -MEXP )
  442.     {
  443.     mtherr( fname, UNDERFLOW );
  444.     return( 0.0 );
  445.     }
  446.  
  447. e = w;
  448. Hb = H - Ha;
  449.  
  450. if( Hb > 0.0 )
  451.     {
  452.     e += 1;
  453.     Hb -= 0.0625;
  454.     }
  455.  
  456. /* Now the product y * log2(x)  =  Hb + e/16.0.
  457.  *
  458.  * Compute base 2 exponential of Hb,
  459.  * where -0.0625 <= Hb <= 0.
  460.  */
  461. z = Hb * polevl( Hb, R, 6 );  /*    z  =  2**Hb - 1    */
  462.  
  463. /* Express e/16 as an integer plus a negative number of 16ths.
  464.  * Find lookup table entry for the fractional power of 2.
  465.  */
  466. if( e < 0 )
  467.     i = 0;
  468. else
  469.     i = 1;
  470. i = e/16 + i;
  471. e = 16*i - e;
  472. w = douba( e );
  473. z = w + w * z;      /*    2**-e * ( 1 + (2**Hb-1) )    */
  474. z = ldexp( z, i );  /* multiply by integer power of 2 */
  475.  
  476. if( nflg )
  477.     {
  478. /* For negative x,
  479.  * find out if the integer exponent
  480.  * is odd or even.
  481.  */
  482.     w = ldexp( y, -1 );
  483.     w = floor(w);
  484.     w = ldexp( w, 1 );
  485.     if( w != y )
  486.         z = -z; /* odd exponent */
  487.     }
  488.  
  489. return( z );
  490. }
  491.  
  492.  
  493. /* Find a multiple of 1/16 that is within 1/16 of x. */
  494. static double reduc(x)
  495. double x;
  496. {
  497. double t;
  498. double ldexp(), floor();
  499.  
  500. t = ldexp( x, 4 );
  501. t = floor( t );
  502. t = ldexp( t, -4 );
  503. return(t);
  504. }
  505.