Share via


imaxdiv

以單一作業計算任何大小之兩個整數值的商數和餘數。

語法

imaxdiv_t imaxdiv(
   intmax_t numer,
   intmax_t denom
);

參數

numer
分子。

denom
分母。

傳回值

imaxdiv,使用 類型的 intmax_t 引數呼叫 ,會傳回類型 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
ldiv, lldiv