Keith Thompson

2016-10-15 21:43:43 UTC

Permalink

N1570 6.2.5p9 says:Raw Message

The range of nonnegative values of a signed integer type is a

subrange of the corresponding unsigned integer type, and the

representation of the same value in each type is the same.

with a footnote (#41 in N1570):

The same representation and alignment requirements are meant

to imply interchangeability as arguments to functions, return

values from functions, and members of unions.

This is straightforward for most systems; the values from 0 to

INT_MAX can be represented either as signed int or as unsigned int,

and have the same representation in both. (The same applies to

other corresponding pairs of signed and unsigned types, but I'll

refer to signed/unsigned int for simplicity.)

But it overlooks the possibility of a value having more than

one representation. It refers to "*the* representation", but

6.2.6.1p4 says:

Two values (other than NaNs) with the same object representation

compare equal, but values that compare equal may have different

object representations.

I think the intent is that if a value within the range 0..INT_MAX has

more than one representation, then both are representions of that

value for both signed int and unsigned int. Otherwise, the intent

expressed in footnote 41 would be difficult or impossible to support.

So far so good -- but what about negative zero in ones' complement

and sign and magnitude systems?

6.2.6.2p2:

Which of these [the three schemes for representing negative

integers] applies is implementation-defined, as is whether

the value with sign bit 1 and all value bits zero (for the

first two), or with sign bit and all value bits 1 (for ones’

complement), is a trap representation or a normal value. In

the case of sign and magnitude and ones’ complement, if this

representation is a normal value it is called a *negative zero*.

So here's the question. For a non-two's-complement system with

negative zero as a normal value, is it required that storing the

representation of negative zero in an unsigned object results in

that object having the value zero?

6.2.5p9 seems to imply that the answer is yes, but practicality

suggests that it's no. Assuming unsigned int has no padding bits and

therefore no trap representations, then representations corresponding

to zero and to negative zero must have different value.

I think the most reasonable resolution is to say that 6.2.5p9

doesn't apply to negative zero.

I'm assuming that zero and negative zero are the same value, and

more generally that two values that compare equal are the same value.

--

Keith Thompson (The_Other_Keith) kst-***@mib.org <http://www.ghoti.net/~kst>

Working, but not speaking, for JetHead Development, Inc.

"We must do something. This is something. Therefore, we must do this."

-- Antony Jay and Jonathan Lynn, "Yes Minister"

Keith Thompson (The_Other_Keith) kst-***@mib.org <http://www.ghoti.net/~kst>

Working, but not speaking, for JetHead Development, Inc.

"We must do something. This is something. Therefore, we must do this."

-- Antony Jay and Jonathan Lynn, "Yes Minister"