Sign in Join
Wiki copytemporary

Fractions: what changed

Revision 1 to 2. Removed lines have a minus sign; added lines have a plus sign.

OldNewLine
11## What is Fractions?
22
33Fractions is a [[Unit|unit]] for doing calculations with fractions.\
44The unit was inspired by a [discussion on the Lazarus forum](http://forum.lazarus.freepascal.org/index.php/topic,27805.0.html).
55
66## Subversion
77
88You can get a copy of fractions on
99
1010`svn `[`https://lazarus-ccr.svn.sourceforge.net/svnroot/lazarus-ccr/components/fractions`](https://lazarus-ccr.svn.sourceforge.net/svnroot/lazarus-ccr/components/fractions)
1111
1212Or you can use viewcv to preview the files:
1313
1414[<http://sourceforge.net/p/lazarus-ccr/svn/HEAD/tree/components/fractions/>](http://sourceforge.net/p/lazarus-ccr/svn/HEAD/tree/components/fractions/)
1515
1616## Features
1717
1818### The TFraction type
1919
2020TFraction is an "advanced record": a [[Record|record]] with [[Method|methods]] and [[Property|properties]].
2121
2222#### Porperties of TFraction
2323
2424- property Numerator: [[Int64]];
2525 - Gets and sets the Numerator part of the fraction
2626- property Denominator: Int64;
2727 - Gets and sets the Denominator part of the fraction.
2828 - Trying to assign 0 (zero) to TFraction.Denomiator will raise an EZeroDivide [[Exceptions|exception]]
2929
3030#### Methods of TFraction
3131
3232- [[Procedure|procedure]] Normalize;
3333 - Normalizes the fraction by dividing the Numerator and Dominatr by their [[Greatest common divisor|greatest common divisor]], e.g. 10/5 becomes 2/1
3434- [[Function|function]] ToString: [[String]];
3535 - returns the string representation of the fraction (no normalizaltion is done), e.g. when Numerator = 10 and Denominator = 5, the function returns '10/5'
3636- function Resolve: String;
37- - retruns the string representation of the fraction <b>after normalization</b>, e.g. when Numerator = 4 and Denominator = 3, the function returns '1 1/3'
37+ - retruns the string representation of the fraction after normalization, e.g. when Numerator = 4 and Denominator = 3, the function returns '1 1/3'
3838- function ToFloat: [[Double]];
3939 - returns the floating point representation of the fraction, e.g. when Numerator = 1 and Denominator = 3, the function returns 0.3333333333333
4040
4141### Assigning and creating Fractions
4242
4343- function Fraction(ANumerator, ADenominator: Int64): TFraction;
4444 - creates the fraction ANumerator/ADenominator, e.g. Fraction(1,2) gives 1/2 (one half).
4545- function Fraction(AIntPart, ANumerator, ADenominator: Int64): TFraction;
4646 - creates the fraction ANumerator/ADenominator, e.g. Fraction(1,1,2) gives 1 1/2 (one and one half).
4747
4848### Supported calculations
4949
5050#### [[Operator overloading|Overloaded]] [[Operator|operators]]
5151
5252- = (equals)
5353- [[Less than|&lt;]] (less than)
5454- [[Greater than|&gt;]] (greater than)
5555- \<= (less than or equal)
5656- \>= (greater than or equal)
5757- [[Becomes|:=]] (assign): this operator allows an Int64 or a String on the right hand side
5858- [[Plus|+]] (add)
5959- [[Minus|-]] (subtract)
6060- [[Asterisk|*]] (multiply): overloaded for fractions and Int64
6161- [[Slash|/]] (divide): overloaded for fractions and Int64
6262- ** (power): only allowed for Integers (on right hand side)
6363
6464#### Mathematical functions
6565
6666- function Min(a, b: TFraction): TFraction; [[Inline|inline]]; [[Overload|overload]];
6767 - returns a if (a \<= b), otherwise retruns b
6868- function Max(a, b: TFraction): TFraction; inline; overload;
6969 - returns a if (a \>= b), otherwise returns b
7070- function InRange(const AValue, AMin, AMax: TFraction): [[Boolean]]; inline; overload;
7171 - returns [[True]] if (AValue \>= AMin) and (AValue \<= AMax), otherwise returns [[False]]
7272- function EnsureRange(const AValue, AMin, AMax: TFraction): TFraction; inline; overload;
7373 - returns AMin if (AValue \< AMin), otherwise returns AMax if (AValue \> AMax), otherwise returns AValue
7474- function Sign(const AValue: TFraction): TValueSign; inline; overload;
7575 - retruns NegativeValue if (AValue \< 0), otherwise returns PositiveValue if (AValue \>0), otherwise returns ZeroValue
7676- function IsZero(const AValue: TFraction): Boolean; overload;
7777 - returns True if (AValue = 0), otherwise returns False
7878- function Abs(const AValue: TFraction): TFraction; overload;
7979 - return AValue if (AValue \>= 0), otherwise returns -AValue;
8080
8181### General purpose functions
8282
8383- function GreatestCommonDivisor(a, b: Int64): Int64;
8484 - returns the greates common divisor of a and b
8585- function Floor(D: Double): Int64; overload;
8686 - returns the integer part of D (subtracted with 1, if (D \< 0))
8787
8888## Conversion routines
8989
9090- function FloatToFraction(Value, Precision: Double): TFraction;
9191 - returns an approximation of Value in the form of a fraction, e.g. FloatTofraction(0.5,0.01) returns 1/2 (one half)
9292 - Precision defines the acceptable value for which (Abs(Result) - Abs(Value) \<= Precision.
9393 - Precision is bound to Value by a magnitude of 15 at most
9494 - FloatToFraction in fact is a function varable which is initialized to the MF_FloatTofraction() function. You can assign your own function to it if wanted.
9595- function TryFloatToFraction(Value, Precision: Double; out F: TFraction; AcceptPrecisionError: Boolean): Boolean;
9696 - return value depends on AcceptPrecisionError value:
9797 - if (AcceptPrecisionError = True) then the function retruns True if an approxiamtion can be found
9898 - if (AcceptPrecisionError = False) the functions returns True if an approximation can be found AND Abs(Abs(Result)-Abs(Value)) \<= Precision
9999 - the value of F only makes sense if the function returns True
100100- function FloatToFractionDef(Value, Precision: Double; Def: TFraction; AcceptPrecisionError: Boolean): TFraction;
101101 - returns the found approximation (F) if TryFloatToFraction(Value, Precision, F, AcceptPrecisionError: Boolean) succeeds, otherwise returns Def
102102- function StrToFraction(const S: String): TFraction;
103103 - returns fraction that is represented in S, raises an EConvertError upon failure
104104- function TryStrToFraction(const S: String; out F: TFraction): Boolean;
105105 - retruns True if S can be converted to a fraction, otherwise returns False
106106 - the value of F only makes sense f the function returns True
107107- function StrToFractionDef(const S: String; Def: TFraction): TFraction;
108108 - returns the fraction represented in S if conversion succeeds, otherwise returns Def
109109
110110## Example program
111111
112112``` pascal
113113program example;
114114
115115{$mode objfpc}{$H+}
116116{$apptype console}
117117
118118uses
119119 Classes, sysutils, fractions, math;
120120
121121var
122122 F1, F2: TFraction;
123123 D, Prec: Double;
124124 i: Integer;
125125begin
126126 F1 := Fraction(1,1,3); // 1 1/3
127127 F2 := Fraction(4,3); // 4/3
128128 writeln('F1.ToString = ',F1.ToString); // '4/3'
129129 writeln('F1.Resolve = ',F1.Resolve); // '1 1/3'
130130 writeln('F1.ToFloat = ',F1.ToFloat:16:16); // 1.3333333333333333
131131 writeln('F2.ToString = ',F2.ToString); // '4/3'
132132 writeln('(F1 = F2) = ',F1=F2); //True
133133
134134 F1 := Fraction(1,2);
135135 F2 := Fraction(1,3);
136136 writeln(F1.ToString,' * ',F2.ToString,' = ',(F1*F2).Resolve); // '1/6'
137137 writeln(F1.ToString,' / ',F2.ToString,' = ',(F1/F2).Resolve); // '1 1/2'
138138 writeln(F1.ToString,' + ',F2.ToString,' = ',(F1+F2).Resolve); // '5/6'
139139 writeln(F1.ToString,' - ',F2.ToString,' = ',(F1-F2).Resolve); // '1/6'
140140 writeln(F1.ToString,' ** 2 = ',(F1**2).Resolve); // '1/6'
141141
142142 D := 0.25;
143143 F1 := FloatToFraction(D, 0.000001);
144144 writeln('FloatTofraction(0.25) -> ',F1.ToString); // '1/4'
145145 writeln;
146146 writeln('Approximations of Pi:');
147147 writeln(' [Pi = ',Pi:16:16,']');
148148 Prec := 1.0;
149149 for i := 1 to 10 do
150150 begin
151151 Prec := Prec / 10;
152152 F2 := FloatTofraction(Pi, Prec);
153153 writeln('FloatTofraction(Pi,',Prec:10:10,') = ',Format('%-13s',[F2.Resolve]),' [',F2.ToFloat:16:16,']');
154154 end;
155155end.
156156```
157157
158158This outputs:
159159
160160 F1.ToString = 4/3
161161 F1.Resolve = 1 1/3
162162 F1.ToFloat = 1.3333333333333333
163163 F2.ToString = 4/3
164164 (F1 = F2) = TRUE
165165 1/2 * 1/3 = 1/6
166166 1/2 / 1/3 = 1 1/2
167167 1/2 + 1/3 = 5/6
168168 1/2 - 1/3 = 1/6
169169 1/2 ** 2 = 1/4
170170 FloatTofraction(0.25) -> 1/4
171171
172172 Approximations of Pi:
173173 [Pi = 3.1415926535897932]
174174 FloatTofraction(Pi,0.1000000000) = 3 1/7 [3.1428571428571428]
175175 FloatTofraction(Pi,0.0100000000) = 3 1/7 [3.1428571428571428]
176176 FloatTofraction(Pi,0.0010000000) = 3 16/113 [3.1415929203539825]
177177 FloatTofraction(Pi,0.0001000000) = 3 16/113 [3.1415929203539825]
178178 FloatTofraction(Pi,0.0000100000) = 3 16/113 [3.1415929203539825]
179179 FloatTofraction(Pi,0.0000010000) = 3 16/113 [3.1415929203539825]
180180 FloatTofraction(Pi,0.0000001000) = 3 4703/33215 [3.1415926539214212]
181181 FloatTofraction(Pi,0.0000000100) = 3 4703/33215 [3.1415926539214212]
182182 FloatTofraction(Pi,0.0000000010) = 3 4703/33215 [3.1415926539214212]
183183 FloatTofraction(Pi,0.0000000001) = 3 14093/99532 [3.1415926536189365]
184184
185185---
186186*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](/wiki-contact).*

Back to history