我想知道为什么这两种情况的结果有所不同。
工作存储:
WS-SUM-LEN PIC S9(4) COMP.
WS-LEN-9000 PIC 9(5) VALUE 9000.
WS-TMP-LEN PIC 9(5).
WS-FIELD-A PIC X(2000).
案例1)COMPUTE WS-SUM-LEN = WS-LEN-9000 + LENGTH OF WS-FIELD-A
结果:WS-SUM-LEN = 1000
案例2)
MOVE LENGTH OF WS-FIELD-A TO WS-TMP-LEN
COMPUTE WS-SUM-LEN = WS-LEN-9000 + WS-TMP-LEN
结果:WS-SUM-LEN = 11000
编译器选项是TRUNC(OPT)。为什么案例2没有发生截断?
答案 0 :(得分:1)
来自公开文件:
TRUNC(OPT) is a performance option. When TRUNC(OPT) is in effect, the compiler assumes that data conforms to PICTURE specifications in USAGE BINARY receiving fields in MOVE statements and arithmetic expressions. The results are manipulated in the most optimal way, either truncating to the number of digits in the PICTURE clause, or to the size of the binary field in storage (halfword, fullword, or doubleword).
Tip: Use the TRUNC(OPT) option only if you are sure that the data being moved into the binary areas will not have a value with larger precision than that defined by the PICTURE clause for the binary item. Otherwise, unpredictable results could occur. This truncation is performed in the most efficient manner possible; therefore, the results are dependent on the particular code sequence generated. It is not possible to predict the truncation without seeing the code sequence generated for a particular statement.
仔细阅读“提示”并了解它对您的情况意味着什么。 (提示:这意味着问你做的问题是没有意义的,因为它字面上说“无论发生什么,它都是不可预测的”或者说“没有解释发生的事情”)。
要使编译器行为可预测,请切换到TRUNC(BIN)或TRUNC(STD)。 STD有利于符合标准但对CPU使用率有害,BIN有利于CPU使用,但需要您小心一点(因为十进制截断不会发生)。