This is a read-only snapshot of the ComputerCraft forums,
taken in April 2020.
how do you tell if a number is even or odd
Started by ETHANATOR360, 25 March 2013 - 11:35 AMPosted 25 March 2013 - 12:35 PM
title says it all
Posted 25 March 2013 - 12:41 PM
you can use division…….
Posted 25 March 2013 - 12:44 PM
in java, i know you can do this
var Num = 4
if (Num % 2 === Num/2)
{
Number is even
}
else
{
Number is odd
}
its java though (tinn57, syntax?)Posted 25 March 2013 - 12:55 PM
i know java im asuming mod symbol works in lua
Posted 25 March 2013 - 01:07 PM
Here's a simple function for you:
function GetType(Number)
local Derp = Number % 2
if Derp == 0 then
return "Even"
else
return "Odd"
end
end
function GetType(Number)
local Derp = Number % 2
if Derp == 0 then
return "Even"
else
return "Odd"
end
end
Posted 25 March 2013 - 01:41 PM
Another nice way is to use bitwise operators, every even number has a zero last bit. To check this:
Although im not sure how much of a difference this makes in lua.
function getType(number)
if bit.band(number,1) == 0 then
return "Even"
end
return "Odd"
end
Although im not sure how much of a difference this makes in lua.
Posted 25 March 2013 - 01:46 PM
Either way works. It is up to him what he wants to use. I would rather use the bitwise operator.
Posted 25 March 2013 - 01:58 PM
It's much easier, way faster, and native to lua to use the modulo operator.
function isEven(num)
return num % 2 == 0
end
Posted 25 March 2013 - 01:59 PM
I guess that works as well :P/> Still a fan of the bitwise operator
Posted 25 March 2013 - 02:07 PM
Im still new to lua, so as i said i wasn't sure if the bitwise operator had an advantage over the modulo operator. I know that in some languages it definitely does.
Posted 25 March 2013 - 02:11 PM
In this case, the modulo operator has the bitwise operator beaten. I still like it though ^_^/>
Posted 25 March 2013 - 02:26 PM
modulo = java :P/>
Posted 25 March 2013 - 02:29 PM
Java = nil :wacko:/>
Posted 25 March 2013 - 02:31 PM
Java = null :P/>Java = nil :wacko:/>/>
Posted 25 March 2013 - 02:57 PM
Lua perspective dear Watson ^_^/>Java = null :P/>Java = nil :wacko:/>/>
If Java had true Garbage Collection, well, there would be no Java..
Posted 25 March 2013 - 03:09 PM
there is also a modulo function in the math library…
its math.fmod iirc. because math.modf gets the fraction part of the number.
its math.fmod iirc. because math.modf gets the fraction part of the number.
Nope, u gunna talk about Java, you look at it from a Java perspective.Lua perspective dear Watson ^_^/>Java = null :P/>
Posted 25 March 2013 - 03:14 PM
But, but, no! Unacceptable!Nope, u gunna talk about Java, you look at it from a Java perspective.
Posted 25 March 2013 - 03:15 PM
because math.modf gets the fraction part of the number.
Wait. Why does modf even exist? It's ridiculously easy to make modf yourself.
function modf(n)
return n - n%1, n%1
end
Posted 25 March 2013 - 03:21 PM
Why do most functions exist… for convenience to the developer not to have to make the function each time… most of the math and string functions can be easily made…Wait. Why does modf even exist? It's ridiculously easy to make modf yourself.
Posted 25 March 2013 - 03:25 PM
Yet people still complain to Cloudy and Dan: "This is too hard, make it easier!"Why do most functions exist… for convenience to the developer not to have to make the function each time… most of the math and string functions can be easily made…
Posted 25 March 2013 - 03:26 PM
I was talking about in programming in general…. why does any api exist. to make our life as developers easier. we CAN programme without these APIs, but they are there so we don't have to write all these functionalities.Yet people still complain to Cloudy and Dan: "This is too hard, make it easier!"Why do most functions exist… for convenience to the developer not to have to make the function each time… most of the math and string functions can be easily made…
Posted 25 March 2013 - 03:30 PM
I was just reminiscing on how people here can't make simple functions by themselves, such as a goto function, and well, you know what I mean..I was talking about in programming in general…. why does any api exist. to make our life as developers easier. we CAN programme without these APIs, but they are there so we don't have to write all these functionalities.Yet people still complain to Cloudy and Dan: "This is too hard, make it easier!"Why do most functions exist… for convenience to the developer not to have to make the function each time… most of the math and string functions can be easily made…
Posted 25 March 2013 - 03:33 PM
goto isn't a function. its a statement/instruction. and it should never be used. when used a velociraptor will appear and bite off your head. (well according to my uni Lecturer anyways) :P/>I was just reminiscing on how people here can't make simple functions by themselves, such as a goto function, and well, you know what I mean..
Posted 25 March 2013 - 03:36 PM
It was my impression that it was Lua's goal to be lightweight and only provide what was needed, which was the reason for not having a round() function implemented (that rounds to any factor, e.g. round(38, 5) –> 40) as I've read on lua-users.Why do most functions exist… for convenience to the developer not to have to make the function each time… most of the math and string functions can be easily made…
The math library is just ridiculously huge and some of it's functions shouldn't even be there in my opinion, like "pow", "fmod" and "modf" as you've mentioned.
Though I suppose there would be some use to having some of these, if you can't use operators (or simple logic) for some reason.
String functions though, I'd basically just quit Lua altogether if even one of them weren't implemented. In fact, the patterns system is even lacking in some aspects.
Posted 25 March 2013 - 03:38 PM
Ok, now that is a bit extreme! I was referring to a goto function for turtles btw… I would never want a goto function for anything other than that :P/>when used a velociraptor will appear and bite off your head. (well according to my uni Lecturer anyways) :P/>
Posted 25 March 2013 - 03:41 PM
Well the % operator would go a little something like this in the number data type implThough I suppose there would be some use to having some of these, if you can't use operators (or simple logic) for some reason.
number.__mod = math.fmod
or if you prefer expanded and pointless
number.__mod = function( a,b ) return math.fmod( a,b ) end
I didn't like the lack of the functions, hence me implementing the Extended String LibraryString functions though, I'd basically just quit Lua altogether if even one of them weren't implemented. In fact, the patterns system is even lacking in some aspects.
Posted 25 March 2013 - 03:49 PM
A goto would be nice. It would make it easy to break out of multiple loops, or to skip iterations in a loop.:Ok, now that is a bit extreme! I was referring to a goto function for turtles btw… I would never want a goto function for anything other than that :P/>/>/>
while blah do
while otherblah do
while blahblahblah do
if fun then
goto gtfo
elseif nofun then
goto omfg
end
end
end
::gtfo::
end
::omfg::
You could also emulate python's "else" for while loops, without using a boolean.
local people = {'insert','a','list','of','names'}
for i=1, #people do
if people[i] == 'Buzz Killington' then
print 'Everyone left the party.'
goto left
end
end
print 'The party has started.'
::left::
Posted 25 March 2013 - 03:53 PM
Umm.. nice example?while blah do while otherblah do while blahblahblah do if fun then goto gtfo elseif nofun then goto omfg end end end ::gtfo:: end ::omfg::
mind = blown