MulDiv函数

标签: , ,

MulDiv函数是Kernel32.dll提供的一个Windows API:

int MulDiv(
    _In_  int nNumber,
    _In_  int nNumerator,
    _In_  int nDenominator
);

把前两个32位数相乘,然后用得到的64位数除以第三个32位数,结果四舍五入后返回。如果除数为0或者发生了溢出,那么返回-1。

#include <stdio.h>
#include <windows.h>

//作者: Demon
//网站: https://demon.tw

int main()
{
    int a, b, c, d;

    a = 0x7fffffff;
    b = 0x7fffffff;
    c = 0x7fffffff;

    d = a * b / c;
    printf("%d\n", d);

    d = MulDiv(a, b, c);
    printf("%d\n", d);

    return 0;
}

可是如果返回-1的话,怎么区分是计算结果本身就是-1还是发生了错误呢?

赞赏

微信赞赏支付宝赞赏

随机文章:

  1. 用C语言实现PHP的basename函数
  2. WMI工具:WMI Event Registration Tool
  3. ActivePython PyPM error: (OperationalError) unable to open database file None None
  4. VB6拾遗:更高效的数组
  5. OpenWrt使用crontab执行计划任务

留下回复