0

Hi I want to write a code for a pow function without using math.h libary.

How is this code?how can i fix it when b<0

    int MyPow(int a,int b){
      if(b<0)      
        return 1 / MyPow (a,-b)
      else if(b==0)
        return 1;
      else if(b==1)
        return a;
      else
        return a*MyPow(a,b-1)
    }
Omer
  • 17
  • 1
  • 1
  • 3
  • 4
    what happens when `b<0`? – Erich Kitzmueller Aug 27 '14 at 11:07
  • @Jayesh linux system that I write there didnt have Math.h – Omer Aug 27 '14 at 11:07
  • that's not the issue... what he means is 'What is this code doing that deviates from your expectations?', 'What does it return, and what should it return', that sort of thing. – Psychemaster Aug 27 '14 at 11:09
  • 2
    @Omer math.h is a standard header, i wonder why it was not available ? – TerryG Aug 27 '14 at 11:11
  • I think you should check `if (a==0) { return 0; }` and `if (b==0||a==1) { return 1; }` and `if (b==1) { return a; }` and finaly `return myPow(a*a, b/2) * ((b%2==0) ? 1 : a);` – Jayesh Bhoi Aug 27 '14 at 11:12
  • @Psychemaster I just asked if its good function of pow and how can I upgrade this function – Omer Aug 27 '14 at 11:12
  • 2
    As a suggestion try exponentiation by squaring algorithm, see http://en.wikipedia.org/wiki/Exponentiation_by_squaring. It's much faster `O(log2(n))` than "naive" method. This may teach you recursion as well. – Grzegorz Szpetkowski Aug 27 '14 at 11:12
  • 3
    This question appears to be off-topic because it is about code review and should be migrated to http://codereview.stackexchange.com/ – bitmask Aug 27 '14 at 11:20
  • @Omer-kindly re-visit my code! I have updated it as it contained a minor flaw! Please re-edit your code! – Am_I_Helpful Aug 27 '14 at 11:27
  • 1
    If `b < 0`, then the result isn't `int`. Pow(1, -2) is 0.25. So without changing the return type it isn't really possible to return anything useful for `b<0`. – Klas Lindbäck Aug 27 '14 at 11:28
  • Possible duplicate of [How can I write a power function myself?](http://stackoverflow.com/questions/2882706/how-can-i-write-a-power-function-myself) – jww Aug 27 '14 at 11:59
  • @KlasLindbäck The original code had no branch for `b<0` so it would most likely have caused a stack overflow (tail recursion optimisation is not possible here!) – Erich Kitzmueller Aug 27 '14 at 12:34

3 Answers3

3

Probably best way I think, found here

int pow(int base, int exp)
    {
      if(exp < 0)
        return -1;

        int result = 1;
        while (exp)
        {
            if (exp & 1)
                result *= base;
            exp >>= 1;
            base *= base;
        }

        return result;
    }
Community
  • 1
  • 1
Suryakant Sharma
  • 3,852
  • 1
  • 25
  • 47
1

Everything seems perfect except for one condition :- when b<0.

For b<0,simply return

return (1.0/a)*MyPow(a,abs(b)-1);   //where  abs(b) is  absolute value of b.

OR

return (1.0/a)*(MyPow(a,b+1));      

Also,your definition of function is not valid for performing negative exponentiation,you should change it to

float MyPow(int a,int b)
Am_I_Helpful
  • 18,735
  • 7
  • 49
  • 73
0

A solution with less complexity taken from http://www.geeksforgeeks.org/write-a-c-program-to-calculate-powxn/

float power(float x, int y)
{
    float temp;
    if( y == 0)
       return 1;
    temp = power(x, y/2);       
    if (y%2 == 0)
        return temp*temp;
    else
    {
        if(y > 0)
            return x*temp*temp;
        else
            return (temp*temp)/x;
    }
}
isalgueiro
  • 1,973
  • 16
  • 20