Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
119 views
in Technique[技术] by (71.8m points)

c++ - Overflow pow function with long long?

I have a problem defining a pow function for long long values, this is the code:

long long int pow( long long int base, long long int exp){
    long long int count = 1;
    for(int i=exp; i>=0; i--){
        if(i==0) {break;}
        if(exp==1) {count = count * base; break;}
        count = count * base;
    }
    return count;
}

it is necessary to use iteration and not to be recursive, if I print the function for 2^i for I up to 63 from a certain moment on it computes 0, it is an overflow error? how can I avoid it?

question from:https://stackoverflow.com/questions/65904515/overflow-pow-function-with-long-long

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

2.1m questions

2.1m answers

60 comments

57.0k users

...