 |
C++Talk.NET C++ language newsgroups
|
| View previous topic :: View next topic |
| Author |
Message |
whyglinux Guest
|
Posted: Thu Dec 21, 2006 4:23 am Post subject: Defect Report: Negative of unsigned computation not suffice? |
|
|
===================================== 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 ] |
|
| Back to top |
|
 |
Alf P. Steinbach Guest
|
Posted: Thu Dec 21, 2006 10:10 am Post subject: Re: Defect Report: Negative of unsigned computation not suff |
|
|
* whyglinux:
| Quote: | ===================================== 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.
|
There's no need for a revision.
§3.9.1/4 "Unsigned integers, declared 'unsigned', shall obey the laws of
arithmetic modulo 2^n where n is the number of bits in the value
representation of that particular size of integer."
2^n - 0 is congruent to 0 modulo 2^n.
--
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 ] |
|
| Back to top |
|
 |
Gennaro Prota Guest
|
Posted: Thu Dec 21, 2006 9:44 pm Post subject: Re: Defect Report: Negative of unsigned computation not suff |
|
|
On Thu, 21 Dec 2006 05:48:01 GMT, "Alf P. Steinbach" wrote:
| Quote: | 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.
There's no need for a revision.
|
I disagree: the sentence the OP quotes is wrong and contradicts the
one in 3.9.1.
| Quote: | §3.9.1/4 "Unsigned integers, declared 'unsigned', shall obey the laws of
arithmetic modulo 2^n where n is the number of bits in the value
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 ] |
|
| Back to top |
|
 |
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
|