cpython list __str__ method for floats

D

[david]

Bjoern said:
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]
 
B

Bjoern Schliessmann

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. said:
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....)
 
D

Duncan Booth

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:
7.1054273576010019e-015

but why shouldn't Python do this instead?:
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.
 
J

John Machin

On Sep 12, 10:59 pm, Duncan Booth <[email protected]>
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 :)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top