double powerf(double base, double exponent)
{
    double result;
    int sign = 1;
    double x = 0;
    if (base < 0)
    {
        sign = -1;
        base = fabs(base);
    }

    if (exponent == 0.0)
    {
         return (1.0*sign);              // n**0 = 1
    }

    if ((base == 0.0) && (exponent > 0.0))
    {
         return (0.0);                   // 0**n = 0, n > 0
    }

    x = exponent - trunc (exponent);

    if ((x == 0.0) && (fabs(exponent) <= 2147483647))
    {
        result = (sign*poweri(base, (int)trunc(exponent)));
    }
    else
    {
        result = (sign*exp(exponent * log(base)));
    }
    return result;
}
Ejemplo n.º 2
0
void  convei(int zahl, char p[SSL])
	{
	  int dig, j, ii = 0, sta, ani, z ;
          double dum;

	  if (zahl < 0) {p[0] = 45; ++ii;} 
	  z = fabs(zahl);

	  dum = log10( (double) z);
          ani = (int) dum;
	  sta = poweri(10,ani);

	  for(j=ii; j <= ani+ii; j++) {
	     dig = z / sta ;
	     p[j]  = dig + 48 ;
	     z = z - dig*sta;
	     sta = sta / 10 ;
	  }
	  p[j] = '\0';
	}
double poweri(double x, int y)
{
//The following code goes into an endless loop when y is negative
//    double value = 1.0;
//    int count = 0;
//    do
//    {
//        if(IS_ODD(exponent))
//        {
//            value *= base;
//        }
//        exponent >>= 1;
//        base *= base;
//        ++count;
//    }
//    while (exponent);
//    return (value);

    double  temp;
    if( y == 0)
    {
       return 1;
    }

    temp = poweri(x, y/2);
    if (y%2 == 0)
    {
        return temp*temp;
    }
    else
    {
        if(y > 0)
            return x*temp*temp;
        else
            return (temp*temp)/x;
    }
}