pow function

here is my pow function. I dont understand how come if power is negative it gives the the correct answer, when power reaches 1 shouldn't it give base? why it is then giving 1/base?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
float pow (int base, int power){
	if(base<0){
		base=-1* base;
		cout<<"Base converted to positive integer "<< base <<endl;
		}
	if(power==0)
		return 1;
	else if (power==1)
		return base;
	else if (power<0){
		power=-1 * power;
		return 1.0/static_cast<float>(base*pow(base, power-1));//here i am confused	
		
	}
	else
		return base*pow(base, power-1);
	}


Enter the power to base: -3
Enter the base to power: 2
2 to the power -3 is: 0.125
Perhaps the following will prove enlightening?

http://ideone.com/jJGECz
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
#include <iostream>
#include <iomanip>

struct tabber
{
    static unsigned width;
    static unsigned increment;

    unsigned increments = 0;

    tabber() : increments(0) { }
    tabber(const tabber&) = delete;

    tabber(unsigned count) : increments(count) { width += increment * increments; }
    ~tabber() { width -= increments * increment; }

    tabber& operator=(const tabber&) = delete;

    tabber& inc() { ++increments; width += increment; return *this; }
    tabber& dec() { if (increments) { --increments; width -= increment; } return *this; }
};

unsigned tabber::width = 0;
unsigned tabber::increment = 3;

std::ostream& operator<<(std::ostream& os, const tabber& tab)
{
    if ( tabber::width )
        os << std::setw(tabber::width) << ' ';

    return os;
}


float return_value(tabber& t, float value)
{
    std::cout << t.dec() << value << '\n';
    return value;
}

float pow(int base, int power)
{
    tabber t;
    std::cout << t << "pow(" << base << ", " << power << ")\n";
    t.inc();

    if (power == 0)
        return return_value(t, 1.0);

    if (power == 1)
        return return_value(t, base);

    if (base < 0)
    {
        std::cout << t << "base is negative - recalling with positive base.\n";
        return return_value(t, pow(-base, power));
    }

    if (power < 0)
    {
        std::cout << t << "power is negative - recalling with positive power.\n";
        float value = 1.0 / pow(base, -power);

        std::cout << t << "Since power was negative, dividing 1.0 by return value.\n";
        return return_value(t, value);
    }

    return return_value(t, base * pow(base, power - 1));
}

int main()
{
    int base = 2; 
    int power = -3;

    pow(base, power);
}

pow(2, -3)
   power is negative - recalling with positive power.
   pow(2, 3)
      pow(2, 2)
         pow(2, 1)
         2
      4
   8
   Since power was negative, dividing 1.0 by return value.
0.125
so I assume power==1 never gets executed?
         pow(2, 1)
         2


power is 1. 2 (base) is returned. As expected, it is the most indented call since it is the base case that terminates the recursive chain.
Last edited on
Topic archived. No new replies allowed.