Would anyone happen to have an exaple of a program to approximate the eponential function e^x?
Thanks
Would anyone happen to have an exaple of a program to approximate the eponential function e^x?
Thanks
Sorry, damsel - don't double post. Wait a while for someone to answer your question.
You could always use
#define e whatever e is
and use the pow function.
-Govtcheez
[email protected]
I felt bad because there was a mean face attaced to it and I ment to put a happy face.
Could you explain a little more your idea?
OK...
You can use
#define e 2.1345 //I don't know the value of e off the top of my head
and then use the pow(x,y) function from math.h to do the exponent.
-Govtcheez
[email protected]
Last edited by Dummies102; 02-25-2002 at 12:21 PM.
Would you happen to have an example program so I have something to reference?????
#include <iostream.h>
#include <math.h>
int main()
{
double x, y;
cout <<"This program will find x^y. please enter x(number) and then y(power)";
x = pow(x, y);//this is the pow function, it takes the first number to the power of the second.
cout <<"\nx to the power of y ="<<x;
return 0;
}
that should do it for ya, post again if you still need help.
There is a function in the math header:
double exp( double arg )
returns the value of e^arg.
Conversely,
double log( double num )
returns the natural logarithm of num.
There are many other useful functions in that header you should look at a reference for it. I suggest picking up the C/C++ programmer's reference (good reference book to have).
If a tree falls in the forest, and no one is around to see it, do the other trees make fun of it?
The thing that keeps tripping me up is that I need to obtain an output as follows:
e to the x 7.38
approximation 6.333
the difference 1.056
With a keyboard inut such as: 2.0
I am just not getting it???
Well... what do you base your approximation and actual answer on?
-Govtcheez
[email protected]
= 1 +(x/1) + (x^2/2) + (x^3/6) = (x^4/24)...X^n/n!
I have to read in x from the keyboard and use the exponential function on x and write the result. Then do the approximation for the first four terms, starting at 1 and write the results. Then write the difference between the two.
And I am just starting to learn this stuff.
All of your answeres were great thanks
if you need to write each function and it's ok to use a little recursion, try something like:
int factorial(a) //computes factorial
{
if(a > 1)
a = a * N(a-1);
else
return 1;
return a;
}
float power(x,y) //computes the power
{
int i;
float result = x;
for(i=y; i>1; i--)
result = result * x;
return result;
}
main()
{
float x, result;
printf("\nPlease enter value for x :>");
scanf("%f", &x);
for(i=1; i<10;i++) //make the "10" bigger if you want more loops
{
result += power(x, i) / factorial( i );
printf("Term %i = %f", i, x); // move the print outside the for() loop if you only want the final answer
}
}
//I think all that would work, I just wrote it out off the top of my head, there might be some minor issues or something, but that's the basics of what you'll need. Also, it's assuming you'll only be using positive, non zero numbers. You'll have to add a couple modifications to fix those, but it's not hard and is just more of the same. Those are fun programs to write.
Last edited by bgrahambo; 02-25-2002 at 08:15 PM.
What you probably want is in the <iomanip.h> header. It is scientific notation (2.55e+32) is what it looks like you want. With iomanip I believe you can just denote the output format like so...
cout>>dec>>x; //outputs the x variable in decimal format.
...The scientific notation manip(dec) needs to be looked up.
My Avatar says: "Stay in School"
Rocco is the Boy!
"SHUT YOUR LIPS..."