P: n/a

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++faqlite
C Faq: http://www.eskimo.com/~scs/cfaq/top.html
alt.comp.lang.learn.cc++ 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  
Share this Question
P: n/a

"Thomas Matthews" <Th***************@cox.network> wrote in message
news:43**************@cox.network...
: 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 bestperforming 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 platformdependent.
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
performanceconscious 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
 http://ivan.vecerina.com/contact/?subject=NG_POST < email contact form
Brainbench MVP for C++ <> http://www.brainbench.com  
P: n/a

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.  
P: n/a

Thomas Matthews <Th***************@cox.network> writes: 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.}
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 (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.  
P: n/a

On Fri, 16 Dec 2005 21:05:16 0800, Thomas Matthews
<Th***************@cox.network> wrote in comp.lang.c: 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.}
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 offtopic here. 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
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 nonzero result of the % operator when either or both of
the operands is negative is implementationdefined.

Jack Klein
Home: http://JKTechnology.Com
FAQs for
comp.lang.c http://www.eskimo.com/~scs/Cfaq/top.html
comp.lang.c++ http://www.parashift.com/c++faqlite/
alt.comp.lang.learn.cc++ http://www.contrib.andrew.cmu.edu/~a...FAQacllc.html  
P: n/a

Thomas Matthews wrote: 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.}
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.

Some useful references about C:
<http://www.ungerhu.com/jxh/clc.welcome.txt>
<http://www.eskimo.com/~scs/Cfaq/top.html>
<http://benpfaff.org/writings/clc/offtopic.html>
<http://anubis.dkuug.dk/jtc1/sc22/wg14/www/docs/n869/> (C99)
<http://www.dinkumware.com/refxc.html> (Clibrary}
<http://gcc.gnu.org/onlinedocs/> (GNU docs)  
P: n/a

Ivan Vecerina wrote: The result is therefore defined, but platformdependent.
It's also implementationdefined, that is, the implementation is
required to document what the behavior is.

Pete Becker
Dinkumware, Ltd. ( http://www.dinkumware.com)  
P: n/a

Keith Thompson wrote: Thomas Matthews <Th***************@cox.network> writes:
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.}
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".
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 crosspost 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
"Reply" at the bottom of the article headers."  Keith Thompson
More details at: <http://cfaj.freeshell.org/google/>  
P: n/a

On 20051217, Jack Klein <ja*******@spamcop.net> wrote: 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 nonzero result of the % operator when either or both of the operands is negative is implementationdefined.
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]   This discussion thread is closed Replies have been disabled for this discussion.   Question stats  viewed: 5039
 replies: 8
 date asked: Dec 17 '05
