Welcome to the new DelphiDabbler Code Library Documentation.

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.

TFraction record

Project: Fractions

Unit: DelphiDabbler.Lib.Fractions

Applies to: ~>0.1

Description

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.

Creating a TFraction instance

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:

Properties

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.

Operator overloads

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.

Methods

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.

Constructor

Method Description
Create Constructs a TFraction instance from a given numerator and denominator.

Instance Methods

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.

Class Methods

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.