control precision for str(obj) output?

Bo Peng bpeng at rice.edu
Wed May 4 09:15:16 EDT 2005


Dan Bishop wrote:
> Andrew Dalke wrote:
> 
>>Mike Meyer wrote:
>>
>>>Someone want to tell me the procedure for submitting FAQ entries,
> 
> so I
> 
>>>can do that for this?
>>
>>You mean more than what already exists at
>>
> 
> http://www.python.org/doc/faq/general.html#why-are-floating-point-calculations-so-inaccurate
> 
>>which has a link to an even more detailed chapter in the tutorial at
>>  http://docs.python.org/tut/node16.html
> 
> 
> Yes.  Those pages explain why repr(0.2) != '0.2', but they don't
> explain why str([x]) != '[%s]' % x .
> 

I am new to Python but has experience with other languages. I am quite 
aware of 'why-are-floating-point-calculations-so-inaccurate'. I would 
not be surprised if

 >>> str[0.0002)
0.000200000000001
 >>> str([0.0002])
[0.000200000000001]

It was the difference between str([x]) and '[%s]' % x str(x) that 
confused me.

Bo



More information about the Python-list mailing list