Fractions: what changed
Revision 0 to 1. Removed lines have a minus sign; added lines have a plus sign.
Title: Fractions
| Old | New | Line | |
|---|---|---|---|
| 1 | + | ## What is Fractions? | |
| 2 | + | ||
| 3 | + | Fractions is a [[Unit|unit]] for doing calculations with fractions.\ | |
| 4 | + | The unit was inspired by a [discussion on the Lazarus forum](http://forum.lazarus.freepascal.org/index.php/topic,27805.0.html). | |
| 5 | + | ||
| 6 | + | ## Subversion | |
| 7 | + | ||
| 8 | + | You can get a copy of fractions on | |
| 9 | + | ||
| 10 | + | `svn `[`https://lazarus-ccr.svn.sourceforge.net/svnroot/lazarus-ccr/components/fractions`](https://lazarus-ccr.svn.sourceforge.net/svnroot/lazarus-ccr/components/fractions) | |
| 11 | + | ||
| 12 | + | Or you can use viewcv to preview the files: | |
| 13 | + | ||
| 14 | + | [<http://sourceforge.net/p/lazarus-ccr/svn/HEAD/tree/components/fractions/>](http://sourceforge.net/p/lazarus-ccr/svn/HEAD/tree/components/fractions/) | |
| 15 | + | ||
| 16 | + | ## Features | |
| 17 | + | ||
| 18 | + | ### The TFraction type | |
| 19 | + | ||
| 20 | + | TFraction is an "advanced record": a [[Record|record]] with [[Method|methods]] and [[Property|properties]]. | |
| 21 | + | ||
| 22 | + | #### Porperties of TFraction | |
| 23 | + | ||
| 24 | + | - property Numerator: [[Int64]]; | |
| 25 | + | - Gets and sets the Numerator part of the fraction | |
| 26 | + | - property Denominator: Int64; | |
| 27 | + | - Gets and sets the Denominator part of the fraction. | |
| 28 | + | - Trying to assign 0 (zero) to TFraction.Denomiator will raise an EZeroDivide [[Exceptions|exception]] | |
| 29 | + | ||
| 30 | + | #### Methods of TFraction | |
| 31 | + | ||
| 32 | + | - [[Procedure|procedure]] Normalize; | |
| 33 | + | - Normalizes the fraction by dividing the Numerator and Dominatr by their [[Greatest common divisor|greatest common divisor]], e.g. 10/5 becomes 2/1 | |
| 34 | + | - [[Function|function]] ToString: [[String]]; | |
| 35 | + | - returns the string representation of the fraction (no normalizaltion is done), e.g. when Numerator = 10 and Denominator = 5, the function returns '10/5' | |
| 36 | + | - 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' | |
| 38 | + | - function ToFloat: [[Double]]; | |
| 39 | + | - returns the floating point representation of the fraction, e.g. when Numerator = 1 and Denominator = 3, the function returns 0.3333333333333 | |
| 40 | + | ||
| 41 | + | ### Assigning and creating Fractions | |
| 42 | + | ||
| 43 | + | - function Fraction(ANumerator, ADenominator: Int64): TFraction; | |
| 44 | + | - creates the fraction ANumerator/ADenominator, e.g. Fraction(1,2) gives 1/2 (one half). | |
| 45 | + | - function Fraction(AIntPart, ANumerator, ADenominator: Int64): TFraction; | |
| 46 | + | - creates the fraction ANumerator/ADenominator, e.g. Fraction(1,1,2) gives 1 1/2 (one and one half). | |
| 47 | + | ||
| 48 | + | ### Supported calculations | |
| 49 | + | ||
| 50 | + | #### [[Operator overloading|Overloaded]] [[Operator|operators]] | |
| 51 | + | ||
| 52 | + | - = (equals) | |
| 53 | + | - [[Less than|<]] (less than) | |
| 54 | + | - [[Greater than|>]] (greater than) | |
| 55 | + | - \<= (less than or equal) | |
| 56 | + | - \>= (greater than or equal) | |
| 57 | + | - [[Becomes|:=]] (assign): this operator allows an Int64 or a String on the right hand side | |
| 58 | + | - [[Plus|+]] (add) | |
| 59 | + | - [[Minus|-]] (subtract) | |
| 60 | + | - [[Asterisk|*]] (multiply): overloaded for fractions and Int64 | |
| 61 | + | - [[Slash|/]] (divide): overloaded for fractions and Int64 | |
| 62 | + | - ** (power): only allowed for Integers (on right hand side) | |
| 63 | + | ||
| 64 | + | #### Mathematical functions | |
| 65 | + | ||
| 66 | + | - function Min(a, b: TFraction): TFraction; [[Inline|inline]]; [[Overload|overload]]; | |
| 67 | + | - returns a if (a \<= b), otherwise retruns b | |
| 68 | + | - function Max(a, b: TFraction): TFraction; inline; overload; | |
| 69 | + | - returns a if (a \>= b), otherwise returns b | |
| 70 | + | - function InRange(const AValue, AMin, AMax: TFraction): [[Boolean]]; inline; overload; | |
| 71 | + | - returns [[True]] if (AValue \>= AMin) and (AValue \<= AMax), otherwise returns [[False]] | |
| 72 | + | - function EnsureRange(const AValue, AMin, AMax: TFraction): TFraction; inline; overload; | |
| 73 | + | - returns AMin if (AValue \< AMin), otherwise returns AMax if (AValue \> AMax), otherwise returns AValue | |
| 74 | + | - function Sign(const AValue: TFraction): TValueSign; inline; overload; | |
| 75 | + | - retruns NegativeValue if (AValue \< 0), otherwise returns PositiveValue if (AValue \>0), otherwise returns ZeroValue | |
| 76 | + | - function IsZero(const AValue: TFraction): Boolean; overload; | |
| 77 | + | - returns True if (AValue = 0), otherwise returns False | |
| 78 | + | - function Abs(const AValue: TFraction): TFraction; overload; | |
| 79 | + | - return AValue if (AValue \>= 0), otherwise returns -AValue; | |
| 80 | + | ||
| 81 | + | ### General purpose functions | |
| 82 | + | ||
| 83 | + | - function GreatestCommonDivisor(a, b: Int64): Int64; | |
| 84 | + | - returns the greates common divisor of a and b | |
| 85 | + | - function Floor(D: Double): Int64; overload; | |
| 86 | + | - returns the integer part of D (subtracted with 1, if (D \< 0)) | |
| 87 | + | ||
| 88 | + | ## Conversion routines | |
| 89 | + | ||
| 90 | + | - function FloatToFraction(Value, Precision: Double): TFraction; | |
| 91 | + | - returns an approximation of Value in the form of a fraction, e.g. FloatTofraction(0.5,0.01) returns 1/2 (one half) | |
| 92 | + | - Precision defines the acceptable value for which (Abs(Result) - Abs(Value) \<= Precision. | |
| 93 | + | - Precision is bound to Value by a magnitude of 15 at most | |
| 94 | + | - 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. | |
| 95 | + | - function TryFloatToFraction(Value, Precision: Double; out F: TFraction; AcceptPrecisionError: Boolean): Boolean; | |
| 96 | + | - return value depends on AcceptPrecisionError value: | |
| 97 | + | - if (AcceptPrecisionError = True) then the function retruns True if an approxiamtion can be found | |
| 98 | + | - if (AcceptPrecisionError = False) the functions returns True if an approximation can be found AND Abs(Abs(Result)-Abs(Value)) \<= Precision | |
| 99 | + | - the value of F only makes sense if the function returns True | |
| 100 | + | - function FloatToFractionDef(Value, Precision: Double; Def: TFraction; AcceptPrecisionError: Boolean): TFraction; | |
| 101 | + | - returns the found approximation (F) if TryFloatToFraction(Value, Precision, F, AcceptPrecisionError: Boolean) succeeds, otherwise returns Def | |
| 102 | + | - function StrToFraction(const S: String): TFraction; | |
| 103 | + | - returns fraction that is represented in S, raises an EConvertError upon failure | |
| 104 | + | - function TryStrToFraction(const S: String; out F: TFraction): Boolean; | |
| 105 | + | - retruns True if S can be converted to a fraction, otherwise returns False | |
| 106 | + | - the value of F only makes sense f the function returns True | |
| 107 | + | - function StrToFractionDef(const S: String; Def: TFraction): TFraction; | |
| 108 | + | - returns the fraction represented in S if conversion succeeds, otherwise returns Def | |
| 109 | + | ||
| 110 | + | ## Example program | |
| 111 | + | ||
| 112 | + | ``` pascal | |
| 113 | + | program example; | |
| 114 | + | ||
| 115 | + | {$mode objfpc}{$H+} | |
| 116 | + | {$apptype console} | |
| 117 | + | ||
| 118 | + | uses | |
| 119 | + | Classes, sysutils, fractions, math; | |
| 120 | + | ||
| 121 | + | var | |
| 122 | + | F1, F2: TFraction; | |
| 123 | + | D, Prec: Double; | |
| 124 | + | i: Integer; | |
| 125 | + | begin | |
| 126 | + | F1 := Fraction(1,1,3); // 1 1/3 | |
| 127 | + | F2 := Fraction(4,3); // 4/3 | |
| 128 | + | writeln('F1.ToString = ',F1.ToString); // '4/3' | |
| 129 | + | writeln('F1.Resolve = ',F1.Resolve); // '1 1/3' | |
| 130 | + | writeln('F1.ToFloat = ',F1.ToFloat:16:16); // 1.3333333333333333 | |
| 131 | + | writeln('F2.ToString = ',F2.ToString); // '4/3' | |
| 132 | + | writeln('(F1 = F2) = ',F1=F2); //True | |
| 133 | + | ||
| 134 | + | F1 := Fraction(1,2); | |
| 135 | + | F2 := Fraction(1,3); | |
| 136 | + | writeln(F1.ToString,' * ',F2.ToString,' = ',(F1*F2).Resolve); // '1/6' | |
| 137 | + | writeln(F1.ToString,' / ',F2.ToString,' = ',(F1/F2).Resolve); // '1 1/2' | |
| 138 | + | writeln(F1.ToString,' + ',F2.ToString,' = ',(F1+F2).Resolve); // '5/6' | |
| 139 | + | writeln(F1.ToString,' - ',F2.ToString,' = ',(F1-F2).Resolve); // '1/6' | |
| 140 | + | writeln(F1.ToString,' ** 2 = ',(F1**2).Resolve); // '1/6' | |
| 141 | + | ||
| 142 | + | D := 0.25; | |
| 143 | + | F1 := FloatToFraction(D, 0.000001); | |
| 144 | + | writeln('FloatTofraction(0.25) -> ',F1.ToString); // '1/4' | |
| 145 | + | writeln; | |
| 146 | + | writeln('Approximations of Pi:'); | |
| 147 | + | writeln(' [Pi = ',Pi:16:16,']'); | |
| 148 | + | Prec := 1.0; | |
| 149 | + | for i := 1 to 10 do | |
| 150 | + | begin | |
| 151 | + | Prec := Prec / 10; | |
| 152 | + | F2 := FloatTofraction(Pi, Prec); | |
| 153 | + | writeln('FloatTofraction(Pi,',Prec:10:10,') = ',Format('%-13s',[F2.Resolve]),' [',F2.ToFloat:16:16,']'); | |
| 154 | + | end; | |
| 155 | + | end. | |
| 156 | + | ``` | |
| 157 | + | ||
| 158 | + | This outputs: | |
| 159 | + | ||
| 160 | + | F1.ToString = 4/3 | |
| 161 | + | F1.Resolve = 1 1/3 | |
| 162 | + | F1.ToFloat = 1.3333333333333333 | |
| 163 | + | F2.ToString = 4/3 | |
| 164 | + | (F1 = F2) = TRUE | |
| 165 | + | 1/2 * 1/3 = 1/6 | |
| 166 | + | 1/2 / 1/3 = 1 1/2 | |
| 167 | + | 1/2 + 1/3 = 5/6 | |
| 168 | + | 1/2 - 1/3 = 1/6 | |
| 169 | + | 1/2 ** 2 = 1/4 | |
| 170 | + | FloatTofraction(0.25) -> 1/4 | |
| 171 | + | ||
| 172 | + | Approximations of Pi: | |
| 173 | + | [Pi = 3.1415926535897932] | |
| 174 | + | FloatTofraction(Pi,0.1000000000) = 3 1/7 [3.1428571428571428] | |
| 175 | + | FloatTofraction(Pi,0.0100000000) = 3 1/7 [3.1428571428571428] | |
| 176 | + | FloatTofraction(Pi,0.0010000000) = 3 16/113 [3.1415929203539825] | |
| 177 | + | FloatTofraction(Pi,0.0001000000) = 3 16/113 [3.1415929203539825] | |
| 178 | + | FloatTofraction(Pi,0.0000100000) = 3 16/113 [3.1415929203539825] | |
| 179 | + | FloatTofraction(Pi,0.0000010000) = 3 16/113 [3.1415929203539825] | |
| 180 | + | FloatTofraction(Pi,0.0000001000) = 3 4703/33215 [3.1415926539214212] | |
| 181 | + | FloatTofraction(Pi,0.0000000100) = 3 4703/33215 [3.1415926539214212] | |
| 182 | + | FloatTofraction(Pi,0.0000000010) = 3 4703/33215 [3.1415926539214212] | |
| 183 | + | FloatTofraction(Pi,0.0000000001) = 3 14093/99532 [3.1415926536189365] | |
| 184 | + | ||
| 185 | + | --- | |
| 186 | + | *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).* |
