Sign in Join
Wiki copytemporary

Revision 3 of 5, by WikiArchive on 11-4-2026. The page as it is now.

Fractions/fi

Contents

Mikä on Fractions unit?

Fractions on käännösyksikko (unit) joka suorittaa laskuja murtoluvuilla.
Käännösyksikkö syntyi keskustelun pohjalta discussion on the Lazarus forum.

Subversion

Löydät Fractions unitin

pascal
svn <https://lazarus-ccr.svn.sourceforge.net/svnroot/lazarus-ccr/components/fractions>

Tai voit vain katsella sitä viewcv:n avulla

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

Ominaisuudet

TFraction tyyppi

TFraction on niin sanottu "advanced record": eli tietue laajennettuna metodeilla ja ominaisuuksilla (Property) .

TFraction ominaisuudet

TFraction metodit

Murtolukujen tekeminen

Tuetut laskut

Operaattorit (Overloaded operators)

Matemaattiset funktiot

Yleiskäyttöiset funktiot

Muunnos rutiinit

Esimerkkiohjelma

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.

Ohjelma tulostaa:

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
    
    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/fi", as the Internet Archive kept it on 2022-07-04. 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.