div, ldiv, lldiv

2 つの整数値の商と剰余を計算します。

構文

div_t div(
   int numer,
   int denom
);
ldiv_t ldiv(
   long numer,
   long denom
);
lldiv_t lldiv(
   long long numer,
   long long denom
);
ldiv_t div(
   long numer,
   long denom
); /* C++ only */
lldiv_t div(
   long long numer,
   long long denom
); /* C++ only */

パラメーター

numer
分子。

denom
分母。

戻り値

int 型の引数を使用して呼び出された div では、商と剰余で構成される div_t 型の構造を返します。 long 型の引数を使用した場合の戻り値は ldiv_t であり、long long 型の引数を使用した場合の戻り値は lldiv_t です。 div_tldiv_tlldiv_t 型は、<stdlib.h> で定義されます。

解説

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

long 型または long long 型の引数を受け取る div のオーバーロードは、C++ コードにのみ使用できます。 戻り値の型 ldiv_tlldiv_t には、メンバー quotrem が含まれており、div_t のメンバーと同じ意味を持ちます。

必要条件

ルーチンによって返される値 必須ヘッダー
div, ldiv, lldiv <stdlib.h>

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

// crt_div.c
// arguments: 876 13

// This example takes two integers as command-line
// arguments and displays the results of the integer
// division. This program accepts two arguments on the
// command line following the program name, then calls
// div to divide the first argument by the second.
// Finally, it prints the structure members quot and rem.
//

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

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

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

   printf( "x is %d, y is %d\n", x, y );
   div_result = div( x, y );
   printf( "The quotient is %d, and the remainder is %d\n",
           div_result.quot, div_result.rem );
}
x is 876, y is 13
The quotient is 67, and the remainder is 5

関連項目

数学と浮動小数点のサポート
imaxdiv