A brief introduction to the difference between modulo and remainder in Lua _lua

Source: Internet
Author: User
Tags lua modulus

I think a lot of people do not know the difference between these two concepts, just in the translation of the LUA manual with the math.fmod of the two operations, by the way to explain it.

The difference between the modulus and the remainder is obtained.

Suppose to do modulo or remainder operations on a and b two integers. So the first step is to find the integer quotient C, A/b value, and the second step is to calculate the modulus or remainder: A-c * b. The difference between modulo and remainder is how to deal with A/b value.

In the modulo operation, the result of A/b is rounded in the direction of infinity, and the result of A/b is rounded to 0 in the remainder operation.

Therefore, the symbol of the result is consistent with B, and the sign of the result is consistent with a.

In Lua, 4% (-3) equals 2, which shows that the% Lua is a modulo operation rather than a remainder operation. 4/(-3) round to infinity Small to 2, then modulus 4-(-2 *-3) =-2.

Math.fmod (4,-3) is equal to 1 in Lua, so Fmod is a remainder operation rather than a modulo operation. 4/(-3) rounded to 1 in 0 direction, the remainder is 4-(-1 *-3) = 1.

It is easy to draw another conclusion: if the a,b are all positive integers, there is no difference between the modulus and the remainder.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.