Negative long literals (was Re: Does Python need a '>>>' operator?)

Bengt Richter bokr at oz.net
Wed Jun 12 14:56:38 EDT 2002


On Mon, 10 Jun 2002 08:19:09 -0400, Tim Peters <tim.one at comcast.net> wrote:

>[Beni Cherniavksy]
>> I just got another idea: use 0x1234 for 0-filled numbers and 1xABCD for
>> 1-filled ones.  That way you impose no restrictions on what follows the
>> prefix and keep backward compatibility.  0xFFFFFFFF stays a 2^n-1
>> _positive_ number, as it should be.  The look of 1x is weird at first but
>> it is very logical...
>
>Indeed, on both counts <wink>.  I like it a lot!
>
>For the other points related to getting rid of machine long-size
>assumptions, see PEP 237:  <http://www.python.org/peps/pep-0237.html>.
>
It was too easy for me to grasp at first ;-) Now that I know that
1x7 == 1xf7 == 1xff7, I can relate it to my (with Greg mod) suggested
format 0hf7. Mostly they're interchageable by substituting 0hf for 1x
and 0h0 for 0x as prefixes, but I like Beni's better. (They'd be fully
interchangable if 1x == 1xf ;-)

In a private email Paul Foley joked about writing 042 for 42 and 942
for -58 (i.e., why limit it to radix 16), and protested that machine
representation was being exposed.

It was not my purpose to expose machine representation, and neither
format (mine or Beni's) should be thought of as such IMO. I was
actually going to suggest an analogous binary notation (0b...), so
I'm happy not to limit it to radix 16.

The following decoding routine shows that the formats are not
about exposing machine representation, IMO:

 >>> def benidecode(s):
 ...     if s[1].lower()=='x': return long(s[2:],16)-long(s[0])*16L**(len(s)-2)
 ...     elif s[1].lower()=='b': return long(s[2:],2)-long(s[0])*2L**(len(s)-2)
 ...     elif s[1].lower()=='d': return long(s[2:],10)-long(s[0])*10L**(len(s)-2)
 ...     elif s[1].lower()=='o': return long(s[2:],8)-long(s[0])*8L**(len(s)-2)
 ...     raise NotImplementedError, 'radix "%s" not implemented"' % s[1]
 ...
 >>> benidecode('1x7')
 -9L
 >>> benidecode('1b0111')
 -9L
 >>> benidecode('1o67')
 -9L
 >>> benidecode('0x7')
 7L
 >>> benidecode('0b0111')
 7L
 >>> benidecode('0o07')
 7L
 >>> benidecode('0z07')
 Traceback (most recent call last):
   File "<stdin>", line 1, in ?
   File "<stdin>", line 6, in benidecode
 NotImplementedError: radix "z" not implemented"

And for Paul ;-)
 >>> benidecode('0d42')
 42L
 >>> benidecode('0d042')
 42L
 >>> benidecode('1d42')
 -58L
 >>> benidecode('1d942')
 -58L
 >>> benidecode('1d9942')
 -58L
 
Regards,
Bengt Richter



More information about the Python-list mailing list