Page 1 of 1

Reading floating-point numbers from file in COBOL

PostPosted: Fri Jul 29, 2016 2:08 am
by Quentin
I have floating-point numbers in file, one in each line, in this format S9(6)V9(2) but when they are actually read, I'm getting "non numeric" errors while trying to put them in math operations. What is more, when I try to display them in program, a number that is written in file as 567123.45 is saved in variable as +567123.04. And for example the number from file 123.45 is saved in variable as +123.45.00 and it provokes the following error 'WS-VALUE' not numeric: '123.45 0' during a math operation. Why is that?
I'm using OpenCobolIDE 4.7.4 for Windows.

Re: Reading floating-point numbers from file in COBOL

PostPosted: Fri Jul 29, 2016 2:49 am
by Akatsukami
As this is a mainframe help board, you are unlikely to get a definitive answer here (even if you had shown code, representative data, etc.) One thing I will note, however, is that even on a toy computer, those are not floating-point numbers.

Re: Reading floating-point numbers from file in COBOL

PostPosted: Fri Jul 29, 2016 2:56 am
by Quentin
Yes, sorry, those are fixed-point numbers.

File has records of the following form separated by new lines (read by READ operation record after record):

01 WS-OPERATION.
    05 WS-ID PIC A(2).
    05 WS-CLIENT PIC 9(5).
    05 WS-COUNTRY PIC A(4).
    05 WS-VALUE PIC S9(6)V9(2).

Re: Reading floating-point numbers from file in COBOL

PostPosted: Fri Jul 29, 2016 2:59 am
by Robert Sample
I see several issues:
1. "Floating point" is a very specific type of data and you apparently have no clue what it is. With COBOL, you declare floating point as COMP-1 or COMP-2 -- nothing else. Using a PICTURE clause means you do NOT have floating point values.
2. If your data is in a file as 567123.45 and you declared that is PIC 9(6)V9(2) then yes of course your data will be 567123.04 -- apparently you do not know what a V in a PICTURE means, either.
3. Is your 123.45 value stored left-justified with trailing blanks? If so, then your PIC does not match the data (which is also part of point 2) and hence the results you got are normal and expected.

A floating point value would look like +5.671345E+05 (or some variation thereof). If your version of COBOL supports intrinsic functions, you may want to investigate the NUMVAL function. In any case, I STRONGLY recommend you spend a lot of time reading the COBOL user's guide for your version of COBOL.