Efficient solution to power calculation of C language _c language

Source: Internet
Author: User

This paper demonstrates the efficient solution of power calculation in C language. is of practical value. Share to everyone for your reference. The specific methods are as follows:

The topics are as follows:

Given base, the power exp of base is obtained

Only consider the basic functions, do not make any boundary conditions of the decision, you can get the following code:

#include <iostream>

using namespace std;

int cacexp (int base, int exp)
 int result = 1;
 int thebase = 1;
 while (exp)
 if (exp & 0x01) Result
  = result * BASE;
 Base = base * BASE;
 EXP = exp >> 1;
 return result;

int getrecurexp (int base, int exp)
 if (exp = = 0)

 if (exp = = 1)

 int result = Getrecurexp (base, exp >> 1);
 Result *= result;
 IF (exp & 0x01) result
 *= base;

 return result;

int main ()
 for (int i = 1; i < i++)
 {int result
 = Cacexp (2, i);
 int result = Getrecurexp (2, i);
 cout << "Result:" << result << Endl;

 return 0;

Then look at the numerical integer method of solving the number of times:

#include <iostream> using namespace std;
 BOOL Equalzero (double) {if (number < 0.000001 && number > -0.000001) return true;
else return false;
 Double _mypow (double base, int exp) {if (exp = 0) return 1;

 if (exp = = 1) return base;
 Double result = _mypow (base, exp >> 1);
 Result *= result;

 IF (exp & 0x01) result *= base;
return result;
 Double _mypow2 (double base, int exp) {if (exp = 0) return 1;
 double result = 1;
 while (exp) {if (exp & 0x01) result *= base;
 Base *= Base;
 EXP = exp >> 1;
return result;
 Double Mypow (double base, int exp) {if (Equalzero (base)) return 0;

 if (exp = = 0) return 1;
 BOOL flag = FALSE;
 if (exp < 0) {flag = true;
 exp =-exp;
 Double result = _mypow2 (base, exp);
 if (flag) {result = 1/result;
return result;
 } void Main () {double base = 2.0;

 int exp =-5;
 Double result = MYPOW (base, exp); cout << "Result:" << result << Endl


I believe that this article on the C program algorithm design learning has a certain reference value.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.