Rounding is inherently unprofitable.
The fastest hack that comes to mind is simply using the built-in behavior (which is floor -ing or trunc -from the result) and an offset of half:
Live on coliru
#include <iostream> #include <fstream> #include <boost/rational.hpp> int main() { using R = boost::rational<int64_t>; for (auto den : {5,6}) { std::cout << "---------\n"; for (auto num : {1,2,3,4,5,6}) { R pq(num, den); std::cout << num << "/" << den << " = " << pq << ": " << boost::rational_cast<int64_t>(pq + R(1,2)) << "\n"; } } }
Print
source share