[Python-Dev] str with base

Adam Olsen rhamph at gmail.com
Wed Jan 18 03:17:27 CET 2006


On 1/17/06, Bob Ippolito <bob at redivi.com> wrote:
>
> On Jan 17, 2006, at 3:38 PM, Adam Olsen wrote:
>
> > 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.

I disagree.  The exponent is involved in rounding to fit in compact
storage but once that is complete the value can be represented exactly
without it.

--
Adam Olsen, aka Rhamphoryncus


More information about the Python-Dev mailing list