我正在尝试使用算法计算除法的余数:
remainder = dividend - (dividend / divisor) * divisor
全部以整数计算。
示例:获取 15 / 6 的余数。
1. (15 / 6) = 2
2. (2) * 6 = 12
3. 15 - 12 = 3
15/6 的余数确实是 3。
我的问题是在我的 CL 脚本中使用这个算法总是简单地返回 0。这是为什么?
pgm
dcl var(÷nd) type(*int) value(15)
dcl var(&divisor) type(*int) value(6)
dcl var(&remainder) type(*int)
dcl var(&msg) type(*char)
/* Calculate remainder. [ed: 29Sep2016] "* &remainder" is now: "* &divisor" */
chgvar var(&remainder) value(÷nd - (÷nd / &divisor) * &divisor)
/* Prior to 29-Sep-2016 redaction, the above was coded as the incorrect expression: +
chgvar var(&remainder) value(÷nd - (÷nd / &divisor) * &remainder) +
and remains, commented-out, to preserve relevance of user-comments about the OP */
/* Cast remainder and display. */
chgvar var(&msg) value(&remainder)
sndpgmmsg msg(&msg)
endpgm
编译:
crtclpgm pgm(test) srcfile(test) srcmbr(test)
跑:
call test
输出:
0