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

Hrounding error

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
5 833
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
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
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
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
>
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: AIM | last post by:
Error in msvc in building inheritance.obj to build hello.pyd Hello, I am trying to build the boost 1.31.0 sample extension hello.cpp. I can not compile the file inheritance.cpp because the two...
5
by: Tony Wright | last post by:
Hi, I am having a problem installing an msi for a web site. The error message I am getting is: "The specified path 'http://mipdev05/features/Fas2' is unavailable. The Internet Information...
13
by: deko | last post by:
I use this convention frequently: Exit_Here: Exit Sub HandleErr: Select Case Err.Number Case 3163 Resume Next Case 3376 Resume Next
7
by: p | last post by:
WE had a Crystal 8 WebApp using vs 2002 which we upgraded to VS2003. I also have Crystal 9 pro on my development machine. The web app runs fine on my dev machine but am having problems deploying....
2
by: f rom | last post by:
----- Forwarded Message ---- From: Josiah Carlson <jcarlson@uci.edu> To: f rom <etaoinbe@yahoo.com>; wxpython-users@lists.wxwidgets.org Sent: Monday, December 4, 2006 10:03:28 PM Subject: Re: ...
2
hyperpau
by: hyperpau | last post by:
Before anything else, I am not a very technical expert when it comes to VBA coding. I learned most of what I know by the excellent Access/VBA forum from bytes.com (formerly thescripts.com). Ergo, I...
0
hyperpau
by: hyperpau | last post by:
Before anything else, I am not a very technical expert when it comes to VBA coding. I learned most of what I know by the excellent Access/VBA forum from bytes.com (formerly thescripts.com). Ergo, I...
5
by: yashu0209 | last post by:
I have an error occurring when i try to compile the program and the error is: D:\Program Files\visual studio 6.0\MSDev98\MyProjects\GLEnabledView\GLNew.cpp(164) : error C2653: 'CGLEnabledView' :...
2
by: randa zaghdan | last post by:
Hi I have a problem with my program in vc 2008 When I compile it, the following errors are listed. I try to resolve it but no hope Does any one can help me ? thanks . Linking......
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: 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
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: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
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...
0
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,...

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.