# why is the "pow" in my equation not working?

Page 1 of 1

## 12 Replies - 1573 Views - Last Post: 10 September 2012 - 08:10 AMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'http://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=291457&amp;s=7402d7cb18e42640f9380e104fc322bc&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 zachkt

Reputation: 0
• Posts: 41
• Joined: 19-July 12

# why is the "pow" in my equation not working?

Posted 09 September 2012 - 08:34 PM

I am trying rewrite the equation to find e^x which is 1 + x + x^2/2! + x^3/3!... x^n/n!. So why is the "pow" in my equation wrong?

```
#include <iostream>
#include <math.h>
using namespace std;
int main ()
{
int x;
double sum;
int i;
double factorial (int i);
double product = 1;

cout << "Enter a value for x " << endl;
cin >> x;
cout << endl;

for (i=1; i<100; i++)
{

product = i * product;

for (i=1; i<100; i++)
{

sum = 1 + x + ((pow(x,i))/product);

}

}

cout << "The value e^" << x << " is" << sum << endl;

return 0;

```

Is This A Good Question/Topic? 0

## Replies To: why is the "pow" in my equation not working?

### #2 macosxnerd101

• Games, Graphs, and Auctions

Reputation: 11447
• Posts: 43,143
• Joined: 27-December 08

## Re: why is the "pow" in my equation not working?

Posted 09 September 2012 - 08:50 PM

Wrong how? Are you getting specific compilation errors? We need more details to better help you.

### #3 zachkt

Reputation: 0
• Posts: 41
• Joined: 19-July 12

## Re: why is the "pow" in my equation not working?

Posted 09 September 2012 - 08:51 PM

macosxnerd101, on 09 September 2012 - 08:50 PM, said:

Wrong how? Are you getting specific compilation errors? We need more details to better help you.

im using visual studio and "pow" is underlined red meaning that that is the error. The program wont run.

### #4 CTphpnwb

• D.I.C Lover

Reputation: 3389
• Posts: 11,999
• Joined: 08-August 08

## Re: why is the "pow" in my equation not working?

Posted 09 September 2012 - 08:52 PM

You have a for loop nested inside another for loop, but both use the variable 'i".

### #5 macosxnerd101

• Games, Graphs, and Auctions

Reputation: 11447
• Posts: 43,143
• Joined: 27-December 08

## Re: why is the "pow" in my equation not working?

Posted 09 September 2012 - 08:53 PM

Quote

im using visual studio and "pow" is underlined red meaning that that is the error. The program wont run.

In the future, you need to post the error. Some IDEs are good about highlighting. Make sure to read why the line in question is highlighted.

### #6 zachkt

Reputation: 0
• Posts: 41
• Joined: 19-July 12

## Re: why is the "pow" in my equation not working?

Posted 09 September 2012 - 09:15 PM

CTphpnwb, on 09 September 2012 - 08:52 PM, said:

You have a for loop nested inside another for loop, but both use the variable 'i".

Still giving me the error on "pow"

```
#include <iostream>
#include <math.h>
using namespace std;
int main ()
{
int x;
double sum;
int i;
double factorial (int i);
double product = 1;

cout << "Enter a value for x " << endl;
cin >> x;
cout << endl;

for (i=1; i<100; i++)
{

product = i * product;
sum = 1 + x + ((pow(x,i))/product);

}

cout << "The value e^" << x << " is" << sum << endl;

return 0;

}

```

### #7 CTphpnwb

• D.I.C Lover

Reputation: 3389
• Posts: 11,999
• Joined: 08-August 08

## Re: why is the "pow" in my equation not working?

Posted 09 September 2012 - 09:19 PM

The error?

### #8 zachkt

Reputation: 0
• Posts: 41
• Joined: 19-July 12

## Re: why is the "pow" in my equation not working?

Posted 09 September 2012 - 09:23 PM

CTphpnwb, on 09 September 2012 - 09:19 PM, said:

The error?

It says " more than one instance of overloaded function "pow" matches the argument list:" that's what is says when I put my mouse over the "pow" in my code. It is underlined red in MS visual studio.

### #9 CTphpnwb

• D.I.C Lover

Reputation: 3389
• Posts: 11,999
• Joined: 08-August 08

## Re: why is the "pow" in my equation not working?

Posted 09 September 2012 - 09:34 PM

Not sure, but maybe you're trying to compile as C code, but using C++?

### #10 sepp2k

• D.I.C Lover

Reputation: 2277
• Posts: 3,507
• Joined: 21-June 11

## Re: why is the "pow" in my equation not working?

Posted 09 September 2012 - 09:45 PM

pow has multiple overloads - one for float, one for double and one for long double. So if you call it with an integer, it tries to convert that integer into one of the types it has an overload for. But since integers are convertible into either of those types, it doesn't know which one to pick.

So to fix the problem, cast the integer to the type you want before calling pow.

### #11 zachkt

Reputation: 0
• Posts: 41
• Joined: 19-July 12

## Re: why is the "pow" in my equation not working?

Posted 09 September 2012 - 10:03 PM

sepp2k, on 09 September 2012 - 09:45 PM, said:

pow has multiple overloads - one for float, one for double and one for long double. So if you call it with an integer, it tries to convert that integer into one of the types it has an overload for. But since integers are convertible into either of those types, it doesn't know which one to pick.

So to fix the problem, cast the integer to the type you want before calling pow.

Ya you were right, I just changed "int i" to "double i" thank you. Since it gives the 100 values for e^x, any idea how I can just have 10 values per line and have 10 lines instead of a long list of 100 values?

### #12 CTphpnwb

• D.I.C Lover

Reputation: 3389
• Posts: 11,999
• Joined: 08-August 08

## Re: why is the "pow" in my equation not working?

Posted 10 September 2012 - 07:20 AM

That's odd. This code works in my system:
```#include <iostream>
#include <math.h>
using namespace std;
int main ()
{
int x;
double sum;
int i;
double factorial (int i);
double product = 1;
cout.precision(15);

cout << "Enter a value for x " << endl;
cin >> x;
cout << endl;
sum = 0;
for (i=0; i<100; i++)
{
sum += ((pow(x,i))/product);
product *= i+1;
}
cout << " e^" << x << " = " << sum << endl;

return 0;
}

```

Notice that my code shows different values than your code for e^x. You might try comparing with a calculator's results.

### #13 sepp2k

• D.I.C Lover

Reputation: 2277
• Posts: 3,507
• Joined: 21-June 11

## Re: why is the "pow" in my equation not working?

Posted 10 September 2012 - 08:10 AM

CTphpnwb, on 10 September 2012 - 04:20 PM, said:

That's odd. This code works in my system:

Mine too, but it's not standard compliant code (not unless you're compiling in C++11 mode anyway). Some compilers are just less stringent than others.