13#error "This header is C++-only."
27 static_assert(std::is_integral_v<Scalar>,
"This type is only for use with integer components.");
50 return Rational{denominator, numerator}.withNonNegativeDenominator();
59 if constexpr (std::is_unsigned_v<Scalar>) {
63 return denominator <
Scalar{0} ?
Rational{-numerator, -denominator} : *
this;
74 return numerator > denominator && denominator >
Scalar{0};
85 return numerator == denominator && denominator !=
Scalar{0};
96 return numerator ==
Scalar{0} && denominator !=
Scalar{0};
107 return denominator >
Scalar{0} && numerator >
Scalar{0} && numerator < denominator;
120 return Rational{denominator - numerator, denominator}.withNonNegativeDenominator();
129 return static_cast<float>(numerator) /
static_cast<float>(denominator);
138 return static_cast<double>(numerator) /
static_cast<double>(denominator);
149template <
typename Scalar>
150constexpr Rational<Scalar>
153 return Rational<Scalar>{lhs.numerator * rhs.numerator, lhs.denominator * rhs.denominator}
154 .withNonNegativeDenominator();
164template <
typename Scalar>
168 return Rational<Scalar>{lhs.numerator * rhs, lhs.denominator}.withNonNegativeDenominator();
178template <
typename Scalar>
182 return (rhs * lhs).withNonNegativeDenominator();
190template <
typename Scalar>
194 return rhs.numerator == lhs.numerator && rhs.denominator == lhs.denominator;
204template <
typename Scalar>
208 return (lhs * rhs.
reciprocal()).withNonNegativeDenominator();
219template <
typename Scalar>
C++-only functionality in the Math helper library.
Definition: m_documentation.hpp:15
A rational (fractional) number type.
Definition: m_rational.hpp:26
constexpr Rational< Scalar > operator*(const Scalar &lhs, const Rational< Scalar > &rhs)
Multiplication operator with a scalar.
Definition: m_rational.hpp:180
constexpr Rational reciprocal() const noexcept
Return the reciprocal of this value.
Definition: m_rational.hpp:48
constexpr float as_float() const noexcept
Get an approximation of this value as a float.
Definition: m_rational.hpp:127
constexpr bool isUnity() const noexcept
Does this rational number represent 1?
Definition: m_rational.hpp:83
constexpr Rational< Scalar > operator/(const Rational< Scalar > &lhs, const Rational< Scalar > &rhs)
Division operator.
Definition: m_rational.hpp:206
constexpr double as_double() const noexcept
Get an approximation of this value as a double.
Definition: m_rational.hpp:136
constexpr Rational< Scalar > operator*(const Rational< Scalar > &lhs, const Rational< Scalar > &rhs)
Multiplication operator.
Definition: m_rational.hpp:151
constexpr bool operator==(const Rational< Scalar > &lhs, const Rational< Scalar > &rhs)
Equality comparison operator.
Definition: m_rational.hpp:192
constexpr bool isBetweenZeroAndOne() const noexcept
Does this rational number represent a value between 0 and 1 (exclusive), and has a positive denominat...
Definition: m_rational.hpp:105
constexpr bool isZero() const noexcept
Does this rational number represent 0?
Definition: m_rational.hpp:94
constexpr Rational withNonNegativeDenominator() const noexcept
Return this value, with the denominator non-negative (0 or positive).
Definition: m_rational.hpp:57
static constexpr Rational< Scalar > simplestUnity() noexcept
Return the rational value 1/1, the simplest unity (== 1) value.
Definition: m_rational.hpp:37
constexpr Rational< Scalar > operator*(const Rational< Scalar > &lhs, const Scalar &rhs)
Multiplication operator with a scalar.
Definition: m_rational.hpp:166
constexpr Rational complement() const noexcept
Get the complementary fraction.
Definition: m_rational.hpp:118
constexpr bool isOverUnity() const noexcept
Does this rational number represent a value greater than 1, with a positive denominator?
Definition: m_rational.hpp:72
constexpr Rational< Scalar > operator/(const Rational< Scalar > &lhs, Scalar rhs)
Division operator by a scalar.
Definition: m_rational.hpp:221