472,989 Members | 3,050 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,989 software developers and data experts.

cpython list __str__ method for floats

returns poorly formatted values:
>>>str(13.3)
'13.3'
>>>str([13.3])
'[13.300000000000001]'

[david]
Sep 11 '07 #1
6 1234
On Sep 11, 4:07 am, "[david]" <da...@nospam.spamwrote:
returns poorly formatted values:
>>>str(13.3)
'13.3'
>>>str([13.3])
'[13.300000000000001]'

[david]
There is some difference in the way repr() and str() convert floats to
strings:
>>a = 13.3
print str(a)
13.3
>>print repr(a)
13.300000000000001
>>a
13.300000000000001
>>a.__str__()
'13.3'
>>a.__repr__()
'13.300000000000001'
>>>
Sep 11 '07 #2
[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

--
BOFH excuse #442:

Trojan horse ran out of hay

Sep 11 '07 #3
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]
Sep 12 '07 #4
[david] wrote:
Leaving aside the question of why str should return repr,
str doesn't "return" repr. str returns a "nice string
representation" of an object. This "nice string representation" of
a list is the opening square bracket, the repr of its contents
seperated by comma, and the closing square bracket.
<prayer-mill>Here, it *only* makes sense to have a list printed
with the repr of their contents.</prayer-mill>
13.300000000000001 is not 'the float in full precision':
it is an arbitrary translation of the float.
Do you know IEEE 754?
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'm intrigued how /you/'d explain this, please do explain.
I hope that when you say that this is a FAQ, you don't
mean that the community has solidified on this naive
interpretation :~)
No, I mean that your complaint is not at all new. Reading the
archives you could have learned a lot about this topic.

Regards,
Björn

--
BOFH excuse #247:

Due to Federal Budget problems we have been forced to cut back on
the number of users able to access the system at one time. (namely
none allowed....)

Sep 12 '07 #5
Bjoern Schliessmann <us**************************@spamgourmet.com>
wrote:
>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'm intrigued how /you/'d explain this, please do explain.
I think he is correct here: 13.300000000000001 is not exactly
representable in IEEE 754. It is a rounded approximation to the true
value just as is 13.3.

An argument can be made that instead of rounding the internal value to
17 digits which is sufficient to ensure that you can roundtrip float->
string->float for all values, you could just round it to the minimum
number of digits which guarantee the float->string->float roundtrip for
that particular value.

Consider this as we gradually lose the more significant digits we see
that last digit wasn't exactly 1 at all:
>>13.3
13.300000000000001
>>13.3-13
0.30000000000000071
>>(13.3-13)*10-3
7.1054273576010019e-015

but why shouldn't Python do this instead?:
>>13.3
13.3
>>13.3-13
0.3
>>(13.3-13)*10-3
7.1054273576e-015

These values will still roundtrip to the exact same internal
representations. BTW, I didn't have to work too hard to figure out what
that last value should be, the first is cut/paste from CPython, the
second is what IronPython gives you.

I believe the claim is that using the full 17 digits ensures the round-
tripping works even if you serialise and deserialise on different
systems, so perhaps we all pay a cost in our interactive sessions for
something which should really be buried deep in IPC code.

Sep 12 '07 #6
On Sep 12, 10:59 pm, Duncan Booth <duncan.bo...@invalid.invalid>
wrote:>

[snip]
I believe the claim is that using the full 17 digits ensures the round-
tripping works even if you serialise and deserialise on different
systems, so perhaps we all pay a cost in our interactive sessions for
something which should really be buried deep in IPC code.
IPC? Most data transfer between systems in the real world is done
using CSV files :-)

Sep 12 '07 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Matthew Walsh | last post by:
Whats wrong with the following code? using pythonWin I get the following error: Traceback (most recent call last): File "C:\Python23\Lib\site-packages\pythonwin\pywin\framework\scriptutils.py",...
1
by: Edward C. Jones | last post by:
#! /usr/bin/env python class A(list): def __init__(self, alist, n): list.__init__(self, alist) self.n = n def __str__(self): return 'AS(%s, %i)' % (list.__str__(self), self.n)
6
by: ahart | last post by:
I'm pretty new to python and am trying to write a fairly small application to learn more about the language. I'm noticing some unexpected behavior in using lists in some classes to hold child...
11
by: Andy Watson | last post by:
I have an application that scans and processes a bunch of text files. The content I'm pulling out and holding in memory is at least 200MB. I'd love to be able to tell the CPython virtual machine...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 4 Oct 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM) The start time is equivalent to 19:00 (7PM) in Central...
0
by: Aliciasmith | last post by:
In an age dominated by smartphones, having a mobile app for your business is no longer an option; it's a necessity. Whether you're a startup or an established enterprise, finding the right mobile app...
2
by: giovanniandrean | last post by:
The energy model is structured as follows and uses excel sheets to give input data: 1-Utility.py contains all the functions needed to calculate the variables and other minor things (mentions...
4
NeoPa
by: NeoPa | last post by:
Hello everyone. I find myself stuck trying to find the VBA way to get Access to create a PDF of the currently-selected (and open) object (Form or Report). I know it can be done by selecting :...
3
NeoPa
by: NeoPa | last post by:
Introduction For this article I'll be using a very simple database which has Form (clsForm) & Report (clsReport) classes that simply handle making the calling Form invisible until the Form, or all...
1
by: Teri B | last post by:
Hi, I have created a sub-form Roles. In my course form the user selects the roles assigned to the course. 0ne-to-many. One course many roles. Then I created a report based on the Course form and...
0
NeoPa
by: NeoPa | last post by:
Introduction For this article I'll be focusing on the Report (clsReport) class. This simply handles making the calling Form invisible until all of the Reports opened by it have been closed, when it...
4
by: GKJR | last post by:
Does anyone have a recommendation to build a standalone application to replace an Access database? I have my bookkeeping software I developed in Access that I would like to make available to other...
3
SueHopson
by: SueHopson | last post by:
Hi All, I'm trying to create a single code (run off a button that calls the Private Sub) for our parts list report that will allow the user to filter by either/both PartVendor and PartType. On...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.