Div: Difference between revisions

From Free Pascal wiki
Jump to navigationJump to search
No edit summary
m (replace legacy syntaxhighlight syntax [s/enclose="none"/inline/g], insert missing word “consider”, remove insignificant trailing/leading blanks)
 
(7 intermediate revisions by 3 users not shown)
Line 1: Line 1:
{{div}}
{{div}}


Div is division in which the fractional part (remainder) is discarded.
<syntaxhighlight lang="pascal" inline>div</syntaxhighlight> is division in which the fractional part (remainder) is discarded.
It means the integer part of the result of dividing two integers.
The expression <syntaxhighlight lang="pascal" inline>a div b</syntaxhighlight> returns the integer part of the result of dividing two integers.
This is in contrast to the expression <syntaxhighlight lang="pascal" inline>a / b</syntaxhighlight> which returns a [[Real|<syntaxhighlight lang="pascal" inline>real</syntaxhighlight>]] result.
 
Both sides of an expression using <syntaxhighlight lang="pascal" inline>div</syntaxhighlight> must be one of the integer types. Using a <syntaxhighlight lang="pascal" inline>real</syntaxhighlight> operand with <syntaxhighlight lang="pascal" inline>div</syntaxhighlight> will result in a compile-time error:
 
  Error: Operator is not overloaded: […]
 
To get an integer result with a {{Doc|package=RTL|unit=system|identifier=real|text=<syntaxhighlight lang="pascal" inline>real</syntaxhighlight>}} operand, use {{Doc|package=RTL|unit=system|identifier=trunc|text=<syntaxhighlight lang="pascal" inline>trunc</syntaxhighlight>}} or {{Doc|package=RTL|unit=system|identifier=round|text=<syntaxhighlight lang="pascal" inline>round</syntaxhighlight>}} with the [[Slash|<syntaxhighlight lang="pascal" inline>/</syntaxhighlight>&nbsp;operator]].
 
To demonstrate what <syntaxhighlight lang="pascal" inline>div</syntaxhighlight> does, consider the following example:
 
<syntaxhighlight lang="pascal" line highlight="15,17">
program divDemo(input, output, stderr);


Example:
<syntaxhighlight>
var
var
i,j:integer;
i: shortInt;
j: shortInt;
q: qWord;
r: qWord;
 
begin
begin
  i:=16;
i := 16;
  j:=3;
j := 3;
  writeln(inttostr(i div j));
q := 1000;
end;
r := 300;
writeLn(i div j);
writeLn(i / j);
writeLn(q div r);
writeLn(q / r);
end.
</syntaxhighlight>
</syntaxhighlight>


== See also ==
outputs:


* [[Mod]]
<syntaxhighlight lang="bash">
$ ./divDemo
5
5.3333333333333330E+000
3
3.3333333333333335E+000
</syntaxhighlight>


== See also ==


[[Category:Pascal]]
* [[Mod|<syntaxhighlight lang="pascal" inline>mod</syntaxhighlight>]] – remainder of integer division
[[Category:FPC]]
* [[Trunc|<syntaxhighlight lang="pascal" inline>trunc</syntaxhighlight>]] – retrieve integer part of real numbers
[[Category:Reserved words]]
* [[Round|<syntaxhighlight lang="pascal" inline>round</syntaxhighlight>]] – round to integer
* {{Doc|package=RTL|unit=math|identifier=divmod|text=<syntaxhighlight lang="pascal" inline>math.divmod</syntaxhighlight>}} – retrieve both quotient and remainder

Latest revision as of 08:48, 31 August 2021

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