Refer to Data types and attributes for general information about coded arithmetic data.
Syntax for coded arithmetic data is shown in the following diagram:
| Attribute | Abbreviation |
|---|---|
| BINARY | BIN |
| COMPLEX | CPLX |
| DECIMAL | DEC |
| PRECISION | PREC |
The base of a coded arithmetic data item is either decimal or binary. DECIMAL is the default.
The scale of a coded arithmetic data item is either fixed-point or floating-point.
A fixed-point data item is a rational number in which the position of the decimal or binary point is specified, either by its appearance in a constant or by a scaling factor declared for a variable.
Floating-point data items are rational numbers in the form of a fractional part and an exponent part.
The precision of a coded arithmetic data item includes the number of digits and the scaling factor. (The scaling factor is used only for fixed-point items).
The precision attribute specification is often represented as (p,q), where p represents the number of digits and q represents the scaling factor.
A negative scaling factor (-q) specifies an integer, with the point assumed to be located q places to the right of the rightmost actual digit. A positive scaling factor (q) that is larger than the number of digits specifies a fraction, with the point assumed to be located q places to the left of the rightmost actual digit. In either case, intervening zeros are assumed, but they are not stored; only the specified number of digits is actually stored.
If PRECISION is omitted, the precision attribute must follow, with no intervening attribute specifications, the scale (FIXED or FLOAT), base (DECIMAL or BINARY), or mode (REAL or COMPLEX) attributes at the same factoring level.
If included, PRECISION can appear anywhere in the declaration.
Integer value means a fixed-point value with a scaling factor of zero.
The mode of an arithmetic data item (coded arithmetic or numeric character) is either real or complex.
A real data item is a number that expresses a real value.
A complex data item consists of two parts--a real part and an imaginary part. For a variable representing complex data items, the base, scale, and precision of the two parts are identical.
Arithmetic variables default to REAL.
An imaginary constant is written as a real constant of any type immediately followed by the letter I. Examples are:
27I 3.968E10I 11011.01BI
Each of these has a real part of zero. A complex value with a nonzero real part is represented by an expression with the following syntax:
|
For example, 38+27I.
Given two complex numbers, y and z:
y = complex(A,B); z = complex(C,D);
x=y/z is calculated by:
real(x) = (A*C + B*D)/(C**2 + D**2); imag(x) = (B*C - A*D)/(C**2 + D**2);
x=y*z is calculated as follows:
real(x) = A*C - B*D; imag(x) = B*C + A*D;
Computational conditions can be raised during these calculations.
The SIGNED and UNSIGNED attributes can be used only with FIXED BINARY variables and ORDINAL variables. SIGNED indicates that the variable can assume negative values. UNSIGNED indicates that the variable can assume only nonnegative values.
UNSIGNED has the following effects on the semantics of fixed-point operations:
If you are using the RULES(ANS) compiler option, UNSIGNED has the following effect on the semantics of fixed- point operations:
The SIGNED and UNSIGNED attributes affect storage requirements, as shown in Table 10 and Table 11.
| This precision: | Occupies this amount of storage (bytes): |
|---|---|
| precision <= 7 | 1 |
| 7 < precision <= 15 | 2 |
| 15 < precision <= 31 | 4 |
| 31 < precision <= 63 | 8 |
| This precision: | Occupies this amount of storage (bytes): |
|---|---|
| precision <= 8 | 1 |
| 8 < precision <= 16 | 2 |
| 16 < precision <= 32 | 4 |
| 32 < precision <= 64 | 8 |
The data attributes for declaring binary fixed-point variables are BINARY and FIXED. For example:
declare Factor binary fixed (20,2);
Factor is declared as a variable that can represent binary fixed-point data of 20 data bits, two of which are to the right of the binary point.
Refer to SIGNED and UNSIGNED attributes for information on how much storage signed and unsigned fixed-binary data occupy.
The declared number of data bits is in the low-order positions, but the extra high-order bits participate in any operation performed upon the data item. Any arithmetic overflow into such extra high-order bit positions can be detected only if the SIZE condition is enabled.
A binary fixed-point constant consists of one or more bits with an optional binary point, followed immediately by the letter B. Binary fixed-point constants have a precision (p,q), where p is the total number of data bits in the constant, and q is the number of bits to the right of the binary point, for example:
The XN constant describes a SIGNED REAL FIXED BINARY constant in hexadecimal notation. If the constant has 8 or fewer digits, it has a precision of 31; otherwise, it has a precision of 63.
|
Consider the following examples:
'100'XN /* same as '00000100'XN with value 256 */ '8000'XN /* same as '00008000'XN with value 32,768 */ 'FFFF'XN /* same as '0000FFFF'XN with value 65,535 */ "ffff_ffff"XN /* is the value -1 */
The hexadecimal value for the XN constant is the value specified padded on the left with hex zeros if necessary.
The XU constant describes an UNSIGNED REAL FIXED BINARY constant in hexadecimal notation. If the constant has 8 or fewer digits, it has a precision of 32; otherwise, it has a precision of 64.
|
Consider the following examples:
'100'XU /* same as '00000100'XU with value 256 */ '8000'XU /* same as '00008000'XU with value 32,768 */ 'FFFF'XU /* same as '0000FFFF'XU with value 65,535 */ "ffff_ffff"XU /* is the value 2**32-1 */
The hexadecimal value for the XU constant is the value specified padded on the left with hex zeros if necessary.
The data attributes for declaring decimal fixed-point variables are DECIMAL and FIXED. For example:
declare A fixed decimal (5,4);
specifies that A represents decimal fixed-point data of 5 digits, 4 of which are to the right of the decimal point.
These two examples:
declare B fixed (7,0) decimal; declare B fixed decimal(7);
both specify that B represents integers of 7 digits.
This example
declare C fixed (7,-2) decimal;
specifies that C has a scaling factor of -2. This means that C holds 7 digits that range from -9999999*100 to 9999999*100, in increments of 100.
This example
declare D decimal fixed real(3,2);
specifies that D represents fixed-point data of 3 digits, 2 of which are fractional.
Decimal fixed-point data is stored two digits per byte, with a sign indication in the rightmost 4 bits of the rightmost byte. Consequently, a decimal fixed-point data item is always stored as an odd number of digits, even though the declaration of the variable can specify the number of digits, p, as an even number.
When the declaration specifies an even number of digits, the extra digit place is in the high-order position, and it participates in any operation performed upon the data item, such as in a comparison operation. If the extra high-order digit place is non-zero, the use of the data in arithmetic operation or assignment may produce an exception. Any arithmetic overflow or assignment into an extra high-order digit place can be detected only if the SIZE condition is enabled.
A decimal fixed-point constant consists of one or more decimal digits with an optional decimal point. Decimal fixed-point constants have a precision (p,q), where p is the total number of digits in the constant and q is the number of digits specified to the right of the decimal point. Examples are:
The data attributes for declaring binary floating-point variables are BINARY and FLOAT. For example:
declare S binary float (16);
S represents binary floating-point data with a precision of 16 binary digits.
The exponent cannot exceed five decimal digits. If the declared precision is less than or equal to (21), short floating-point form is used. If the declared precision is greater than (21) and less than or equal to (53), long floating-point form is used. If the declared precision is greater than (53), extended floating-point form is used.
A binary floating-point constant is a mantissa followed by an exponent and the letter B. The mantissa is a binary fixed-point constant. The exponent is the letter E, S, D, or Q followed by an optionally-signed decimal integer (meaning 2 to the power of this integer). Constants using E have a precision (p) where p is the number of binary digits of the mantissa. Constants using S, D, and Q always have maximum single, double, and extended precisions, respectively. Examples are:
The data attributes for declaring decimal floating-point variables are DECIMAL and FLOAT. Consider this example:
declare Light_years decimal float(5);
The value for Light_years represents decimal floating-point data of 5 decimal digits.
For IEEE decimal floating-point data,
For all other decimal floating-point data,
A decimal floating-point constant is a mantissa followed by an exponent. The mantissa is a decimal fixed-point constant. The exponent is the letter E, S, D, or Q followed by an optionally-signed decimal integer of four or less digits (meaning 10 to the power of this integer). Constants using E have a precision (p) where p is the number of digits of the mantissa. Constants using S, D, and Q always represent single, double, and extended precision respectively. Examples are:
The last five examples represent the same value (although with different precisions).
For IEEE Decimal Floating Point (DFP), decimal floating-point literals, when converted to "right-units-view", i.e. when the exponent has been adjusted, if needed, so that no non-zero digits follow the decimal point (for example, as would be done when viewing 3.1415E0 as 31415E-4), must have an exponent within the range of the normal numbers for the precision given by the literal. These bounds are given by the value of MINEXP-1 and MAXEXP-PLACES. In particular, the following must hold:
So, for IEEE Decimal Floating Point (DFP), the largest positive short decimal floating-point literal is 9999999E90 (or .9999999E97), and the smallest postive non-zero short decimal floating-point literal is 1E-95.