Exponent calculator issue

Write your question here.

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
#include <iostream>

using namespace std;

int main()
{

	//store base and total as double so we can get output for decimal bases
	double base, total = 1;
	//book wants int value exponents, so will not work w/ decimal exponents
	int exponent, i = 0;

	cin >> base;
	cin >> exponent;
	
	//exponent less than 0
	if (exponent < 0)
	{
		while (i > exponent)
		{
			
			//check if i value is greater than exponent
			//take total (initially 1) and multiply by input base.
			//store value as new total
			//subtract 1 from i counter
			//repeat
			total = (total * base);
			i--;
		}
		cout << 1.0 / total; 
	}
	
	//exponent EQUIVELANT to 0
	if (exponent == 0)
	{
		//anything to the power of 0 is 1
		cout << 1;
		return 0;
	}
	
	//exponent greater than 0
	else 
	{
		while (i < exponent)
		{
			//check if i value is less than exponent
			//repeat steps from previous if loop for negative exponent
			//add 1 to i counter
			//repeat
			total = total * base;
			i++;
		}
	}
	
	cout << total;
}
\

Hello all,

So this is only the negative exponent part of my code, but this is where I am getting an issue. The input I am having issue with is when I put in a decimal base with a negative integer. So for example, when I input the base as 0.5 and the exponent as -3, i get the output 80.125 rather than 8. Any input would be greatly appreciated!
Edit: Sorry forgot to mention I am using Visual C++ 2019
Edit2:Ty for the responses, appreciate all the help. Sorry I didn't post the full code I'm kind of fresh in this but @lastchane you raise a good point why would i be a on a forum if i knew what the problem was so ty for the reality check. Going to try these solutions.
Last edited on
this code works, so odds are that some you took out at line 9 for positive values is screwing it up. try putting a print statement saying "here I am @ line xxxx" on like 16 and another up in the positive exponent code to see what is going on.
Show your full code.

If you put the headers in and the final curly brace then that reduced code works fine: produces 8 when base=0.5 and exponent= -3.

Writing "this is where I am getting an issue" is a red flag. If you knew that then you wouldn't have to ask in a forum.

At a rough guess, you correctly wrote 8 and then, since you don't write a spacer or line feed, the next thing you wrote afterwards for some other reason in some other part of your code was 0.125
Last edited on
Edit: Sorry I am using Visual C++ 2019

Why would you be "sorry" for using the latest software?

I'm tempted to post you a full code but instead here are just pieces and building blocks:

I recommend you to verify these functions and constructs here:
https://en.wikipedia.org/wiki/Exponentiation

If you want your power function to be universal, that is to handle any valid input declare a template like this:

1
2
template<typename B, typename E>
double pow(B base, E exponent)


Next first thing you want to check is invalid input:

1
2
3
4
// 0^-n = UNDEFINED
// 0^0 = UNDEFINED
if (base == 0 && exponent <= 0)
       // handle error 


another invalid input case:
1
2
3
4
5
if(std::is_floating_point<B>::value && std::is_floating_point<E>::value && base < 0 && exponent < 0)
{
	// would be valid for complex numbers only!
	throw 0;
}


then also, there are inputs for which calculation is very simple, do this next!
1
2
3
4
5
6
7
8
if (base == 0)
	return 0; // b^0 = 0 [where b not 0]

if (base == 1 || exponent == 0)
	return 1;

if (exponent == 1)
	return base; // b^1 = b 



OK, now that you know user input is valid, only at this point you do the calculation, I'll give just the formula, you part of the job is to figure out how to implement it in C++

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
// First check if exponent is integral value!
if (std::is_integral<E>::value)
{
	if (exponent < 0)
	{
		// b^-n = 1/(b^-(-n))
        }
	else
	{
		// b^n = b * b^(n-1)
	}
}
// if exponent is not integral calculation is more complicated!
else if (exponent >= 1)
{
         // positive decimal exponent grater than 1
         // (b^(x/y) = y-th root of b^x) OR (b^(1/n) = n-th root of b) [where b > 0]
}
else
{
         // decimal exponent positive less then 1 or negative	
         // (b^(x/y) = exp((x/y) * ln(b))) OR (b^n = exp(n ln(b))) [where b > 0 | n = R]
}


EDIT:
this code can be further improved, such as handling the epsilon if exponent is not integral.
but we'll get to that point once you implement the above code.

Let me know if you need help or hints!
Last edited on
Let the positive-exponent case do all the heavy lifting.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
int main()
{
	
	double base, total;
	int exponent, i = 0;
	bool negativeExponent = false;  // the original exponent was negative

	cin >> base;
	cin >> exponent;
	
	//exponent less than 0
	if (exponent < 0)
	{
		exponent = -exponent;
		negativeExponent = true;
	}

	// Code to handle non-negative exponent

	if (negativeExponent) {
		total = 1 / total;
	}
	cout << total;
}
1
2
3
4
5
6
double power( double base, int exponent )
{
   if      ( exponent == 0 ) return 1.0;
   else if ( exponent <  0 ) return 1.0 / power( base, -exponent );
   else                      return base * power( base, exponent - 1 );
}
Topic archived. No new replies allowed.