gpt4 book ai didi

cobol - MULTIPLY 的行为与我预期的不同

转载 作者:行者123 更新时间:2023-12-02 06:48:38 27 4
gpt4 key购买 nike

我有这个 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.

关于cobol - MULTIPLY 的行为与我预期的不同,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50606837/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com