"Jean-Denis Muys" <[email protected]>
wrote in message news:c6386d6d.45a7%[email protected]
> On 5/19/09 1:57 PM, "Igor Tandetnik"
> <[email protected]> wrote:
>
>> "John Machin" <[email protected]>
>> wrote in message
>> news:[email protected]
>>> It's handy for checking how things work e.g.
>>>
>>> sqlite> select (-1) % 7;
>>> -1
>>> sqlite> -- it's not a real modulo operator :-(
>>
>> What do you feel is wrong with this result? What should a "real"
>> modulo operator return, in your opinion? Before you answer, note
>> that you very likely want this equality to hold for all a, b!=0 :
>>
>> a = q*b + r
>> where q = a/b, r = a%b
>>
>> Igor Tanetnik
>>
>
> My math courses taught me a long time ago, that the remainder r in
> Euclidian division of a by b is defined by:
>
> There exists unique natural numbers q and r such as:
>
> a = b*q+r
> 0 <= r < b
>
> q is defined as the quotient, r is defined as the remainder.
>
> So if the % operator wants to match that math definition, its results
> should
> never be negative. In the example given, (-1)%7 should therefore be 6.

Well then, for the equality to hold, (-1)/7 should be -1. Would you be 
happy with such an outcome?

Wikipedia gives a definition different from yours, for what it's worth:

http://en.wikipedia.org/wiki/Remainder#The_case_of_general_integers
http://en.wikipedia.org/wiki/Modulo_operation

Once you decide that division truncates towards zero (as is the 
long-standing tradition in C and many other languages, for better or 
worse), you have no choice but to let the remainder take the sign of the 
dividend.

Igor Tandetnik



_______________________________________________
sqlite-users mailing list
[email protected]
http://sqlite.org:8080/cgi-bin/mailman/listinfo/sqlite-users

Reply via email to