AS / 400:使用COMPUTE函数时,不同字段定义的结果不一致 [英] AS/400: Using COMPUTE function, inconsistent results with different field definition

查看:58
本文介绍了AS / 400:使用COMPUTE函数时,不同字段定义的结果不一致的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在AS / 400中使用COMPUTE函数时,我遇到了一个神秘的问题。



情况如下:

  01 WSAA-AMOUNT-A PIC S9(15)V9(02)COMP-3。 
01 WSAA-AMOUNT-B-01 PIC S9(16)V9(02)值0.
01 WSAA-AMOUNT-B-02 PIC S9(13)V9(05)值0.
01 WSAA-AMOUNT-C PIC S9(16)V9(02)值0.
01 WSAA-RESULT PIC S9(15)V9(02)值0.

移动2500.87致WSAA-AMOUNT-A。
将12285移动到WSAA-AMOUNT-B-01。
将12285移动到WSAA-AMOUNT-B-02。
将4387.5移动到WSAA-AMOUNT-C。

COMPUTE WSAA-RESULT ROUNDED =(WSAA-AMOUNT-A /(WSAA-AMOUNT-B-01 + WSAA-AMOUNT-C)* 100)。
显示WSAA结果。

COMPUTE WSAA-RESULT ROUNDED =(WSAA-AMOUNT-A /(WSAA-AMOUNT-B-02 + WSAA-AMOUNT-C)* 100)。
显示WSAA结果。

结果使我感到惊讶,第一个公式的结果为14.90
变成= 15



听起来后一个更合乎逻辑,例如2500.87 /(12285 + 4387.5)* 100 = 14.99997001。
我希望四舍五入后的第一个结果也是15。



有人知道这些不一致结果的根本原因是什么吗? / strong>

解决方案

获得的结果为14.90和15.00。



计算机的格式不正确。乘以100时,请尽早执行,因为如果您最后进行一次,则两个小数位会消失(除以100时,最后要保留小数位,这样就不会早早丢失有效数字)。



它不仅适用于100,所以请考虑一下。无论涉及什么值,都先乘以,最后除。除非您在最终答案中要求五个小数位,否则完全没有必要使用具有五个小数位的字段来获得正确答案。



计算器或电子表格。它使用中间结果取决于您的要求,而不是根据其他结果而给您的结果提供无数小数位。您可以使用所使用的字段中的小数位数来要求小数精度。



了解使用手册的一个关键是Fine Manual。另一个是实验。

 计算WSAA-RESULT ROUNDED = 
(WSAA-AMOUNT-A /( WSAA-AMOUNT-B-01 + WSAA-AMOUNT-C)* 100)

显示WSAA-RESULT 。得到答案14.90(十四点九零)。

 计算机WSAA-RESULT = 
(WSAA-AMOUNT- A /(WSAA-AMOUNT-B-01 + WSAA-AMOUNT-C)* 100)

显示WSAA结果。

  COMPUTE WSAA-RESULT ROUNDED = $ b $,则得到答案14.00(十四点零零)。 b(WSAA-AMOUNT-A /(WSAA-AMOUNT-B-02 + WSAA-AMOUNT-C)* 100)

显示WSAA-RESULT通过使用小数点后五个位,可以得到15.00(十五分零零)的正确预期答案。



记住,您最后乘以100。因此在此之前,值分别为0.149(小数点后三位),0.14(小数点后两位)和14.99999n(小数点后六位),我不再进行计算,所以我ve将第六个留为 n)。



为什么小数点后三,二和六?六个是因为B-02(具有五个位,再加上一个用于ROUNDED,则将所需的小数位增加1,以便可以计算ROUNDED),两个是由于B-01(具有两个小数位) )这三个是因为B-01和ROUNDED。



这是经过重新设计的COMPUTE,在以下三个版本中:

 计算WSAA-结果圆=((WSAA-AMOUNT-A * 100)
/(WSAA-AMOUNT-B-01
+ WSAA -AMOUNT-C))

计算WSAA-RESULT =((WSAA-AMOUNT-A * 100)
/(WSAA-AMOUNT-B-01
+ WSAA-AMOUNT -C))

计算WSAA-结果圆=((WSAA-AMOUNT-A * 100)
/(WSAA-AMOUNT-B-02
+ WSAA-AMOUNT- C))

结果为15.00、14.99和15.00。



这是与Enterprise Cobol。似乎OpenCobol使用了不同的中间结果,这些中间结果可能已记录在案。



此处是有关中间结果的《企业Cobol编程指南》: b

要了解有关中间结果的此信息,您需要了解以下术语。
...
d中间结果携带的小数位数。(如果使用ROUNDED短语,如果有必要,还可以再保留一个小数位以确保准确性。)
dmax在特定的语句中,下列项中最大的一个:
v最终结果字段或字段$ b所需的小数位数$ bv为任何操作数定义的最大小数位数,除除数或指数外,

v任何函数操作数的external-dmax



 操作...小数位
+或-... d1或d2,以较大的$ b为准$ b * ... d1 + d2
/ ...(d2-d1)或dmax,以较大者为准

在错误示例中,加法得到两个小数位(d1和d2均为两个小数),除法得到三个小数位(dmax为3),乘法得到3(ROUNDED为2 + 0 + 1)。尝试四舍五入后的最终答案(它将永远不会起作用,因为两个低位小数始终由于乘以100而始终为零)被截断为两位小数。



对于ROUNDED B-01,dmax为3(结果字段为2位,加上1个舍入)。对于普通的B-01,dmax为2。对于ROUNDED B-02,dmax为6。



注意,不仅显示的示例是错误的。在问题的第一个示例中,ROUNDED从不运行,并且始终删除一个有效的小数,因此,只有在四舍五入前的答案为十分之一时,您才获得正确的答案。然后,这些正确答案将被由于截断而变的9个其他答案掩盖。



如果您始终编写有效的COMPUTE,就不会遇到问题。 ROUNDED仅对最终结果起作用。如果您需要将任何中间字段设为圆形,请分别进行计算并将结果用于新的计算中。



COMPUTE有效。 COMPUTE不能像计算器/电子表格那样运行。其他Cobol动词(它们是动词不是功能)也没有。考虑如何编写COMPUTE,以免失去意义。阅读手册。实验。重复直到正确并理解为止。


I have faced a mysterious problem while using the COMPUTE function in AS/400.

The scenario is as follows:

01  WSAA-AMOUNT-A               PIC S9(15)V9(02) COMP-3.
01  WSAA-AMOUNT-B-01            PIC S9(16)V9(02) VALUE 0.
01  WSAA-AMOUNT-B-02            PIC S9(13)V9(05) VALUE 0.
01  WSAA-AMOUNT-C               PIC S9(16)V9(02) VALUE 0.
01  WSAA-RESULT                 PIC S9(15)V9(02) VALUE 0.

MOVE 2500.87             TO WSAA-AMOUNT-A. 
MOVE 12285               TO WSAA-AMOUNT-B-01.
MOVE 12285               TO WSAA-AMOUNT-B-02.
MOVE 4387.5              TO WSAA-AMOUNT-C. 

COMPUTE WSAA-RESULT ROUNDED = (WSAA-AMOUNT-A / ( WSAA-AMOUNT-B-01 + WSAA-AMOUNT-C) * 100 ).
DISPLAY WSAA-RESULT.

COMPUTE WSAA-RESULT ROUNDED = (WSAA-AMOUNT-A / ( WSAA-AMOUNT-B-02 + WSAA-AMOUNT-C) * 100 ).
DISPLAY WSAA-RESULT.

The results surprised me that the result from the first formula = 14.90 while the second one became = 15

It sounds the later one is more logical as 2500.87 / (12285+4387.5) * 100 = 14.99997001. I expect the result from first result should be 15 too after rounding.

Does anyone know what is the root cause for those inconsistent results?

解决方案

The results achieved were 14.90 and 15.00.

The COMPUTE is poorly-formed. When multiplying by 100, do it as early as possible, because if you do it last, two decimal-places disappear (when dividing by 100, do it last, so you don't lose significant digits early on).

It doesn't only apply to 100, so think about it. Multiply first, divide last, whatever values are involved. It is entirely unnecessary to use a field with five decimal places to get the "correct" answer, unless you require five decimal places in the final answer.

The computer is not a calculator or spreadsheet. It uses intermediate results depending on what you ask for rather than the others which give you numerous decimal places for anything. You ask for decimal precision using the number of decimal places in the fields you use.

One key to understanding what is going on is the Fine Manual. Another is experimentation. This can often be a general answer.

COMPUTE WSAA-RESULT ROUNDED = 
                  (WSAA-AMOUNT-A / ( WSAA-AMOUNT-B-01 + WSAA-AMOUNT-C) * 100 )

DISPLAY WSAA-RESULT. gets the answer 14.90 (fourteen point nine-zero).

COMPUTE WSAA-RESULT = 
                  (WSAA-AMOUNT-A / ( WSAA-AMOUNT-B-01 + WSAA-AMOUNT-C) * 100 )

DISPLAY WSAA-RESULT. gets the answer 14.00 (fourteen point zero-zero), by removing the ROUNDED.

COMPUTE WSAA-RESULT ROUNDED = 
                  (WSAA-AMOUNT-A / ( WSAA-AMOUNT-B-02 + WSAA-AMOUNT-C) * 100 )

DISPLAY WSAA-RESULT gets 15.00 (fifteen point zero-zero) the "correct" "expected" answer, by using five decimal places.

Remember, you multiplied, last, by 100. So before that the values were 0.149 (three decimal places), 0.14 (two decimal places) and 14.99999n (six decimal places, and I'm not going to do the calculation again, so I've left the sixth as "n").

Why are there three, two and six decimal places? The six is because of the B-02 (with five places, plus one for ROUNDED, which increase the required decimal places by one, so that the ROUNDED can be calculated), the two because of the B-01 (with two decimal places) the three is because of the B-01 and ROUNDED.

Here is a re-worked COMPUTE, in the three versions:

COMPUTE WSAA-RESULT ROUNDED =  ( ( WSAA-AMOUNT-A * 100 )
                               / ( WSAA-AMOUNT-B-01 
                                 + WSAA-AMOUNT-C ) ) 

COMPUTE WSAA-RESULT         =  ( ( WSAA-AMOUNT-A * 100 )
                               / ( WSAA-AMOUNT-B-01 
                                 + WSAA-AMOUNT-C) ) 

COMPUTE WSAA-RESULT ROUNDED = ( ( WSAA-AMOUNT-A * 100 ) 
                              / ( WSAA-AMOUNT-B-02 
                                + WSAA-AMOUNT-C) ) 

The results are 15.00, 14.99 and 15.00.

This is with Enterprise Cobol. It seems OpenCobol uses different intermediate results, which are probably documented.

Here is the Enterprise Cobol Programming Guide on intermediate results:

"To understand this information about intermediate results, you need to understand the following terminology. ... d The number of decimal places carried for an intermediate result. (If you use the ROUNDED phrase, one more decimal place might be carried for accuracy if necessary.) dmax In a particular statement, the largest of the following items: v The number of decimal places needed for the final result field or fields v The maximum number of decimal places defined for any operand, except divisors or exponents v The outer-dmax for any function operand"

and

"Operation ... Decimal places
+ or - ... d1 or d2, whichever is greater
* ... d1 + d2
/ ... (d2 - d1) or dmax, whichever is greater"

With the "erroneous" example, the addition gets two decimal places (both d1 and d2 are two for the addition), the division gets three decimal places (dmax is three) and the multiplication gets three (two + zero + one for the ROUNDED). The final answer, after rounding is attempted (it will never operate, as the two low-order decimals are always zero due to the multiply by 100), is truncated to two decimal places.

For the ROUNDED B-01, dmax is three (result field of two places, plus one for rounding). For the plain B-01, dmax is two. For the ROUNDED B-02 dmax is six.

Note, it is not only the example shown which is wrong. ROUNDED never operates in the first example in the question, and one significant decimal is always dropped, so you only obtain the correct answer at the times when the answer before rounding is in tenths. These correct answers are then masked by nine further answers which become the same due to truncation.

If you always write effective COMPUTEs, you won't run into problems. ROUNDED only operates on the final result. If you need any intermediate fields ROUNDED, calculate that separately and use the result in a new calculation.

COMPUTE works. COMPUTE does not operate like a calculator/spreadsheet. Nor does any other Cobol verb (they are verbs not functions). Think about how to write the COMPUTE so as not to loose significance. Read the manual. Experiment. Repeat until correct and understood.

这篇关于AS / 400:使用COMPUTE函数时,不同字段定义的结果不一致的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆