This issue tracker has been migrated to GitHub ,
and is currently read-only.
For more information,
see the GitHub FAQs in the Python's Developer Guide.
Created on 2011年12月06日 00:49 by ncoghlan, last changed 2022年04月11日 14:57 by admin. This issue is now closed.
| Messages (7) | |||
|---|---|---|---|
| msg148896 - (view) | Author: Alyssa Coghlan (ncoghlan) * (Python committer) | Date: 2011年12月06日 00:49 | |
This RFE proposes two new methods "to_signed" and "to_unsigned" on 'int' objects and on the numbers.Integral ABC.
Semantics (and number.Integral implementation):
def to_unsigned(self, bits):
"Convert this integer to its unsigned two's complement equivalent for the given bit length"
if self.bit_length() >= bits:
raise ValueError("{} is too large for {}-bit two's complement
precision".format(self, bits))
if self >= 0:
return self
return 2**bits + self # self is known to be negative at this point
def to_signed(self, bits):
"Convert an integer in two's complement format to its signed equivalent for the given bit length"
if self < 0:
raise ValueError("{} is already signed".format(self))
if self.bit_length() > bits:
raise ValueError("{} is too large for {}-bit two's complement
precision".format(self, bits))
upper_bound = 2**bits
if self < (upper_bound / 2):
return self
return upper_bound - self
To add these methods to numbers.Integral, a concrete numbers.Integral.bit_length() operation will also be needed. This can be implemented simply as:
def bit_length(self):
return int(self).bit_length()
(Initial concept from this python-ideas thread: http://mail.python.org/pipermail/python-ideas/2011-December/012989.html)
|
|||
| msg148909 - (view) | Author: Antoine Pitrou (pitrou) * (Python committer) | Date: 2011年12月06日 10:37 | |
Instead of requiring bit_length(), couldn't you simply compare self to 2**(bits-1) (for to_unsigned) and 2**bits (for to_signed)? |
|||
| msg148929 - (view) | Author: Mark Dickinson (mark.dickinson) * (Python committer) | Date: 2011年12月06日 19:24 | |
The 'self.bit_length() >= bits' condition for to_unsigned doesn't look right to me. E.g., if bits == 32, I'd expect the acceptable range of values to be range(-2**31, 2**31)---i.e., including -2**31, but excluding 2**31. But the ValueError for 'self.bit_length() >= 32' means that -2**31 is excluded. |
|||
| msg148930 - (view) | Author: Mark Dickinson (mark.dickinson) * (Python committer) | Date: 2011年12月06日 19:35 | |
On the feature request itself, I have to say that I'm unconvinced. Doing x % (2**32) seems good enough to me, in situations where you don't need the bounds checking. (And it's not clear to me that needing the bounds checks is the more common situation.) |
|||
| msg148931 - (view) | Author: Antoine Pitrou (pitrou) * (Python committer) | Date: 2011年12月06日 19:39 | |
> On the feature request itself, I have to say that I'm unconvinced. > Doing x % (2**32) seems good enough to me, in situations where you > don't need the bounds checking. Ah, I didn't know that modulo worked for that. Nice trick. |
|||
| msg252412 - (view) | Author: Serhiy Storchaka (serhiy.storchaka) * (Python committer) | Date: 2015年10月06日 17:21 | |
I'm for closing this issue. This is not the common operation. |
|||
| msg289189 - (view) | Author: STINNER Victor (vstinner) * (Python committer) | Date: 2017年03月07日 21:33 | |
If you open the can of worms, other features will be requested like uint32+uint32 which would silently overflow. IMHO it would be better to have a package (on PyPI) providing integers of fixed size implementing two's complement arithmetic: int8, uint16, etc. I failed to find an existing module. |
|||
| History | |||
|---|---|---|---|
| Date | User | Action | Args |
| 2022年04月11日 14:57:24 | admin | set | github: 57744 |
| 2017年03月07日 21:33:01 | vstinner | set | messages: + msg289189 |
| 2017年03月07日 19:17:33 | serhiy.storchaka | set | status: pending -> closed resolution: rejected stage: needs patch -> resolved |
| 2015年10月06日 17:21:03 | serhiy.storchaka | set | status: open -> pending nosy: + serhiy.storchaka messages: + msg252412 |
| 2011年12月06日 20:07:07 | vstinner | set | nosy:
+ vstinner |
| 2011年12月06日 19:39:21 | pitrou | set | messages: + msg148931 |
| 2011年12月06日 19:35:54 | mark.dickinson | set | messages: + msg148930 |
| 2011年12月06日 19:24:47 | mark.dickinson | set | messages: + msg148929 |
| 2011年12月06日 19:18:09 | mark.dickinson | set | nosy:
+ mark.dickinson |
| 2011年12月06日 10:37:59 | pitrou | set | nosy:
+ pitrou messages: + msg148909 |
| 2011年12月06日 01:26:22 | cvrebert | set | nosy:
+ cvrebert |
| 2011年12月06日 00:54:40 | eric.smith | set | nosy:
+ eric.smith |
| 2011年12月06日 00:49:34 | ncoghlan | create | |