0

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?

Tarquila
  • 1,167
  • 3
  • 11
  • 17
  • If I'd seen this when you first asked it, I would have said something like "what are you talking about? *Every* numeric type will wrap around!" The key is to use the opcodes, not expressions (see my comment on Steve's answer). – John Y Jul 02 '11 at 02:35

3 Answers3

3

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
  • 1,839
  • 12
  • 13
3

You can use the fixed format mathematical operations (add,sub,mult, and div). They will truncate when overflow is reached. Cumbersome but works.

0001.00 D Fld1            s              3  0                                         
0001.01 D                                                                             
0002.00 C     999           add       3             Fld1                              
0003.00  /free                                                                        
0004.00    dsply ('The current value '+%editc(Fld1:'X'));                             
0005.00    *inlr=*on;                                                                 
0006.00    return;                              

Display Program Messages

Job 912834/SPRICE/DP88LT started on 01/11/11 at 15:39:15 in subsystem QINTER Message queue SPRICE is allocated to another job.
DSPLY The current value 002

Tracy Probst
  • 1,839
  • 12
  • 13
Steve
  • 31
  • 1
  • I know I'm noticing this really, really late, but I am not sure why this was not suggested at the beginning. Yeah, the old-school opcodes aren't "ILE RPG", but *every* RPG compiler still accepts them, and it's an utterly straightforward way to get the wrapping behavior. I actually disagree that it's cumbersome at all. – John Y Jul 02 '11 at 02:33
  • Good point, Steve. I think I just get lost in ILE and forget some of the old school stuff I was brought up with. – Tracy Probst Feb 23 '12 at 20:30
2

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
  • 21
  • 1