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.....)?
shaguarAsked:
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.

shlomoyCommented:
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
bakry99Commented:
 please write more about your Question
i'm waiting
 
  Bakry
0
shlomoyCommented:
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
Exploring ASP.NET Core: Fundamentals

Learn to build web apps and services, IoT apps, and mobile backends by covering the fundamentals of ASP.NET Core and  exploring the core foundations for app libraries.

VIBESPROCommented:
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
shaguarAuthor 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
VIBESPROCommented:
sorry about that!
I'll write a function for you then..

and Shlomoy!  How do you get that code to work?
0
shlomoyCommented:
shaguar , Is everything clear, or you need more?
If you need more, please specify.
0
shlomoyCommented:
shaguar , Is everything clear, or you need more?
If you need more, please specify.
0
shlomoyCommented:
shaguar , Is everything clear, or you need more?
If you need more, please specify.
0
mehranCommented:
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:

   procedure addtwofrac
                   (f1,f2:TFraction;
                  var res:TFraction);
   procedure addinttofrac(i:integer;
                        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).

AND ABOUT the 2'nd question.

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

Your issues matter to us.

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

Start your 7-day free trial
shaguarAuthor 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
shlomoyCommented:
^ - 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.
Please try this code.

sum, current, sign are all Integers.
This is an infinite loop.
It will continue forever.
You can have the loop stop when you reach some known "current" or known "sum" or when you think that the abs(current) is bigger than some value you want.
0
shlomoyCommented:
The code I sent produces the series you wanted by itself! you don' t need to create it yourself.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Pascal

From novice to tech pro — start learning today.