473,666 Members | 2,575 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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.2344999999999 999
>>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 1669

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.2344999999999 999)

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.2344999999999 999)

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.2344999999999 999

>>>>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.2344999999999 999000000

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.23449 99999999999999, 6)
1.2344999999999 999

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.2344 999999999999999 , 6)

1.2344999999999 999

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
representabl e 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.2344999999999 999 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
2394
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
2904
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: $Revision: 0.0 $
0
1822
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 There is also a wiki page that collects the ideas: http://wiki.python.org/moin/PrintAsFunction There is the will to keep the printing operation very simple for the most common situation, but at the same time to make it flexible (optional no...
1
5704
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 of a Microsoft book, "Visual Basic,Net Step by Step" in Chapter 18. All but the bottom two subroutines will open a text file, and then allow me to use the above controls, example 1. The bottom two subroutines will print a graphic file, example...
1
2318
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 but now I am not sure if that makes sense. Can you tell me please ? <html> <head>
3
2105
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 improvement. And of course it's possible there's some serious "gotcha" I've overlooked. Thus I welcome any and all comments. If there's some agreement that this proposal is worth further consideration then I'll re-submit a formal document in...
69
3213
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 this pep would be to make it impossible to use the name 'print' in a backward compatible manner. Indeed, if a program is to compile in both Python 2.x and Python 3.x, the print function (or the print statement with parentheses) can not use the...
2
22113
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 select the print directly option, it also prints right away to the defauilt printer. However, if I use the printer dialog control to print and I click 'OK' to actually print the document - nothing happens. The job does not even go into the print...
7
10755
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 like it would be helpful with transitioning.
12
3530
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 print the report three times, but do not know how
0
8362
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8785
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8560
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
7389
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
4200
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4372
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2776
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
2012
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1778
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.