67 #ifndef LIB_RATIONAL_H 68 #define LIB_RATIONAL_H 71 #include "lib/integral.hpp" 77 #include <boost/rational.hpp> 82 using Rat = boost::rational<int64_t>;
83 using boost::rational_cast;
89 can_represent_Product (int64_t a, int64_t b)
91 return ilog2(abs(a))+1
97 can_represent_Product (Rat a, Rat b)
99 return can_represent_Product(a.numerator(), b.numerator())
100 and can_represent_Product(a.denominator(), b.denominator());
104 can_represent_Sum (Rat a, Rat b)
106 return can_represent_Product(a.numerator(), b.denominator())
107 and can_represent_Product(b.numerator(), a.denominator());
134 f128
const ROUND_ULP = 1 + 1/(f128(std::numeric_limits<int64_t>::max()) * 2);
137 f128 frac = f128(r) / den;
138 int64_t res = d*u + int64_t(frac*u * ROUND_ULP);
139 ENSURE (abs (f128(res)/u - rational_cast<f128>(Rat{num,den})) <= 1.0/abs(u)
140 ,
"Requantisation error exceeded num=%li / den=%li -> res=%li / quant=%li" 161 return Rat{
reQuant (src.numerator(), src.denominator(), u), u};
174 operator""_r (
unsigned long long num)
176 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)