[Question] P110 sword refers to offer: Bit operation: Interview question 16: The integer power of the value

Interview Question 16: The integer power of the value

Topic:
Implement the function double Power(double base, int exponent), and find the exponent power of base. Do not use library functions, and do not need to consider the issue of large numbers.

Comprehensive but not efficient code

// 全局变量:判断返回值是否为异常值
// 比如返回值0.0
// ( base==0.0 && exponent<0 )属于异常情况
bool g_InvalidInput = false;

// 判断两个浮点数是否相等,不能使用"=="
bool equal(double num1, double num2);
// 求base的exponent次方
double PowerWithUnsignedExponent(double base, unsigned int exponent);

double Power(double base, int exponent)
{
    
    
    g_InvalidInput = false;
    // 异常情况
    if (equal(base, 0.0) && exponent < 0)
    {
    
    
        g_InvalidInput = true;
        return 0.0;
    }
    // 得到exponent的绝对值
    unsigned int absExponent = (unsigned int)(exponent);
    if (exponent < 0)
        absExponent = (unsigned int)(-exponent);

    double result = PowerWithUnsignedExponent(base, absExponent);
    if (exponent < 0)
        result = 1.0 / result;

    return result;
}


double PowerWithUnsignedExponent(double base, unsigned int exponent)
{
    
    
    // exponent==0
    // 程序不进入循环,直接返回1.0
    double result = 1.0;

    for (int i = 1; i <= exponent; ++i)
        result *= base;
    return result;
}


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


double Test1()
{
    
    
    return Power(2, 3);
}


int main(int argc, char* argv[])
{
    
    
    double a = Test1();
    cout << a << endl;

    return 0;
}

Comprehensive and efficient code

If the input exponent exponent is 32, 31 multiplications are required in the loop of the function PowerWithUnsignedExponent. But we can think about it in another way:
our goal is to find the 32th power of a number. If we already know its 16th power, then just square it once more on the basis of the 16th power. The 16th power is the square of the 8th power.
In this way, we only need to do 5 multiplications to find the 32th power:
first find the square, find the 4th power on the basis of the square, find the 8th power on the basis of the 4th power, and find the 8th power on the basis of the 8th power. Find the 16th power and finally find the 32th power on the basis of the 16th power.

bool g_InvalidInput = false;
bool equal(double num1, double num2);
double PowerWithUnsignedExponent(double base, unsigned int exponent);

double Power(double base, int exponent)
{
    
    
    g_InvalidInput = false;
    if (equal(base, 0.0) && exponent < 0)
    {
    
    
        g_InvalidInput = true;
        return 0.0;
    }
    unsigned int absExponent = (unsigned int)(exponent);
    if (exponent < 0)
        absExponent = (unsigned int)(-exponent);

    double result = PowerWithUnsignedExponent(base, absExponent);
    if (exponent < 0)
        result = 1.0 / result;

    return result;
}


double PowerWithUnsignedExponent(double base, unsigned int exponent)
{
    
    
    if (exponent == 0)
        return 1;
    if (exponent == 1)
        return base;

    double result = PowerWithUnsignedExponent(base, exponent >> 1);
    result *= result;
    // 考虑exponent是否是奇数,如果是奇数,在除法运算中会损失一次操作
    if ((exponent & 0x1) == 1)
        result *= base;

    return result;
}


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


double Test1()
{
    
    
    return Power(2, 3);
}


int main(int argc, char* argv[])
{
    
    
    double a = Test1();
    cout << a << endl;

    return 0;
}

Guess you like

Origin blog.csdn.net/m0_46613023/article/details/114932279