The MulDiv convenience function in Windows API is equivalent to (a*b)/c, but it stores the intermediate result of a*b in a 64-bit variable before dividing it by c to avoid integer overflow where a*b is greater than MAX_INT but (a*b)/c is not.
WINBASEAPI
int
WINAPI
MulDiv(
    _In_ int nNumber,
    _In_ int nNumerator,
    _In_ int nDenominator
    );
When programming in Linux, is there an equivalent convenience function?
It seems there is no equivalent function for Linux.
I created a simple inline function that works (I haven't tested it with 64-bit compilation though)
inline int mul_div(int number, int numerator, int denominator) {
    long long ret = number;
    ret *= numerator;
    ret /= denominator;
    return (int) ret;
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With