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

Hrounding error

P: n/a
Hi,
I am new to python. I was messing around in the interperator checking
out the floating point handling and I believe I may have found a
rounding bug:
>>234 - 23234.2345
-23000.234499999999
This is not correct by my calculations.

I am using python 2.5.2 in ubuntu 8.04. I am wondering if this is a
known bug, or if I am just not understanding some feature of python.
Thanks for the help!

-Cooper Quintin
http://www.bitsamurai.net
Jun 27 '08 #1
Share this Question
Share on Google+
5 Replies

P: n/a
On Jun 17, 10:47*pm, coop...@gmail.com wrote:
Hi,
I am new to python. *I was messing around in the interperator checking
out the floating point handling and I believe I may have found a
rounding bug:
>234 - 23234.2345

-23000.234499999999

This is not correct by my calculations.

I am using python 2.5.2 in ubuntu 8.04. *I am wondering if this is a
known bug, or if I am just not understanding some feature of python.
Thanks for the help!

-Cooper Quintinhttp://www.bitsamurai.net
It is a side-effect of binary floating-point.

http://www.python.org/doc/faq/genera...-so-inaccurate

casevh

Jun 27 '08 #2

P: n/a
co*****@gmail.com wrote:
Hi,
I am new to python. I was messing around in the interperator checking
out the floating point handling and I believe I may have found a
rounding bug:
>>>234 - 23234.2345
-23000.234499999999
This is not correct by my calculations.

I am using python 2.5.2 in ubuntu 8.04. I am wondering if this is a
known bug, or if I am just not understanding some feature of python.
Thanks for the help!

-Cooper Quintin
http://www.bitsamurai.net

If you need to round before you display the result, I find the following
works:
>>'%.4f' % (234 - 23234.2345)
'-23000.2345'

The %.4f means that the interpolated value will be treated as a float,
rounded to 4 digits after the decimal point.
Jun 27 '08 #3

P: n/a
On Wed, Jun 18, 2008 at 1:47 AM, <co*****@gmail.comwrote:
>>>234 - 23234.2345
-23000.234499999999

This is not correct by my calculations.
Python floating point operations use the underlying C floating point
libraries which, in turn, usually rely on the hardware's floating
point implementations. This Wikipedia article talks about how those
values are usually stored and manipulated:
http://en.wikipedia.org/wiki/IEEE_fl...point_standard

So, which IEEE double precision floating point value would you like
instead? As far as I understand it, these are your two choices:

'ba490c020f76d6c0' = -23000.234499999999
'bb490c020f76d6c0' = -23000.234500000002

Alternatively, investigate the Decimal module, but keep in mind that
it can have the same sorts of issues, just on different numbers.
Compare, for instance:
>>(1.0/3.0) * 3.0
1.0
>>from decimal import Decimal
( Decimal('1.0')/Decimal('3.0') ) * Decimal('3.0')
Decimal("0.9999999999999999999999999999")
>>>
--
Jerry
Jun 27 '08 #4

P: n/a
On Jun 18, 8:02*am, "Jerry Hill" <malaclyp...@gmail.comwrote:
On Wed, Jun 18, 2008 at 1:47 AM, *<coop...@gmail.comwrote:
>>234 - 23234.2345
-23000.234499999999
This is not correct by my calculations.

Python floating point operations use the underlying C floating point
libraries which, in turn, usually rely on the hardware's floating
point implementations. *This Wikipedia article talks about how those
values are usually stored and manipulated:http://en.wikipedia.org/wiki/IEEE_fl...point_standard

So, which IEEE double precision floating point value would you like
instead? *As far as I understand it, these are your two choices:

'ba490c020f76d6c0' = -23000.234499999999
'bb490c020f76d6c0' = -23000.234500000002

Alternatively, investigate the Decimal module, but keep in mind that
it can have the same sorts of issues, just on different numbers.
Compare, for instance:
>(1.0/3.0) * 3.0

1.0
>from decimal import Decimal
( Decimal('1.0')/Decimal('3.0') ) * Decimal('3.0')

Decimal("0.9999999999999999999999999999")

--
Jerry
So it seems then that python might not be very good for doing
precision floating point work, because there is a good chance its
floating points will be off by a (very small) amount? Or is there a
way to get around this and be guaranteed an accurate answer?
Jun 27 '08 #5

P: n/a
>
So it seems then that python might not be very good for doing
precision floating point work, because there is a good chance its
floating points will be off by a (very small) amount? *Or is there a
way to get around this and be guaranteed an accurate answer?- Hide quotedtext -
It's not a Python problem. That is just the behavior for floating-
point arithmetic.

casevh

Jun 27 '08 #6

This discussion thread is closed

Replies have been disabled for this discussion.