我对cobol中的计算函数的舍入感到困惑.
宣言:
VAR-A PIC S9(9)V99 COMP-3.
VAR-B PIC S9(9)V9(6) COMP-3.
Run Code Online (Sandbox Code Playgroud)
程序.
MOVE +12.08 TO VAR-A.
MOVE +6.181657 TO VAR-B.
COMPUTE VAR-A ROUNDED = VAR-A + VAR-B.
Run Code Online (Sandbox Code Playgroud)
VAR-A的结果是18.27还是18.26?cobol会对计算做什么?它会将VAR-B首先舍入到VAR-A中指定的小数位,还是将cobol添加2个变量然后将它们四舍五入到VAR-A中指定的小数位?
任何帮助将不胜感激.
@NealB,
这个例子怎么样:
宣言:
01 VAR-ARRAY OCCURS 22 TIMES.
03 VAR-A PIC S9(9)V9(6) COMP-3.
01 VAR-B PIC S9(9)V99 COMP-3.
Run Code Online (Sandbox Code Playgroud)
假设VAR-A是一个数组,其值如下:
VAR-A(01) = 123.164612
VAR-A(02) = 12.07865
VAR-A(03) = 6.181657
VAR-A(04) = 1.744353
VAR-A(05) = 6.118182
VAR-A(06) = 1.744353
VAR-A(07) = 6.158715
VAR-A(08) = 1.744353
VAR-A(09) = 6.194759
VAR-A(10) = 1.744353
VAR-A(11) = 3.037896
VAR-A(12) = 1.743852
VAR-A(13) = 6.14653
VAR-A(14) = 1.744353
VAR-A(15) = 0.000377
VAR-A(16) = 1.743852
VAR-A(17) = 6.144363
VAR-A(18) = 1.743852
VAR-A(19) = 0.007649
VAR-A(20) = 1.744353
VAR-A(21) = 0.000377
VAR-A(22) = 1.744353
Run Code Online (Sandbox Code Playgroud)
VAR-B的价值是:
VAR-B = 405.25
Run Code Online (Sandbox Code Playgroud)
程序:
PERFORM VAR-IDX FROM 1 BY 1 UNTIL VAR-IDX > 22
COMPUTE VAR-B ROUNDED = VAR-B + VAR-A(VAR-IDX)
END-PERFORM.
Run Code Online (Sandbox Code Playgroud)
为什么计算后得到VAR-B的597.87?
我相信默认的COBOL舍入行为是:最接近于零.
COMPUTE VAR-A ROUNDED = VAR-A + VAR-B
Run Code Online (Sandbox Code Playgroud)
应该导致VAR-A包含18.26
在计算表达式后进行舍入.一个更有趣的例子可能是:
01 VAR-A PIC S9(9)V99 COMP-3.
01 VAR-B PIC S9(9)V9(6) COMP-3.
01 VAR-C PIC S9(9)V9(6) COMP-3.
MOVE +12.08 TO VAR-A.
MOVE +06.182000 TO VAR-B.
MOVE +00.004000 TO VAR-C.
COMPUTE VAR-A ROUNDED = VAR-A + VAR-B + VAR-C.
Run Code Online (Sandbox Code Playgroud)
结果是18.27.四舍五入VAR-B并VAR-C在做加法前2位小数会产生18.26,因为VAR-B轮6.18和VAR-C轮0.00.结果实际上是18.27,因此在评估表达式后进行舍入.
回复已编辑的问题
不是很好的输出,但这是我的计算使用IBM Enterprise COBOL for z/OS的方式
VAR-IDX = 01 VAR-B = +405.25 VAR-A = +123.164612 VAR-B + VAR-A = +528.41 VAR-IDX = 02 VAR-B = +528.41 VAR-A = +012.078650 VAR-B + VAR-A = +540.49 VAR-IDX = 03 VAR-B = +540.49 VAR-A = +006.181657 VAR-B + VAR-A = +546.67 VAR-IDX = 04 VAR-B = +546.67 VAR-A = +001.744353 VAR-B + VAR-A = +548.41 VAR-IDX = 05 VAR-B = +548.41 VAR-A = +006.118182 VAR-B + VAR-A = +554.53 VAR-IDX = 06 VAR-B = +554.53 VAR-A = +001.744353 VAR-B + VAR-A = +556.27 VAR-IDX = 07 VAR-B = +556.27 VAR-A = +006.158715 VAR-B + VAR-A = +562.43 VAR-IDX = 08 VAR-B = +562.43 VAR-A = +001.744353 VAR-B + VAR-A = +564.17 VAR-IDX = 09 VAR-B = +564.17 VAR-A = +006.194759 VAR-B + VAR-A = +570.36 VAR-IDX = 10 VAR-B = +570.36 VAR-A = +001.744353 VAR-B + VAR-A = +572.10 VAR-IDX = 11 VAR-B = +572.10 VAR-A = +003.037896 VAR-B + VAR-A = +575.14 VAR-IDX = 12 VAR-B = +575.14 VAR-A = +001.743852 VAR-B + VAR-A = +576.88 VAR-IDX = 13 VAR-B = +576.88 VAR-A = +006.146530 VAR-B + VAR-A = +583.03 VAR-IDX = 14 VAR-B = +583.03 VAR-A = +001.744353 VAR-B + VAR-A = +584.77 VAR-IDX = 15 VAR-B = +584.77 VAR-A = +000.000377 VAR-B + VAR-A = +584.77 VAR-IDX = 16 VAR-B = +584.77 VAR-A = +001.743852 VAR-B + VAR-A = +586.51 VAR-IDX = 17 VAR-B = +586.51 VAR-A = +006.144363 VAR-B + VAR-A = +592.65 VAR-IDX = 18 VAR-B = +592.65 VAR-A = +001.743852 VAR-B + VAR-A = +594.39 VAR-IDX = 19 VAR-B = +594.39 VAR-A = +000.007649 VAR-B + VAR-A = +594.40 VAR-IDX = 20 VAR-B = +594.40 VAR-A = +001.744353 VAR-B + VAR-A = +596.14 VAR-IDX = 21 VAR-B = +596.14 VAR-A = +000.000377 VAR-B + VAR-A = +596.14 VAR-IDX = 22 VAR-B = +596.14 VAR-A = +001.744353 VAR-B + VAR-A = +597.88 FINAL RESULT = +597.88