Skip to main content

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

The 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.
#include "astd/math/math.hpp"
// or via the umbrella header:
#include "astd/astd.hpp"

Template parameters

ParameterRole
TType of the numerator
UType of the denominator
Both T and U must be integral types compatible with std::lcm and the standard arithmetic operators.

Constructor

fraction(T numerator, U denominator);
Constructs a fraction with the given numerator and denominator, storing both as public members.
If denominator is 0, the constructor throws std::exception with the message "Undefined fraction". Always validate user-supplied denominators before construction.
numerator
T
required
The numerator of the fraction. Stored directly as the public member numerator.
denominator
U
required
The denominator of the fraction. Must be non-zero; passing 0 throws std::exception. Stored as the public member denominator.

Public data members

MemberTypeDescription
numeratorTThe top part of the fraction
denominatorUThe bottom part of the fraction (never 0 post-construction)

Member functions

fractionToDecimal

double fractionToDecimal();
Converts the fraction to its decimal equivalent by casting both members to double and performing floating-point division.
result
double
The decimal value of the fraction: (double)numerator / (double)denominator.
fraction<int, int> half{ 1, 2 };
double d = half.fractionToDecimal(); // 0.5

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 with std::lcm, and each numerator is scaled by the appropriate factor before the operation is applied. This keeps results exact in integer arithmetic.

operator+=

fraction& operator+=(fraction& fraction2);
Adds fraction2 to *this in place and returns a reference to *this.
fraction2
fraction&
required
The fraction to add. Its numerator is scaled to the LCM denominator before addition.
result
fraction&
Reference to the modified *this after addition.

operator-=

fraction& operator-=(fraction& fraction2);
Subtracts fraction2 from *this in place and returns a reference to *this.
fraction2
fraction&
required
The fraction to subtract. Numerator is scaled to the LCM denominator before subtraction.
result
fraction&
Reference to the modified *this after subtraction.

operator*=

fraction& operator*=(fraction& fraction2);
Multiplies *this by fraction2 in place using LCM normalisation and returns a reference to *this.
fraction2
fraction&
required
The fraction to multiply by.
result
fraction&
Reference to the modified *this after multiplication.

operator/=

fraction& operator/=(fraction& fraction2);
Divides *this by fraction2 in place using LCM normalisation and returns a reference to *this.
fraction2
fraction&
required
The fraction to divide by.
result
fraction&
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+

template<...>
fraction<A, B>& operator+(fraction<C, D>& frac, fraction<E, F> frac2);
Returns a new fraction equal to frac + frac2 by delegating to operator+=.

operator-

template<...>
fraction<G, H>& operator-(fraction<I, J>& frac, fraction<K, L> frac2);
Returns a new fraction equal to frac - frac2 by delegating to operator-=.

operator*

template<...>
fraction<M, N>& operator*(fraction<O, P> frac, fraction<Q, R> frac2);
Returns a new fraction equal to frac * frac2 by delegating to operator*=.

operator<<

template<typename S, typename V>
std::ostream& operator<<(std::ostream& out, fraction<S, V> frac);
Writes the fraction to an output stream in the form numerator/denominator.
out
std::ostream&
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:
auto commonDenom = std::lcm(denom1, denom2);
auto scaledNum1  = num1 * (commonDenom / denom1);
auto scaledNum2  = num2 * (commonDenom / denom2);
The operation is then applied to 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.

Full usage example

#include "astd/math/math.hpp"
#include <iostream>

int main() {
    // Construction
    fraction<int, int> a{ 1, 3 };  // 1/3
    fraction<int, int> b{ 1, 6 };  // 1/6

    // Stream output
    std::cout << "a = " << a << '\n'; // a = 1/3
    std::cout << "b = " << b << '\n'; // b = 1/6

    // Decimal conversion
    std::cout << "a as decimal: " << a.fractionToDecimal() << '\n'; // 0.333...

    // Addition: LCM(3, 6) = 6  →  2/6 + 1/6 = 3/6
    fraction<int, int> sum = a + b;
    std::cout << "a + b = " << sum << '\n'; // 3/6

    // Subtraction: 2/6 - 1/6 = 1/6
    fraction<int, int> diff = a - b;
    std::cout << "a - b = " << diff << '\n'; // 1/6

    // Multiplication
    fraction<int, int> prod = a * b;
    std::cout << "a * b = " << prod << '\n';

    // Compound assignment
    a += b;
    std::cout << "a after += b: " << a << '\n';

    // Exception on zero denominator
    try {
        fraction<int, int> bad{ 1, 0 }; // throws std::exception
    } catch (const std::exception& e) {
        std::cout << "Caught: " << e.what() << '\n'; // Undefined fraction
    }
}

Build docs developers (and LLMs) love