473,395 Members | 1,454 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,395 software developers and data experts.

a print bug?

it seems that the behavior of "print" is that it will round off
properly for any floating point imperfection, such as shown in the
first two samples. The third sample seems to be a bug? It doesn't
know how to handle the floating imperfection in this case.
>>1.2345
1.2344999999999999
>>print 1.2345
1.2345
>>print "%10.3f" % 1.2345 # seems like a bug ----------------------------------
1.234
>>print "%10.3f" % 1.23450001
1.235
>>print "%10.3f" % 1.2344
1.234
>>print "%10.3f" % 1.2346
1.235

Jul 26 '06 #1
9 1656

Sybren Stuvel wrote:
It has nothing to do with the print command, and everything with
floating point precision. See http://docs.python.org/tut/node16.html

how about the discrepancy between
>>print 1.2345
1.2345
>>print "%10.3f" % 1.2345 # seems like a bug
1.234

the first one, print knows enough to recognize and print it as 1.2345.
however, in the second line, when it is round off, it doesn't know it
is 1.2345 any more.

Jul 27 '06 #2

Summercooln...@gmail.com wrote:
how about the discrepancy between
>print 1.2345

1.2345
>print "%10.3f" % 1.2345 # seems like a bug

1.234

the first one, print knows enough to recognize and print it as 1.2345.
however, in the second line, when it is round off, it doesn't know it
is 1.2345 any more.
I think maybe this is the reason: the first one, print will print it
out with a rounding to the 11th decimal point, therefore hiding any
floating point imperfection.

however, in the second one, print will not first round it off to the
11th decimal point with a subsequent rounding off to the 3rd decimal
point. In that case, the floating point imperfection is manifested.
(by thinking it is 1.2344999999999999)

a question is: since print can nicely hide and smooth out the floating
point imperfection, and probably most people prefer it that way, how
come the implementation of print "%10.3f" doesn't also do that --
eliminating the imperfection first, and then print it out accordingly.
I think one argument is the loss of precision, but we only print it,
rather than modify the number or the variable itself... hm... say, if
a bank uses python to print out the "change that should be returned to
the customer" using print "%20.2f", then there will be a cent missing
here and there... why not smooth out that imperfection and return that
penny to the customer? (not that they really care).

Jul 27 '06 #3
>>print "%10.3f" % 1.2345 # seems like a bug
1.234

the first one, print knows enough to recognize and print it as 1.2345.
however, in the second line, when it is round off, it doesn't know it
is 1.2345 any more.
That is because it isn't 1.2345 anymore. 1.2345 cannot be represented
exactly in radix-2 floating point arithmetic.
I think maybe this is the reason: the first one, print will print it
out with a rounding to the 11th decimal point, therefore hiding any
floating point imperfection.

however, in the second one, print will not first round it off to the
11th decimal point with a subsequent rounding off to the 3rd decimal
point. In that case, the floating point imperfection is manifested.
(by thinking it is 1.2344999999999999)

a question is: since print can nicely hide and smooth out the floating
point imperfection, and probably most people prefer it that way, how
I wouldn't prefer it that way. ;)
come the implementation of print "%10.3f" doesn't also do that --
eliminating the imperfection first, and then print it out accordingly.
I think one argument is the loss of precision, but we only print it,
rather than modify the number or the variable itself... hm... say, if
a bank uses python to print out the "change that should be returned to
the customer" using print "%20.2f", then there will be a cent missing
here and there... why not smooth out that imperfection and return that
penny to the customer? (not that they really care).
The decimal module will do what you want. One of its primary
motivations was correct rounding for financial activities.

casevh

Jul 27 '06 #4
Su************@gmail.com wrote:
>
Sybren Stuvel wrote:
>It has nothing to do with the print command, and everything with
floating point precision. See http://docs.python.org/tut/node16.html


how about the discrepancy between
>>>print 1.2345

1.2345
>>>print "%10.3f" % 1.2345 # seems like a bug

1.234

the first one, print knows enough to recognize and print it as 1.2345.
however, in the second line, when it is round off, it doesn't know it
is 1.2345 any more.
But you wouldn't complain about this would you?
>>print "%10.4f" % 1.23445
1.2345
>>print "%10.3f" % 1.23445
1.234

A value which is slightly than 1.2345 prints to 4 decimal places as 1.2345
and to 3 decimal places as 1.234.

That's all that happens with your value as well: 1.2345 is not exactly
representable as a floating point number, and the nearest representable
number is less than 1.2345.
Jul 27 '06 #5
Su************@gmail.com wrote:
it seems that the behavior of "print" is that it will round off
properly for any floating point imperfection, such as shown in the
first two samples. The third sample seems to be a bug? It doesn't
know how to handle the floating imperfection in this case.

>>>>1.2345

1.2344999999999999

>>>>print 1.2345

1.2345

>>>>print "%10.3f" % 1.2345 # seems like a bug ----------------------------------

1.234

>>>>print "%10.3f" % 1.23450001

1.235

>>>>print "%10.3f" % 1.2344

1.234

>>>>print "%10.3f" % 1.2346

1.235
>>print "%25.22f" % 1.2345
1.2344999999999999000000

You obviously haven't yet passed your floating-point number proficiency
test yet. Please restrict yourself to integers until you understand the
difficulties that inaccuracies in floating-point can create ;-)

regards
Steve
--
Steve Holden +44 150 684 7255 +1 800 494 3119
Holden Web LLC/Ltd http://www.holdenweb.com
Skype: holdenweb http://holdenweb.blogspot.com
Recent Ramblings http://del.icio.us/steve.holden

Jul 27 '06 #6

Duncan Booth wrote:
But you wouldn't complain about this would you?
>print "%10.4f" % 1.23445
1.2345
>print "%10.3f" % 1.23445
1.234

A value which is slightly than 1.2345 prints to 4 decimal places as 1.2345
and to 3 decimal places as 1.234.

That's all that happens with your value as well: 1.2345 is not exactly
representable as a floating point number, and the nearest representable
number is less than 1.2345.
This is the expected behavior though... even school when they first
teach "rounding off", they will tell you 1.23445 rounding off to 3
decimal places is not 1.235.... and i don't see anything weird about
the two lines above.

Jul 28 '06 #7
Steve Holden wrote:
You obviously haven't yet passed your floating-point number proficiency
test yet. Please restrict yourself to integers until you understand the
difficulties that inaccuracies in floating-point can create ;-)
hm, actually, i understand the limitation of floating point.
but my real concern is, how come "print" favors one version over the
other...
the first version will favor the correct rounding, while the second
doesn't favor it. to me, this is biased computing.... i will feel
happier if the behavior is consistent. (such as the first line
printing out as 1.2344999999999) . if most people favor the behavior
of line 1, that is, silently rounding off to about the 11th decimal
places, then why doesn't printing with formatting also use that same
behavior, which is rounding off to the 11th places first, and then
round off to whatever the user wants.
>>>>print 1.2345
1.2345
>>>>print "%10.3f" % 1.2345
1.234
but then again, i just realize even if i do a
>>round(1.2344999999999999999, 6)
1.2344999999999999

so even if you round it off, it is still represented the same way....
still, how "print" handles 1.2345 and treating it and printing it as
1.2345, i wonder why we don't make the "print with formatting" the same
behavior, treating it as 1.2345 first and round it off to 1.235

Jul 28 '06 #8
Su************@gmail.com schreef:
Steve Holden wrote:
>You obviously haven't yet passed your floating-point number proficiency
test yet. Please restrict yourself to integers until you understand the
difficulties that inaccuracies in floating-point can create ;-)

hm, actually, i understand the limitation of floating point.
but my real concern is, how come "print" favors one version over the
other...
the first version will favor the correct rounding, while the second
doesn't favor it. to me, this is biased computing.... i will feel
happier if the behavior is consistent. (such as the first line
printing out as 1.2344999999999) . if most people favor the behavior
of line 1, that is, silently rounding off to about the 11th decimal
places, then why doesn't printing with formatting also use that same
behavior, which is rounding off to the 11th places first, and then
round off to whatever the user wants.
>>>>print 1.2345
>1.2345
>>>>print "%10.3f" % 1.2345
> 1.234

but then again, i just realize even if i do a
>>>round(1.2344999999999999999, 6)

1.2344999999999999

so even if you round it off, it is still represented the same way....
still, how "print" handles 1.2345 and treating it and printing it as
1.2345, i wonder why we don't make the "print with formatting" the same
behavior, treating it as 1.2345 first and round it off to 1.235
You do realize that your formatting uses less decimal places than the
print statement, do you?
>>print 1.2345
1.2345
>>print "%0.3f" % 1.2345
1.234
>>print "%0.4f" % 1.2345
1.2345

If you use 4 decimals after the decimal sign, you get the same result as
with a plain print statement (at least in this case). That means that
print is not treating it as 1.2345 first and then rounding it off to
1.235; it is just using the actual internal representation and rounding
it off to 1.2345. Which IMO is the only sensible thing one can do.
--
If I have been able to see further, it was only because I stood
on the shoulders of giants. -- Isaac Newton

Roel Schroeven
Jul 28 '06 #9
wrote:
>
Duncan Booth wrote:
>But you wouldn't complain about this would you?
>>print "%10.4f" % 1.23445
1.2345
>>print "%10.3f" % 1.23445
1.234

A value which is slightly than 1.2345 prints to 4 decimal places as
1.2345 and to 3 decimal places as 1.234.

That's all that happens with your value as well: 1.2345 is not
exactly representable as a floating point number, and the nearest
representable number is less than 1.2345.

This is the expected behavior though... even school when they first
teach "rounding off", they will tell you 1.23445 rounding off to 3
decimal places is not 1.235.... and i don't see anything weird about
the two lines above.
At least where I went to school they taught that the correct rounding was
to the even digit, so 1.2335 and 1.2345 would both round to 1.234.

However, that is irrelevant. The number in question here is
1.2344999999999999 and however you were taught to round numbers that ought
to round to 1.234 for 3 decimals, and 1.2345 for 4.

Jul 28 '06 #10

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

Similar topics

12
by: Michael Foord | last post by:
Here's a little oddity with 'print' being a reserved word... >>> class thing: pass >>> something = thing() >>> something.print = 3 SyntaxError: invalid syntax >>> print something.__dict__...
14
by: Marcin Ciura | last post by:
Here is a pre-PEP about print that I wrote recently. Please let me know what is the community's opinion on it. Cheers, Marcin PEP: XXX Title: Print Without Intervening Space Version:...
0
by: bearophileHUGS | last post by:
There is/was a long discussion about the replacement for print in Python 3.0 (I don't know if this discussion is finished): http://mail.python.org/pipermail/python-dev/2005-September/055968.html ...
1
by: hamil | last post by:
I am trying to print a graphic file (tif) and also use the PrintPreview control, the PageSetup control, and the Print dialog control. The code attached is a concatination of two examples taken out...
1
by: Steff | last post by:
I am wandering if my code is making sense... I use a lot the print function. Is it weird in this case where I have to display an array ? I thought it would be better to have the entire array in php...
3
by: James J. Besemer | last post by:
I would like to champion a proposed enhancement to Python. I describe the basic idea below, in order to gage community interest. Right now, it's only an idea, and I'm sure there's room for...
69
by: Edward K Ream | last post by:
The pros and cons of making 'print' a function in Python 3.x are well discussed at: http://mail.python.org/pipermail/python-dev/2005-September/056154.html Alas, it appears that the effect of...
2
by: Brad Pears | last post by:
I have some sample code that uses the print dialog, print preview and a print direct options. If I select print preview and then click the printer icon from that, the document prints. If I...
7
by: samslists | last post by:
Am I the only one that thinks this would be useful? :) I'd really like to be able to use python 3.0's print statement in 2.x. Is this at least being considered as an option for 2.6? It seems...
12
by: Studiotyphoon | last post by:
Hi, I have report which I need to print 3 times, but would like to have the following headings Customer Copy - Print 1 Accounts Copy - Print 2 File Copy -Print 3 I created a macro to...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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
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...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

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.