# Damsel in distress...

• 02-25-2002
kaslor
Damsel in distress...
Would anyone happen to have an exaple of a program to approximate the eponential function e^x?

Thanks:)
• 02-25-2002
Govtcheez
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.
• 02-25-2002
kaslor
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?:)
• 02-25-2002
Govtcheez
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.
• 02-25-2002
Dummies102
• 02-25-2002
kaslor
Hi
Would you happen to have an example program so I have something to reference?????:) :)
• 02-25-2002
Dummies102
#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.
• 02-25-2002
taylorguitarman
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).
• 02-25-2002
kaslor
Hi every one!!!
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???:)
• 02-25-2002
Govtcheez
• 02-25-2002
kaslor
The formula looks like this
= 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.
• 02-25-2002
bgrahambo
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. :)
• 02-25-2002
OneStiffRod
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.