Docstoc

Arithmetic-Concepts

Document Sample
Arithmetic-Concepts Powered By Docstoc
					25 April 2005

J4/05-0095
Page 1 of 10
Arithmetic Concepts Charles C. Stevens

Subject: Author: References: 1.

ISO/IEC 1989:2002, Programming language COBOL 20xx -- Base Document for 20xx standard (Nelson) (3/29/2005)

2. IEEE DRAFT Standard for Floating-Point Arithmetic P754 (see http://754r.ucbtest.org/ for current draft) 3. J4/05-0058, Specification of Default ROUNDED MODE IS (Stevens) Previous version of document: J4/05-0063, Standard-binary and standard-decimal concepts (Stevens) Discussion: With the introduction of Standard-Binary and Standard-Decimal arithmetic, the INTERMEDIATE ROUNDING clause and new methods of rounding associated with the ROUNDED clause, it was decided that significant revisions and clarifications to the discussion of arithmetic in the section of the standard would be appropriate. At Meeting #251, J4 suggested a number of changes to the document. Changes required: 1) Page 763, after all text associated with D.13, Common exception processing, add new headings and text: D.13a Rounding COBOL provides the capability of specifying rounding in arithmetic statements and expressions at various points in the evaluation process and as values are prepared for storing into receiving data items. The following forms of rounding are provided (examples presume an integer destination):   AWAY-FROM-ZERO: Rounding is to the nearest value of larger magnitude. NEAREST-AWAY-FROM-ZERO: Rounding is to the nearest value. If two values are equally near, the value with the larger magnitude is selected. This mode has historically been associated with the ROUNDED clause in previous versions of standard COBOL. NEAREST-EVEN: Rounding is to the nearest value. If two values are equally near, the value whose rightmost digit is even is selected. This mode is sometimes called “Banker’s rounding”.



25 April 2005
 

J4/05-0095
Page 2 of 10
NEAREST-TOWARD-ZERO: Rounding is to the nearest value. If two values are equally near, the value with the smaller magnitude is selected. PROHIBITED: No rounding is performed. If the value cannot be represented exactly in the desired format, the EC-SIZE-TRUNCATION condition is set to exist and the results of the operation are undefined. TOWARD-GREATER: Rounding is toward the nearest value whose algebraic value is larger. TOWARD-LESSER: Rounding is toward the nearest value whose algebraic value is smaller. TRUNCATION: Rounding is to the nearest value whose magnitude is smaller. This mode has historically been associated with the absence of the ROUNDED clause as well as for the formation of intermediate results in the prior COBOL standard.

  

Table D.1 illustrates the effect of the various rounding modes with various values. The ellipses in the column heading values indicate repetitions of the last digit in the intermediate result. The column contents indicate the result expected when rounding that intermediate result value to an integer. Table D.1: ROUNDED MODE examples
+2.49… +3 +2 +2 +2 +3 +2 +2 -2.49… -3 -2 -2 -2 -2 -3 -2 +2.50… +3 +3 +2 +2 +3 +2 +2 -2.50… -3 -3 -2 -2 -2 -3 -2 +3.49… +4 +3 +3 +3 +4 +3 +3 -3.49… -4 -3 -3 -3 -3 -4 -3 +3.50… +4 +4 +4 +3 +4 +3 +3 -3.50… -4 -4 -4 -3 -3 -4 -3 +3.510 +4 +4 +4 +4 +4 +3 +3 -3.510 -4 -4 -4 -4 -3 -4 -3

Away-from-Zero Nearest-Away-from-Zero Nearest-Even Nearest-Toward-Zero Toward-Greater Toward-Lesser Truncation

The programmer may specify how individual intermediate values are rounded when they are stored into receiving data items through the ROUNDED clause; may select a default mode of rounding to be used when the ROUNDED clause appears with no further qualification on a receiving data item through the DEFAULT ROUNDED MODE clause of the OPTIONS paragraph of the IDENTIFICATION DIVISION; and may specify how arithmetic operations and conversions to and from intermediate forms are rounded through the INTERMEDIATE ROUNDING clause.

D.13a.1 Intermediate rounding Intermediate rounding applies when data items are retrieved for inclusion in an arithmetic operation and during the execution of arithmetic operations to produce an intermediate result. In the previous standard, for multiplication and division in Standard Arithmetic, the default mode of rounding for inexact results was truncation to 32 significant digits. This default is unchanged in this standard, and is also the default for Standard-Binary and Standard-Decimal arithmetic. When the intermediate value can be represented exactly in the appropriate intermediate format, the exact value is used.

25 April 2005

J4/05-0095
Page 3 of 10

In the event the value cannot be exactly represented, the user may now specify other modes of rounding for arithmetic operations and for the conversions to and from intermediate forms used in the arithmetic operations through the optional INTERMEDIATE ROUNDING clause of the OPTIONS paragraph of the IDENTIFICATION DIVISION. Specifically, the following options are available in the INTERMEDIATE ROUNDING clause     NEAREST-AWAY-FROM-ZERO NEAREST-EVEN PROHIBITED TRUNCATION

for which the descriptions are found in D.13a, Rounding. If the INTERMEDIATE ROUNDING clause is not specified, INTERMEDIATE ROUNDING IS TRUNCATION is implied. The implicit intermediate rounding specified in this draft international standard is unchanged from previous editions of Standard COBOL. D.13a.2 Final rounding (the ROUNDED clause) Final rounding applies to the formation of the final result of the expression or statement immediately before the result is placed in the destination. This form of rounding is that which is associated with the ROUNDED clause. In previous COBOL standards, only two methods of “final” rounding were provided:   Rounding toward the smaller magnitude (truncation, signaled by the absence of the ROUNDED clause) Rounding to the nearest value; if two values were equally near, the value with the larger magnitude was chosen (signaled by the presence of the ROUNDED clause).

The ROUNDED clause has been enhanced to provide explicit selection of rounding modes, for example, ROUNDED MODE IS NEAREST-EVEN. The rounding modes available in the ROUNDED clause are described in D.13a, Rounding. If the ROUNDED clause is not present for a given result, the rules for ROUNDED MODE IS TRUNCATION apply. This provides the same behavior as specified in the previous COBOL standard. The optional DEFAULT ROUNDED MODE clause in the OPTIONS paragraph of the IDENTIFICATION DIVISION is provided to allow the user to specify the mode of rounding for any operation for which the ROUNDED phrase is specified without the MODE phrase. The default rounding modes that may be specified are described in D.13a, Rounding. If the DEFAULT ROUNDED MODE clause is not specified in the program, the effect of the ROUNDED phrase without the MODE phrase is as if ROUNDED MODE IS NEAREST-AWAY-FROM-ZERO had been specified. This provides the same functionality for

25 April 2005

J4/05-0095
Page 4 of 10

rounding as was provided in prior COBOL standards, and is also the same functionality provided by DEFAULT ROUNDED MODE IS NEAREST-AWAY-FROM-ZERO. If the DEFAULT ROUNDED MODE clause appears, ROUNDED phrases without MODE phrases are treated as if they had been specified with the rounding mode specified in the DEFAULT ROUNDED MODE clause. 2) Page 780, D.14, Standard arithmetic: Change the heading to read "Forms of Arithmetic”. Add the following text immediately after that heading: COBOL provides four basic forms of arithmetic: Native, Standard, Standard-binary and Standard-decimal. Native arithmetic allows the implementor to use intermediate data item formats and computational techniques that the implementor deems most efficient for a particular operating environment. Standard arithmetic, introduced in the previous edition of this draft international standard, provided rules that specified intermediate results that would in most cases be identical regardless of the operating environment. Standard arithmetic is an archaic element of Standard COBOL. Standard-binary arithmetic provides most of the advantages of standard arithmetic, slightly greater inherent precision, a specific format for arithmetic operands and intermediate results, and arithmetic rules and formats explicitly described in a platform-independent, languageindependent standard. In standard-binary arithmetic, the arithmetic operand format is a binary floating-point format, and for decimal operands there is some risk of precision loss during the conversion from binary floating-point form to decimal and vice versa. Standard-decimal arithmetic provides all of the advantages of standard-binary arithmetic, and in addition provides a platform-independent, language-independent decimal floating-point format, thereby avoiding the conversion precision loss that might be entailed for decimal operands in standard-binary arithmetic. For brevity, throughout D.14, Forms of Arithmetic, “IEEE DRAFT Standard for Floating-Point Arithmetic P754/D0.8.1 -- 2004 November 29 20:00” shall be referred to simply as “IEEE 754r”. The ARITHMETIC clause in the identification division determines the mode of arithmetic for the source unit: native arithmetic, standard arithmetic, standard-decimal arithmetic or standardbinary arithmetic. If the ARITHMETIC clause is not specified, native arithmetic is the mode of arithmetic for the source unit. Most numeric operands in COBOL have historically been in some decimal format. When standard-decimal arithmetic is in effect, intermediate results are in a decimal form and must be converted from IEEE 754r Decimal128 to IEEE 754r Binary128 format before the values can be stored in binary destinations. Similarly, when standard-binary arithmetic is in effect, intermediate results must be converted from IEEE 754r Binary128 format to IEEE 754r Decimal128 format before the values can be stored in decimal destinations.

25 April 2005

J4/05-0095
Page 5 of 10

Radix conversions can cause precision loss and may be costly in performance. Results using decimal sending and receiving operands are likely to be more precise using standard-decimal arithmetic than standard-binary arithmetic precisely because radix conversions are avoided. The following characteristics are common to standard arithmetic, standard-binary arithmetic, and standard-decimal arithmetic: 1) A single format appropriate to the specified arithmetic mode is used for each operand in an arithmetic expression and the result of every arithmetic operation, arithmetic expression, and integer and numeric intrinsic functions. 2) The binary arithmetic operators +, -, * and / and the SQRT function are defined to give results that are accurate to the precision of the format appropriate to the arithmetic mode. Exponentiation is defined to give results that are accurate to the precision of that format for exponents with the values –4, -3, -2, -1, 0, 1, 2, 3, and 4. 3) The size error condition exists when the result of any single arithmetic operation cannot be contained in the format appropriate to the arithmetic mode. 4) The size error condition exists when certain division and exponentiation errors occur and when the resultant identifier of an arithmetic statement cannot contain the value in the intermediate result. 3) Page 780, D.14, Forms of arithmetic: Add a new heading and text (renumbering headings originally subordinate to D.14 accordingly) immediately after all changes introduced by change 2 and before the paragraph that begins “When standard arithmetic …”: D.14.1 Standard arithmetic Standard arithmetic was introduced in the previous edition of the standard. It is both an archaic element of standard COBOL and a processor-dependent feature of the language. Technological improvements in the industry, in particular the rules detailed in IEEE 754r regarding rounding and arithmetic operations, and the 128-bit decimal and binary floating-point formats in that standard, have outstripped the capabilities and the potential portability of standard arithmetic. These superior mechanisms are represented by standard-decimal and standard-binary arithmetic, discussed in D.14.2, Standard-decimal arithmetic, and D.14.3, Standard-binary arithmetic, respectively. 4) Page 780, D.14.1, Standard arithmetic (as introduced by Change 3): Delete the paragraph that begins “In order to achieve …” and the seven numbered items that immediately follow it. 5) Page 781, D.14.1, immediately before the heading “D.14.1.1 Maximum error due to truncation and rounding” (as modified by the renumbering in Change 3 above, and renumbered hereby to D.14.1.2 with appropriate renumbering of all subordinate headings), add the following new heading and text: D.14.1.1 Specification
The ARITHMETIC IS STANDARD clause in the options paragraph of the identification division specifies that the mode of arithmetic for the source unit is standard arithmetic.

25 April 2005

J4/05-0095
Page 6 of 10

6) Page 784, after all text associated with D.14.1, Standard arithmetic, add a new heading and text: D.14.2 Standard-decimal arithmetic Standard-decimal arithmetic is preferred over standard arithmetic (described at D.14.1, Standard arithmetic), as well as over standard-binary arithmetic (described at D.14.3, Standard-binary arithmetic). Standard-decimal arithmetic shares with standard arithmetic the specification that arithmetic operations are performed, and intermediate results produced, in a decimal floating-point format, which is an advantage because most numeric operands in COBOL have historically been in some decimal format, and the performing of arithmetic operations using a floating-point decimal format eliminates conversion from and to binary forms, with the possibility of precision loss in that process, when the operands themselves are decimal. The format for a standard-decimal intermediate data item is explicitly defined in IEEE 754r (as the Decimal128 format). This format is identical to that of a data item declared USAGE IS FLOAT-DECIMAL-34, which means the COBOL standard provides for the ability of a user to declare a data item in the same format. This contrasts with standard arithmetic in that there is no USAGE in standard COBOL that directly corresponds to a “standard intermediate data item”, and in that the implementor defines the actual format of such a data item. The rules for addition, subtraction, multiplication and division and the SQRT intrinsic function are explicitly defined in IEEE 754r, and COBOL uses the definitions in that industry standard rather than relying on language-specific rules for these operations. Likewise, the specifications as to how intermediate and final rounding is performed either explicitly cite the corresponding rules in IEEE 754r or use them as their basis. The major motivation for this enhancement was to improve the potential portability of COBOL applications by having COBOL produce results consistent with existing, platform-indepenent, language-independent standards, specifically, IEEE 754r. Results produced in conformance with that standard are well-defined. It is anticipated that vendors will implement efficient means of storing and operating upon data items in IEEE 754r formats, leading to the expectation that such implementations will also be efficient. The use of IEEE 754r specifications in support of COBOL data types, arithmetic rules, and rounding and truncation algorithms, helps the user to control and minimize imprecision. The primary advantage of standard-decimal arithmetic over standard-binary arithmetic in environments in which both are available is that no radix conversion is needed for decimal sending and receiving operands, and thus no associated precision loss occurs. D.14.2.1 Specification The ARITHMETIC IS STANDARD-DECIMAL clause in the options paragraph of the identification division specifies that the mode of arithmetic for the source unit is standard arithmetic.

25 April 2005

J4/05-0095
Page 7 of 10

D.14.2.2 Examples
These examples demonstrate a few of the features and requirements of standard-decimal arithmetic. In the following examples, “sbidi” stands for standard-binary intermediate data item, “sdidi” stands for standard-decimal intermediate data item, “ulp” is an abbreviation for “unit in last position”, and “ir-n” stands for the “nth intermediate result”. “Rounding” in the examples below implies the application of the MODE phrase of the INTERMEDIATE ROUNDING clause according to the rules of IEEE 754r. The application of the ROUNDED clause is explicitly described in the examples. 1 2 3 4 5 6 7 A B C D E F G pic s9(4) value +8000. pic s9(4) value +3000. pic s9(4) value –4001. pic s9(4) value is zero. pic s99V99 value is +56.79. usage float-binary-34. usage float-decimal-34.

COMPUTE D = A + B + C begin convert A convert B add ir-1, convert C add ir-1, move ir-1 end to sdidi as ir-1 to sdidi as ir-2 ir-2 with rounding, result in ir-1 to sdidi as ir-2 ir-2 with rounding, store in ir-1 to D (truncating fractional digits)

COMPUTE D ROUNDED = D + E begin convert D to sdidi as ir-1 convert E to sdidi as ir-2 add ir-1, ir-2 with rounding, store in ir-1 align decimal point of ir-1 to that of D adjust ulp according to DEFAULT ROUNDED MODE clause move adjusted ir-1 to D end COMPUTE F = A + B + C begin convert A convert B add ir-1, convert C add ir-1, to sdidi to sdidi ir-2 with to sdidi ir-2 with as ir-1 as ir-2 rounding, store in ir-1 as ir-2 rounding, store in ir-1

25 April 2005

J4/05-0095
Page 8 of 10
convert ir-1 to sbidi format with rounding as ir-2 move ir-2 to F (ir-2 and F are in the same format) end COMPUTE G ROUNDED = G + E begin retrieve G (it is already in sdidi format) as ir-1 convert E to sdidi as ir-2 add ir-1, ir-2 with rounding, store in ir-1 align decimal point of ir-1 to that of G adjust ulp according to DEFAULT ROUNDED MODE clause move adjusted ir-1 to D end

D.14.3 Standard-binary arithmetic Standard-binary arithmetic is provided as an alternative and as an adjunct to standard-decimal arithmetic, described at D.14.2, Standard-decimal arithmetic. Standard-binary arithmetic operations will produce results that are predictable, reasonable, and portable. In addition, the format of the intermediate results and the rounding rules associated with intermediate operations are defined in an external standard. This feature provides for arithmetic results that are not only identical from implementation to implementation but also are defined by an international standard intended for platform-independent implementation. Standard-binary arithmetic shares these advantages with standard-decimal arithmetic. However, the radix conversions required to convert decimal sending operands into standardbinary intermediate operand format, as well as those required to convert the intermediate values into standard-decimal intermediate data items intermediate values must be converted from binary to decimal form before they can be stored in decimal operands. Radix conversions are by nature often imprecise; they also introduce complexity into the algorithm that is absent when the calculations as well as the operands are decimal. In an environment like COBOL in which operands are so often in a decimal form, the results will be more accurate using standard-decimal arithmetic than with standard-binary, precisely because those radix conversions are unnecessary for those operands in the former case. It is also likely that these additional radix conversions will require execution-time resources that would not otherwise be needed. D.14.3.1 Specification The ARITHMETIC IS STANDARD-BINARY clause in the options paragraph of the identification division specifies that the mode of arithmetic for the source unit is standard arithmetic. D.14.3.2 Examples
These examples demonstrate a few of the features and requirements of standard-binary arithmetic.

25 April 2005

J4/05-0095
Page 9 of 10

In the following examples, “sbidi” stands for standard-binary intermediate data item, “sdidi” stands for standard-binary intermediate data item, “ulp” is an abbreviation for “unit in last position”, and “ir-n” stands for the “nth intermediate result”. “Rounding” in the examples below implies the application of the MODE phrase of the INTERMEDIATE ROUNDING clause according to the rules of the floating-point standard. The application of the ROUNDED clause is explicitly described in the examples. 8 9 10 11 12 13 14 A B C D E F G pic s9(4) value +8000. pic s9(4) value +3000. pic s9(4) value –4001. pic s9(4) value is zero. pic s99V99 value is +56.79. usage float-binary-34. usage float-decimal-34.

COMPUTE D = A + B + C begin convert A to sbidi as ir-1 convert B to sbidi as ir-2 add ir-1, ir-2 with rounding, store in ir-1 convert C to sbidi as ir-2 add ir-1, ir-2 with rounding, store in ir-1 Convert ir-1 to sdidi format with rounding as ir-2 move ir-2 to D (truncating fractional digits) end COMPUTE D ROUNDED = D + E begin convert D to sbidi as ir-1 convert E to sbidi as ir-2 add ir-1, ir-2 with rounding, store in ir-1 convert ir-1 to sdidi format with rounding as ir-2 align decimal point of ir-2 to that of D adjust ulp of ir-2 according to DEFAULT ROUNDED MODE clause move adjusted ir-2 to D end COMPUTE F = A + B + C begin convert A convert B add ir-1, convert C add ir-1, move ir-1 end to sbidi as ir-1 to sbidi as ir-2 ir-2 with rounding, store in ir-1 to sbidi as ir-2 ir-2 with rounding, store in ir-1 to F (ir-2 and F are in the same format)

25 April 2005

J4/05-0095
Page 10 of 10
COMPUTE G ROUNDED = G + E begin convert G from sdidi to sbidi format as ir-1 convert E to sbidi format as ir-2 add ir-1, ir-2 with rounding, store in ir-1 convert ir-1 from sbidi to sdidi format as ir-2 NOTE no rounding applies; ir-2 and G are in the same format move ir-1 to G end


				
DOCUMENT INFO
Shared By:
Tags: Arith, metic
Stats:
views:182
posted:11/29/2009
language:English
pages:10
Description: Arithmetic-Concepts