I have a structure representing a non-negative rational number p / q:
struct rational { uint64_t p; uint64_t q;
I would like to multiply my rational by uint64 n and get an integer result rounded down. That is, I would like to calculate:
uint64_t m = (n * rp)/rq;
avoiding intermediate overflow in n * rp . (Of course, the end result may overflow, which is acceptable.)
How can i do this? Is there any way to do this without zooming in?
(I was looking at boost :: rational, but it does not seem to provide this function.)
source share