COBOL 数据类型
我对 COBOL 数据类型感到困惑。 就像在许多采访中一样,它被要求解释 COMP-3 和 COMP 之间的区别...... 确切的区别是什么? COBOL 中使用模式的含义是什么?它与数据类型有何关系?
I have confusion regarding COBOL data types.
Like in many interviews it is asked to explain the difference between COMP-3 and COMP...
what is the exact difference?
what is the meaning of usage modes in COBOL and how is it related to data types?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
COBOL 中的
USAGE
描述了如何使用数据项。几个例子用途是:
报告。这可能是也可能不是数字(例如可以是文本值)。这
DISPLAY 项的描述由 PICture 子句给出。例如:
PIC 9(5) USAGE DISPLAY
描述了可以显示(打印)的 5 位数字。USAGE DISPLAY
通常会被省略,因为如果缺少它,则暗示它。算术运算(即它是某种类型的数字)。
数字项有多种类型。最常用的两个
数值数据类型有:
COMP (BINARY) 数据项通常是最有效的执行方式
对表示整数值的数据项进行计算。
COBOL 中使用 COMP-3 (PACKED-DECIMAL) 数据项是因为
他们保持固定的小数位数。所有计算
导致结果具有规定的小数位数。
这在会计类型操作中特别有用。
浮点数表示后面的位数
小数点变量(例如小数点可以“浮动”)
这不是金融运作通常的表现方式。
您可以找到 IBM Enterprise COBOL 的计算项目的完整列表
此处
许多问题之一程序员在开始使用 COBOL 时有
了解 COMP 项目非常适合做数学,但不能
显示(打印)直到通过转换为 DISPLAYable 项目
MOVE 语句。如果您将 COMP 项目移至报告或
屏幕上它不会很好地呈现。需要将其移至 DISPLAY
第一个项目。
您可能想要更多研究的另一件事是
定义变量时PICture和USAGE的关系
在 COBOL 中。这是一个非常好的介绍性COBOL 教程的链接
来自利默里克大学。
USAGE
in COBOL describes how a data item is to be used. A few examplesof USAGE are:
report. This may or may not be a number (e.g. could be a text value). The
description of the DISPLAY item is given by the PICture clause. For example:
PIC 9(5) USAGE DISPLAY
describes a 5 digit number that may be displayed (printed).Often
USAGE DISPLAY
is left off because it is implied if missing.arithmetic operations (i.e. it is a number of some type).
There are various types of numeric item. Two of the most commonly used
numeric data types are:
COMP (BINARY) data items are generally the most efficient way to perform
calculations on data items that represent integer values.
COMP-3 (PACKED-DECIMAL) data items are used in COBOL because
they maintain a fixed number of decimal points. All computations
lead to a result having the prescribed number of decimal points.
This is particularly useful in accounting type operations.
Floating point numbers make the number of digits after the
decimal point variable (e.g. the decimal point can "float") which is
not the way financial operations are usually represented.
You can find a complete list of COMPutational items for IBM Enterprise COBOL
here
One of the problems many programmers have when beginning with COBOL is
understanding that a COMP item is great for doing math but cannot be
displayed (printed) until it is converted into a DISPLAYable item through
a MOVE statement. If you MOVE a COMP item into a report or onto a
screen it will not present very well. It needs to be moved into a DISPLAY
item first.
The other thing that you may want to research a bit more is the
relationship between the PICture and the USAGE when defining variables
in COBOL. Here is a link to a very good introductory COBOL Tutorial
from the University of Limerick.
COBOL 实际上只有两种数据类型:数字和字符串。
COBOL 记录中每个字段的布局由
PICTURE
(通常缩写为PIC
)子句精确指定。最常见的是:PIC X
。PIC X(100)
表示 100 字节字符串。PIC 9
用于数字,可以选择带有S
(符号)或V
(隐式小数点)。例如,PIC S9(7)V99
表示隐式小数点左侧 7 位、右侧 2 位的带符号数。数字字段可以有一个
USAGE
子句来优化其存储。最常见的USAGE
是DISPLAY
、COMP
和COMP-3
。DISPLAY
将每个数字存储为一个字符。例如,PIC 9(4) VALUE 123
将数字存储为字符串“0123”。PIC 9(4)V99 VALUE 123.45
将其存储为“012345”。请注意,小数点实际上并未存储。这是一种低效格式,因为它需要 8 位来表示每个数字。但它确实通过使用最后一个字节的一半来存储符号来对有符号数进行“优化”。通常,EBCDIC 数字的高半字节都是 F,所以 0123 就是 F0 F1 F2 F3。但-0123是F0 F1 F2 D3; D表示负数。 C表示正数,F表示无符号(即正数)。 (类似的格式也用于 COBOL 的 ASCII 版本中,但不符合标准化。)
COMP-3
是带有尾随符号 nybble 的二进制编码的十进制。PIC 9(3) COMP-3 VALUE 123
变为两个字节 12 3F。COMP
或BINARY
是本机二进制格式,就像short
、int
或long
> 在 C 中。COBOL really only has two data types: Numbers and strings.
The layout of each field in a COBOL record is precisely specified by a
PICTURE
(usually abbreviatedPIC
) clause. The most common ones are:PIC X
for strings.PIC X(100)
means a 100-byte string.PIC 9
for numbers, optionally withS
(sign) orV
(implicit decimal point). For example,PIC S9(7)V99
means a signed number with 7 digits to the left of the implicit decimal point and 2 digits to the right.Numeric fields can have a
USAGE
clause to optimize their storage. The most commonUSAGE
s areDISPLAY
,COMP
, andCOMP-3
.DISPLAY
stores each digit as a character. For example,PIC 9(4) VALUE 123
stores the number as if it were the string "0123". AndPIC 9(4)V99 VALUE 123.45
stores it as "012345". Note that the decimal point is not actually stored.This is an inefficient format in that it requires 8 bits to represent each digit. But it does have an "optimization" for signed numbers by using half of the last byte to store the sign. Normally, EBCDIC digits all have a high nybble of F, so 0123 is F0 F1 F2 F3. But -0123 is F0 F1 F2 D3; the D indicates negative. C means positive, and F means unsigned (i.e., positive). (Similar formats are used in ASCII versions of COBOL, but not as standardized.)
COMP-3
is binary-coded decimal with trailing sign nybble.PIC 9(3) COMP-3 VALUE 123
becomes the two bytes 12 3F.COMP
orBINARY
is native binary format, just likeshort
,int
, orlong
in C.至于决定使用哪种数据类型,它可能会变得非常复杂 - 但是 - 一组简单的指导原则是:
显示和编辑区域小数只能用于在报告或系统输出中显示数字。将 COMP 和 COMP-3 字段移至“显示/编辑”字段,然后再将其放入报告或系统输出中。
COMP - 对整数具有最快的计算速度
COMP-3 (PACKED Decimal) - 当小数位置时应使用> 应予以维护。
COMP 和 COMP-3 字段可以在计算中一起使用。编译器将确定哪种字段类型将(在幕后)转换为单个通用数字数据类型 - 基于规则。
As for deciding which data type to use, it can be made very complicated - BUT - a simple set of guidelines are:
DISPLAY and Edited Zone Decimal should only be used for displaying numerics in a report or sysout. Move COMP and COMP-3 fields to a DISPLAY/Edited field before putting it in a report or to sysout.
COMP - has the fastest calculation speed for integers
COMP-3 (PACKED Decimal) - should be used when decimal positions should be maintained.
COMP and COMP-3 fields can be used together in calculations. The compiler will determeine which field type will be converted (under the covers) to a single common numeric data type - rules based.
正如其他回复所暗示的,COMP 表示大端二进制。 COMP-3 是压缩十进制 - 这意味着一个十进制数字映射到每个半字节。
我不确定之前的回复是否正确解决了精度问题。
PIC S9(9)V9(9) 补偿
和
PIC S9(9)V9(9) COMP-3
具有完全相同的精度。这是 ANSI85 标准的一部分。编译器和运行时的工作是确保 COMP 中的二进制表示形式经过适当的转换,以确保获得与使用显示或 COMP 时完全相同的结果。 3.
IBM 大型计算机在硬件中封装了十进制计算。这非常有帮助,因为十进制到二进制的转换为 n 的平方 n 是数字的长度。这意味着 COMP-3 通常是最快的大型机格式,但不太可能出现在分布式系统上。然而,情况并非总是如此。例如,对于非常大的十进制精度(>18 位),Micro Focus 原生 COBOL 解决方案在 COMP-3 中往往比 COMP-5 更快,但在其他情况下则相反。 Micro Focus 的托管 COBOL 系统在 COMP 中几乎总是最快的(实际上,COMP-5 是最好的 - 它与 COMP 类似,但具有硬件字节序而不是强制执行大字节序内存布局)。
最后,我建议,对于中间值和一般数学,二进制长整型和二进制双精度型的较新数据定义是更好的选择,因为这样编译器可以为您做出有关如何存储和优化的决定。
有关分布式 COBOL 和托管 COBOL 的更多信息,请查看此 knol:http://knol.google.com/k/alex-turner/micro-focus-management-cobol/2246polgkyjfl/4,也可以随时在 Facebook 上查找 cobol :)
As other reply suggests, COMP means big endian binary. COMP-3 is packed decimal- which means one decimal digit is mapped to each nibble.
I am not sure the previous reply got the issue around precision correct though.
PIC S9(9)V9(9) COMP
and
PIC S9(9)V9(9) COMP-3
Have exactly the same precision. That is part of the ANSI85 standard. It is the job of the compiler and runtime to ensure that the binary representation in the COMP has the appropriate transformations placed upon it to ensure exactly the same results are achieved as would be if usage was display or COMP-3.
IBM mainframe computers have packed decimal calculations in hardware. This is very helpful, because the conversion of decimal to binary scales as n squared n is the length of the number. This means that COMP-3 is every often the fastest format of the mainframe, but is less likely to be on distributed systems. However, this again is not always the case. For example, the Micro Focus native COBOL solution will tend to be faster in COMP-3 than COMP-5 for very large decimal precision (>18 digits) but the reverse for otherwise. The Managed COBOL system from Micro Focus is almost always fastest in COMP (actually, COMP-5 is the best - which is similar to COMP but will have hardware endian rather than enforcing big-endian memory layout).
Finally, my I suggest that for intermediate values and general mathematics, the newer data definitions of binary-long and binary-double are a better choice because then the compiler can make the decisions about how to store and optimize for you.
For more on COBOL on distributed and Managed COBOL check out this knol: http://knol.google.com/k/alex-turner/micro-focus-managed-cobol/2246polgkyjfl/4 and also feel free to look up cobol on facebook :)
澄清何时为数据项选择特定类型和用途。
任何字符数据然后 PIC X(n) 具有适合字符串的大小。
较短的字符串将用尾随空格填充。
计算中很少使用但经常显示的数字(例如年龄、邮政编码、客户编号)然后是 PIC 9(n) 使用情况显示。
用于计算计算中使用的事物的整数(例如 QTY_AVAILABLE)然后 PIC S9(4) COMP。 S9(4) 在大多数平台上是小整数 S9(8) 在大多数平台上是 32 位整数。
计算中使用的货币值(例如 PRICE、DELIVERY_COST、TAX),然后是 PIC S9(4)V99 COMP 或 COMP-3。这将使会计计算能够进行正确的舍入。
如果平台是 IBM 大型机或类似的具有压缩十进制硬件支持的平台,则选择 COMP-3,否则 COMP 效率更高。
请注意,要在屏幕或报告上显示 COMP 值,您必须首先将其移动到 DISPLAY 类型项目,因此“PIC S9(4)V99 COMP”应移动到“PIC ---9.99 DISPLAY”项目以使其易于阅读。这会将数字显示为“12.45”和“-123.45”。
To clarify when you would select a particular type and usage for a data item.
Any character data then PIC X(n) of the appropriate size for the string.
Shorter strings will be padded with trailing spaces.
Numbers which are seldom used in calculations but are displayed often (e.g. AGE, ZIPCODE, CUSTOMER_NUMBER) then PIC 9(n) USAGE DISPLAY.
Whole numbers used to count things which are used in calculations (e.g. QTY_AVAILABLE) THEN PIC S9(4) COMP. S9(4) is a smallint on most paltforms S9(8) is a 32 bit integer on most platforms.
Currency values used in calculations (e.g. PRICE, DELIVERY_COST, TAX ) then PIC S9(4)V99 COMP or COMP-3. This will enable accounting calculations with the correct rounding.
If platform is an IBM mainframe or similar which has hardware support for packed decimal then choose COMP-3, otherwise COMP is more efficient.
Note that to show COMP values on a scrren or report you must first move it to a DISPLAY type item so "PIC S9(4)V99 COMP" should be moved to a "PIC ---9.99 DISPLAY" item to make it human readable. This would display numbers as " 12.45" and " -123.45".