Sign in Join

Live chat for Free Pascal, Lazarus, Delphi and every Pascal programmer.

Mailing lists / fpc-devel / 2018-06

Signed division

3 messages · 2 people ·

Reply by email Subscribe to this list Your subscription

Read-only archive. Use j and k to move between messages.

J. Gareth Moreton

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

Reply by email

Christo Crause
to J. Gareth Moreton

On Fri, Jun 1, 2018 at 11:13 AM, J. Gareth Moreton <

Show quoted text 20 lines
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

Reply by email

J. Gareth Moreton

Aah, thank you. That clears things up.
Hooray for documentation! It's the same as
suspected and listed below.

(and I noticed a mistake. I meant to say
only if the numerator (left-hand side
operand) is negative, not positive)

Gareth aka. Kit

On Fri 01/06/18 12:42 , "Christo Crause"
sent:

On Fri, Jun 1, 2018 at 11:13 AM, J.
Gareth Moreton 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 "

html/current/ref/refsu45.html [1]

Links: ------ [1] https://freepascal.org/docs-

html/current/ref/refsu45.html

Reply by email