[Python-ideas] Negative hexes

Guido van Rossum guido at python.org
Tue Dec 6 01:16:59 CET 2011


On Mon, Dec 5, 2011 at 4:14 PM, Nick Coghlan <ncoghlan at gmail.com> wrote:

> On Tue, Dec 6, 2011 at 9:10 AM, Antoine Pitrou <solipsis at pitrou.net>
> wrote:
> > On Tue, 6 Dec 2011 08:19:53 +1000
> > Nick Coghlan <ncoghlan at gmail.com> wrote:
> >> On Tue, Dec 6, 2011 at 3:15 AM, Antoine Pitrou <
> solipsis-xNDA5Wrcr86sTnJN9+BGXg at public.gmane.org> wrote:
> >> >> That is, currently:
> >> >>
> >> >> >>> "{:.4x}".format(31)
> >> >> Traceback (most recent call last):
> >> >>   File "<stdin>", line 1, in <module>
> >> >> ValueError: Precision not allowed in integer format specifier
> >> >
> >> > This is so poorly discoverable that I don't think it's worth it.
> >> > Guido's approach looks sufficient to me.
> >>
> >> I agree the formatting approach is way too obscure, but did you see my
> >> later suggestion of an "as_twos_complement(bit_length)" conversion
> >> method on int objects? (we could actually provide a more generic
> >> version on numbers.Integer as well)
> >
> > Ah, right. Yes, I think that would be useful, although a shorter name
> > would be nicer. to_unsigned() perhaps?
>
> Hmm, in the sense that the answer we're getting is the same answer you
> would get with a cast to an unsigned type at the C level? I think
> that's a little misleading - conceptually, the number is still signed,
> we're just representing it differently (i.e. explicitly using the twos
> complement form, rather than the the normal sign bit).
>

I don't think there's a better term available. As long as the return value
of to_unsigned() is never negative I think it's a fine name.


> I'd be OK with dropping the explicit 'twos' qualifier, though - then
> the method name could just be "to_complement()".
>
> I guess we'd also want a "to_signed()" to reverse the process:
>

Sure.


> def to_complement(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
>
> Regards,
> Nick.
>
> --
> Nick Coghlan   |   ncoghlan at gmail.com   |   Brisbane, Australia
> _______________________________________________
> Python-ideas mailing list
> Python-ideas at python.org
> http://mail.python.org/mailman/listinfo/python-ideas
>



-- 
--Guido van Rossum (python.org/~guido)
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-ideas/attachments/20111205/a3a2531f/attachment.html>


More information about the Python-ideas mailing list