[fpc-devel] Signed division

Christo Crause christo.crause at gmail.com
Fri Jun 1 13:42:53 CEST 2018


On Fri, Jun 1, 2018 at 11:13 AM, J. Gareth Moreton <
gareth at moreton-family.com> wrote:

> Hi everyone,
>
> I'm starting to make some modifications to the optimizer so it optimises
> div and mod operations on signed integers (I've done unsigned integers
> already). One question that I've stumbled across... what are the rules
> regarding the sign of the result?  Experimenting with IDIV suggests it
> returns a negative modulus only if the numerator is positive (so -28 / 3 =
> -9 remainder -1, and -28 / -3 = 9 remainder -1, but 28 / -3 = -9 remainder
> +1), which follows the Euclidean division rule of n = dq + r, where n is
> the numerator, d is the divisor, q is the resultant quotient and r is the
> resultant remainder.
>
> While I'm planning on being consistent with IDIV, what are the rules of
> signed division in Free Pascal? There are hints that some dialects of
> Pascal only return a positive modulus no matter what.
>
> Gareth aka. Kit
>

  " The sign of the result of a Mod operator is the same as the sign of the
left side operand of the Mod operator " -
https://freepascal.org/docs-html/current/ref/refsu45.html
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.freepascal.org/pipermail/fpc-devel/attachments/20180601/25b7e240/attachment.html>


More information about the fpc-devel mailing list