public inbox for gcc@gcc.gnu.org
 help / color / mirror / Atom feed
* gcc interprets -0 incorrectly
@ 2002-12-06 15:12 Justin Pryzby
  2002-12-06 15:33 ` Zack Weinberg
                   ` (2 more replies)
  0 siblings, 3 replies; 5+ messages in thread
From: Justin Pryzby @ 2002-12-06 15:12 UTC (permalink / raw)
  To: gcc

/*
 * GCC interprets -0 as -INT_MIN==-INT_MAX - 1 (See <limits.h>).
 * It knows how to overflow from positive to negative,
 * and from negative to positive.
 *
 * However, I object to this interpretation,
 * even if it does allow an int to hold an extra number;
 * From algebra: it is clear that for all x=-x, 2x=0, x=0.
 * Also, for all x=0, 2x=0, x=-x.
 * That is, x=-x implies that x=0, AND x=0 implies that x=-x.
 *
 * Please CC: me in all replies, as I am not subscribed to the list.
 *
 * Justin
 */


#include <stdio.h>
#include <assert.h>

int main()
{
	int i=0;
	struct byte_struct {
		int a:1;
		int b:6;
		int c:1;
	} *byte;

	byte=(struct byte_struct *) (((char *) &i)+3);
	byte->c=1;

	// -2147483648
	printf("%d\n", i);

	// warning: this decimal constant is unsigned only in ISO C90.
	i=-2147483648;
	i--;
	printf("%d\n", i);

	i=2147483647;
	i++;
	printf("%d\n", i);

	return 0;
}

^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: gcc interprets -0 incorrectly
  2002-12-06 15:12 gcc interprets -0 incorrectly Justin Pryzby
@ 2002-12-06 15:33 ` Zack Weinberg
  2002-12-06 17:02 ` Neil Booth
  2002-12-20  0:30 ` Segher Boessenkool
  2 siblings, 0 replies; 5+ messages in thread
From: Zack Weinberg @ 2002-12-06 15:33 UTC (permalink / raw)
  To: Justin Pryzby; +Cc: gcc

On Fri, Dec 06, 2002 at 05:43:13PM -0500, Justin Pryzby wrote:
> /*
>  * GCC interprets -0 as -INT_MIN==-INT_MAX - 1 (See <limits.h>).
>  * It knows how to overflow from positive to negative,
>  * and from negative to positive.

No, both -0 and 0 are represented as 0x00000000 in 32-bit
twos-complement integer arithmetic.  Your code does not calculate -0.

> 	int i=0;
> 	struct byte_struct {
> 		int a:1;
> 		int b:6;
> 		int c:1;
> 	} *byte;
> 
> 	byte=(struct byte_struct *) (((char *) &i)+3);
> 	byte->c=1;

This has undefined behavior.  However, assuming it does what a literal
translation to assembly language would do, its effect is to set the
high bit of a four-byte little-endian integer to 1.  In other words,
it's equivalent to

 int i = 0x80000000U;

That has the numeric value -2147483648, which is indeed INT_MIN in
32-bit twos-complement arithmetic.  You seem to think that integer
arithmetic is sign-and-magnitude, which is not true for any
architecture supported by GCC.

Please read up on twos-complement arithmetic.

zw

^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: gcc interprets -0 incorrectly
  2002-12-06 15:12 gcc interprets -0 incorrectly Justin Pryzby
  2002-12-06 15:33 ` Zack Weinberg
@ 2002-12-06 17:02 ` Neil Booth
  2002-12-20  0:30 ` Segher Boessenkool
  2 siblings, 0 replies; 5+ messages in thread
From: Neil Booth @ 2002-12-06 17:02 UTC (permalink / raw)
  To: Justin Pryzby; +Cc: gcc

Justin Pryzby wrote:-

> 	// warning: this decimal constant is unsigned only in ISO C90.
> 	i=-2147483648;

Is this warning what's confusing you?

Neil.

^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: gcc interprets -0 incorrectly
  2002-12-06 15:12 gcc interprets -0 incorrectly Justin Pryzby
  2002-12-06 15:33 ` Zack Weinberg
  2002-12-06 17:02 ` Neil Booth
@ 2002-12-20  0:30 ` Segher Boessenkool
  2002-12-22 20:04   ` Gaute B Strokkenes
  2 siblings, 1 reply; 5+ messages in thread
From: Segher Boessenkool @ 2002-12-20  0:30 UTC (permalink / raw)
  To: Justin Pryzby; +Cc: gcc

Justin Pryzby wrote:
> 
>  * From algebra: it is clear that for all x=-x, 2x=0, x=0.

x=0 is not implied by 2x=0 in this ring (the ring of integers
modulo some power of two); this ring is not an integral domain,
and 2 is a zero divisor.

-MIN_INT == MIN_INT  is perfectly fine, mathematically.


Segher


^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: gcc interprets -0 incorrectly
  2002-12-20  0:30 ` Segher Boessenkool
@ 2002-12-22 20:04   ` Gaute B Strokkenes
  0 siblings, 0 replies; 5+ messages in thread
From: Gaute B Strokkenes @ 2002-12-22 20:04 UTC (permalink / raw)
  To: Segher Boessenkool; +Cc: Justin Pryzby, gcc

On Thu, 19 Dec 2002, segher@koffie.nl wrote:
> Justin Pryzby wrote:
>> 
>>  * From algebra: it is clear that for all x=-x, 2x=0, x=0.
> 
> x=0 is not implied by 2x=0 in this ring (the ring of integers
> modulo some power of two); this ring is not an integral domain,
> and 2 is a zero divisor.
> 
> -MIN_INT == MIN_INT  is perfectly fine, mathematically.

OTOH a lot of interesting theory only applies when 2 != 0;
specifically because the ability to divide by 2 turns out to be
particularly useful.

One of my lecturers when I was a maths undergraduate once said that
some people liked to do stuff in fields with characteristic 2 as a
kind of special spice; but that people who wish to remain sane should
stay away from them...

-- 
Gaute Strokkenes                        http://www.srcf.ucam.org/~gs234/
A KAISER ROLL?!  What good is a Kaiser Roll without a little
 COLE SLAW on the SIDE?

^ permalink raw reply	[flat|nested] 5+ messages in thread

end of thread, other threads:[~2002-12-22 14:02 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2002-12-06 15:12 gcc interprets -0 incorrectly Justin Pryzby
2002-12-06 15:33 ` Zack Weinberg
2002-12-06 17:02 ` Neil Booth
2002-12-20  0:30 ` Segher Boessenkool
2002-12-22 20:04   ` Gaute B Strokkenes

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).