As quoted in "Integer division rounding with negatives in C++", in C before C99 (i.e. in C89) and in C++ before C++11 (i.e. in C++98 and C++03), for an integer division computation where either operand is negative the sign of the remainder (or equivalently, the rounding direction of the quotient) is implementation-defined.
Then comes the standard function std::div which is specified to truncate the quotient towards zero (i.e. the remainder has the same sign as the dividend (numerator)) (see for example this answer to "what is purpose of div() library function?").
Here is glibc's code for div() (source) (also quoted in "Is div function useful (stdlib.h)?"):
(Note: div_t is defined as:
typedef struct
{
int quot;
int rem;
} div_t;
-- end note.)
/* Return the `div_t' representation of NUMER over DENOM. */
div_t
div (numer, denom)
int numer, denom;
{
div_t result;
result.quot = numer / denom;
result.rem = numer % denom;
/* The ANSI standard says that |QUOT| <= |NUMER / DENOM|, where
NUMER / DENOM is to be computed in infinite precision. In
other words, we should always truncate the quotient towards
zero, never -infinity. Machine division and remainer may
work either way when one or both of NUMER or DENOM is
negative. If only one is negative and QUOT has been
truncated towards -infinity, REM will have the same sign as
DENOM and the opposite sign of NUMER; if both are negative
and QUOT has been truncated towards -infinity, REM will be
positive (will have the opposite sign of NUMER). These are
considered `wrong'. If both are NUM and DENOM are positive,
RESULT will always be positive. This all boils down to: if
NUMER >= 0, but REM < 0, we got the wrong answer. In that
case, to get the right answer, add 1 to QUOT and subtract
DENOM from REM. */
if (numer >= 0 && result.rem < 0)
{
++result.quot;
result.rem -= denom;
}
return result;
}
As you can see there is a test after the big comment block, whose purpose is to "correct" the result if the built-in division truncates towards -infinity instead of towards zero.
Now the question:
Isn't there a bug in that code?
Let's first consider the example call div(42, -5). "In math" 42/-5 is exactly -8.4, so theoretically in C89 and C++03 42 / -5 could yield either -8 (truncated) or -9 (floored) depending on the implementation. Reading the code:
If 42 / -5 yields -8 then 42 % -5 yields 2 (as 42 == -8 * -5 + 2), so the test is (42 >= 0 && 2 < 0) which is not true and the above function returns -8 and 2, as wanted;
If 42 / -5 yields -9 then 42 % -5 yields -3 (as 42 == -9 * -5 + -3), so the test is (42 >= 0 && -3 < 0) which is true, so the above function returns the "corrected" -9 + 1 and -3 - -5, i.e. -8 and 2, as wanted.
But now let's consider the call div(-42, 5) (signs inverted):
If -42 / 5 yields -8 then -42 % 5 yields -2 (as -42 == -8 * 5 + -2), so the test is (-42 >= 0 && -2 < 0) which is not true and the above function returns -8 and -2, as wanted;
If -42 / 5 yields -9 then -42 % 5 yields 3 (as -42 == -9 * 5 + 3), so the test is (-42 >= 0 && 3 < 0) which... is not true! and the above function returns -9 and 3 instead of -8 and -2!
The comment in the code above first seems right when it says that the situation that needs a correction is when "REM has the opposite sign of NUMER", but then it makes the huge simplification "This all boils down to: if NUMER >= 0, but REM < 0, we got the wrong answer", which seems wrong (incomplete) to me because it omits the case "if NUMER < 0, but REM > 0" (-42 and 3 in the previous example).
I can hardly believe that such a bug would have remained unnoticed since 1992 or 1990 (apparently someone tried to "fix" it but it still seems incorrect because div(-42, 5) could return -10 and 8)... Arguably, most implementations have been truncating towards zero by default (and all are required to do so starting from C99 and C++11, so the issue is "moot" in the latest Standards 1) so the bug wouldn't manifest on them, but still... Maybe I'm missing something here?
Thank you for any insights.
1 (Edit) As for "the issue is moot in C++11 and C99 (and newer)": accordingly, in these Standards the built-in division is required to truncate towards zero, so we never need to adjust the result, but doesn't that then mean that the present implementation is more complex than needed and unnecessarily inefficient? The "big comment" is obsolete and the if test useless, so shouldn't that part just be removed entirely?
As the original author of the code, I have to say: You're right. It's broken. We had no systems that behaved "the wrong way" for testing, and I probably wrote the above too late (or early...) in the day or something.
We're saved by the newer standards, and the entire thing should be cleaned out, maybe with a small #ifdef (and corrected adjustment code) for pre-C99 if necessary.
(Also I'll note that the original was not indented with GNU-style indentation :-) )
There's rationale, why it can't be implementation-defined and what must it be.
TLDR: remainder sign must match the division sign.
Rationale:
When recovering divided value, knowing divions result, remainder and divisor, it remainder has to be summed with divisor-division product. If remander flag is implementation-specific, than operation to sum it should be also implementation-specific, is not it? Or at least have a code, after division, which would fix remainder sign if it's incorrect.
Related
In a C program I was trying the below operations (Just to check the behavior)
x = 5 % (-3);
y = (-5) % (3);
z = (-5) % (-3);
printf("%d ,%d ,%d", x, y, z);
It gave me output as (2, -2 , -2) in gcc. I was expecting a positive result every time. Can a modulus be negative? Can anybody explain this behavior?
C99 requires that when a/b is representable:
(a/b) * b + a%b shall equal a
This makes sense, logically. Right?
Let's see what this leads to:
Example A. 5/(-3) is -1
=> (-1) * (-3) + 5%(-3) = 5
This can only happen if 5%(-3) is 2.
Example B. (-5)/3 is -1
=> (-1) * 3 + (-5)%3 = -5
This can only happen if (-5)%3 is -2
The % operator in C is not the modulo operator but the remainder operator.
Modulo and remainder operators differ with respect to negative values.
With a remainder operator, the sign of the result is the same as the sign of the dividend (numerator) while with a modulo operator the sign of the result is the same as the divisor (denominator).
C defines the % operation for a % b as:
a == (a / b * b) + a % b
with / the integer division with truncation towards 0. That's the truncation that is done towards 0 (and not towards negative inifinity) that defines the % as a remainder operator rather than a modulo operator.
Based on the C99 Specification: a == (a / b) * b + a % b
We can write a function to calculate (a % b) == a - (a / b) * b!
int remainder(int a, int b)
{
return a - (a / b) * b;
}
For modulo operation, we can have the following function (assuming b > 0)
int mod(int a, int b)
{
int r = a % b;
return r < 0 ? r + b : r;
}
My conclusion is that a % b in C is a remainder operation and NOT a modulo operation.
I don't think there isn't any need to check if the number is negative.
A simple function to find the positive modulo would be this -
Edit: Assuming N > 0 and N + N - 1 <= INT_MAX
int modulo(int x,int N){
return (x % N + N) %N;
}
This will work for both positive and negative values of x.
Original P.S: also as pointed out by #chux, If your x and N may reach something like INT_MAX-1 and INT_MAX respectively, just replace int with long long int.
And If they are crossing limits of long long as well (i.e. near LLONG_MAX), then you shall handle positive and negative cases separately as described in other answers here.
Can a modulus be negative?
% can be negative as it is the remainder operator, the remainder after division, not after Euclidean_division. Since C99 the result may be 0, negative or positive.
// a % b
7 % 3 --> 1
7 % -3 --> 1
-7 % 3 --> -1
-7 % -3 --> -1
The modulo OP wanted is a classic Euclidean modulo, not %.
I was expecting a positive result every time.
To perform a Euclidean modulo that is well defined whenever a/b is defined, a,b are of any sign and the result is never negative:
int modulo_Euclidean(int a, int b) {
int m = a % b;
if (m < 0) {
// m += (b < 0) ? -b : b; // avoid this form: it is UB when b == INT_MIN
m = (b < 0) ? m - b : m + b;
}
return m;
}
modulo_Euclidean( 7, 3) --> 1
modulo_Euclidean( 7, -3) --> 1
modulo_Euclidean(-7, 3) --> 2
modulo_Euclidean(-7, -3) --> 2
The other answers have explained in C99 or later, division of integers involving negative operands always truncate towards zero.
Note that, in C89, whether the result round upward or downward is implementation-defined. Because (a/b) * b + a%b equals a in all standards, the result of % involving negative operands is also implementation-defined in C89.
According to C99 standard, section 6.5.5
Multiplicative operators, the following is required:
(a / b) * b + a % b = a
Conclusion
The sign of the result of a remainder operation, according
to C99, is the same as the dividend's one.
Let's see some examples (dividend / divisor):
When only dividend is negative
(-3 / 2) * 2 + -3 % 2 = -3
(-3 / 2) * 2 = -2
(-3 % 2) must be -1
When only divisor is negative
(3 / -2) * -2 + 3 % -2 = 3
(3 / -2) * -2 = 2
(3 % -2) must be 1
When both divisor and dividend are negative
(-3 / -2) * -2 + -3 % -2 = -3
(-3 / -2) * -2 = -2
(-3 % -2) must be -1
6.5.5 Multiplicative operators
Syntax
multiplicative-expression:
cast-expression
multiplicative-expression * cast-expression
multiplicative-expression / cast-expression
multiplicative-expression % cast-expression
Constraints
Each of the operands shall have arithmetic type. The
operands of the % operator shall have integer type.
Semantics
The usual arithmetic conversions are performed on the
operands.
The result of the binary * operator is the product of
the operands.
The result of the / operator is the quotient from
the division of the first operand by the second; the
result of the % operator is the remainder. In both
operations, if the value of the second operand is zero,
the behavior is undefined.
When integers are divided, the result of the / operator
is the algebraic quotient with any fractional part
discarded [1]. If the quotient a/b is representable,
the expression (a/b)*b + a%b shall equal a.
[1]: This is often called "truncation toward zero".
The result of Modulo operation depends on the sign of numerator, and thus you're getting -2 for y and z
Here's the reference
http://www.chemie.fu-berlin.de/chemnet/use/info/libc/libc_14.html
Integer Division
This section describes functions for performing integer division.
These functions are redundant in the GNU C library, since in GNU C the
'/' operator always rounds towards zero. But in other C
implementations, '/' may round differently with negative arguments.
div and ldiv are useful because they specify how to round the
quotient: towards zero. The remainder has the same sign as the
numerator.
In Mathematics, where these conventions stem from, there is no assertion that modulo arithmetic should yield a positive result.
Eg.
1 mod 5 = 1, but it can also equal -4. That is, 1/5 yields a remainder 1 from 0 or -4 from 5. (Both factors of 5)
Similarly,
-1 mod 5 = -1, but it can also equal 4. That is, -1/5 yields a remainder -1 from 0 or 4 from -5. (Both factors of 5)
For further reading look into equivalence classes in Mathematics.
Modulus operator gives the remainder.
Modulus operator in c usually takes the sign of the numerator
x = 5 % (-3) - here numerator is positive hence it results in 2
y = (-5) % (3) - here numerator is negative hence it results -2
z = (-5) % (-3) - here numerator is negative hence it results -2
Also modulus(remainder) operator can only be used with integer type and cannot be used with floating point.
I believe it's more useful to think of mod as it's defined in abstract arithmetic; not as an operation, but as a whole different class of arithmetic, with different elements, and different operators. That means addition in mod 3 is not the same as the "normal" addition; that is; integer addition.
So when you do:
5 % -3
You are trying to map the integer 5 to an element in the set of mod -3. These are the elements of mod -3:
{ 0, -2, -1 }
So:
0 => 0, 1 => -2, 2 => -1, 3 => 0, 4 => -2, 5 => -1
Say you have to stay up for some reason 30 hours, how many hours will you have left of that day? 30 mod -24.
But what C implements is not mod, it's a remainder. Anyway, the point is that it does make sense to return negatives.
It seems the problem is that / is not floor operation.
int mod(int m, float n)
{
return m - floor(m/n)*n;
}
I wanted to test what happens when I write this code. I can not explain the following case:
Input: 5
Output: -6
#include <iostream>
int lastBit(int n){ return ~(n); }
int main() { std::cout << lastBit(5); }
Computers express negative numbers in quite a specific way. Values are always stored as series of bits and there is no way of introducing negative sign, so this has to be solved differently: one of bits plays role of a negative sign.
But this is not all - the system must be designed to handle maths properly (and ideally, the same way as for positive numbers).
So for instance 0 == 0b00000000. If you subtract 1 from 0, you get -1, but from the binary perspective, due to "binary underflow", 0b00000000 - 0b00000001 == 0b11111111, hence 0b11111111 == -1.
If you then subtract 1 from -1, you get 0b11111111 - 0b00000001 == 0b11111110 == -2. But 2 == 0b00000010, which shows, why -2 != ~2 (and the same rule applies to next values).
The very short, but maybe more intuitive answer might be: "-5 != ~5, because there is only one zero binarily (eg. 0 == -0), so there is always one more negative value than positive ones"
Not on all systems but on systems that use complement of two for signed values. By definition there, the binary representation of negative X = -n, where n is a positive integer, is ~n + 1, which allows signed and unsigned addition operations to be same.
Until C++20 result of ~(n) for signed negative n here would be undefined, because it depends on platform and compiler. In C++20 it's required to behave as if complement of two is used.
I found out the following
5 = 0101
Therefore, ~(5) = 1010
The 1 at the most significant bit denotes negativee (-)
010 = 6
Therefore, output is -6
Since this is a 2's complement machine
I am having trouble understanding how exactly this code works:
int length = 1;
int x = 234567545;
while (x /= 10)
length++;
It is supposed to count the number of digits in the int variable. I don't get how the while loop is working. Does the loop just go to zero and stop by default? Also, why is the length starting at 1?
There are three things that might be suspicious for you if you are a C++ beginner:
First thing might be operator /=, which combines an integral division (i.e. without remainder), with an assignment. So x /= 10 actually is the same as x = x / 10.
Second, each expression in C++ has - after having been evaluated - a value. For an assignment like (x = 0), the result is the value of x after the assignment, i.e. 0 in this case.
Third, a condition like if (x) ... with x being of integral type has in C++ the same meaning as if(x != 0), i.e. it is false if x equals 0, and it is true if x is anything else but 0.
All together: while ( x /= 10 ) means assign x the value of an integral division by 10 and then compare the value to 0. If 0 is reached, the loop ends.
BTW: length starts with 1, because any number, even 0, comprises at least one digit.
x /= 10 continuously divides x by 10, which will make it 0 eventually and cause the while loop to terminate due to 0 being interpreted as false (and any other value than 0 as true).
The reason it starts at length = 1 is because there is always at least 1 digit in the number: if x was from 0 to 9 inclusive, then x /= 10 would cause x to become 0 immediately, meaning nothing inside the loop would execute. Therefore, if length started at 0, it would never get to be incremented to 1 which would be wrong if x was a single digit large.
Manually calculating this example by hand:
234567545 / 10 = 23456754, which is true, so the while loop continues and length becomes 2.
23456754 / 10 = 2345675, true. length becomes 3.
2345675 / 10 = 234567, true. length becomes 4.
234567 / 10 = 23456, true. length becomes 5.
23456 / 10 = 2345, true. length becomes 6.
2345 / 10 = 234, true. length becomes 7.
234 / 10 = 23, true. length becomes 8.
23 / 10 = 2, true. length becomes 9.
2 / 10 = 0, false. The while loop stops with length equal 9.
The loop
while (x /= 10) {
length++;
}
will go until the result of x /= 10 evaluates to false, since 0 means false it will go until x /= 10 is 0. Integer division truncates, ensuring the condition will be reached. This can be illustrated by adding a simple debug statement, i.e.
while (x /= 10) {
length++;
std::cout << length << " " << x << std::endl;
}
Which outputs
2 23456754
3 2345675
4 234567
5 23456
6 2345
7 234
8 23
9 2
Integer division will truncate the remainder, so continually dividing a number with integer division will inevitably result in zero.
Dividing a number n by 10 while incrementing a counter i once for each time the resulting quotient (stored back into n) is not zero will result in the i containing the number of digits for the base-10 representation of n.
It helps to understand two parts:
what is "/="
when does the loop terminate
Explain "/="
This:
x /= 10
is the same as:
x = x / 10
Explain when the loop terminates
The while loop terminates, when the condition is false. And 0 is equivalent to false.
while (condition) {
length++;
}
So x is, with every pass through the loop, divided by 10, until is is 0. That terminates the loop.
So, the condition is two things at the same time:
it is a value, that is compared to 0. The loop continues until this
evaluates to 0.
it is an assignment: x gets a new value with every evaluation. It's divided by 10, so it converges to 0.
This is a bit of stupidity you'll often see in C/C++, taking advantage of the fact that TRUE is implemented as non-zero, and FALSE as zero*. So x is repeatedly divided by 10, and the expression eventually becomes zero, so the loop stops.
Though confusing, this works - until the day someone in a hurry changes x from an int to a double :-) Much clearer and less failure-prone to write "while (x /= 10 >= 1)", or even to put the math inside the loop body rather than in the condition.
*IMHO one of the few shortcomings of C is that it didn't have an explicit logical type, as FORTRAN did.
I have seen many answers for questions concerning modulo of negative numbers. Every answer placed the standard
(a/b)*b + a%b is equal to a
explanation. I can calculate any modulo with this method, and I understand it's necessary employ a modulo function that adds b to the value of a%b if it is negative for the modulo to make sense.
I am trying to make sense of this in laymen's terms. Just what is the modulo of a negative number? I read somewhere that you can calculate the proper modulo of negative numbers by hand some laymen's method of just adding numbers together. This would be helpful, because the a/b *b + a%b method is a little tedious.
To clarify, the modulo of a positive number can be interpreted in laymen's terms as the remainder when you divide the numbers. Obviously this isn't true in the case of negative numbers, so how do you properly "make sense" of the result?
This used to be implementation-defined in older revisions of C++, but now it's all fully specified:
Division truncates, i.e. a / b is the mathematical value with the fractional part discarded. For example, 9 / -5 is the truncation of −1.8, so it's -1.
The remainder operation a % b is defined by the identity you presented. So let's compute: (a / b) * b is -1 * -5 is 5, so 9 % -5 is 4.
By contrast,-9 % 5 is -4. So even though a / -b is the same as -a / b, a % -b is in general different from -a % b. (Similarly, the mathematical notion of modular equivalence, where two integers are congruent modulo n if they differ by an integral multiple of n, is invariant under replacing n with -n.)
TL;DR: There is a difference between modulo operator which is used in math and C++ % operator.
For example, let f(x) = x % 4. Then:
x : -9 -8 -7 -6 -5 -4 -3 -2 -1 0 1 2 3 4 5 6 7 8 9
f(x) in math : 3 0 1 2 3 0 1 2 3 0 1 2 3 0 1 2 3 0 1
f(x) in C : -1 -0 -3 -2 -1 0 -3 -2 -1 0 1 2 3 0 1 2 3 0 1
^~~~~~~~~~~~~~~~~~~~~~~~^
This part is different
You don't need any special tricks to compute C++-style % of a negative number.
Just use a%b == a - (a/b)*b, which is derived from (a/b)*b + a%b == a.
Long answer:
Cppreference says following:
The binary operator % yields the remainder of the integer division of the first operand by the second (after usual arithmetic conversions; note that the operand types must be integral types). If the quotient a/b is representable in the result type, (a/b)*b + a%b == a. If the second operand is zero, the behavior is undefined. If the quotient a/b is not representable in the result type, the behavior of both a/b and a%b is undefined (that means INT_MIN%-1 is undefined on 2's complement systems)
Note: Until C++11, if one or both operands to binary operator % were negative, the sign of the remainder was implementation-defined, as it depends on the rounding direction of integer division. The function std::div provided well-defined behavior in that case.
The important parts are:
(a/b)*b + a%b == a.
"Until C++11, if one or both operands to binary operator % were negative, the sign of the remainder was implementation-defined."
This implies that since C++11 the operator is well-defined for negative operands too.
There is no mention of any special handling of negative operands, thus we can say that above identity words for them too.
From (a/b)*b + a%b == a we can easily derive a formula for a%b:
a%b == a - (a/b)*b
If you think about it, this formula ignores the sign of b, works as if the modulo was computed with the absolute value of a, with the sign of a appended to the result.
If you want to compute the "classical" modulo, you may use something like following function:
template <typename T, typename TT> constexpr T true_mod(T a, TT b)
{
static_assert(std::is_integral<T>::value &&
std::is_integral<TT>::value, "Argument types must be integral.");
if (a >= 0)
return a % b;
else
return (b >= 0 ? b : -b) - 1 + (a + 1) % b;
}
(a/b)*b + a%b is equal to a
Even if this statement is true, the result can change from a language to an other.
This difference also depends on the result of the division.
for example, in python, I have:
>>> # in python, use "//" for the floor division
>>> 3 // 4 # 0.75 is rounded to 0 : OK
0
>>> 3 % 4 # still as expected
3
>>> 0 * 4 + 3 # standard valided
3
>>> (-3) // 4 # -0.75 is rounded to -1, not 0 (the floor)
-1
>>> (-3) % 4 # the result is consistant; a modulo garanteed to be between 0 and b is very usefull
1
>>> (-1) * 4 + 1 # standard valided
-3
>>> 3 // (-4) # -0.75 is rounded to -1, not 0 (the floor)
-1
>>> 3 % (-4) # still a number between 0 and b
-1
>>> (-1) * (-4) + (-1) # standard valided
3
SUMMARY:
MODULO TEST: language=python
a=3 b=4 a/b=0 a%b=3 standard:true
a=-3 b=4 a/b=-1 a%b=1 standard:true
a=3 b=-4 a/b=-1 a%b=-1 standard:true
a=-3 b=-4 a/b=0 a%b=-3 standard:true
If my memory is good, the modulo doesn't work like that in C, even if the standard is valided. It can be very disturbing.
I've juste written a little programm to test the results in C:
#include <stdio.h>
void test(int a, int b) {
int q = a/b;
int r = a%b;
int ok = q*b+r == a;
printf("a=%-2d b=%-2d a/b=%-2d a%%b=%-2d standard:%s\n", a, b, q, r, ok?"true":"false");
}
int main(int argc, char const *argv[]) {
printf("MODULO TEST: language=c\n");
test( 3, 4);
test(-3, 4);
test( 3,-4);
test(-3,-4);
return 0;
}
which gives:
MODULO TEST: language=c
a=3 b=4 a/b=0 a%b=3 standard:true
a=-3 b=4 a/b=0 a%b=-3 standard:true
a=3 b=-4 a/b=0 a%b=3 standard:true
a=-3 b=-4 a/b=0 a%b=-3 standard:true
So yes, the standard is not enough to fix a unique method for the modulo of two (negative) numbers.
You could use this code when the left number has an unknown sign :
int mod = a % b;
if (mod*b < 0) mod += b;
This code will give you a number between 0 and b all the time, like in python (0 <= mod < b, or b < mod <= 0 if b is negative).
The * b is useless if b is strictly positive (in most of the cases).
EDIT
Using a XOR is better than a multiplication, as it prevents overflow.
int mod = a % b;
if ((mod < 0) ^ (b < 0)) mod += b;
And when b is strictly positive:
int mod = a % b;
if (mod < 0) mod += b;
EDIT 2 (2018-10-09)
Better use this, to use python-style division (modulo between 0 included and b excluded) in C:
int q = a / b;
int r = a % b;
if ((b<0) ? (r<0) : (r>0)) {
q -= 1;
r += b;
}
It prevents "extremes" cases like b is negative and divides a (like 6 % (-3)). The result must be 0.
$5.6/4 in C++03 states- "If both
operands are nonnegative then the
remainder is nonnegative;if not, the
sign of the remainder is
implementation-defined74).
where Note 74 is
According to work underway toward the
revision of ISO C, the preferred
algorithm for integer division
follows the rules defined inthe ISO
Fortran standard, ISO/IEC 1539:1991,
in which the quotient is always
rounded toward zero.
C++0x states -
$5.6/4- "For integral operands the / operator yields the algebraic quotient with any fractional part discarded;79 if the quotient a/b is representable in the type of the result, (a/b)*b + a%b is equal to a.
Note 79 says
This is often called truncation towards zero.
So I have two questions:
Can someone explain this concept of 'truncation towards zero'?
Is modulus with negative operands a defined behavior in C++0x?
Truncation towards zero means converting a real number to an integer by selecting the next integer nearest zero. Equivalently, you write the number down, and ignore everything after the decimal point, whether the number is positive or negative.
Consider 11/4 = 2.75 -- if you truncate this towards zero, you get 2.
consider -11/4 or 11/-4 = -2.75 -- if you truncate this towards zero, you get -2.
It's important for some maths operations that (a/b)*b + a%b == a. If we must make this equation hold, and we also accept that integer division truncates towards zero, then we can deduce the operation of the % operator as follows:
a == 11, b == 4:
a/b == 2, 2*4 + a%b == 11, therefore a%b == 3.
a == -11, b == 4:
a/b == -2, -2 * 4 + a%b == -11, therefore a%b == -3.
a == 11, b == -4:
a/b == -2, -2 * -4 + a%b == 11, therefore a%b == 3.
a == -11, b == -4:
a/b == 2, 2 * -4 + a%b == -11, therefore a%b == -3.
It might be confusing at first, but C++0x is defining the behaviour of the a%b operator using the equation (a/b)*b + a%b == a. This equation holds even for negative numbers, so a%b is defined for negative numbers.
a) Consider (±5)/(±3) -> ±1. On the number line:
(-5)/3 5/3
5/(-3) (-5)/(-3)
= =
-1.66 --> -1 1 <-- 1.66
v v v v
- + - - - + - - - + - - - + - - - + -
| | | | |
-2 -1 0 1 2
the rounding is thus towards zero.
b) Yes. Since a/b is now defined for all a and b (except b == 0), and (a/b)*b + a%b == a, there is only 1 unique solution for a%b, so the % operator is also well-defiend for all a and b (except b == 0).
a) "Truncation towards zero" simply means that any decimal part is lopped off. The truncated number is always at least as close to 0 as, and usually closer than, the original number.
This is most noticeable in negative numbers, but the goal is to make it less tricky to use / and % with negative numbers (since currently, any implementation can handle it however they choose). -7/4 can be thought of two ways: -2 with a remainder of 1, or -1 with a remainder of -3. And there are compilers and processors that handle it both ways. Since -7/4 is actually -1.75, "truncation toward zero" would give you -1, so the latter way is what'll be standard.
b)
That's what it looks like. It's always been semi-defined ("implementation-defined"), but this looks like an attempt to define what should have been standard from the beginning.