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

"S*Basic designers have chosen to use the "flooring" approach (round the floating 
point result to the next smaller number)"

This was some time ago but I don't think it is quite that. It is to do with the REMAINDER and not the result. For positive divisors, the remainder is defined to be in the range 0 to divisor-1 (the modulus) and the operation is defined to be reversible (quotient * divisor + remainder => original value of dividend). (Jan Jones is a mathematician). This does not correspond to the MC68000 instruction set definition that Martyn Hill seems to expect.

Viewed another way, Martyn Hill's expected result would twice as many 0 quotients as any other value of quotient for a uniform distribution of dividends. This uneven distribution can be seen clearly as a discontinuity in top graph in the Wiki article Modulo Operation.

BUT !

If the divisor is negative, there is a problem. The "Euclidian division" definition requires that the remainder be between 0 and |divisor|-1. This would seem to be a rather less than rigorous definition as |divisor| is not a continuous function. In SBASIC the MOD function gives a remainder with the same sign as the divisor, which seemed, at the time, to be more conventional, more logical and more useful. This forces the use of the "floored division" approach (second graph in the Wiki article Modulo Operation) as described by Tobias. Floored division for DIV was not the choice, it was a consequence of the choice made for the MOD operation.

Tony Tebby
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


_______________________________________________
QL-Users Mailing List

Reply via email to