Register forum user name Search FAQ

Gammon Forum

Notice: Any messages purporting to come from this site telling you that your password has expired, or that you need to verify your details, confirm your email, resolve issues, making threats, or asking for money, are spam. We do not email users with any such messages. If you have lost your password you can obtain a new one by using the password reset link.

Due to spam on this forum, all posts now need moderator approval.

 Entire forum ➜ MUSHclient ➜ Lua ➜ Even/Odd comparison

Even/Odd comparison

It is now over 60 days since the last post. This thread is closed.     Refresh page


Posted by tobiassjosten   Sweden  (79 posts)  Bio
Date Mon 30 May 2005 10:38 AM (UTC)
Message
I can't seem to find a way to check wether a number is odd or even. I've tried with type() (which just gives 'number') and string.find(tostring(number), "."). String.find didn't prove very helpful, despite being my best bet. :/ I tried escaping the dot (is it in regexp format?) but it didn't work either. Anyone got any suggestions as to how I could do this?

The purpose is to get this function to output a centered text:
function visLine(msg, color)
    lnum, rnum = (74 - string.len(msg)) / 2, (74 - string.len(msg)) / 2
    if not string.find(tostring(lnum), "\.") == false then rnum = lnum + 2 end
    ColourTell("lightgrey", "", string.rep("-",lnum) .. " [ ")
    ColourTell(color, "", msg)
    ColourNote("lightgrey", "", " ] " .. string.rep("-",rnum))
end

Simplicity is Divine | http://nogfx.org/
Top

Posted by Larkin   (278 posts)  Bio
Date Reply #1 on Mon 30 May 2005 02:28 PM (UTC)
Message
Use the math.mod function to find the remainder of your number from dividing by 2. If the result is 0, it's even. Otherwise, it's odd.

if math.mod(num, 2) == 0 then
  print("even")
else
  print("odd")
end
Top

Posted by tobiassjosten   Sweden  (79 posts)  Bio
Date Reply #2 on Mon 30 May 2005 03:53 PM (UTC)
Message
Kind of makes sense to use the math-functions for integers. :P
Thanks alot!

Simplicity is Divine | http://nogfx.org/
Top

Posted by Nick Gammon   Australia  (23,133 posts)  Bio   Forum Administrator
Date Reply #3 on Sun 05 Jun 2005 02:42 AM (UTC)
Message
Another approach which doesn't use division is to use the "bit" extension in the MUSHclient Lua implementation.

eg.


isodd = bit.band (x, 1)


Strangely I couldn't find these functions documented anywhere on this site. If someone can find the URL please let me know.

In summary, MUSHclient has a "bit" table which implements bitwise operations, for use in cases like this.


bit.shr

Shift right - shift arg1 right by arg2 bits

eg.


print (bit.shr (64, 2))  --> 16



bit.shl

Shift left - shift arg1 left by arg2 bits

eg.


print (bit.shl (64, 2))  --> 256




bit.band

Bitwise and ("and" is a keyword) - takes multiple arguments

eg.


print (bit.band (15, 7, 3))  --> 3



bit.bor

Bitwise or ("or" is keyword) - takes multiple arguments

eg.


print (bit.bor (1, 4, 8))  --> 13



bit.xor

Exclusive or - takes multiple arguments

eg.


print (bit.xor (15, 1))  --> 14



bit.neg

Bitwise negate (ones complement as a 32-bit unsigned number)

eg.


print (bit.neg (15))  --> 4294967280


- Nick Gammon

www.gammon.com.au, www.mushclient.com
Top

Posted by Ked   Russia  (524 posts)  Bio
Date Reply #4 on Sun 05 Jun 2005 06:37 AM (UTC)
Message
The post with the introduction of Lua bit functions is at:

http://www.gammon.com.au/forum/bbshowpost.php?bbsubject_id=4907

Top

The dates and times for posts above are shown in Universal Co-ordinated Time (UTC).

To show them in your local time you can join the forum, and then set the 'time correction' field in your profile to the number of hours difference between your location and UTC time.


27,733 views.

It is now over 60 days since the last post. This thread is closed.     Refresh page

Go to topic:           Search the forum


[Go to top] top

Information and images on this site are licensed under the Creative Commons Attribution 3.0 Australia License unless stated otherwise.