please experts could you help me to solve this equation in pascal
the equation is :
Y =1-(x^2) / 2! +(x^4) / 4! - (x^6) / 6! + (x^8) / 8! - (x^10) / 10!
send me the solutuon as soooooooon as you can
###### Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Quid, Me Anxius Sum?Commented:
program prob4;
{\$N+}
var
x : longint;
y : extended;
temp1, temp2 : longint;
i : longint;

function exponent(number, exp : longint) : longint;
var
count, temp : longint;
begin
temp := 1;
for count := 1 to exp do
temp := temp * number;
exponent := temp;
end;

function factorial(number : longint) : longint;
var
temp : longint;
begin
if number = 1 then
temp := number
else
temp := number * factorial(number - 1);
factorial := temp;
end;

{
begin
for i := 1 to 6 do
writeln(factorial(i));
for i := 1 to 6 do
writeln(exponent(6, i));
end.
}

begin
x := 12;
temp1 := 1 - exponent(x, 2) div factorial(2) + exponent(x, 4) div factorial(4);
temp2 := exponent(x, 6) div factorial(6) + exponent(x, 8) div factorial(8) + exponent(x, 10) div factorial(10);
y := temp1 + temp2;
writeln(y);
end.

You'll need a floating point chip to compile this. Most 486 and Pentium chips will do this.
0
Commented:
>>You'll need a floating point chip to compile this.

That's true only because you stated {\$N+} , which forces program to use 80x87 math co-processor.

But if you say {\$N+,E+} , then this occurs:
a) If machine has a co-processor available, then program will use it
b) If not, then co-processor will be emulated.

Thus, a program compiled with {\$N+,E+} can run on any machine, including an ancient XT.

Talking about other things, the factorial function can be achieved much more efficiently if you use a non-recursive function, like this:

function factorial(number : longint) : longint;
var
temp : longint;
i : byte;
begin
temp := 1;
for i:=2 to number do temp := temp * i;
factorial := temp;
end;

Regarding this, the argument ("number", and due to it, nor the index variable "i") needn't be a longint; much before the limit of a longint value, factorial falls beyond the allowed range.

In fact, 70! equals a value higher than 100 digits (a longint allows less than 10). Thus, an integer (even a byte) value suffices.
0
Commented:
Apparently this is a y = sin(x) function.
0
Commented:
This question are worth at least 10 times the points you want to give here :-)

Batalf
0
Quid, Me Anxius Sum?Commented:
Viking

Yes you are correct. The factorial can be done that way but recursion is nicer but slower.

I just made everything longint to make life simpler.

And correction of code follows:

temp1 := exponent(x, 4) div factorial(4) + exponent(x, 8) div factorial(8);
temp2 := exponent(x, 2) div factorial(2) + exponent(x, 4) div
factorial(4) + exponent(x, 10) div factorial(10);
y := 1 + temp1 - temp2;
0
Commented:
y make it so complicated? doesn't the code below work? and if factorial is too big it'll the subtotal will just become 0 so no overflow problems...

program approx;
var
total: longint;
subtot: longint;
i, j: integer;
x: longint;

begin
total:=0;
for i:=0 to 5 do begin
subtot:=1;
for j:=1 to i do begin
subtot:=-subtot*x*x div (2*j) div (2 * j - 1);
end;
total:=total + subtot;
end;
writeln(total);
end.

and yar... the fn reminds me of the taylor exp of sinx
0
Commented:
oops... not sinx but cosx
0
Commented:
>>recursion is nicer but slower.

There's no doubt about that, but, although there exist situations where recursion can't be avoided (or, at least, it's the easiest way to get a solution, as in Hanoi's Towers), whenever you can, try to replace it with a non-recursive solution; it will be surely faster and cheaper in resources.
0
Commented:
I was starting to write a smiliar solution to the one lychee presented... but I stopped myself on time...
....As this is too long discution for too short question.

To vikiing - recursive can always be avoided by using stack emulation.
Actually it not avoiding the recursive, but "hiding" it inside a loop.

Fuzzy.
0
Commented:
>>recursive can always be avoided by using stack emulation

Such a process keep on being recursive; the fact of hiding automatic stack piling, local variables storage and all that stuff does not eliminate recursion itself.

0
Commented:
{
Increase number of iterations to improve the accuracy.
}
{
(*   This program uses fact() function. *)

program cos;
var y,x,bracket : double; { real will also work}
c,sign : integer;

function fact(x: integer) :real;
var f : real;
c : integer;
begin
f := 1;
for c := 2 to x do
begin
f := f * c;
end;
fact := f;
end;

begin
writeln;
write('Enter value of x (angle in radians): ');
y := 1;
c:=2;
sign := -1;
while c <= 18 do
begin
bracket := sign * (exp(c * ln(x)))/fact(c);
y := y + bracket;
sign := sign * -1;
c := c +2;
end;
write('Value (cosine)= ',y:10:5);
end.
}
{ This program uses a simple loop }
program cos;
var y,x,bracket,fact : double;{ real will also work}
c,sign : integer;
begin
writeln;
write('Enter value of x (angle in radians): ');
y := 1;
bracket := 1;    (* bracket means (X^2/2) or (X^4/4)  *)
fact := 1;
sign := -1;
c := 0;
while c <= 18 do
begin
bracket := bracket * x * x;

c := c + 1;             {   calculation      }
fact := fact * c;       {        of          }
c := c + 1;             {    factorial       }
fact := fact * c;       {     is here        }

y := y + (sign*bracket/fact);
sign := sign * -1;
end;
write('Value (cosine) = ',y:10:5);