Canned Turkey
Guest
|
|
« on: November 17, 2014, 07:33:58 PM » |
|
I know the rule, if the last digit is 0 or 5, but I need something that uses math, i.e.
if x/5 = 1 or 2 or 3 or 4 or 5... then do the thing else don't do that thing
But, I don't want to check the specific number, i.e.
if x = 5 or 10 or 15 or 20... then do the thing else don't do that thing
I just need a function that if the input is divisible by 5, than it returns true. I'm using GML, but basic math should translate anyway.
|
|
|
Logged
|
|
|
|
Glyph
|
|
« Reply #1 on: November 17, 2014, 07:42:50 PM » |
|
if ((x mod 5) == 0) ...
|
|
|
Logged
|
|
|
|
Canned Turkey
Guest
|
|
« Reply #2 on: November 17, 2014, 07:50:10 PM » |
|
if ((x mod 5) == 0) ...
Thank you!
|
|
|
Logged
|
|
|
|
|
Canned Turkey
Guest
|
|
« Reply #4 on: November 18, 2014, 10:40:35 AM » |
|
Thanks for letting me know, no wonder I was having a hard time finding anything other than hard math.
|
|
|
Logged
|
|
|
|
Columbo
|
|
« Reply #5 on: November 18, 2014, 11:39:51 AM » |
|
I suppose you could also do something like: if ((x/5)*5 == x) If your % key was broken or something
|
|
|
Logged
|
|
|
|
Glyph
|
|
« Reply #6 on: November 18, 2014, 11:59:20 AM » |
|
Yeah, or even if (abs(cos(x/5 * pi)) == 1) ...
|
|
|
Logged
|
|
|
|
oahda
|
|
« Reply #7 on: November 18, 2014, 12:03:06 PM » |
|
for (int i = 0; true; i += 5) if (x == i || x == -i) break;
|
|
|
Logged
|
|
|
|
Rarykos
|
|
« Reply #8 on: November 18, 2014, 12:33:44 PM » |
|
Yeah, or even if (abs(cos(x/5 * pi)) == 1) ... Wow, this must be the most complicated way! I'm so writing this on my next job interview when I'm asked this question
|
|
|
Logged
|
|
|
|
DocProctopus
|
|
« Reply #9 on: November 18, 2014, 01:12:59 PM » |
|
for (int i = 0; true; i += 5) if (x == i || x == -i) break; This will work but it's very inefficient. A single modulo operation is the best way to do this.
|
|
|
Logged
|
|
|
|
Marchal_Mig12
|
|
« Reply #10 on: November 18, 2014, 01:46:57 PM » |
|
Binary operations would be more efficient imo.
|
|
|
Logged
|
|
|
|
Christian Knudsen
|
|
« Reply #11 on: November 18, 2014, 02:09:48 PM » |
|
for (int i = 0; true; i += 5) if (x == i || x == -i) break; This will work but it's very inefficient. A single modulo operation is the best way to do this.
|
|
|
Logged
|
|
|
|
Sik
|
|
« Reply #12 on: November 18, 2014, 06:56:45 PM » |
|
Yeah, or even if (abs(cos(x/5 * pi)) == 1) ... ...I hate you. So much.
|
|
|
Logged
|
|
|
|
Gtoknu
|
|
« Reply #13 on: November 18, 2014, 07:25:41 PM » |
|
some go with human-thinking: int lastDigit = x % 10; if(lastDigit == 5 or lastDigit == 0) { functionCall(); } EDIT: By the way, I think this: http://en.wikipedia.org/wiki/Modulo_operation is far more informative for a beginner.
|
|
|
Logged
|
wut
|
|
|
alvarop
|
|
« Reply #14 on: November 18, 2014, 07:28:10 PM » |
|
Can anyone explain why anyone would use anything but modulo?
|
|
|
Logged
|
|
|
|
Gtoknu
|
|
« Reply #15 on: November 18, 2014, 07:30:47 PM » |
|
Can anyone explain why anyone would use anything but modulo?
We wouldn't. But it's funny to point out other ways to solve a problem.
|
|
|
Logged
|
wut
|
|
|
Sik
|
|
« Reply #16 on: November 18, 2014, 09:53:32 PM » |
|
Can anyone explain why anyone would use anything but modulo?
Reason #1: you don't know it even exists (which seems to be the case here), although I have to admit it's usually one of the first things you're taught (alongside + - * /) because of how easy and useful it is. Reason #2: your language doesn't have that operator (or you don't know if it does), you need to come up with a workaround in that case (I believe some Basic variants have this issue).
|
|
|
Logged
|
|
|
|
Kyle Preston
|
|
« Reply #17 on: November 18, 2014, 10:38:46 PM » |
|
Yeah, or even
if (abs(cos(x/5 * pi)) == 1) ... Just as an fyi, this will work with some languages/versions of languages but it's probably not a good practice to implement. As an example, this works with Python 2 point whatever, but in Python 3, fractions < 1 aren't rounded to 0 and hence will not give you the same as % 5. Just my two cents.
|
|
|
Logged
|
|
|
|
oahda
|
|
« Reply #18 on: November 18, 2014, 10:55:30 PM » |
|
Yeah, or even
if (abs(cos(x/5 * pi)) == 1) ... Just as an fyi, this will work with some languages/versions of languages but it's probably not a good practice to implement. As an example, this works with Python 2 point whatever, but in Python 3, fractions < 1 aren't rounded to 0 and hence will not give you the same as % 5. Just my two cents. if (abs(cos(round(x/5) * pi)) == 1) ...Just keep on adding those functions.
|
|
|
Logged
|
|
|
|
Ads
TIGBaby
|
|
« Reply #19 on: November 19, 2014, 12:54:33 AM » |
|
Another method: double mod(double value, double m) { return value - m * Math.floor(value / m); } Test output: -10 mod 3 = 2.0 -9 mod 3 = 0.0 -8 mod 3 = 1.0 -7 mod 3 = 2.0 -6 mod 3 = 0.0 -5 mod 3 = 1.0 -4 mod 3 = 2.0 -3 mod 3 = 0.0 -2 mod 3 = 1.0 -1 mod 3 = 2.0 0 mod 3 = 0.0 1 mod 3 = 1.0 2 mod 3 = 2.0 3 mod 3 = 0.0 4 mod 3 = 1.0 5 mod 3 = 2.0 6 mod 3 = 0.0 7 mod 3 = 1.0 8 mod 3 = 2.0 9 mod 3 = 0.0
|
|
|
Logged
|
|
|
|
|