Categories: MSDN / DotNet / Java / Scripts / Linux / PHP Ask - La ask - La Answer

who gona make this factorial function to me

how to write this function ?

sin(x) approximated value

sin(x) = x - (x^3/3!) + (x^5/5!) - (x^7/7!)+......

x is in radians and (! denotes factorial)

notice that one +ve and one -ve
x^3 = mean x*x*x (x of power 3)

and i want simply way to do this function not someting defficult .. cuz iam beggenner in C++ :confused:

thanks alot ..
[391 byte] By [PowerBook] at [2007-11-11 8:30:07]
# 1 Re: who gona make this factorial function to me
just hardcode the darn thing as a lookup table.

static double fact[] =
{
1, /*0! ... 20!, precision is about to drop off for doubles */
1,
2,
6,
24,
120,
720,
5040,
40320,
362880,
3628800,
39916800,
479001600,
6227020800,
87178291200,
1307674368000,
20922789888000,
355687428096000,
6402373705728000,
121645100408832000,
2432902008176640000
};
jonnin at 2007-11-11 21:01:22 >
# 2 Re: who gona make this factorial function to me
so to use it you just say
num = fact[n];

and you sin function becomes

double Mysin(double x)
{
return x - (x*x*x/fact[3]) + (x*x*x*x*x/fact[5]) -
(x*x*x*x*x*x*x/fact[7])+ etc... ;
}
jonnin at 2007-11-11 21:02:18 >
# 3 Re: who gona make this factorial function to me
Note that pow(x,y) is x^y but it seems slow compared to a smart multiply such as
const double x2 = x*x;
const double x3 = x2*x;
const double x5 = x3*x2;
const double x7 = x5*x2;
etc ...

pow is best saved for fractional powers pow(x, 7.1234) as this is nontrivial. The function overhead + the decisions made inside (integer or floating power) make it slower than a couple of smart multiplies.
jonnin at 2007-11-11 21:03:16 >
# 4 Re: who gona make this factorial function to me
Thanks alot .. it seems that u are advance programmer maybe professional !
this function will not allow to us to use it .. cuz we didnt take it yet !

any way .. i take an idea how to make this fact. function..
here some of the suggest answer from a friend .. maybe u would like to take alook on it ..

n=3;
sin2= x;
count=1;
while ( -- where u wana to stop --)
{
sin2= sin2*-x*x/(n*(n-1));
count++;
}

and i didnt check it yet ..

thanks any way ..
PowerBook at 2007-11-11 21:04:16 >
# 5 Re: who gona make this factorial function to me
It looks ok at a glance. This is called recursion (where the function calls itself until some terminal is reached). You asked for a simple function -- I do not consider recursion simple. Its also relatively slow. However, if you want to be able to change the accuracy of the function on the fly or something to watch it converge on sin() then the loop is the way to go -- mine would have a fixed accuracy.
jonnin at 2007-11-11 21:05:28 >