P
Pp
I am writing power series expansion programs.
For eg.
e^x = 1 + x + x^2/2! + x^3/3! + .....
Depending on how much precision I need, how do I stop the expansion.
For eg. if I need up to precision 5 then my loop will run in the form
double ex;
double newterm;
// new term is 1 in first iteration
// is x in 2nd iteration
// is x^2/2! in 3rd iteration & so on.
do
{
calculate newterm;
ex = ex + newterm;
} while (newterm > 1.0e-5);
Is the right way to do it or is there a better way do this?
What about for expansions for stuff like cos(x), sin(x) where negative
terms are involved?
For eg.
e^x = 1 + x + x^2/2! + x^3/3! + .....
Depending on how much precision I need, how do I stop the expansion.
For eg. if I need up to precision 5 then my loop will run in the form
double ex;
double newterm;
// new term is 1 in first iteration
// is x in 2nd iteration
// is x^2/2! in 3rd iteration & so on.
do
{
calculate newterm;
ex = ex + newterm;
} while (newterm > 1.0e-5);
Is the right way to do it or is there a better way do this?
What about for expansions for stuff like cos(x), sin(x) where negative
terms are involved?