473,587 Members | 2,451 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

problem: sum is incorrect (e-10)

DM
Hi all,
I've a problem.

The query select mo_qta from oobjm.movoptitt raded where mo_opzione =
'1,253CEUUS16M0 8' gives:
MO_QTA
------------------------
+2.503477100000 00E+005
+2.503477100000 00E+005
+2.503477100000 00E+005
+3.337969400000 00E+005
+1.001390820000 00E+006
-2.5034771000000 0E+005
-2.5034771000000 0E+005
-2.5034771000000 0E+005
-3.3379694000000 0E+005
-1.0013908200000 0E+006

and the query select sum(mo_qta) from oobjm.movoptitt raded where
mo_opzione = '1,253CEUUS16M0 8' gives:
1
------------------------
+3.492459654808 04E-010

The field is a double.

I think is impossible...
How can i find where the problem is?

Thanks all,
DM
Jun 27 '08 #1
3 2811
DM wrote:
Hi all,
I've a problem.

The query select mo_qta from oobjm.movoptitt raded where mo_opzione =
'1,253CEUUS16M0 8' gives:
MO_QTA
------------------------
+2.503477100000 00E+005
+2.503477100000 00E+005
+2.503477100000 00E+005
+3.337969400000 00E+005
+1.001390820000 00E+006
-2.5034771000000 0E+005
-2.5034771000000 0E+005
-2.5034771000000 0E+005
-3.3379694000000 0E+005
-1.0013908200000 0E+006

and the query select sum(mo_qta) from oobjm.movoptitt raded where
mo_opzione = '1,253CEUUS16M0 8' gives:
1
------------------------
+3.492459654808 04E-010

The field is a double.

I think is impossible...
How can i find where the problem is?
Your problem is in binary floating point arithmetic.
You can either use DECIMAL to do exact math, or in DB2 9.5 you can use
DECFLOAT.

Cheers
Serge
--
Serge Rielau
DB2 Solutions Development
IBM Toronto Lab
Jun 27 '08 #2
DM wrote:
Hi all,
I've a problem.

The query select mo_qta from oobjm.movoptitt raded where mo_opzione =
'1,253CEUUS16M0 8' gives:
MO_QTA
------------------------
+2.503477100000 00E+005
+2.503477100000 00E+005
+2.503477100000 00E+005
+3.337969400000 00E+005
+1.001390820000 00E+006
-2.5034771000000 0E+005
-2.5034771000000 0E+005
-2.5034771000000 0E+005
-3.3379694000000 0E+005
-1.0013908200000 0E+006

and the query select sum(mo_qta) from oobjm.movoptitt raded where
mo_opzione = '1,253CEUUS16M0 8' gives:
1
------------------------
+3.492459654808 04E-010

The field is a double.

I think is impossible...
How can i find where the problem is?
Ah, the classic floating point rounding issue. The answer is "correct"
in that DB2 doesn't "lie by rounding". Try the following under Python
(another thing which doesn't lie about floating point results by
rounding):

Python 2.5.1 (r251:54863, Oct 5 2007, 13:50:07)
[GCC 4.1.3 20070929 (prerelease) (Ubuntu 4.1.2-16ubuntu2)] on linux2
Type "help", "copyright" , "credits" or "license" for more information.
>>(+2.503477100 00000E+005
.... +2.503477100000 00E+005
.... +2.503477100000 00E+005
.... +3.337969400000 00E+005
.... +1.001390820000 00E+006
.... -2.5034771000000 0E+005
.... -2.5034771000000 0E+005
.... -2.5034771000000 0E+005
.... -3.3379694000000 0E+005
.... -1.0013908200000 0E+006)
3.4924596548080 444e-10

The problem is that floating point can't accurately represent these
values. For example, again under Python:
>>+2.5034771000 0000E+005
250347.70999999 999

Many tools round off the last significant digit to "correct" the
answer, but DB2's CLP doesn't - it's "brutally honest" about the value
of floating point numbers. The best solution (provided the range of
numbers you're dealing will fit) is to use DECIMAL instead (e.g.
DECIMAL(10, 2) looks like it'd be sufficient for the values above).

See "Representa ble numbers, conversion and rounding" under
http://en.wikipedia.org/wiki/Floating_point for more information.
Cheers,

Dave.
Jun 27 '08 #3
DM
On 19 Mag, 18:20, "Dave Hughes" <d...@waveform. plus.comwrote:
DM wrote:
Hi all,
I've a problem.
The query select mo_qta from oobjm.movoptitt raded where mo_opzione =
'1,253CEUUS16M0 8' gives:
MO_QTA
------------------------
+2.503477100000 00E+005
+2.503477100000 00E+005
+2.503477100000 00E+005
+3.337969400000 00E+005
+1.001390820000 00E+006
-2.5034771000000 0E+005
-2.5034771000000 0E+005
-2.5034771000000 0E+005
-3.3379694000000 0E+005
-1.0013908200000 0E+006
and the query select sum(mo_qta) from oobjm.movoptitt raded where
mo_opzione = '1,253CEUUS16M0 8' gives:
1
------------------------
+3.492459654808 04E-010
The field is a double.
I think is impossible...
How can i find where the problem is?

Ah, the classic floating point rounding issue. The answer is "correct"
in that DB2 doesn't "lie by rounding". Try the following under Python
(another thing which doesn't lie about floating point results by
rounding):

Python 2.5.1 (r251:54863, Oct 5 2007, 13:50:07)
[GCC 4.1.3 20070929 (prerelease) (Ubuntu 4.1.2-16ubuntu2)] on linux2
Type "help", "copyright" , "credits" or "license" for more information.>>( +2.503477100000 00E+005

... +2.503477100000 00E+005
... +2.503477100000 00E+005
... +3.337969400000 00E+005
... +1.001390820000 00E+006
... -2.5034771000000 0E+005
... -2.5034771000000 0E+005
... -2.5034771000000 0E+005
... -3.3379694000000 0E+005
... -1.0013908200000 0E+006)
3.4924596548080 444e-10

The problem is that floating point can't accurately represent these
values. For example, again under Python:
>+2.50347710000 000E+005

250347.70999999 999

Many tools round off the last significant digit to "correct" the
answer, but DB2's CLP doesn't - it's "brutally honest" about the value
of floating point numbers. The best solution (provided the range of
numbers you're dealing will fit) is to use DECIMAL instead (e.g.
DECIMAL(10, 2) looks like it'd be sufficient for the values above).

See "Representa ble numbers, conversion and rounding" underhttp://en.wikipedia.or g/wiki/Floating_pointf or more information.

Cheers,

Dave.
Thanks a lot.
We cannot change the db definition right now but we solved the problem
adding a having clause at the end of the query (we need to know for
wich "opzione" this sum is <0, now we use having abs(sum(mo_qta) ) >
0.0001).

Thanks.

DM
Jun 27 '08 #4

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

Similar topics

2
5926
by: Michael Jordan | last post by:
I'm hoping that someone here can give me some insight into a problem I'm running into with Python, pywin32 and Excel. All-in-all using Python and pywin32 is great but I've run into a strange problem with the range Offset property, I'm not getting the correct offset and the returned range is a single cell and not the same size as the original...
1
4567
by: bdwise | last post by:
I have this in my style: #divNavigation { position: relative; overflow: hidden; align: right; vertical-align: top; text-align: center; width: 126;
117
7151
by: Peter Olcott | last post by:
www.halting-problem.com
31
1921
by: Peter Olcott | last post by:
The Halting Problem can not be solved within the degree of expressability of a TM. My solution only worked because of its more limited degree of expressability. There is no such thing as a void function in a TM, thus there is no way to make constructing the counter-example program impossible for a TM.
18
6154
by: Ian Stanley | last post by:
Hi, Continuing my strcat segmentation fault posting- I have a problem which occurs when appending two sting literals using strcat. I have tried to fix it by writing my own function that does the strcat (mystract). Program below. However this appears not to have fixed the problem and I don't know why it shouldn't ? Any further help as to...
6
3793
by: Ammar | last post by:
Dear All, I'm facing a small problem. I have a portal web site, that contains articles, for each article, the end user can send a comment about the article. The problem is: I the comment length is more that 1249 bytes, then the progress bar of the browser will move too slow and then displaying that the page not found!!!! If the message is...
3
3335
by: murphy | last post by:
Hi, I've been seeing two symptoms with my asp.net site that have started recently after a long period of smooth running. As others on our team make changes to referenced dll's I find that I get the following errors from time to time. Apparently the following procedure alleviates the problems:
29
1875
by: ApeX | last post by:
Hi guys, i hace a question i have a datagrid col1 col2 ----------------- D text0 text1 text2 D text3
5
1386
by: Alexnb | last post by:
I am not sure what is going on here. Here is the code that is being run: def getWords(self): self.n=0 for entry in self.listBuffer: self.wordList = entry.get() self.n=self.n+1 print self.wordList
10
1368
by: grego9 | last post by:
I have written the following function to total up the values in a column where the values in columns 5 and 6 of a range I have created (called "lori") are equal to the values on another summary tab (details lower down) Function StCashTotalGBP(Managed, Entity) Counter = 1: StCashTotalGBP = 0 Do While Counter <= Range("lori").Rows.Count ...
0
7920
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7849
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...
0
8347
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...
1
7973
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...
0
8220
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
1
5718
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
5394
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3844
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...
0
3879
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?

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.