Reputation: 2984
I'm currently trying to divide a PIC
number by 100 and then put the whole result (including the numbers after the floating point) into a fixed decimal (16,2)
.
My problem is, I'm not getting it to work and I only get 0 (instead 0.25 as example) if the result is not >=1.
DCL finalresult DEC FIXED (16,2);
DCL mynumber PIC '999999999999999' INIT(0);
mynumber = 50;
finalresult = mynumber / 100; <-- results in 0
finalresult = ((1.0 * mynumber) / 100.0) <-- results in 0
So how can I truly do this division and get the correct result? (0.5 in this case)
Upvotes: 0
Views: 519
Reputation: 11911
Typing of intermediate results is a complex topic which has several pitfalls which may in part depend on compiler options and some limits that might appear rather unexpected.
You can look up the data type of intermediate results for arithmetic operations here. As a diagnostic aid you can also (since Enterprise PL/I V5) use the DECOMP
compiler-option to show the types of all intermediate results in the compiler listing.
So let's have a look at your special case:
For purposes of this you can treat your mynumber
as FIXED(15,0)
, the literal 100
is a FIXED(3,0)
. By the linked table the result of the division has a total of N digits and a scale of N-p1+q1-q2= N-15+0-0 decimal places. So what is N? The documentation says it's "the maximum precision for fixed decimal" - and that depends on the LIMITS(FIXEDDEC)
compiler-option. By default N is 15 so your result has 15-15=0 decimal places and 0.5 is truncated to 0.
In the second case the result of 1.0 * mynumber
has a precision of 1+p1+p2=1+2+15=17 and a scale of q1+q2 = 1+0 = 1 decimal places but because of the FIXEDDEC-Limit the precision is reduced to 15. So as above the division results in 0 decimal places.
So how to fix this? Either specify LIMITS(FIXEDDEC(31))
as compiler-option or reduce the number of digits in mynumber
to something below 15.
P.S.: the "floating point" you mention is not floating at all, it is a fixed decimal point (as opposed to java or C PL/I does distinguish between floating-point- and fixed-point-arithmetic). To get a floating point value you have to either declare a variable as FLOAT
(DEC
or BIN
) or use exponential notation on literals.
Upvotes: 1