Hi,
I have come across a problem while coding in RPG free format.
I need to find the remainder of a decimal division. Consider an example below
D Premium S 11 2 Inz( 1230.10) D Instalments S 2 0 Inz(12) D Remainder S 11 2
how do I calculate the remainder when I divide Premium/Instalments.
%REM is accepting only interger values.
Can you please throw me some ideas if there is any way to use BIFs?
Software/Hardware used:
AS400
ASKED:
August 3, 2010 4:21 AM
UPDATED:
August 5, 2010 2:23 AM
%REM is accepting only interger values.
…Which is as it should do. Note that the related %DIV() BIF is also an integer function.
While the MVR op-code would ‘work’ with decimal fractions, you should understand that it is a relatively expensive arithmetic operation. Division of decimal fractions at the binary/bit level is imprecise. Decimal fractions generally do not have precise binary equivalents. Under the covers, the same thing happens with the DIV and MVR op-codes as Phil recommends in his Answer. The values are adjusted to be integers in order to preserve accurate “remainders”.
This is a part of the fundamental incompatibility between the decimal fractions that we (humans) use and the binary representations that computers use.
Try expressing a simple “.3″ as a binary fraction for example. That might give you an idea of the difficulties involved.
It might appear as if you are drastically slowing down your calculations, but it really doesn’t make that much difference when you need to capture a fractional remainder.
Ideally, you would use redefined variables at the start, and eliminate the need for the “multiply both factors by 100, etc.” piece.
Tom
ohhhhh..that was a senior moment — parameters need to be integer .. guess I had a flash of fmod — from the C math stuff which you could use in RPGLE.
But, to finally answer your question.
Remainder = %REM(%INT(Premium) : %INT(Instalments))
or if you need remainder to 2 decimals
Remainder = %REM(%INT(Premium*100) : %INT( Instalments *100))/100
Yes, that will burn a few cycles, but what are a few cycles between friends.
The concept that modulus should be based on integers appears to date to the early 1800;s.
Phil