This is a new site that's currently running on alpha code. There are going to be bugs. If you discover any, please report them on the site's issues page (GitHub account required). Thanks.

**Warning:** Many URLs *are* going to change. Refer to the README file to discover which library project's documentation has been completed.

**Project:** Fractions

**Unit:** *DelphiDabbler.Lib.Fractions*

**Applies to:** ~>0.1

*TFraction* is an advanced “record with methods” encapsulates a vulgar fraction and defines various mathematical operations on it.

The record is immutable, i.e. its properties are read-only and are never modified by any operations on the record. Any methods that manipulate a fraction always return a new record instance containing the result of the manipulation.

A variable of type *TFraction* can be instantiated by passing the numerator and denominator to the record’s constructor. The fraction is normalised by the constructor, so that the denominator is always positive and the numerator takes the sign.

Furthermore a *TFraction* variable can be instantiated by assigning a variable of a valid type to it. Supported types are:

- Another
*TFraction*. Properties are simply copied. - Any integer type. This creates a fractions with numerator equal to the integer and denominator 1.
- 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.

The following read-only properties are exposed.

Property | Description |
---|---|

Numerator |
The fraction’s numerator. |

Denominator |
The fraction’s denominator. |

WholeNumberPart |
The whole number part of the fraction when considered as a mixed fraction. |

FractionalPart |
The fractional part of the fraction when considered as a mixed fraction. |

The operators are overloaded by the record

Operator | Description |
---|---|

Implicit |
Enables a TFraction record to be cast / assigned from integer and floating point types and to floating point types (not integers). |

Equal |
Enables two TFraction records to be compared for equality using the `=` operator. |

NotEqual |
Enables two TFraction records to be compared for inequality using the `<>` operator. |

LessThan |
Enables two TFraction records to be compared using the less-than (`<` ) operator. |

LessThanOrEqual |
Enables two TFraction records to be compared using the less-than-or-equal (`<=` ) operator. |

GreaterThan |
Enables two TFraction records to be compared using the greater-than (`>` ) operator. |

GreaterThanOrEqual |
Enables two TFraction records to be compared using the greater-than-or-equal (`>=` ) operator. |

Negative |
Enables the unary minus operator to be used to negate a TFraction record. |

Positive |
Enables the unary plus operator to be applied to a TFraction record. |

Add |
Enables the addition operator (`+` ) to be used to add two TFraction records. |

Subtract |
Enables the subtraction operator (`-` ) to be used to subtract two TFraction records. |

Multiply |
Enables the multiplication operator (`*` ) to be used to multiple two TFraction records. |

Divide |
Enables the division operator (`/` ) to be used to divide to TFraction records. |

IntDivide |
Enables the integer division operator (`div` ) to be used to divide two integers giving an integer result. |

Modulus |
Enables the modulus operator (`mod` ) to be used to get the fractional remainder after dividing one TFraction record by another. |

Round |
Enables the `Round` operator to round a TFraction record to the nearest whole number value. |

Trunc |
Enables the `Trunc` operator to truncate a TFraction record to the nearest whole number in the direction of zero. |

*TFraction* defines several methods. Some are static class methods that operate on fractions passed as parameters while others are instance methods that operate on the current record. There is also a single constructor.

Method | Description |
---|---|

Create |
Constructs a TFraction instance from a given numerator and denominator. |

Method | Description |
---|---|

CompareTo |
Compares the fraction to another and returns a value indicating which fraction is greatest or if they are equal. |

Convert |
Converts the fraction into an equivalent one in which the numerator and denominator are a given integer multiple of their original values. |

HasCommonFactor |
Checks if a given integer is a common factor of the fraction. |

IsProper |
Checks if the fraction is a proper fraction. |

IsWholeNumber |
Checks if the fraction represents a whole number. |

Reciprocal |
Returns the reciprocal of the fraction. |

RoundToMultiple |
Rounds the fraction to the nearest whole number multiple of another fraction. |

Sign |
Returns a value representing the sign of the fraction. |

Simplify |
Two overloaded methods that reduce the fraction either to its common terms or by a given common factor. |

TruncateToMultiple |
Truncates the fraction to a whole number multiple of another fraction. |

Method | Description |
---|---|

Abs |
Returns the absolute value of a given fraction. |

Compare |
Compares two fractions and returns a value indicating which of the two is greater or if they equal. |

LCD |
Computes the least common denominator of two fractions. |

Max |
Overloaded methods that find the largest of two or more fractions. |

Min |
Overloaded methods that find the smallest of two or more fractions. |

Power |
Computes an integer power of a fraction. |