アルゴリズムで除算の剰余を計算しようとしています:
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