Connecting Tech Pros Worldwide Forums | Help | Site Map

Semantics of unary minus

Marc
Guest
 
Posts: n/a
#1: Feb 12 '06
Hi,

I've been lurking on clc for a few months now, and want to start by
thanking the regulars here for opening my eyes to a whole new dimension
of "knowing c". Considering I had never even touched the standards a
year ago, though I graduated in embedded SW development...

Anyway, to the problem at hand: I've stumbled upon the following
construct at work recently, and cannot really make up my mind about the
standard's take on the matter.

int a;
unsigned int b;

if(a < 0) {
b = -a;
}

I just can't decide if this is valid when a's value is -32768 (admitting
16 bit ints). I can't see how the standard's wording on unary minus
semantics allows for 2's complement asymmetry. C99 says:

"The result of the unary - operator is the negative of its (promoted)
operand. The integer promotions are performed on the operand, and the
result has the promoted type." (I'm also slightly suspicious about the
disappearance of "value" as compared to the semantics of unary plus...)

Unless I'm mistaken, a won't be promoted (being an int), so there might
be no such thing as "the negative of its (promoted) operand"? I feel
like the standard tells me I might end up with an int worth 32768...
(still assuming 16 bit ints)? Or should I consider there is an implied
"if-it-fits-otherwise-overflow"?

I couldn't find any help through google or in the FAQ, so I'd really
appreciate any clarification.

Marc

P.J. Plauger
Guest
 
Posts: n/a
#2: Feb 12 '06

re: Semantics of unary minus


"Marc" <nospam@mail.com> wrote in message
news:dsnip5$47a$02$1@news.t-online.com...
[color=blue]
> int a;
> unsigned int b;
>
> if(a < 0) {
> b = -a;
> }
>
> I just can't decide if this is valid when a's value is -32768 (admitting
> 16 bit ints). I can't see how the standard's wording on unary minus
> semantics allows for 2's complement asymmetry. C99 says:[/color]

-a overflows and typically yields -32768 without a trap
(though an implementation doesn't have to). The conversion
is 2^16 - 32768, which is 32768. So you luck out.

Converting to unsigned long doesn't work quite as well.
It is much safer to write "b = 0U - a" instead.

P.J. Plauger
Dinkumware, Ltd.
http://www.dinkumware.com


Eric Sosman
Guest
 
Posts: n/a
#3: Feb 12 '06

re: Semantics of unary minus


Marc wrote:[color=blue]
> Hi,
>
> I've been lurking on clc for a few months now, and want to start by
> thanking the regulars here for opening my eyes to a whole new dimension
> of "knowing c". Considering I had never even touched the standards a
> year ago, though I graduated in embedded SW development...
>
> Anyway, to the problem at hand: I've stumbled upon the following
> construct at work recently, and cannot really make up my mind about the
> standard's take on the matter.
>
> int a;
> unsigned int b;
>
> if(a < 0) {
> b = -a;
> }
>
> I just can't decide if this is valid when a's value is -32768 (admitting
> 16 bit ints). I can't see how the standard's wording on unary minus
> semantics allows for 2's complement asymmetry. C99 says:
>
> "The result of the unary - operator is the negative of its (promoted)
> operand. The integer promotions are performed on the operand, and the
> result has the promoted type." (I'm also slightly suspicious about the
> disappearance of "value" as compared to the semantics of unary plus...)
>
> Unless I'm mistaken, a won't be promoted (being an int), so there might
> be no such thing as "the negative of its (promoted) operand"? I feel
> like the standard tells me I might end up with an int worth 32768...
> (still assuming 16 bit ints)? Or should I consider there is an implied
> "if-it-fits-otherwise-overflow"?
>
> I couldn't find any help through google or in the FAQ, so I'd really
> appreciate any clarification.[/color]

Yes, two's complement is asymmetric about zero: there
is a negative number whose absolute value is not representable.
(The Standard actually permits an implementation to dodge this
asymmetry by defining "all ones" to be a trap representation;
I've never heard of an implementation that does so.)

However, that doesn't mean unary minus is undefined: it's
only undefined if its operand has an inappropriate value. This
is really no different from the situation with binary minus:

"The result of the binary - operator is the difference
resulting from the subtraction of the second operand
from the first." (6.5.6/6)

This is not to be taken as implying that all subtractions must
produce mathematically correct results: `INT_MIN - 42', for
example, is clearly not going to produce a value less than
INT_MIN.

When this sort of thing happens, another provision of the
Standard takes over:

"If an _exceptional condition_ occurs during the
evaluation of an expression (that is, if the result
is not mathematically defined or not in the range of
representable values for its type), the behavior is
undefined." (6.5/5)

So: If you try to negate (or take the absolute value of)
INT_MIN on a system where `INT_MIN + INT_MAX' is not zero, the
C language does not specify the outcome -- anything can happen,
at least in principle. The commonest behavior is that the
overflow is ignored, and the resulting representation (all ones)
is equal to INT_MIN again: INT_MIN is its own negation on most
two's-complement systems. However, this should be thought of
as a quirk of those systems, not as part of the C language.

--
Eric Sosman
esosman@acm-dot-org.invalid
pete
Guest
 
Posts: n/a
#4: Feb 12 '06

re: Semantics of unary minus


Marc wrote:
[color=blue]
> int a;[/color]
[color=blue]
> -a;[/color]
[color=blue]
> "if-it-fits-otherwise-overflow"?[/color]

Yes.
"overflow" as in "undefined behavior".

It's a point of trivia which also comes up when writing itoa,
that there is no integer type which is guaranteed
to be able to represent the magnitude of INT_MIN.

--
pete
Rod Pemberton
Guest
 
Posts: n/a
#5: Feb 12 '06

re: Semantics of unary minus



"Marc" <nospam@mail.com> wrote in message
news:dsnip5$47a$02$1@news.t-online.com...[color=blue]
> Hi,
>
> I've been lurking on clc for a few months now, and want to start by
> thanking the regulars here for opening my eyes to a whole new dimension
> of "knowing c". Considering I had never even touched the standards a
> year ago, though I graduated in embedded SW development...
>
> Anyway, to the problem at hand: I've stumbled upon the following
> construct at work recently, and cannot really make up my mind about the
> standard's take on the matter.
>
> int a;
> unsigned int b;
>
> if(a < 0) {
> b = -a;
> }
>
> I just can't decide if this is valid when a's value is -32768 (admitting
> 16 bit ints). I can't see how the standard's wording on unary minus
> semantics allows for 2's complement asymmetry. C99 says:
>
> "The result of the unary - operator is the negative of its (promoted)
> operand. The integer promotions are performed on the operand, and the
> result has the promoted type." (I'm also slightly suspicious about the
> disappearance of "value" as compared to the semantics of unary plus...)
>
> Unless I'm mistaken, a won't be promoted (being an int), so there might
> be no such thing as "the negative of its (promoted) operand"? I feel
> like the standard tells me I might end up with an int worth 32768...
> (still assuming 16 bit ints)? Or should I consider there is an implied
> "if-it-fits-otherwise-overflow"?
>
> I couldn't find any help through google or in the FAQ, so I'd really
> appreciate any clarification.[/color]

I'm not going to help you comprehend the spec. There are a few other people
here better at that than me. But, I can tell how the 16-bit and 32-bit
compilers that I use work in this area. The 16-bit compilers will not
convert -32768. It remains negative. But, they will convert -32767 (to -1)
to +32767 (to +1). The 32-bit compilers convert -32768 to +32768.
Basically, if it won't fit into the converted type, it doesn't get
converted.


Rod Pemberton



pete
Guest
 
Posts: n/a
#6: Feb 12 '06

re: Semantics of unary minus


pete wrote:[color=blue]
>
> Marc wrote:
>[color=green]
> > int a;[/color]
>[color=green]
> > -a;[/color]
>[color=green]
> > "if-it-fits-otherwise-overflow"?[/color]
>
> Yes.
> "overflow" as in "undefined behavior".
>
> It's a point of trivia which also comes up when writing itoa,
> that there is no integer type which is guaranteed
> to be able to represent the magnitude of INT_MIN.[/color]

You can see what INT_MIN expands to on your implementation.
It might be something like (-32767 - 1),
if you have two's complement representation of negative integers.

/* BEGIN new.c */

#include <stdio.h>
#include <limits.h>

#define str(s) # s
#define xstr(s) str(s)

int main(void)
{
puts("INT_MIN is " xstr(INT_MIN));
return 0;
}

/* END new.c */

--
pete
Marc
Guest
 
Posts: n/a
#7: Feb 12 '06

re: Semantics of unary minus


Eric Sosman wrote:
[...][color=blue]
> When this sort of thing happens, another provision of the
> Standard takes over:
>
> "If an _exceptional condition_ occurs during the
> evaluation of an expression (that is, if the result
> is not mathematically defined or not in the range of
> representable values for its type), the behavior is
> undefined." (6.5/5)
>
> So: If you try to negate (or take the absolute value of)
> INT_MIN on a system where `INT_MIN + INT_MAX' is not zero, the
> C language does not specify the outcome -- anything can happen,
> at least in principle. The commonest behavior is that the
> overflow is ignored, and the resulting representation (all ones)
> is equal to INT_MIN again: INT_MIN is its own negation on most
> two's-complement systems. However, this should be thought of
> as a quirk of those systems, not as part of the C language.[/color]

Ah yes ... 6.5/5 states exactly what I was looking for. Thanks a lot for
the pointer - guess I still have a cover-to-cover reading to do ...

As you and others suggested, the negation did work on our system, now I
know for sure it was just "lucking-out" from a standard's point of view.

Thanks all for the answers,

Marc
Antonio Contreras
Guest
 
Posts: n/a
#8: Feb 13 '06

re: Semantics of unary minus


Eric Sosman wrote:

<snip: question about unary minus overflowing for INT_MIN>
[color=blue]
> Yes, two's complement is asymmetric about zero: there
> is a negative number whose absolute value is not representable.
> (The Standard actually permits an implementation to dodge this
> asymmetry by defining "all ones" to be a trap representation;
> I've never heard of an implementation that does so.)[/color]

ITYM a 1 followed by all 0s. All ones is -1 in two's complement.

pete
Guest
 
Posts: n/a
#9: Feb 13 '06

re: Semantics of unary minus


Antonio Contreras wrote:[color=blue]
>
> Eric Sosman wrote:
>
> <snip: question about unary minus overflowing for INT_MIN>
>[color=green]
> > Yes, two's complement is asymmetric about zero: there
> > is a negative number whose absolute value is not representable.
> > (The Standard actually permits an implementation to dodge this
> > asymmetry by defining "all ones" to be a trap representation;
> > I've never heard of an implementation that does so.)[/color]
>
> ITYM a 1 followed by all 0s. All ones is -1 in two's complement.[/color]

He's talking about negative zero.
A one followed by all zeros, is negative zero
in sign and magnitude representation.
All ones is negative zero in one's complement representation.
There's different ways for an implementation
to deal with negative zero, trapping is one of them.

--
pete
Antonio Contreras
Guest
 
Posts: n/a
#10: Feb 13 '06

re: Semantics of unary minus


pete wrote:[color=blue]
> Antonio Contreras wrote:[color=green]
> >
> > Eric Sosman wrote:
> >
> > <snip: question about unary minus overflowing for INT_MIN>
> >[color=darkred]
> > > Yes, two's complement is asymmetric about zero: there
> > > is a negative number whose absolute value is not representable.
> > > (The Standard actually permits an implementation to dodge this
> > > asymmetry by defining "all ones" to be a trap representation;
> > > I've never heard of an implementation that does so.)[/color]
> >
> > ITYM a 1 followed by all 0s. All ones is -1 in two's complement.[/color]
>
> He's talking about negative zero.
> A one followed by all zeros, is negative zero
> in sign and magnitude representation.
> All ones is negative zero in one's complement representation.
> There's different ways for an implementation
> to deal with negative zero, trapping is one of them.[/color]

But he was talking about two's complement. And AFAIK the standard
allows a two's complement representation with trap representations. In
this case the obvious pattern for a trap representation is a 1 followed
by all 0s, which implies that INT_MIN = - INT_MAX

pete
Guest
 
Posts: n/a
#11: Feb 13 '06

re: Semantics of unary minus


Antonio Contreras wrote:[color=blue]
>
> pete wrote:[color=green]
> > Antonio Contreras wrote:[color=darkred]
> > >
> > > Eric Sosman wrote:
> > >
> > > <snip: question about unary minus overflowing for INT_MIN>
> > >
> > > > Yes, two's complement is asymmetric about zero: there
> > > > is a negative number whose absolute value is not representable.
> > > > (The Standard actually permits an implementation to dodge this
> > > > asymmetry by defining "all ones" to be a trap representation;
> > > > I've never heard of an implementation that does so.)
> > >
> > > ITYM a 1 followed by all 0s. All ones is -1 in two's complement.[/color]
> >
> > He's talking about negative zero.
> > A one followed by all zeros, is negative zero
> > in sign and magnitude representation.
> > All ones is negative zero in one's complement representation.
> > There's different ways for an implementation
> > to deal with negative zero, trapping is one of them.[/color]
>
> But he was talking about two's complement. And AFAIK the standard
> allows a two's complement representation with trap representations. In
> this case the obvious pattern
> for a trap representation is a 1 followed
> by all 0s, which implies that INT_MIN = - INT_MAX[/color]

Yes, I had missed the point.

--
pete
Jordan Abel
Guest
 
Posts: n/a
#12: Feb 13 '06

re: Semantics of unary minus


On 2006-02-12, pete <pfiland@mindspring.com> wrote:[color=blue]
> Marc wrote:
>[color=green]
>> int a;[/color]
>[color=green]
>> -a;[/color]
>[color=green]
>> "if-it-fits-otherwise-overflow"?[/color]
>
> Yes.
> "overflow" as in "undefined behavior".
>
> It's a point of trivia which also comes up when writing itoa,
> that there is no integer type which is guaranteed
> to be able to represent the magnitude of INT_MIN.[/color]

Not unsigned int?
Eric Sosman
Guest
 
Posts: n/a
#13: Feb 13 '06

re: Semantics of unary minus


Antonio Contreras wrote:[color=blue]
> Eric Sosman wrote:
>
> <snip: question about unary minus overflowing for INT_MIN>
>[color=green]
>> Yes, two's complement is asymmetric about zero: there
>>is a negative number whose absolute value is not representable.
>>(The Standard actually permits an implementation to dodge this
>>asymmetry by defining "all ones" to be a trap representation;
>>I've never heard of an implementation that does so.)[/color]
>
>
> ITYM a 1 followed by all 0s. All ones is -1 in two's complement.[/color]

Right you are. Sorry for the blunder.

--
Eric Sosman
esosman@acm-dot-org.invalid
pete
Guest
 
Posts: n/a
#14: Feb 13 '06

re: Semantics of unary minus


Jordan Abel wrote:[color=blue]
>
> On 2006-02-12, pete <pfiland@mindspring.com> wrote:[color=green]
> > Marc wrote:
> >[color=darkred]
> >> int a;[/color]
> >[color=darkred]
> >> -a;[/color]
> >[color=darkred]
> >> "if-it-fits-otherwise-overflow"?[/color]
> >
> > Yes.
> > "overflow" as in "undefined behavior".
> >
> > It's a point of trivia which also comes up when writing itoa,
> > that there is no integer type which is guaranteed
> > to be able to represent the magnitude of INT_MIN.[/color]
>
> Not unsigned int?[/color]

No.
The requirement is for the unsigned type to have
at least as many value bits as the signed type.
You can have:
CHAR_BIT == 16
sizeof(int) == 2
UINT_MAX == 65535
INT_MIN == -65536


--
pete
Closed Thread