C Power function negative exponent without pow()

16,117

You are using integers to hold decimal values, in this case with x and with the return type of the power function.

try:

float power(x,y)
{
   float p=1.00;
   float xx = (float)x;
   int i;
    if (y<0){
        y=-1*y;
        xx=1/xx;
    }
    for (i=1;i<=y;i++)
    {
        p=p*xx;
    }

return p;
}
Share:
16,117

Related videos on Youtube

NOOBAF
Author by

NOOBAF

Updated on June 04, 2022

Comments

  • NOOBAF
    NOOBAF almost 2 years

    I'm trying to make a little power calculator for learning purposes in C without using pow, but it always returns 0.00 when exponent is negative, please help.

    full code:

    #include<stdio.h>
    //*  power caculator function
    
    int power(x,y)
    {
       float p=1.00;
       int i;
        if (y<0){
            y=-1*y;
            x=1/x;
        }
        for (i=1;i<=y;i++)
        {
            p=p*x;
        }
    
    return p;
    }
    
    
    
    //*  main gets input, calls power caculator and prints result'
    int main()
    {
    int b;
    int e;
    float p;
    printf("enter base");
    scanf("%d",&b);
    printf("enter exponent");
    scanf("%d",&e);
    p=power(b,e);
    printf("%d to the power of %d is %.2f",b,e,p);
    return 0;
    }
    //* I am NOOB
    
    • Weather Vane
      Weather Vane about 6 years
      int power(x,y) what are the argument types? Also please pay attention to compiler warnings. By omitting the type, then compiler assumes they are int. You did supply int arguments but x=1/x; assumes integer division.
    • Christian Gibbons
      Christian Gibbons about 6 years
      What is 1/x for any x > 1?
    • Lee Daniel Crocker
      Lee Daniel Crocker about 6 years
      pow() works just fine with negative exponents, but then you chop them off by declaring your function to return int.
    • infinite
      infinite about 6 years
      Truncation is a problem apart from the other basic ones. 1 divided by any int is 0.
    • Weather Vane
      Weather Vane about 6 years
      The return value of power() is int. So even when you get the argument types (or the casting) right, any negative power (result < 1) will return 0.
    • Christian Gibbons
      Christian Gibbons about 6 years
      @infinite almost, but not quite. 1 and -1 will give you a different result. And 0 makes babies cry.
    • infinite
      infinite about 6 years
      @ChristianGibbons I tried 1/i, 1/(-i), -1/(i), -1/(-i) where i is an integer other than 0 and got the same result 0 on gcc 6.3
    • Christian Gibbons
      Christian Gibbons about 6 years
      @infinite 1 is an integer other than 0. I would be very interested in a system where 1/1 comes out to 0.
  • NOOBAF
    NOOBAF about 6 years
    Ah I SEE NOW! Thanks Miguel!
  • Miguel Mota
    Miguel Mota about 6 years
    Great! Best regards!
  • Christian Gibbons
    Christian Gibbons about 6 years
    Not the downvoter, but I assume it's because the function still has not explicitly defined the types of x and y.
  • Miguel Mota
    Miguel Mota about 6 years
    yea... defining types for parameters will help a lot... I agree.. I'll down vote myself... jejeje