# fractions

How do you do fractions in Pascal? Does it require a function if so how would it be written? Also how can I aternately add and subtract a series of numbers i.e. (1-3+5-7+9.....)?
###### 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.

Commented:
to have a variable which can work with fractions (real numbers) just declare one like this:

var
fraction : real;

the series can be printed like this:

sum := 1;
while(true) do begin
println(sum);
sum := sum + 2 * (-1)^(sum)
end;
0
Commented:
i'm waiting

Bakry
0
Commented:
I have an error, this is correct:

sum := 1;
pow:=1;
while(true) do begin
println(sum);
sum := sum + 2 * (-1)^pow;
pow:=pow+1;
end;
0
Commented:
That's easy!  There is a fraction function right in pascal!!  You can even make one if you want.  here the one from TP:

var
I, R: Real;

begin
I:= int(123.456);
R:= frac(123.456);
writeln(I, '   ', R);
end.

I returns 123;   R returns 0.456

there you have it.  The whole number on the left and the remainder on the right.

And to add a series of numbers is the same as 1+1.  No different!!

writeln(1+8+5-3+9-8+2);   { 13 }

that's it
0
Author Commented:
No I wanted fractions like this 1/2 with a denomonator. Shlomoy I like what you have given me I'll see what I can do with it. Thanks.
0
Commented:
I'll write a function for you then..

and Shlomoy!  How do you get that code to work?
0
Commented:
shaguar , Is everything clear, or you need more?
If you need more, please specify.
0
Commented:
shaguar , Is everything clear, or you need more?
If you need more, please specify.
0
Commented:
shaguar , Is everything clear, or you need more?
If you need more, please specify.
0
Commented:
Hi, shaguar.

There is no built-in tools in pascal for handling fractions, although you can make it yourself.

In order to make it, you should declare a new type like this:

type
TFraction = record
num,den:longint
end;

and you should write some functions for common operators, such as addition of two fractions or addition of an integer and a fraction and so on (only write those which are necessary). some headers are:

(f1,f2:TFraction;
var res:TFraction);
f:TFraction;
var res:TFraction);

and be carefull. You should write your functions as efficient as possible; because in working with fractions, numerator and denomerator grow very fast and may overflow from a longint variable. As a simple solution to this problem you can frequently divide 'num' and 'den' to 'gcd(num,den)'----(gcd ::= greatest common divisor).

you can use at least two method to solve this problem:

First Method: you can do two operation(one summation and one subtraction) in the body of the loop.

Second Method: you can declare a simple variable, such as Mult, initiated to 1, and place a

Mult := Mult * -1;

in the body of the loop and then use Mult with the terms of that seri:

Sum := Sum + Mult * Term;

I am not so good in english writing. I hope my answer will be usefull for you.

Please give some points to me. :)
0

Experts Exchange Solution brought to you by

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Author Commented:
Ok looks good I'll give it a whirl and let you know how it works.  Shlomoy evrything is clear except this ^ symbol keeps giving me an invalid indentifier.I'll figure it out. Thanks again.
0
Commented:
^ - is ment as a Power operator.
I forgot that in pascal it means "pointer".
Therefore it's wrong to use it the way I did.

let me try to give you that peace of code again:

sum:=0;
current:=1;
sign:=1;
while(true) do begin
println("current element: ", current);
sum:=sum+current;
println("current sum: ", sum);
current:= (current+2*sign)*(-1)*sign;
sign:= sign*(-1);
end;

This is the correct code. Sorry for the previous stupid error I made.