9 mod 5
(-9) mod 5
9 mod (-5)
9 rem 5
(-9) rem 5
9 rem (-5)
2 回答
24
一种看待不同的方法是在测试平台上运行快速模拟,例如使用如下过程:
process is
begin
report " 9 mod 5 = " & integer'image(9 mod 5);
report " 9 rem 5 = " & integer'image(9 rem 5);
report " 9 mod (-5) = " & integer'image(9 mod (-5));
report " 9 rem (-5) = " & integer'image(9 rem (-5));
report "(-9) mod 5 = " & integer'image((-9) mod 5);
report "(-9) rem 5 = " & integer'image((-9) rem 5);
report "(-9) mod (-5) = " & integer'image((-9) mod (-5));
report "(-9) rem (-5) = " & integer'image((-9) rem (-5));
wait;
end process;
它显示结果:
# ** Note: 9 mod 5 = 4
# ** Note: 9 rem 5 = 4
# ** Note: 9 mod (-5) = -1
# ** Note: 9 rem (-5) = 4
# ** Note: (-9) mod 5 = 1
# ** Note: (-9) rem 5 = -4
# ** Note: (-9) mod (-5) = -4
# ** Note: (-9) rem (-5) = -4
2 回答
一种看待不同的方法是在测试平台上运行快速模拟,例如使用如下过程:
它显示结果:
Wikipedia - Modulo operation有详尽的描述,包括规则:
mod有除数的符号,因此在
a mod n
中n
rem有分红的迹象,因此
a
在a rem n
mod
运算符给出一个向下舍入的除法(浮动除法)的残差,所以a = floor_div(a, n) * n + (a mod n)
. 优点是a mod n
是一个重复的锯齿图,当a
甚至通过零增加时,这在某些计算中很重要 .rem
运算符给出了常数整数除法a / n
的余数,它朝向0(截断除法)舍入,因此a = (a / n) * n + (a rem n)
.