Would anyone happen to have an exaple of a program to approximate the eponential function e^x?

Thanks

This is a discussion on *Damsel in distress...* within the **C++ Programming** forums, part of the General Programming Boards category; Would anyone happen to have an exaple of a program to approximate the eponential function e^x?
Thanks...

- 02-25-2002 #1

- Join Date
- Feb 2002
- Posts
- 7

## Damsel in distress...

Would anyone happen to have an exaple of a program to approximate the eponential function e^x?

Thanks

- 02-25-2002 #2
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

govtcheez03@hotmail.com

- 02-25-2002 #3

- Join Date
- Feb 2002
- Posts
- 7

## Sorry about the double post..

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 #4
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

govtcheez03@hotmail.com

- 02-25-2002 #5

- Join Date
- Feb 2002
- Posts
- 33

Last edited by Dummies102; 02-25-2002 at 12:21 PM.

- 02-25-2002 #6

- Join Date
- Feb 2002
- Posts
- 7

## Hi

Would you happen to have an example program so I have something to reference?????

- 02-25-2002 #7

- Join Date
- Feb 2002
- Posts
- 33

#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**pow**er 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 #8
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?

- 02-25-2002 #9

- Join Date
- Feb 2002
- Posts
- 7

## 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 #10
Well... what do you base your approximation and actual answer on?

-Govtcheez

govtcheez03@hotmail.com

- 02-25-2002 #11

- Join Date
- Feb 2002
- Posts
- 7

## 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.

All of your answeres were great thanks

- 02-25-2002 #12

- Join Date
- Feb 2002
- Posts
- 4

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.

- 02-25-2002 #13
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..."

- Exactly how to get started with C++ (or C) today
- C Tutorial
- C++ Tutorial
- 5 ways you can learn to program faster
- The 5 Most Common Problems New Programmers Face
- How to set up a compiler
- 8 Common programming Mistakes
- What is C++11?
- Creating a game, from start to finish

- How to create a shared library on Linux with GCC - December 30, 2011
- Enum classes and nullptr in C++11 - November 27, 2011
- Learn about The Hash Table - November 20, 2011
- Rvalue References and Move Semantics in C++11 - November 13, 2011
- C and C++ for Java Programmers - November 5, 2011
- A Gentle Introduction to C++ IO Streams - October 10, 2011