By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
428,997 Members | 1,657 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 428,997 IT Pros & Developers. It's quick & easy.

a print bug?

P: n/a
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
Share this Question
Share on Google+
9 Replies


P: n/a

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

P: n/a

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

P: n/a
>>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

P: n/a
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

P: n/a
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

P: n/a

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

P: n/a
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

P: n/a
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

P: n/a
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 discussion thread is closed

Replies have been disabled for this discussion.