当前位置:网站首页>In programming languages, the difference between remainder and modulo

In programming languages, the difference between remainder and modulo

2022-08-09 23:13:00 Full stack programmer webmaster

Hello everyone, meet again, I'm your friend Quanstack Jun.

Take the remainder and follow the principle of keeping the quotient as close to 0 as possible

Take the modulo and follow the principle of making the quotient as close to negative infinity as possible

In matlab, the remainder and modulo are defined as follows:

When y≠0:

Remainder: rem(x,y)=x-y.*fix(x./y)

Modulus: mod(x,y)=x-y.*floor(x./y)

Among them, the fix() function is to round to 0, and the floor() function is to round to negative infinity

Operation example:

7/(-3)=-2.3, in this operation, x is 7, y is -3, call the two functions fix() and floor() respectively, the result is:

fix(-2.3)=-2

floor(-2.3)=-3

So, rem(7,-3)=1, mod(7,-3)=-2

Publisher: Full-stack programmer, please indicate the source: https://javaforall.cn/105810.htmlOriginal link: https://javaforall.cn

原网站

版权声明
本文为[Full stack programmer webmaster]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/221/202208092049543897.html