问题
I have this very simple dummy COBOL program which does a dummy COMPUTE and displays the result.
ID DIVISION.
PROGRAM-ID. DUMMYPGM.
DATA DIVISION.
WORKING-STORAGE SECTION.
01 NUM-A PIC 9(3) VALUE 399.
01 NUM-B PIC 9(3) VALUE 211.
01 NUM-C PIC 9(3).
*
PROCEDURE DIVISION.
MAIN.
COMPUTE NUM-C = ((NUM-A / 100) - (NUM-B / 100)) * 100
DISPLAY 'NUM-C IS ' NUM-C
STOP RUN.
When I compile this code on a mainframe (with compiler MVS Enterprise COBOL V4.2) and execute it, I get "NUM-C IS 100", probably because (399 / 100) is treated as a 3 instead of a 3.99 in the calculation (and same goes for 211 / 100).
But when I compile the same exact code on a PC (with the GnuCobol compiler) and execute it, I get "NUM-C IS 188". The PC's answer is correct , but I would like to make it behave like the mainframe (and thus, losing precision in that compute statement to give 100 instead of 188)... How would I do that ?
The reason for the above is a general expression of this code:
COMPUTE PDISCR = (((((X(1) + DX - XBRAK) * (ABRAK(1) / 1000)) / 100)
+ PHT(1) + DPH - PHBRAK) * 2) + ((V(1) + DV
+ VBRAKMPM) * (V(1) + DV - VBRAKMPM) / 100000))
This is part of a 50-year-old Train simulation program, which I need to migrate to GnuCOBOL. All the fields used in the COMPUTE are integers. I need to be able to get the same answer from GnuCOBOL.
Confirmed for OpenCOBOL/GnuCOBOL up to 2.0.
回答1:
https://lwn.net/Articles/733129/
This link mentions a new std option for gnuCobol 2.2: ibm-strict. I wonder if that would force the compute statement to do what you want.
回答2:
Since it looks like the IBM truncates your values and GnuCobol does not, you may want to make use of GnuCobol functions to emulate the way IBM does it.
For positive integers at least, that's probably as simple as:
COMPUTE NUM-C = ((INTEGER(NUM-A / 100)) - (INTEGER(NUM-B / 100))) * 100
I haven't tested whether this works for negative integers since (1) the doco seems to indicate it rounds toward negative infinity rather than zero; (2) there doesn't appear to be a TRUNCATE
function; and (3) I assumed you didn't care since you're data types aren't signed anyway.
If you do need to handle negatives, there are mathematical methods that will do it for you, I'd suggest asking it in a different question.
来源:https://stackoverflow.com/questions/39854413/cobol-differing-answer-from-mainframe-to-pc-for-same-compute