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

how do you tell if a number is even or odd

Started by ETHANATOR360, 25 March 2013 - 11:35 AM
ETHANATOR360 #1
Posted 25 March 2013 - 12:35 PM
title says it all
LuaEclipser #2
Posted 25 March 2013 - 12:41 PM
you can use division…….
LuaEclipser #3
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?)
ETHANATOR360 #4
Posted 25 March 2013 - 12:55 PM
i know java im asuming mod symbol works in lua
SuicidalSTDz #5
Posted 25 March 2013 - 12:58 PM
Bam!

Read on!
alakazard12 #6
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
Brandhout #7
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:


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.
SuicidalSTDz #8
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.
Kingdaro #9
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
SuicidalSTDz #10
Posted 25 March 2013 - 01:59 PM
I guess that works as well :P/> Still a fan of the bitwise operator
Brandhout #11
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.
SuicidalSTDz #12
Posted 25 March 2013 - 02:11 PM
In this case, the modulo operator has the bitwise operator beaten. I still like it though ^_^/>
LuaEclipser #13
Posted 25 March 2013 - 02:26 PM
modulo = java :P/>
SuicidalSTDz #14
Posted 25 March 2013 - 02:29 PM
Java = nil :wacko:/>
Engineer #15
Posted 25 March 2013 - 02:31 PM
Java = nil :wacko:/>/>
Java = null :P/>
SuicidalSTDz #16
Posted 25 March 2013 - 02:57 PM
Java = nil :wacko:/>/>
Java = null :P/>
Lua perspective dear Watson ^_^/>

If Java had true Garbage Collection, well, there would be no Java..
theoriginalbit #17
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.


Java = null :P/>
Lua perspective dear Watson ^_^/>
Nope, u gunna talk about Java, you look at it from a Java perspective.
SuicidalSTDz #18
Posted 25 March 2013 - 03:14 PM
Nope, u gunna talk about Java, you look at it from a Java perspective.
But, but, no! Unacceptable!
Kingdaro #19
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
theoriginalbit #20
Posted 25 March 2013 - 03:21 PM
Wait. Why does modf even exist? It's ridiculously easy to make modf yourself.
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…
SuicidalSTDz #21
Posted 25 March 2013 - 03:25 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…
Yet people still complain to Cloudy and Dan: "This is too hard, make it easier!"
theoriginalbit #22
Posted 25 March 2013 - 03:26 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…
Yet people still complain to Cloudy and Dan: "This is too hard, make it easier!"
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.
SuicidalSTDz #23
Posted 25 March 2013 - 03:30 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…
Yet people still complain to Cloudy and Dan: "This is too hard, make it easier!"
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.
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..

theoriginalbit #24
Posted 25 March 2013 - 03:33 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..
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/>
Kingdaro #25
Posted 25 March 2013 - 03:36 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…
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.

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.
SuicidalSTDz #26
Posted 25 March 2013 - 03:38 PM
when used a velociraptor will appear and bite off your head. (well according to my uni Lecturer anyways) :P/>
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/>
theoriginalbit #27
Posted 25 March 2013 - 03:41 PM
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.
Well the % operator would go a little something like this in the number data type impl

number.__mod = math.fmod
or if you prefer expanded and pointless

number.__mod = function( a,b ) return math.fmod( a,b ) end

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.
I didn't like the lack of the functions, hence me implementing the Extended String Library
Kingdaro #28
Posted 25 March 2013 - 03:49 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/>/>/>
A goto would be nice. It would make it easy to break out of multiple loops, or to skip iterations in a loop.:


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::
SuicidalSTDz #29
Posted 25 March 2013 - 03:53 PM

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::
Umm.. nice example?
mind = blown