473,402 Members | 2,072 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,402 software developers and data experts.

operator % and signed integers

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

Dec 17 '05 #1
8 5324
"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 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
Dec 17 '05 #2
jat
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.

Dec 17 '05 #3
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.
Dec 17 '05 #4
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 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
Dec 17 '05 #5
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/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)

Dec 19 '05 #6
Ivan 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)
Dec 26 '05 #7
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 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/>
Dec 26 '05 #8
On 2005-12-17, 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 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]
Dec 28 '05 #9

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

12
by: chirs | last post by:
Hi, These 2 lines should be the same. But in IE5, they display 2 different numbers. Why? document.write((0xfffffff0).toString(10) + "<br>"); document.write((~0xf).toString(10) + "<br>"); ...
44
by: JKop | last post by:
You know how the saying goes that *unsigned* overflow is... well.. defined. That means that if you add 1 to its maximum value, then you know exactly what value it will have afterward on all...
9
by: Thomas Matthews | last post by:
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? ...
11
by: Frederick Gotham | last post by:
I'd like to discuss the use of signed integers types where unsigned integers types would suffice. A common example would be: #include <cassert> #include <cstddef> int...
9
by: yuyang08 | last post by:
Dear all, Is there a logic shift operator in C++? I tried ">>", it is an arithmetic shift operator. Thanks! -Andy
94
by: krypto.wizard | last post by:
Last month I appeared for an interview with EA sports and they asked me this question. How would you divide a number by 7 without using division operator ? I did by doing a subtraction and...
5
by: Rahul | last post by:
Hi Everyone, I have a program unit which does >and << of an integer which is of 4 bytes length. The logic of shifting and action based on the result, assumes that the system is big-endian. ...
39
by: Juha Nieminen | last post by:
I was once taught that if some integral value can never have negative values, it's a good style to use an 'unsigned' type for that: It's informative, self-documenting, and you are not wasting half...
10
by: deepak | last post by:
Hi, when I execute following program which find's one's compliment of 2, I'm getting -3 where I was expecting -2. Is there anything fundamentally wrong in my understanding? main() {
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.