15 posts
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/>/>
175 posts
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.
15 posts
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 ^^
175 posts
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.
15 posts
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.
1111 posts
Location
Portland OR
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
818 posts
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
2217 posts
Location
3232235883
Posted 15 October 2012 - 02:05 PM
math.floor(int)==int
will return false if it has decimals
1111 posts
Location
Portland OR
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/>/>
818 posts
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.