lundi 5 septembre 2016

C++ size_t modulus operation with negative operand

So there are three values that a modulus operation can give you

Let:

size_t n1 = 5, n2 = 4;

Then:

-7 % 5 = 3 (math, remainder >= 0)

-7 % 5 = -2 (C++ general)

-7 % n1 = 4 (C++ if n1 is of type size_t)

Another example:

-7 % 4 = 1 (math, remainder >= 0)

-7 % 4 = -3 (C++ general)

-7 % n2 = 1 (C++ if n2 is of type size_t)

When the left hand operand is positive, the answer between all three methods are the same. How is the value modulus operation calculated using size_t?

Aucun commentaire:

Enregistrer un commentaire