473,385 Members | 2,013 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

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

Bizarre floating-point output


x = (1.234567890125, 1.2345678901255)
print x
print x[0], x[1]
>>(1.2345678901249999, 1.2345678901254999)
1.23456789012 1.23456789013
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.
Jan 8 '07 #1
14 1428

"Nick Maclaren" <nm**@cus.cam.ac.ukwrote in message
news:en**********@gemini.csx.cam.ac.uk...
>
x = (1.234567890125, 1.2345678901255)
print x
print x[0], x[1]
>>>(1.2345678901249999, 1.2345678901254999)
1.23456789012 1.23456789013

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')
Jan 8 '07 #2

In article <en*********@south.jnrs.ja.net>,
"Richard Brodie" <R.******@rl.ac.ukwrites:
|>
|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.
Jan 8 '07 #3
Nick Maclaren wrote:
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

--
BOFH excuse #292:

We ran out of dial tone and we're and waiting for the phone company
to deliver another bottle.

Jan 8 '07 #4

In article <50*************@mid.individual.net>,
Bjoern Schliessmann <us**************************@spamgourmet.comwrite s:
|Nick Maclaren wrote:
|>
| 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.
Jan 8 '07 #5
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 ?

</F>

Jan 8 '07 #6

In article <ma***************************************@python. org>, Fredrik Lundh <fr*****@pythonware.comwrites:
|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.
Jan 8 '07 #7
Nick Maclaren wrote:
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

--
BOFH excuse #359:

YOU HAVE AN I/O ERROR -Incompetent Operator error

Jan 8 '07 #8
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.
You mistake "precision" with "display".

Regards,
Björn

--
BOFH excuse #12:

dry joints on cable plug

Jan 8 '07 #9
Nick Maclaren wrote:
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

Jan 8 '07 #10

In article <11**********************@s34g2000cwa.googlegroups .com>,
"Ziga Seilnacht" <zi************@gmail.comwrites:
|>
| 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.
Jan 8 '07 #11

In article <50*************@mid.individual.net>,
Bjoern Schliessmann <us**************************@spamgourmet.comwrite s:
|>
| 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.
Jan 8 '07 #12
Nick Maclaren wrote:
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

Jan 8 '07 #13

In article <11**********************@11g2000cwr.googlegroups. com>,
"Ziga Seilnacht" <zi************@gmail.comwrites:
|>
|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.
Jan 8 '07 #14
Nick Maclaren wrote:
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

--
BOFH excuse #199:

the curls in your keyboard cord are losing electricity.

Jan 8 '07 #15

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

Similar topics

4
by: Alan Little | last post by:
This is very bizarre. Could someone else have a look at this? Maybe you can see something I'm overlooking. Go here: http://www.newsletters.forbes.com/enews/admin/deliver.php4 U: bugtest P:...
1
by: George Hester | last post by:
At the time this suggestion was made I didn't have the wherewithall to even attempt it. But over time I learned enough to make a stab at it. Let just say the foating DIV had to provide the same...
11
by: Frances Del Rio | last post by:
this is so bizarre, and don't even know if this is right place to ask, but don't know where else: about two days I changed webhosting, changed DNS for my domain, francesdelrio.com, now when I...
1
by: Richard | last post by:
Here's a Bizarre one: I have a C# service program. The service registers and runs fine on one machine but when I copy the EXE to a second machine and try to use InstallUtil to register the...
1
by: Michael Carr | last post by:
I have created a website that responds to the following two domain names: logintest.carr-engineering.com logintest.onualumni.org If you open either of these websites on their own, everything...
2
by: Qiang | last post by:
Those who have used Google notebook may notice that google notebook displays the notes in a small floating window of the browser. I have tried to create a similar floating window, but with no luck....
1
by: zoehart | last post by:
I'm working with VBScript to build a text email message. I'm seeing a variety of bizarre formatting issues. The following lines of code MT = MT & vbCrLf & "Card Type: " & CardType MT = MT &...
0
by: ckfan.painter | last post by:
I've run into a seemingly bizarre problem with insert() for std::vector. (This was done on Microsoft Visual C++ 2005 express version 8...maybe it is a compiler specific bug?) Here's the code: ...
0
by: dartanian | last post by:
Hi, I'm trying to implement the traditional CSS-only nifty corners on two panes that are floating within the same DIV. With one pane, it works great. But with 2 panes, the border (and rounded...
6
by: Jeremy | last post by:
I've got a floating div which becomes visible when a link is clicked. I want the div to be hidden when the user clicks anywhere on the page except for whithin the div. What is the best way to do...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

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.