TheDocumentation Index
Fetch the complete documentation index at: https://mintlify.com/X-ARTEMIS/ASTD/llms.txt
Use this file to discover all available pages before exploring further.
fraction<T, U> class template provides exact rational-number arithmetic using two independently typed members for the numerator and denominator. All arithmetic operations normalise operands to a common denominator via std::lcm before performing the calculation, ensuring correctness when the two fractions do not already share a denominator.
Template parameters
| Parameter | Role |
|---|---|
T | Type of the numerator |
U | Type of the denominator |
T and U must be integral types compatible with std::lcm and the standard arithmetic operators.
Constructor
The numerator of the fraction. Stored directly as the public member
numerator.The denominator of the fraction. Must be non-zero; passing
0 throws
std::exception. Stored as the public member denominator.Public data members
| Member | Type | Description |
|---|---|---|
numerator | T | The top part of the fraction |
denominator | U | The bottom part of the fraction (never 0 post-construction) |
Member functions
fractionToDecimal
double and performing floating-point division.
The decimal value of the fraction:
(double)numerator / (double)denominator.Compound assignment operators
All four compound assignment operators use LCM-based normalisation: when the two fractions have different denominators, the least common multiple of the two denominators is computed withstd::lcm, and each numerator is scaled by the appropriate factor before the operation is applied. This keeps results exact in integer arithmetic.
operator+=
fraction2 to *this in place and returns a reference to *this.
The fraction to add. Its numerator is scaled to the LCM denominator before
addition.
Reference to the modified
*this after addition.operator-=
fraction2 from *this in place and returns a reference to *this.
The fraction to subtract. Numerator is scaled to the LCM denominator before
subtraction.
Reference to the modified
*this after subtraction.operator*=
*this by fraction2 in place using LCM normalisation and returns a reference to *this.
The fraction to multiply by.
Reference to the modified
*this after multiplication.operator/=
*this by fraction2 in place using LCM normalisation and returns a reference to *this.
The fraction to divide by.
Reference to the modified
*this after division.Free operators
These non-member operators are provided for convenient expression syntax and are implemented in terms of their compound-assignment counterparts.operator+
frac + frac2 by delegating to operator+=.
operator-
frac - frac2 by delegating to operator-=.
operator*
frac * frac2 by delegating to operator*=.
operator<<
numerator/denominator.
The same output stream passed in, allowing chained
<< expressions.LCM normalisation explained
When two fractions have different denominators, direct numerator arithmetic would produce a wrong result. The operators first compute:scaledNum1 and scaledNum2 over commonDenom. This approach stays within integer arithmetic and avoids the rounding errors introduced by converting to floating-point early.
std::lcm is part of <numeric> (C++17), but the fraction header also
relies on const auto parameters which require C++20. Your toolchain
must target C++20 or later to use fraction.