ILE RPG 中是否有一种数字类型可以溢出而不会使我的程序崩溃?
我正在 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
RPG不会让你这么做。我建议的最佳解决方案是创建一个程序来为您进行数学计算并处理溢出。虽然 RPG 确实具有 TRUNCNBR 编译选项和控制规范关键字,但它仅适用于某些场景。
如果您正在做一个简单的计数器,您可以创建一个具有重叠数字字段的数据结构,如下所示:
然后添加到 WholeCounter,然后立即将 CountOverflow 清零。在此示例中,Counter 是一个 4 位数字。您可以对整数字段执行相同的操作,但我建议您将它们保持为无符号:
同样,这最好在过程中完成。
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:
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:
Again, this is best done in a procedure.
您可以使用固定格式的数学运算(add、sub、mult 和 div)。当达到溢出时它们将被截断。麻烦但有效。
显示程序消息
作业 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.
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: