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

Division error on float data type in DB2

I'm running DB2 v7 for z/OS. When I use SPUFI, SELECT CAST(6.0 AS
FLOAT)/CAST(10.0 AS FLOAT) FROM SYSIBM.SYSDUMMY1 returns
0.6000000000000000E+00. When I use DSNTIAUL,DSNTEP2, or DSNALI (call
attach facility), the same statement returns 0.59999999999999999E 00.
The only reason I$B!G(Bve heard to explain this behavior is that float
stores too much precision, but I$B!G(Bve used double-precision floating-
point data types in SQL Server$B!G(Bs Transact-SQL, COBOL, VB.NET and I
haven$B!G(Bt seen this odd behavior. DB2$B!G(Bs SQL is the only language I know
of where 6/10 $B!b(B 0.6.

My particular problem is that my COBOL programs are getting inacurate
results (6/10 $B!b(B 10) when using CAF to divide the values of some FLOAT
columns on a DB2 table. The only two work-arounds I can think of are
a) Don$B!G(Bt use SQL to do divide the column values
b) Round the resulting value

I$B!G(Bm not keen on either band-aid, but I don$B!G(Bt see any DSNALI call
parameters that could solve this problem. Can anyone help?

P.S. Changing the data type is not an option for us. We need to store
a wide variety of precisions in these columns.

Thanks in advance,
Mike
Feb 11 '08 #1
2 5321
Mike wrote:
I'm running DB2 v7 for z/OS. When I use SPUFI, SELECT CAST(6.0 AS
FLOAT)/CAST(10.0 AS FLOAT) FROM SYSIBM.SYSDUMMY1 returns
0.6000000000000000E+00. When I use DSNTIAUL,DSNTEP2, or DSNALI (call
attach facility), the same statement returns 0.59999999999999999E 00.
The only reason I've heard to explain this behavior is that
float stores too much precision, but I've used
double-precision floating- point data types in SQL Server's
Transact-SQL, COBOL, VB.NET and I haven't seen this odd
behavior. DB2's SQL is the only language I know of where 6/10
!= 0.6.
This isn't an error - it's just DB2 being "brutally honest" about the
result of the calculation. It's impossible to accurately represent the
decimal value 0.6 in binary floating point (0.5999999... is the closest
that can be achieved). It looks like SPUFI is performing a common trick
of rounding off the last digit for display purposes, whereas CAF isn't
bothering and is simply displaying the unrounded result (note the extra
digit in the CAF result):

SPUFI: 0.6000000000000000E+00
CAF: 0.59999999999999999E 00

You can see the same thing in Python on an ordinary PC, which is also
"brutally honest" about the results of floating point calculations
(i.e. doesn't perform any rounding on the result):

Python 2.5 (r25:51908, Sep 19 2006, 09:52:17) [MSC v.1310 32 bit
(Intel)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>6.0 / 10.0
0.59999999999999998

All the other environments you mention (SQL Server, COBOL, VB.NET) will
be performing rounding for display purposes, but internally they'll be
getting 0.599999... as the result of the calculation (if they're using
floating point and not something else).
My particular problem is that my COBOL programs are getting inacurate
results (6/10 ? 10) when using CAF to divide the values of
some FLOAT columns on a DB2 table. The only two work-arounds I can
think of are a) Don't use SQL to do divide the column values
b) Round the resulting value
If CAF isn't doing the rounding step, then your best option is to do it
yourself.

You can find more information on binary floating point representation
here:

http://en.wikipedia.org/wiki/Floating_point
(see the "Value", "Conversion and rounding" and "Accuracy Problems"
sections)

http://en.wikipedia.org/wiki/Binary_numeral_system
(see the "Representing Real Numbers" section)

http://en.wikipedia.org/wiki/IBM_Flo...t_Architecture
(I've no idea if SPUFI or CAF are using standard IEEE754 floating point
values, or the IBM floating point representation - but you'll encounter
such issues in either)
Cheers,

Dave.
Feb 12 '08 #2
Mike wrote:
I'm running DB2 v7 for z/OS. When I use SPUFI, SELECT CAST(6.0 AS
FLOAT)/CAST(10.0 AS FLOAT) FROM SYSIBM.SYSDUMMY1 returns
0.6000000000000000E+00. When I use DSNTIAUL,DSNTEP2, or DSNALI (call
attach facility), the same statement returns 0.59999999999999999E 00.
The only reason I’ve heard to explain this behavior is that float
stores too much precision, but I’ve used double-precision floating-
point data types in SQL Server’s Transact-SQL, COBOL, VB.NET and I
haven’t seen this odd behavior. DB2’s SQL is the only language I know
of where 6/10 â‰* 0.6.
As Dave pointet out, DB2's SQL behaves perfectly normal and the same
as all other programming languages.

P.S. Changing the data type is not an option for us. We need to store
a wide variety of precisions in these columns.
Actually, with the float data type it is not possible to store "a wide
variety of precisions". Floats have a fixed precision. You should read
a bit about this topic, especially when it comes to financial applications
it tends to be somewhat important...
Feb 12 '08 #3

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

Similar topics

6
by: Artemisio | last post by:
I am a non programmer who just started with Python. So far I love it. I would appreciate if you could help me fix this error I get taking this exercise: count= 0 sum= 0.0 number= 1 print...
15
by: joel | last post by:
I have a table which I want to update by dividing one field into another. The update runs with no errors, but the results come out as only a positive integer number. The datatype for the result...
9
by: Darius Fatakia | last post by:
hi, i'm new to MS Visual Studio and C++ but not to C programming in general. i'm trying to divide two integers and get their actual quotient (eg 5/3 = 1.666667 etc). i thought i had type cast...
4
by: yanyo | last post by:
hi, im trying to figure out whats the problem with this program i get a runtime error but i dont see where the problem is i tried changing declaration but nothing if somrbody can try this on their...
8
by: Dave Brown | last post by:
Hi all, I'm having trouble understanding which data type to use to get results I expect. using float, decimal, and double I have tried the following, .. float test = 55/60; I always get 0...
6
by: J.Marsch | last post by:
Suppose that I have an integer division problem, but I want the answer was a float. What's the cleanest syntax for that? Example: In this code, the variable z ends up == 0. And that is...
17
by: seb.haase | last post by:
Hi, Is it true that that "Python 3000" is dead ? Honestly I think that e.g. changing 5/2 to be 2.5 (instead of 2) would just break to much code :-( On the otherhand I'm using Python as "Matlab...
9
by: PengYu.UT | last post by:
Hi, The usually integer division will round the result to the biggest integet smaller than the float version division.For example, 10/3 = 3. I'm wondering if there is any easy way to round it...
10
by: Artemio | last post by:
Hello all! I just stumbled across a weird problem with precision of a division operation. I am on Mac OS X, GCC 4.0.1. Say I have two float or double numbers, and I want to divide one by...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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...

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.