Integer--11 of numerical value

Source: Internet
Author: User

The implementation function is double Power (double base, int exponent), which is the exponent of the base. You must not use library functions, and you do not need to consider large number problems.


The first thing you can think of is that if exponent is a number greater than 0, it can be multiplied by a loop, and if exponent is a plural, it is still possible to multiply base, and finally the reciprocal is 1 to be removed to multiply the result. The program is as follows:

#include  <iostream> #include  <math.h>using namespace std;double my_pow ( DOUBLE&NBSP;BASE,&NBSP;INT&NBSP;EXP) {    if (exp == 0)//When exp is 0, no matter how much base the result is 1         return 1;    if ((base == 0)  | |   (BASE&NBSP;==&NBSP;1))//when base is 0 or 1, no matter how much exp is, the result is base         Return base;    double ret = 1.0;    int flag  = 1;//set the flag bit, to determine the positive and negative exp     if (exp < 0)     {         flag = 0;         exp *=  ( -1);     }    while (exp--)      {        ret *= base;    }     if(flag == 0)         ret = 1/ret;     return ret;} Int main () {    double base = 6.123784;    int  exp = -5;    double ret = my_pow (BASE,&NBSP;EXP);     cout<< "my_pow ret: " <<ret<<endl;    cout< < "pow ret: " <<pow (Base, exp) <<endl;    return 0;}


The above pest, in order to verify the correctness of the results or not in the comparison of output results call the library function of the POW to compare, run the program results:

650) this.width=650; "src=" Http://s1.51cto.com/wyfs02/M01/7F/D2/wKioL1cvBoiRrR3rAAAQ1TQ1Kg0366.png "title=" Powret.png "alt=" Wkiol1cvboirrr3raaaq1tq1kg0366.png "/>


But the above code still has an optimized component, such as if the exp value is larger, then the number of cycles is more, this time complexity is O (N), if the value of exp is 100, then in fact, when the cycle of 50, only need to multiply the last 50 to get results, And no need to cycle 50 times, and 50 can be multiplied by 25 times the result and then squared once, and 25 times the result can be multiplied by the result of 12 times by itself and then multiply a base to get ... Therefore, change the above code to read as follows:

Double getpow (DOUBLE&NBSP;BASE,&NBSP;INT&NBSP;EXP) {    double ret = 1;     if (exp != 0)     {            ret = getpow (base, exp>>1);         if ((exp & 0x1)  == 0)          {            ret *= ret;         }        else         {            ret  *=  (ret * base);        }        }       return ret;} Double my_pow (double base, int exp) {&NBsp;    if ((base == 0)  | |   (base == 1)          return base;     int flag = 1;    if (exp < 0)      {           flag = 0;         exp *=  ( -1);    }        double ret = getpow (BASE,&NBSP;EXP);     if (flag ==  0)         ret = 1/ret;    return  ret;}


Explain the above Getpow function, because the front said can reduce the number of cycles, such as 100 times can be 50 square, 50 times to 25 square square ... And so on, in fact is 100->50->25->12 (+1)->6->3->1 (+1)->0, and we calculate from the lowest position, base of 0->base of the 1 square Base of 3 Times Square->base of 6 square->base of 12 Times Square-> This can be found, in fact, is in the exponent binary representation of the operation, from the highest to the lowest bit, when the bit value is 1 o'clock, the previous result is multiplied by the previous base (+1), if the bit value is 0, the preceding result is directly multiplied ... And from high to low need to be handed back to obtain;

Run the program with the same results as the POW library function.



Finish

This article is from the "Knock Code good Sleep zzz" blog, please be sure to keep this source http://2627lounuo.blog.51cto.com/10696599/1771568

Integer--11 of numerical value

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.