powf.c 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338
  1. /* powf.c
  2. *
  3. * Power function
  4. *
  5. *
  6. *
  7. * SYNOPSIS:
  8. *
  9. * float x, y, z, powf();
  10. *
  11. * z = powf( 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 -10,10 100,000 1.4e-7 3.6e-8
  33. * 1/10 < x < 10, x uniformly distributed.
  34. * -10 < y < 10, y uniformly distributed.
  35. *
  36. *
  37. * ERROR MESSAGES:
  38. *
  39. * message condition value returned
  40. * powf overflow x**y > MAXNUMF MAXNUMF
  41. * powf underflow x**y < 1/MAXNUMF 0.0
  42. * powf domain x<0 and y noninteger 0.0
  43. *
  44. */
  45. /*
  46. Cephes Math Library Release 2.2: June, 1992
  47. Copyright 1984, 1987, 1988 by Stephen L. Moshier
  48. Direct inquiries to 30 Frost Street, Cambridge, MA 02140
  49. */
  50. #include <math.h>
  51. static char fname[] = {"powf"};
  52. /* 2^(-i/16)
  53. * The decimal values are rounded to 24-bit precision
  54. */
  55. static float A[] = {
  56. 1.00000000000000000000E0,
  57. 9.57603275775909423828125E-1,
  58. 9.17004048824310302734375E-1,
  59. 8.78126084804534912109375E-1,
  60. 8.40896427631378173828125E-1,
  61. 8.05245161056518554687500E-1,
  62. 7.71105408668518066406250E-1,
  63. 7.38413095474243164062500E-1,
  64. 7.07106769084930419921875E-1,
  65. 6.77127778530120849609375E-1,
  66. 6.48419797420501708984375E-1,
  67. 6.20928883552551269531250E-1,
  68. 5.94603538513183593750000E-1,
  69. 5.69394290447235107421875E-1,
  70. 5.45253872871398925781250E-1,
  71. 5.22136867046356201171875E-1,
  72. 5.00000000000000000000E-1
  73. };
  74. /* continuation, for even i only
  75. * 2^(i/16) = A[i] + B[i/2]
  76. */
  77. static float B[] = {
  78. 0.00000000000000000000E0,
  79. -5.61963907099083340520586E-9,
  80. -1.23776636307969995237668E-8,
  81. 4.03545234539989593104537E-9,
  82. 1.21016171044789693621048E-8,
  83. -2.00949968760174979411038E-8,
  84. 1.89881769396087499852802E-8,
  85. -6.53877009617774467211965E-9,
  86. 0.00000000000000000000E0
  87. };
  88. /* 1 / A[i]
  89. * The decimal values are full precision
  90. */
  91. static float Ainv[] = {
  92. 1.00000000000000000000000E0,
  93. 1.04427378242741384032197E0,
  94. 1.09050773266525765920701E0,
  95. 1.13878863475669165370383E0,
  96. 1.18920711500272106671750E0,
  97. 1.24185781207348404859368E0,
  98. 1.29683955465100966593375E0,
  99. 1.35425554693689272829801E0,
  100. 1.41421356237309504880169E0,
  101. 1.47682614593949931138691E0,
  102. 1.54221082540794082361229E0,
  103. 1.61049033194925430817952E0,
  104. 1.68179283050742908606225E0,
  105. 1.75625216037329948311216E0,
  106. 1.83400808640934246348708E0,
  107. 1.91520656139714729387261E0,
  108. 2.00000000000000000000000E0
  109. };
  110. #ifdef DEC
  111. #define MEXP 2032.0
  112. #define MNEXP -2032.0
  113. #else
  114. #define MEXP 2048.0
  115. #define MNEXP -2400.0
  116. #endif
  117. /* log2(e) - 1 */
  118. #define LOG2EA 0.44269504088896340736F
  119. extern float MAXNUMF;
  120. #define F W
  121. #define Fa Wa
  122. #define Fb Wb
  123. #define G W
  124. #define Ga Wa
  125. #define Gb u
  126. #define H W
  127. #define Ha Wb
  128. #define Hb Wb
  129. #ifdef ANSIC
  130. float floorf( float );
  131. float frexpf( float, int *);
  132. float ldexpf( float, int );
  133. float powif( float, int );
  134. #else
  135. float floorf(), frexpf(), ldexpf(), powif();
  136. #endif
  137. /* Find a multiple of 1/16 that is within 1/16 of x. */
  138. #define reduc(x) 0.0625 * floorf( 16 * (x) )
  139. #ifdef ANSIC
  140. float powf( float x, float y )
  141. #else
  142. float powf( x, y )
  143. float x, y;
  144. #endif
  145. {
  146. float u, w, z, W, Wa, Wb, ya, yb;
  147. /* float F, Fa, Fb, G, Ga, Gb, H, Ha, Hb */
  148. int e, i, nflg;
  149. nflg = 0; /* flag = 1 if x<0 raised to integer power */
  150. w = floorf(y);
  151. if( w < 0 )
  152. z = -w;
  153. else
  154. z = w;
  155. if( (w == y) && (z < 32768.0) )
  156. {
  157. i = w;
  158. w = powif( x, i );
  159. return( w );
  160. }
  161. if( x <= 0.0F )
  162. {
  163. if( x == 0.0 )
  164. {
  165. if( y == 0.0 )
  166. return( 1.0 ); /* 0**0 */
  167. else
  168. return( 0.0 ); /* 0**y */
  169. }
  170. else
  171. {
  172. if( w != y )
  173. { /* noninteger power of negative number */
  174. mtherr( fname, DOMAIN );
  175. return(0.0);
  176. }
  177. nflg = 1;
  178. if( x < 0 )
  179. x = -x;
  180. }
  181. }
  182. /* separate significand from exponent */
  183. x = frexpf( x, &e );
  184. /* find significand in antilog table A[] */
  185. i = 1;
  186. if( x <= A[9] )
  187. i = 9;
  188. if( x <= A[i+4] )
  189. i += 4;
  190. if( x <= A[i+2] )
  191. i += 2;
  192. if( x >= A[1] )
  193. i = -1;
  194. i += 1;
  195. /* Find (x - A[i])/A[i]
  196. * in order to compute log(x/A[i]):
  197. *
  198. * log(x) = log( a x/a ) = log(a) + log(x/a)
  199. *
  200. * log(x/a) = log(1+v), v = x/a - 1 = (x-a)/a
  201. */
  202. x -= A[i];
  203. x -= B[ i >> 1 ];
  204. x *= Ainv[i];
  205. /* rational approximation for log(1+v):
  206. *
  207. * log(1+v) = v - 0.5 v^2 + v^3 P(v)
  208. * Theoretical relative error of the approximation is 3.5e-11
  209. * on the interval 2^(1/16) - 1 > v > 2^(-1/16) - 1
  210. */
  211. z = x*x;
  212. w = (((-0.1663883081054895 * x
  213. + 0.2003770364206271) * x
  214. - 0.2500006373383951) * x
  215. + 0.3333331095506474) * x * z;
  216. w -= 0.5 * z;
  217. /* Convert to base 2 logarithm:
  218. * multiply by log2(e)
  219. */
  220. w = w + LOG2EA * w;
  221. /* Note x was not yet added in
  222. * to above rational approximation,
  223. * so do it now, while multiplying
  224. * by log2(e).
  225. */
  226. z = w + LOG2EA * x;
  227. z = z + x;
  228. /* Compute exponent term of the base 2 logarithm. */
  229. w = -i;
  230. w *= 0.0625; /* divide by 16 */
  231. w += e;
  232. /* Now base 2 log of x is w + z. */
  233. /* Multiply base 2 log by y, in extended precision. */
  234. /* separate y into large part ya
  235. * and small part yb less than 1/16
  236. */
  237. ya = reduc(y);
  238. yb = y - ya;
  239. F = z * y + w * yb;
  240. Fa = reduc(F);
  241. Fb = F - Fa;
  242. G = Fa + w * ya;
  243. Ga = reduc(G);
  244. Gb = G - Ga;
  245. H = Fb + Gb;
  246. Ha = reduc(H);
  247. w = 16 * (Ga + Ha);
  248. /* Test the power of 2 for overflow */
  249. if( w > MEXP )
  250. {
  251. mtherr( fname, OVERFLOW );
  252. return( MAXNUMF );
  253. }
  254. if( w < MNEXP )
  255. {
  256. mtherr( fname, UNDERFLOW );
  257. return( 0.0 );
  258. }
  259. e = w;
  260. Hb = H - Ha;
  261. if( Hb > 0.0 )
  262. {
  263. e += 1;
  264. Hb -= 0.0625;
  265. }
  266. /* Now the product y * log2(x) = Hb + e/16.0.
  267. *
  268. * Compute base 2 exponential of Hb,
  269. * where -0.0625 <= Hb <= 0.
  270. * Theoretical relative error of the approximation is 2.8e-12.
  271. */
  272. /* z = 2**Hb - 1 */
  273. z = ((( 9.416993633606397E-003 * Hb
  274. + 5.549356188719141E-002) * Hb
  275. + 2.402262883964191E-001) * Hb
  276. + 6.931471791490764E-001) * Hb;
  277. /* Express e/16 as an integer plus a negative number of 16ths.
  278. * Find lookup table entry for the fractional power of 2.
  279. */
  280. if( e < 0 )
  281. i = -( -e >> 4 );
  282. else
  283. i = (e >> 4) + 1;
  284. e = (i << 4) - e;
  285. w = A[e];
  286. z = w + w * z; /* 2**-e * ( 1 + (2**Hb-1) ) */
  287. z = ldexpf( z, i ); /* multiply by integer power of 2 */
  288. if( nflg )
  289. {
  290. /* For negative x,
  291. * find out if the integer exponent
  292. * is odd or even.
  293. */
  294. w = 2 * floorf( (float) 0.5 * w );
  295. if( w != y )
  296. z = -z; /* odd exponent */
  297. }
  298. return( z );
  299. }