[Python-Dev] str with base

Bob Ippolito bob at redivi.com
Wed Jan 18 02:56:48 CET 2006


On Jan 17, 2006, at 3:38 PM, Adam Olsen wrote:

> On 1/17/06, Thomas Wouters <thomas at xs4all.net> wrote:
>> On Tue, Jan 17, 2006 at 09:23:29AM -0500, Jason Orendorff wrote:
>>
>>> I think a method 5664400.to_base(13) sounds nice.
>> [And others suggested int-methods too]
>>
>> I would like to point out that this is almost, but not quite,  
>> entirely as
>> inapropriate as using str(). Integers don't have a base. String
>> representations of integers -- and indeed, numbers in general, as  
>> the Python
>> tutorial explains in Appendix B -- have a base. Adding such a  
>> method to
>> integers (and, I presume, longs) would beg the question why  
>> floats, Decimals
>> and complex numbers don't have them.
>
> I dream of a day when str(3.25, base=2) == '11.01'.  That is the
> number a float really represents.  It would be so much easier to
> understand why floats behave the way they do if it were possible to
> print them in binary.

Actually if you wanted something that closely represents what a  
floating point number is then you would want to see this::

	>>> str(3.25, base=2)
	'1.101e1'
	>>> str(0.25, base=2)
	'1.0e-10'

Printing the bits without an exponent is nearly as misleading as  
printing them in decimal.

-bob



More information about the Python-Dev mailing list