operator % and signed integers

Discussion in 'C Programming' started by Thomas Matthews, Dec 17, 2005.

1. Thomas MatthewsGuest

Hi,

We had an applications guy use a {signed} int and the operator % in an
embedded system. None of us could figure out if this was a valid
operation, and if so, what is sign of the result?

In searching the newsgroup, I found an article stating that the
operator % is a "remainder operator" not a modulus operator.
Is this true? If so, is the result ever negative?

{Posted to comp.lang.c and comp.lang.c++ because it pertains
to both languages.}

Given:
signed int A;
signed int B;

What are the signs of the result column below
where Result = A % B; /* B != 0, A != 0 */?
A B Result
----------------------------------------------
positive, > B positive
positive, < B positive
negative, magnitude < B positive
-2 * B positive
positive, magnitude > B negative
positive, magnitude < B negative
negative, magnitude < B negative
negative, magnitude > B negative

--
Thomas Matthews (Yep, I'm back.)

C++ newsgroup welcome message:
http://www.slack.net/~shiva/welcome.txt
C++ Faq: http://www.parashift.com/c++-faq-lite
C Faq: http://www.eskimo.com/~scs/c-faq/top.html
alt.comp.lang.learn.c-c++ faq:
http://www.comeaucomputing.com/learn/faq/
Other sites:
http://www.josuttis.com -- C++ STL Library book
http://www.sgi.com/tech/stl -- Standard Template Library

Thomas Matthews, Dec 17, 2005

2. Ivan VecerinaGuest

: We had an applications guy use a {signed} int and the operator % in an
: embedded system. None of us could figure out if this was a valid
: operation, and if so, what is sign of the result?

It is a valid operation. However, to allow the compiler to use
the best-performing signed division operation supported on a
given hardware, the C language had chosen not to specify the
sign of the result. (and this remains the case in C++ today).

The result is therefore defined, but platform-dependent.
This also applies to the division operator:
int x = (-3)/2; // x might be -1 or -2 !!!

: In searching the newsgroup, I found an article stating that the
: operator % is a "remainder operator" not a modulus operator.
: Is this true? If so, is the result ever negative?
It might be.

The only guarantee you have is that / and % are to
behave consistently. I.e.:
void f(int a, int b)
{
int d = a/b;
int r = a%b;
assert( d*b + r == a ); //safe
assert( abs(r) < abs(b) ); //also safe
// And for given signs of a and b, the
// sign of d and r will always be consistent.
}

Yes this situation is akward.
Many are of the opinion that the result of signed
/ and % shall be specified strictly, and that
performance-conscious users will always have the
option to rely on unsigned / and %, which would
still provide the same (best possible) performance
on all platforms.

I hope this helps,
Ivan

Ivan Vecerina, Dec 17, 2005

3. jatGuest

dear sir,
The result will be remainder having the sign of
numerator.

so result can be calculated like this remainder( magnitude(A),
magnitude(B)) * sign(A)

sign of denominator does have any effect on the result.

jat, Dec 17, 2005
4. Keith ThompsonGuest

C99 6.5.5 says:

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 (88). If the
quotient a/b is representable, the expression (a/b)*b + a%b shall
equal a.

with a footnote:

(88) This is often called "truncation toward zero".

Keith Thompson, Dec 17, 2005
5. Jack KleinGuest

The statement above raises a serious issue, at least as far as posting
in comp.lang.c is concerned. As far as the C language is concerned,
C++ does not exist. Actually, I exaggerate. The C language and
standard take no notice, nor any responsibility, for languages "based
on C" or that adopt part of C's syntax. As far as the C standard, and
comp.lang.c are concerned, there is no "Objective C", "Java", "C#",
"D", and who knows how many others.

Where did I exaggerate? The C language and its ISO standard do barely
acknowledge that C++. It is mentioned in no less than four footnotes
in the current C standard, basically at the request of the C++
standard committee. And the C standard specifically forbids a
conforming C implementation from defining a macro "__cplusplus". So
since 1999, C acknowledges that C++ exists.

Why am I making such a point of this? Because C++ adopts part of, but
not all of, an earlier (1995) version of the C standard, and makes
subtle changes to other parts, some of them quiet and likely to trap
the unwary.

So here in comp.lang.c, the only answer is what the C standard
requires and/or allows to happen in C. Whether C++ requires/allows
the same, or something different, is quite off-topic here.
As for the C language, the operation is valid regardless of the signs
of the operands, as long as B is not 0.

Here is exactly what the C standard guarantees for A % B given that A
and B are signed int:

A positive or 0, B positive: result positive or 0.

Any other case: result positive, negative, or 0.

The sign of a non-zero result of the % operator when either or both of
the operands is negative is implementation-defined.

Jack Klein, Dec 17, 2005
6. Chuck F.Guest

I suspect this is a bad idea, inasmuch as the answer is very likely
to be different in the two languages, and can probably only be
answered by careful perusal of the appropriate standards. I also
seem to remember that the answer changed between C90 and C99 (for
C), which further emphasizes the uselessness of the crosspost.

F'ups set.

--
<http://www.ungerhu.com/jxh/clc.welcome.txt>
<http://www.eskimo.com/~scs/C-faq/top.html>
<http://benpfaff.org/writings/clc/off-topic.html>
<http://anubis.dkuug.dk/jtc1/sc22/wg14/www/docs/n869/> (C99)
<http://www.dinkumware.com/refxc.html> (C-library}
<http://gcc.gnu.org/onlinedocs/> (GNU docs)

Chuck F., Dec 19, 2005
7. Pete BeckerGuest

It's also implementation-defined, that is, the implementation is
required to document what the behavior is.

Pete Becker, Dec 26, 2005
8. Chuck F.Guest

I believe this is different from the C90 specification. If I am
right it probably also means that the C and C++ specifications
differ, showing once more how silly it is to cross-post between
c.l.c and c.l.c++. They are different languages. F'ups set.

--
"If you want to post a followup via groups.google.com, don't use
the broken "Reply" link at the bottom of the article. Click on
"show options" at the top of the article, then click on the

Chuck F., Dec 26, 2005
9. Tim RentschGuest

If the result is representable. The expression 'INT_MIN / -1'
might not be representable. Such cases aren't valid either.

It _was_ implementation-defined in C90. In C99 it's well-defined
(division truncates toward zero, remainder consistent with division)
as long as the results are representable.

Tim Rentsch, Dec 28, 2005
10. Jordan AbelGuest

Is it still required that, for C = A%B and D = A/B, that D*B+C==A? [i.e.
the two results are related in a way that makes that expression true,
with the sign of the % result determined by the rounding of the /
result]

Jordan Abel, Dec 28, 2005