I came across these statements in VHDL programming and could not understand the difference between the two operators mod and rem
9 mod 5
(-9) mod 5
9 mod (-5)
9 rem 5
(-9) rem 5
9 rem (-5)
In VHDL - Modulo ( "mod" ) is an operator that returns the reminder of the division of 2 numbers. Example : signal result_1 , result_2 , result_3 , result_4 : integer ; result_1 = 9 mod 3 ; -- result will equal 0.
The MOD() function in MySQL is used to find the remainder of one number divided by another. The MOD() function returns the remainder of dividend divided by divisor. if the divisor is zero, it returns NULL. Syntax : MOD(N, M) or N % M or N MOD M.
Operator: /= The inequality operator which can be used in an expression on any type except file types. The resulting type of an expression using this operator is Boolean (that is, True or False). The expression "A /= B" returns True only if A and B are not equal.
rem is a mathematical remainder in the sense of the remainder of an integer division: (x rem y) + (x div y)*y == x.
A way to see the different is to run a quick simulation in a test bench, for example using a process like this:
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;
It shows the result to be:
# ** 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
Wikipedia - Modulo operation has an elaborate description, including the rules:
n
in a mod n
a
in a rem n
The mod
operator gives the residue for a division that rounds down (floored division), so a = floor_div(a, n) * n + (a mod n)
. The advantage is that a mod n
is a repeated sawtooth graph when a
is increasing even through zero, which is important in some calculations.
The rem
operator gives the remainder for the regular integer division a / n
that rounds towards 0 (truncated division), so a = (a / n) * n + (a rem n)
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With