Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
Windows API 中的MulDiv便利函数相当于(a*b)/c,但它在除以之前将的中间结果存储a*b 在一个 64 位变量c中,以避免a*b大于MAX_INT但(a*b)/c不是的整数溢出。
(a*b)/c
a*b
c
MAX_INT
WINBASEAPI int WINAPI MulDiv( _In_ int nNumber, _In_ int nNumerator, _In_ int nDenominator );
在Linux中编程时,是否有等效的便利功能?
Linux 似乎没有等效的功能。
我创建了一个可以工作的简单内联函数(虽然我还没有用 64 位编译测试过它)
inline int mul_div(int number, int numerator, int denominator) { long long ret = number; ret *= numerator; ret /= denominator; return (int) ret; }