How can I compute a * b / c when both a and b are smaller than c, but a * b overflows?
问题 Assuming that uint is the largest integral type on my fixed-point platform, I have: uint func(uint a, uint b, uint c); Which needs to return a good approximation of a * b / c . The value of c is greater than both the value of a and the value of b . So we know for sure that the value of a * b / c would fit in a uint . However, the value of a * b itself overflows the size of a uint . So one way to compute the value of a * b / c would be: return a / c * b; Or even: if (a > b) return a / c * b;