Wiki copymirror

This is a mirror of the wiki at pascal.chat. Editing, history and sign-in are there.

Div

Deutsch · English · Español · Français · Suomi · Русский

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:

text
 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:

pascal
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:

bash
$ ./divDemo
5
 5.3333333333333330E+000
3
 3.3333333333333335E+000

See also


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.

Revision 2, last changed by WikiArchive .