634 pág.

# IBM - VisualAge Cobol for Windows Programming Guide

Pré-visualização50 páginas

Ave-Tax = Function Mean (Tax-S Tax-T Tax-W Tax-B) Compute Median-Tax = Function Median (Tax-S Tax-T Tax-W Tax-B) Compute Tax-Range = Function Range (Tax-S Tax-T Tax-W Tax-B) Fixed-point versus floating-point arithmetic Many statements in your program could involve arithmetic. For example, each of the following types of COBOL statements requires some arithmetic evaluation: v General arithmetic compute report-matrix-col = (emp-count ** .5) + 1 add report-matrix-min to report-matrix-max giving report-matrix-tot v Expressions and functions compute report-matrix-col = function sqrt(emp-count) + 1 compute whole-hours = function integer-part((average-hours) + 1) v Arithmetic comparisons if report-matrix-col < function sqrt(emp-count) + 1 if whole-hours not = function integer-part((average-hours) + 1) How you code arithmetic in your program (whether an arithmetic statement, an intrinsic function, an expression, or some combination of these nested within each other) determines whether the evaluation is in floating-point or fixed-point arithmetic. Floating-point evaluations In general, if your arithmetic coding has either of the characteristics listed below, it is evaluated in floating-point arithmetic: v An operand or result field is floating point. A data item is floating point if you code it as a floating-point literal or if you define it as USAGE COMP-1, USAGE COMP-2, or external floating point (USAGE DISPLAY with a floating-point PICTURE). An operand that is a nested arithmetic expression or a reference to a numeric intrinsic function results in floating-point arithmetic when any of the following is true: \u2013 An argument in an arithmetic expression results in floating point. \u2013 The function is a floating-point function. \u2013 The function is a mixed function with one or more floating-point arguments. v An exponent contains decimal places. An exponent contains decimal places if you use a literal that contains decimal places, give the item a PICTURE containing decimal places, or use an arithmetic expression or function whose result has decimal places. Chapter 3. Working with numbers and arithmetic 45 An arithmetic expression or numeric function yields a result with decimal places if any operand or argument (excluding divisors and exponents) has decimal places. Fixed-point evaluations In general, if an arithmetic operation contains neither of the characteristics listed above for floating point, the compiler will cause it to be evaluated in fixed-point arithmetic. In other words, arithmetic evaluations are handled as fixed point only if all the operands are fixed point, the result field is defined to be fixed point, and none of the exponents represent values with decimal places. Nested arithmetic expressions and function references must also represent fixed-point values. Arithmetic comparisons (relation conditions) When you compare numeric expressions using a relational operator, the numeric expressions (whether they are data items, arithmetic expressions, function references, or some combination of these) are comparands in the context of the entire evaluation. That is, the attributes of each can influence the evaluation of the other: both expressions are evaluated in fixed point, or both are evaluated in floating point. This is also true of abbreviated comparisons even though one comparand does not explicitly appear in the comparison. For example: if (a + d) = (b + e) and c This statement has two comparisons: (a + d) = (b + e), and (a + d) = c. Although (a + d) does not explicitly appear in the second comparison, it is a comparand in that comparison. Therefore, the attributes of c can influence the evaluation of (a + d). The compiler handles comparisons (and the evaluation of any arithmetic expressions nested in comparisons) in floating-point arithmetic if either comparand is a floating-point value or resolves to a floating-point value. The compiler handles comparisons (and the evaluation of any arithmetic expressions nested in comparisons) in fixed-point arithmetic if both comparands are fixed-point values or resolve to fixed-point values. Implicit comparisons (no relational operator used) are not handled as a unit, however; the two comparands are treated separately as to their evaluation in floating-point or fixed-point arithmetic. In the following example, five arithmetic expressions are evaluated independently of one another\u2019s attributes, and then are compared to each other. evaluate (a + d) when (b + e) thru c when (f / g) thru (h * i) . . . end-evaluate \u201cExamples: fixed-point and floating-point evaluations\u201d RELATED REFERENCES \u201cArithmetic expressions in nonarithmetic statements\u201d on page 488 Examples: fixed-point and floating-point evaluations Assume you define the data items for an employee table in the following manner: 01 employee-table. 05 emp-count pic 9(4). 05 employee-record occurs 1 to 1000 times 46 Programming Guide depending on emp-count. 10 hours pic +9(5)e+99. . . . 01 report-matrix-col pic 9(3). 01 report-matrix-min pic 9(3). 01 report-matrix-max pic 9(3). 01 report-matrix-tot pic 9(3). 01 average-hours pic 9(3)v9. 01 whole-hours pic 9(4). These statements are evaluated using floating-point arithmetic: compute report-matrix-col = (emp-count ** .5) + 1 compute report-matrix-col = function sqrt(emp-count) + 1 if report-matrix-tot < function sqrt(emp-count) + 1 These statements are evaluated using fixed-point arithmetic: add report-matrix-min to report-matrix-max giving report-matrix-tot compute report-matrix-max = function max(report-matrix-max report-matrix-tot) if whole-hours not = function integer-part((average-hours) + 1) Using currency signs Many programs need to process financial information and present that information using the appropriate currency signs. With COBOL currency support (and the appropriate code page for your printer or display unit), you can use one or more of the following signs in a program: v Symbols such as the dollar sign ($) v Currency signs of more than one character (such as USD, DEM, EUR) v Euro sign, established by the Economic and Monetary Union (EMU) To specify the symbols for displaying financial information, use the CURRENCY SIGN clause (in the SPECIAL-NAMES paragraph in the CONFIGURATION SECTION) with the PICTURE characters that relate to the symbols. In the following example, the PICTURE character $ indicates that the currency sign $US is to be used: Currency Sign is \u201c$US\u201d with Picture Symbol \u201c$\u201d. . . . 77 Invoice-Amount Pic $$,$$9.99. . . . Display \u201cInvoice amount is \u201d Invoice-Amount. In this example, if Invoice-Amount contained 1500.00, the display output would be: Invoice amount is $US1,500.00 By using more than one CURRENCY SIGN clause in your program, you can allow for multiple currency signs to be displayed. You can use a hexadecimal literal to indicate the currency sign value. This could be useful if the data-entry method for the source program does not allow the entry of the intended characters easily. The following example shows the hex value X\u2019D5\u2019 used as the currency sign: Currency Sign X'D5' with Picture Symbol 'U'. . . . 01 Deposit-Amount Pic UUUUU9.99. If there is no corresponding character for the euro sign on your keyboard, you need to specify it as a hexadecimal value in the CURRENCY SIGN clause. Chapter 3. Working with numbers and arithmetic 47 The hexadecimal value for the euro sign is either X\u201980\u2019 or X\u201988\u2019 depending on the code page in use, as shown in the following table. Code page Euro sign IBM-1250 (Latin 2) X\u201980\u2019 IBM-1251 (Cyrillic) X\u201988\u2019 IBM-1252 (Latin 1) X\u201980\u2019 IBM-1253 (Greek) X\u201980\u2019 IBM-1254 (Turkish) X\u201980\u2019 IBM-1255 (Hebrew) X\u201980\u2019 IBM-1256 (Arabic) X\u201980\u2019 IBM-1257 (Baltic) X\u201980\u2019 IBM-1258 (Vietnamese) X\u201980\u2019 IBM-874 (Thai) X\u201980\u2019 \u201cExample: multiple currency signs\u201d Example: