Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > control precision for str(obj) output?

Reply
Thread Tools

control precision for str(obj) output?

 
 
Bo Peng
Guest
Posts: n/a
 
      05-04-2005
Dear list,

I have enjoyed the convenience to output any object with str(obj) for a
while. However, I get long output for things like str([0.0002]) in my
output (which bothers my users more than me though). I also do not
understand why the following is happening:

>>> str([0.0002])

'[0.00020000000000000001]'
>>> str(0.0002)

'0.0002'

Is there any way to fix this, other than checking the type of everything
and output with %.4f?

Many thanks in advance.
Bo
 
Reply With Quote
 
 
 
 
Mike Meyer
Guest
Posts: n/a
 
      05-04-2005
Bo Peng <(E-Mail Removed)> writes:

> Dear list,
>
> I have enjoyed the convenience to output any object with str(obj) for
> a while. However, I get long output for things like str([0.0002]) in
> my output (which bothers my users more than me though). I also do not
> understand why the following is happening:
>
> >>> str([0.0002])

> '[0.00020000000000000001]'
> >>> str(0.0002)

> '0.0002'
>
> Is there any way to fix this, other than checking the type of
> everything and output with %.4f?


Two things are going on here.

1) str is returning a "nicely printable" version of the object. For
floats, that means providing a precision so they behave as naive users
expect. repr, on the other hand, is returning a printable
representation of the object - which in the case of floats means the
true value of the float, not it's "nice" approximation.

2) str on a list uses repr to turn all the elements in the list to
strings for printing. This is pretty much required for strings to be
recognizable as such in the output.

And no, there's no way to control what repr is going to do. You could
subclass list and provide an str method that checks for floats, and
calls str on them instead of repr (probably better than using %.4f on
them all), but you can't really fix list.

Someone want to tell me the procedure for submitting FAQ entries, so I
can do that for this?

Thanks,
<mike
--
Mike Meyer <(E-Mail Removed)> http://www.mired.org/home/mwm/
Independent WWW/Perforce/FreeBSD/Unix consultant, email for more information.
 
Reply With Quote
 
 
 
 
Dan Bishop
Guest
Posts: n/a
 
      05-04-2005
Bo Peng wrote:
> Dear list,
>
> I have enjoyed the convenience to output any object with str(obj) for

a
> while. However, I get long output for things like str([0.0002]) in my


> output (which bothers my users more than me though). I also do not
> understand why the following is happening:
>
> >>> str([0.0002])

> '[0.00020000000000000001]'
> >>> str(0.0002)

> '0.0002'


Floats make a distinction betwen repr and str: repr(x) uses 17
significant digits (to ensure that repr(1.0) and repr(1.0+epsilon) are
distinct, while str(x) uses only 12 (to produce more user-friendly
output).

For lists, repr(x) calls repr(x[i]) for each of its elements. Just
what you'd expect.

But lists, unlike floats, do *not* make a distinction between str and
repr; i.e. str(x) == repr(x). The main reason for doing it this way
was to avoid confusion in cases like str(["1, 2", 3]): If list.__str__
were implemented like the list_str function below, the result would
look like a list of 3 ints instead of a string and an int.

The unfortunate side effect of this is to make str(list of floats)
ugly, but...

> Is there any way to fix this, other than checking the type of

everything
> and output with %.4f?


def list_str(x):
return '[%s]' % ', '.join(map(str, x))

 
Reply With Quote
 
Bo Peng
Guest
Posts: n/a
 
      05-04-2005
> The unfortunate side effect of this is to make str(list of floats)
> ugly, but...


Yeah, Nothing is perfect. I can control the damage as long as I know
what is going on. I guess this would fit in a faq list quite well.

I would have to thank Mike and Dan for your quick and detailed reply. As
a matter of fact, this python list is the most friendly mailinglist I
have ever joined. Thanks!

Bo
 
Reply With Quote
 
Andrew Dalke
Guest
Posts: n/a
 
      05-04-2005
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/genera...-so-inaccurate

which has a link to an even more detailed chapter in the tutorial at
http://docs.python.org/tut/node16.html

?

Andrew
http://www.velocityreviews.com/forums/(E-Mail Removed)

 
Reply With Quote
 
Dan Bishop
Guest
Posts: n/a
 
      05-04-2005
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/genera...-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 .

 
Reply With Quote
 
Bo Peng
Guest
Posts: n/a
 
      05-04-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/genera...-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
 
Reply With Quote
 
Mike Meyer
Guest
Posts: n/a
 
      05-04-2005
"Dan Bishop" <(E-Mail Removed)> writes:

> 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/genera...-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 .


Exactly. The point that needs to go in the FAQ is the behavior of str
on lists, not floating point representation. The latter needs to be
mentioned, but isn't critical.

A quick check of the Python site turns up no discussion on submitting
new FAQ entries. There are patch submission guidelines, and lots of
things related to the mailman FAQ, but nothing on how to submit new
entries for the FAQ. Or has the FAQ been supplanted by the wiki at
www.python.org/moin, even though there doesn't appear to be a FAQ
page?

<mike
--
Mike Meyer <(E-Mail Removed)> http://www.mired.org/home/mwm/
Independent WWW/Perforce/FreeBSD/Unix consultant, email for more information.
 
Reply With Quote
 
Michael Hoffman
Guest
Posts: n/a
 
      05-04-2005
Mike Meyer wrote:

> A quick check of the Python site turns up no discussion on submitting
> new FAQ entries. There are patch submission guidelines, and lots of
> things related to the mailman FAQ, but nothing on how to submit new
> entries for the FAQ.


E-mail (E-Mail Removed) and (E-Mail Removed).

> Or has the FAQ been supplanted by the wiki at
> www.python.org/moin...


No.
--
Michael Hoffman
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
How to control the precision of double Bo Yang C++ 10 03-16-2007 03:13 PM
ERROR: [ODBC Microsoft Access Driver]Invalid precision value =?Utf-8?B?UmV6YSBOYWJp?= ASP .Net 1 09-09-2005 01:26 PM
Bug in double precision computing? Daniel Geisenhoff ASP .Net 1 07-29-2004 01:58 PM
Need > 15 digits precision without big performance hit Jasper Perl 1 06-27-2004 08:25 AM
Decimal precision ? Piotr ASP .Net 1 04-02-2004 12:43 PM



Advertisments