ILE RPG 中是否有一种数字类型可以溢出而不会使我的程序崩溃?

发布于 2024-08-10 04:16:46 字数 61 浏览 4 评论 0原文

我正在 ILE RPG 中寻找一种数字类型,它在溢出时会“环绕”,类似于 C int 的方式。有这样的事吗?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

疧_╮線 2024-08-17 04:16:46

RPG不会让你这么做。我建议的最佳解决方案是创建一个程序来为您进行数学计算并处理溢出。虽然 RPG 确实具有 TRUNCNBR 编译选项和控制规范关键字,但它仅适用于某些场景。

如果您正在做一个简单的计数器,您可以创建一个具有重叠数字字段的数据结构,如下所示:

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

然后添加到 WholeCounter,然后立即将 CountOverflow 清零。在此示例中,Counter 是一个 4 位数字。您可以对整数字段执行相同的操作,但我建议您将它们保持为无符号:

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

同样,这最好在过程中完成。

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.

纸伞微斜 2024-08-17 04:16:46

您可以使用固定格式的数学运算(add、sub、mult 和 div)。当达到溢出时它们将被截断。麻烦但有效。

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;                              

显示程序消息

作业 912834/SPRICE/DP88LT 于 2011 年 1 月 11 日 15:39:15 在子系统 QINTER 中启动
消息队列 SPRICE 已分配给另一个作业。
DSPLY 当前值 002

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

多彩岁月 2024-08-17 04:16:46

或者,您可以在发生溢出时监视错误代码:

 D Counter         S             10I 0

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

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
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文