我对cobol中的计算函数中的四舍五入感到困惑。
声明:
VAR-A PIC S9(9)V99 COMP-3.
VAR-B PIC S9(9)V9(6) COMP-3.
步骤。
MOVE +12.08 TO VAR-A.
MOVE +6.181657 TO VAR-B.
COMPUTE VAR-A ROUNDED = VAR-A + VAR-B.
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.
假设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
VAR-B的价值是:
VAR-B = 405.25
PROCEDURE:
PERFORM VAR-IDX FROM 1 BY 1 UNTIL VAR-IDX > 22
COMPUTE VAR-B ROUNDED = VAR-B + VAR-A(VAR-IDX)
END-PERFORM.
为什么计算后得到VAR-B的597.87?
答案 0 :(得分:5)
我认为默认的COBOL舍入行为是:最接近于零。
COMPUTE VAR-A ROUNDED = VAR-A + VAR-B
应该导致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.
结果是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
答案 1 :(得分:1)
答案 2 :(得分:-2)
IDENTIFICATION DIVISION.
PROGRAM-ID. HELLO.
DATA DIVISION.
WORKING-STORAGE SECTION.
01 VAR_NUM PIC 9(3)V9(02).
01 VAR_RESULT PIC 9(3).
PROCEDURE DIVISION.
MOVE 256.50 TO VAR_NUM.
COMPUTE VAR_NUM ROUNDED = VAR_NUM / 100.
MULTIPLY 100 BY VAR_NUM
MOVE VAR_NUM TO VAR_RESULT.
DISPLAY "Result : " VAR_RESULT.
STOP RUN.