T
Thomas Matthews
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
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