12 Replies - 927 Views - Last Post: 10 September 2012 - 08:10 AM Rate Topic: -----

#1 zachkt  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 36
  • 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  Icon User is offline

  • Self-Trained Economist
  • member icon




Reputation: 10184
  • View blog
  • Posts: 37,599
  • 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.
Was This Post Helpful? 0
  • +
  • -

#3 zachkt  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 36
  • Joined: 19-July 12

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

Posted 09 September 2012 - 08:51 PM

View Postmacosxnerd101, 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.
Was This Post Helpful? 0
  • +
  • -

#4 CTphpnwb  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2834
  • View blog
  • Posts: 9,740
  • 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".
Was This Post Helpful? 1
  • +
  • -

#5 macosxnerd101  Icon User is offline

  • Self-Trained Economist
  • member icon




Reputation: 10184
  • View blog
  • Posts: 37,599
  • 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.
Was This Post Helpful? 0
  • +
  • -

#6 zachkt  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 36
  • Joined: 19-July 12

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

Posted 09 September 2012 - 09:15 PM

View PostCTphpnwb, 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;

}



Was This Post Helpful? 0
  • +
  • -

#7 CTphpnwb  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2834
  • View blog
  • Posts: 9,740
  • Joined: 08-August 08

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

Posted 09 September 2012 - 09:19 PM

The error?
Was This Post Helpful? 0
  • +
  • -

#8 zachkt  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 36
  • Joined: 19-July 12

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

Posted 09 September 2012 - 09:23 PM

View PostCTphpnwb, 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.
Was This Post Helpful? 0
  • +
  • -

#9 CTphpnwb  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2834
  • View blog
  • Posts: 9,740
  • 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++?
Was This Post Helpful? 0
  • +
  • -

#10 sepp2k  Icon User is online

  • D.I.C Lover
  • member icon

Reputation: 2013
  • View blog
  • Posts: 3,038
  • 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.
Was This Post Helpful? 0
  • +
  • -

#11 zachkt  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 36
  • Joined: 19-July 12

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

Posted 09 September 2012 - 10:03 PM

View Postsepp2k, 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?
Was This Post Helpful? 0
  • +
  • -

#12 CTphpnwb  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2834
  • View blog
  • Posts: 9,740
  • 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.
Was This Post Helpful? 0
  • +
  • -

#13 sepp2k  Icon User is online

  • D.I.C Lover
  • member icon

Reputation: 2013
  • View blog
  • Posts: 3,038
  • Joined: 21-June 11

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

Posted 10 September 2012 - 08:10 AM

View PostCTphpnwb, 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.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1