imaxdiv

任意のサイズの 2 つの整数値の商および剰余を単一の操作として計算します。

構文

imaxdiv_t imaxdiv(
   intmax_t numer,
   intmax_t denom
);

パラメーター

numer
分子。

denom
分母。

戻り値

imaxdivは、型intmax_tの引数を使用して呼び出され、商と reメインder を構成する型imaxdiv_tの構造体を返します。

解説

imaxdiv 関数は numerdenom で割り、商と剰余を計算します。 imaxdiv_t 構造体には、商 intmax_tquot と剰余 intmax_trem が含まれます。 商の符号は、数学的な商の符号と同じです。 その絶対値は最も大きい整数であり、数学的な商の絶対値よりも小さくなります。 分母が 0 の場合、プログラムはエラー メッセージにより終了します。

必要条件

ルーチンによって返される値 必須ヘッダー
imaxdiv <inttypes.h>

互換性の詳細については、「 Compatibility」を参照してください。

// 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