Author: Bill Woodger
Subject: Reply to: ROUNDED Problem with COMPUTE statement
Posted: Thu Feb 09, 2017 10:27 pm (GMT 5.5)
Terry, you have two surplus decimal places (surplus to your final result). shalem could achieve the same in the COMPUTE by adding two redundant decimal-places to his fields :-)
shalem,
Simply rearrange your COMPUTE to do the multiplication first, and the division last.
Work your example through, existing COMPUTE and rearranged COMPUTE, with the Appendix Terry suggested earlier, and you'll see the difference in action.
If you want to split it up, that is fine, but you have to look after sizes yourself (because what is otherwise an intermediate value, you have to save for the next stage).,
You either have to make you intermediate 100 times larger than the source field (MULTIPLY first), or have the two excess decimal places (DIVIDE first).
Using COMPUTE you need neither excess size nor excess decimal places. You just have to do things in the correct order. In COPUTE, multiply, then divide, will get your result.
is what you want.
Algebraically that is the same as 100 * ( A / B ). It just isn't the same, because the intermediates are not like a calculator.
A = 33
B = 70
A / B = 0.47142857142857142857142857142857 with a calculator. In the intermediates (with ROUNDED you get an extra decimal place, which is how the compiler will know to round or not) it is 0.471.
When you multiply that by 100, you get 47.100, and the ROUNDED operates on the low-order digit, which is 100% guaranteed to always be zero (because of the multiply).
Multiply first, divide last, and the precision is preserved for the correct rounding.
Subject: Reply to: ROUNDED Problem with COMPUTE statement
Posted: Thu Feb 09, 2017 10:27 pm (GMT 5.5)
Terry, you have two surplus decimal places (surplus to your final result). shalem could achieve the same in the COMPUTE by adding two redundant decimal-places to his fields :-)
shalem,
Simply rearrange your COMPUTE to do the multiplication first, and the division last.
Work your example through, existing COMPUTE and rearranged COMPUTE, with the Appendix Terry suggested earlier, and you'll see the difference in action.
If you want to split it up, that is fine, but you have to look after sizes yourself (because what is otherwise an intermediate value, you have to save for the next stage).,
You either have to make you intermediate 100 times larger than the source field (MULTIPLY first), or have the two excess decimal places (DIVIDE first).
Using COMPUTE you need neither excess size nor excess decimal places. You just have to do things in the correct order. In COPUTE, multiply, then divide, will get your result.
Code: |
COMPUTE C = ( 100 * A ) / B |
is what you want.
Algebraically that is the same as 100 * ( A / B ). It just isn't the same, because the intermediates are not like a calculator.
A = 33
B = 70
A / B = 0.47142857142857142857142857142857 with a calculator. In the intermediates (with ROUNDED you get an extra decimal place, which is how the compiler will know to round or not) it is 0.471.
When you multiply that by 100, you get 47.100, and the ROUNDED operates on the low-order digit, which is 100% guaranteed to always be zero (because of the multiply).
Multiply first, divide last, and the precision is preserved for the correct rounding.