compute rounded in cobol - cobol

I am confused with the rounded in the compute function in cobol.
Declaration:
VAR-A PIC S9(9)V99 COMP-3.
VAR-B PIC S9(9)V9(6) COMP-3.
Procedure.
MOVE +12.08 TO VAR-A.
MOVE +6.181657 TO VAR-B.
COMPUTE VAR-A ROUNDED = VAR-A + VAR-B.
Will the result of VAR-A be 18.27 or 18.26? What would cobol do upon computing?
Would it round VAR-B first to the decimal places specified in VAR-A or will cobol add the 2 variables then round them up to the decimal places specified in VAR-A?
Any help will be appreciated.
#NealB,
How about this example:
DECLARATION:
01 VAR-ARRAY OCCURS 22 TIMES.
03 VAR-A PIC S9(9)V9(6) COMP-3.
01 VAR-B PIC S9(9)V99 COMP-3.
Supposing VAR-A is an array, and the following are its values:
VAR-A(01) = 123.164612
VAR-A(02) = 12.07865
VAR-A(03) = 6.181657
VAR-A(04) = 1.744353
VAR-A(05) = 6.118182
VAR-A(06) = 1.744353
VAR-A(07) = 6.158715
VAR-A(08) = 1.744353
VAR-A(09) = 6.194759
VAR-A(10) = 1.744353
VAR-A(11) = 3.037896
VAR-A(12) = 1.743852
VAR-A(13) = 6.14653
VAR-A(14) = 1.744353
VAR-A(15) = 0.000377
VAR-A(16) = 1.743852
VAR-A(17) = 6.144363
VAR-A(18) = 1.743852
VAR-A(19) = 0.007649
VAR-A(20) = 1.744353
VAR-A(21) = 0.000377
VAR-A(22) = 1.744353
VAR-B's value is:
VAR-B = 405.25
PROCEDURE:
PERFORM VAR-IDX FROM 1 BY 1 UNTIL VAR-IDX > 22
COMPUTE VAR-B ROUNDED = VAR-B + VAR-A(VAR-IDX)
END-PERFORM.
Why do I get 597.87 for VAR-B as a result after the computation?

I believe the default COBOL rounding behaviour is: Nearest away from zero.
COMPUTE VAR-A ROUNDED = VAR-A + VAR-B
should result in VAR-A containing 18.26
Rounding occurs after the expression has been evaluated. A more interesting example might be:
01 VAR-A PIC S9(9)V99 COMP-3.
01 VAR-B PIC S9(9)V9(6) COMP-3.
01 VAR-C PIC S9(9)V9(6) COMP-3.
MOVE +12.08 TO VAR-A.
MOVE +06.182000 TO VAR-B.
MOVE +00.004000 TO VAR-C.
COMPUTE VAR-A ROUNDED = VAR-A + VAR-B + VAR-C.
The result is 18.27. Rounding VAR-B and VAR-C to 2 decimal places before doing the addition would have yielded 18.26 because VAR-B rounds to 6.18 and VAR-C rounds to 0.00. The result is actually 18.27 so the rounding occurs after evaluation of the expression.
Reply to edited question
Not pretty output, but this is how my calculation goes using IBM Enterprise COBOL for z/OS
VAR-IDX = 01 VAR-B = +405.25 VAR-A = +123.164612 VAR-B + VAR-A = +528.41
VAR-IDX = 02 VAR-B = +528.41 VAR-A = +012.078650 VAR-B + VAR-A = +540.49
VAR-IDX = 03 VAR-B = +540.49 VAR-A = +006.181657 VAR-B + VAR-A = +546.67
VAR-IDX = 04 VAR-B = +546.67 VAR-A = +001.744353 VAR-B + VAR-A = +548.41
VAR-IDX = 05 VAR-B = +548.41 VAR-A = +006.118182 VAR-B + VAR-A = +554.53
VAR-IDX = 06 VAR-B = +554.53 VAR-A = +001.744353 VAR-B + VAR-A = +556.27
VAR-IDX = 07 VAR-B = +556.27 VAR-A = +006.158715 VAR-B + VAR-A = +562.43
VAR-IDX = 08 VAR-B = +562.43 VAR-A = +001.744353 VAR-B + VAR-A = +564.17
VAR-IDX = 09 VAR-B = +564.17 VAR-A = +006.194759 VAR-B + VAR-A = +570.36
VAR-IDX = 10 VAR-B = +570.36 VAR-A = +001.744353 VAR-B + VAR-A = +572.10
VAR-IDX = 11 VAR-B = +572.10 VAR-A = +003.037896 VAR-B + VAR-A = +575.14
VAR-IDX = 12 VAR-B = +575.14 VAR-A = +001.743852 VAR-B + VAR-A = +576.88
VAR-IDX = 13 VAR-B = +576.88 VAR-A = +006.146530 VAR-B + VAR-A = +583.03
VAR-IDX = 14 VAR-B = +583.03 VAR-A = +001.744353 VAR-B + VAR-A = +584.77
VAR-IDX = 15 VAR-B = +584.77 VAR-A = +000.000377 VAR-B + VAR-A = +584.77
VAR-IDX = 16 VAR-B = +584.77 VAR-A = +001.743852 VAR-B + VAR-A = +586.51
VAR-IDX = 17 VAR-B = +586.51 VAR-A = +006.144363 VAR-B + VAR-A = +592.65
VAR-IDX = 18 VAR-B = +592.65 VAR-A = +001.743852 VAR-B + VAR-A = +594.39
VAR-IDX = 19 VAR-B = +594.39 VAR-A = +000.007649 VAR-B + VAR-A = +594.40
VAR-IDX = 20 VAR-B = +594.40 VAR-A = +001.744353 VAR-B + VAR-A = +596.14
VAR-IDX = 21 VAR-B = +596.14 VAR-A = +000.000377 VAR-B + VAR-A = +596.14
VAR-IDX = 22 VAR-B = +596.14 VAR-A = +001.744353 VAR-B + VAR-A = +597.88
FINAL RESULT = +597.88

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.

IDENTIFICATION DIVISION.
PROGRAM-ID. HELLO.
DATA DIVISION.
WORKING-STORAGE SECTION.
01 VAR_NUM PIC 9(3)V9(02).
01 VAR_RESULT PIC 9(3).
PROCEDURE DIVISION.
MOVE 256.50 TO VAR_NUM.
COMPUTE VAR_NUM ROUNDED = VAR_NUM / 100.
MULTIPLY 100 BY VAR_NUM
MOVE VAR_NUM TO VAR_RESULT.
DISPLAY "Result : " VAR_RESULT.
STOP RUN.

Related

How to convert c++ code to cobol correctly

I have written code to get the number that appears the most in an array. The main function will make a call to readData which will read data into the array that I passed to the readData subroutine. After the readData subroutine has executed, i use the mode function to find the number that appears most in an array.
Example array: |1|2|2|1|1|
Output generated: 1
My c++ code is as follows:
#include <iostream>
using namespace std;
void mode(int array[]){
int max = 0, num;
int count;
for(int i=0; i<5; ++i){
count = 0;
for(int j=0; j<5; ++j){
if(array[i] == array[j]){
++count;
}
if(max < count){
max = count;
num = array[i];
}
}
}
cout << num << endl;
}
void arrayData(int array[]){
for(int i=0; i<5; i++){
cin >> array[i];
}
mode(array);
}
int main()
{
int array[5];
arrayData(array);
return 0;
}
I would like to convert the above c++ code to cobol, I am using gnuCobol. The code i have generated so far is bellow.
IDENTIFICATION DIVISION.
PROGRAM-ID. Main.
DATA DIVISION.
WORKING-STORAGE SECTION.
01 array.
05 val PIC X(1) OCCURS 5 TIMES.
PROCEDURE DIVISION.
CALL 'READDATA' USING array.
STOP RUN.
*> readData: fills the array with user diffined digits
IDENTIFICATION DIVISION.
PROGRAM-ID. READDATA.
WORKING-STORAGE SECTION. ***> error appears here**
01 array.
05 val PIC X(1) OCCURS 5 TIMES.
PROCEDURE DIVISION.
PERFORM VARYING I FROM 1 BY 1
UNTIL I > 5
SET I TO 1
ACCEPT array(I) FROM SYSIN.
SET I UP BY 1
END-PERFORM.
CALL 'MODE' USING array.
EXIT PROGRAM.
*> mode: gets the number that appears most in the array
IDENTIFICATION DIVISION.
PROGRAM-ID. MODE.
WORKING-STORAGE SECTION.
01 array.
05 val PIC X(1) OCCURS 5 TIMES.
01 maxv PIC(1) VALUE 0.
01 counter PIC(1).
01 num PIC(1).
PROCEDURE DIVISION.
PERFORM VARYING I FROM 1 BY 1
UNTIL I > 5
MOVE 0 TO counter
PERFORM VARYING J FROM 1 BY 1
UNTIL J > 5
IF array(I) = array(I) THEN
SET counter UP BY 1
END-IF
IF maxv < counter THEN
MOVE counter TO maxv
MOVE array(I) TO num
END-IF
SET J UP BY 1
END-PERFORM.
SET I UP BY 1
END-PERFORM.
DISPLAY "Mode: "num.
EXIT PROGRAM.
I am getting the following error:
Error: syntax error, unexpected "WORKING-STORAGE", expecting "END PROGRAM" or "PROGRAM-ID"
There were several issues with the code. The reported error appears to have been the absence of an END PROGRAM statement to separate the first program from the second.
Other errors include:
Using SET statements unnecessarily
Passing the array without a LINKAGE SECTION or USING phrase
Missing DATA DIVISION statements
Missing data items
Improper subscripting
Including a "separator period" before a scope terminator
Invalid PICTURE clauses
These issues are shown in comments in the following code. Compare the original code with the modified code.
Modified code:
IDENTIFICATION DIVISION.
PROGRAM-ID. Main.
DATA DIVISION.
WORKING-STORAGE SECTION.
01 array.
05 val PIC X(1) OCCURS 5 TIMES.
PROCEDURE DIVISION.
CALL 'READDATA' USING array.
STOP RUN.
END PROGRAM MAIN. *> ADDED
*> readData: fills the array with user diffined digits
IDENTIFICATION DIVISION.
PROGRAM-ID. READDATA.
DATA DIVISION. *> ADDED
WORKING-STORAGE SECTION. *> error appears here**
01 I PIC 9.
LINKAGE SECTION. *> ADDED
01 array.
05 val PIC X(1) OCCURS 5 TIMES.
PROCEDURE DIVISION USING ARRAY. *> MODIFIED
PERFORM VARYING I FROM 1 BY 1
UNTIL I > 5
*> SET I TO 1 *> REMOVED
ACCEPT VAL (I) FROM SYSIN *> MODIFIED
*> SET I UP BY 1 *> REMOVED
END-PERFORM.
CALL 'MO-DE' USING array.
EXIT PROGRAM.
END PROGRAM READDATA. *> ADDED
*> mode: gets the number that appears most in the array
IDENTIFICATION DIVISION.
PROGRAM-ID. MO-DE. *> 'MODE' IS A RESERVED WORD
DATA DIVISION. *> ADDED
WORKING-STORAGE SECTION.
01 maxv PIC 9 VALUE 0. *> MODIFIED
01 counter PIC 9. *> MODIFIED
01 num PIC 9. *> MODIFIED
01 I PIC 9. *> ADDED
01 J PIC 9. *> ADDED
LINKAGE SECTION. *> ADDED
01 array.
05 val PIC X(1) OCCURS 5 TIMES.
PROCEDURE DIVISION USING ARRAY. *> MODIFIED
PERFORM VARYING I FROM 1 BY 1
UNTIL I > 5
MOVE 0 TO counter
PERFORM VARYING J FROM 1 BY 1
UNTIL J > 5
IF VAL (I) = VAL (J) THEN *> MODIFIED
ADD 1 TO COUNTER *> MODIFIED
END-IF
IF maxv < counter THEN
MOVE counter TO maxv
MOVE VAL (I) TO num
END-IF
*> SET J UP BY 1 *> REMOVED
END-PERFORM *> MODIFIED
*> SET I UP BY 1 *> REMOVED
END-PERFORM.
DISPLAY "Mode: "num.
EXIT PROGRAM.
END PROGRAM MO-DE. *> ADDED
Input:
1
5
5
2
3
Output:
Mode: 5

How much value do the 8 bit variable holds?

Probably answer is 256 but I am not satisfied with it.
Suppose a variable has 8 bits , its mean its 8th bit can hold the value 256 . But it also has other seven bits . Wouldn't the total value be the sum of all bits?
To me final value that 8 bit variable holds would be the sum of all bits. But it doesn't. Why?
The max value 8 bits can hold is: 11111111 which is equal to 255. If you have a signed value, the max value it can hold is 127, the left-most bit is used for sign.
The binary 10000000 equals 128 (2 ^ 7), not 256. That's where your confusion lays I think.
00000001 = 2 ^ 0 = 1
00000010 = 2 ^ 1 = 2
00000100 = 2 ^ 2 = 4
00001000 = 2 ^ 3 = 8
00010000 = 2 ^ 4 = 16
00100000 = 2 ^ 5 = 32
01000000 = 2 ^ 6 = 64
10000000 = 2 ^ 7 = 128
The value is indeed the sum of all bits set to 1, but the place value of the eighth bit is 27 (128), not 256 as you suggest - the least significant bit is 20 (i.e. 1), so for eight bits the MSB is 27. You appear to have started from 21 (2) .
For an unsigned integer:
Bit 0 = 20 = 1
Bit 1 = 21 = 2
Bit 2 = 22 = 4
Bit 3 = 23 = 8
Bit 4 = 24 = 16
Bit 5 = 25 = 32
Bit 6 = 26 = 64
Bit 7 = 27 = 128
Sum of all ones = 255 - not 256 as you suggest: 0 to 255 = 28 (256) values.
For a two's complement signed 8 bit type:
Bit 7 = -27 = -128
Sum of all ones = -1,
while if Bit 8 = 0, sum = +127,
and all zeros except bit 8 = -128.
(-128 to +127 = 28 (256) values).
Either way an 8 bit integer signed or otherwise has 28 (256) possible bit patterns.

Decomposion a 1D signal into differnt sub band using wavelet

I am working on wavelet and I am new in this field.I want to decompose a signal into multiple band.So I use wavedec() to decompose a signal into 5 level and use wrcoef() to reconstruct individual band.But problem is that when I sum 5 band then reconstruct signal is more differ than Original signal.
plz any body help me about this.
Here my code..
load sumsin; s = sumsin;
figure;plot(s);
% Perform decomposition at level 5 of s using sym4.
[c,l] = wavedec(s,5,'sym4');
% Reconstruct approximation at level 5,
% from the wavelet decomposition structure [c,l].
a1= wrcoef('a',c,l,'sym4',1);
a2 = wrcoef('a',c,l,'sym4',2);
a3 = wrcoef('a',c,l,'sym4',3);
a4 = wrcoef('a',c,l,'sym4',4);
a5 = wrcoef('a',c,l,'sym4',5);
figure; subplot(5,1,1); plot(a1); title('Approximation at level 1');
subplot(5,1,2); plot(a2); title('Approximation at level 2');
subplot(5,1,3); plot(a3); title('Approximation at level 3');
subplot(5,1,4); plot(a4); title('Approximation at level 4');
subplot(5,1,5); plot(a5); title('Approximation at level 5');
figure;plot(a1+a2+a3+a4+a5);title('Reconstruct Original signal');
To reconstruct the original signal you need to sum together five detailed components and the approximation component at the last, fifth, level.
d1= wrcoef('d',c,l,'sym4',1);
d2 = wrcoef('d',c,l,'sym4',2);
d3 = wrcoef('d',c,l,'sym4',3);
d4 = wrcoef('d',c,l,'sym4',4);
d5 = wrcoef('d',c,l,'sym4',5);
a5 = wrcoef('a',c,l,'sym4',5);
s_origin=d1+d2+d3+d4+d5+a5;

Getting value using percent and rounding down

I have a few values
Quantity := 5;
Quantity2 := 8;
percent :=50;
so i want
Percent of Quantity + Quantity 2
which would be like : 50% of 13 = 6.5
I done it like this
HowMuchDamage := trunc(percent*(Quantity + Quantity2)/100);
How can i make it round up?
How can i make it round down?
Floor(X) returns the highest integer less than or equal to X.
Ceil(X) returns the lowest integer greater than or equal to X.

SET A, 0x1E vs SET A, 0x1F

This is my first attempt at dpcu, I'm checking machine code generated by dpcu-16 assembly
I am using this emulator : http://dcpu.ru/
I am trying to compare code generated by
SET A, 0x1E
SET A, 0x1F
code generated is as follow :
fc01
7c01 001f
I don't get why operand size changes between those two values
That emulator appears to be using the next version of the DCPU-16 spec, which specifies that the same-word literal value for a permits values from 0xFFFF (-1) to 0x1E (30). This means that to get any literal value outside this range the assembler has to use the next-word literal syntax, which makes the operand one byte bigger.
0x1F (dec:31) is no longer a short literal (values -1 to 30), so it has to be read as a "next word" argument.
The opcodes are thus:
SET A, 0x1E
SET = 00001
A = 00000
1E = 111111
op = 1111110000000001 = fc01
SET A, 0x1F
SET = 00001
A = 00000
NW = 011111
op = 0111110000000001 = 7c01 + 001f

Resources