# Thread: Why so many programs return wrong remainder?

1. ## Why so many programs return wrong remainder?

Why many programs including all C flavours compilers, windows calc, Excel....
Code:
```#include <iostream> /* TEST, SEE WITH YOUR EYES */
int main()<% std::cout << -3 % 2; %>```
...return wrong result? (-1 instead of 1, in this case)

I guess it is something about how to the machine compare less significant bits.
But I'd like to know where is the problem exactly.

Do someone know? Thanks.

*** Note: actually there is a small imprecision, b CAN be negative and 0 <= r < abs(b). Probably it is wrote that wat because it won't affect remainder.

2. According to the Wikipedia Entry, for the set of integers, Z, remainder is defined as follows:

If a and d are integers, with d non-zero, then a remainder is an integer r such that a = qd + r for some integer q, and with 0 ≤ |r| < |d|.
The way it is defined in the link you provided, it seems a if it applies more towards natural numbers, even though it mentionse all integers earlier on.

3. Wiki is wrong.
If it was correct it would mean that remainder is not unique.

7 % 3 -> 1 or -2 ?
13 % -4 -> 1 or -3 ?
-4 % 3 -> 2 or -1 ?
-6 % -4 -> 2 or -2 ?
Remainder IS positive.

If remainder would not unique also division result not would be...
7 / 3 -> 2 or 3?

4. >Why many programs including all C flavours compilers, windows calc, Excel....
Let's stick to C++, since it's easier to prove you're wrong that way.

>...return wrong result? (-1 instead of 1, in this case)
What makes you think it's wrong? The result is only "wrong" if it fails to meet the requirements as defined by the standard. The standard requires (ignoring division by 0) that (a/b)*b + a%b is equal to a. In fact, it is stated very clearly that if either a or b is negative, the sign of the remainder is implementation defined, but it's easy to see why your example would have to result in -1 or be wrong for real because it would fail to meet the requirement.

>But I'd like to know where is the problem exactly.
The problem is in your understanding of what should happen. Just because in college you always used absolute values for finding the remainder doesn't mean that that's the only way to do it.

5. Aren't there two possibilities allowed by the standard?

Either (-3) / 2 = -1 and (-3) % 2 = -1
Or (-3) / 2 = -2 and (-3) % 2 = 1

Aren't both these cases allowed? Both satisfy the formula Prelude posted. The standard only suggests that the algorithm follows Fortran.

6. >Aren't there two possibilities allowed by the standard?
Yes, it serves me right for trying to do math (which I'm notoriously bad at). Though everything besides "would have to result in -1 or be wrong" still stands. The standard says that it's implementation defined, and unless the implementation fails to do what it says, there's nothing wrong with the result, even if the OP doesn't agree.

7. Thanks for nothing.

8. I was taught in school that the remainder is always positive. So the remainer of -3/2 is indeed 1. Either my math teacher was wrong ( unlikely ), there is a more sophisticated way to calculate remainders taught later in university ( unlikely ) or the definition does not conform to popular third grade math rules.

9. even in high school I was taught that there were such things as negative remainders... just like there are negative differences for subtraction...

take this situation: say somebody wants to put something on layaway for \$300. you decide that you want to have them make a deposit for the total price divided by two. now, this may get tricky (with the negatives), but anybody who's taken accounting 101 would be able to follow (from the other person's P.O.V.)

I owe \$300 (-300)
I need to pay the remainder of what I owe divided by two (-300%2)
I owe \$100 this month (-100)

now, as you can see, that clearly explains why -300%2=-100 this is the way I've always learned it. if that equation came out to +100, then you would end up owing this guy \$100 this month.

10. >Thanks for nothing.
You asked a question and got the answer. If you don't like the answer then that's your problem. Or were you just trying to act smart by claiming that you're right and every program in the world is wrong?