Finding remainder for decimal division in AS400 free format

5 pts.
Tags:
AS/400
AS/400 administration
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

Answer Wiki

Thanks. We'll let you know when a new response is added.

%REM will return an integer.

You can multiply both factors by 100,1000,10000 and divide the result by the same number
Answer = %REM( A*100 : B*100)/100
so if A= 2.5421 and B = 2
the %REM(254.21 : 200) /100 = .54

or

Result = %int(A/B)
Remainder = A – (Result * B)

Phil

Discuss This Question: 2  Replies

 
There was an error processing your information. Please try again later.
Thanks. We'll let you know when a new response is added.
Send me notifications when members answer or reply to this question.

REGISTER or login:

Forgot Password?
By submitting you agree to receive email from TechTarget and its partners. If you reside outside of the United States, you consent to having your personal data transferred to and processed in the United States. Privacy
  • TomLiotta
    %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
    125,585 pointsBadges:
    report
  • philpl1jb
    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
    50,860 pointsBadges:
    report

Forgot Password

No problem! Submit your e-mail address below. We'll send you an e-mail containing your password.

Your password has been sent to:

To follow this tag...

There was an error processing your information. Please try again later.

REGISTER or login:

Forgot Password?
By submitting you agree to receive email from TechTarget and its partners. If you reside outside of the United States, you consent to having your personal data transferred to and processed in the United States. Privacy

Thanks! We'll email you when relevant content is added and updated.

Following