# Odd or Even

## Recommended Posts

How do I tell if a variable is odd or even using Lua?

% is used in other languages is it the same with Leadwerks?

##### Link to post

Seems Lua doesn't have a modulo operator prior to 5.1 (not sure what LE is at the moment).

a % b == a - math.floor(a/b)*b

You can also try math.mod or fmod.

##### Link to post

I don't know if Lua does integer math but you can divide the number by 2 then multiply it back by 2 and see if it becomes the same number.  In C it looks like this:

```if ((n/2)*2 == n)
printf("Even\n");
else
printf("Odd\n");```

For example, 7 divided by 2 would become 3 (because 3.5 becomes 3 when it's an integer) and would then double to 6.  Since 7 doesn't equal 6, you know it was an odd number.

##### Link to post

Thanks Catch and GC for the help.

Turns out that Leadwerks does recognize the % as  modulo.

So if  X%2  equals 0 then its an even number and

if X%2 does not equal 0 its an odd number.

Changing the 2 to other numbers will be true if the x is a multiple of that number.

Now back to making a hex map.

##### Link to post

or go into 3d and make fractals with modulo.. altoguh u would use cpp for that

## Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

Only 75 emoji are allowed.

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×

• Pages

• Back
• Store

• #### Support

• Projects
×
• Create New...