View previous topic :: :: View next topic

Author 
Message 
sijayapal
New User
Joined: 23 Dec 2008 Posts: 69 Location: India




Hello,
I came across a requirement to display comp2 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?
Thanks,
Siva. 

Back to top 




Bill Woodger
DFSORT Moderator
Joined: 09 Mar 2011 Posts: 7313




Why do you think 1.23456999999999E+03 is your value?
To see what is happening, I'd suggest compiler option ARITH(EXTEND) and make your edit 9V29E+99. 

Back to top 


sijayapal
New User
Joined: 23 Dec 2008 Posts: 69 Location: India




Quote: 
Why do you think 1.23456999999999E+03 is your value? 
That is what displayed in file manager when i browse the file with copybook layout.
Your question makes me to think if file manager is right?
I should try to decode the hex value to see what i get.
Thanks 

Back to top 


Robert Sample
Global Moderator
Joined: 06 Jun 2008 Posts: 8027 Location: Bellevue, IA




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. 

Back to top 


Rohit Umarjikar
Senior Member
Joined: 21 Sep 2010 Posts: 1674 Location: NY,USA




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:
• AWAYFROMZERO: Rounding is to the nearest value of larger magnitude.
• NEARESTAWAYFROMZERO: 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.
• NEARESTEVEN: 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”.
• NEARESTTOWARDZERO: 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 ECSIZETRUNCATION condition is set to exist and the results of the operation are undefined.
• TOWARDGREATER: Rounding is toward the nearest value whose algebraic value is larger.
• TOWARDLESSER: 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 StandardBinary and StandardDecimal 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 NEARESTAWAYFROMZERO • INTERMEDIATE ROUNDING IS NEARESTEVEN • 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 AWAYFROMZERO • ROUNDED MODE IS NEARESTAWAYFROMZERO • ROUNDED MODE IS NEARESTEVEN • ROUNDED MODE IS NEARESTTOWARDZERO • ROUNDED MODE IS PROHIBITED • ROUNDEDMODE IS TOWARDGREATER • ROUNDED MODE IS TOWARDLESSER • 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 AWAYFROMZERO • DEFAULT ROUNDED MODE IS NEARESTAWAYFROMZERO • DEFAULT ROUNDED MODE IS NEARESTEVEN • DEFAULT ROUNDED MODE IS NEARESTTOWARDZERO • DEFAULT ROUNDED MODE IS PROHIBITED • DEFAULT ROUNDED MODE IS TOWARDGREATER • DEFAULT ROUNDED MODE IS TOWARDLESSER • 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. 


Back to top 


Bill Woodger
DFSORT Moderator
Joined: 09 Mar 2011 Posts: 7313




Rohit,
That's for COBOL itself, I think first from the 2002 "Standard", now replaced by 2014.
Enterprise COBOL is to the 1985 Standard, with IBM Extensions, including things from 2002 (but not all) and some of their own things.
Rounding is only ever done when ROUNDED is explicitly specified. 

Back to top 



