Div

From Free Pascal wiki

Deutsch (de) English (en) español (es) suomi (fi) français (fr) русский (ru)

div is division in which the fractional part (remainder) is discarded. The expression a div b returns the integer part of the result of dividing two integers. This is in contrast to the expression a / b which returns a real result.

Both sides of an expression using div must be one of the integer types. Using a real operand with div will result in a compile-time error:

 Error: Operator is not overloaded: […]

To get an integer result with a real operand, use trunc or round with the / operator.

To demonstrate what div does, consider the following example:

 1 program divDemo(input, output, stderr);
 2 
 3 var
 4 	i: shortInt;
 5 	j: shortInt;
 6 	q: qWord;
 7 	r: qWord;
 8 
 9 begin
10 	i := 16;
11 	j := 3;
12 	q := 1000;
13 	r := 300;
14 	
15 	writeLn(i div j);
16 	writeLn(i / j);
17 	writeLn(q div r);
18 	writeLn(q / r);
19 end.

outputs:

$ ./divDemo
5
 5.3333333333333330E+000
3
 3.3333333333333335E+000

See also

  • mod – remainder of integer division
  • trunc – retrieve integer part of real numbers
  • round – round to integer
  • math.divmod – retrieve both quotient and remainder