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

Project: Fractions

Unit: DelphiDabbler.Lib.Fractions

Record: TFraction

Applies to: ~>0.1

``````class operator Implicit(const I: Integer): TFraction;
class operator Implicit(const F: TFraction): Extended;
class operator Implicit(const E: Extended): TFraction;
``````

## Description

TFraction defines three implicit casts:

### Integer to TFraction

``````class operator Implicit(const I: Integer): TFraction;
``````

This cast enables any integer value to be cast as a TFraction that represents a whole number.

The cast works with any signed or unsigned integer type up to 32 bits wide, but not 64 bit integer types.

Both implicit and explicit casting are supported.

The TFraction record created when an integer is cast to it has its Numerator property set to the same value as the integer. The Denominator property is always set to 1.

A consequence of this cast is that integer values can be assigned, or explicitly cast to a TFraction. Integers can also be passed as parameters where a TFraction is expected, including as elements of dynamic arrays of TFraction.

#### Integer to TFraction Example

``````var
F: TFraction;
I: Integer;
N, D: Int64;
begin
I := 42;
F := I;   // implicit cast of integer variable
Assert((F.Numerator = 42) and (F.Denominator = 1));
F := -56; // implicit cast of integer literal
Assert((F.Numerator = -56) and (F.Denominator = 1));
N := TFraction(I).Numerator;    // explicit casts
D := TFraction(I).Denominator;
Assert((N = I) and (D = 1));
end;
``````

#### Integer to TFraction Note

The reverse operation is not defined, so that a TFraction instance canâ€™t be cast to an Integer. This is because information would most probably be lost.

To convert a TFraction to an integer use the Trunc or Round operators.

### TFraction to Extended

``````class operator Implicit(const F: TFraction): Extended;
``````

This cast enables a TFraction instance to be converted to its floating point number representation.

Although defined for the Extended floating point type, the cast works equally well for the Single, Double and Extended types.

Both implicit and explicit casting is supported.

A consequence of this cast is that TFraction instances can be passed as parameters to methods whenever a floating point input parameter is expected.

#### TFraction to Extended Example

``````var
F: TFraction;
E: Extended;
D: Double;
begin
F := TFraction.Create(2, 8);  // 2/8 or 1/4
E := F;
Assert(Math.SameValue(E, 0.25));
D := Double(F); // same effect as D := F;
Assert(Math.SameValue(D, 0.25));
Assert(Math.SameValue(F, 0.25)); // F acts as floating point parameter
``````

### Extended to TFraction

``````class operator Implicit(const E: Extended): TFraction;
``````

This cast enables a floating point value to be cast to a TFraction.

Although defined for the Extended floating point type, the cast works equally well for the Single, Double and Extended types.

Both implicit and explicit casting is supported.

A consequence of this cast is that, subject to the restrictions below, floating point values can be assigned, or explicitly cast to a TFraction. Floats can also be passed as parameters where a TFraction is expected, including as elements of dynamic arrays of TFraction.

#### Extended to TFraction Restrictions

1. The absolute value of floating point value being cast must be in the range 1.0E-19 to 1.0E+19, or be 0.
2. Conversions are accurate to 5 decimal places. Any further decimal places are ignored.

#### Extended to TFraction Example

``````var
E: Extended;
D: Double;
F: TFraction;
begin
E := -4 / 7;
F := E;             // variable assignment
Assert((F.Numerator = -4) and (F.Denominator = 7));
D := 0.75
F := TFraction(D);  // explicit cast
Assert((F.Numerator = 3) and (F.Denominator = 4));
F := 1.07407407;    // floating point literal assignment
Assert((F.Numerator = 29) and (F.Denominator = 27));
end;
``````