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

Reply via email to