Wiki copymirror

This is a mirror of the wiki at pascal.chat. Editing, history and sign-in are there.

Fractions

English ยท Suomi

What is Fractions?

Fractions is a unit for doing calculations with fractions.
The unit was inspired by a discussion on the Lazarus forum.

Subversion

You can get a copy of fractions on

text
svn https://lazarus-ccr.svn.sourceforge.net/svnroot/lazarus-ccr/components/fractions

Or you can use viewcv to preview the files:

http://sourceforge.net/p/lazarus-ccr/svn/HEAD/tree/components/fractions/

Features

The TFraction type

TFraction is an "advanced record": a record with methods and properties.

Porperties of TFraction

Methods of TFraction

Assigning and creating Fractions

Supported calculations

Overloaded operators

Mathematical functions

General purpose functions

Conversion routines

Example program

pascal
program example;

{$mode objfpc}{$H+}
{$apptype console}

uses
  Classes, sysutils, fractions, math;

var
  F1, F2: TFraction;
  D, Prec: Double;
  i: Integer;
begin
  F1 := Fraction(1,1,3); // 1 1/3
  F2 := Fraction(4,3);   // 4/3
  writeln('F1.ToString = ',F1.ToString); // '4/3'
  writeln('F1.Resolve  = ',F1.Resolve);  // '1 1/3'
  writeln('F1.ToFloat  = ',F1.ToFloat:16:16); // 1.3333333333333333
  writeln('F2.ToString = ',F2.ToString); // '4/3'
  writeln('(F1 = F2)   = ',F1=F2);         //True

  F1 := Fraction(1,2);
  F2 := Fraction(1,3);
  writeln(F1.ToString,' * ',F2.ToString,'   = ',(F1*F2).Resolve);  // '1/6'
  writeln(F1.ToString,' / ',F2.ToString,'   = ',(F1/F2).Resolve);  // '1 1/2'
  writeln(F1.ToString,' + ',F2.ToString,'   = ',(F1+F2).Resolve);  // '5/6'
  writeln(F1.ToString,' - ',F2.ToString,'   = ',(F1-F2).Resolve);  // '1/6'
  writeln(F1.ToString,' ** 2    = ',(F1**2).Resolve);  // '1/6'

  D := 0.25;
  F1 := FloatToFraction(D, 0.000001);
  writeln('FloatTofraction(0.25) -> ',F1.ToString);   // '1/4'
  writeln;
  writeln('Approximations of Pi:');
  writeln('                                              [Pi = ',Pi:16:16,']');
  Prec := 1.0;
  for i := 1 to 10 do
  begin
    Prec := Prec / 10;
    F2 := FloatTofraction(Pi, Prec);
    writeln('FloatTofraction(Pi,',Prec:10:10,') = ',Format('%-13s',[F2.Resolve]),'   [',F2.ToFloat:16:16,']');
  end;
end.

This outputs:

pascal
F1.ToString = 4/3
F1.Resolve  = 1 1/3
F1.ToFloat  = 1.3333333333333333
F2.ToString = 4/3
(F1 = F2)   = TRUE
1/2 * 1/3   = 1/6
1/2 / 1/3   = 1 1/2
1/2 + 1/3   = 5/6
1/2 - 1/3   = 1/6
1/2 ** 2    = 1/4
FloatTofraction(0.25) -> 1/4
pascal
Approximations of Pi:
                                              [Pi = 3.1415926535897932]
FloatTofraction(Pi,0.1000000000) = 3 1/7           [3.1428571428571428]
FloatTofraction(Pi,0.0100000000) = 3 1/7           [3.1428571428571428]
FloatTofraction(Pi,0.0010000000) = 3 16/113        [3.1415929203539825]
FloatTofraction(Pi,0.0001000000) = 3 16/113        [3.1415929203539825]
FloatTofraction(Pi,0.0000100000) = 3 16/113        [3.1415929203539825]
FloatTofraction(Pi,0.0000010000) = 3 16/113        [3.1415929203539825]
FloatTofraction(Pi,0.0000001000) = 3 4703/33215    [3.1415926539214212]
FloatTofraction(Pi,0.0000000100) = 3 4703/33215    [3.1415926539214212]
FloatTofraction(Pi,0.0000000010) = 3 4703/33215    [3.1415926539214212]
FloatTofraction(Pi,0.0000000001) = 3 14093/99532   [3.1415926536189365]

From the Free Pascal wiki, page "Fractions", as it stood on 2022-08-06 after a change by Kai Burghardt, out of the wiki's dump of 2024-05-26. Copyright stays with the author who wrote it, according to the old wiki's Licensing page. If you have concerns about this material, message the admin.

Revision 3, last changed by WikiArchive .