Sign in Join
Wiki copytemporary

Fractions: what changed

Revision 2 to 3. 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
10-`svn `[`https://lazarus-ccr.svn.sourceforge.net/svnroot/lazarus-ccr/components/fractions`](https://lazarus-ccr.svn.sourceforge.net/svnroot/lazarus-ccr/components/fractions)
10+```text
11+svn https://lazarus-ccr.svn.sourceforge.net/svnroot/lazarus-ccr/components/fractions
12+```
1113
1214Or you can use viewcv to preview the files:
1315
1416[<http://sourceforge.net/p/lazarus-ccr/svn/HEAD/tree/components/fractions/>](http://sourceforge.net/p/lazarus-ccr/svn/HEAD/tree/components/fractions/)
1517
1618## Features
1719
1820### The TFraction type
1921
2022TFraction is an "advanced record": a [[Record|record]] with [[Method|methods]] and [[Property|properties]].
2123
2224#### Porperties of TFraction
2325
2426- property Numerator: [[Int64]];
2527 - Gets and sets the Numerator part of the fraction
2628- property Denominator: Int64;
2729 - Gets and sets the Denominator part of the fraction.
2830 - Trying to assign 0 (zero) to TFraction.Denomiator will raise an EZeroDivide [[Exceptions|exception]]
2931
3032#### Methods of TFraction
3133
3234- [[Procedure|procedure]] Normalize;
3335 - Normalizes the fraction by dividing the Numerator and Dominatr by their [[Greatest common divisor|greatest common divisor]], e.g. 10/5 becomes 2/1
3436- [[Function|function]] ToString: [[String]];
3537 - returns the string representation of the fraction (no normalizaltion is done), e.g. when Numerator = 10 and Denominator = 5, the function returns '10/5'
3638- function Resolve: String;
3739 - retruns the string representation of the fraction after normalization, e.g. when Numerator = 4 and Denominator = 3, the function returns '1 1/3'
3840- function ToFloat: [[Double]];
3941 - returns the floating point representation of the fraction, e.g. when Numerator = 1 and Denominator = 3, the function returns 0.3333333333333
4042
4143### Assigning and creating Fractions
4244
4345- function Fraction(ANumerator, ADenominator: Int64): TFraction;
4446 - creates the fraction ANumerator/ADenominator, e.g. Fraction(1,2) gives 1/2 (one half).
4547- function Fraction(AIntPart, ANumerator, ADenominator: Int64): TFraction;
4648 - creates the fraction ANumerator/ADenominator, e.g. Fraction(1,1,2) gives 1 1/2 (one and one half).
4749
4850### Supported calculations
4951
5052#### [[Operator overloading|Overloaded]] [[Operator|operators]]
5153
5254- = (equals)
5355- [[Less than|&lt;]] (less than)
5456- [[Greater than|&gt;]] (greater than)
5557- \<= (less than or equal)
5658- \>= (greater than or equal)
5759- [[Becomes|:=]] (assign): this operator allows an Int64 or a String on the right hand side
5860- [[Plus|+]] (add)
5961- [[Minus|-]] (subtract)
6062- [[Asterisk|*]] (multiply): overloaded for fractions and Int64
6163- [[Slash|/]] (divide): overloaded for fractions and Int64
6264- ** (power): only allowed for Integers (on right hand side)
6365
6466#### Mathematical functions
6567
6668- function Min(a, b: TFraction): TFraction; [[Inline|inline]]; [[Overload|overload]];
6769 - returns a if (a \<= b), otherwise retruns b
6870- function Max(a, b: TFraction): TFraction; inline; overload;
6971 - returns a if (a \>= b), otherwise returns b
7072- function InRange(const AValue, AMin, AMax: TFraction): [[Boolean]]; inline; overload;
7173 - returns [[True]] if (AValue \>= AMin) and (AValue \<= AMax), otherwise returns [[False]]
7274- function EnsureRange(const AValue, AMin, AMax: TFraction): TFraction; inline; overload;
7375 - returns AMin if (AValue \< AMin), otherwise returns AMax if (AValue \> AMax), otherwise returns AValue
7476- function Sign(const AValue: TFraction): TValueSign; inline; overload;
7577 - retruns NegativeValue if (AValue \< 0), otherwise returns PositiveValue if (AValue \>0), otherwise returns ZeroValue
7678- function IsZero(const AValue: TFraction): Boolean; overload;
7779 - returns True if (AValue = 0), otherwise returns False
7880- function Abs(const AValue: TFraction): TFraction; overload;
7981 - return AValue if (AValue \>= 0), otherwise returns -AValue;
8082
8183### General purpose functions
8284
8385- function GreatestCommonDivisor(a, b: Int64): Int64;
8486 - returns the greates common divisor of a and b
8587- function Floor(D: Double): Int64; overload;
8688 - returns the integer part of D (subtracted with 1, if (D \< 0))
8789
8890## Conversion routines
8991
9092- function FloatToFraction(Value, Precision: Double): TFraction;
9193 - returns an approximation of Value in the form of a fraction, e.g. FloatTofraction(0.5,0.01) returns 1/2 (one half)
9294 - Precision defines the acceptable value for which (Abs(Result) - Abs(Value) \<= Precision.
9395 - Precision is bound to Value by a magnitude of 15 at most
9496 - 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.
9597- function TryFloatToFraction(Value, Precision: Double; out F: TFraction; AcceptPrecisionError: Boolean): Boolean;
9698 - return value depends on AcceptPrecisionError value:
9799 - if (AcceptPrecisionError = True) then the function retruns True if an approxiamtion can be found
98100 - if (AcceptPrecisionError = False) the functions returns True if an approximation can be found AND Abs(Abs(Result)-Abs(Value)) \<= Precision
99101 - the value of F only makes sense if the function returns True
100102- function FloatToFractionDef(Value, Precision: Double; Def: TFraction; AcceptPrecisionError: Boolean): TFraction;
101103 - returns the found approximation (F) if TryFloatToFraction(Value, Precision, F, AcceptPrecisionError: Boolean) succeeds, otherwise returns Def
102104- function StrToFraction(const S: String): TFraction;
103105 - returns fraction that is represented in S, raises an EConvertError upon failure
104106- function TryStrToFraction(const S: String; out F: TFraction): Boolean;
105107 - retruns True if S can be converted to a fraction, otherwise returns False
106108 - the value of F only makes sense f the function returns True
107109- function StrToFractionDef(const S: String; Def: TFraction): TFraction;
108110 - returns the fraction represented in S if conversion succeeds, otherwise returns Def
109111
110112## Example program
111113
112114``` pascal
113115program example;
114116
115117{$mode objfpc}{$H+}
116118{$apptype console}
117119
118120uses
119121 Classes, sysutils, fractions, math;
120122
121123var
122124 F1, F2: TFraction;
123125 D, Prec: Double;
124126 i: Integer;
125127begin
126128 F1 := Fraction(1,1,3); // 1 1/3
127129 F2 := Fraction(4,3); // 4/3
128130 writeln('F1.ToString = ',F1.ToString); // '4/3'
129131 writeln('F1.Resolve = ',F1.Resolve); // '1 1/3'
130132 writeln('F1.ToFloat = ',F1.ToFloat:16:16); // 1.3333333333333333
131133 writeln('F2.ToString = ',F2.ToString); // '4/3'
132134 writeln('(F1 = F2) = ',F1=F2); //True
133135
134136 F1 := Fraction(1,2);
135137 F2 := Fraction(1,3);
136138 writeln(F1.ToString,' * ',F2.ToString,' = ',(F1*F2).Resolve); // '1/6'
137139 writeln(F1.ToString,' / ',F2.ToString,' = ',(F1/F2).Resolve); // '1 1/2'
138140 writeln(F1.ToString,' + ',F2.ToString,' = ',(F1+F2).Resolve); // '5/6'
139141 writeln(F1.ToString,' - ',F2.ToString,' = ',(F1-F2).Resolve); // '1/6'
140142 writeln(F1.ToString,' ** 2 = ',(F1**2).Resolve); // '1/6'
141143
142144 D := 0.25;
143145 F1 := FloatToFraction(D, 0.000001);
144146 writeln('FloatTofraction(0.25) -> ',F1.ToString); // '1/4'
145147 writeln;
146148 writeln('Approximations of Pi:');
147149 writeln(' [Pi = ',Pi:16:16,']');
148150 Prec := 1.0;
149151 for i := 1 to 10 do
150152 begin
151153 Prec := Prec / 10;
152154 F2 := FloatTofraction(Pi, Prec);
153155 writeln('FloatTofraction(Pi,',Prec:10:10,') = ',Format('%-13s',[F2.Resolve]),' [',F2.ToFloat:16:16,']');
154156 end;
155157end.
156158```
157159
158160This outputs:
159161
160162 F1.ToString = 4/3
161163 F1.Resolve = 1 1/3
162164 F1.ToFloat = 1.3333333333333333
163165 F2.ToString = 4/3
164166 (F1 = F2) = TRUE
165167 1/2 * 1/3 = 1/6
166168 1/2 / 1/3 = 1 1/2
167169 1/2 + 1/3 = 5/6
168170 1/2 - 1/3 = 1/6
169171 1/2 ** 2 = 1/4
170172 FloatTofraction(0.25) -> 1/4
171173
172174 Approximations of Pi:
173175 [Pi = 3.1415926535897932]
174176 FloatTofraction(Pi,0.1000000000) = 3 1/7 [3.1428571428571428]
175177 FloatTofraction(Pi,0.0100000000) = 3 1/7 [3.1428571428571428]
176178 FloatTofraction(Pi,0.0010000000) = 3 16/113 [3.1415929203539825]
177179 FloatTofraction(Pi,0.0001000000) = 3 16/113 [3.1415929203539825]
178180 FloatTofraction(Pi,0.0000100000) = 3 16/113 [3.1415929203539825]
179181 FloatTofraction(Pi,0.0000010000) = 3 16/113 [3.1415929203539825]
180182 FloatTofraction(Pi,0.0000001000) = 3 4703/33215 [3.1415926539214212]
181183 FloatTofraction(Pi,0.0000000100) = 3 4703/33215 [3.1415926539214212]
182184 FloatTofraction(Pi,0.0000000010) = 3 4703/33215 [3.1415926539214212]
183185 FloatTofraction(Pi,0.0000000001) = 3 14093/99532 [3.1415926536189365]
184186
185187---
186188*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