Skip to content

Commit 0df64f0

Browse files
committed
Merge branch 'update-fractions-for-v0.2'
2 parents a029564 + 4e556ae commit 0df64f0

File tree

7 files changed

+119
-16
lines changed

7 files changed

+119
-16
lines changed

Fractions/0/API/TFraction-Abs.md

Lines changed: 30 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,19 +6,30 @@
66

77
**Record:** [_TFraction_](./TFraction.md)
88

9-
**Applies to:** ~>0.1
9+
**Applies to:** 0.1.x
1010

1111
```pascal
1212
class function Abs(const F: TFraction): TFraction; static;
1313
```
1414

15+
**Applies to:** ~>0.2
16+
17+
```pascal
18+
class function Abs(const F: TFraction): TFraction; overload; static;
19+
function Abs: TFraction; overload;
20+
```
21+
1522
## Description
1623

17-
Returns the absolute value of the given fraction _F_.
24+
Returns the absolute value of a fraction. The returned fraction is not simplified.
25+
26+
The class method operates on the fraction passed as parameter _F_.
1827

19-
The returned fraction is not simplified.
28+
[~>0.2] The instance method operates upon the fraction instance on which it is called.
2029

21-
### Example
30+
### Examples
31+
32+
Using the class method:
2233

2334
```pascal
2435
var
@@ -33,6 +44,21 @@ begin
3344
end;
3445
```
3546

47+
[~>0.2] Using the instance method:
48+
49+
```pascal
50+
var
51+
F, G: TFraction;
52+
begin
53+
F := TFraction.Create(3, 4);
54+
G := F.Abs;
55+
Assert(G = +F);
56+
F := TFraction.Create(-7, 8);
57+
G := F.Abs;
58+
Assert(G = -F);
59+
end;
60+
```
61+
3662
## See Also
3763

3864
* [_Sign_](./TFraction-Sign.md) method

Fractions/0/API/TFraction-Create.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,4 +82,5 @@ end;
8282

8383
## See Also
8484

85-
* [_Implicit_](./TFraction-Implicit.md) operator overload
85+
* [_Implicit_](./TFraction-Implicit.md) operator overload.
86+
* _[Intialize](./TFraction-Initialize.md) [~>0.2]_ operator overload (Delphi 10.4 and later only).

Fractions/0/API/TFraction-Hash.md

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
# Hash method
2+
3+
**Project:** [Fractions](../API.md)
4+
5+
**Unit:** _DelphiDabbler.Lib.Fractions_
6+
7+
**Record:** [_TFraction_](./TFraction.md)
8+
9+
**Applies to:** ~>0.2
10+
11+
```pascal
12+
function Hash: Integer;
13+
```
14+
15+
## Description
16+
17+
Computes and returns a hash value for the fraction as an _Integer_.
18+
19+
If two fractions that simplify to the same fraction have the same hash.
20+
21+
### Example
22+
23+
```pascal
24+
var
25+
F, G: TFraction;
26+
begin
27+
F := TFraction.Create(1, 2);
28+
G := TFraction.Create(3, 6);
29+
Assert(F.Hash = G.Hash);
30+
end;
31+
```
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
# Initialize operator overload
2+
3+
**Project:** [Fractions](../API.md)
4+
5+
**Unit:** _DelphiDabbler.Lib.Fractions_
6+
7+
**Record:** [_TFraction_](./TFraction.md)
8+
9+
**Applies to:** ~>0.2
10+
11+
```pascal
12+
class operator Initialize(out Dest: TFraction);
13+
```
14+
15+
## Description
16+
17+
> **This operator overload is conditionally compiled for Delphi 10.4 and later only.**
18+
19+
_Initialize_ is called automatically whenever a new _TFraction_ record is created. It sets the values of the _[Numerator](./TFraction-Numerator.md)_ and _[Denominator](./TFraction-Denominator.md)_ properties to `0` and `1` respectively, to correctly represent a zero fraction.
20+
21+
⚠️ When compiled with Delphi version prior to 10.4 the initial values of _[Numerator](./TFraction-Numerator.md)_ and _[Denominator](./TFraction-Denominator.md)_ properties are undefined.
22+
23+
_Initialize_ should never be called explicitly.
24+
25+
## See Also
26+
27+
* [_Create_](./TFraction-Create.md) constructor

Fractions/0/API/TFraction-Power.md

Lines changed: 19 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,23 +6,34 @@
66

77
**Record:** [_TFraction_](./TFraction.md)
88

9-
**Applies to:** ~>0.1
9+
**Applies to:** 0.1.x
1010

1111
```pascal
1212
class function Power(const F: TFraction; Exponent: ShortInt): TFraction; static;
1313
```
14+
**Applies to:** ~>0.2
15+
16+
```pascal
17+
class function Power(const F: TFraction; Exponent: ShortInt): TFraction;
18+
overload; static;
19+
function Power(const Exponent: ShortInt): TFraction; overload;
20+
```
1421

1522
## Description
1623

17-
Returns the given fraction _F_ raised to the power of integer _Exponent_.
24+
Return a fraction raised to an integer power specified by the _Exponent_ parameter. The resulting fraction is simplified.
25+
26+
_Exponent_ can be positive, zero or negative except unless the fraction is zero when _Exponent_ must be non-negative.
27+
28+
When _Exponent_ is negative the reciprocal of _F_ is raised to the power of the absolute value of _Exponent_. This is because, for any number `n`, `n^-x = 1/(N^x)`.
1829

19-
_Exponent_ can be positive, zero or negative. When _Exponent_ is negative the reciprocal of _F_ is raised to the power of the absolute value of _Exponent_. This is because, for any number `n`, `n^-x = 1/(N^x)`.
30+
The class method takes the fraction to be operated on, _F_, as its first parameter.
2031

21-
If _F_ is zero, _Exponent_ must be non-negative.
32+
[~>0.2] The instance method operates on the fraction instance upon which it is called.
2233

23-
The resulting fraction is simplified.
34+
### Examples
2435

25-
### Examples
36+
#### Class method examples
2637

2738
Here's a simple function that squares the given fraction.
2839

@@ -33,7 +44,7 @@ begin
3344
end;
3445
```
3546

36-
And here is some code that write out all the integer powers of 1/2 from -32 to +32:
47+
[~>0.2] Here is some code that write out all the integer powers of 1/2 from -32 to +32:
3748

3849
```pascal
3950
var
@@ -43,7 +54,7 @@ begin
4354
F := TFraction.Create(1, 2); // 1/2
4455
for N := -32 to 32 do
4556
begin
46-
G := TFraction.Power(F, N);
57+
G := F.Power(N);
4758
WriteLn(
4859
Format(
4960
'1/2^%d = %d/%d', [N, G.Numerator, G.Denominator]

Fractions/0/API/TFraction.md

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,9 @@ Furthermore a _TFraction_ variable can be instantiated by assigning a variable o
2121
* Another _TFraction_. Properties are simply copied.
2222
* Any integer type. This creates a fractions with numerator equal to the integer and denominator 1.
2323
* A _Single_, _Double_ or _Extended_ floating point type. The floating point value is converted to a fraction to an accuracy of 5 decimal places. The floating point value must be 0 or have an absolute value in the range 1.0e-19 to 1.0e+19.
24+
* [~>0.2] **When compiled with Delphi 10.4 or later only:** simply declaring a _TFraction_ variable will instantiate it as if `0` had been assigned to it.
25+
26+
⚠️ Never use a _TFraction_ variable that has not been instantiated since its value is undefined.
2427

2528
### Properties
2629

@@ -56,6 +59,7 @@ The operators are overloaded by the record
5659
| [_Modulus_](./TFraction-Modulus.md) | Enables the modulus operator (`mod`) to be used to get the fractional remainder after dividing one _TFraction_ record by another. |
5760
| [_Round_](./TFraction-Round.md) | Enables the `Round` operator to round a _TFraction_ record to the nearest whole number value. |
5861
| [_Trunc_](./TFraction-Trunc.md) | Enables the `Trunc` operator to truncate a _TFraction_ record to the nearest whole number in the direction of zero. |
62+
| _[Initialize](./TFraction-Initialize.md) [~>0.2]_ | When compiled with Delphi 10.4 or later, automatically initialises _TFraction_ records with _[Numerator](./TFraction-Numerator.md)_ = `0` and _[Denominator](./TFraction-Denominator.md)_ = `1`. |
5963

6064
### Methods
6165

@@ -71,11 +75,14 @@ _TFraction_ defines several methods. Some are static class methods that operate
7175

7276
| Method | Description |
7377
|:-------|:------------|
78+
| _[Abs](./TFraction-Abs.md) [~>0.2]_ | Returns the absolute value of the fraction (overloaded with class method). |
7479
| [_CompareTo_](./TFraction-CompareTo.md) | Compares the fraction to another and returns a value indicating which fraction is greatest or if they are equal. |
7580
| [_Convert_](./TFraction-Convert.md) | Converts the fraction into an equivalent one in which the numerator and denominator are a given integer multiple of their original values. |
7681
| [_HasCommonFactor_](./TFraction-HasCommonFactor.md) | Checks if a given integer is a common factor of the fraction. |
82+
| _[Hash](./TFraction-Hash.md) [~>0.2]_ | Computes a hash for the fraction. |
7783
| [_IsProper_](./TFraction-IsProper.md) | Checks if the fraction is a proper fraction. |
7884
| [_IsWholeNumber_](./TFraction-IsWholeNumber.md) | Checks if the fraction represents a whole number. |
85+
| _[Power](./TFraction-Power.md) [~>0.2]_ | Computes an integer power of a fraction (overloaded with class method). |
7986
| [_Reciprocal_](./TFraction-Reciprocal.md) | Returns the reciprocal of the fraction. |
8087
| [_RoundToMultiple_](./TFraction-RoundToMultiple.md) | Rounds the fraction to the nearest whole number multiple of another fraction. |
8188
| [_Sign_](./TFraction-Sign.md) | Returns a value representing the sign of the fraction. |
@@ -86,9 +93,9 @@ _TFraction_ defines several methods. Some are static class methods that operate
8693

8794
| Method | Description |
8895
|:-------|:------------|
89-
| [_Abs_](./TFraction-Abs.md) | Returns the absolute value of a given fraction. |
96+
| [_Abs_](./TFraction-Abs.md) | Returns the absolute value of a given fraction ([~>0.2] overloaded with instance method). |
9097
| [_Compare_](./TFraction-Compare.md) | Compares two fractions and returns a value indicating which of the two is greater or if they equal. |
9198
| [_LCD_](./TFraction-LCD.md) | Computes the least common denominator of two fractions. |
9299
| [_Max_](./TFraction-Max.md) | Overloaded methods that find the largest of two or more fractions. |
93100
| [_Min_](./TFraction-Min.md) | Overloaded methods that find the smallest of two or more fractions. |
94-
| [_Power_](./TFraction-Power.md) | Computes an integer power of a fraction. |
101+
| [_Power_](./TFraction-Power.md) | Computes an integer power of a fraction ([~>0.2] overloaded with instance method). |

Fractions/0/Overview.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ Operator overloading is used extensively to enable normal arithmetic operations
1414
1515
## Compatibility
1616

17-
This unit is designed for compilation with 32 bit and 64 bit Windows Delphi compilers. It is designed to compile with compilers from Delphi 2009 onwards.
17+
This unit is designed for compilation with 32 bit and 64 bit Windows Delphi compilers. It is designed to compile with compilers from Delphi XE onwards. It may compile with Delphi 2009 and Delphi 2010, but this is not tested and not guaranteed.
1818

1919
The code uses only Delphi RTL libraries and so should be able to compile for any platform supported by Delphi.
2020

0 commit comments

Comments
 (0)