I want to see if one number is divisible by another number
-
- Prole
- Posts: 10
- Joined: Tue Oct 11, 2011 6:37 pm
I want to see if one number is divisible by another number
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!
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!
- 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
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.
Re: I want to see if one number is divisible by another numb
It's not the modulo operation, but for what you want to do I usually go with this:
It's more code but I like having my counters split.
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
Last edited by Ellohir on Fri Nov 11, 2011 1:28 am, edited 1 time in total.
- 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
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.TechnoCat wrote:http://en.wikipedia.org/wiki/Modulo_operationCode: Select all
if number%3 then print("EVENLY DIVISIBLE BY 3!!!") else print("Not evently divisible by 3.") end
It doesn't make much sense why you're trying to figure out if the timer is divisible by three.
- If you want to have something execute every third update, then increment a temp var by one, and use TechnoCat's modulo.
- 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
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
- 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
Disregard me, I suck cocks. 8==>josefnpat wrote:I think you have it backwards TC: if number%3 == 0, then it is divisible by three.
In my defense I wrote it on the way out to the gym.
Re: I want to see if one number is divisible by another numb
How to find out if one number is divisible by another:
if n1/n2 = math.floor(n1/n2) then
--it is divisible
if n1/n2 = math.floor(n1/n2) then
--it is divisible
- slime
- Solid Snayke
- Posts: 3162
- 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
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.
- 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
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.
Re: I want to see if one number is divisible by another numb
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
But it's stupid.
Code: Select all
if math.floor(number)%3 == 0 then
--whatever
end
number = number + dt
My game called Hat Cat and the Obvious Crimes Against the Fundamental Laws of Physics is out now!
- 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
Howdy!enderspike wrote:I'm a noob trying to make a simple flexible timer like ...
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
Code: Select all
cron.every(3, function() thisis.happening end)
cron.every(0.5, function() thisis.moreoften end)
You can use regular functions instead of anonymous ones:
Code: Select all
function thisis_happening()
-- do stuff
end
cron.every(3, thisis_happening)
When I write def I mean function.
Who is online
Users browsing this forum: Ahrefs [Bot], Google [Bot] and 4 guests