Div

From Free Pascal wiki
Revision as of 10:35, 20 August 2016 by FPC user (talk | contribs) (extra / example)
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 error: "Error: Operator is not overloaded:". To get an integer result with a Real operand, use Trunc or Round with the / operator.

Example:

var 
  i : ShortInt = 16; 
  j : ShortInt = 3;
  q : QWord = 1000;
  r : QWord = 300;

begin
  i := 16;
  j := 3;
  WriteLn(i div j);
  WriteLn(i / j);
  WriteLn(q div r);
  Writeln(q / r)
end.

Output:
5
 5.3333333333333330E+000
3
 3.3333333333333335E+000

See also