Div

From Free Pascal wiki
Jump to navigationJump to search

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:

program divDemo(input, output, stderr);

var
	i: shortInt;
	j: shortInt;
	q: qWord;
	r: qWord;

begin
	i := 16;
	j := 3;
	q := 1000;
	r := 300;
	
	writeLn(i div j);
	writeLn(i / j);
	writeLn(q div r);
	writeLn(q / r);
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