- **C Programming**
(*http://www.velocityreviews.com/forums/f42-c-programming.html*)

- - **operator % and signed integers**
(*http://www.velocityreviews.com/forums/t440574-operator-and-signed-integers.html*)

operator % and signed integersHi,
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 |

Re: operator % and signed integers"Thomas Matthews" <Thomas_Hates_Spam@cox.network> wrote in message
news:43A39C8C.7070505@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 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 -- http://ivan.vecerina.com/contact/?subject=NG_POST <- email contact form Brainbench MVP for C++ <> http://www.brainbench.com |

Re: operator % and signed integersdear 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. |

Re: operator % and signed integersThomas Matthews <Thomas_Hates_Spam@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) kst-u@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. |

Re: operator % and signed integersOn Fri, 16 Dec 2005 21:05:16 -0800, Thomas Matthews
<Thomas_Hates_Spam@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 off-topic 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 non-zero result of the % operator when either or both of the operands is negative is implementation-defined. -- Jack Klein Home: http://JK-Technology.Com FAQs for comp.lang.c http://www.eskimo.com/~scs/C-faq/top.html comp.lang.c++ http://www.parashift.com/c++-faq-lite/ alt.comp.lang.learn.c-c++ http://www.contrib.andrew.cmu.edu/~a...FAQ-acllc.html |

Re: operator % and signed integersThomas 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/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) |

Re: operator % and signed integersIvan Vecerina wrote:
> > The result is therefore defined, but platform-dependent. It's also implementation-defined, that is, the implementation is required to document what the behavior is. -- Pete Becker Dinkumware, Ltd. (http://www.dinkumware.com) |

Re: operator % and signed integersKeith Thompson wrote:
> Thomas Matthews <Thomas_Hates_Spam@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 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 "Reply" at the bottom of the article headers." - Keith Thompson More details at: <http://cfaj.freeshell.org/google/> |

Re: operator % and signed integersJack Klein <jackklein@spamcop.net> writes:
> On Fri, 16 Dec 2005 21:05:16 -0800, Thomas Matthews > <Thomas_Hates_Spam@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.} > > [C++ portion snipped] > > > > > 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. If the result is representable. The expression 'INT_MIN / -1' might not be representable. Such cases aren't valid either. > 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. 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. |

Re: operator % and signed integersOn 2005-12-17, Jack Klein <jackklein@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 non-zero result of the % operator when either or both of > the operands is negative is implementation-defined. 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] |

All times are GMT. The time now is 09:52 AM. |

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.

SEO by vBSEO ©2010, Crawlability, Inc.