IBM Mainframe Forum Index
 
Log In
 
IBM Mainframe Forum Index Mainframe: Search IBM Mainframe Forum: FAQ Register
 

Moving float to comp-2


IBM Mainframe Forums -> COBOL Programming
Post new topic   Reply to topic
View previous topic :: View next topic  
Author Message
bhairon singh rathore

New User


Joined: 19 Jun 2008
Posts: 91
Location: banglore

PostPosted: Fri Jan 09, 2009 10:05 pm
Reply with quote

Hi all,

My table has a col defined as float and having value 0.1.
After fetching from the table i am moving this value to a comp-2 variable in my program. the comp-2 variable is part of an array.

But here 0.1 is getting converted to 0.10000000000000001 ... the 17th position is having a 1, due to which computations are coming wrong.

Can anyone help ?

Thanks,
Bhairon
Back to top
View user's profile Send private message
CICS Guy

Senior Member


Joined: 18 Jul 2007
Posts: 2146
Location: At my coffee table

PostPosted: Fri Jan 09, 2009 10:15 pm
Reply with quote

Why comp-2? floating point will almost always have a small difference in the least significant digits. Why not half round and move to a comp-3 field?
Back to top
View user's profile Send private message
Robert Sample

Global Moderator


Joined: 06 Jun 2008
Posts: 8696
Location: Dubuque, Iowa, USA

PostPosted: Sat Jan 10, 2009 1:25 am
Reply with quote

Ah, the days of numerical analysis in college ... long ago yet still relevant. The decimal value 1/10 or .1 cannot be exactly represented as a floating point binary number. The computer generates the best approximation it can but the number's not going to be exact no matter what. Any time floating point is used, representation errors occur and need to be dealt with.

You can convert the value to a fixed number as CICS guy suggested (if possible -- not all floating point values can be so represented) and work with the fixed value.

If you cannot convert the value, then either rounding or truncation will be required in your coding to adjust values for the inherent imprecision of floating point.

The classic test is to use this code:
Code:
           05  I                       PIC 9(02) VALUE 1.
           05  INCR-LOW-TO-HIGH        USAGE COMP-2 .
           05  INCR-HIGH-TO-LOW        USAGE COMP-2 .
           05  MULT-LOW-TO-HIGH        USAGE COMP-2 .
           05  MULT-HIGH-TO-LOW        USAGE COMP-2 .

       LINKAGE SECTION.
      /
       PROCEDURE DIVISION.
       S1000-MAIN       SECTION.
           MOVE 1                      TO  INCR-LOW-TO-HIGH
                                           MULT-LOW-TO-HIGH
                                           MULT-HIGH-TO-LOW.
           MOVE 30                     TO  INCR-HIGH-TO-LOW.
           PERFORM
               VARYING I FROM 1 BY 1
                   UNTIL I > 30
               COMPUTE MULT-LOW-TO-HIGH = MULT-LOW-TO-HIGH *
                                         INCR-LOW-TO-HIGH
               ADD 1                   TO  INCR-LOW-TO-HIGH
               COMPUTE MULT-HIGH-TO-LOW = MULT-HIGH-TO-LOW *
                                         INCR-HIGH-TO-LOW
               SUBTRACT 1              FROM INCR-HIGH-TO-LOW
           END-PERFORM.
           DISPLAY 'MULT LOW TO HIGH    ' MULT-LOW-TO-HIGH.
           DISPLAY 'MULT HIGH TO LOW    ' MULT-HIGH-TO-LOW.
which produces this output:
Code:
 MULT LOW TO HIGH     .26525285981219099E 33
 MULT HIGH TO LOW     .26525285981219102E 33
which shows that 1 times 2 times ... 29 times 30 is not the same as 30 times 29 times ... 2 times 1 on a computer.
Back to top
View user's profile Send private message
dick scherrer

Moderator Emeritus


Joined: 23 Nov 2006
Posts: 19244
Location: Inside the Matrix

PostPosted: Sat Jan 10, 2009 1:28 am
Reply with quote

Hello,

What is the content of the field? If it is a quantity or an amount, it should probably not be defined as float. . .
Back to top
View user's profile Send private message
View previous topic :: :: View next topic  
Post new topic   Reply to topic View Bookmarks
All times are GMT + 6 Hours
Forum Index -> COBOL Programming

 


Similar Topics
Topic Forum Replies
No new posts COBOL - Move S9(11)v9(7) COMP-3 to -(... COBOL Programming 5
No new posts Moving Or setting POINTER to another ... COBOL Programming 2
No new posts Converting ASCII values to COMP-3 (ZD... JCL & VSAM 2
No new posts Interviewers are surprised with my an... Mainframe Interview Questions 6
No new posts Cobol COMP-2 fields getting scrambled... Java & MQSeries 6
Search our Forums:

Back to Top