-
Notifications
You must be signed in to change notification settings - Fork 3
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
modulo of a negative number is expected to be positive #3
Comments
http://mathforum.org/library/drmath/view/52343.html Date: 07/05/2001 at 16:17:04 Hi, Andre. You're right that this relation is relevant to the question; below But it only proves what MOD should do if we know how DIV is defined;
-340 = -6 * 60 + 20 This is perfectly consistent if their integer division truncates
So in fact I did refer to your rule. Here's a more complete answer: Computer languages and libraries are notoriously inconsistent, or at Properly, the modulus operator a mod b should be mathematically
Unfortunately, this statement about FORTRAN, and implicitly about the The problem is that people tend to think of modulus as the same as
so that "%" is really defined as the remainder of integer division as Because FORTRAN defined division and MOD this way, computers have Similarly, in Ada there are two different operators, "mod" (modulus)
A B A/B A rem B A mod B A B A/B A rem B A mod B 10 5 2 0 0 -10 5 -2 0 0 10 -5 -2 0 0 -10 -5 2 0 0 So what's the conclusion? There are basically two models, reasonably
|
clib computes: -12 % 5 = -2
mathematically right: -12 % 5 = 3
This special rare case is not really a mistake, but mathematically the result is expected in [0..4]. So the result should be 3.
Not sure if this bug should be fixed in clib upstream since it is there for decades. A change could break existing applications. Should it be fixed in grpn?
The text was updated successfully, but these errors were encountered: