This is a read-only snapshot of the ComputerCraft forums, taken in April 2020.
5tarKaster's profile picture

Even numbers

Started by 5tarKaster, 15 October 2012 - 08:15 AM
5tarKaster #1
Posted 15 October 2012 - 10:15 AM
Hey all,

got a question… is it possible to determine if a number is an even number or an odd number?
In java its easy enough.. you just devide by 2 and convert to int, then catch the exception and there you go… easy… probably not the best way but it does work :D/>/>
I've noticed Lua does not have integers or doubles or anything so its got me dumbfounded

any help would be greatly apreciated
Thanks :P/>/>
robhol #2
Posted 15 October 2012 - 10:16 AM
In java AND lua: n % 2 == 0
In "mathematic", this is the same as "n mod 2 = 0", ie. the remainder of the integer division n/2. 2 -> 0, 8-> 0, 65536->0, you get the idea.
5tarKaster #3
Posted 15 October 2012 - 10:27 AM
lol now I feel silly… I just tested it and it seems to work 100%
thanks for the speedy info :D/>/>

I don't quite understand how it works, the maths behind it all…

but it does work ^^
robhol #4
Posted 15 October 2012 - 10:45 AM
Well, integer division is usually the first kind of division people learn.

If you have 10 apples and 2 people, each person gets 5. If you have 11 apples, each person still gets 5 (you're not allowed to split the actual apples), and so there's one left over. That's why 11%2 = 1.
5tarKaster #5
Posted 15 October 2012 - 10:59 AM
-click- I see now ^^

its been like 8 months since I was in school, can't blame me :D/>/> but I see now how its working. Its finding and returning the remainder, I never knew there was this functionality in java :P/>/> and well neither Lua, hence the post but again thanks a lot.
Luanub #6
Posted 15 October 2012 - 12:50 PM
You can also use bit.band()


bit.band(number, 1)  -- will return 0 if number is even or 1 if it is odd
Doyle3694 #7
Posted 15 October 2012 - 01:03 PM
it's about to detect if it has a decimal or not, not to check if it's odd or even
PixelToast #8
Posted 15 October 2012 - 02:05 PM
math.floor(int)==int

will return false if it has decimals
Luanub #9
Posted 15 October 2012 - 10:01 PM
it's about to detect if it has a decimal or not, not to check if it's odd or even

Read the post he's asking how to tell if a number is odd or even. I don't see anything about decimal's in the OP, or any of his subsequent replies.

Hey all,

got a question… is it possible to determine if a number is an even number or an odd number?
In java its easy enough.. you just devide by 2 and convert to int, then catch the exception and there you go… easy… probably not the best way but it does work :D/>/>
I've noticed Lua does not have integers or doubles or anything so its got me dumbfounded

any help would be greatly apreciated
Thanks :P/>/>
Doyle3694 #10
Posted 16 October 2012 - 08:37 AM
well of everything I have seen except a little "odd" in his sentence it looks like he is asking to check if the number is even. guess fastreading text isn't smart.