Date: Tue, 12 Apr 2022 23:19:27 +0100

On Tue, 12 Apr 2022 at 18:16, pieter_at_[hidden] via Std-Proposals <

std-proposals_at_[hidden]> wrote:

> In Expressions, Integral conversions, the same formula is given for

> conversion to unsigned and to signed:

>

> > Otherwise, the result is the unique value of the destination type

> > that is congruent to the source integer modulo $2^N$,

> > where $N$ is the width of the destination type.

>

> I believe that this cannot be right: it would imply that signed integers

> of the same width have the same range of positive numbers as unsigned

> integers, so for signed integrals, a power N-1 should be used, to account

> for the reduced positive range due to the "sign bit" (if you can use that

> term for two-s complement).

>

Suppose you decide to convert 255 to int8_t. There is a unique value of

type int8_t congruent to 255 modulo 2^8; that value is -1.

If you then decide to convert -1 to uint8_t; well, again, there is a unique

value of type uint8_t congruent to -1 modulo 2^8; that value is 255.

(Congruence is an equivalence relation.)

This works because we don't have signed zero (thanks to P0907/P1236 as you

note), and so there are 2^N distinct and adjacent values of each signed

integer type width N, strictly representing Z_2^N, just as there are 2^N

distinct and adjacent values of each unsigned integer type width N.

Hope this helps.

std-proposals_at_[hidden]> wrote:

> In Expressions, Integral conversions, the same formula is given for

> conversion to unsigned and to signed:

>

> > Otherwise, the result is the unique value of the destination type

> > that is congruent to the source integer modulo $2^N$,

> > where $N$ is the width of the destination type.

>

> I believe that this cannot be right: it would imply that signed integers

> of the same width have the same range of positive numbers as unsigned

> integers, so for signed integrals, a power N-1 should be used, to account

> for the reduced positive range due to the "sign bit" (if you can use that

> term for two-s complement).

>

Suppose you decide to convert 255 to int8_t. There is a unique value of

type int8_t congruent to 255 modulo 2^8; that value is -1.

If you then decide to convert -1 to uint8_t; well, again, there is a unique

value of type uint8_t congruent to -1 modulo 2^8; that value is 255.

(Congruence is an equivalence relation.)

This works because we don't have signed zero (thanks to P0907/P1236 as you

note), and so there are 2^N distinct and adjacent values of each signed

integer type width N, strictly representing Z_2^N, just as there are 2^N

distinct and adjacent values of each unsigned integer type width N.

Hope this helps.

Received on 2022-04-12 22:19:39