I came across a requirement to display comp-2 variables as external float. I used the edited picture clause to display the values. It works fine for values with small decimals. But if the value has more decimal values COBOL is rounding them.
I used the edit mask -9.9(14)E+99. IF the value is something like 1.23456999999999E+03 then it is getting converted to 1.23457000000000E+03.
Does COBOL rounds them by default? If so, could someone suggest me with any other alternative with SYNCSORT or Easytrieve?
Joined: 06 Jun 2008 Posts: 8696 Location: Dubuque, Iowa, USA
One thing you DEFINITELY need to be aware of is that different software products have different ways to handle floating point values. The different ways are not necessarily better -- or worse -- merely different. Assuming that COBOL is not right merely because File Manager gives you a different value is just as wrong as assuming that File Manager is not right because COBOL gives you a different value.
Also, some values, such as 0.1, cannot be expressed exactly as a binary value without using an infinite number of binary digits and hence such values will be an approximation when expressed as a floating point number on a computer. Hence for some values there is no way to avoid rounding them since they cannot be exactly expressed.
There are many valid reasons to use floating point variables. Expecting to be able to get precise numbers, however, is NOT one of those reasons.
This is what I found..... Take a break and see if you get an answer
Code:
It depends on the intermediate rounding and the final rounding set.
see this for more info :
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 in receiving data items.
There are eight different forms of rounding supported by this standard:
• 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”.
• NEAREST-TOWARD-ZERO: Rounding is to the nearest value. If two values are equally near, the value with the smaller magnitude is selected.
• PROHIBITED: Since 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.
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 or arithmetic expression, and during the execution of arithmetic operators 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.
In the event the value cannot be exactly represented, the user may also 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:
• INTERMEDIATE ROUNDING IS NEAREST-AWAY-FROM-ZERO • INTERMEDIATE ROUNDING IS NEAREST-EVEN • INTERMEDIATE ROUNDING IS PROHIBITED • INTERMEDIATE ROUNDING IS TRUNCATION
for which the subclause descriptions are found in D.13a, Rounding.
If the INTERMEDIATE ROUNDING clause is not specified, INTERMEDIATE ROUNDING IS TRUNCATION is presumed. This is unchanged from previous standards.
D.13a.2 Final rounding (the ROUNDED clause)
Final rounding applies to the formation of the final result of the expression or statement, at the completion of evaluation of the statement or expression, 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); and rounding to the nearest values, and if two values were equally near, choose the value with the larger magnitude (signaled by the presence of the ROUNDED clause).
The ROUNDED clause has been enhanced to allow explicit selection of any of eight modes of rounding (including the two previously available):
• ROUNDED MODE IS AWAY-FROM-ZERO • ROUNDED MODE IS NEAREST-AWAY-FROM-ZERO • ROUNDED MODE IS NEAREST-EVEN • ROUNDED MODE IS NEAREST-TOWARD-ZERO • ROUNDED MODE IS PROHIBITED • ROUNDED-MODE IS TOWARD-GREATER • ROUNDED MODE IS TOWARD-LESSER • ROUNDED MODE IS TRUNCATION
If the ROUNDED clause is not present for a given result, the rules for ROUNDED MODE IS TRUNCATION apply.
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 to any operation for which the ROUNDED clause appears without the MODE IS subclause.
The DEFAULT ROUNDED MODE clause may take any of these forms:
• DEFAULT ROUNDED MODE IS AWAY-FROM-ZERO • DEFAULT ROUNDED MODE IS NEAREST-AWAY-FROM-ZERO • DEFAULT ROUNDED MODE IS NEAREST-EVEN • DEFAULT ROUNDED MODE IS NEAREST-TOWARD-ZERO • DEFAULT ROUNDED MODE IS PROHIBITED • DEFAULT ROUNDED MODE IS TOWARD-GREATER • DEFAULT ROUNDED MODE IS TOWARD-LESSER • DEFAULT ROUNDED MODE IS TRUNCATION
for which the subclauses of the DEFAULT ROUNDED MODE is clause are described in D.13a, Rounding.
If the DEFAULT ROUNDED MODE clause does not appear in the program, the effect of the ROUNDED clause without the MODE IS subclause is as if ROUNDED MODE IS NEAREST AWAY FROM ZERO had been specified. This provides the same functionality available in prior COBOL standards.
If the DEFAULT ROUNDED MODE clause appears, ROUNDED clauses without the MODE IS subclause are treated as if they had been specified with the rounding mode specified in the DEFAULT ROUNDED MODE clause.