Tobias, I just tested this against my own version of DIV/MOD and got these results:

100 CLS
110 DivMod 5, 2
120 DivMod -5, 2
130 DivMod -5, -2
140 DivMod 5, -2
150 :
160 DEFine PROCedure DivMod(a, b)
170 PRINT 'd'! a DIV b, 'm'! a MOD b, '='! a * (b DIV a) + b MOD a
180 PRINT 'D'! LDIV(a, b), 'M'! LMOD(a, b), '='! a * LDIV(b, a) + LMOD(b, a)\\
190 END DEFine
200 :

    d 2     m 1     = 2
    D 2     M 1     = 2

    d -3    m 1     = 2
    D -2    M -1    = 2

    d 2     m -1    = -2
    D 2     M -1    = -2

    d -3    m -1    = -2
    D -2    M 1     = -2

My LDIV/LMOD (written before the longword versions came out) works by removing the sign(s), then doing the division (based on an "algorithm" from an old mechanical calculator my grandfather used in the bank ;) and then adds back the sign(s) afterwards. Although the answers are somewhat different, using your formula below, the end result seems to be the same.

Per

(I sent this message 11 hours ago and it still hadnt arrived, so Ill try again. Apologies for any repetition)

On 06/01/2017 11:22, Tobias Fröschle wrote:
The "flooring" i.e. round down approach is normally chosen to make sure that

x = a *(x DIV a) + x MOD a

stays valid for negative numbers. That would not necessarily be the case if 
rounding towards 0.

Tobias

Am 05.01.2017 um 16:55 schrieb Martyn Hill <martyn.joseph.h...@gmail.com>:

Thank you Tobias!

Makes sense with that context.

M.

On 05/01/2017 15:49, Tobias Fröschle wrote:
Martyn,

Integer division for negative numbers is not very well defined. Language designers get to 
choose if their language will round towards zero, negative infinity, or positive infinity 
when doing integer division. Different languages have made different choices. S*Basic 
designers have chosen to use the "flooring" approach (round the floating point 
result to the next smaller number).

Some other reasoning: -1 / 2 should be?
(1) -1/2 = 0 remainder -1
(2)$FFFF ASR 1 = $FFFF = -1

Another note: The original QL allowed word-size divisors and dividends. while 
SMSQ/E extended this to long integers. And the original QL manual even had an 
example illustrating what you see here:

-5 DIV 2 { will output -3}

Tobias


Am 05.01.2017 um 16:27 schrieb Martyn Hill <martyn.joseph.h...@gmail.com>:

Hi everyone

Can anyone tell me the expected behaviour for the integer-divide operator 'DIV' 
in SBASIC, when provided with a negative dividend/numerator?

My number-theory is a bit rusty, but I would have thought that, say, -1 DIV 10 
should result in 0 (with remainder/MOD of -1).

Instead, on QPC2/SBASIC, I get the result -1 for that example - and (almost) 
always 1 less than expected for negative dividends, thus:

12 DIV 10 = 1
11 DIV 10 = 1
10 DIV 10 = 1
   9 DIV 10 = 0
...
   2 DIV 10 = 0
   1 DIV 10 = 0
   0 DIV 10 = 0
* -1 DIV 10 = -1 - expected '0'**
** -2 DIV 10 = -1**
**...**
**  -9 DIV 10 = -1*
*-10 DIV 10 = -1 - as expected*
*-11 DIV 10 = -2 - expected '-1'
-12 DIV 10 = -2

*etc...

Thanks in advance!

Martyn.
_______________________________________________
QL-Users Mailing List
_______________________________________________
QL-Users Mailing List
--
"There are 10 types of people in this world. Those who understand binary and those 
who don't."

_______________________________________________
QL-Users Mailing List
_______________________________________________
QL-Users Mailing List


_______________________________________________
QL-Users Mailing List

Reply via email to