Comments on: Finding remainder for decimal division in AS400 free format Fri, 29 Apr 2016 20:51:11 +0000 hourly 1 By: philpl1jb Thu, 05 Aug 2010 02:23:24 +0000 #comment-80020 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.

By: tomliotta Tue, 03 Aug 2010 20:46:49 +0000 #comment-79969 %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.