views:

135

answers:

2

I'm looking for a numeric type in ILE RPG which will "wrap round" when it overflows, in a similar way to how a C int would. Is there such a thing?

A: 

RPG won't let you do that. The best solution I can suggest would be to create a procedure that does the math for you and handles the overflow. While RPG does have the TRUNCNBR compile option and control spec keyword, it's only applicable in certain scenarios.

If you're doing a simple counter, you can create a data structure with overlapping numeric fields like this:

DCounterDS        DS                        
D Counter                 5      8  0       
D CountOverflow           1      4  0       
D WholeCounter            1      8  0 INZ(0)

Then you add to WholeCounter and then zero-out CountOverflow immediately afterwards. In this example, Counter is a 4-digit number. You can do the same thing with integer fields, but I recommend you keep them unsigned:

DCounterDS        DS                        
D Counter                 5      8U 0       
D CountOverflow           1      4U 0       
D WholeCounter            1      8U 0 INZ(0)

Again, this is best done in a procedure.

Tracy Probst
A: 

Or you could monitor for the error code when an overflow happens:

 D Counter         S             10I 0

  /FREE
   Monitor;
      Counter += 1;
   On-Error 103;
      Clear Counter;
   EndMon;
  /END-FREE
GuyB