Understanding Modulus Operators in C Programming

  • Thread starter Thread starter ming2194
  • Start date Start date
  • Tags Tags
    Modulus Operators
Join the discussion
Ask a follow-up here, or get your own question answered by working scientists, mathematicians and engineers — people, not an autocomplete.
Real named experts · corrections over time · the nuance an AI answer skips
2 replies · 2K views
ming2194
Messages
31
Reaction score
0
im the very beginner of learning c programe so i would like to ask the follow questions and hope someone can give me some idea::

i know:

3%2 = 1

8%3 = 2

12%4 = 0

but I am not sure about the followings (or my concept is wrong or right):

12%100 = 12
1%2 = 1

can i say when the number after "%" is larger then the number before "%", the ans is equal to the formar number?

how about (-ve)%(+ve) or (+ve)%(-ve) or (-ve)%(-ve) ?

rly cannot get idea.

*it's not homework and it's just created by myself
 
Physics news on Phys.org
ming2194 said:
im the very beginner of learning c programe so i would like to ask the follow questions and hope someone can give me some idea::

i know:

3%2 = 1

8%3 = 2

12%4 = 0

but I am not sure about the followings (or my concept is wrong or right):

12%100 = 12
1%2 = 1

can i say when the number after "%" is larger then the number before "%", the ans is equal to the formar number?

how about (-ve)%(+ve) or (+ve)%(-ve) or (-ve)%(-ve) ?

rly cannot get idea.

*it's not homework and it's just created by myself
Modulus operations are always done on integer types, and are most often done on positive integer types. The modulus operater is associated with integer division in C, which is a kind of division that gives whole-number quotients. For example, 6/2 == 3, which is not surprising, but 7/2 == 3 as well. It is only when one or both numbers in division are floating point numbers that we get a floating point result. E.g, 7.0/2 == 3.5, or 7/2.0 == 3.5.

In an expression a % b, the result is the remainder when a is divided by b. If a < b, then a % b == a, as you have discovered.

For your questions about positive and negative operands use in a modulus expression, I looked in a book I have had for a long time, "The C Programming Language," by Brian Kernighan and Dennis Ritchie. In it, they say "On all machines covered by this manual, the remainder has the same sign as the dividend."

The result of a modulus expression with operands that have mixed signs is compiler dependent. The documentation for Microsoft Visual C says this: "If either operand is negative and the result is inexact, the result is implementation defined." For example, it's possible that different compilers would produce different results for 7 % -2.
 
One argument for a mathematically "correct" answer for mixed sign division and modulo is for the operation to be origin independent. (x+a*y)%(y) should always produce the same result regardless of the value of a or it's sign. As an example of this, imagine a "modulo 10" clock that cycles through the number 0 through 9 incrementing once each second (10 second cycle time); currently the display indicates 4, what will the display read 20 seconds from now (a = +2)? What did the display indicate 40 seconds ago (a = -4)?

If you plot a graph of integer division by some fixed divisor, you should get a stair step graph that doesn't have a double or skipped step at zero. APL is one of the few computer languages to get this right, a language that dates back to the 1960's.

The sign of the remainder should equal the sign of the divisor (or zero), despite the wording in the C reference. It makes more sense and it's more consistent for m%n to have |n| possible values (and not 2|n|-1 values).

Some examples of "correct" results, but it's unlikely you get these results from any modern computer (unless it's running a language that defines this behavior such as APL):

101/10 = +10
101%10 = +1

101/-10 = -11
101%-10 = -9

-101/10 = -11
-101%10 = +9

-101/-10 = -10
-101%-10 = -1

Using that last line, include a multiplier factor, as mentioned above:

(-101 + ( (a)*(-10) ) )%-10 = -1

a = +20 => (-101 + ( (+20)*(-10) ) )%-10 => -301%-10 = -1
a = -20 => (-101 + ( (-20)*(-10) ) )%-10 => +99%-10 = -1

As mentioned, this behavior for mixed signs isn't defined in C, and in most computers integer division won't follow this rule either. Some older computers that implemented division using one type of a "non-restoring" algorithm would get the "correct" results.
 
Last edited: