template<typename T>
fractions::Fraction struct

Fraction.

Template parameters
T

Example:

Fraction<int> f(1, 2);
f.numer() = 1;
f.denom() = 2;

Public functions

auto Fraction(T numer, T denom) -> CONSTEXPR14
denominator
auto normalize() -> T -> CONSTEXPR14 auto
auto keep_denom_positive() -> CONSTEXPR14 void
auto reduce() -> T -> CONSTEXPR14 auto
auto Fraction(T&& numer) -> CONSTEXPR14 explicit
auto Fraction(const T& numer) -> CONSTEXPR14 explicit
auto Fraction() -> CONSTEXPR14
auto numer() const noexcept -> const T & -> CONSTEXPR14 auto
auto denom() const noexcept -> const T & -> CONSTEXPR14 auto
auto cross(const Fraction& rhs) const -> T -> CONSTEXPR14 auto
auto reciprocal() -> CONSTEXPR14 void
Inverts the numerator and denominator of the fraction to calculate its reciprocal.
auto operator*=(Fraction rhs) -> Fraction & -> CONSTEXPR14 auto
auto operator*=(T rhs) -> Fraction & -> CONSTEXPR14 auto
auto operator/=(Fraction rhs) -> Fraction & -> CONSTEXPR14 auto
auto operator/=(T rhs) -> Fraction & -> CONSTEXPR14 auto
auto operator-() const -> Fraction -> CONSTEXPR14 auto
auto operator+(const Fraction& other) const -> Fraction -> CONSTEXPR14 auto
auto operator-(const Fraction& other) const -> Fraction -> CONSTEXPR14 auto
auto operator-(const T& rhs) const -> Fraction -> CONSTEXPR14 auto
auto operator+=(const Fraction& rhs) -> Fraction & -> CONSTEXPR14 auto
auto operator-=(const Fraction& rhs) -> Fraction & -> CONSTEXPR14 auto
auto operator+=(const T& rhs) -> Fraction & -> CONSTEXPR14 auto
auto operator++() -> Fraction & -> CONSTEXPR14 auto
auto operator--() -> Fraction & -> CONSTEXPR14 auto
auto operator++(int) -> Fraction -> CONSTEXPR14 auto
auto operator--(int) -> Fraction -> CONSTEXPR14 auto
auto operator-=(const T& rhs) -> Fraction & -> auto

Public variables

T _numer
T _denom
numerator

Comparison operators

==, !=, <, >, <=, >= etc.

auto operator!=(const Fraction& rhs) const -> bool -> CONSTEXPR14 auto
auto operator>(const Fraction& rhs) const -> bool -> CONSTEXPR14 auto
auto operator>=(const Fraction& rhs) const -> bool -> CONSTEXPR14 auto
auto operator<=(const Fraction& rhs) const -> bool -> CONSTEXPR14 auto
auto operator>(const T& rhs) const -> bool -> CONSTEXPR14 auto
auto operator<=(const T& rhs) const -> bool -> CONSTEXPR14 auto
auto operator>=(const T& rhs) const -> bool -> CONSTEXPR14 auto
auto operator==(const Fraction& lhs, const T& rhs) -> bool -> CONSTEXPR14 auto
auto operator<(const Fraction& lhs, const T& rhs) -> bool -> CONSTEXPR14 auto
auto operator<(const T& lhs, const Fraction& rhs) -> bool -> CONSTEXPR14 auto
auto operator==(const T& lhs, const Fraction& rhs) -> bool -> CONSTEXPR14 auto
auto operator==(const Fraction& lhs, const Fraction& rhs) -> bool -> CONSTEXPR14 auto
auto operator<(const Fraction& lhs, const Fraction& rhs) -> bool -> CONSTEXPR14 auto
auto operator>(const T& lhs, const Fraction& rhs) -> bool -> CONSTEXPR14 auto
auto operator<=(const T& lhs, const Fraction& rhs) -> bool -> CONSTEXPR14 auto
auto operator>=(const T& lhs, const Fraction& rhs) -> bool -> CONSTEXPR14 auto

Friends

auto operator*(Fraction lhs, const Fraction& rhs) -> Fraction -> CONSTEXPR14 auto
auto operator*(Fraction lhs, const T& rhs) -> Fraction -> CONSTEXPR14 auto
auto operator*(const T& lhs, Fraction rhs) -> Fraction -> CONSTEXPR14 auto
auto operator/(Fraction lhs, const Fraction& rhs) -> Fraction -> CONSTEXPR14 auto
auto operator/(Fraction lhs, const T& rhs) -> Fraction -> CONSTEXPR14 auto
auto operator/(const T& lhs, Fraction rhs) -> Fraction -> CONSTEXPR14 auto
auto operator+(Fraction lhs, const T& rhs) -> Fraction -> CONSTEXPR14 auto
auto operator+(const T& lhs, Fraction rhs) -> Fraction -> CONSTEXPR14 auto
auto operator-(const T& c, const Fraction& frac) -> Fraction -> CONSTEXPR14 auto
auto operator+(int&& c, const Fraction& frac) -> Fraction -> CONSTEXPR14 auto
auto operator-(int&& c, const Fraction& frac) -> Fraction -> CONSTEXPR14 auto
auto operator*(int&& c, const Fraction& frac) -> Fraction -> CONSTEXPR14 auto
template<typename _Stream>
auto operator<<(_Stream& os, const Fraction& frac) -> _Stream & -> auto

Function documentation

template<typename T>
CONSTEXPR14 fractions::Fraction<T>::Fraction(T numer, T denom)

denominator

Parameters
numer in The numerator
denom in The denominator

Constructs a new Fraction object from the given numerator and denominator.

Normalizes the fraction after construction.

Example: Fraction<int> f(1, 2); // f = 1/2

template<typename T>
CONSTEXPR14 auto fractions::Fraction<T>::normalize() -> T

Normalizes the fraction to a canonical form where the denominator is always non-negative and co-prime with the numerator.

template<typename T>
CONSTEXPR14 void fractions::Fraction<T>::keep_denom_positive()

Normalizes the fraction by making the denominator positive. If the denominator is negative, both numerator and denominator are negated. This ensures the denominator is always positive.

template<typename T>
CONSTEXPR14 auto fractions::Fraction<T>::reduce() -> T

Normalizes the fraction to a canonical form by making the numerator and denominator coprime. The gcd of the numerator and denominator is computed and used to divide out any common factors.

Example:

Fraction<int> f(2, 6); // f = 1/3
f.reduce();

template<typename T>
CONSTEXPR14 fractions::Fraction<T>::Fraction(T&& numer) explicit

Parameters
numer in The numerator.

Constructs a new Fraction object from the given numerator.

The denominator is initialized to 1.

Example:

Fraction<int> f(2); // f = 2/1
assert(f.numer() == 2 && f.denom() == 1);

template<typename T>
CONSTEXPR14 fractions::Fraction<T>::Fraction(const T& numer) explicit

Parameters
numer in The numerator.

Constructs a new Fraction object from the given numerator.

The denominator is initialized to 1.

Example:

Fraction<int> f(2); // f = 2/1
assert(f == Fraction<int>(2, 1));

template<typename T>
CONSTEXPR14 fractions::Fraction<T>::Fraction()

Constructs a new Fraction object with numerator initialized to 0 and denominator initialized to 1.

Example:

Fraction<int> f; // f = 0/1
assert(f.numer() == 0);

template<typename T>
CONSTEXPR14 auto fractions::Fraction<T>::numer() const noexcept -> const T &

Returns A const reference to the numerator.

Gets the numerator of the fraction.

template<typename T>
CONSTEXPR14 auto fractions::Fraction<T>::denom() const noexcept -> const T &

Returns A const reference to the denominator.

Gets the denominator of the fraction.

template<typename T>
CONSTEXPR14 auto fractions::Fraction<T>::cross(const Fraction& rhs) const -> T

Parameters
rhs in The right-hand fraction to compute the cross product with.
Returns The computed cross product.

Computes the cross product of this fraction and another fraction.

The cross product is defined as: cross(a/b, c/d) = a*d - b*c

This can be used to compute the determinant of a 2x2 matrix with the two fractions as the entries on the diagonal.

Example:

cross(Fraction(1,2), Fraction(3,4)) = -1 cross(Fraction(1,2), Fraction(1,2)) = 0 cross(Fraction(1,2), Fraction(-1,2)) = 0

template<typename T>
CONSTEXPR14 auto fractions::Fraction<T>::operator*=(Fraction rhs) -> Fraction &

Parameters
rhs The Fraction to multiply.
Returns A reference to this Fraction after multiplication.

Multiplies this Fraction by the given Fraction rhs and assigns the result to this Fraction.

Normalizes this Fraction and rhs before multiplying. Updates the numerator and denominator of this Fraction with the result.

template<typename T>
CONSTEXPR14 auto fractions::Fraction<T>::operator*=(T rhs) -> Fraction &

Parameters
rhs The integer to multiply.
Returns A reference to this Fraction after multiplication.

Multiplies this Fraction by the given integer rhs and assigns the result to this Fraction.

Normalizes this Fraction before multiplying. Updates the numerator of this Fraction with the result and retains the existing denominator.

template<typename T>
CONSTEXPR14 auto fractions::Fraction<T>::operator/=(Fraction rhs) -> Fraction &

Parameters
rhs The Fraction to divide by.
Returns A reference to this Fraction after division.

Divides this Fraction by the given Fraction rhs and assigns the result to this Fraction.

Swaps the denominator of this Fraction with the numerator of rhs. Normalizes this Fraction and rhs before dividing. Updates the numerator and denominator of this Fraction with the result.

template<typename T>
CONSTEXPR14 auto fractions::Fraction<T>::operator/=(T rhs) -> Fraction &

Parameters
rhs The integer to divide this Fraction by.
Returns A reference to this Fraction after dividing by rhs.

Divides this Fraction by the integer rhs and assigns the result to this Fraction.

Divides the numerator of this Fraction by rhs and updates it with the result.

template<typename T>
CONSTEXPR14 auto fractions::Fraction<T>::operator-() const -> Fraction

Negates this Fraction by negating its numerator.

Returns a new Fraction with the negated numerator.

template<typename T>
CONSTEXPR14 auto fractions::Fraction<T>::operator+(const Fraction& other) const -> Fraction

Adds this Fraction to the Fraction rhs.

Finds the lowest common denominator and converts both Fractions to it. Then adds the numerators and returns a new Fraction with the result. Handles zero denominators by returning a Fraction with a zero denominator.

template<typename T>
CONSTEXPR14 auto fractions::Fraction<T>::operator-(const Fraction& other) const -> Fraction

Parameters
other in The Fraction to subtract from this one.
Returns A new Fraction containing the result.

Subtracts another Fraction from this Fraction.

Negates the passed in Fraction, then adds it to this Fraction.

template<typename T>
CONSTEXPR14 auto fractions::Fraction<T>::operator-(const T& rhs) const -> Fraction

Parameters
rhs The integer to subtract.
Returns A new Fraction containing the difference.

Subtracts an integer from this Fraction.

Converts the integer rhs to a Fraction with denominator 1 and subtracts it from this Fraction.

template<typename T>
CONSTEXPR14 auto fractions::Fraction<T>::operator+=(const Fraction& rhs) -> Fraction &

Parameters
rhs in The Fraction to add.
Returns A reference to this Fraction after adding.

Adds another Fraction to this Fraction.

If the denominators are equal, simply add the numerators. Otherwise, find the greatest common divisor of both denominators, multiply the numerators by the denominator over the gcd, add the new numerators, multiply by the product of the original denominators, and reduce to lowest terms.

template<typename T>
CONSTEXPR14 auto fractions::Fraction<T>::operator-=(const Fraction& rhs) -> Fraction &

Parameters
rhs The Fraction to subtract.
Returns A reference to this Fraction after subtracting.

Subtracts another Fraction from this Fraction.

If the denominators are equal, simply subtract the numerators. Otherwise, find the greatest common divisor of both denominators, multiply the numerators by the denominator over the gcd, subtract the new numerators, multiply by the product of the original denominators, and reduce to lowest terms.

template<typename T>
CONSTEXPR14 auto fractions::Fraction<T>::operator+=(const T& rhs) -> Fraction &

Parameters
rhs in The T (integer) to add.
Returns A reference to this Fraction after adding.

Adds a T (integer) to this Fraction.

If the denominator is 1, simply add to the numerator. Otherwise, multiply the T by the denominator and add to the numerator. Then, reduce to lowest terms.

template<typename T>
CONSTEXPR14 auto fractions::Fraction<T>::operator++() -> Fraction &

Returns A reference to this Fraction after incrementing.

Increments this Fraction by 1.

Adds 1 to this Fraction

template<typename T>
CONSTEXPR14 auto fractions::Fraction<T>::operator--() -> Fraction &

Returns A reference to this Fraction after decrementing.

Decrements this Fraction by 1.

Subtracts 1 from this Fraction by calling operator-=() with a T (integer) value of 1.

template<typename T>
CONSTEXPR14 auto fractions::Fraction<T>::operator++(int) -> Fraction

Post-increment operator.

Makes a copy of this Fraction, increments the copy, and returns the copy.

template<typename T>
CONSTEXPR14 auto fractions::Fraction<T>::operator--(int) -> Fraction

Post-decrement operator.

Makes a copy of this Fraction, decrements the copy, and returns the copy.

template<typename T>
auto fractions::Fraction<T>::operator-=(const T& rhs) -> Fraction &

Parameters
rhs The integer to subtract.
Returns A reference to this fraction after subtracting.

Subtracts an integer from this fraction.

If the denominator is 1, simply subtract from the numerator. Otherwise, multiply the integer by the denominator and subtract from the numerator. Then, reduce to lowest terms.

template<typename T>
CONSTEXPR14 auto fractions::Fraction<T>::operator!=(const Fraction& rhs) const -> bool

Parameters
rhs The right hand side fraction to compare against.
Returns True if the fractions are not equal, false if they are equal.

Compares this fraction for inequality against another fraction.

Returns true if this fraction and the given fraction represent different rational values, false if they are equal. Uses the equality operator to compare the two fractions.

template<typename T>
CONSTEXPR14 auto fractions::Fraction<T>::operator>(const Fraction& rhs) const -> bool

Parameters
rhs The right hand side fraction to compare.
Returns True if this fraction is greater than rhs, false otherwise.

Compares this fraction to another fraction for greater than.

Returns true if this fraction is greater than the other fraction, false otherwise. Greater than is determined by converting to a common denominator and comparing the resulting numerators.

template<typename T>
CONSTEXPR14 auto fractions::Fraction<T>::operator>=(const Fraction& rhs) const -> bool

Parameters
rhs The right hand side fraction to compare.
Returns True if this fraction is greater than or equal to the other fraction, false otherwise.

Compares this fraction to another fraction for greater than or equal to.

template<typename T>
CONSTEXPR14 auto fractions::Fraction<T>::operator<=(const Fraction& rhs) const -> bool

Parameters
rhs The right hand side fraction to compare.
Returns True if this fraction is less than or equal to the other fraction, false otherwise.

Compares this fraction to another fraction for less than or equal to.

template<typename T>
CONSTEXPR14 auto fractions::Fraction<T>::operator>(const T& rhs) const -> bool

Parameters
rhs The right hand side fraction to compare.
Returns True if this fraction is greater than the other T integer, false otherwise.

Compares this fraction to another T integer for greater than.

template<typename T>
CONSTEXPR14 auto fractions::Fraction<T>::operator<=(const T& rhs) const -> bool

Parameters
rhs The right hand side fraction to compare.
Returns True if this fraction is less than or equal to the other T integer, false otherwise.

Compares this fraction to another T integer for less than or equal to.

template<typename T>
CONSTEXPR14 auto fractions::Fraction<T>::operator>=(const T& rhs) const -> bool

Parameters
rhs The right hand side fraction to compare.
Returns True if this fraction is greater than or equal to the other T integer, false otherwise.

Compares this fraction to another T integer for greater than or equal to.

template<typename T>
CONSTEXPR14 auto fractions::Fraction<T>::operator==(const Fraction& lhs, const T& rhs) -> bool

Parameters
lhs The left hand side fraction to compare.
rhs The right hand side integer to compare.
Returns True if lhs == rhs, false otherwise.

Compares this fraction to an integer value for equality.

Returns true if the fraction is equal to the integer, false otherwise. The fraction is considered equal if its normalized value matches the integer when the denominator is 1.

Example: Fraction(1, 1) == 1 -> true Fraction(1, 2) == 2 -> false Fraction(1, 2) == 3 -> false

template<typename T>
CONSTEXPR14 auto fractions::Fraction<T>::operator<(const Fraction& lhs, const T& rhs) -> bool

Parameters
lhs The left hand side fraction to compare.
rhs The right hand side integer to compare.
Returns True if lhs < rhs, false otherwise.

Compares this fraction to an integer value for less than.

Returns true if this fraction is less than the integer, false otherwise. The fraction is considered less if its normalized value is less than the integer when the denominator is 1.

Example: Fraction(1, 1) < 1 -> false Fraction(1, 2) < 2 -> true Fraction(1, 2) < 3 -> true

template<typename T>
CONSTEXPR14 auto fractions::Fraction<T>::operator<(const T& lhs, const Fraction& rhs) -> bool

Parameters
lhs The left hand side integer to compare.
rhs The right hand side fraction to compare.
Returns True if lhs < rhs, false otherwise.

Compares an integer value to this fraction for less than.

Returns true if the integer is less than this fraction, false otherwise. The fraction is considered greater if its normalized value is greater than the integer when the denominator is 1.

Example: 1 < Fraction(1, 1) -> false 2 < Fraction(1, 2) -> false 3 < Fraction(7, 2) -> true

template<typename T>
CONSTEXPR14 auto fractions::Fraction<T>::operator==(const T& lhs, const Fraction& rhs) -> bool

Parameters
lhs The left hand side integer to compare.
rhs The right hand side fraction to compare.
Returns True if lhs == rhs, false otherwise.

Compares an integer value to this fraction for equality.

Returns true if the integer is equal to this fraction, false otherwise. Equality is checked by comparing the integer to the normalized value of the fraction.

Example: 1 == Fraction(1, 1) -> true 2 == Fraction(1, 2) -> false 3 == Fraction(7, 2) -> false

template<typename T>
CONSTEXPR14 auto fractions::Fraction<T>::operator==(const Fraction& lhs, const Fraction& rhs) -> bool

Parameters
lhs The left hand side fraction to compare.
rhs The right hand side fraction to compare.
Returns True if lhs == rhs, false otherwise.

Compares this fraction to another fraction for equality.

Returns true if the two fractions represent the same rational value, false otherwise. Equality is checked by normalizing both fractions to a common denominator and comparing the numerators.

Example: Fraction(1, 1) == Fraction(1, 1) -> true Fraction(1, 2) == Fraction(2, 4) -> true Fraction(1, 2) == Fraction(3, 4) -> false

template<typename T>
CONSTEXPR14 auto fractions::Fraction<T>::operator<(const Fraction& lhs, const Fraction& rhs) -> bool

Parameters
lhs The left hand side fraction to compare.
rhs The right hand side fraction to compare.
Returns True if lhs < rhs, false otherwise.

Compares this fraction to another fraction for less than.

Returns true if this fraction is less than the other fraction, false otherwise. Less than is determined by converting both fractions to a common denominator and comparing the resulting numerators.

template<typename T>
CONSTEXPR14 auto fractions::Fraction<T>::operator>(const T& lhs, const Fraction& rhs) -> bool

Parameters
lhs The T integer on the left hand side.
rhs The Fraction on the right hand side.
Returns True if lhs is greater than rhs, false otherwise.

Compares a T integer to a Fraction for greater than.

This is a friend function that allows comparing a T integer on the left hand side to a Fraction on the right hand side.

template<typename T>
CONSTEXPR14 auto fractions::Fraction<T>::operator<=(const T& lhs, const Fraction& rhs) -> bool

Parameters
lhs The T integer on the left hand side.
rhs The Fraction on the right hand side.
Returns True if lhs is less than or equal to rhs, false otherwise.

Compares a T integer to a Fraction for less than or equal to.

This is a friend function that allows comparing a T integer on the left hand side to a Fraction on the right hand side.

template<typename T>
CONSTEXPR14 auto fractions::Fraction<T>::operator>=(const T& lhs, const Fraction& rhs) -> bool

Parameters
lhs The T integer on the left hand side.
rhs The Fraction on the right hand side.
Returns True if lhs is greater than or equal to rhs, false otherwise.

Compares a T integer to a Fraction for greater than or equal to.

This is a friend function that allows comparing a T integer on the left hand side to a Fraction on the right hand side.

template<typename T>
CONSTEXPR14 auto operator*(Fraction lhs, const Fraction& rhs) -> Fraction

Parameters
lhs The left hand Fraction to multiply.
rhs The right hand Fraction to multiply.
Returns A new Fraction containing the result of the multiplication.

Multiplies the given Fraction lhs by the Fraction rhs.

Normalizes lhs and rhs before multiplying. Returns a new Fraction containing the result of the multiplication.

template<typename T>
CONSTEXPR14 auto operator*(Fraction lhs, const T& rhs) -> Fraction

Parameters
lhs The left hand Fraction to multiply.
rhs The right hand integer to multiply.
Returns A new Fraction containing the result of the multiplication.

Multiplies the given Fraction lhs by the integer rhs.

Normalizes lhs before multiplying. Returns a new Fraction containing the result of the multiplication.

template<typename T>
CONSTEXPR14 auto operator*(const T& lhs, Fraction rhs) -> Fraction

Parameters
lhs The left hand integer to multiply.
rhs The right hand Fraction to multiply.
Returns A new Fraction containing the result of the multiplication.

Multiplies the Fraction rhs by the integer lhs.

Normalizes rhs before multiplying. Returns a new Fraction containing the result of the multiplication.

template<typename T>
CONSTEXPR14 auto operator/(Fraction lhs, const Fraction& rhs) -> Fraction

Parameters
lhs in
rhs in
Returns A Fraction after division.

Divides the Fraction lhs by the Fraction rhs.

Returns a new Fraction containing the result.

template<typename T>
CONSTEXPR14 auto operator/(Fraction lhs, const T& rhs) -> Fraction

Parameters
lhs in
rhs in
Returns A Fraction after division.

Divides the Fraction lhs by the integer rhs.

template<typename T>
CONSTEXPR14 auto operator/(const T& lhs, Fraction rhs) -> Fraction

Parameters
lhs in
rhs in
Returns A Fraction after division.

Divides the integer lhs by the Fraction rhs.

Returns a new Fraction containing the result.

template<typename T>
CONSTEXPR14 auto operator+(Fraction lhs, const T& rhs) -> Fraction

Parameters
lhs The Fraction to add to.
rhs The integer to add.
Returns A new Fraction containing the sum.

Adds a Fraction and an integer.

Converts the integer to a Fraction and adds it to the Fraction lhs.

template<typename T>
CONSTEXPR14 auto operator+(const T& lhs, Fraction rhs) -> Fraction

Parameters
lhs The Fraction to add to.
rhs The integer to add.
Returns A new Fraction containing the sum.

Adds an integer to a Fraction.

Converts the integer rhs to a Fraction and adds it to the Fraction lhs.

template<typename T>
CONSTEXPR14 auto operator-(const T& c, const Fraction& frac) -> Fraction

Parameters
c The integer to subtract.
frac The Fraction to subtract from.
Returns A new Fraction containing the difference.

Subtracts a T integer from this Fraction.

Subtracts the integer c from this Fraction frac. Returns the result as a new Fraction.

template<typename T>
CONSTEXPR14 auto operator+(int&& c, const Fraction& frac) -> Fraction

Parameters
c The integer to add.
frac The Fraction to add to.
Returns A new Fraction containing the sum.

Adds an integer to a Fraction.

This friend function allows adding an integer and a Fraction by converting the integer to a Fraction and delegating to the Fraction += operator.

template<typename T>
CONSTEXPR14 auto operator-(int&& c, const Fraction& frac) -> Fraction

Parameters
c The integer to subtract.
frac The Fraction to subtract from.
Returns A new Fraction containing the difference.

Subtracts an integer from a Fraction.

This allows subtracting an integer from a Fraction by first converting the integer to a Fraction.

template<typename T>
CONSTEXPR14 auto operator*(int&& c, const Fraction& frac) -> Fraction

Parameters
c The integer value to multiply.
frac The Fraction to multiply.
Returns A new Fraction containing the product.

Multiplies an integer by a Fraction.

This friend function converts the integer to a Fraction and delegates to the Fraction *= operator to perform the multiplication.

template<typename T> template<typename _Stream>
auto operator<<(_Stream& os, const Fraction& frac) -> _Stream &

Parameters
os in
frac in
Returns _Stream&

Overloads the stream output operator to print a Fraction to a stream.

This allows printing a Fraction to an output stream in the format "(numerator/denominator)".