ibmi-brunch-learn

Announcement

Collapse
No announcement yet.

eval(h) with division inside divisor

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • eval(h) with division inside divisor

    Hello people. I found an issue when making a calculation that should work OK. I have fixed it already but was wondering why this happens:

    XMONOBJ and XMONTOPROV are 15 2
    IVA is 5 2

    EVAL(H) XMONOBJ=XMONTOPROV/(1+(IVA/100);

    This calculation makes eval(h) truncate all decimals. Even trying 1.00 and 100.00 truncates all decimals.
    I had to split the divisor into another variable:
    PORCIVA=1+(IVA/100)
    EVAL(H) XMONOBJ=XMONTOPROV/PORCIVA;

    is there any way i can fool RPG into calculating what i want without doing the split? My guess is that 1+(IVA/100) is being calculated internally with a different size therefore eval(h) does not do what i expect it to do.

    By the way, what i am trying to do is that i have a number with an added 12% and i want to substract that value to obtain the original number. IE i have 15.10 and i need to obtain 13.48

  • #2
    Re: eval(h) with division inside divisor

    lose that wacky eval ..
    PHP Code:
    XMONOBJ=%dech(XMONTOPROV/(1+(IVA/100)):15:5); 
    All my answers were extracted from the "Big Dummy's Guide to the As400"
    and I take no responsibility for any of them.

    www.code400.com

    Comment


    • #3
      Re: eval(h) with division inside divisor

      Originally posted by jamief View Post
      lose that wacky eval ..
      PHP Code:
      XMONOBJ=%dech(XMONTOPROV/(1+(IVA/100)):15:5); 
      i love this forum! thanks, already tested it.

      Comment


      • #4
        Re: eval(h) with division inside divisor

        and the forum loves you back!
        All my answers were extracted from the "Big Dummy's Guide to the As400"
        and I take no responsibility for any of them.

        www.code400.com

        Comment


        • #5
          Re: eval(h) with division inside divisor

          Originally posted by jamief View Post
          lose that wacky eval ..
          PHP Code:
          XMONOBJ=%dech(XMONTOPROV/(1+(IVA/100)):15:5); 
          This will work in this instance, but RPG has an "engineered" approach to this that avoids the use of the BIF. I haven't time to check it but I'm also pretty certain that the %DEC BIF approach will not always work.

          The "RPG Way" would be to use the op-code extender R on the Eval
          Code:
          EVAL(RH) XMONOBJ=XMONTOPROV/(1+(IVA/100);
          The idea behind the R is to tell RPG that when calculating the size of the intermediate result it should never let the number of decimal places fall below the number needed to create the final result. That is probably an easier generic approach as you never need to work out at what point the %dec needs to be inserted to guarantee the desired approach.

          Comment


          • #6
            Re: eval(h) with division inside divisor

            Great tip Jon. I had forgotten about that. As I program in /free I don't usually think about opcode extenders for eval. Maybe if they had an H spec that forced this... say maybe something like....
            Code:
                 H EXPROPTS(*RESDECPOS)
            Put this at the top of your source member and forget about opcode extenders
            Your future President
            Bryce

            ---------------------------------------------
            http://www.bravobryce.com

            Comment

            Working...
            X