The title more or less says it all: in C99, is the value of
INT_MIN % -1
well defined (when performed as signed integers) under the assumption of
two-complement representation.
Note, that this is not the usual negative-values-and-% question -- the problem
here is that the corresponding signed division, INT_MIN / -1, overflows. Thus
I don't see what use a%b = a-(a/b)*b can be here. 6 2709
On 5 Aug 2004 06:57:20 -0700, te***@gnome.org (M Welinder) wrote in
comp.lang.c: The title more or less says it all: in C99, is the value of
INT_MIN % -1
well defined (when performed as signed integers) under the assumption of two-complement representation.
No version of the C standard assumes two's complement representation,
nor makes any additional exceptions or requirements for platforms that
use this representation as opposed to the other two allowed choices.
Note, that this is not the usual negative-values-and-% question -- the problem here is that the corresponding signed division, INT_MIN / -1, overflows. Thus I don't see what use a%b = a-(a/b)*b can be here.
The division does not necessarily overflow. You are assuming that
INT_MIN is -(pow(2,n)) for some typical value of 'n' such as 15 or 31.
While this is common on 2's complement platforms, the C standard does
not require that this value be supported. INT_MIN is allowed to be
-(INT_MAX), even on 2's complement systems. The binary value
containing only the sign bit set may be an invalid bit-pattern, what
the standard calls a trap representation.
So if INT_MIN is equal to -(INT_MAX), both the division and mod
operation are well-defined.
On the other hand, if INT_MIN is equal to -(INT_MAX)-1, I would say
that it produces overflow, which makes it undefined behavior.
The definition of the '%' is specifically defined as the remainder of
the division of the two operands. If the division overflows, so does
the modulus.
See 6.5.5 para. 5 and 3.4.3 para 3.
--
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.l earn.c-c++ http://www.contrib.andrew.cmu.edu/~a...FAQ-acllc.html
In article <e8************ *************** *****@4ax.com>,
Jack Klein <ja*******@spam cop.net> wrote: On the other hand, if INT_MIN is equal to -(INT_MAX)-1, I would say that it produces overflow, which makes it undefined behavior. The definition of the '%' is specifically defined as the remainder of the division of the two operands. If the division overflows, so does the modulus.
Behavior of arithmetic operations is undefined if the result cannot be
represented. The remainder of (-INT_MAX - 1) / (-1) can be represented
without any problems, even though the result of the division can't be
represented.
However, the C Standard says "If a/b can be represented, then (a/b)*b +
(a%b) == a". This is supposed to define the result that a%b should
produce, and it doesn't define the result in the case above. On the
other hand, I would say that the "remainder" is a well-defined
mathematical term except in cases like (-10) % (-3), where it is not
obviously clear whether the result should be -1 or +2. If a is a
multiple of b, then the remainder is always 0, and that is the case
here.
So I would say the result of x % (-1) is well-defined and zero in all
cases.
Christian Bau <ch***********@ cbau.freeserve. co.uk> wrote in message news:<ch******* *************** ***********@slb-newsm1.svr.pol. co.uk>... So I would say the result of x % (-1) is well-defined and zero in all cases.
Ok, but if that is the case, then all i86 compilers need to generate code
like morally equivalent to
a C99% b = (b == -1) ? 0 : (a machine% b)
because the machine% (aka the division instruction) will not work for INT_MIN
and -1. (And forget about multiple evaluation of b or missing evaluation of
a -- that's not the point here.)
It was my impression that C99 intended to shore up the semantics while still
allowing for an efficient implementation on i86. I had sort-of hoped for the
answer "clearly undefined under the assumptions".
M.
Christian Bau <ch***********@ cbau.freeserve. co.uk> wrote in message news:<ch******* *************** ***********@slb-newsm1.svr.pol. co.uk>... Behavior of arithmetic operations is undefined if the result cannot be represented. The remainder of (-INT_MAX - 1) / (-1) can be represented without any problems, even though the result of the division can't be represented.
Depends on what "representa ble" means.
However, the C Standard says "If a/b can be represented, then (a/b)*b + (a%b) == a".
This is true, though the actual statement is:
6 When integers are divided, the result of the / operator is
the algebraic quotient with any fractional part discarded.
If the quotient a/b is representable, the expression
(a/b)*b + a%b shall equal a.
The question is what does the word "representa ble" mean. The
result of the expression (-INT_MAX - 1) / -1 can certainly be
represented as an unsigned int, though you may have a problem
if you tried to assign the value to a signed int.
This is supposed to define the result that a%b should produce, and it doesn't define the result in the case above.
Ok but see below. (1)
On the other hand, I would say that the "remainder" is a well-defined mathematical term except in cases like (-10) % (-3), where it is not obviously clear whether the result should be -1 or +2.
Mathematically it should be -1 and never +2.
So I would say the result of x % (-1) is well-defined and zero in all cases.
This contradicts your statement above. (1)
In article <a5************ **************@ posting.google. com>, k_*****@yahoo.c om (kal) wrote: Christian Bau <ch***********@ cbau.freeserve. co.uk> wrote in message news:<ch******* *************** ***********@slb-newsm1.svr.pol. co.uk>...
Behavior of arithmetic operations is undefined if the result cannot be represented. The remainder of (-INT_MAX - 1) / (-1) can be represented without any problems, even though the result of the division can't be represented.
Depends on what "representa ble" means.
However, the C Standard says "If a/b can be represented, then (a/b)*b + (a%b) == a".
This is true, though the actual statement is:
6 When integers are divided, the result of the / operator is the algebraic quotient with any fractional part discarded. If the quotient a/b is representable, the expression (a/b)*b + a%b shall equal a.
The question is what does the word "representa ble" mean. The result of the expression (-INT_MAX - 1) / -1 can certainly be represented as an unsigned int, though you may have a problem if you tried to assign the value to a signed int.
This is supposed to define the result that a%b should produce, and it doesn't define the result in the case above.
Ok but see below. (1)
On the other hand, I would say that the "remainder" is a well-defined mathematical term except in cases like (-10) % (-3), where it is not obviously clear whether the result should be -1 or +2.
Mathematically it should be -1 and never +2.
So I would say the result of x % (-1) is well-defined and zero in all cases.
This contradicts your statement above. (1)
What makes you think that? The remainder of division of an integer by
minus one is always zero, no matter what exact definition of remainder
you use (and there are several possibilities). If x is a multiple of y,
then the remainder of x/y is zero. And every integer is a multiple of
-1.
So the C Standard gives one definition which is not quite complete,
because "remainder" as a mathematical term does not have a well defined
meaning in all cases, but it has a well defined meaning in the case we
discuss. The C Standard gives another definition which explicitely
excludes the case we are discussing but clarifies the first definition
in many cases. Since Definition 1 applies and defines the result as 0,
and Definition 2 does not apply, the result must be 0.
In article <a5************ **************@ posting.google. com> k_*****@yahoo.c om (kal) writes: Christian Bau <ch***********@ cbau.freeserve. co.uk> wrote in message news:<ch******* *************** ***********@slb-newsm1.svr.pol. co.uk>...
.... On the other hand, I would say that the "remainder" is a well-defined mathematical term except in cases like (-10) % (-3), where it is not obviously clear whether the result should be -1 or +2.
Mathematically it should be -1 and never +2.
Mathematics does not know the operator '%'. Nor is there a well-defined
mathematical term "remainder" . Mathematics talks about residue classes,
and in that case the results "-1" and "+2" are the same. That is:
-10 == -1 == 2 (mod -3).
--
dik t. winter, cwi, kruislaan 413, 1098 sj amsterdam, nederland, +31205924131
home: bovenover 215, 1025 jn amsterdam, nederland; http://www.cwi.nl/~dik/ This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
by: Sean Kenwrick |
last post by:
I am writing a byte-code interpreter/emulator for a language that
exclusively uses strings for variables (i.e all variables are pushed onto
the stack as strings). Therefore for arithmetic operations I need to
convert the string to a 32 bit integer, carry out the arithmetic operation,
then convert it back to a string before pushing it back...
|
by: RoSsIaCrIiLoIA |
last post by:
write the function
int readINT(FILE* fp);
that read an int from fp and return it.
given the following conditions
a. Do not use arrays
b. Do not use any comparison function like if/then
or switch-case
c. you can use pointers only
d. you cannot use any of the loops either.
e. you cannot use in it any library function
|
by: Jordan Abel |
last post by:
Say int is a 16-bit twos-complement type.
Is the expression (-32768) actually negative? Or is it the unsigned
int literal 32768 with the unary minus operator applied to it, resulting
in the value of the expression being an unsigned int 32768, which can
then trap when being assigned to a signed int type
|
by: pemo |
last post by:
As far as I understand it, a trap representation means something like -
an uninitialised automatic variable might /implicitly/ hold a bit-pattern
that, if read, *might* cause a 'trap' (I'm not sure what 'a trap' means
here - anyone?).
I also read into this that, an *initialised* automatic variable, may never
hold a bit pattern that...
|
by: jzhang918 |
last post by:
Hi,
Should f(INT_MIN) return 0 according to the C99 for the following
function f (), or its result is undefined?
int f(int i)
{
i = i 0 ? i : -i;
if (i<0)
return 0;
| |
by: p_cricket_guy |
last post by:
Please see this test program:
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <limits.h>
4
5 int main (void)
6 {
7 int y = -2147483648;
8 int x = INT_MIN;
|
by: Nimmi Srivastav |
last post by:
Apologies if my cross posting has offended anyone....
For a pure hobbyist C/C++ programmer, who wants to develop
applications to run on Windows, what would be a better choice to
install: Visual C++ Express 2005 Edition or lcc-win32? Does anyone
have any opinion to share?
Also, is there a C++ compiler akin to lcc-win32?
Thanks,
|
by: Ron Ford |
last post by:
I'm looking for a freeware c99 compiler for windows. I had intended to use
MS's Visual C++ Express and use its C capability. In the past with my MS
products, I've simply needed to make .c the filetype to invoke the C
compiler. Here's a link
http://www.microsoft.com/express/download/#webInstall
The download is 2.6 megs, which is near a...
|
by: viza |
last post by:
Hi all
int i= INT_MIN;
unsigned int u= -i;
Is u guaranteed to have the absolute value of INT_MIN?
Why it might not: -i has type (int), and -INT_MIN might be more than
INT_MAX.
|
by: polas |
last post by:
Afternoon all,
I have some code (which I thought was OK, but now appreciate is
probably not by the standard) which contains
int a;
......
if (a==NULL)
{
....
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it.
First, let's disable language...
| |
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed.
This is as boiled down as I can make it. ...
|
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
|
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
|
by: conductexam |
last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one.
At the time of converting from word file to html my equations which are in the word document file was convert...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
| |
by: 6302768590 |
last post by:
Hai team
i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
|
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |