How to write a loop that calculates power?

一世执手 提交于 2020-06-17 09:16:09

问题


I'm trying to write a loop that calculates power without using the pow() function. I'm stuck on how to do that. Doing base *= base works for even powers upto 4, so there is something totally weird that I can't seem to figure out.

int Fast_Power(int base, int exp){
    int i = 2;
    int result;

    if(exp == 0){
        result = 1;
        }
    if(exp == 1){
        result = base;
        }
    else{
        for(i = 2; i < exp; i++){
            base *= base; 
            result = base;
        }
    }
    return result;
}

回答1:


First off, I agree it was probably a mistake to use base *= base. That said, it's not necessarily the mistake. My first impression was that OP was trying to compute powers the way that a human might do by hand. For example if you wanted to compute 3^13 a reasonable way is to start is by computing exponents which are powers of 2.

  • 3^1 = 3
  • 3^2 = 3*3 = 9
  • 3^4 = 3^2 * 3^2 = 81
  • 3^8 = 3^4 * 3^4 = 6,561

Then you can use these results to compute 3^13 as

  • 3^13 = 3^1 * 3^4 * 3^8 = 1,594,323

Once you understand the steps you could code this. The hardest part is probably determining when to stop squaring the base, and which squares should be included in the final calculation. Perhaps surprisingly the (unsigned) binary representation of the exponent tells us this! This is because the digits in binary represent the powers of two which sum together to form the number. With that in mind we can write the following.

int Fast_Power(int base, int exp) {
    int result = 1;
    unsigned int expu = exp;
    unsigned int power_of_two = 1;
    while (expu > 0) {
        if (power_of_two & expu) {
            result *= base;
            expu ^= power_of_two;
        }
        power_of_two <<= 1;
        base *= base;
    }
    return result;
}

This code doesn't have overflow protection, though that would be a good idea. Sticking with the original prototype it still accepts negative exponents and returns integers, which is a contradiction. Since OP didn't specify what should occur upon overflow or negative exponents this code doesn't attempt to handle either of those cases. Reasonable methods of addressing these issues are provided by other answers.




回答2:


base *= base;

Your problem lies with that statement, you should not be changing base at all. Rather, you should be adjusting result based on the constant value of base.

To do powers, you need repeated multiplication, but the base *= base gives you a repeated squaring of the value and you'll therefore get a much bigger value than desired. This actually works for powers of four since you iterate 4 - 2 times, squaring each iteration, and x4 == (x2)2.

It will not work for higher powers like six since you iterate 6 - 2 times, and x6 != (((x2)2)2)2. That latter value is actually equivalent to x16.

As an aside (despite your contention), it's actually not guaranteed to work for powers of two. If you follow the code in that case, you'll see that result is never assigned a value so the return value will be arbitrary. If it's working for you, that's accidental and likely to bite you at some point.


The algorithm you can use should be something like:

float power(float base, int exponent):
    # 0^0 is undefined.

    if base == 0 and exponent == 0:
        throw bad_input

    # Handle negative exponents.

    if exponent < 0:
        return 1 / power(base, -exponent)

    # Repeated multiplication to get power.

    float result = 1
    while exponent > 0:
        # Use checks to detect overflow.

        float oldResult = result
        result *= base
        if result / base is not close to oldResult:
            throw overflow
        exponent -= 1

    return result

This algorithm handles:

  • negative integral exponents (since x-y = 1/xy);
  • the undefined case of 00; and
  • overflow if you do not have arbitrary-precision values (basically, if (x * y) / y != x, you can be reasonably certain an overflow has occurred). Note the use of "not close to", it's unwise to check floats for exact equality due to potential for errors due to precision limits - far better to implement a "is close enough to" check of some description.

One thing to keep in mind when translating to C or C++, a 2's complement implementation will cause issues when using the most negative integer, since its negation is often the same value again again due to the imbalance between the positive and negative values. This is likely to lead to infinite recursion.

You can fix that simply by detecting the case early on (before anything else), with something like:

if INT_MIN == -INT_MAX - 1 and exp == INT_MIN:
    throw bad_input

The first part of that detects a 2's complement implementation, while the second detects the (problematic) use of INT_MIN as an exponent.




回答3:


What you were doing wrong is base *= base each time through the loop, which changes the base itself, each iteration.

Instead you want the base to remain the same, and multiply the final result by that original base "exp" times.

int Fast_Power(int base, int exp){
    int result=1;

    if(exp == 0){
        result = 1;
    }
    if(exp == 1){
        result = base;
    }
    else{
        for(int i = 0; i < exp; i++){
            result *= base;
        }
    }
    return result;
}



回答4:


The basic but naive algorithm you are looking for that is horribly subject to integer overflow is:

int Fast_Power (int base, int exp)
{
    int result = base;

    if (exp == 0)
        return result ? 1 : 0;

    for (int i = 1; i < exp; i++) {
        result *= base;
    }

    return result;
}

Note: result can very easily overflow. You need to employ some basic check to prevent integer-overflow and Undefined Behavior.

A minimal check (see: Catch and compute overflow during multiplication of two large integers), can be incorporated as follows. You must use a wider-type for the temporary calculation here and then compare the results against INT_MIN and INT_MAX (provided in the limits.h header) to determine if overflow occurred:

#include <limits.h>
...
int Fast_Power (int base, int exp)
{
    int result = base;

    if (exp == 0)
        return result ? 1 : 0;

    for (int i = 1; i < exp; i++) {
        long long int tmp = (long long) result * base;  /* tmp of wider type */
        if (tmp < INT_MIN || INT_MAX < tmp) {           /* check for overflow */
            fputs ("error: overflow occurred.\n", stderr);
            return 0;
        }
        result = tmp;
    }

    return result;
}

Now if you attempt, e.g. Fast_Power (2, 31); an error is generated and zero returned.

Additionally as @paxdiablo notes in the comment Zero to the power of zero may be undefined as there is no agreed upon value. You can add a test and issue a warning/error in that case if you desire.



来源:https://stackoverflow.com/questions/60022757/how-to-write-a-loop-that-calculates-power

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!