Topic: Defect Report: Negative of unsigned computation not suffice?
Author: gennaro_pruota@yahoo.com (Gennaro Prota)
Date: Thu, 21 Dec 2006 15:44:43 GMT Raw View
On Thu, 21 Dec 2006 05:48:01 GMT, "Alf P. Steinbach" wrote:
>> In Section 5.3.1 Unary operators [expr.unary.op], part of the paragrap=
h
>> 7 describes how to compute the negative of an unsigned quantity:
>>=20
>> "The negative of an unsigned quantity is computed by subtracting its
>> value from 2^n, where n is the number of bits in the promoted operand.
>> The type of the result is the type of the promoted operand."
>>=20
>> (2^n means the n-th power of 2.)
>>=20
>> According to the above computing method, -0U, the negative of an
>> unsigned int 0, for example, will get value 2^N - 0 =3D 2^N, where N i=
s
>> the number of bits in an unsigned int. However, 2^N is obviously out o=
f
>> the range of values representable by an unsigned int and thus not the
>> actual value of -0U. To get the result, a truncating conversion has to
>> be conducted subsequently.
>>=20
>> It seems the method is good for calculating the negative of any
>> unsigned exept unsigned 0. Perhaps a revision is needed.
>
>There's no need for a revision.
I disagree: the sentence the OP quotes is wrong and contradicts the
one in 3.9.1.
>=A73.9.1/4 "Unsigned integers, declared 'unsigned', shall obey the laws =
of=20
>arithmetic modulo 2^n where n is the number of bits in the value=20
>representation of that particular size of integer."
Which means that the aforementioned sentence in 5.3.1 would be
superfluous, even if it were correct, or were corrected. There's no
analogue of it in the corresponding C99 section on unary arithmetic
operators, by the way.
--
Gennaro Prota. C++ developer. For hire.
(to mail me, remove any 'u' from the address)
---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.comeaucomputing.com/csc/faq.html ]
Author: "whyglinux" <whyglinux@gmail.com>
Date: Wed, 20 Dec 2006 22:23:25 CST Raw View
===================================== MODERATOR'S COMMENT:
Forwarded to the C++ Committee.
===================================== END OF MODERATOR'S COMMENT
In Section 5.3.1 Unary operators [expr.unary.op], part of the paragraph
7 describes how to compute the negative of an unsigned quantity:
"The negative of an unsigned quantity is computed by subtracting its
value from 2^n, where n is the number of bits in the promoted operand.
The type of the result is the type of the promoted operand."
(2^n means the n-th power of 2.)
According to the above computing method, -0U, the negative of an
unsigned int 0, for example, will get value 2^N - 0 = 2^N, where N is
the number of bits in an unsigned int. However, 2^N is obviously out of
the range of values representable by an unsigned int and thus not the
actual value of -0U. To get the result, a truncating conversion has to
be conducted subsequently.
It seems the method is good for calculating the negative of any
unsigned exept unsigned 0. Perhaps a revision is needed.
---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.comeaucomputing.com/csc/faq.html ]
Author: alfps@start.no ("Alf P. Steinbach")
Date: Thu, 21 Dec 2006 05:48:01 GMT Raw View
* whyglinux:
> =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D MODERATOR'S COMMENT:=20
> Forwarded to the C++ Committee.
>=20
>=20
> =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D END OF MODERATOR'S COMMENT
> In Section 5.3.1 Unary operators [expr.unary.op], part of the paragraph
> 7 describes how to compute the negative of an unsigned quantity:
>=20
> "The negative of an unsigned quantity is computed by subtracting its
> value from 2^n, where n is the number of bits in the promoted operand.
> The type of the result is the type of the promoted operand."
>=20
> (2^n means the n-th power of 2.)
>=20
> According to the above computing method, -0U, the negative of an
> unsigned int 0, for example, will get value 2^N - 0 =3D 2^N, where N is
> the number of bits in an unsigned int. However, 2^N is obviously out of
> the range of values representable by an unsigned int and thus not the
> actual value of -0U. To get the result, a truncating conversion has to
> be conducted subsequently.
>=20
> It seems the method is good for calculating the negative of any
> unsigned exept unsigned 0. Perhaps a revision is needed.
There's no need for a revision.
=A73.9.1/4 "Unsigned integers, declared 'unsigned', shall obey the laws o=
f=20
arithmetic modulo 2^n where n is the number of bits in the value=20
representation of that particular size of integer."
2^n - 0 is congruent to 0 modulo 2^n.
--=20
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.comeaucomputing.com/csc/faq.html ]