1: /*
   2: 	computes a^b.
   3: 	uses log and exp
   4: */
   5: /*! Modified by Bruce R. Julian, USGS Menlo Park, Calif.  16 NOv 1982 */
   6: /*! to use multiplication for integral exponents, using binary method */
   7: /*! (Knuth, Seminumerical Algorithms, First ed., sec. 4.6.3)          */
   8: 
   9: #include    <errno.h>
  10: int errno;
  11: double log(), exp();
  12: 
  13: double
  14: pow(arg1,arg2)
  15: double arg1, arg2;
  16: {
  17:     double  temp;
  18:     long    l;
  19:     int n, odd;
  20: 
  21:     if ((n=arg2) == arg2 && n != 0) {       /* Integral exponent */
  22:         if (n < 0.) {
  23:             if (arg1 == 0.)
  24:                 goto domain;
  25:             n = -n;
  26:             arg1 = 1./arg1;
  27:         }
  28:         temp = 1.;
  29:         for(;;) {
  30:             odd = n & 1;
  31:             n >>= 1;    /* n /= 2; */
  32:             if(odd) {
  33:                 temp *= arg1;
  34:                 if (n == 0)
  35:                     return(temp);
  36:             }
  37:             arg1 *= arg1;
  38:         }
  39:     }
  40: 
  41:     /* Non-integral or very large exponent */
  42:     if(arg1 <= 0.) {
  43:         if(arg1 == 0.) {
  44:             if(arg2 <= 0.)
  45:                 goto domain;
  46:             return(0.);
  47:         }
  48:         l = arg2;
  49:         if(l != arg2)
  50:             goto domain;
  51:         temp = exp(arg2 * log(-arg1));
  52:         if(l & 1)
  53:             temp = -temp;
  54:         return(temp);
  55:     }
  56:     return(exp(arg2 * log(arg1)));
  57: 
  58: domain:
  59:     errno = EDOM;
  60:     return(0.);
  61: }

Defined functions

pow defined in line 13; never used

Defined variables

errno defined in line 10; used 1 times
  • in line 59
Last modified: 1983-03-30
Generated: 2016-12-26
Generated by src2html V0.67
page hit count: 638
Valid CSS Valid XHTML 1.0 Strict