Subversion Repositories Kolibri OS

Rev

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

  1. /*                                                      powi.c
  2.  *
  3.  *      Real raised to integer power
  4.  *
  5.  *
  6.  *
  7.  * SYNOPSIS:
  8.  *
  9.  * double x, y, __powi();
  10.  * int n;
  11.  *
  12.  * y = __powi( x, n );
  13.  *
  14.  *
  15.  *
  16.  * DESCRIPTION:
  17.  *
  18.  * Returns argument x raised to the nth power.
  19.  * The routine efficiently decomposes n as a sum of powers of
  20.  * two. The desired power is a product of two-to-the-kth
  21.  * powers of x.  Thus to compute the 32767 power of x requires
  22.  * 28 multiplications instead of 32767 multiplications.
  23.  *
  24.  *
  25.  *
  26.  * ACCURACY:
  27.  *
  28.  *
  29.  *                      Relative error:
  30.  * arithmetic   x domain   n domain  # trials      peak         rms
  31.  *    DEC       .04,26     -26,26    100000       2.7e-16     4.3e-17
  32.  *    IEEE      .04,26     -26,26     50000       2.0e-15     3.8e-16
  33.  *    IEEE        1,2    -1022,1023   50000       8.6e-14     1.6e-14
  34.  *
  35.  * Returns MAXNUM on overflow, zero on underflow.
  36.  *
  37.  */
  38. /*                                                      powi.c  */
  39.  
  40. /*
  41. Cephes Math Library Release 2.8:  June, 2000
  42. Copyright 1984, 1995, 2000 by Stephen L. Moshier
  43. */
  44.  
  45. /*
  46. Modified for mingw
  47. 2002-07-22 Danny Smith <dannysmith@users.sourceforge.net>
  48. */
  49.  
  50. #ifdef __MINGW32__
  51. #include "cephes_mconf.h"
  52. #else
  53. #include "mconf.h"
  54. #ifdef ANSIPROT
  55. extern double log ( double );
  56. extern double frexp ( double, int * );
  57. extern int signbit ( double );
  58. #else
  59. double log(), frexp();
  60. int signbit();
  61. #endif
  62. extern double NEGZERO, INFINITY, MAXNUM, MAXLOG, MINLOG, LOGE2;
  63. #endif /* __MINGW32__ */
  64.  
  65. #ifndef _SET_ERRNO
  66. #define _SET_ERRNO(x)
  67. #endif
  68.  
  69. double __powi( x, nn )
  70. double x;
  71. int nn;
  72. {
  73. int n, e, sign, asign, lx;
  74. double w, y, s;
  75.  
  76. /* See pow.c for these tests.  */
  77. if( x == 0.0 )
  78.         {
  79.         if( nn == 0 )
  80.                 return( 1.0 );
  81.         else if( nn < 0 )
  82.             return( INFINITY );
  83.         else
  84.           {
  85.             if( nn & 1 )
  86.               return( x );
  87.             else
  88.               return( 0.0 );
  89.           }
  90.         }
  91.  
  92. if( nn == 0 )
  93.         return( 1.0 );
  94.  
  95. if( nn == -1 )
  96.         return( 1.0/x );
  97.  
  98. if( x < 0.0 )
  99.         {
  100.         asign = -1;
  101.         x = -x;
  102.         }
  103. else
  104.         asign = 0;
  105.  
  106.  
  107. if( nn < 0 )
  108.         {
  109.         sign = -1;
  110.         n = -nn;
  111.         }
  112. else
  113.         {
  114.         sign = 1;
  115.         n = nn;
  116.         }
  117.  
  118. /* Even power will be positive. */
  119. if( (n & 1) == 0 )
  120.         asign = 0;
  121.  
  122. /* Overflow detection */
  123.  
  124. /* Calculate approximate logarithm of answer */
  125. s = frexp( x, &lx );
  126. e = (lx - 1)*n;
  127. if( (e == 0) || (e > 64) || (e < -64) )
  128.         {
  129.         s = (s - 7.0710678118654752e-1) / (s +  7.0710678118654752e-1);
  130.         s = (2.9142135623730950 * s - 0.5 + lx) * nn * LOGE2;
  131.         }
  132. else
  133.         {
  134.         s = LOGE2 * e;
  135.         }
  136.  
  137. if( s > MAXLOG )
  138.         {
  139.         mtherr( "powi", OVERFLOW );
  140.         _SET_ERRNO(ERANGE);
  141.         y = INFINITY;
  142.         goto done;
  143.         }
  144.  
  145. #if DENORMAL
  146. if( s < MINLOG )
  147.         {
  148.         y = 0.0;
  149.         goto done;
  150.         }
  151.  
  152. /* Handle tiny denormal answer, but with less accuracy
  153.  * since roundoff error in 1.0/x will be amplified.
  154.  * The precise demarcation should be the gradual underflow threshold.
  155.  */
  156. if( (s < (-MAXLOG+2.0)) && (sign < 0) )
  157.         {
  158.         x = 1.0/x;
  159.         sign = -sign;
  160.         }
  161. #else
  162. /* do not produce denormal answer */
  163. if( s < -MAXLOG )
  164.         return(0.0);
  165. #endif
  166.  
  167.  
  168. /* First bit of the power */
  169. if( n & 1 )
  170.         y = x;
  171.                
  172. else
  173.         y = 1.0;
  174.  
  175. w = x;
  176. n >>= 1;
  177. while( n )
  178.         {
  179.         w = w * w;      /* arg to the 2-to-the-kth power */
  180.         if( n & 1 )     /* if that bit is set, then include in product */
  181.                 y *= w;
  182.         n >>= 1;
  183.         }
  184.  
  185. if( sign < 0 )
  186.         y = 1.0/y;
  187.  
  188. done:
  189.  
  190. if( asign )
  191.         {
  192.         /* odd power of negative number */
  193.         if( y == 0.0 )
  194.                 y = NEGZERO;
  195.         else
  196.                 y = -y;
  197.         }
  198. return(y);
  199. }
  200.