Jump to content

% math operator


ManeXi

Recommended Posts

That's modulus. It get's the remainder of 2 numbers that divide into each other. For example

5%2 will give you 1, because you can only divide 2 into 5 twice (2x2=4) and the remainder is 1.

6%3 = 0, because 2x3 = 6

7%3 = 1, because 2x3 = 6 and remainder is 1

etc.

Most of the times I used it to check if a number is even like:

local myNum = 11

if ( myNum % 2 == 0 ) then -- if you divide by 2 and no remainder, it's even
  print('even')
else
  print('odd')
end

 

  • Like 1
Link to comment
  • Moderators
On 09/03/2017 at 18:45, ManeXi said:

it just gives the remainder of a division

Yes exactly.

22 hours ago, Gordon_G said:

In french we call this operation : Division euclidienne

Actually, no. We call this operator a "modulo" which is the remainder of a "Division euclidienne" as you said. You mixed "/" with "%"

Quote

5 % 3 = 2

5 modulo 3  = 2

 

  • Like 1
Link to comment

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...