MULTIPLY 的行为与我预期的不同

MULTIPLY doesn't behave as I would expect

我有这个 cobol 程序,用于计算阶乘:

   IDENTIFICATION DIVISION.
   PROGRAM-ID. Factorial-hopefully.
   AUTHOR. Darth Egregious.
   DATA DIVISION.
   WORKING-STORAGE SECTION.
   01 Keeping-Track-Variables.
      05 Operand                      PIC S99   VALUE 0.
      05 Product                      PIC S99   VALUE 1.
   PROCEDURE DIVISION.
   PERFORM-FACTORIAL.
    DISPLAY SPACES
    PERFORM VARYING Operand FROM 6 BY -1 UNTIL Operand = 0

      DISPLAY "Before Product " Product " Operand " Operand
      MULTIPLY Product By Operand GIVING Product
      DISPLAY "After Product " Product " Operand " Operand
    END-PERFORM
    DISPLAY Product.
   STOP RUN.

我运行是这样的:

cobc -free -x -o a.out fact.cbl && ./a.out

我得到了这个奇怪的输出:

Before Product +01 Operand +06
After Product +06 Operand +06
Before Product +06 Operand +05
After Product +30 Operand +05
Before Product +30 Operand +04
After Product +30 Operand +04
Before Product +30 Operand +03
After Product +90 Operand +03
Before Product +90 Operand +02
After Product +90 Operand +02
Before Product +90 Operand +01
After Product +90 Operand +01
+90

我的递减循环按预期工作,但 MULTIPLY 命令运行异常。它正确地执行 1*66*5,但是 30*4 似乎不起作用,然后 30*3 起作用,最后 90*2 又不起作用。 COBOL 不喜欢乘以 2 的幂之类的东西吗?

My decrementing loop is working as expected, but the MULTIPLY command is behaving strangely. It's doing 1*6, and 6*5 correctly, but 30*4 doesn't seem to work, then 30*3 does, and finally 90*2 doesn't work again. Does COBOL not like multiplying by powers of two or something?

  05 Operand                      PIC S99   VALUE 0.
  05 Product                      PIC S99   VALUE 1.

当您将 30*490*2 相乘时,值大于 PICTURE 子句,S99

PIC 子句的大小增加到 S999


回复评论:

从技术上讲,结果是未定义的 [COBOL 85],因此什么都不做是一个有效的选择。其他实现将截断给出不同结果的值。

与其说是语言,不如说是实现。

该语言还允许 SIZE ERROR 短语捕获截断错误。在那种情况下,结果不会改变,但可能会执行其他代码以指示发生了错误。

对于 COBOL 2002,如果未指定 ON SIZE ERROR 短语并且未激活 EC-SIZE-TRUNCATION 异常检查,则结果由实现者定义。


引自 2002 标准:

F.1 Substantive changes potentially affecting existing programs

15) Size error condition with no SIZE ERROR phrase. If a size error condition occurs, the statement in which it occurs contains no SIZE ERROR or NOT SIZE ERROR phrase, and there is no associated declarative, the implementor defines whether the run unit is terminated or execution continues with incorrect values.

Justification:

In the previous COBOL standard, the rules for size error stated that execution would continue with undefined values, but it was not clear where execution would continue, particularly in conditional statements. Additionally, continued execution with incorrect results was not acceptable for many critical applications, where it might cause corruption of databases, incorrect continued execution of the program, and potentially a multitude of additional errors. It was prohibitive to modify programs to add ON SIZE ERROR for every affected statement. Responding to user requirements, several implementors terminated execution of the program in this situation; in some cases, the implementor allowed selection of termination based on a compiler directive.

The number and criticality of applications that terminated in this situation provides strong justification for this change. It is expected that this change will have little impact on existing programs because implementors are free to continue or terminate, in accordance with their implementation of the previous COBOL standard.