imaxdiv

按单个操作计算两个任意大小整数值的商和余数。

语法

imaxdiv_t imaxdiv(
   intmax_t numer,
   intmax_t denom
);

参数

numer
分子。

denom
分母。

返回值

使用类型为 intmax_t 的参数调用 imaxdiv,返回类型为 imaxdiv_t、包含商和余数的结构。

备注

imaxdiv 函数将 numer 除以 denom,从而计算商和余数。 imaxdiv_t 结构包含商,intmax_tquot 和余数,intmax_trem。 商的符号与数学商的符号相同。 其绝对值是小于数学商的绝对值的最大整数。 如果分母为 0,程序将终止并显示错误消息。

要求

例程 必需的标头
imaxdiv <inttypes.h>

有关兼容性的详细信息,请参阅 兼容性

示例

// crt_imaxdiv.c
// Build using: cl /W3 /Tc crt_imaxdiv.c
// This example takes two integers as command-line
// arguments and calls imaxdiv to divide the first
// argument by the second, then displays the results.

#include <stdio.h>
#include <stdlib.h>
#include <inttypes.h>

int main(int argc, char *argv[])
{
   intmax_t x,y;
   imaxdiv_t div_result;

   x = atoll(argv[1]);
   y = atoll(argv[2]);

   printf("The call to imaxdiv(%lld, %lld)\n", x, y);
   div_result = imaxdiv(x, y);
   printf("results in a quotient of %lld, and a remainder of %lld\n\n",
          div_result.quot, div_result.rem);
}

在使用命令行参数 9460730470000000 8766 进行生成和调用时,代码将生成以下输出:

The call to imaxdiv(9460730470000000, 8766)
results in a quotient of 1079252848505, and a remainder of 5170

另请参阅

数学和浮点支持
div
ldivlldiv