26#include <fmt/ostream.h>
42template <base::concepts::
integral IntegerType>
57 "The denominator of a fraction must not be zero.");
118 const integer_type my_coeff = right.denominator_ / common_divisor;
155 const integer_type my_coeff = right.denominator_ / common_divisor;
251 if constexpr (std::is_signed_v<integer_type>) {
263 const auto common_divisor =
277 if constexpr (std::is_unsigned_v<integer_type>) {
302template <num_collect::base::concepts::
integral Integer>
316template <num_collect::base::concepts::
integral Integer>
330template <num_collect::base::concepts::
integral Integer>
344template <num_collect::base::concepts::
integral Integer>
359template <num_collect::base::concepts::
integral Integer>
368 constexpr auto parse(format_parse_context& context)
369 ->
decltype(context.begin()) {
370 return context.end();
381 format_context& context)
const {
382 return fmt::format_to(
399template <num_collect::base::concepts::
integral Integer>
402 fmt::print(stream,
"{}", val);
constexpr auto operator/=(const fraction &right) -> fraction &
Divide this fraction by a fraction.
static constexpr auto int_abs(integer_type x) noexcept -> integer_type
Get the absolute value of an integer.
constexpr auto operator==(const fraction &right) const noexcept -> bool
Compare this fraction with another fraction.
constexpr auto denominator() const noexcept -> integer_type
Get the denominator.
constexpr auto operator!=(const fraction &right) const noexcept -> bool
Compare this fraction with another fraction.
constexpr auto operator+=(const fraction &right) -> fraction &
Add a fraction to this fraction.
constexpr auto add(const fraction &right) -> fraction &
Add a fraction to this fraction.
constexpr fraction() noexcept
Constructor. (Initialize to zero.)
constexpr auto divide_by(const fraction &right) -> fraction &
Divide this fraction by a fraction.
constexpr auto subtract(const fraction &right) -> fraction &
Subtract a fraction from this fraction.
constexpr fraction(integer_type integer) noexcept
Constructor. (Convert from an integer.)
constexpr fraction(integer_type numerator, integer_type denominator)
Constructor.
integer_type numerator_
Numerator.
constexpr auto operator*=(const fraction &right) -> fraction &
Multiply a fraction to this fraction.
constexpr void normalize()
Normalize this fraction.
constexpr auto multiply(const fraction &right) -> fraction &
Multiply a fraction to this fraction.
constexpr auto inverse() const -> fraction
Get the inverse of this fraction.
constexpr auto numerator() const noexcept -> integer_type
Get the numerator.
IntegerType integer_type
Type of integers.
constexpr auto operator-=(const fraction &right) -> fraction &
Subtract a fraction from this fraction.
integer_type denominator_
Denominator.
Definition of exceptions.
Definition of greatest_common_divisor function.
Definition of integral concept.
Definition of macros for logging.
Definition of multiply_safely function.
Namespace of fmt library.
Namespace of classes of numbers.
auto operator<<(std::ostream &stream, const fraction< Integer > &val) -> std::ostream &
Format a fraction.
constexpr auto operator*(const fraction< Integer > &left, const fraction< Integer > &right) -> fraction< Integer >
Multiply two fractions.
constexpr auto operator-(const fraction< Integer > &left, const fraction< Integer > &right) -> fraction< Integer >
Subtract a fraction from another fraction.
constexpr auto operator/(const fraction< Integer > &left, const fraction< Integer > &right) -> fraction< Integer >
Divide a fraction by another fraction.
constexpr auto operator+(const fraction< Integer > &left, const fraction< Integer > &right) -> fraction< Integer >
Add two fractions.
constexpr auto multiply_safely(T a, T b) -> T
Multiply two integers with check of overflow.
constexpr auto greatest_common_divisor(T a, T b) -> T
Calculate the greatest common divisor.
Namespace of num_collect source codes.
Definition of NUM_COLLECT_PRECONDITION macro.
#define NUM_COLLECT_PRECONDITION(CONDITION,...)
Check whether a precondition is satisfied and throw an exception if not.