Bizarre floating-point output

N

Nick Maclaren

x = (1.234567890125, 1.2345678901255)
print x
print x[0], x[1]

Is there a rational reason, or is that simply an artifact of the way
that the code has evolved? It is clearly not a bug :)


Regards,
Nick Maclaren.
 
R

Richard Brodie

Nick Maclaren said:
x = (1.234567890125, 1.2345678901255)
print x
print x[0], x[1]

Is there a rational reason, or is that simply an artifact of the way
that the code has evolved? It is clearly not a bug :)

print x[0] gives the same result as printing str(x[0]),
the value of x formatted as a string (rounded to a
sensible number of places).

x[0] at the command prompt gives the same result as
printing repr(x), the representation of the text value as
a string.

When you do print on a tuple it doesn't recursively
call str(), so you get the repr representations.

You can get similar results with anything where the
str() and repr() values are different.
e.g. x = ( u'a', u'b')
 
N

Nick Maclaren

|>
|> When you do print on a tuple it doesn't recursively
|> call str(), so you get the repr representations.

Ah! That explains it. I would call that reason intermediate
between rational and an artifact of the way the code has evolved!


Regards,
Nick Maclaren.
 
B

Bjoern Schliessmann

Nick said:
Ah! That explains it. I would call that reason intermediate
between rational and an artifact of the way the code has evolved!

Which code has evolved? Those precision problems are inherent
problems of the way floats are stored in memory.

Regards,


Björn
 
N

Nick Maclaren

|>
|> > Ah! That explains it. I would call that reason intermediate
|> > between rational and an artifact of the way the code has evolved!
|>
|> Which code has evolved? Those precision problems are inherent
|> problems of the way floats are stored in memory.

The use of different precisions for the two cases is not, however,
and it is that I was and am referring to.


Regards,
Nick Maclaren.
 
F

Fredrik Lundh

Nick said:
The use of different precisions for the two cases is not, however,
and it is that I was and am referring to.

that's by design, of course. maybe you should look "repr" up in the
documentation ?

</F>
 
N

Nick Maclaren

|> Nick Maclaren wrote:
|>
|> > The use of different precisions for the two cases is not, however,
|> > and it is that I was and am referring to.
|>
|> that's by design, of course. maybe you should look "repr" up in the
|> documentation ?

I think that you should. Where does it say that tuple's __str__ is
the same as its __repr__?

The obvious interpretation of the documentation is that a sequence
type's __str__ would call __str__ on each sub-object, and its __repr__
would call __repr__.


Regards,
Nick Maclaren.
 
B

Bjoern Schliessmann

Nick said:
I think that you should.

Big words.
Where does it say that tuple's __str__ is the same as its
__repr__?

Where does it say that a tuple's __str__ does not call its contents'
__repr__?
The obvious interpretation of the documentation is that a sequence
type's __str__ would call __str__ on each sub-object,

Where do you read that? BTW, that makes absolutely no sense to me.
Also, lists of Strings would quickly get messed up when displaying
them using __str__.

Regards,


Björn
 
B

Bjoern Schliessmann

Nick said:
The use of different precisions for the two cases is not, however,
and it is that I was and am referring to.

You mistake "precision" with "display".

Regards,


Björn
 
Z

Ziga Seilnacht

Nick said:
I think that you should. Where does it say that tuple's __str__ is
the same as its __repr__?

The obvious interpretation of the documentation is that a sequence
type's __str__ would call __str__ on each sub-object, and its __repr__
would call __repr__.

How would you distinguish ['3', '2', '1'] from [3, 2, 1] in that case?

Ziga
 
N

Nick Maclaren

|>
|> > I think that you should. Where does it say that tuple's __str__ is
|> > the same as its __repr__?
|> >
|> > The obvious interpretation of the documentation is that a sequence
|> > type's __str__ would call __str__ on each sub-object, and its __repr__
|> > would call __repr__.
|>
|> How would you distinguish ['3', '2', '1'] from [3, 2, 1] in that case?

Well, it's not felt necessary to distinguish those at top level, so
why should it be when they are in a sequence?

print "3", 3
3 3

But this whole thing is getting ridiculous. The current implementation
is a bizarre interpretation of the specification, but clearly not an
incorrect one. It isn't important enough to get involved in a religious
war over - I was merely puzzled as to the odd behaviour, because I have
to teach it, and it is the sort of thing that can confuse naive users.


Regards,
Nick Maclaren.
 
N

Nick Maclaren

|>
|> > The use of different precisions for the two cases is not, however,
|> > and it is that I was and am referring to.
|>
|> You mistake "precision" with "display".

Not at all. "Precision" has been used to indicate the number of digits
after the decimal point for at least 60 years, probably 100; in 40 years
of IT and using dozens of programming languages, I have never seen
"display" used for that purpose.


Regards,
Nick Maclaren.
 
Z

Ziga Seilnacht

Nick said:
Well, it's not felt necessary to distinguish those at top level, so
why should it be when they are in a sequence?

Well, this probably wasn't the best example, see the links below
for a better one.
But this whole thing is getting ridiculous. The current implementation
is a bizarre interpretation of the specification, but clearly not an
incorrect one. It isn't important enough to get involved in a religious
war over - I was merely puzzled as to the odd behaviour, because I have
to teach it, and it is the sort of thing that can confuse naive users.

There was a recent bug report identical to your complaints, which
was closed as invalid. The rationale for closing it was that things
like:

print ("a, bc", "de f,", "gh), i")

would be extremely confusing if the current behaviour was changed. See
http://www.python.org/sf/1534769
for details.

Ziga
 
N

Nick Maclaren

|>
|> There was a recent bug report identical to your complaints, which
|> was closed as invalid. The rationale for closing it was that things
|> like:
|>
|> print ("a, bc", "de f,", "gh), i")
|>
|> would be extremely confusing if the current behaviour was changed. See
|> http://www.python.org/sf/1534769
|> for details.

Well, I wasn't complaining - merely querying.

If this approach is taken, it would be better to document it, so that
authors of derived classes follow the convention.


Regards,
Nick Maclaren.
 
B

Bjoern Schliessmann

Nick said:
Not at all. "Precision" has been used to indicate the number of
digits after the decimal point for at least 60 years,

Not only, remember: Computer memories can't think in powers of ten.
probably 100; in 40 years of IT and using dozens of programming
languages, I have never seen "display" used for that purpose.

Yes, but since the representation in computers is based on powers of
two, a certain precision in the dual system, i. e. a fixed amount
of dual places, doesn't correspond with a fixed amount of decimal
places. Thus the rounding while displaying -- just to make it look
prettier. The very minimal additional error is silently accepted.

Regards,


Björn
 

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

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top