cpython list __str__ method for floats

david david at nospam.spam
Tue Sep 11 21:50:49 EDT 2007


Bjoern Schliessmann wrote:
> [david] wrote:
>> returns poorly formatted values:
> 
> Please explain.
> 
>>  >>>str(13.3)
>> '13.3'
>>  >>>str([13.3])
>> '[13.300000000000001]'
> 
> This is quite a FAQ.
> 
> str of a float returns the float, rounded to decimal precision.
> 
> str of a list returns a square brackets enclosed enumeration of the
> contents (using repr on them). repr of a float returns the float in
> full precision.
> 
> Regards,
> 
> 
> Björn
> 

 > contents (using repr on them). repr of a float returns the float in
 > full precision.

But of course it doesn't, as illustrated, which is the whole point.

It returns a string at greater than full precision.

Leaving aside the question of why str should return repr,
13.300000000000001 is not 'the float in full precision':
it is an arbitrary translation of the float.

The idea that 13.3 is a 'rounded' value for the number,
and that 13.300000000000001 is not a 'rounded' value of
the number, is a common error of intuitive mathematics.

I hope that when you say that this is a FAQ, you don't
mean that the community has solidified on this naive
interpretation :~)


[david]





More information about the Python-list mailing list