开发者

Is there an numeric type in ILE RPG which will overflow without crashing my program?

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?


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.


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


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
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜