Sign in Join
Wiki copytemporary

Div: what changed

Revision 1 to 2. Removed lines have a minus sign; added lines have a plus sign.

OldNewLine
11`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|real]] result.
22
33Both 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:
44
5-` Error: Operator is not overloaded: […]`
5+```text
6+ Error: Operator is not overloaded: […]
7+```
68
79To get an integer result with a [`real`](https://www.freepascal.org/docs-html/rtl/system/real.html) operand, use [`trunc`](https://www.freepascal.org/docs-html/rtl/system/trunc.html) or [`round`](https://www.freepascal.org/docs-html/rtl/system/round.html) with the [[Slash|/ operator]].
810
911To demonstrate what `div` does, consider the following example:
1012
1113``` pascal
1214program divDemo(input, output, stderr);
1315
1416var
1517 i: shortInt;
1618 j: shortInt;
1719 q: qWord;
1820 r: qWord;
1921
2022begin
2123 i := 16;
2224 j := 3;
2325 q := 1000;
2426 r := 300;
2527
2628 writeLn(i div j);
2729 writeLn(i / j);
2830 writeLn(q div r);
2931 writeLn(q / r);
3032end.
3133```
3234
3335outputs:
3436
3537``` bash
3638$ ./divDemo
37395
3840 5.3333333333333330E+000
39413
4042 3.3333333333333335E+000
4143```
4244
4345## See also
4446
4547- [[Mod|mod]] – remainder of integer division
4648- [[Trunc|trunc]] – retrieve integer part of real numbers
4749- [[Round|round]] – round to integer
4850- [`math.divmod`](https://www.freepascal.org/docs-html/rtl/math/divmod.html) – retrieve both quotient and remainder
4951
5052---
5153*From the Free Pascal wiki, page "Div", as it stood on 2021-08-31 after a change by Kai Burghardt, out of the wiki's dump of 2024-05-26. Copyright stays with the author who wrote it, according to the old wiki's [[Licensing]] page. If you have concerns about this material, [message the admin](/wiki-contact).*

Back to history