1. 程式人生 > >vhdl中mod與rem的區別

vhdl中mod與rem的區別

mod(取模)and rem(取餘)

VHDL has mod and rem. They return the same value if both arguments are positive.
but, they produce different results for negative inputs:
5 mod 3 = 2
(-5) mod 3 = 1
5 mod (-3) = -1
(-5) mod (-3) = -2

for mod, the result has the same sign with the first argument.
whereas
5 rem 3 = 2
(-5) rem 3 = -2
5 rem (-3) = 2
(-5) rem (-3) = -2
for rem,the result has the same sign with the second argument.

A rem B = A - ( A / B ) * B --餘數運算子 利用運算元A決定結果的正負號

A mod B = A - B * N --取模運算子 利用運算元B決定結果的正負號