I want to see if one number is divisible by another number

General discussion about LÖVE, Lua, game development, puns, and unicorns.
enderspike
Prole
Posts: 10
Joined: Tue Oct 11, 2011 6:37 pm

I want to see if one number is divisible by another number

Post by enderspike »

I haven't included a file because I believe my question is rather simple: how do I say "See if X is divisible by Y?"

I'm a noob trying to make a simple flexible timer like

love.update(dt)
timer = timer + dt
...

And then be able to make events happen at various times like

if timer //is divisible by// 3 then
thisis.happening
end

if timer //is divisible by// .5 then
thisis.moreoften
end

Is this real? Should I do this a different way? Help!
User avatar
TechnoCat
Inner party member
Posts: 1611
Joined: Thu Jul 30, 2009 12:31 am
Location: Milwaukee, WI
Contact:

Re: I want to see if one number is divisible by another numb

Post by TechnoCat »

http://en.wikipedia.org/wiki/Modulo_operation

Code: Select all

if number%3==0 then --fixed because i'm stupid
  print("EVENLY DIVISIBLE BY 3!!!")
else
  print("Not evenly divisible by 3.")
end
Last edited by TechnoCat on Fri Nov 11, 2011 2:11 am, edited 2 times in total.
User avatar
Ellohir
Party member
Posts: 235
Joined: Sat Oct 22, 2011 11:12 pm

Re: I want to see if one number is divisible by another numb

Post by Ellohir »

It's not the modulo operation, but for what you want to do I usually go with this:

Code: Select all

if timerA > 3 then
    A.happening -- every 3 seconds
    timerA = 0
end

if timerB > 0.5 then
    B.happening -- every half second
    timerB = 0
end
It's more code but I like having my counters split.
Last edited by Ellohir on Fri Nov 11, 2011 1:28 am, edited 1 time in total.
User avatar
josefnpat
Inner party member
Posts: 955
Joined: Wed Oct 05, 2011 1:36 am
Location: your basement
Contact:

Re: I want to see if one number is divisible by another numb

Post by josefnpat »

TechnoCat wrote:http://en.wikipedia.org/wiki/Modulo_operation

Code: Select all

if number%3 then
  print("EVENLY DIVISIBLE BY 3!!!")
else
  print("Not evently divisible by 3.")
end
I think you have it backwards TC: if number%3 == 0, then it is divisible by three. But this is goofy anyway, because timer is a float, not an integer.

It doesn't make much sense why you're trying to figure out if the timer is divisible by three.
  1. If you want to have something execute every third update, then increment a temp var by one, and use TechnoCat's modulo.
  2. If you want something to happen over a period of time (let's say 1/3 of a second) then you need to keep adding up the dt's in a temp variable. When the variable is > 1/3 seconds, execute your command.
Missing Sentinel Software | Twitter

FORCIBLY IGNORED.
<leafo> when in doubt delete all of your code
<bartbes> git rm -r *
<bartbes> git commit -m "Fixed all bugs"
<bartbes> git push
User avatar
TechnoCat
Inner party member
Posts: 1611
Joined: Thu Jul 30, 2009 12:31 am
Location: Milwaukee, WI
Contact:

Re: I want to see if one number is divisible by another numb

Post by TechnoCat »

josefnpat wrote:I think you have it backwards TC: if number%3 == 0, then it is divisible by three.
Disregard me, I suck cocks. 8==> :o

In my defense I wrote it on the way out to the gym.
User avatar
Tesselode
Party member
Posts: 555
Joined: Fri Jul 23, 2010 7:55 pm

Re: I want to see if one number is divisible by another numb

Post by Tesselode »

How to find out if one number is divisible by another:

if n1/n2 = math.floor(n1/n2) then
--it is divisible
User avatar
slime
Solid Snayke
Posts: 3159
Joined: Mon Aug 23, 2010 6:45 am
Location: Nova Scotia, Canada
Contact:

Re: I want to see if one number is divisible by another numb

Post by slime »

Tesselode wrote:How to find out if one number is divisible by another:

if n1/n2 = math.floor(n1/n2) then
--it is divisible

Code: Select all

 if n1 % n2 == 0 then ...
......

As was already pointed out though, the OP shouldn't be finding out if a number is divisible, in this circumstance. :P
User avatar
Robin
The Omniscient
Posts: 6506
Joined: Fri Feb 20, 2009 4:29 pm
Location: The Netherlands
Contact:

Re: I want to see if one number is divisible by another numb

Post by Robin »

So, enderspike: the answer to your question is Technocat's updated answer, but what you really want is Ellohir's answer.
Help us help you: attach a .love.
User avatar
T-Bone
Inner party member
Posts: 1492
Joined: Thu Jun 09, 2011 9:03 am

Re: I want to see if one number is divisible by another numb

Post by T-Bone »

Also, if you want something to happen while the timer is in between a number divisible by three and the next, you can do this

Code: Select all

if math.floor(number)%3 == 0 then
    --whatever
end
number = number + dt
But it's stupid.
User avatar
kikito
Inner party member
Posts: 3153
Joined: Sat Oct 03, 2009 5:22 pm
Location: Madrid, Spain
Contact:

Re: I want to see if one number is divisible by another numb

Post by kikito »

enderspike wrote:I'm a noob trying to make a simple flexible timer like ...
Howdy!

Some time ago I made a lib just for that - cron.lua.

Here's how you install it:

Code: Select all

local cron = require 'cron'
...
function love.update(dt)
  cron.update(dt) -- you must put this line inside love.update
end
And here is how you use it to do the two examples you gave above:

Code: Select all

cron.every(3, function() thisis.happening end)
cron.every(0.5, function() thisis.moreoften end)
These calls can come from love.update, love.load, or any other place where the cron variable is defined.

You can use regular functions instead of anonymous ones:

Code: Select all

function thisis_happening()
  -- do stuff
end

cron.every(3, thisis_happening)
And you can also pass parameters to the functions, call events only once, and cancel events. I'm very happy with the result, please give it a look.
When I write def I mean function.
Post Reply

Who is online

Users browsing this forum: No registered users and 5 guests