Understanding Modulus Operators in C Programming

  • Thread starter Thread starter ming2194
  • Start date Start date
  • Tags Tags
    Modulus Operators
Click For Summary
SUMMARY

The discussion focuses on the modulus operator (%) in C programming, clarifying its behavior with both positive and negative integers. It establishes that for any expression a % b, the result is the remainder when a is divided by b, and if a < b, then a % b equals a. The sign of the result is dependent on the dividend, and the behavior of mixed-sign operands is compiler-dependent, as noted in "The C Programming Language" by Kernighan and Ritchie. Examples illustrate the complexities of modulus operations, particularly with negative numbers, emphasizing that results may vary across different compilers.

PREREQUISITES
  • Understanding of basic arithmetic operations in C programming
  • Familiarity with integer types and division in C
  • Knowledge of compiler behavior and implementation specifics
  • Basic concepts of signed and unsigned integers
NEXT STEPS
  • Study the behavior of the modulus operator in C with negative integers
  • Explore compiler documentation for Microsoft Visual C and GCC regarding modulus operations
  • Learn about integer division and remainder behavior in different programming languages
  • Investigate the mathematical properties of modulus operations in programming
USEFUL FOR

Beginner C programmers, computer science students, and developers interested in understanding arithmetic operations and their implications in programming languages.

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
 
Technology 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:

Similar threads

  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 23 ·
Replies
23
Views
3K
  • · Replies 5 ·
Replies
5
Views
3K
  • · Replies 5 ·
Replies
5
Views
3K
  • · Replies 1 ·
Replies
1
Views
4K
  • · Replies 4 ·
Replies
4
Views
2K
  • · Replies 17 ·
Replies
17
Views
3K
  • · Replies 2 ·
Replies
2
Views
3K
  • · Replies 19 ·
Replies
19
Views
2K
  • · Replies 1 ·
Replies
1
Views
1K