## 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.

# Create constructor

Project: Fractions

Unit: DelphiDabbler.Lib.Fractions

Record: TFraction

Applies to: ~>0.1

``````constructor Create(const Numerator, Denominator: Int64);
``````

## Description

This constructor creates a TFraction with the given numerator and denominator.

The Numerator and Denominator parameters are “normalised” before being assigned to the fraction’s Numerator and Denominator properties.

“Normalisation” ensures that Numerator always takes the sign of the fraction and Denominator is always a positive integer. This means that if a negative value is passed to Denominator then the signs of both Numerator and Denominator are reversed. It follows that if both Numerator and Denominator and negative then both are made positive.

It is an error if Denominator is zero.

Fractions are not simplified in the constructor.

Because TFraction is an invariant record to only way to set the Numerator and Denominator properties is via the construction process. They retain the same value through the life of the record instance.

### Implicit construction

Due to the magic of the Implicit operator overload, a TFraction instance can be created simply by assigning a floating point or integer value to a TFraction variable, for example:

``````var
F, G: TFraction;
begin
F := 42;    // integer assignment
G := 3/4;   // floating point assignment (3/4 = 0.75)
end;
``````

In the case of integer assignment the Implicit operator overload calls Create internally, passing the integer value in the Numerator parameter and 1 to thr Denominator parameter.

For floating point assignments, Implicit calculates the numerator and denominator of the best fractional approximation to the floating point value, within five decimal places. Create is again called internally and the calculated values are passed to Numerator and Denominator parameters.

It is tempting to use the floating point assignment to enable fraction to assign values expressed using the divide operator to avoid calling Create explicitly, as in.

``````G := 3/4;
``````

``````G := TFraction.Create(3, 4);
``````

Be aware though that `3/4` is actually converted to a floating point value before being converted to a TFraction. This is not only quite a slow operation but it is potentially inaccurate, especially for fractions with large numerators and denominators because rounding errors can occur.

### Note

The WholeNumberPart and FractionalPart properties are not set explicitly: they are calculated from the Numerator and Denominator properties.

### Example

``````var
F1, F2, F3, F4, F5: TFraction;
begin
F1 := TFraction.Create(3, 8);
F2 := TFraction.Create(-3, 8);
F3 := TFraction.Create(3, -8);
F4 := TFraction.Create(-3, -8);
F4 := TFraction.Create(0 -8);
Assert((F1.Numerator = 3) and (F1.Denominator = 8));
Assert((F2.Numerator = -3) and (F2.Denominator = 8));
Assert((F3.Numerator = -3) and (F3.Denominator = 8));
Assert((F4.Numerator = 3) and (F4.Denominator = 8));
Assert((F4.Numerator = 0) and (F4.Denominator = 8));
end;
``````