58 #ifndef LIB_RATIONAL_H 59 #define LIB_RATIONAL_H 62 #include "lib/integral.hpp" 68 #include <boost/rational.hpp> 73 using Rat = boost::rational<int64_t>;
74 using boost::rational_cast;
80 can_represent_Product (int64_t a, int64_t b)
82 return ilog2(abs(a))+1
88 can_represent_Product (Rat a, Rat b)
90 return can_represent_Product(a.numerator(), b.numerator())
91 and can_represent_Product(a.denominator(), b.denominator());
95 can_represent_Sum (Rat a, Rat b)
97 return can_represent_Product(a.numerator(), b.denominator())
98 and can_represent_Product(b.numerator(), a.denominator());
125 f128
const ROUND_ULP = 1 + 1/(f128(std::numeric_limits<int64_t>::max()) * 2);
128 f128 frac = f128(r) / den;
129 int64_t res = d*u + int64_t(frac*u * ROUND_ULP);
130 ENSURE (abs (f128(res)/u - rational_cast<f128>(Rat{num,den})) <= 1.0/abs(u)
131 ,
"Requantisation error exceeded num=%li / den=%li -> res=%li / quant=%li" 152 return Rat{
reQuant (src.numerator(), src.denominator(), u), u};
165 operator""_r (ullong num)
167 return util::Rat{num};
int64_t reQuant(int64_t num, int64_t den, int64_t u)
Re-Quantise a number into a new grid, truncating to the next lower grid point.
Utilities for quantisation (grid alignment) and comparisons.
IDiv< I > iDiv(I num, I den)
constexpr int ilog2(I num)
Integral binary logarithm (disregarding fractional part)