Difference between revisions of "Div"

From Free Pascal wiki
Jump to navigationJump to search
(add mention of "/")
Line 2: Line 2:
  
 
Div is division in which the fractional part (remainder) is discarded.
 
Div is division in which the fractional part (remainder) is discarded.
It means the integer part of the result of dividing two integers.
+
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 with div will result in a compile error: "Error: Operator is not overloaded:"
  
 
Example:
 
Example:
 
<syntaxhighlight>
 
<syntaxhighlight>
var
+
var  
i,j:integer;
+
  i : ShortInt = 16;
 +
  j : ShortInt = 3;
 +
  q : QWord = 1000;
 +
  r : QWord = 300;
 +
 
 
begin
 
begin
   i:=16;
+
   i := 16;
   j:=3;
+
   j := 3;
   writeln(inttostr(i div j));
+
   WriteLn(i div j);
end;
+
  WriteLn(i / j);
 +
  WriteLn(q div r);
 +
end.
 
</syntaxhighlight>
 
</syntaxhighlight>
 
+
'''Output:'''<br/>
 +
5<br/>
 +
&nbsp;5.3333333333333330E+000<br/>
 +
3
 
== See also ==
 
== See also ==
  

Revision as of 10:27, 20 August 2016

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 with div will result in a compile error: "Error: Operator is not overloaded:"

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);
end.

Output:
5
 5.3333333333333330E+000
3

See also