数值的整数次方

#include <iostream>

using namespace std;
bool equal(double a,double b);
double powabs(double a,unsigned int absb);
bool chucuo=false;

double pow(double a,int b)
{
    if(equal(a,0)&&b<0) //0的负数次方
    {
        chucuo=true;
        return 0.0;
    }

    unsigned int absb=(unsigned int)b;
    if(b<0)
        absb=(unsigned int)(-b);

    double result=powabs(a,absb);
    if(b<0)
        result=1/result;
    return result;
}

bool equal(double a,double b)
{
    if((a-b>-0.0000001&&a-b<0.0000001))
        return true;
    else
        return false;
}

double powabs(double a,unsigned int absb)
{
    double result=1.0;
    while(absb)
    {
        result*=a;
        absb--;
    }
    return result;
}

int main()
{
    cout<<pow(5,-1);
    return 0;
}

 

posted @ 2017-08-28 15:34  home普通的人  阅读(138)  评论(0编辑  收藏  举报