25#include <fmt/format.h>
28#include <libavutil/rational.h>
63 return fmt::format(
"{}/{}", num_, den_);
89 template<
typename R =
double>
92 return num_ / (
R) den_;
98 return {num_ * r.den_ + r.num_ * den_, den_ * r.den_};
102 return {num_ * r.den_ - r.num_ * den_, den_ * r.den_};
109 std::swap(*
this, *
this + r);
114 std::swap(*
this, *
this - r);
172 explicit constexpr operator bool()
const {
return num_; }
177 bool inv = (den_ > 0) != (r.den_ > 0);
178 return inv != (num_ * r.den_ < den_ * r.num_);
182 bool inv = (den_ > 0) != (r.den_ > 0);
183 return inv != (num_ * r.den_ > den_ * r.num_);
189 constexpr bool operator<(
I i)
const {
return den_ < 0 ? (num_ >
i * den_) : (num_ <
i * den_); }
190 constexpr bool operator>(
I i)
const {
return den_ < 0 ? (num_ <
i * den_) : (num_ >
i * den_); }
199 constexpr void reduce()
201 if constexpr (std::is_integral<I>::value) {
203 auto g = gcd(num_ >= 0 ? num_ : -num_, den_ >= 0 ? den_ : -den_);
228template<
typename I,
typename II>
230template<
typename I,
typename II>
251 is >> r.num_ >>
sep >> r.den_;
264template<
typename T,
typename I>
271struct modulus<double>
273 double operator()(
const double& lhs,
const double& rhs)
const {
return std::fmod(lhs, rhs); }
278struct fmt::formatter<
jami::rational<I>> : fmt::formatter<std::string_view> {
279 template<
typename FormatContext>
281 return fmt::formatter<std::string_view>::format(r.to_string(), ctx);
Naive implementation of the boost::rational interface, described here: https://www....
constexpr bool operator!() const
constexpr bool operator<(I i) const
constexpr rational & operator/=(const rational &r)
constexpr bool operator>(const rational &r) const
rational & operator=(I n)
constexpr rational operator-(const rational &r) const
constexpr const rational & operator--()
constexpr bool operator!=(I i) const
constexpr bool operator==(const rational &r) const
constexpr rational & operator-=(I i)
constexpr I denominator() const
constexpr bool operator!=(const rational &r) const
constexpr bool operator==(I i) const
constexpr rational operator/(const rational &r) const
constexpr I numerator() const
constexpr rational(AVRational r)
constexpr rational & operator/=(I i)
constexpr bool operator<(const rational &r) const
constexpr rational & operator-=(const rational &r)
constexpr rational & operator*=(I i)
constexpr rational operator+(const rational &r) const
constexpr bool operator>(I i) const
constexpr rational & operator*=(const rational &r)
constexpr rational & operator+=(const rational &r)
constexpr rational(I n, I d)
constexpr rational operator*(const rational &r) const
constexpr rational & operator+=(I i)
rational & assign(I n, I d)
constexpr const rational & operator++()
std::string to_string() const
rational< I > abs(const rational< I > &r)
void emitSignal(Args... args)
rational< I > operator+(const rational< I > &r)
std::istream & operator>>(std::istream &is, rational< I > &r)
T rational_cast(const rational< I > &r)
rational< I > operator-(const rational< I > &r)
rational< I > operator/(II i, const rational< I > &r)
static std::ostream & operator<<(std::ostream &os, const Account &acc)