#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;
why is the "pow" in my equation not working?
Page 1 of 112 Replies - 480 Views - Last Post: 10 September 2012 - 08:10 AM
#1
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?
Replies To: why is the "pow" in my equation not working?
#2
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
Re: why is the "pow" in my equation not working?
Posted 09 September 2012 - 08:51 PM
#4
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
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
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
Re: why is the "pow" in my equation not working?
Posted 09 September 2012 - 09:19 PM
The error?
#8
Re: why is the "pow" in my equation not working?
Posted 09 September 2012 - 09:23 PM
#9
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
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.
So to fix the problem, cast the integer to the type you want before calling pow.
#11
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.
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
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:
Notice that my code shows different values than your code for e^x. You might try comparing with a calculator's results.
#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
Re: why is the "pow" in my equation not working?
Posted 10 September 2012 - 08:10 AM
Page 1 of 1
|
|

New Topic/Question
Reply



MultiQuote







|